001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.validator.internal;
019:
020: import java.io.File;
021: import java.io.FileNotFoundException;
022: import java.io.FileReader;
023: import java.io.IOException;
024: import java.net.URI;
025: import java.net.URL;
026: import java.util.Iterator;
027: import javax.xml.parsers.ParserConfigurationException;
028: import javax.xml.stream.XMLEventReader;
029: import javax.xml.stream.XMLInputFactory;
030: import javax.xml.stream.XMLStreamConstants;
031: import javax.xml.stream.XMLStreamException;
032: import javax.xml.stream.events.Attribute;
033: import javax.xml.stream.events.Namespace;
034: import javax.xml.stream.events.StartElement;
035: import javax.xml.stream.events.XMLEvent;
036:
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.Node;
040:
041: import org.apache.cxf.common.util.StringUtils;
042: import org.apache.cxf.helpers.XMLUtils;
043: import org.apache.cxf.tools.common.ToolException;
044: import org.apache.cxf.tools.util.URIParserUtil;
045: import org.apache.cxf.wsdl.WSDLConstants;
046:
047: public class Stax2DOM {
048:
049: private Element currentElement;
050: private Document doc;
051: private XMLInputFactory factory;
052: private XMLEventReader reader;
053:
054: public Stax2DOM() {
055: }
056:
057: private void init() {
058: try {
059: factory = XMLInputFactory.newInstance();
060: factory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
061: true);
062: } catch (Exception e) {
063: e.printStackTrace();
064: throw new ToolException(e);
065: }
066: }
067:
068: public Document getDocument(String wsdl) throws ToolException {
069: try {
070: URI wsdlURI = new URI(URIParserUtil.getAbsoluteURI(wsdl));
071: if (wsdlURI.toString().startsWith("http")) {
072: return getDocument(wsdlURI.toURL());
073: }
074: return getDocument(new File(wsdlURI));
075: } catch (Exception e) {
076: throw new ToolException(e);
077: }
078: }
079:
080: public Document getDocument(URL url) throws ToolException {
081: if (reader == null) {
082: init();
083: try {
084: reader = factory.createXMLEventReader(url.openStream());
085: } catch (FileNotFoundException fe) {
086: throw new ToolException("Cannot get the wsdl " + url,
087: fe);
088: } catch (XMLStreamException e) {
089: throw new ToolException(e);
090: } catch (IOException ioe) {
091: throw new ToolException(ioe);
092: }
093: }
094: return getDocument(reader, url.toString());
095: }
096:
097: public Document getDocument(File wsdl) throws ToolException {
098: if (reader == null) {
099: init();
100: try {
101: reader = factory.createXMLEventReader(new FileReader(
102: wsdl));
103: } catch (FileNotFoundException fe) {
104: throw new ToolException("Cannot get the wsdl " + wsdl,
105: fe);
106: } catch (XMLStreamException e) {
107: throw new ToolException(e);
108: }
109:
110: }
111: return getDocument(reader, wsdl.toString());
112: }
113:
114: public Document getDocument(XMLEventReader xmlEventReader)
115: throws ToolException {
116: return getDocument(xmlEventReader, null);
117: }
118:
119: public Document getDocument(XMLEventReader xmlEventReader,
120: String wsdlurl) throws ToolException {
121: try {
122: doc = XMLUtils.newDocument();
123: } catch (ParserConfigurationException e) {
124: throw new ToolException(e);
125: }
126: doc.setDocumentURI(wsdlurl);
127:
128: currentElement = doc.getDocumentElement();
129:
130: while (xmlEventReader.hasNext()) {
131: XMLEvent xmleve = (XMLEvent) xmlEventReader.next();
132:
133: if (xmleve.getEventType() == XMLStreamConstants.END_ELEMENT) {
134: endElement();
135: }
136:
137: if (xmleve.getEventType() == XMLStreamConstants.START_ELEMENT) {
138: startElement((StartElement) xmleve);
139: }
140: }
141: return doc;
142: }
143:
144: public void startElement(StartElement ele) {
145: Element element = null;
146: element = doc.createElementNS(ele.getName().getNamespaceURI(),
147: ele.getName().getLocalPart());
148:
149: Iterator ite = ele.getAttributes();
150:
151: while (ite.hasNext()) {
152: Attribute attr = (Attribute) ite.next();
153: element.setAttribute(attr.getName().getLocalPart(), attr
154: .getValue());
155: }
156:
157: String xmlns = "http://schemas.xmlsoap.org/wsdl/";
158: ite = ele.getNamespaces();
159: while (ite.hasNext()) {
160: Namespace ns = (Namespace) ite.next();
161: String name = ns.getPrefix();
162: if (!StringUtils.isEmpty(name)) {
163: element.setAttributeNS(xmlns, name, ns
164: .getNamespaceURI());
165: } else {
166: xmlns = ns.getNamespaceURI();
167: }
168: }
169:
170: if (currentElement == null) {
171: doc.appendChild(element);
172: } else {
173: currentElement.appendChild(element);
174: }
175:
176: currentElement = element;
177: element.setUserData(WSDLConstants.NODE_LOCATION, ele
178: .getLocation(), null);
179:
180: }
181:
182: public void endElement() {
183: Node node = currentElement.getParentNode();
184: if (node instanceof Document) {
185: currentElement = ((Document) node).getDocumentElement();
186: } else {
187: currentElement = (Element) currentElement.getParentNode();
188: }
189: }
190:
191: }
|