Author: dkulp Date: Mon Jul 15 18:50:21 2013 New Revision: 1503415 URL: http://svn.apache.org/r1503415 Log: Merged revisions 1503374 via git cherry-pick from https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes
........ r1503374 | dkulp | 2013-07-15 14:03:50 -0400 (Mon, 15 Jul 2013) | 10 lines Merged revisions 1502607 via git cherry-pick from https://svn.apache.org/repos/asf/cxf/branches/2.7.x-fixes ........ r1502607 | dkulp | 2013-07-12 12:51:58 -0400 (Fri, 12 Jul 2013) | 2 lines Port back some of the new methods on StaxUtils and DOMUtils to make porting other tests and changes back to 2.7.x easier. ........ ........ Added: cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/staxutils/PrettyPrintXMLStreamWriter.java Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Added: cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/staxutils/PrettyPrintXMLStreamWriter.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/staxutils/PrettyPrintXMLStreamWriter.java?rev=1503415&view=auto ============================================================================== --- cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/staxutils/PrettyPrintXMLStreamWriter.java (added) +++ cxf/branches/2.5.x-fixes/api/src/main/java/org/apache/cxf/staxutils/PrettyPrintXMLStreamWriter.java Mon Jul 15 18:50:21 2013 @@ -0,0 +1,264 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.cxf.staxutils; + +import java.util.Stack; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +public class PrettyPrintXMLStreamWriter implements XMLStreamWriter { + + static final int DEFAULT_INDENT_LEVEL = 2; + + XMLStreamWriter baseWriter; + + int curIndent; + int indentAmount = DEFAULT_INDENT_LEVEL; + Stack<CurrentElement> elems = new Stack<CurrentElement>(); + + public PrettyPrintXMLStreamWriter(XMLStreamWriter writer, + int indentAmount) { + this(writer, indentAmount, 0); + } + public PrettyPrintXMLStreamWriter(XMLStreamWriter writer, + int indentAmount, + int initialLevel) { + baseWriter = writer; + curIndent = initialLevel; + this.indentAmount = indentAmount; + } + + public void writeSpaces() throws XMLStreamException { + for (int i = 0; i < curIndent; i++) { + baseWriter.writeCharacters(" "); + } + } + + public void indentWithSpaces() throws XMLStreamException { + writeSpaces(); + indent(); + } + + public void indent() { + curIndent += indentAmount; + } + + public void unindent() { + curIndent -= indentAmount; + } + + public void close() throws XMLStreamException { + baseWriter.close(); + } + + public void flush() throws XMLStreamException { + baseWriter.flush(); + } + + public NamespaceContext getNamespaceContext() { + return baseWriter.getNamespaceContext(); + } + + + public java.lang.String getPrefix(java.lang.String uri) throws XMLStreamException { + return baseWriter.getPrefix(uri); + } + + public java.lang.Object getProperty(java.lang.String name) throws IllegalArgumentException { + return baseWriter.getProperty(name); + } + + public void setDefaultNamespace(java.lang.String uri) throws XMLStreamException { + baseWriter.setDefaultNamespace(uri); + } + + public void setNamespaceContext(NamespaceContext context) throws XMLStreamException { + baseWriter.setNamespaceContext(context); + } + + public void setPrefix(java.lang.String prefix, java.lang.String uri) + throws XMLStreamException { + baseWriter.setPrefix(prefix, uri); + } + + public void writeAttribute(java.lang.String localName, java.lang.String value) + throws XMLStreamException { + baseWriter.writeAttribute(localName, value); + } + + public void writeAttribute(java.lang.String namespaceURI, + java.lang.String localName, + java.lang.String value) throws XMLStreamException { + baseWriter.writeAttribute(namespaceURI, localName, value); + } + + public void writeAttribute(java.lang.String prefix, + java.lang.String namespaceURI, + java.lang.String localName, + java.lang.String value) throws XMLStreamException { + baseWriter.writeAttribute(prefix, namespaceURI, localName, value); + } + + public void writeCData(java.lang.String data) throws XMLStreamException { + baseWriter.writeCData(data); + } + + public void writeCharacters(char[] text, int start, int len) throws XMLStreamException { + baseWriter.writeCharacters(text, start, len); + } + + public void writeCharacters(java.lang.String text) throws XMLStreamException { + baseWriter.writeCharacters(text); + } + + public void writeComment(java.lang.String data) throws XMLStreamException { + baseWriter.writeComment(data); + } + + public void writeDefaultNamespace(java.lang.String namespaceURI) throws XMLStreamException { + baseWriter.writeDefaultNamespace(namespaceURI); + } + + public void writeDTD(java.lang.String dtd) throws XMLStreamException { + baseWriter.writeDTD(dtd); + } + + public void writeEmptyElement(java.lang.String localName) throws XMLStreamException { + baseWriter.writeEmptyElement(localName); + } + + public void writeEmptyElement(java.lang.String namespaceURI, java.lang.String localName) + throws XMLStreamException { + baseWriter.writeEmptyElement(localName, namespaceURI); + } + + public void writeEmptyElement(java.lang.String prefix, + java.lang.String localName, + java.lang.String namespaceURI) throws XMLStreamException { + baseWriter.writeEmptyElement(prefix, localName, namespaceURI); + } + + public void writeEndDocument() throws XMLStreamException { + baseWriter.writeEndDocument(); + } + + public void writeEndElement() throws XMLStreamException { + CurrentElement elem = elems.pop(); + unindent(); + if (elem.hasChildElements()) { + baseWriter.writeCharacters("\n"); + writeSpaces(); + } + baseWriter.writeEndElement(); + if (elems.empty()) { + baseWriter.writeCharacters("\n"); + } + } + + public void writeEntityRef(java.lang.String name) throws XMLStreamException { + baseWriter.writeEntityRef(name); + } + + public void writeNamespace(java.lang.String prefix, java.lang.String namespaceURI) + throws XMLStreamException { + baseWriter.writeNamespace(prefix, namespaceURI); + } + + public void writeProcessingInstruction(java.lang.String target) + throws XMLStreamException { + baseWriter.writeProcessingInstruction(target); + } + + public void writeProcessingInstruction(java.lang.String target, java.lang.String data) + throws XMLStreamException { + baseWriter.writeProcessingInstruction(target, data); + } + + public void writeStartDocument() throws XMLStreamException { + baseWriter.writeStartDocument(); + } + + public void writeStartDocument(java.lang.String version) throws XMLStreamException { + baseWriter.writeStartDocument(version); + } + + public void writeStartDocument(java.lang.String encoding, java.lang.String version) + throws XMLStreamException { + baseWriter.writeStartDocument(encoding, version); + } + + public void writeStartElement(java.lang.String localName) throws XMLStreamException { + writeStartElement(null, localName, null); + } + + public void writeStartElement(java.lang.String namespaceURI, java.lang.String localName) + throws XMLStreamException { + writeStartElement(null, localName, namespaceURI); + } + + public void writeStartElement(java.lang.String prefix, + java.lang.String localName, + java.lang.String namespaceURI) throws XMLStreamException { + QName currElemName = new QName(namespaceURI, localName); + if (elems.empty()) { + indentWithSpaces(); + } else { + baseWriter.writeCharacters(""); + baseWriter.writeCharacters("\n"); + indentWithSpaces(); + CurrentElement elem = elems.peek(); + elem.setChildElements(true); + } + if (prefix == null && namespaceURI == null) { + baseWriter.writeStartElement(localName); + } else if (prefix == null) { + baseWriter.writeStartElement(namespaceURI, localName); + } else { + baseWriter.writeStartElement(prefix, localName, namespaceURI); + } + elems.push(new CurrentElement(currElemName)); + } + + + class CurrentElement { + private QName name; + private boolean hasChildElements; + + CurrentElement(QName qname) { + name = qname; + } + + public QName getQName() { + return name; + } + + public boolean hasChildElements() { + return hasChildElements; + } + + public void setChildElements(boolean childElements) { + hasChildElements = childElements; + } + } + +} Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java?rev=1503415&r1=1503414&r2=1503415&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java (original) +++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/helpers/DOMUtils.java Mon Jul 15 18:50:21 2013 @@ -26,9 +26,11 @@ import java.io.Reader; import java.io.StringReader; import java.util.ArrayList; import java.util.Collections; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.WeakHashMap; import javax.xml.XMLConstants; @@ -61,30 +63,48 @@ import org.apache.cxf.common.util.String * Few simple utils to read DOM. This is originally from the Jakarta Commons Modeler. */ public final class DOMUtils { + private static final Map<ClassLoader, DocumentBuilder> DOCUMENT_BUILDERS + = Collections.synchronizedMap(new WeakHashMap<ClassLoader, DocumentBuilder>()); private static final String XMLNAMESPACE = "xmlns"; - private static final Map<ClassLoader, DocumentBuilder> DOCUMENT_BUILDERS = Collections - .synchronizedMap(new WeakHashMap<ClassLoader, DocumentBuilder>()); - private DOMUtils() { } - private static DocumentBuilder getBuilder() throws ParserConfigurationException { + private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = DOMUtils.class.getClassLoader(); } if (loader == null) { - return XMLUtils.getParser(); + return DocumentBuilderFactory.newInstance().newDocumentBuilder(); + } + DocumentBuilder factory = DOCUMENT_BUILDERS.get(loader); + if (factory == null) { + DocumentBuilderFactory f2 = DocumentBuilderFactory.newInstance(); + f2.setNamespaceAware(true); + factory = f2.newDocumentBuilder(); + DOCUMENT_BUILDERS.put(loader, factory); } - DocumentBuilder builder = DOCUMENT_BUILDERS.get(loader); - if (builder == null) { - builder = XMLUtils.getParser(); - DOCUMENT_BUILDERS.put(loader, builder); + return factory; + } + + /** + * Creates a new Docuement object + * @return + * @throws ParserConfigurationException + */ + public static Document newDocument() { + return createDocument(); + } + public static Document createDocument() { + try { + return getDocumentBuilder().newDocument(); + } catch (ParserConfigurationException e) { + throw new RuntimeException(e); } - return builder; } + /** * This function is much like getAttribute, but returns null, not "", for a nonexistent attribute. * @@ -172,6 +192,17 @@ public final class DOMUtils { return null; } + + public static boolean hasAttribute(Element element, String value) { + NamedNodeMap attributes = element.getAttributes(); + for (int i = 0; i < attributes.getLength(); i++) { + Node node = attributes.item(i); + if (value.equals(node.getNodeValue())) { + return true; + } + } + return false; + } public static String getAttribute(Node element, String attName) { NamedNodeMap attrs = element.getAttributes(); if (attrs == null) { @@ -228,18 +259,18 @@ public final class DOMUtils { * @param attName attribute we're looking for * @param attVal attribute value or null if we just want any */ - public static Node findChildWithAtt(Node parent, String elemName, String attName, String attVal) { + public static Element findChildWithAtt(Node parent, String elemName, String attName, String attVal) { - Node child = DOMUtils.getChild(parent, Node.ELEMENT_NODE); + Element child = (Element)getChild(parent, Node.ELEMENT_NODE); if (attVal == null) { while (child != null && (elemName == null || elemName.equals(child.getNodeName())) && DOMUtils.getAttribute(child, attName) != null) { - child = getNext(child, elemName, Node.ELEMENT_NODE); + child = (Element)getNext(child, elemName, Node.ELEMENT_NODE); } } else { while (child != null && (elemName == null || elemName.equals(child.getNodeName())) && !attVal.equals(DOMUtils.getAttribute(child, attName))) { - child = getNext(child, elemName, Node.ELEMENT_NODE); + child = (Element)getNext(child, elemName, Node.ELEMENT_NODE); } } return child; @@ -266,6 +297,66 @@ public final class DOMUtils { public static QName getElementQName(Element el) { return new QName(el.getNamespaceURI(), el.getLocalName()); } + + /** + * Creates a QName object based on the qualified name + * and using the Node as a base to lookup the namespace + * for the prefix + * @param qualifiedName + * @param node + * @return + */ + public static QName createQName(String qualifiedName, Node node) { + if (qualifiedName == null) { + return null; + } + + int index = qualifiedName.indexOf(":"); + + if (index == -1) { + return new QName(qualifiedName); + } + + String prefix = qualifiedName.substring(0, index); + String localName = qualifiedName.substring(index + 1); + String ns = node.lookupNamespaceURI(prefix); + + if (ns == null || localName == null) { + throw new RuntimeException("Invalid QName in mapping: " + qualifiedName); + } + + return new QName(ns, localName, prefix); + } + + public static QName convertStringToQName(String expandedQName) { + return convertStringToQName(expandedQName, ""); + } + + public static QName convertStringToQName(String expandedQName, String prefix) { + int ind1 = expandedQName.indexOf('{'); + if (ind1 != 0) { + return new QName(expandedQName); + } + + int ind2 = expandedQName.indexOf('}'); + if (ind2 <= ind1 + 1 || ind2 >= expandedQName.length() - 1) { + return null; + } + String ns = expandedQName.substring(ind1 + 1, ind2); + String localName = expandedQName.substring(ind2 + 1); + return new QName(ns, localName, prefix); + } + public static Set<QName> convertStringsToQNames(List<String> expandedQNames) { + Set<QName> dropElements = Collections.emptySet(); + if (expandedQNames != null) { + dropElements = new LinkedHashSet<QName>(expandedQNames.size()); + for (String val : expandedQNames) { + dropElements.add(convertStringToQName(val)); + } + } + return dropElements; + } + /** * Get the first direct child with a given type @@ -501,23 +592,7 @@ public final class DOMUtils { t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(n), new StreamResult(os)); } - - public static DocumentBuilder createDocumentBuilder() { - try { - return getBuilder(); - } catch (ParserConfigurationException e) { - throw new RuntimeException("Couldn't find a DOM parser.", e); - } - } - - public static Document createDocument() { - try { - return getBuilder().newDocument(); - } catch (ParserConfigurationException e) { - throw new RuntimeException("Couldn't find a DOM parser.", e); - } - } - + public static String getPrefixRecursive(Element el, String ns) { String prefix = getPrefix(el, ns); if (prefix == null && el.getParentNode() instanceof Element) { @@ -620,7 +695,7 @@ public final class DOMUtils { return null; } - + public static List<Element> findAllElementsByTagNameNS(Element elem, String nameSpaceURI, String localName) { List<Element> ret = new LinkedList<Element>(); Modified: cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java?rev=1503415&r1=1503414&r2=1503415&view=diff ============================================================================== --- cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java (original) +++ cxf/branches/2.5.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java Mon Jul 15 18:50:21 2013 @@ -19,6 +19,9 @@ package org.apache.cxf.staxutils; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; @@ -74,6 +77,7 @@ import org.w3c.dom.Node; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; import org.w3c.dom.UserDataHandler; + import org.xml.sax.InputSource; import org.xml.sax.XMLReader; @@ -83,7 +87,6 @@ import org.apache.cxf.common.util.String import org.apache.cxf.common.util.SystemPropertyAction; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.helpers.DOMUtils; -import org.apache.cxf.helpers.XMLUtils; public final class StaxUtils { // System properies for defaults, but also contextual properties usable @@ -639,6 +642,36 @@ public final class StaxUtils { xsw.close(); } + public static void writeTo(Node node, OutputStream os) throws XMLStreamException { + copy(new DOMSource(node), os); + } + public static void writeTo(Node node, OutputStream os, int indent) throws XMLStreamException { + if (indent > 0) { + XMLStreamWriter writer = new PrettyPrintXMLStreamWriter(createXMLStreamWriter(os), indent); + try { + copy(new DOMSource(node), writer); + } finally { + writer.close(); + } + } else { + copy(new DOMSource(node), os); + } + } + public static void writeTo(Node node, Writer os) throws XMLStreamException { + writeTo(node, os, 0); + } + public static void writeTo(Node node, Writer os, int indent) throws XMLStreamException { + XMLStreamWriter writer = createXMLStreamWriter(os); + if (indent > 0) { + writer = new PrettyPrintXMLStreamWriter(writer, indent); + } + try { + copy(new DOMSource(node), writer); + } finally { + writer.close(); + } + } + /** * Copies the reader to the writer. The start and end document methods must @@ -1043,6 +1076,26 @@ public final class StaxUtils { } } } + public static Document read(Reader s) throws XMLStreamException { + XMLStreamReader reader = createXMLStreamReader(s); + try { + return read(reader); + } finally { + try { + reader.close(); + } catch (Exception ex) { + //ignore + } + } + } + public static Document read(File is) throws XMLStreamException, IOException { + InputStream fin = new FileInputStream(is); + try { + return read(fin); + } finally { + fin.close(); + } + } public static Document read(InputSource s) throws XMLStreamException { XMLStreamReader reader = createXMLStreamReader(s); try { @@ -1549,7 +1602,16 @@ public final class StaxUtils { public static void printXmlFragment(XMLStreamReader reader) { try { - LOG.info(XMLUtils.toString(StaxUtils.read(reader), 4)); + StringWriter sw = new StringWriter(1024); + XMLStreamWriter writer = null; + try { + writer = new PrettyPrintXMLStreamWriter(createXMLStreamWriter(sw), 4); + copy(reader, writer); + writer.flush(); + } finally { + StaxUtils.close(writer); + } + LOG.info(sw.toString()); } catch (XMLStreamException e) { LOG.severe(e.getMessage()); } @@ -1667,6 +1729,21 @@ public final class StaxUtils { } } + public static String toString(Source src) throws XMLStreamException { + StringWriter sw = new StringWriter(1024); + XMLStreamWriter writer = null; + try { + writer = createXMLStreamWriter(sw); + copy(src, writer); + writer.flush(); + } finally { + StaxUtils.close(writer); + } + return sw.toString(); + } + public static String toString(Node src) throws XMLStreamException { + return toString(new DOMSource(src)); + } public static String toString(Document doc) throws XMLStreamException { StringWriter sw = new StringWriter(1024); XMLStreamWriter writer = null; @@ -1674,24 +1751,33 @@ public final class StaxUtils { writer = createXMLStreamWriter(sw); copy(doc, writer); writer.flush(); + } catch (XMLStreamException e) { + throw new RuntimeException(e); } finally { StaxUtils.close(writer); } return sw.toString(); } public static String toString(Element el) throws XMLStreamException { + return toString(el, 0); + } + public static String toString(Element el, int indent) { StringWriter sw = new StringWriter(1024); XMLStreamWriter writer = null; try { writer = createXMLStreamWriter(sw); + if (indent > 0) { + writer = new PrettyPrintXMLStreamWriter(writer, indent); + } copy(el, writer); writer.flush(); + } catch (XMLStreamException e) { + throw new RuntimeException(e); } finally { StaxUtils.close(writer); } - return sw.toString(); + return sw.toString(); } - public static void close(XMLStreamReader reader) { if (reader != null) { try {
