KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > wsdl > Utilities


1 /*
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  *
19  */

20 package org.apache.beehive.wsm.wsdl;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.lang.reflect.Array JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.xmlbeans.SchemaType;
29 import org.apache.xmlbeans.XmlException;
30 import org.apache.xmlbeans.XmlObject;
31
32 public class Utilities {
33
34     public static <T extends XmlObject> T[] selectChildren(XmlObject parent,
35             Class JavaDoc<T> childClass) throws IllegalAccessException JavaDoc,
36             NoSuchFieldException JavaDoc {
37         // retrieve the SchemaType from the static type field
38
SchemaType st = (SchemaType) childClass.getField("type").get(null);
39         XmlObject[] kids = parent.selectChildren(st.getDocumentElementName());
40         T[] castKids = (T[]) Array.newInstance(childClass, kids.length);
41         for (int j = 0; j < castKids.length; j++) {
42             castKids[j] = childClass.cast(kids[j]);
43         }
44         return castKids;
45     }
46
47     /**
48      * @param docType
49      * @return
50      * @throws XmlException
51      * @throws IOException
52      * @throws IllegalAccessException
53      * @throws NoSuchFieldException
54      */

55
56     // Keep a cache of schemas that have been read.
57
static Map JavaDoc<String JavaDoc, Schema> schemaCache = new HashMap JavaDoc<String JavaDoc, Schema>();
58     public static Schema findtSchemaDocument(SchemaType docType)
59             throws XmlException, IOException JavaDoc, IllegalAccessException JavaDoc,
60             NoSuchFieldException JavaDoc {
61         Schema mySchema = null;
62         if (null != (mySchema = schemaCache.get(docType.getName()
63                 .getNamespaceURI()))) {
64             return mySchema;
65         }
66         String JavaDoc schemaSrc = docType.getSourceName();
67         InputStream JavaDoc stream = docType.getTypeSystem().getSourceAsStream(
68                 schemaSrc);
69
70         if (null == stream) {
71             throw new RuntimeException JavaDoc("WSDL file not found: " + schemaSrc);
72         }
73         if (schemaSrc.endsWith(".wsdl") || schemaSrc.endsWith(".WSDL")) {
74             mySchema = new WSDLParser(stream).getSchema();
75         } else {
76             mySchema = new Schema(stream);
77         }
78         schemaCache.put(docType.getName().getNamespaceURI(), mySchema);
79         return mySchema;
80     }
81
82 }
83
Popular Tags