1 /*
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45 package org.exolab.castor.builder;
46
47 import java.util.Hashtable;
48
49 import org.castor.xml.JavaNaming;
50 import org.exolab.castor.xml.schema.ComplexType;
51 import org.exolab.castor.xml.schema.ElementDecl;
52 import org.exolab.castor.xml.schema.Group;
53 import org.exolab.castor.xml.schema.ModelGroup;
54 import org.exolab.castor.xml.schema.Structure;
55
56 /***
57 * A simple class used for creating class names for unnamed Groups in XML Schema.
58 *
59 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
60 * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
61 */
62 public final class GroupNaming {
63
64 /**
65 * A HashTable of HashTables that keeps track of group names for each
66 * package.
67 */
68 private Hashtable<String, Hashtable<Group, String>> _packageGroupNames = null;
69
70 /**
71 * JavaNaming to be used.
72 * @since 1.1.3
73 */
74 private JavaNaming _javaNaming;
75
76
77 /**
78 * Creates a new GroupNaming.
79 * @param javaNaming the {@link JavaNaming} to be used
80 */
81 public GroupNaming(final JavaNaming javaNaming) {
82 _packageGroupNames = new Hashtable<String, Hashtable<Group, String>>();
83 // JavaNaming was introduced @since 1.1.3
84 _javaNaming = javaNaming;
85 } //-- GroupNaming
86
87 /**
88 * Returns the qualified group name for a given Group instance and a package
89 * name; null if there isn't one already.
90 *
91 * @param group A Group instance
92 * @param packageName A package name
93 * @return The fully qualified group name for the Group instance/package
94 * name combination
95 */
96 private String getGroupName(final Group group, final String packageName) {
97 Hashtable<Group, String> groupNames = _packageGroupNames.get(packageName);
98 if (groupNames == null) {
99 return null;
100 }
101 return groupNames.get(group);
102 }
103
104 /**
105 * Sets the qualified group name for a given Group instance and package
106 * name.
107 *
108 * @param group A Group instance
109 * @param packageName A package name
110 * @param name The fully qualified group name for the Group instance/package
111 * name combination
112 */
113 private void putGroupName(final Group group, final String packageName, final String name) {
114 Hashtable<Group, String> groupNames = _packageGroupNames.get(packageName);
115 if (groupNames == null) {
116 groupNames = new Hashtable<Group, String>();
117 _packageGroupNames.put(packageName, groupNames);
118 }
119 groupNames.put(group, name);
120 }
121
122 /**
123 * Checks whether for a given group and package name combination an entry
124 * already exists.
125 *
126 * @param packageName A package name
127 * @param name A group name.
128 * @return True if a mapping already exists
129 */
130 private boolean containsGroupName(final String packageName, final String name) {
131 Hashtable<Group, String> groupNames = _packageGroupNames.get(packageName);
132 if (groupNames == null) {
133 return false;
134 }
135 return groupNames.contains(name);
136 }
137
138 /**
139 * Creates a class name for the given Group. A null value will be returned
140 * if a name cannot be created for the group.
141 *
142 * @param group the group to create a class name for
143 * @param packageName the package name for this group
144 * @return the class name for the given Group.
145 */
146 public String createClassName(final Group group, final String packageName) {
147 String name = group.getName();
148 if (name != null) {
149 return _javaNaming.toJavaClassName(name);
150 }
151 name = getGroupName(group, packageName);
152 if (name != null) {
153 return name;
154 }
155
156 Structure parent = group.getParent();
157 if (parent == null) {
158 return null;
159 }
160
161 boolean addOrder = true;
162 switch(parent.getStructureType()) {
163 case Structure.GROUP:
164 name = createClassName((Group) parent, packageName);
165 break;
166 case Structure.MODELGROUP:
167 name = ((ModelGroup) parent).getName();
168 name = _javaNaming.toJavaClassName(name);
169 addOrder = false;
170 break;
171 case Structure.COMPLEX_TYPE:
172 name = getClassName((ComplexType) parent);
173 addOrder = false;
174 break;
175 default:
176 break;
177 }
178
179 if (name != null) {
180 if (addOrder) {
181 String order = group.getOrder().toString();
182 name += _javaNaming.toJavaClassName(order);
183 }
184
185 int count = 2;
186 String tmpName = name;
187 while (containsGroupName(packageName, name)) {
188 name = tmpName + count;
189 ++count;
190 }
191 putGroupName(group, packageName, name);
192 }
193 return name;
194 } //-- createClassName
195
196 /**
197 * Returns the class name for the given ComplexType.
198 * <p>
199 * If the ComplexType instance is named, simply return the name of the
200 * ComplexType.
201 * <p>
202 * If it is not named (in other words, if it is an anonymous ComplexType
203 * definition), check for the name of the containing element (definition).
204 *
205 * @param complexType the ComplexType for which to return a class name
206 * @return the class name for the given ComplexType
207 */
208 private String getClassName(final ComplexType complexType) {
209 //-- if complexType has name, simply return it
210 String name = complexType.getName();
211 if (name != null) {
212 return _javaNaming.toJavaClassName(name);
213 }
214
215 //-- otherwise get name of containing element
216 Structure parent = complexType.getParent();
217 if (parent != null && parent.getStructureType() == Structure.ELEMENT) {
218 name = ((ElementDecl) parent).getName();
219 }
220
221 if (name != null) {
222 name = _javaNaming.toJavaClassName(name);
223 }
224 return name;
225 } //-- getName
226
227 } //-- class GroupNaming