📄 utilxml.java
字号:
/*
* $Id: UtilXml.java,v 1.3 2003/12/06 16:00:29 ajzeneski Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package org.ofbiz.base.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Utilities methods to simplify dealing with JAXP & DOM XML parsing
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.3 $
* @since 2.0
*/
public class UtilXml {
public static final String module = UtilXml.class.getName();
public static String writeXmlDocument(Document document) throws java.io.IOException {
if (document == null) {
Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module);
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
writeXmlDocument(bos, document);
String outString = bos.toString("UTF-8");
if (bos != null) bos.close();
return outString;
}
public static void writeXmlDocument(String filename, Document document)
throws java.io.FileNotFoundException, java.io.IOException {
if (document == null) {
Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module);
return;
}
if (filename == null) {
Debug.logWarning("[UtilXml.writeXmlDocument] Filename was null, doing nothing", module);
return;
}
File outFile = new File(filename);
FileOutputStream fos = null;
fos = new FileOutputStream(outFile);
try {
writeXmlDocument(fos, document);
} finally {
if (fos != null) fos.close();
}
}
public static void writeXmlDocument(OutputStream os, Document document) throws java.io.IOException {
if (document == null) {
Debug.logWarning("[UtilXml.writeXmlDocument] Document was null, doing nothing", module);
return;
}
if (os == null) {
Debug.logWarning("[UtilXml.writeXmlDocument] OutputStream was null, doing nothing", module);
return;
}
// if(document instanceof XmlDocument) {
// Crimson writer
// XmlDocument xdoc = (XmlDocument) document;
// xdoc.write(os);
// }
// else {
// Xerces writer
OutputFormat format = new OutputFormat(document);
format.setIndent(2);
XMLSerializer serializer = new XMLSerializer(os, format);
serializer.asDOMSerializer();
serializer.serialize(document.getDocumentElement());
// }
}
public static Document readXmlDocument(String content)
throws SAXException, ParserConfigurationException, java.io.IOException {
return readXmlDocument(content, true);
}
public static Document readXmlDocument(String content, boolean validate)
throws SAXException, ParserConfigurationException, java.io.IOException {
if (content == null) {
Debug.logWarning("[UtilXml.readXmlDocument] content was null, doing nothing", module);
return null;
}
ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes("UTF-8"));
return readXmlDocument(bis, validate, "Internal Content");
}
public static Document readXmlDocument(URL url)
throws SAXException, ParserConfigurationException, java.io.IOException {
return readXmlDocument(url, true);
}
public static Document readXmlDocument(URL url, boolean validate)
throws SAXException, ParserConfigurationException, java.io.IOException {
if (url == null) {
Debug.logWarning("[UtilXml.readXmlDocument] URL was null, doing nothing", module);
return null;
}
return readXmlDocument(url.openStream(), validate, url.toString());
}
public static Document readXmlDocument(InputStream is)
throws SAXException, ParserConfigurationException, java.io.IOException {
return readXmlDocument(is, true, null);
}
public static Document readXmlDocument(InputStream is, boolean validate, String docDescription)
throws SAXException, ParserConfigurationException, java.io.IOException {
if (is == null) {
Debug.logWarning("[UtilXml.readXmlDocument] InputStream was null, doing nothing", module);
return null;
}
Document document = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validate);
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
if (validate) {
LocalResolver lr = new LocalResolver(new DefaultHandler());
ErrorHandler eh = new LocalErrorHandler(docDescription, lr);
builder.setEntityResolver(lr);
builder.setErrorHandler(eh);
}
document = builder.parse(is);
return document;
}
public static Document makeEmptyXmlDocument() {
return makeEmptyXmlDocument(null);
}
public static Document makeEmptyXmlDocument(String rootElementName) {
Document document = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
// factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
} catch (Exception e) {
Debug.logError(e, module);
}
if (document == null) return null;
if (rootElementName != null) {
Element rootElement = document.createElement(rootElementName);
document.appendChild(rootElement);
}
return document;
}
/** Creates a child element with the given name and appends it to the element child node list. */
public static Element addChildElement(Element element, String childElementName, Document document) {
Element newElement = document.createElement(childElementName);
element.appendChild(newElement);
return newElement;
}
/** Creates a child element with the given name and appends it to the element child node list.
* Also creates a Text node with the given value and appends it to the new elements child node list.
*/
public static Element addChildElementValue(Element element, String childElementName,
String childElementValue, Document document) {
Element newElement = addChildElement(element, childElementName, document);
newElement.appendChild(document.createTextNode(childElementValue));
return newElement;
}
/** Creates a child element with the given name and appends it to the element child node list.
* Also creates a CDATASection node with the given value and appends it to the new elements child node list.
*/
public static Element addChildElementCDATAValue(Element element, String childElementName,
String childElementValue, Document document) {
Element newElement = addChildElement(element, childElementName, document);
newElement.appendChild(document.createCDATASection(childElementValue));
return newElement;
}
/** Return a List of Element objects that have the given name and are
* immediate children of the given element; if name is null, all child
* elements will be included. */
public static List childElementList(Element element, String childElementName) {
if (element == null) return null;
List elements = new LinkedList();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -