This project has retired. For details please refer to its
Attic page.
TypeValidator xref
1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing,13 * software distributed under the License is distributed on an14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 * KIND, either express or implied. See the License for the16 * specific language governing permissions and limitations17 * under the License.18 */19package org.apache.chemistry.opencmis.inmemory;
2021import java.util.Collection;
22import java.util.Iterator;
2324import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
25import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
26import org.apache.chemistry.opencmis.commons.definitions.TypeDefinitionContainer;
27import org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException;
28import org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
29import org.apache.chemistry.opencmis.server.support.TypeManager;
3031/**32 * A helper class doing some consistency checks when new type definitions are added to the system33 * 34 * @author Jens35 *36 */37publicclassTypeValidator {
3839publicstaticvoid checkType(TypeManager tm, TypeDefinition td) {
4041if (null == tm.getTypeById(td.getParentTypeId())) {
42thrownew CmisInvalidArgumentException("Cannot add type, because parent with id " + td.getParentTypeId()
43 + " does not exist.");
44 }
4546 checkTypeId(tm, td.getId());
47 checkTypeQueryName(tm, td.getQueryName());
48 checkTypeLocalName(tm, td.getLocalName());
49 }
5051publicstaticvoid checkProperties(TypeManager tm, Collection<PropertyDefinition<?>> pds) {
5253 Collection<TypeDefinitionContainer> tdl = tm.getTypeDefinitionList();
54for (PropertyDefinition<?> pd2 : pds) {
55// check id syntax56if (null == pd2.getId())
57thrownew CmisInvalidArgumentException("property id cannot be null.");
58if (!NameValidator.isValidId(pd2.getId()))
59thrownew CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
6061// check query name syntax62if (null == pd2.getQueryName())
63thrownew CmisInvalidArgumentException("property query name cannot be null.");
64if (!NameValidator.isValidQueryName(pd2.getQueryName()))
65thrownew CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
6667// check local name syntax68if (null == pd2.getLocalName())
69thrownew CmisInvalidArgumentException("property local name cannot be null.");
70if (!NameValidator.isValidLocalName(pd2.getLocalName()))
71thrownew CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
7273for (TypeDefinitionContainer tdc : tdl) {
74 TypeDefinition td = tdc.getTypeDefinition();
75for (PropertyDefinition<?> pd1 : td.getPropertyDefinitions().values()) {
76// check if id is used77if (pd1.getId().equals(pd2.getId()))
78thrownew CmisConstraintException("Property id " + pd2.getId() + " already in use in type "79 + td.getId());
80// check if query name is used81if (pd1.getQueryName().equals(pd2.getQueryName()))
82thrownew CmisConstraintException("Property query name " + pd2.getQueryName() + " already in use in type "83 + td.getQueryName());
84// check if local name is used85if (pd1.getLocalName().equals(pd2.getLocalName()))
86thrownew CmisConstraintException("Property local name " + pd2.getLocalName() + " already in use in type "87 + td.getId());
88 }
89 }
90 }
91 }
9293privatestaticvoid checkTypeId(TypeManager tm, String typeId) {
9495if (null == typeId) {
96thrownew CmisInvalidArgumentException("Type id cannot be null.");
97 }
9899// check name syntax100if (!NameValidator.isValidId(typeId)) {
101thrownew CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_ID);
102 }
103104if (null != tm.getTypeById(typeId)) {
105thrownew CmisInvalidArgumentException("You cannot add type with id " + typeId
106 + " because it already exists.");
107 }
108 }
109110privatestaticvoid checkTypeQueryName(TypeManager tm, String queryName) {
111112if (null == queryName) {
113thrownew CmisInvalidArgumentException("Query name cannot be null.");
114 }
115116if (!NameValidator.isValidQueryName(queryName)) {
117thrownew CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
118 }
119120// check set query name is unique in the type system121if (null != tm.getTypeByQueryName(queryName)) {
122thrownew CmisInvalidArgumentException("You cannot add type with query name " + queryName
123 + " because it already exists.");
124 }
125 }
126127privatestaticvoid checkTypeLocalName(TypeManager tm, String localName) {
128129if (null == localName) {
130thrownew CmisInvalidArgumentException("Local name cannot be null.");
131 }
132133if (!NameValidator.isValidLocalName(localName)) {
134thrownew CmisInvalidArgumentException(NameValidator.ERROR_ILLEGAL_NAME);
135 }
136137for (TypeDefinitionContainer tdc : tm.getTypeDefinitionList()) {
138if (tdc.getTypeDefinition().getLocalName().equals(localName))
139thrownew CmisConstraintException("You cannot add type with local name " + localName
140 + " because it already exists.");
141 }
142 }
143144 }