KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > fastmodel > impl > FastWSDLDefinitionsFactoryImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * Created on Mar 10, 2005
22  *
23  * To change the template for this generated file go to
24  * Window - Preferences - Java - Code Generation - Code and Comments
25  */

26 package org.netbeans.modules.xml.wsdl.ui.fastmodel.impl;
27
28 import java.io.File JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
33
34 import javax.xml.namespace.QName JavaDoc;
35 import javax.xml.parsers.SAXParser JavaDoc;
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37
38 import org.netbeans.modules.xml.wsdl.ui.fastmodel.FastWSDLDefinitions;
39 import org.netbeans.modules.xml.wsdl.ui.fastmodel.FastWSDLDefinitionsFactory;
40 import org.xml.sax.Attributes JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.SAXParseException JavaDoc;
44 import org.xml.sax.helpers.DefaultHandler JavaDoc;
45
46 /**
47  * @author radval
48  *
49  * A factory which parses wsdl fast.
50  * Just parse some attributes from wsdl and ignore rests.
51  */

52 public class FastWSDLDefinitionsFactoryImpl extends FastWSDLDefinitionsFactory {
53     
54     private boolean mParseImports = false;
55     
56     
57     private Logger JavaDoc logger = Logger.getLogger(this.getClass().getName());
58     
59     @Override JavaDoc
60     public FastWSDLDefinitions newFastWSDLDefinitions(InputStream JavaDoc in, boolean parseImports) {
61         this.mParseImports = parseImports;
62         FastWSDLDefinitionsImpl def = new FastWSDLDefinitionsImpl();
63         try {
64             SAXParserFactory JavaDoc fac = SAXParserFactory.newInstance();
65             SAXParser JavaDoc parser = fac.newSAXParser();
66             FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def);
67             parser.parse(in, handler);
68             
69         } catch(Exception JavaDoc ex) {
70             logger.log(Level.SEVERE, "Failed to parse wsdl", ex);
71             def.setParseErrorMessage(ex.getMessage());
72         }
73         
74         return def;
75     }
76     
77     @Override JavaDoc
78     public FastWSDLDefinitions newFastWSDLDefinitions(Reader JavaDoc in, boolean parseImports) {
79         this.mParseImports = parseImports;
80         FastWSDLDefinitionsImpl def = new FastWSDLDefinitionsImpl();
81         try {
82             SAXParserFactory JavaDoc fac = SAXParserFactory.newInstance();
83             SAXParser JavaDoc parser = fac.newSAXParser();
84             FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def);
85             InputSource JavaDoc ins = new InputSource JavaDoc(in);
86             parser.parse(ins, handler);
87             
88         } catch(Exception JavaDoc ex) {
89             logger.log(Level.SEVERE, "Failed to parse wsdl", ex);
90             def.setParseErrorMessage(ex.getMessage());
91         }
92         
93         return def;
94     }
95     
96     @Override JavaDoc
97     public FastWSDLDefinitions newFastWSDLDefinitions(String JavaDoc defFileUrl) {
98         return newFastWSDLDefinitions(defFileUrl, false);
99     }
100     
101     @Override JavaDoc
102     public FastWSDLDefinitions newFastWSDLDefinitions(String JavaDoc defFileUrl,
103             boolean parseImports) {
104         File JavaDoc file = new File JavaDoc(defFileUrl);
105         return newFastWSDLDefinitions(file, parseImports);
106     }
107     
108
109     @Override JavaDoc
110     public FastWSDLDefinitions newFastWSDLDefinitions(File JavaDoc file) {
111         return newFastWSDLDefinitions(file, false);
112     }
113
114     @Override JavaDoc
115     public FastWSDLDefinitions newFastWSDLDefinitions(File JavaDoc file, boolean parseImports) {
116         this.mParseImports = parseImports;
117         FastWSDLDefinitionsImpl def = new FastWSDLDefinitionsImpl();
118         
119         
120         
121         try {
122             SAXParserFactory JavaDoc fac = SAXParserFactory.newInstance();
123             SAXParser JavaDoc parser = fac.newSAXParser();
124             FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def);
125             parser.parse(file, handler);
126             
127         } catch(Exception JavaDoc ex) {
128             logger.log(Level.SEVERE, "Failed to parse "+ file.getAbsolutePath(), ex);
129             def.setParseErrorMessage(ex.getMessage());
130         }
131         
132         return def;
133     }
134     
135     public class FastWSDLDefinitionsHandler extends DefaultHandler JavaDoc {
136         
137         private String JavaDoc targetNamespace;
138         
139         private FastWSDLDefinitionsImpl mDef;
140         
141         public FastWSDLDefinitionsHandler(FastWSDLDefinitionsImpl def) {
142             this.mDef = def;
143         }
144         
145         public String JavaDoc getTargetNamespace() {
146             return targetNamespace;
147         }
148         
149         @Override JavaDoc
150         public void startElement(String JavaDoc uri,
151                 String JavaDoc localName,
152                 String JavaDoc qName,
153                 Attributes JavaDoc attributes)
154                 throws SAXException JavaDoc {
155
156             QName JavaDoc wsdlTagQName = new QName JavaDoc(qName);
157             
158             if(wsdlTagQName != null && wsdlTagQName.getLocalPart().endsWith("definitions")) {//NOI18N
159
this.mDef.setWSDL(true);
160                 for(int i = 0 ; i < attributes.getLength(); i++) {
161                     QName JavaDoc attrQName = new QName JavaDoc(attributes.getQName(i));
162                     String JavaDoc attrLocalName = attrQName.getLocalPart();
163                     if(attrLocalName.equals("targetNamespace")) {//NOI18N
164
targetNamespace = attributes.getValue(i);
165                         mDef.setTargetNamespace(targetNamespace);
166                         break;
167                     }
168                 }
169             }
170 // } else if(mParseImports && Import.TAG.equals(wsdlTagQName.getLocalPart())) {
171
// Import wsdlImport = this.mDef.createImport();
172
// wsdlImport.setQualifiedName(qName);
173
// SAXParserSupport.setAttributes(wsdlImport, attributes);
174
// mDef.addImport(wsdlImport);
175
// }
176

177             
178         }
179         
180         @Override JavaDoc
181         public void fatalError(SAXParseException JavaDoc e)
182         throws SAXException JavaDoc {
183             
184         }
185         
186         @Override JavaDoc
187         public void error(SAXParseException JavaDoc e)
188         throws SAXException JavaDoc {
189             
190         }
191         
192     }
193 }
194
Popular Tags