001/* 002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Bogdan Stefanescu 018 * Florent Guillaume 019 */ 020package org.nuxeo.ecm.core.schema.types; 021 022import java.util.HashMap; 023import java.util.Map; 024import java.util.Set; 025 026import org.nuxeo.ecm.core.schema.Namespace; 027import org.nuxeo.ecm.core.schema.SchemaNames; 028import org.nuxeo.ecm.core.schema.types.constraints.Constraint; 029 030/** 031 * The implementation of a Schema 032 */ 033public class SchemaImpl extends ComplexTypeImpl implements Schema { 034 035 private static final long serialVersionUID = 1L; 036 037 private final Map<String, Type> types = new HashMap<String, Type>(); 038 039 /** 040 * Constructor for a schema. Its types (fields) are then added through {@link #registerType}. 041 */ 042 public SchemaImpl(String name, Namespace ns) { 043 super(null, SchemaNames.SCHEMAS, name, ns == null ? Namespace.DEFAULT_NS : ns); 044 } 045 046 /** 047 * Create a schema from a ComplexType 048 * 049 * @since 5.7 050 * @param complexType 051 * @param name 052 * @param ns 053 */ 054 public SchemaImpl(ComplexType complexType, String name, Namespace ns) { 055 super(null, SchemaNames.SCHEMAS, name, ns == null ? Namespace.DEFAULT_NS : ns); 056 if (complexType != null) { 057 for (Field field : complexType.getFields()) { 058 QName fieldname = QName.valueOf(field.getName().getLocalName(), ns.prefix); 059 Type type = field.getType(); 060 String defaultValue = type.encode(field.getDefaultValue()); 061 Set<Constraint> constraint = field.getConstraints(); 062 FieldImpl newField = new FieldImpl(fieldname, this, type, defaultValue, 0, constraint); 063 newField.setConstant(field.isConstant()); 064 addField(newField); 065 } 066 } 067 } 068 069 @Override 070 public Type getType(String typeName) { 071 return types.get(typeName); 072 } 073 074 @Override 075 public Type[] getTypes() { 076 return types.values().toArray(new Type[types.size()]); 077 } 078 079 @Override 080 public void registerType(Type type) { 081 types.put(type.getName(), type); 082 } 083 084 @Override 085 public String toString() { 086 return getClass().getSimpleName() + '(' + name + ')'; 087 } 088 089 @Override 090 public Schema getSchema() { 091 return this; 092 } 093 094}