Modified:
incubator/woden/trunk/java/src/org/apache/woden/internal/util/StringUtils.java
URL:
http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/StringUtils.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
---
incubator/woden/trunk/java/src/org/apache/woden/internal/util/StringUtils.java
(original)
+++
incubator/woden/trunk/java/src/org/apache/woden/internal/util/StringUtils.java
Thu Aug 23 04:01:23 2007
@@ -1,238 +1,238 @@
-/**
+/**
* 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.woden.internal.util;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.List;
-import java.util.StringTokenizer;
-import java.util.Vector;
-
-/**
- * This class originated from WSDL4J.
- *
- * @author Matthew J. Duftler
- * @author [EMAIL PROTECTED] (Woden changes)
- */
-public class StringUtils {
-
- public static final String lineSeparator =
- System.getProperty("line.separator", "\n");
- public static final String lineSeparatorStr = cleanString(lineSeparator);
-
- // Ensure that escape sequences are passed through properly.
- public static String cleanString(String str)
- {
- if (str == null)
- return null;
- else
- {
- char[] charArray = str.toCharArray();
- StringBuffer sBuf = new StringBuffer();
-
- for (int i = 0; i < charArray.length; i++)
- switch (charArray[i])
- {
- case '\"' : sBuf.append("\\\"");
- break;
- case '\\' : sBuf.append("\\\\");
- break;
- case '\n' : sBuf.append("\\n");
- break;
- case '\r' : sBuf.append("\\r");
- break;
- default : sBuf.append(charArray[i]);
- break;
- }
-
- return sBuf.toString();
- }
- }
-
- /*
- This method will return the correct name for a class object
representing
- a primitive, a single instance of a class, as well as n-dimensional
arrays
- of primitives or instances. This logic is needed to handle the string
returned
- from Class.getName(). If the class object represents a single instance
(or
- a primitive), Class.getName() returns the fully-qualified name of the
class
- and no further work is needed. However, if the class object represents
an
- array (of n dimensions), Class.getName() returns a Descriptor (the
Descriptor
- grammar is defined in section 4.3 of the Java VM Spec). This method
will
- parse the Descriptor if necessary.
- */
- public static String getClassName(Class targetClass)
- {
- String className = targetClass.getName();
-
- return targetClass.isArray() ? parseDescriptor(className) : className;
- }
-
- /*
- See the comment above for getClassName(targetClass)...
- */
- private static String parseDescriptor(String className)
- {
- char[] classNameChars = className.toCharArray();
- int arrayDim = 0;
- int i = 0;
-
- while (classNameChars[i] == '[')
- {
- arrayDim++;
- i++;
- }
-
- StringBuffer classNameBuf = new StringBuffer();
-
- switch (classNameChars[i++])
- {
- case 'B' : classNameBuf.append("byte");
- break;
- case 'C' : classNameBuf.append("char");
- break;
- case 'D' : classNameBuf.append("double");
- break;
- case 'F' : classNameBuf.append("float");
- break;
- case 'I' : classNameBuf.append("int");
- break;
- case 'J' : classNameBuf.append("long");
- break;
- case 'S' : classNameBuf.append("short");
- break;
- case 'Z' : classNameBuf.append("boolean");
- break;
- case 'L' : classNameBuf.append(classNameChars,
- i, classNameChars.length - i - 1);
- break;
- }
-
- for (i = 0; i < arrayDim; i++)
- classNameBuf.append("[]");
-
- return classNameBuf.toString();
- }
-
- /*
- * Return a URL created from a context URL and a relative URI.
- * If a valid URL cannot be created the only other possibility
- * this method will consider is that an absolute file path has
- * been passed in as the relative URI argument, and it will try
- * to create a 'file' URL from it.
- *
- * @param contextURL the document base URL
- * @param fileSpec a file URI relative to the contextURL or an
- * absolute file path
- * @return the URL created from contextURL and fileSpec
- */
- public static URL getURL(URL contextURL, String fileSpec)
- throws MalformedURLException {
-
- URL url = null;
-
- try {
- url = new URL(contextURL, fileSpec);
- }
- catch (MalformedURLException e) {
-
- File file = new File(fileSpec);
- if (file.isAbsolute()) {
- url = file.toURL();
- }
- else {
- throw e;
- }
- }
-
- return url;
- }
-
- public static InputStream getContentAsInputStream(URL url)
- throws IllegalArgumentException, IOException {
-
- if (url == null)
- {
- //TODO externalize the message
- throw new IllegalArgumentException("URL cannot be null.");
- }
-
- //TODO consider exception handling used in wsdl4j
- Object content = url.getContent();
-
- if (content == null)
- {
- //TODO externalize the message
- throw new IllegalArgumentException("No content.");
- }
-
- if (content instanceof InputStream)
- {
- return (InputStream)content;
- }
- else
- {
- //TODO externalize the message
- throw new IllegalArgumentException((content instanceof String)
- ? (String)content
- : "This URL points to a: " +
- StringUtils.getClassName(content.getClass()));
- }
- }
-
- public static List parseNMTokens(String nmTokens)
- {
- return parseNMTokens(nmTokens, " ");
- }
-
- public static List parseNMTokens(String nmTokens, String delimiter)
- {
- StringTokenizer strTok = new StringTokenizer(nmTokens, delimiter);
- List tokens = new Vector();
-
- while (strTok.hasMoreTokens())
- {
- tokens.add(strTok.nextToken());
- }
-
- return tokens;
- }
-
- public static String getNMTokens(List list)
- {
- if (list != null)
- {
- StringBuffer strBuf = new StringBuffer();
- int size = list.size();
-
- for (int i = 0; i < size; i++)
- {
- String token = (String)list.get(i);
-
- strBuf.append((i > 0 ? " " : "") + token);
- }
-
- return strBuf.toString();
- }
- else
- {
- return null;
- }
- }
-}
+ *
+ * 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.woden.internal.util;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+import java.util.StringTokenizer;
+import java.util.Vector;
+
+/**
+ * This class originated from WSDL4J.
+ *
+ * @author Matthew J. Duftler
+ * @author [EMAIL PROTECTED] (Woden changes)
+ */
+public class StringUtils {
+
+ public static final String lineSeparator =
+ System.getProperty("line.separator", "\n");
+ public static final String lineSeparatorStr = cleanString(lineSeparator);
+
+ // Ensure that escape sequences are passed through properly.
+ public static String cleanString(String str)
+ {
+ if (str == null)
+ return null;
+ else
+ {
+ char[] charArray = str.toCharArray();
+ StringBuffer sBuf = new StringBuffer();
+
+ for (int i = 0; i < charArray.length; i++)
+ switch (charArray[i])
+ {
+ case '\"' : sBuf.append("\\\"");
+ break;
+ case '\\' : sBuf.append("\\\\");
+ break;
+ case '\n' : sBuf.append("\\n");
+ break;
+ case '\r' : sBuf.append("\\r");
+ break;
+ default : sBuf.append(charArray[i]);
+ break;
+ }
+
+ return sBuf.toString();
+ }
+ }
+
+ /*
+ This method will return the correct name for a class object
representing
+ a primitive, a single instance of a class, as well as n-dimensional
arrays
+ of primitives or instances. This logic is needed to handle the string
returned
+ from Class.getName(). If the class object represents a single instance
(or
+ a primitive), Class.getName() returns the fully-qualified name of the
class
+ and no further work is needed. However, if the class object represents
an
+ array (of n dimensions), Class.getName() returns a Descriptor (the
Descriptor
+ grammar is defined in section 4.3 of the Java VM Spec). This method
will
+ parse the Descriptor if necessary.
+ */
+ public static String getClassName(Class targetClass)
+ {
+ String className = targetClass.getName();
+
+ return targetClass.isArray() ? parseDescriptor(className) : className;
+ }
+
+ /*
+ See the comment above for getClassName(targetClass)...
+ */
+ private static String parseDescriptor(String className)
+ {
+ char[] classNameChars = className.toCharArray();
+ int arrayDim = 0;
+ int i = 0;
+
+ while (classNameChars[i] == '[')
+ {
+ arrayDim++;
+ i++;
+ }
+
+ StringBuffer classNameBuf = new StringBuffer();
+
+ switch (classNameChars[i++])
+ {
+ case 'B' : classNameBuf.append("byte");
+ break;
+ case 'C' : classNameBuf.append("char");
+ break;
+ case 'D' : classNameBuf.append("double");
+ break;
+ case 'F' : classNameBuf.append("float");
+ break;
+ case 'I' : classNameBuf.append("int");
+ break;
+ case 'J' : classNameBuf.append("long");
+ break;
+ case 'S' : classNameBuf.append("short");
+ break;
+ case 'Z' : classNameBuf.append("boolean");
+ break;
+ case 'L' : classNameBuf.append(classNameChars,
+ i, classNameChars.length - i - 1);
+ break;
+ }
+
+ for (i = 0; i < arrayDim; i++)
+ classNameBuf.append("[]");
+
+ return classNameBuf.toString();
+ }
+
+ /*
+ * Return a URL created from a context URL and a relative URI.
+ * If a valid URL cannot be created the only other possibility
+ * this method will consider is that an absolute file path has
+ * been passed in as the relative URI argument, and it will try
+ * to create a 'file' URL from it.
+ *
+ * @param contextURL the document base URL
+ * @param fileSpec a file URI relative to the contextURL or an
+ * absolute file path
+ * @return the URL created from contextURL and fileSpec
+ */
+ public static URL getURL(URL contextURL, String fileSpec)
+ throws MalformedURLException {
+
+ URL url = null;
+
+ try {
+ url = new URL(contextURL, fileSpec);
+ }
+ catch (MalformedURLException e) {
+
+ File file = new File(fileSpec);
+ if (file.isAbsolute()) {
+ url = file.toURL();
+ }
+ else {
+ throw e;
+ }
+ }
+
+ return url;
+ }
+
+ public static InputStream getContentAsInputStream(URL url)
+ throws IllegalArgumentException, IOException {
+
+ if (url == null)
+ {
+ //TODO externalize the message
+ throw new IllegalArgumentException("URL cannot be null.");
+ }
+
+ //TODO consider exception handling used in wsdl4j
+ Object content = url.getContent();
+
+ if (content == null)
+ {
+ //TODO externalize the message
+ throw new IllegalArgumentException("No content.");
+ }
+
+ if (content instanceof InputStream)
+ {
+ return (InputStream)content;
+ }
+ else
+ {
+ //TODO externalize the message
+ throw new IllegalArgumentException((content instanceof String)
+ ? (String)content
+ : "This URL points to a: " +
+ StringUtils.getClassName(content.getClass()));
+ }
+ }
+
+ public static List parseNMTokens(String nmTokens)
+ {
+ return parseNMTokens(nmTokens, " ");
+ }
+
+ public static List parseNMTokens(String nmTokens, String delimiter)
+ {
+ StringTokenizer strTok = new StringTokenizer(nmTokens, delimiter);
+ List tokens = new Vector();
+
+ while (strTok.hasMoreTokens())
+ {
+ tokens.add(strTok.nextToken());
+ }
+
+ return tokens;
+ }
+
+ public static String getNMTokens(List list)
+ {
+ if (list != null)
+ {
+ StringBuffer strBuf = new StringBuffer();
+ int size = list.size();
+
+ for (int i = 0; i < size; i++)
+ {
+ String token = (String)list.get(i);
+
+ strBuf.append((i > 0 ? " " : "") + token);
+ }
+
+ return strBuf.toString();
+ }
+ else
+ {
+ return null;
+ }
+ }
+}
Propchange:
incubator/woden/trunk/java/src/org/apache/woden/internal/util/StringUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOM2Writer.java
URL:
http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOM2Writer.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
---
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOM2Writer.java
(original)
+++
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOM2Writer.java
Thu Aug 23 04:01:23 2007
@@ -1,451 +1,451 @@
-/**
+/**
* 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.woden.internal.util.dom;
-
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.io.Writer;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.apache.woden.internal.util.ObjectRegistry;
-import org.apache.woden.internal.util.StringUtils;
-import org.apache.woden.internal.wsdl20.Constants;
-
-import org.w3c.dom.Attr;
-import org.w3c.dom.Element;
-import org.w3c.dom.NamedNodeMap;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-/**
- * This class copied from WSDL4J.
- *
- * 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 ([EMAIL PROTECTED])
- * @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 namespaceURI represented by the prefix <code>xml</code>.
- */
- private static String NS_URI_XML = "http://www.w3.org/XML/1998/namespace";
-
- private static Map xmlEncodingMap = new HashMap();
-
- static
- {
- xmlEncodingMap.put(null, Constants.XML_DECL_DEFAULT);
- xmlEncodingMap.put(System.getProperty("file.encoding"),
- Constants.XML_DECL_DEFAULT);
- xmlEncodingMap.put("UTF8", "UTF-8");
- xmlEncodingMap.put("UTF-16", "UTF-16");
- xmlEncodingMap.put("UnicodeBig", "UTF-16");
- xmlEncodingMap.put("UnicodeLittle", "UTF-16");
- xmlEncodingMap.put("ASCII", "US-ASCII");
- xmlEncodingMap.put("ISO8859_1", "ISO-8859-1");
- xmlEncodingMap.put("ISO8859_2", "ISO-8859-2");
- xmlEncodingMap.put("ISO8859_3", "ISO-8859-3");
- xmlEncodingMap.put("ISO8859_4", "ISO-8859-4");
- xmlEncodingMap.put("ISO8859_5", "ISO-8859-5");
- xmlEncodingMap.put("ISO8859_6", "ISO-8859-6");
- xmlEncodingMap.put("ISO8859_7", "ISO-8859-7");
- xmlEncodingMap.put("ISO8859_8", "ISO-8859-8");
- xmlEncodingMap.put("ISO8859_9", "ISO-8859-9");
- xmlEncodingMap.put("ISO8859_13", "ISO-8859-13");
- xmlEncodingMap.put("ISO8859_15_FDIS", "ISO-8859-15");
- xmlEncodingMap.put("GBK", "GBK");
- xmlEncodingMap.put("Big5", "Big5");
- }
-
- /**
- * Return a string containing this node serialized as XML.
- */
- public static String nodeToString(Node node)
- {
- StringWriter sw = new StringWriter();
-
- serializeAsXML(node, sw);
-
- return sw.toString();
- }
-
-
- /**
- * Print an XML declaration before serializing the element.
- */
- public static void serializeElementAsDocument(Element el, Writer writer)
- {
- PrintWriter pw = new PrintWriter(writer);
- String javaEncoding = (writer instanceof OutputStreamWriter)
- ? ((OutputStreamWriter) writer).getEncoding()
- : null;
-
- String xmlEncoding = java2XMLEncoding(javaEncoding);
-
- if (xmlEncoding != null)
- {
- pw.println(Constants.XML_DECL_START +
- xmlEncoding +
- Constants.XML_DECL_END);
- }
- else
- {
- pw.println("<?xml version=\"1.0\"?>");
- }
-
- serializeAsXML(el, writer);
- }
-
- /**
- * Serialize this node into the writer as XML.
- */
- public static void serializeAsXML(Node node, Writer writer)
- {
- ObjectRegistry namespaceStack = new ObjectRegistry();
-
- namespaceStack.register("xml", NS_URI_XML);
-
- PrintWriter pw = new PrintWriter(writer);
- String javaEncoding = (writer instanceof OutputStreamWriter)
- ? ((OutputStreamWriter) writer).getEncoding()
- : null;
-
- print(node, namespaceStack, pw, java2XMLEncoding(javaEncoding));
- }
-
- private static void print(Node node, ObjectRegistry namespaceStack,
- PrintWriter out, String xmlEncoding)
- {
- if (node == null)
- {
- return;
- }
-
- boolean hasChildren = false;
- int type = node.getNodeType();
-
- switch (type)
- {
- case Node.DOCUMENT_NODE :
- {
- if (xmlEncoding != null)
- {
- out.println(Constants.XML_DECL_START +
- xmlEncoding +
- Constants.XML_DECL_END);
- }
- else
- {
- out.println("<?xml version=\"1.0\"?>");
- }
-
- NodeList children = node.getChildNodes();
-
- if (children != null)
- {
- int numChildren = children.getLength();
-
- for (int i = 0; i < numChildren; i++)
- {
- print(children.item(i), namespaceStack, out, xmlEncoding);
- }
- }
- break;
- }
-
- case Node.ELEMENT_NODE :
- {
- namespaceStack = new ObjectRegistry(namespaceStack);
-
- out.print('<' + node.getNodeName());
-
- String elPrefix = node.getPrefix();
- String elNamespaceURI = node.getNamespaceURI();
-
- if (elPrefix != null && elNamespaceURI != null)
- {
- boolean prefixIsDeclared = false;
-
- try
- {
- String namespaceURI = (String)namespaceStack.lookup(elPrefix);
-
- if (elNamespaceURI.equals(namespaceURI))
- {
- prefixIsDeclared = true;
- }
- }
- catch (IllegalArgumentException e)
- {
- // ignore this and carry on
- }
-
- 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 = (String)namespaceStack.lookup(attrPrefix);
-
- if (attrNamespaceURI.equals(namespaceURI))
- {
- prefixIsDeclared = true;
- }
- }
- catch (IllegalArgumentException e)
- {
- // ignore this and carry on
- }
-
- 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, xmlEncoding);
- }
- }
- 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;
- }
- }
-
- public static String java2XMLEncoding(String javaEnc)
- {
- return (String)xmlEncodingMap.get(javaEnc);
- }
-
-
- private static void printNamespaceDecl(Node node,
- ObjectRegistry 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,
- ObjectRegistry namespaceStack,
- PrintWriter out)
- {
- String namespaceURI = node.getNamespaceURI();
- String prefix = node.getPrefix();
-
- if (!(namespaceURI.equals(NS_URI_XMLNS) && prefix.equals("xmlns")))
- {
- if (DOMUtils.getAttributeNS(owner, NS_URI_XMLNS, prefix) == null)
- {
- out.print(" xmlns:" + prefix + "=\"" + namespaceURI + '\"');
- }
- }
- else
- {
- prefix = node.getLocalName();
- namespaceURI = node.getNodeValue();
- }
-
- namespaceStack.register(prefix, namespaceURI);
- }
-
- 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(StringUtils.lineSeparator);
- }
- else
- {
- str.append('\n');
- }
- }
- else
- {
- str.append(StringUtils.lineSeparator);
- }
- break;
- }
- default :
- {
- str.append(ch);
- }
- }
- }
-
- return (str.toString());
- }
+ *
+ * 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.woden.internal.util.dom;
+
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.woden.internal.util.ObjectRegistry;
+import org.apache.woden.internal.util.StringUtils;
+import org.apache.woden.internal.wsdl20.Constants;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * This class copied from WSDL4J.
+ *
+ * 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 ([EMAIL PROTECTED])
+ * @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 namespaceURI represented by the prefix <code>xml</code>.
+ */
+ private static String NS_URI_XML = "http://www.w3.org/XML/1998/namespace";
+
+ private static Map xmlEncodingMap = new HashMap();
+
+ static
+ {
+ xmlEncodingMap.put(null, Constants.XML_DECL_DEFAULT);
+ xmlEncodingMap.put(System.getProperty("file.encoding"),
+ Constants.XML_DECL_DEFAULT);
+ xmlEncodingMap.put("UTF8", "UTF-8");
+ xmlEncodingMap.put("UTF-16", "UTF-16");
+ xmlEncodingMap.put("UnicodeBig", "UTF-16");
+ xmlEncodingMap.put("UnicodeLittle", "UTF-16");
+ xmlEncodingMap.put("ASCII", "US-ASCII");
+ xmlEncodingMap.put("ISO8859_1", "ISO-8859-1");
+ xmlEncodingMap.put("ISO8859_2", "ISO-8859-2");
+ xmlEncodingMap.put("ISO8859_3", "ISO-8859-3");
+ xmlEncodingMap.put("ISO8859_4", "ISO-8859-4");
+ xmlEncodingMap.put("ISO8859_5", "ISO-8859-5");
+ xmlEncodingMap.put("ISO8859_6", "ISO-8859-6");
+ xmlEncodingMap.put("ISO8859_7", "ISO-8859-7");
+ xmlEncodingMap.put("ISO8859_8", "ISO-8859-8");
+ xmlEncodingMap.put("ISO8859_9", "ISO-8859-9");
+ xmlEncodingMap.put("ISO8859_13", "ISO-8859-13");
+ xmlEncodingMap.put("ISO8859_15_FDIS", "ISO-8859-15");
+ xmlEncodingMap.put("GBK", "GBK");
+ xmlEncodingMap.put("Big5", "Big5");
+ }
+
+ /**
+ * Return a string containing this node serialized as XML.
+ */
+ public static String nodeToString(Node node)
+ {
+ StringWriter sw = new StringWriter();
+
+ serializeAsXML(node, sw);
+
+ return sw.toString();
+ }
+
+
+ /**
+ * Print an XML declaration before serializing the element.
+ */
+ public static void serializeElementAsDocument(Element el, Writer writer)
+ {
+ PrintWriter pw = new PrintWriter(writer);
+ String javaEncoding = (writer instanceof OutputStreamWriter)
+ ? ((OutputStreamWriter) writer).getEncoding()
+ : null;
+
+ String xmlEncoding = java2XMLEncoding(javaEncoding);
+
+ if (xmlEncoding != null)
+ {
+ pw.println(Constants.XML_DECL_START +
+ xmlEncoding +
+ Constants.XML_DECL_END);
+ }
+ else
+ {
+ pw.println("<?xml version=\"1.0\"?>");
+ }
+
+ serializeAsXML(el, writer);
+ }
+
+ /**
+ * Serialize this node into the writer as XML.
+ */
+ public static void serializeAsXML(Node node, Writer writer)
+ {
+ ObjectRegistry namespaceStack = new ObjectRegistry();
+
+ namespaceStack.register("xml", NS_URI_XML);
+
+ PrintWriter pw = new PrintWriter(writer);
+ String javaEncoding = (writer instanceof OutputStreamWriter)
+ ? ((OutputStreamWriter) writer).getEncoding()
+ : null;
+
+ print(node, namespaceStack, pw, java2XMLEncoding(javaEncoding));
+ }
+
+ private static void print(Node node, ObjectRegistry namespaceStack,
+ PrintWriter out, String xmlEncoding)
+ {
+ if (node == null)
+ {
+ return;
+ }
+
+ boolean hasChildren = false;
+ int type = node.getNodeType();
+
+ switch (type)
+ {
+ case Node.DOCUMENT_NODE :
+ {
+ if (xmlEncoding != null)
+ {
+ out.println(Constants.XML_DECL_START +
+ xmlEncoding +
+ Constants.XML_DECL_END);
+ }
+ else
+ {
+ out.println("<?xml version=\"1.0\"?>");
+ }
+
+ NodeList children = node.getChildNodes();
+
+ if (children != null)
+ {
+ int numChildren = children.getLength();
+
+ for (int i = 0; i < numChildren; i++)
+ {
+ print(children.item(i), namespaceStack, out, xmlEncoding);
+ }
+ }
+ break;
+ }
+
+ case Node.ELEMENT_NODE :
+ {
+ namespaceStack = new ObjectRegistry(namespaceStack);
+
+ out.print('<' + node.getNodeName());
+
+ String elPrefix = node.getPrefix();
+ String elNamespaceURI = node.getNamespaceURI();
+
+ if (elPrefix != null && elNamespaceURI != null)
+ {
+ boolean prefixIsDeclared = false;
+
+ try
+ {
+ String namespaceURI = (String)namespaceStack.lookup(elPrefix);
+
+ if (elNamespaceURI.equals(namespaceURI))
+ {
+ prefixIsDeclared = true;
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ // ignore this and carry on
+ }
+
+ 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 = (String)namespaceStack.lookup(attrPrefix);
+
+ if (attrNamespaceURI.equals(namespaceURI))
+ {
+ prefixIsDeclared = true;
+ }
+ }
+ catch (IllegalArgumentException e)
+ {
+ // ignore this and carry on
+ }
+
+ 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, xmlEncoding);
+ }
+ }
+ 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;
+ }
+ }
+
+ public static String java2XMLEncoding(String javaEnc)
+ {
+ return (String)xmlEncodingMap.get(javaEnc);
+ }
+
+
+ private static void printNamespaceDecl(Node node,
+ ObjectRegistry 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,
+ ObjectRegistry namespaceStack,
+ PrintWriter out)
+ {
+ String namespaceURI = node.getNamespaceURI();
+ String prefix = node.getPrefix();
+
+ if (!(namespaceURI.equals(NS_URI_XMLNS) && prefix.equals("xmlns")))
+ {
+ if (DOMUtils.getAttributeNS(owner, NS_URI_XMLNS, prefix) == null)
+ {
+ out.print(" xmlns:" + prefix + "=\"" + namespaceURI + '\"');
+ }
+ }
+ else
+ {
+ prefix = node.getLocalName();
+ namespaceURI = node.getNodeValue();
+ }
+
+ namespaceStack.register(prefix, namespaceURI);
+ }
+
+ 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(StringUtils.lineSeparator);
+ }
+ else
+ {
+ str.append('\n');
+ }
+ }
+ else
+ {
+ str.append(StringUtils.lineSeparator);
+ }
+ break;
+ }
+ default :
+ {
+ str.append(ch);
+ }
+ }
+ }
+
+ return (str.toString());
+ }
}
Propchange:
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOM2Writer.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMQNameUtils.java
URL:
http://svn.apache.org/viewvc/incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMQNameUtils.java?rev=568932&r1=568931&r2=568932&view=diff
==============================================================================
---
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMQNameUtils.java
(original)
+++
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMQNameUtils.java
Thu Aug 23 04:01:23 2007
@@ -1,48 +1,48 @@
-/**
+/**
* 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.woden.internal.util.dom;
-
-import javax.xml.namespace.QName;
-
-import org.w3c.dom.Node;
-
-/**
- * This class originated from WSDL4J
- *
- * @author [EMAIL PROTECTED] (Woden changes)
- *
- */
-public class DOMQNameUtils
-{
- public static boolean matches(QName qname, Node node)
- {
- return (node != null && qname.equals(newQName(node)));
- }
-
- public static QName newQName(Node node)
- {
- if (node != null)
- {
- return new QName(node.getNamespaceURI(), node.getLocalName());
- }
- else
- {
- return null;
- }
- }
-
+ *
+ * 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.woden.internal.util.dom;
+
+import javax.xml.namespace.QName;
+
+import org.w3c.dom.Node;
+
+/**
+ * This class originated from WSDL4J
+ *
+ * @author [EMAIL PROTECTED] (Woden changes)
+ *
+ */
+public class DOMQNameUtils
+{
+ public static boolean matches(QName qname, Node node)
+ {
+ return (node != null && qname.equals(newQName(node)));
+ }
+
+ public static QName newQName(Node node)
+ {
+ if (node != null)
+ {
+ return new QName(node.getNamespaceURI(), node.getLocalName());
+ }
+ else
+ {
+ return null;
+ }
+ }
+
}
Propchange:
incubator/woden/trunk/java/src/org/apache/woden/internal/util/dom/DOMQNameUtils.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]