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 public boolean isVersionWritabe; 040 041 /** 042 * Constructor for a schema. Its types (fields) are then added through {@link #registerType}. 043 */ 044 public SchemaImpl(String name, Namespace ns) { 045 this(name, ns, false); 046 } 047 048 public SchemaImpl(String name, Namespace ns, boolean isVersionWritabe) { 049 super(null, SchemaNames.SCHEMAS, name, ns == null ? Namespace.DEFAULT_NS : ns); 050 this.isVersionWritabe = isVersionWritabe; 051 } 052 053 /** 054 * Create a schema from a ComplexType 055 * 056 * @since 5.7 057 * @param complexType 058 * @param name 059 * @param ns 060 */ 061 public SchemaImpl(ComplexType complexType, String name, Namespace ns, boolean isVersionWritabe) { 062 super(null, SchemaNames.SCHEMAS, name, ns == null ? Namespace.DEFAULT_NS : ns); 063 this.isVersionWritabe = isVersionWritabe; 064 if (complexType != null) { 065 for (Field field : complexType.getFields()) { 066 QName fieldname = QName.valueOf(field.getName().getLocalName(), ns.prefix); 067 Type type = field.getType(); 068 String defaultValue = type.encode(field.getDefaultValue()); 069 Set<Constraint> constraint = field.getConstraints(); 070 FieldImpl newField = new FieldImpl(fieldname, this, type, defaultValue, 0, constraint); 071 newField.setConstant(field.isConstant()); 072 addField(newField); 073 } 074 } 075 } 076 077 @Override 078 public Type getType(String typeName) { 079 return types.get(typeName); 080 } 081 082 @Override 083 public Type[] getTypes() { 084 return types.values().toArray(new Type[types.size()]); 085 } 086 087 @Override 088 public void registerType(Type type) { 089 types.put(type.getName(), type); 090 } 091 092 @Override 093 public String toString() { 094 return getClass().getSimpleName() + '(' + name + ')'; 095 } 096 097 @Override 098 public Schema getSchema() { 099 return this; 100 } 101 102 @Override 103 public boolean isVersionWritabe() { 104 return isVersionWritabe; 105 } 106 107}