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  import java.util.StringTokenizer;
17  
18  import org.exolab.castor.builder.SourceGenerator;
19  import org.exolab.castor.builder.binding.ExtendedBinding;
20  import org.exolab.castor.xml.schema.Annotated;
21  import org.exolab.javasource.JClass;
22  
23  /**
24   * Base class for class name conflict resolver implementations.
25   * 
26   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
27   */
28  public abstract class BaseClassNameConflictResolver implements ClassNameConflictResolver {
29  
30    /**
31     * Calling {@link SourceGenerator} instance, used to retrieve configuration information.
32     */
33    private SourceGenerator _sourceGenerator;
34  
35    /**
36     * Changes the JClass' internal class name, as a result of an XPATH expression uniquely
37     * identifying an XML artefact within an XML schema.
38     * 
39     * @param jClass The {@link JClass} instance whose local name should be changed.
40     * @param xpath XPATH expression used to defer the new local class name
41     * @param typedXPath typed XPATH expression used to defer the new local class name
42     * @param annotated {@link Annotated} instance
43     */
44    public abstract void changeClassInfoAsResultOfConflict(final JClass jClass, final String xpath,
45        final String typedXPath, final Annotated annotated);
46  
47  
48    /**
49     * Calculate XPath prefix.
50     * 
51     * @param xpath The XPath to be transformed into a class name prefix
52     * @return The class name prefix to use.
53     */
54    protected String calculateXPathPrefix(final String xpath) {
55      String prefix = "";
56      StringTokenizer stringTokenizer = new StringTokenizer(xpath, "/.");
57      while (stringTokenizer.hasMoreTokens()) {
58        String token = stringTokenizer.nextToken();
59        // break on last token
60        if (!stringTokenizer.hasMoreTokens()) {
61          break;
62        }
63        if (token.startsWith(ExtendedBinding.COMPLEXTYPE_ID)
64            || token.startsWith(ExtendedBinding.SIMPLETYPE_ID)
65            || token.startsWith(ExtendedBinding.ENUMTYPE_ID)
66            || token.startsWith(ExtendedBinding.GROUP_ID)) {
67          token = token.substring(token.indexOf(":") + 1);
68        }
69        prefix += _sourceGenerator.getJavaNaming().toJavaClassName(token);
70      }
71      return prefix;
72    }
73  
74    /**
75     * Sets the calling {@link SourceGenerator} instance.
76     * 
77     * @param sourceGenerator The calling {@link SourceGenerator} instance.
78     */
79    public void setSourceGenerator(final SourceGenerator sourceGenerator) {
80      _sourceGenerator = sourceGenerator;
81    }
82  
83  
84    /**
85     * Returns the calling {@link SourceGenerator} instance.
86     * 
87     * @return the calling {@link SourceGenerator} instance
88     */
89    protected SourceGenerator getSourceGenerator() {
90      return _sourceGenerator;
91    }
92  
93  }