View Javadoc
1   /*
2    * Copyright 2007 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.builder.conflict.strategy;
15  
16  
17  import org.exolab.castor.xml.schema.Annotated;
18  import org.exolab.castor.xml.schema.ElementDecl;
19  import org.exolab.castor.xml.schema.XMLType;
20  import org.exolab.javasource.JClass;
21  
22  /**
23   * Class name conflict resolver implementation, adding a By<Type> suffix to the suggested
24   * class name.
25   * 
26   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
27   */
28  public class TypeClassNameConflictResolver extends BaseClassNameConflictResolver
29      implements ClassNameConflictResolver {
30  
31    /**
32     * Changes the JClass' internal class name, appedning a type suffix to the suggested class name.
33     * 
34     * @param jClass The {@link JClass} instance whose local name should be changed.
35     * @param xpath XPATH expression used to defer the new local class name
36     * @param typedXPath typed XPATH expression used to defer the new local class name
37     * @param annotated {@link Annotated} instance
38     */
39    public void changeClassInfoAsResultOfConflict(final JClass jClass, final String xpath,
40        final String typedXPath, final Annotated annotated) {
41      String typeString = typedXPath.substring(typedXPath.indexOf("[") + 1, typedXPath.indexOf("]"));
42      if (annotated instanceof ElementDecl) {
43        ElementDecl element = (ElementDecl) annotated;
44        // keep the suggested names for global elements
45        if (element.getParent() == element.getSchema()) {
46          return;
47        }
48        // keep the suggested name for element references
49        if (element.isReference()) {
50          return;
51        }
52        // keep the suggested class name if the local element is of the same
53        // type as the global one
54        XMLType xmlType = element.getType();
55        ElementDecl globalElement = element.getSchema().getElementDecl(element.getName());
56        if (globalElement != null) {
57          XMLType globalElementType = globalElement.getType();
58          if (globalElementType.getName() != null
59              && globalElementType.getName().equals(xmlType.getName())) {
60            return;
61          }
62        }
63        if (xmlType.isComplexType() && xmlType.getName() == null) {
64          typeString = "/complexType:anon";
65        }
66      }
67      if (typeString.startsWith("/complexType:anon")) {
68        // set new classname
69        String newClassName = calculateXPathPrefix(xpath) + jClass.getLocalName();
70        jClass.changeLocalName(newClassName);
71      } else if (typeString.startsWith("/complexType:")) {
72        // set new classname
73        String newClassName =
74            jClass.getLocalName() + getSourceGenerator().getAutomaticConflictResolutionTypeSuffix()
75                + typeString.substring(typeString.indexOf(":") + 1);
76        jClass.changeLocalName(newClassName);
77      }
78    }
79  
80  }