1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.exolab.castor.builder.binding;
17
18 import org.exolab.castor.xml.schema.AttributeDecl;
19 import org.exolab.castor.xml.schema.ComplexType;
20 import org.exolab.castor.xml.schema.ElementDecl;
21 import org.exolab.castor.xml.schema.Group;
22 import org.exolab.castor.xml.schema.ModelGroup;
23 import org.exolab.castor.xml.schema.Schema;
24 import org.exolab.castor.xml.schema.SimpleType;
25 import org.exolab.castor.xml.schema.Structure;
26
27
28
29
30
31
32
33 public class XPathHelper {
34
35
36
37
38
39 private static final int INITIAL_XPATH_SIZE = 50;
40
41
42
43
44
45
46
47 public static void getSchemaLocation(
48 final Structure structure,
49 final StringBuffer location) {
50 getSchemaLocation(structure, location, false);
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public static void getSchemaLocation(
66 final Structure structure,
67 final StringBuffer location,
68 final boolean dealWithAnonTypes) {
69 if (structure == null) {
70 throw new IllegalArgumentException("Structure cannot be null");
71 }
72
73 if (location == null) {
74 throw new IllegalArgumentException("location cannot be null");
75 }
76
77 Structure parent = null;
78 switch (structure.getStructureType()) {
79 case Structure.ELEMENT:
80 parent = ((ElementDecl) structure).getParent();
81 if (parent.getStructureType() != Structure.SCHEMA) {
82 getSchemaLocation(parent, location, dealWithAnonTypes);
83 }
84 location.append(ExtendedBinding.PATH_SEPARATOR);
85 location.append(((ElementDecl) structure).getName());
86 if (parent instanceof Schema) {
87 Schema schema = (Schema) parent;
88 if (schema.getTargetNamespace() != null) {
89 String targetNamespace = schema.getTargetNamespace();
90 location.append("{" + targetNamespace + "}");
91 }
92 }
93 break;
94
95 case Structure.COMPLEX_TYPE:
96 ComplexType complexType = (ComplexType) structure;
97 parent = (complexType).getParent();
98 if (parent.getStructureType() != Structure.SCHEMA) {
99 getSchemaLocation(parent, location, dealWithAnonTypes);
100 }
101 if (complexType.getName() != null) {
102 location.append(ExtendedBinding.PATH_SEPARATOR);
103 location.append(ExtendedBinding.COMPLEXTYPE_ID);
104 location.append(((ComplexType) structure).getName());
105 }
106
107
108
109
110
111 break;
112
113 case Structure.SIMPLE_TYPE:
114 SimpleType simpleType = (SimpleType) structure;
115 parent = simpleType.getParent();
116 if (parent != null && parent.getStructureType() != Structure.SCHEMA) {
117 getSchemaLocation(parent, location, dealWithAnonTypes);
118 }
119
120 if (parent != null && simpleType.getName() != null) {
121 location.append(ExtendedBinding.PATH_SEPARATOR);
122 location.append(ExtendedBinding.ENUMTYPE_ID);
123 location.append(((SimpleType) structure).getName());
124 }
125
126
127
128
129
130 break;
131
132 case Structure.MODELGROUP:
133 ModelGroup group = (ModelGroup) structure;
134 parent = group.getParent();
135 if (parent.getStructureType() != Structure.SCHEMA) {
136 getSchemaLocation(parent, location, dealWithAnonTypes);
137 }
138 if (group.getName() != null) {
139 location.append(ExtendedBinding.PATH_SEPARATOR);
140 location.append(ExtendedBinding.GROUP_ID);
141 location.append(group.getName());
142 }
143 break;
144
145 case Structure.ATTRIBUTE:
146 parent = ((AttributeDecl) structure).getParent();
147 if (parent.getStructureType() != Structure.SCHEMA) {
148 getSchemaLocation(parent, location, dealWithAnonTypes);
149 }
150 location.append(ExtendedBinding.PATH_SEPARATOR);
151 location.append(ExtendedBinding.ATTRIBUTE_PREFIX);
152 location.append(((AttributeDecl) structure).getName());
153 break;
154
155 case Structure.GROUP:
156
157 getSchemaLocation(((Group) structure).getParent(), location, dealWithAnonTypes);
158 break;
159
160
161
162
163 default:
164 break;
165 }
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public static String getSchemaLocation(final Structure structure,
203 final boolean dealWithAnonTypes) {
204 if (structure == null) {
205 return null;
206 }
207 StringBuffer buffer = new StringBuffer(INITIAL_XPATH_SIZE);
208 getSchemaLocation(structure, buffer, dealWithAnonTypes);
209 return buffer.toString();
210 }
211
212
213
214
215
216
217
218
219
220 public static String getSchemaLocation(final Structure structure) {
221 if (structure == null) {
222 return null;
223 }
224 StringBuffer buffer = new StringBuffer(INITIAL_XPATH_SIZE);
225 getSchemaLocation(structure, buffer, false);
226 return buffer.toString();
227 }
228
229 }