KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33 import javax.xml.parsers.SAXParser JavaDoc;
34 import javax.xml.parsers.SAXParserFactory JavaDoc;
35 import org.netbeans.modules.xml.wsdl.ui.fastmodel.FastSchema;
36 import org.netbeans.modules.xml.wsdl.ui.fastmodel.FastSchemaFactory;
37
38 import org.xml.sax.Attributes JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.SAXParseException JavaDoc;
41 import org.xml.sax.helpers.DefaultHandler JavaDoc;
42
43
44
45 /**
46  * @author radval
47  *
48  * A factory which parses wsdl fast.
49  * Just parse some attributes from wsdl and ignore rests.
50  */

51 public class FastSchemaFactoryImpl extends FastSchemaFactory {
52     
53     private boolean mParseImports = false;
54     
55     
56     private Logger JavaDoc logger = Logger.getLogger(this.getClass().getName());
57     
58     @Override JavaDoc
59     public FastSchema newFastSchema(InputStream JavaDoc in, boolean parseImports) {
60         this.mParseImports = parseImports;
61         FastSchema def = new FastSchemaImpl();
62         try {
63             SAXParserFactory JavaDoc fac = SAXParserFactory.newInstance();
64             SAXParser JavaDoc parser = fac.newSAXParser();
65             FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def);
66             parser.parse(in, handler);
67             
68         } catch(Exception JavaDoc ex) {
69             logger.log(Level.SEVERE, "Failed to parse schema", ex);
70             def.setParseErrorMessage(ex.getMessage());
71         }
72         
73         return def;
74     }
75     
76     @Override JavaDoc
77     public FastSchema newFastSchema(String JavaDoc defFileUrl) {
78         return newFastSchema(defFileUrl, false);
79     }
80     
81     @Override JavaDoc
82     public FastSchema newFastSchema(String JavaDoc defFileUrl,
83             boolean parseImports) {
84         File JavaDoc file = new File JavaDoc(defFileUrl);
85         return newFastSchema(file, parseImports);
86     }
87     
88     @Override JavaDoc
89     public FastSchema newFastSchema(File JavaDoc file) {
90         return newFastSchema(file, false);
91     }
92     
93     @Override JavaDoc
94     public FastSchema newFastSchema(File JavaDoc file, boolean parseImports) {
95         this.mParseImports = parseImports;
96         FastSchema def = new FastSchemaImpl();
97         
98         
99         
100         try {
101             SAXParserFactory JavaDoc fac = SAXParserFactory.newInstance();
102             SAXParser JavaDoc parser = fac.newSAXParser();
103             FastWSDLDefinitionsHandler handler = new FastWSDLDefinitionsHandler(def);
104             parser.parse(file, handler);
105             
106         } catch(Exception JavaDoc ex) {
107             logger.log(Level.SEVERE, "Failed to parse "+ file.getAbsolutePath(), ex);
108             def.setParseErrorMessage(ex.getMessage());
109         }
110         
111         return def;
112     }
113     
114     public class FastWSDLDefinitionsHandler extends DefaultHandler JavaDoc {
115         
116         private String JavaDoc targetNamespace;
117         
118         private FastSchema mDef;
119         
120         public FastWSDLDefinitionsHandler(FastSchema def) {
121             this.mDef = def;
122         }
123         
124         public String JavaDoc getTargetNamespace() {
125             return targetNamespace;
126         }
127         
128         @Override JavaDoc
129         public void startElement (String JavaDoc uri,
130                 String JavaDoc localName,
131                 String JavaDoc qName,
132                 Attributes JavaDoc attributes)
133         throws SAXException JavaDoc
134         {
135             if(qName.endsWith("schema")) {
136                 for(int i = 0 ; i < attributes.getLength(); i++) {
137                     String JavaDoc attrQName = attributes.getQName(i);
138                     if(attrQName.endsWith("targetNamespace")) {
139                         targetNamespace = attributes.getValue(i);
140                         mDef.setTargetNamespace(targetNamespace);
141                         break;
142                     }
143                 }
144             }
145             
146             
147         }
148         
149         @Override JavaDoc
150         public void fatalError (SAXParseException JavaDoc e)
151         throws SAXException JavaDoc
152         {
153             
154         }
155         
156         @Override JavaDoc
157         public void error (SAXParseException JavaDoc e)
158         throws SAXException JavaDoc
159         {
160             
161         }
162         
163     }
164 }
165
Popular Tags