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