📄 xmlutils.java
字号:
/* * $Id: XMLUtils.java 12425 2008-07-29 20:17:53Z tcarlson $ * -------------------------------------------------------------------------------------- * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com * * The software in this package is published under the terms of the CPAL v1.0 * license, a copy of which has been included with this distribution in the * LICENSE.txt file. */package org.mule.module.xml.util;import org.mule.RequestContext;import org.mule.api.transport.OutputHandler;import org.mule.module.xml.stax.DelegateXMLStreamReader;import org.mule.module.xml.stax.StaxSource;import org.mule.module.xml.transformer.DelayedResult;import org.mule.module.xml.transformer.XmlToDomDocument;import org.mule.util.IOUtils;import java.io.ByteArrayInputStream;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.StringReader;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.XMLStreamWriter;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.TransformerFactoryConfigurationError;import javax.xml.transform.dom.DOMResult;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.sax.SAXSource;import javax.xml.transform.stream.StreamSource;import org.apache.commons.io.output.ByteArrayOutputStream;import org.apache.commons.lang.StringUtils;import org.dom4j.DocumentException;import org.dom4j.io.DOMReader;import org.dom4j.io.DocumentSource;import org.w3c.dom.Document;import org.xml.sax.InputSource;/** * General utility methods for working with XML. */public class XMLUtils extends org.mule.util.XMLUtils{ public static final String TRANSFORMER_FACTORY_JDK5 = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"; // xml parser feature names for optional XSD validation public static final String APACHE_XML_FEATURES_VALIDATION_SCHEMA = "http://apache.org/xml/features/validation/schema"; public static final String APACHE_XML_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING = "http://apache.org/xml/features/validation/schema-full-checking"; // JAXP property for specifying external XSD location public static final String JAXP_PROPERTIES_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; // JAXP properties for specifying external XSD language (as required by newer // JAXP implementation) public static final String JAXP_PROPERTIES_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; public static final String JAXP_PROPERTIES_SCHEMA_LANGUAGE_VALUE = "http://www.w3.org/2001/XMLSchema"; /** * Converts a DOM to an XML string. */ public static String toXml(Document dom) { return new DOMReader().read(dom).asXML(); } /** * @return a new XSLT transformer * @throws TransformerConfigurationException if no TransformerFactory can be located in the * runtime environment. */ public static Transformer getTransformer() throws TransformerConfigurationException { TransformerFactory tf; try { tf = TransformerFactory.newInstance(); } catch (TransformerFactoryConfigurationError e) { System.setProperty("javax.xml.transform.TransformerFactory", TRANSFORMER_FACTORY_JDK5); tf = TransformerFactory.newInstance(); } if (tf != null) { return tf.newTransformer(); } else { throw new TransformerConfigurationException("Unable to instantiate a TransformerFactory"); } } public static org.dom4j.Document toDocument(Object obj) throws Exception { return toDocument(obj, null); } /** * Converts an object of unknown type to an org.dom4j.Document if possible. * @return null if object cannot be converted * @throws DocumentException if an error occurs while parsing */ public static org.dom4j.Document toDocument(Object obj, String externalSchemaLocation) throws Exception { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); if (externalSchemaLocation != null) { reader.setValidation(true); reader.setFeature(APACHE_XML_FEATURES_VALIDATION_SCHEMA, true); reader.setFeature(APACHE_XML_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, true); InputStream xsdAsStream = IOUtils.getResourceAsStream(externalSchemaLocation, XMLUtils.class); if (xsdAsStream == null) { throw new IllegalArgumentException("Couldn't find schema at " + externalSchemaLocation); } // Set schema language property (must be done before the schemaSource // is set) reader.setProperty(JAXP_PROPERTIES_SCHEMA_LANGUAGE, JAXP_PROPERTIES_SCHEMA_LANGUAGE_VALUE); // Need this one to map schemaLocation to a physical location reader.setProperty(JAXP_PROPERTIES_SCHEMA_SOURCE, xsdAsStream); } if (obj instanceof org.dom4j.Document) { return (org.dom4j.Document) obj; } else if (obj instanceof org.w3c.dom.Document) { org.dom4j.io.DOMReader domReader = new org.dom4j.io.DOMReader(); return domReader.read((org.w3c.dom.Document) obj); } else if (obj instanceof org.xml.sax.InputSource) { return reader.read((org.xml.sax.InputSource) obj); } else if (obj instanceof javax.xml.transform.Source || obj instanceof javax.xml.stream.XMLStreamReader) { // TODO Find a more direct way to do this XmlToDomDocument tr = new XmlToDomDocument(); tr.setReturnClass(org.dom4j.Document.class); return (org.dom4j.Document) tr.transform(obj); } else if (obj instanceof java.io.InputStream) { return reader.read((java.io.InputStream) obj); } else if (obj instanceof String) { return reader.read(new StringReader((String) obj)); } else if (obj instanceof byte[]) { // TODO Handle encoding/charset somehow return reader.read(new StringReader(new String((byte[]) obj))); } else if (obj instanceof File) { return reader.read((File) obj); } else { return null; } } /** * Returns an XMLStreamReader for an object of unknown type if possible. * @return null if no XMLStreamReader can be created for the object type * @throws XMLStreamException */ public static javax.xml.stream.XMLStreamReader toXMLStreamReader(javax.xml.stream.XMLInputFactory factory, Object obj) throws XMLStreamException { if (obj instanceof javax.xml.stream.XMLStreamReader) { return (javax.xml.stream.XMLStreamReader) obj; } else if (obj instanceof org.mule.module.xml.stax.StaxSource) { return ((org.mule.module.xml.stax.StaxSource) obj).getXMLStreamReader(); } else if (obj instanceof javax.xml.transform.Source) { return factory.createXMLStreamReader((javax.xml.transform.Source) obj); } else if (obj instanceof org.xml.sax.InputSource) { return factory.createXMLStreamReader(((org.xml.sax.InputSource) obj).getByteStream()); } else if (obj instanceof org.w3c.dom.Document) { return factory.createXMLStreamReader(new javax.xml.transform.dom.DOMSource((org.w3c.dom.Document) obj)); } else if (obj instanceof org.dom4j.Document) { return factory.createXMLStreamReader(new org.dom4j.io.DocumentSource((org.dom4j.Document) obj)); } else if (obj instanceof java.io.InputStream) { final InputStream is = (java.io.InputStream) obj; XMLStreamReader xsr = factory.createXMLStreamReader(is); return new DelegateXMLStreamReader(xsr) { public void close() throws XMLStreamException { super.close(); try { is.close(); } catch (IOException e) { throw new XMLStreamException(e); } } }; } else if (obj instanceof String)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -