comparison WebServiceToolWorkflow/lib/SAWADLParser/src/edu/uga/cs/lsdis/meteors/wadls/util/SchemaUtils.java @ 0:d5cd409b8a18 default tip

Migrated tool version 1.0.0 from old tool shed archive to new tool shed repository
author ganjoo
date Tue, 07 Jun 2011 18:00:50 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d5cd409b8a18
1 /*
2 * Created on Apr 10, 2005
3 *
4 */
5 package edu.uga.cs.lsdis.meteors.wadls.util;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.wadls.Grammars;
13 import javax.wadls.Params;
14 import javax.wadls.WADLSException;
15 import javax.wadls.extensions.schema.Schema;
16 import javax.xml.namespace.QName;
17
18 import org.w3c.dom.DOMException;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22
23 import edu.uga.cs.lsdis.meteors.wadls.Constants;
24 import edu.uga.cs.lsdis.meteors.wadls.extensions.schema.SchemaConstants;
25 import edu.uga.cs.lsdis.meteors.wadls.util.xml.QNameUtils;
26 import edu.uga.cs.lsdis.meteors.wadls.util.xml.XPathUtils;
27
28 /**
29 * This file is a collection of utilities which find the location of a XML/XSD Element by using the given path.
30 * @author Zixin Wu
31 *
32 */
33
34 public class SchemaUtils {
35
36 /**
37 * Search an XML element in the schemas contained in the given Types.
38 * @param types Search in the schemas contained in this WSDLS Types.
39 * @param tagName XML Element name of the desired element.
40 * @param elementName The value of the attribute "name" of the desired element.
41 * @return An XML element whose XML Element name is "tagName" and whose attribute "name" is "elementName".
42 * @throws WADLSException
43 */
44 public static Element findXMLEleInSchemas(Grammars grammars, String tagName, QName elementName) throws WADLSException{
45 Map schemas = grammars.getSchemas();
46 Iterator it = schemas.values().iterator();
47 while(it.hasNext()){
48 Schema schema = (Schema)(it.next());
49 Element schemaEle = schema.getElement();
50 if (schemaEle.getAttribute(Constants.ATTR_TARGET_NAMESPACE).equals(elementName.getNamespaceURI())){
51 Element foundEle = findXMLEleByName(schemaEle, tagName, elementName);
52 if (foundEle != null)
53 return foundEle;
54 }
55 }
56 return null;
57 }
58
59 /**
60 * Search the element whose XML Element name is "tagName" and whose attribute "name" is "elementName",
61 * in the startElement, search in only one level depth.
62 * @param startElement Start searching from this XML Element.
63 * @param tagName XML Element name of the desired element.
64 * @param elementName The value of the attribute "name" of the desired element.
65 * @return An XML element whose XML Element name is "tagName" and whose attribute "name" is "elementName".
66 * @throws WADLSException
67 */
68 public static Element findXMLEleByName(Element startElement, String tagName, QName elementName) throws WADLSException{
69 NodeList nodeList = startElement.getChildNodes();
70 //find the element with the required name as an attribute
71 int listLength = nodeList.getLength();
72 for(int i=0;i<listLength;i++){
73 Node currentNode = nodeList.item(i);
74 if (currentNode.getNodeType() != Node.ELEMENT_NODE) continue;
75 Element currentElement = (Element)nodeList.item(i);
76 //get the "name" attribute
77 String nameAttr = currentElement.getAttribute(SchemaConstants.ATTR_NAME);
78 QName qNameAttr = QNameUtils.getQName(nameAttr, currentElement);
79 //compare both namespaceURI and localname of the "name" attribute
80 if (elementName == null || qNameAttr.equals(elementName)){
81 //compare localname of the tag, and check namespace of the tag is XSD
82 QName currentTagQName = QNameUtils.getQName(currentElement.getTagName(), currentElement);
83 if (SchemaConstants.XSD_STR_LIST.contains(currentTagQName.getNamespaceURI())
84 && tagName.equals(currentTagQName.getLocalPart())){
85 //this is the element we are looking for.
86 return currentElement;
87 }
88 }
89 }
90 return null;
91 }
92
93
94 /**
95 * Return a list of XSD elements contained in the startElement (XSD Element), search in only 1 level depth.
96 * @param startElement Start search from this XSD element.
97 * @param types WSDLS Types
98 * @return A list of XSD elements contained in the startElement. If no XSD element is contained, the list has 0 element.
99 * @throws WADLSException
100 */
101 public static List listXSDElesInEle(Element startElement, Grammars grammars) throws WADLSException{
102 String refAttr = startElement.getAttribute(SchemaConstants.ATTR_REF);
103 if (refAttr != ""){
104 //look for the referenced element
105 QName elementQName = QNameUtils.getQName(refAttr, startElement);
106 Element refenecedElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_ELEMENT, elementQName);
107 if (refenecedElement != null)
108 return listXSDElesInEle(refenecedElement, grammars);
109 else{
110 throw new DOMException(DOMException.NOT_FOUND_ERR, "Cannot find referenced element");
111 }
112 }
113 else{
114 Element complexElement = null;
115 String typeAttr = startElement.getAttribute(SchemaConstants.ATTR_TYPE);
116 if (typeAttr != ""){
117 //get the QName of typeAttr
118 QName typeQName = QNameUtils.getQName(typeAttr, startElement);
119 if (SchemaConstants.XSD_STR_LIST.contains(typeQName.getNamespaceURI()))
120 //primitive data type
121 return new ArrayList();
122 else{
123 //ComplexType or SimpleType
124 complexElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_COMPLEXTYPE, typeQName);
125 if (complexElement == null)
126 //simpleType
127 return new ArrayList();;
128 }
129 }
130 else{
131 //No type, look for complexType in the subNodes
132 complexElement = findXMLEleByName(startElement, SchemaConstants.ELEM_COMPLEXTYPE, null);
133 if (complexElement == null){
134 WADLSException wsdlsExc = new WADLSException(WADLSException.NOT_FOUND_ELE_BY_PATH,
135 "cannot find complexType matching the path");
136 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(startElement));
137 throw wsdlsExc;
138 }
139 }
140
141 //we got the complexType element so far.
142 //we need to collect all the elements
143 return listXSDElesInComplexType(complexElement);
144 }
145 }
146
147 public static List listXSDElesInEle(Element startElement, Params params) throws WADLSException{
148 /*String refAttr = startElement.getAttribute(SchemaConstants.ATTR_REF);
149 if (refAttr != ""){
150 //look for the referenced element
151 QName elementQName = QNameUtils.getQName(refAttr, startElement);
152 Element refenecedElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_ELEMENT, elementQName);
153 if (refenecedElement != null)
154 return listXSDElesInEle(refenecedElement, grammars);
155 else{
156 throw new DOMException(DOMException.NOT_FOUND_ERR, "Cannot find referenced element");
157 }
158 }
159 else{
160 Element complexElement = null;
161 String typeAttr = startElement.getAttribute(SchemaConstants.ATTR_TYPE);
162 if (typeAttr != ""){
163 //get the QName of typeAttr
164 QName typeQName = QNameUtils.getQName(typeAttr, startElement);
165 if (SchemaConstants.XSD_STR_LIST.contains(typeQName.getNamespaceURI()))
166 //primitive data type
167 return new ArrayList();
168 else{
169 //ComplexType or SimpleType
170 complexElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_COMPLEXTYPE, typeQName);
171 if (complexElement == null)
172 //simpleType
173 return new ArrayList();;
174 }
175 }
176 else{
177 //No type, look for complexType in the subNodes
178 complexElement = findXMLEleByName(startElement, SchemaConstants.ELEM_COMPLEXTYPE, null);
179 if (complexElement == null){
180 WADLSException wsdlsExc = new WADLSException(WADLSException.NOT_FOUND_ELE_BY_PATH,
181 "cannot find complexType matching the path");
182 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(startElement));
183 throw wsdlsExc;
184 }
185 }
186
187 //we got the complexType element so far.
188 //we need to collect all the elements
189 return listXSDElesInComplexType(complexElement);
190 }*/
191
192 List temp = null;
193 temp.set(1, startElement);
194 return temp;
195
196 }
197
198 /**
199 * Return a list of XSD elements contained in the complexElement (XSD complexType), search in only 1 level depth.
200 * @param complexElement Start search from this XSD complexType.
201 * @return A list of XSD elements contained in the startElement. If no XSD element is contained, the list has 0 element.
202 * @throws WADLSException
203 */
204 public static List listXSDElesInComplexType(Element complexElement) throws WADLSException{
205 List subElements = new ArrayList();
206
207 NodeList subNodes = complexElement.getChildNodes();
208 int length = subNodes.getLength();
209 for(int i=0;i<length;i++){
210 Node currentNode = subNodes.item(i);
211 if (currentNode.getNodeType() != Node.ELEMENT_NODE) continue;
212 Element currentElement = (Element)currentNode;
213 String tagName = currentElement.getTagName();
214 QName tagQName = QNameUtils.getQName(tagName, currentElement);
215 String localPart = tagQName.getLocalPart();
216 if (!SchemaConstants.XSD_STR_LIST.contains(tagQName.getNamespaceURI()))
217 continue;
218 if (localPart.equals(SchemaConstants.ELEM_ELEMENT))
219 subElements.add(currentElement);
220 else if
221 (localPart.equals(SchemaConstants.ELEM_ALL)
222 || localPart.equals(SchemaConstants.ELEM_SEQUENCE)
223 || localPart.equals(SchemaConstants.ELEM_CHOICE)
224 || localPart.equals(SchemaConstants.ELEM_COMPLEXCONTENT)
225 || localPart.equals(SchemaConstants.ELEM_RESTRICTION)
226 )
227 //Just skip it, call this method recursively.
228 subElements.addAll(listXSDElesInComplexType(currentElement));
229 }
230 return subElements;
231 }
232
233 /**
234 * Search an XSD element located by the startElement and path.
235 * @param startElement Start search from this XSD element.
236 * @param path
237 * @param types WSDLS Types
238 * @return An XSD element
239 * @throws WADLSException
240 */
241 public static Element findXSDEleOnEle(Element startElement, String path, Grammars grammars) throws WADLSException{
242 String refAttr = startElement.getAttribute(SchemaConstants.ATTR_REF);
243 if (refAttr != ""){
244 //look for the referenced element
245 QName elementQName = QNameUtils.getQName(refAttr, startElement);
246 Element refenecedElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_ELEMENT, elementQName);
247 if (refenecedElement != null)
248 return findXSDEleOnEle(refenecedElement, path, grammars);
249 else{
250 throw new DOMException(DOMException.NOT_FOUND_ERR, "Cannot find referenced element");
251 }
252 }
253 else{
254 if (path == "")
255 return startElement;
256
257 String typeAttr = startElement.getAttribute(SchemaConstants.ATTR_TYPE);
258 if (typeAttr != ""){ //<... type="...">
259 //get the QName of typeAttr
260 QName typeQName = QNameUtils.getQName(typeAttr, startElement);
261 if (SchemaConstants.XSD_STR_LIST.contains(typeQName.getNamespaceURI())){
262 //primitive data type
263 WADLSException wsdlsExc = new WADLSException(WADLSException.PATH_ERROR,
264 "Primitive type cannot has path");
265 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(startElement));
266 throw wsdlsExc;
267 }
268 else{
269 //find the complexType with the given type QName
270 Element complexTypeElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_COMPLEXTYPE, typeQName);
271 if (complexTypeElement != null)
272 return findXSDEleOnComplexType(complexTypeElement, path, grammars);
273 else
274 //cannot find in complexType
275 throw new DOMException(DOMException.NOT_FOUND_ERR, "cannot find complexType by the given path");
276 }
277 }
278 else{
279 //No type, look for complexType in the subNodes
280 Element complexElement = findXMLEleByName(startElement, SchemaConstants.ELEM_COMPLEXTYPE, null);
281 if (complexElement != null)
282 return findXSDEleOnComplexType(complexElement, path, grammars);
283 else{
284 WADLSException wsdlsExc = new WADLSException(WADLSException.NOT_FOUND_ELE_BY_PATH,
285 "cannot find complexType matching the path");
286 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(startElement));
287 throw wsdlsExc;
288 }
289 }
290 }
291 }
292
293 public static Element findXSDEleOnEle(Element startElement, String path, Params params) throws WADLSException{
294 /*String refAttr = startElement.getAttribute(SchemaConstants.ATTR_REF);
295 if (refAttr != ""){
296 //look for the referenced element
297 QName elementQName = QNameUtils.getQName(refAttr, startElement);
298 Element refenecedElement = findXMLEleInSchemas(params, SchemaConstants.ELEM_ELEMENT, elementQName);
299 if (refenecedElement != null)
300 return findXSDEleOnEle(refenecedElement, path, grammars);
301 else{
302 throw new DOMException(DOMException.NOT_FOUND_ERR, "Cannot find referenced element");
303 }
304 }
305 else{
306 if (path == "")
307 return startElement;
308
309 String typeAttr = startElement.getAttribute(SchemaConstants.ATTR_TYPE);
310 if (typeAttr != ""){ //<... type="...">
311 //get the QName of typeAttr
312 QName typeQName = QNameUtils.getQName(typeAttr, startElement);
313 if (SchemaConstants.XSD_STR_LIST.contains(typeQName.getNamespaceURI())){
314 //primitive data type
315 WADLSException wsdlsExc = new WADLSException(WADLSException.PATH_ERROR,
316 "Primitive type cannot has path");
317 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(startElement));
318 throw wsdlsExc;
319 }
320 else{
321 //find the complexType with the given type QName
322 Element complexTypeElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_COMPLEXTYPE, typeQName);
323 if (complexTypeElement != null)
324 return findXSDEleOnComplexType(complexTypeElement, path, grammars);
325 else
326 //cannot find in complexType
327 throw new DOMException(DOMException.NOT_FOUND_ERR, "cannot find complexType by the given path");
328 }
329 }
330 else{
331 //No type, look for complexType in the subNodes
332 Element complexElement = findXMLEleByName(startElement, SchemaConstants.ELEM_COMPLEXTYPE, null);
333 if (complexElement != null)
334 return findXSDEleOnComplexType(complexElement, path, grammars);
335 else{
336 WADLSException wsdlsExc = new WADLSException(WADLSException.NOT_FOUND_ELE_BY_PATH,
337 "cannot find complexType matching the path");
338 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(startElement));
339 throw wsdlsExc;
340 }
341 }
342 }*/
343 return startElement;
344 }
345
346 /**
347 * Search an XSD Element from the XSD complexType.
348 * @param complexTypeElement Start searching from this XSD ComplexType
349 * @param path
350 * @param types WSDLS Types
351 * @return An XSD Element
352 * @throws WADLSException
353 */
354 public static Element findXSDEleOnComplexType(Element complexTypeElement, String path, Grammars grammars) throws WADLSException{
355 if (path == null || path == "")
356 return null;
357 //look for the matching element.
358 String splitedPath[] = splitPath(path);
359 String elementName = splitedPath[0];
360 path = splitedPath[1];
361 Element targetElement = findXSDEleInComplexType(complexTypeElement, elementName, grammars);
362 if (targetElement != null)
363 return findXSDEleOnEle(targetElement, path, grammars);
364 else{
365 WADLSException wsdlsExc = new WADLSException(WADLSException.NOT_FOUND_ELE_BY_PATH,
366 "Cannot find the element matching the path");
367 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(complexTypeElement));
368 throw wsdlsExc;
369 }
370 }
371
372 public static Element findXSDEleOnComplexType(Element complexTypeElement, String path, Params params) throws WADLSException{
373 /*if (path == null || path == "")
374 return null;
375 //look for the matching element.
376 String splitedPath[] = splitPath(path);
377 String elementName = splitedPath[0];
378 path = splitedPath[1];
379 Element targetElement = findXSDEleInComplexType(complexTypeElement, elementName, grammars);
380 if (targetElement != null)
381 return findXSDEleOnEle(targetElement, path, grammars);
382 else{
383 WADLSException wsdlsExc = new WADLSException(WADLSException.NOT_FOUND_ELE_BY_PATH,
384 "Cannot find the element matching the path");
385 wsdlsExc.setLocation(XPathUtils.getXPathExprFromNode(complexTypeElement));
386 throw wsdlsExc;
387 }*/
388 return complexTypeElement;
389 }
390
391 /**
392 * Search an XSD Element recursively inside of the complexType.
393 * @param startElement
394 * @param elementName The desired attribute "name"
395 * @param types WSDL Types
396 * @return A XSD Element
397 * @throws WADLSException
398 */
399 public static Element findXSDEleInComplexType(Element startElement, String elementName, Grammars grammars) throws WADLSException{
400 NodeList subNodes = startElement.getChildNodes();
401 int length = subNodes.getLength();
402 for(int i=0;i<length;i++){
403 Node currentNode = subNodes.item(i);
404 if (currentNode.getNodeType() != Node.ELEMENT_NODE) continue;
405 Element currentElement = (Element)currentNode;
406 String tagName = currentElement.getTagName();
407 QName tagQName = QNameUtils.getQName(tagName, currentElement);
408 String localPart = tagQName.getLocalPart();
409 if (!SchemaConstants.XSD_STR_LIST.contains(tagQName.getNamespaceURI()))
410 continue;
411 if (localPart.equals(SchemaConstants.ELEM_ELEMENT)){
412 String refAttr = currentElement.getAttribute(SchemaConstants.ATTR_REF);
413 if (refAttr != ""){
414 if (refAttr.equals(elementName)){ //TODO need to compare the namespaces
415 //look for the referenced element
416 QName elementQName = QNameUtils.getQName(refAttr, currentElement);
417 Element refenecedElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_ELEMENT, elementQName);
418 return refenecedElement;
419 }
420 }
421 else{
422 String nameAttr = currentElement.getAttribute(SchemaConstants.ATTR_NAME);
423 // if (nameAttr == "") continue;
424 if (nameAttr.equals(elementName)) //TODO need to compare the namespaces
425 //this is the element we are looking for.
426 return currentElement;
427 }
428 }
429 else if
430 (localPart.equals(SchemaConstants.ELEM_ALL)
431 || localPart.equals(SchemaConstants.ELEM_SEQUENCE)
432 || localPart.equals(SchemaConstants.ELEM_CHOICE)
433 || localPart.equals(SchemaConstants.ELEM_COMPLEXCONTENT)
434 || localPart.equals(SchemaConstants.ELEM_RESTRICTION)
435 ){
436 //Just skip it, call this method recursively.
437 return findXSDEleInComplexType(currentElement, elementName, grammars);
438 }
439 else if (localPart.equals(SchemaConstants.ELEM_EXTENSION)){
440 //try to find it inside the <xsd:extension>
441 Element returnElement = findXSDEleInComplexType(currentElement, elementName, grammars);
442 if (returnElement != null)
443 return returnElement;
444 else{
445 String baseAttr = currentElement.getAttribute(SchemaConstants.ATTR_BASE);
446 QName baseQName = QNameUtils.getQName(baseAttr, currentElement);
447 Element baseElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_COMPLEXTYPE, baseQName);
448 return findXSDEleInComplexType(baseElement, elementName, grammars);
449 }
450 }
451 else if (localPart.equals(SchemaConstants.ELEM_GROUP)){
452 //find <group>.
453 String refAttr = currentElement.getAttribute(SchemaConstants.ATTR_REF);
454 if (refAttr != ""){
455 //look for the referenced <group>
456 QName groupQName = QNameUtils.getQName(refAttr, currentElement);
457 Element groupElement = findXMLEleInSchemas(grammars, SchemaConstants.ELEM_GROUP, groupQName);
458 if (groupElement != null){
459 //try to find it in the referenced <group>
460 Element returnElement = findXSDEleInComplexType(groupElement, elementName, grammars);
461 if (returnElement != null)
462 return returnElement;
463 else
464 //Not in the referenced <group>
465 continue;
466 }
467 else
468 throw new DOMException(DOMException.NOT_FOUND_ERR, "Cannot find the referenced group");
469 }
470 else
471 //Just skip it, call this method recursively.
472 return findXSDEleInComplexType(currentElement, elementName, grammars);
473 }
474 }
475 return null;
476 }
477
478 /**
479 * return the first schema in the given extensible elements.
480 * @param extElements Search the first schema in this extensible elements.
481 * @return The first schema in the given extensible elements.
482 * @throws WADLSException
483 */
484 public static Schema getFirstSchema(List extElements){
485 if (extElements == null)
486 return null;
487 Object schemaObj = extElements.iterator().next();
488 if (schemaObj == null)
489 return null;
490 /* ExtensibilityElement schemaEE = (ExtensibilityElement) schemaObj;
491 if (SchemaConstants.XSD_QNAME_LIST.contains(schemaEE.getElementType()))
492 return (Schema)schemaObj;
493 else*/
494 return null;
495 }
496
497 /**
498 * return all schemas in the given extensible elements.
499 * @param extElements Search the first schema in this extensible elements.
500 * @return All schemas in the given extensible elements.
501 * @throws WADLSException
502 */
503 public static List getSchemas(List extElements){
504 List schemas = new ArrayList();
505 if (extElements != null){
506 Iterator it = extElements.iterator();
507 while(it.hasNext()){
508 Object schemaObj = it.next();
509 if (schemaObj == null)
510 continue;
511 /*ExtensibilityElement schemaEE = (ExtensibilityElement) schemaObj;
512 if (SchemaConstants.XSD_QNAME_LIST.contains(schemaEE.getElementType()))
513 schemas.add((Schema)schemaObj);*/
514 }
515 }
516 return schemas;
517 }
518
519 /**
520 * This method split the path string, return a string array,
521 * whose first dim is the string before the first '/' ,
522 * and the second dim is the remaining string.
523 * @param path The path to be splitted.
524 * @return The splitted string.
525 */
526 public static String[] splitPath(String path){
527 String returnStr[] = new String[2]; //the string before '/'
528 int slashPosition = path.indexOf('/');
529 if (slashPosition == -1){
530 //no deeper path
531 returnStr[0] = path;
532 returnStr[1] = "";
533 }
534 else{
535 returnStr[0] = path.substring(0, slashPosition);
536 returnStr[1] = path.substring(slashPosition+1);
537 //if the remaining path is "/", empty it.
538 if (returnStr[1] == "/") returnStr[1] = "";
539 }
540 return returnStr;
541 }
542
543 }