You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ru...@apache.org on 2001/06/06 17:54:24 UTC
cvs commit: xml-axis/java/src/org/apache/axis/utils DOM2Writer.java NSStack.java XMLUtils.java
rubys 01/06/06 08:54:23
Modified: java/src/org/apache/axis/utils NSStack.java XMLUtils.java
Added: java/src/org/apache/axis/utils DOM2Writer.java
Log:
Add DOM2Writer from xml-soap. It was nearly a drop-in replacement - the
only changes were to convert to Axis's NSStack and make the intial xml
header optional.
This removes the dependency on Xalan.
Revision Changes Path
1.3 +34 -1 xml-axis/java/src/org/apache/axis/utils/NSStack.java
Index: NSStack.java
===================================================================
RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/NSStack.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NSStack.java 2001/04/26 23:14:13 1.2
+++ NSStack.java 2001/06/06 15:54:17 1.3
@@ -67,12 +67,18 @@
private Stack stack = new Stack();
+ private NSStack parent = null;
+
public NSStack() {}
public NSStack(Hashtable table) {
push(table);
}
+ public NSStack(NSStack parent) {
+ this.parent = parent;
+ }
+
public void push() {
if (stack == null) stack = new Stack();
if (DEBUG_LOG)
@@ -89,7 +95,11 @@
public Hashtable peek() {
if (stack.isEmpty())
- return null;
+ if (parent != null)
+ return parent.peek();
+ else
+ return null;
+
return (Hashtable)stack.peek();
}
@@ -98,6 +108,8 @@
if (stack.isEmpty()) {
if (DEBUG_LOG)
System.out.println("NSPop (empty)");
+ if (parent != null)
+ return parent.pop();
return null;
}
@@ -116,6 +128,9 @@
table.put(namespaceURI, prefix);
}
+ /**
+ * remove a namespace from the topmost hashtable on the stack
+ */
public void remove(String namespaceURI) {
if (stack.isEmpty()) return;
peek().remove(namespaceURI);
@@ -131,6 +146,8 @@
}
}
+ if (parent != null)
+ return parent.getPrefix(namespaceURI);
return null;
}
@@ -150,6 +167,9 @@
}
}
+ if (parent != null)
+ return parent.getNamespaceURI(prefix);
+
dump();
System.out.println("didn't find prefix '" + prefix + "'");
new Exception().printStackTrace();
@@ -168,6 +188,9 @@
}
}
+ if (parent != null)
+ return parent.searchTable(t, prefix);
+
return null;
}
@@ -179,6 +202,10 @@
return true;
}
}
+
+ if (parent != null)
+ return parent.isDeclared(namespaceURI);
+
return false;
}
@@ -198,6 +225,12 @@
System.out.println(key + " -> " + map.get(key));
}
}
+
+ if (parent != null) {
+ System.out.println("----parent");
+ parent.dump();
+ }
+
System.out.println("----end");
}
}
1.10 +3 -51 xml-axis/java/src/org/apache/axis/utils/XMLUtils.java
Index: XMLUtils.java
===================================================================
RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/utils/XMLUtils.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- XMLUtils.java 2001/05/23 18:19:11 1.9
+++ XMLUtils.java 2001/06/06 15:54:18 1.10
@@ -59,9 +59,6 @@
import java.util.Properties;
import org.w3c.dom.* ;
import javax.xml.parsers.* ;
-import javax.xml.transform.*;
-import javax.xml.transform.dom.*;
-import javax.xml.transform.stream.*;
import org.xml.sax.* ;
import org.apache.axis.Constants;
@@ -163,44 +160,10 @@
}
}
- /** Obtain a JAXP Transformer.
- *
- * (I'm assuming that using the transformer API is the accepted JAXP-style
- * way to do XML serialization)
- *
- * @return a JAXP Transformer
- */
- public static Transformer getTransformer()
- {
- try {
- TransformerFactory factory = TransformerFactory.newInstance();
- return factory.newTransformer();
- } catch (TransformerConfigurationException e) {
- e.printStackTrace();
- return null;
- }
- }
-
private static String privateElementToString(Element element,
boolean omitXMLDecl)
{
- try {
- StringWriter sw = new StringWriter();
- DOMSource source = new DOMSource(element);
- StreamResult result = new StreamResult(sw);
- Transformer transformer = getTransformer();
- Properties p = new Properties();
- p.put(OutputKeys.OMIT_XML_DECLARATION,
- omitXMLDecl ? "yes" : "no");
- transformer.setOutputProperties(p);
- transformer.transform(source, result);
- sw.close();
- return sw.toString();
- }
- catch( Exception e) {
- e.printStackTrace();
- }
- return( null );
+ return DOM2Writer.nodeToString(element, omitXMLDecl);
}
public static String ElementToString(Element element) {
@@ -213,19 +176,8 @@
public static void privateElementToStream(Element element, OutputStream out,
boolean omitXMLDecl) {
- try {
- Transformer transformer = getTransformer();
- DOMSource source = new DOMSource(element);
- StreamResult result = new StreamResult(out);
- Properties p = new Properties();
- p.put(OutputKeys.OMIT_XML_DECLARATION,
- omitXMLDecl ? "yes" : "no");
- transformer.setOutputProperties(p);
- transformer.transform(source, result);
- }
- catch( Exception e ) {
- e.printStackTrace();
- }
+ Writer writer = new OutputStreamWriter(out);
+ DOM2Writer.serializeAsXML(element, writer, omitXMLDecl);
}
public static void ElementToStream(Element element, OutputStream out) {
1.1 xml-axis/java/src/org/apache/axis/utils/DOM2Writer.java
Index: DOM2Writer.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "SOAP" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2000, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.axis.utils;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import org.apache.axis.utils.*;
/**
* This class is a utility to serialize a DOM node as XML. This class
* uses the <code>DOM Level 2</code> APIs.
* The main difference between this class and DOMWriter is that this class
* generates and prints out namespace declarations.
*
* @author Matthew J. Duftler (duftler@us.ibm.com)
* @author Joseph Kesselman
*/
public class DOM2Writer
{
/**
* The namespaceURI represented by the prefix <code>xmlns</code>.
*/
private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/";
/**
* The prefered line separator
*/
private static final String lineSeparator =
System.getProperty("line.separator", "\n");
/**
* Return a string containing this node serialized as XML.
*/
public static String nodeToString(Node node, boolean omitXMLDecl)
{
StringWriter sw = new StringWriter();
serializeAsXML(node, sw, omitXMLDecl);
return sw.toString();
}
/**
* Serialize this node into the writer as XML.
*/
public static void serializeAsXML(Node node, Writer writer,
boolean omitXMLDecl)
{
PrintWriter out = new PrintWriter(writer);
if (!omitXMLDecl)
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
print(node, null, out);
out.flush();
}
private static void print(Node node, NSStack namespaceStack,
PrintWriter out)
{
if (node == null)
{
return;
}
boolean hasChildren = false;
int type = node.getNodeType();
switch (type)
{
case Node.DOCUMENT_NODE :
{
NodeList children = node.getChildNodes();
if (children != null)
{
int numChildren = children.getLength();
for (int i = 0; i < numChildren; i++)
{
print(children.item(i), namespaceStack, out);
}
}
break;
}
case Node.ELEMENT_NODE :
{
namespaceStack = new NSStack(namespaceStack);
out.print('<' + node.getNodeName());
String elPrefix = node.getPrefix();
String elNamespaceURI = node.getNamespaceURI();
if (elPrefix != null && elNamespaceURI != null)
{
boolean prefixIsDeclared = false;
try
{
String namespaceURI = namespaceStack.getNamespaceURI(elPrefix);
if (elNamespaceURI.equals(namespaceURI))
{
prefixIsDeclared = true;
}
}
catch (IllegalArgumentException e)
{
}
if (!prefixIsDeclared)
{
printNamespaceDecl(node, namespaceStack, out);
}
}
NamedNodeMap attrs = node.getAttributes();
int len = (attrs != null) ? attrs.getLength() : 0;
for (int i = 0; i < len; i++)
{
Attr attr = (Attr)attrs.item(i);
out.print(' ' + attr.getNodeName() +"=\"" +
normalize(attr.getValue()) + '\"');
String attrPrefix = attr.getPrefix();
String attrNamespaceURI = attr.getNamespaceURI();
if (attrPrefix != null && attrNamespaceURI != null)
{
boolean prefixIsDeclared = false;
try
{
String namespaceURI = namespaceStack.getNamespaceURI(attrPrefix);
if (attrNamespaceURI.equals(namespaceURI))
{
prefixIsDeclared = true;
}
}
catch (IllegalArgumentException e)
{
}
if (!prefixIsDeclared)
{
printNamespaceDecl(attr, namespaceStack, out);
}
}
}
NodeList children = node.getChildNodes();
if (children != null)
{
int numChildren = children.getLength();
hasChildren = (numChildren > 0);
if (hasChildren)
{
out.print('>');
}
for (int i = 0; i < numChildren; i++)
{
print(children.item(i), namespaceStack, out);
}
}
else
{
hasChildren = false;
}
if (!hasChildren)
{
out.print("/>");
}
break;
}
case Node.ENTITY_REFERENCE_NODE :
{
out.print('&');
out.print(node.getNodeName());
out.print(';');
break;
}
case Node.CDATA_SECTION_NODE :
{
out.print("<![CDATA[");
out.print(node.getNodeValue());
out.print("]]>");
break;
}
case Node.TEXT_NODE :
{
out.print(normalize(node.getNodeValue()));
break;
}
case Node.COMMENT_NODE :
{
out.print("<!--");
out.print(node.getNodeValue());
out.print("-->");
break;
}
case Node.PROCESSING_INSTRUCTION_NODE :
{
out.print("<?");
out.print(node.getNodeName());
String data = node.getNodeValue();
if (data != null && data.length() > 0)
{
out.print(' ');
out.print(data);
}
out.println("?>");
break;
}
}
if (type == Node.ELEMENT_NODE && hasChildren == true)
{
out.print("</");
out.print(node.getNodeName());
out.print('>');
hasChildren = false;
}
}
private static void printNamespaceDecl(Node node,
NSStack namespaceStack,
PrintWriter out)
{
switch (node.getNodeType())
{
case Node.ATTRIBUTE_NODE :
{
printNamespaceDecl(((Attr)node).getOwnerElement(), node,
namespaceStack, out);
break;
}
case Node.ELEMENT_NODE :
{
printNamespaceDecl((Element)node, node, namespaceStack, out);
break;
}
}
}
private static void printNamespaceDecl(Element owner, Node node,
NSStack namespaceStack,
PrintWriter out)
{
String namespaceURI = node.getNamespaceURI();
String prefix = node.getPrefix();
if (!(namespaceURI.equals(NS_URI_XMLNS) && prefix.equals("xmlns")))
{
if (XMLUtils.getNamespace(prefix, owner) == null)
{
out.print(" xmlns:" + prefix + "=\"" + namespaceURI + '\"');
}
}
else
{
prefix = node.getLocalName();
namespaceURI = node.getNodeValue();
}
namespaceStack.add(namespaceURI, prefix);
}
private static String normalize(String s)
{
StringBuffer str = new StringBuffer();
int len = (s != null) ? s.length() : 0;
for (int i = 0; i < len; i++)
{
char ch = s.charAt(i);
switch (ch)
{
case '<' :
{
str.append("<");
break;
}
case '>' :
{
str.append(">");
break;
}
case '&' :
{
str.append("&");
break;
}
case '"' :
{
str.append(""");
break;
}
case '\n' :
{
if (i > 0)
{
char lastChar = str.charAt(str.length() - 1);
if (lastChar != '\r')
{
str.append(lineSeparator);
}
else
{
str.append('\n');
}
}
else
{
str.append(lineSeparator);
}
break;
}
default :
{
str.append(ch);
}
}
}
return (str.toString());
}
}