View Javadoc
1   /*
2    * Copyright 2007 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.exolab.castor.builder.conflict.strategy;
17  
18  import java.util.StringTokenizer;
19  
20  import org.exolab.castor.builder.SourceGenerator;
21  import org.exolab.castor.builder.binding.ExtendedBinding;
22  import org.exolab.castor.xml.schema.Annotated;
23  import org.exolab.javasource.JClass;
24  
25  /**
26   * Base class for class name conflict resolver implementations.
27   * 
28   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttmann</a>
29   */
30  public abstract class BaseClassNameConflictResolver implements ClassNameConflictResolver {
31      
32      /**
33       * Calling {@link SourceGenerator} instance, used to retrieve configuration
34       * information.
35       */
36      private SourceGenerator _sourceGenerator;
37  
38      /**
39       * Changes the JClass' internal class name, as a result of an XPATH
40       * expression uniquely identifying an XML artefact within an XML schema.
41       * 
42       * @param jClass
43       *            The {@link JClass} instance whose local name should be
44       *            changed.
45       * @param xpath
46       *            XPATH expression used to defer the new local class name
47       * @param typedXPath
48       *            typed XPATH expression used to defer the new local class name
49       * @param annotated {@link Annotated} instance
50       */
51      public abstract void changeClassInfoAsResultOfConflict(final JClass jClass,
52              final String xpath, final String typedXPath, final Annotated annotated);
53      
54  
55      /**
56       * Calculate XPath prefix.
57       * @param xpath The XPath to be transformed into a class name prefix
58       * @return The class name prefix to use.
59       */
60      protected String calculateXPathPrefix(final String xpath) {
61          String prefix = "";
62          StringTokenizer stringTokenizer = new StringTokenizer(xpath, "/.");
63          while (stringTokenizer.hasMoreTokens()) {
64              String token = stringTokenizer.nextToken();
65              // break on last token
66              if (!stringTokenizer.hasMoreTokens()) {
67                  break;
68              }
69              if (token.startsWith(ExtendedBinding.COMPLEXTYPE_ID)
70                      || token.startsWith(ExtendedBinding.SIMPLETYPE_ID)
71                      || token.startsWith(ExtendedBinding.ENUMTYPE_ID)
72                      || token.startsWith(ExtendedBinding.GROUP_ID)) {
73                  token = token.substring(token.indexOf(":") + 1);
74              }
75              prefix += _sourceGenerator.getJavaNaming().toJavaClassName(token);
76          }
77          return prefix;
78      }
79      
80      /**
81       * Sets the calling {@link SourceGenerator} instance.
82       * @param sourceGenerator The calling {@link SourceGenerator} instance.
83       */
84      public void setSourceGenerator(final SourceGenerator sourceGenerator) {
85          _sourceGenerator = sourceGenerator;
86      }
87  
88  
89      /**
90       * Returns the calling {@link SourceGenerator} instance.
91       * @return the calling {@link SourceGenerator} instance
92       */
93      protected SourceGenerator getSourceGenerator() {
94          return _sourceGenerator;
95      }
96  
97  }