001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.identity.profile.ui.support;
043:
044: /**
045: * Custom parser for a wsdl file.
046: *
047: * Created on April 18, 2006, 1:29 PM
048: *
049: * @author Srividhya Narayanan
050: */
051:
052: import java.io.BufferedReader;
053: import java.io.File;
054: import java.io.FileReader;
055: import java.io.IOException;
056: import java.util.ArrayList;
057: import javax.xml.parsers.SAXParserFactory;
058: import javax.xml.parsers.SAXParser;
059: import javax.xml.parsers.ParserConfigurationException;
060: import org.xml.sax.Attributes;
061: import org.xml.sax.helpers.DefaultHandler;
062: import org.xml.sax.SAXException;
063: import org.xml.sax.InputSource;
064:
065: public class WsdlParser {
066: public static final String TNS = "targetNamespace"; // NOI18N
067: public static final String ADDRESS = "soapAddress"; // NOI18N
068: public static final String PORT = "port"; // NOI18N
069:
070: /** Creates a new instance of XMLParser */
071: public WsdlParser() {
072: }
073:
074: public static WsdlData parseWSDLFile(File xmlFile, String svc)
075: throws IOException {
076: BufferedReader br = new BufferedReader(new FileReader(xmlFile));
077: InputSource is = new InputSource(br);
078: try {
079: return parseWSDLFile(is, svc);
080: } finally {
081: br.close();
082: }
083: }
084:
085: public static WsdlData parseWSDLFile(InputSource xmlFile, String svc)
086: throws IOException {
087: WsdlData wsdlData = null;
088: try {
089: SAXParserFactory saxFactory = SAXParserFactory
090: .newInstance();
091: saxFactory.setNamespaceAware(true);
092: SAXParser parser = saxFactory.newSAXParser();
093: WsdlHandler handler = new WsdlHandler(svc);
094: parser.parse(xmlFile, handler);
095: wsdlData = handler.getWsdlData();
096: } catch (ParserConfigurationException excp) {
097: IOException ioe = new IOException();
098: ioe.initCause(excp);
099: throw ioe;
100: } catch (SAXException excp) {
101: IOException ioe = new IOException();
102: ioe.initCause(excp);
103: throw ioe;
104: }
105: return wsdlData;
106: }
107:
108: public static ArrayList getWsdlSvcNames(File xmlFile)
109: throws IOException {
110: BufferedReader br = new BufferedReader(new FileReader(xmlFile));
111: InputSource is = new InputSource(br);
112: try {
113: return getWsdlSvcNames(is);
114: } finally {
115: br.close();
116: }
117: }
118:
119: public static ArrayList getWsdlSvcNames(InputSource xmlFile)
120: throws IOException {
121: try {
122: SAXParserFactory saxFactory = SAXParserFactory
123: .newInstance();
124: saxFactory.setNamespaceAware(true);
125: SAXParser parser = saxFactory.newSAXParser();
126: SvcNameHandler handler = new SvcNameHandler();
127: parser.parse(xmlFile, handler);
128: return handler.getWsdlSvcNames();
129: } catch (ParserConfigurationException excp) {
130: IOException ioe = new IOException();
131: ioe.initCause(excp);
132: throw ioe;
133: } catch (SAXException excp) {
134: IOException ioe = new IOException();
135: ioe.initCause(excp);
136: throw ioe;
137: }
138: }
139:
140: private static class WsdlHandler extends DefaultHandler {
141: private static final String W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl"; // NOI18N
142: private static final String W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/"; // NOI18N
143:
144: private String svcName;
145: WsdlData wsdlData = new WsdlData();
146: boolean found = false;
147:
148: public WsdlHandler(String svc) {
149: super ();
150: svcName = svc;
151: }
152:
153: public WsdlData getWsdlData() {
154: return wsdlData;
155: }
156:
157: public void startDocument() {
158: }
159:
160: public void endDocument() {
161: }
162:
163: public void startElement(String uri, String localName,
164: String qName, Attributes attributes) {
165: if (W3C_WSDL_SCHEMA.equals(uri)
166: || W3C_WSDL_SCHEMA_SLASH.equals(uri)) {
167: if (localName.equals("definitions")) { // NOI18N
168: wsdlData.setTargetNameSpace(attributes
169: .getValue("targetNamespace")); // NOI18N
170: }
171: if (localName.equals("service")) { // NOI18N
172: if (svcName.equalsIgnoreCase(attributes
173: .getValue("name"))) { // NOI18N
174: found = true;
175: }
176: }
177: if (found && localName.equals("port")) { // NOI18N
178: wsdlData.setPort(attributes.getValue("name")); // NOI18N
179: }
180: }
181: if (found && localName.equals("address")) { // NOI18N
182: wsdlData.setAddress(attributes.getValue("location")); // NOI18N
183: found = false;
184: }
185: }
186:
187: public void endElement(String uri, String localName,
188: String qName) {
189: }
190: }
191:
192: private static class SvcNameHandler extends DefaultHandler {
193: private static final String W3C_WSDL_SCHEMA = "http://schemas.xmlsoap.org/wsdl"; // NOI18N
194: private static final String W3C_WSDL_SCHEMA_SLASH = "http://schemas.xmlsoap.org/wsdl/"; // NOI18N
195:
196: ArrayList wsdlSvcNames = new ArrayList();
197:
198: public SvcNameHandler() {
199: super ();
200: }
201:
202: public ArrayList getWsdlSvcNames() {
203: return wsdlSvcNames;
204: }
205:
206: public void startDocument() {
207: }
208:
209: public void endDocument() {
210: }
211:
212: public void startElement(String uri, String localName,
213: String qName, Attributes attributes) {
214: if (W3C_WSDL_SCHEMA.equals(uri)
215: || W3C_WSDL_SCHEMA_SLASH.equals(uri)) {
216: if (localName.equals("service")) { // NOI18N
217: String name = attributes.getValue("name"); // NOI18N
218: wsdlSvcNames.add(name);
219: }
220: }
221: }
222:
223: public void endElement(String uri, String localName,
224: String qName) {
225: }
226: }
227: }
|