openwfe.org.xml.XmlUtils Maven / Gradle / Ivy
/*
* Copyright (c) 2001-2006, John Mettraux, OpenWFE.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . 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.
*
* . Neither the name of the "OpenWFE" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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.
*
* $Id: XmlUtils.java 3475 2006-10-16 00:49:17Z jmettraux $
*/
//
// XmlUtils.java
//
// [email protected]
//
// generated with
// jtmpl 1.1.01 2004/05/19 ([email protected])
//
package openwfe.org.xml;
import openwfe.org.Utils;
import openwfe.org.FileUtils;
import openwfe.org.Service;
import openwfe.org.OpenWfeException;
/**
* Utility methods for XML stuff
*
* CVS Info :
*
$Author: jmettraux $
*
$Id: XmlUtils.java 3475 2006-10-16 00:49:17Z jmettraux $
*
* @author [email protected]
*/
public abstract class XmlUtils
{
//
// TODO : clean this utility class !
//
private final static org.apache.log4j.Logger log = org.apache.log4j.Logger
.getLogger(XmlUtils.class.getName());
//
// CONSTANTS
/**
* 'resource:' is a prefix indicating the XML document can be found
* in the resources of the program (classpath).
* This prefix is especially used by the embedded engine when storing
* the expression map in its jar for example.
*/
public final static String RESOURCE_URL_PREFIX = "resource:";
//
// a local singleton for caching XML documents
private final static XmlDocumentCache cache = new XmlDocumentCache(777);
/* *
* This 'openwfe.xml.SAXParserFactory' takes precedence over
* the classical 'javax.xml.parsers.SAXParserFactory'.
* /
public final static String SP_SAX_PARSER_FACTORY
= "openwfe.xml.SAXParserFactory";
*/
//
// CONSTRUCTORS
/**
* Just preventing further extensions.
*/
private XmlUtils ()
{
super();
}
//
// METHODS
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Element extractXml
(String docUrl, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
if (log.isDebugEnabled())
log.debug("extractXml(sUrl) docUrl is >"+docUrl+"<");
if (docUrl.startsWith(RESOURCE_URL_PREFIX))
{
final java.io.InputStream is =
XmlUtils.class.getResourceAsStream(docUrl.substring(9));
return extractXml(is, validate);
}
docUrl = FileUtils.ensureProtocol(docUrl);
if (log.isDebugEnabled())
log.debug("extractXml(sUrl) docUrl is >"+docUrl+"<");
return extractXml(new java.net.URL(docUrl), validate);
}
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Element extractXml
(final java.net.URL docUrl, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
if (log.isDebugEnabled())
log.debug("extractXml(url) "+docUrl.toString());
return cache.getElement(docUrl, validate);
}
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Element extractXml
(final java.io.InputStream is, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
log.debug("extractXml(is)");
final org.jdom.input.SAXBuilder builder = getSAXBuilder(validate);
final org.jdom.Document doc = builder.build(is);
return doc.getRootElement();
}
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Element extractXml
(final java.io.File file, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
if (log.isDebugEnabled())
log.debug("extractXml(file) "+file.getName());
org.jdom.input.SAXBuilder builder = getSAXBuilder(validate);
//org.jdom.Document doc = builder.build(new java.io.FileReader(file));
org.jdom.Document doc =
builder.build(new java.io.FileInputStream(file));
return doc.getRootElement();
}
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Element extractXml
(final java.net.URLConnection uc, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
if (log.isDebugEnabled())
log.debug("extractXml(uc) "+uc.getURL().toString());
org.jdom.input.SAXBuilder builder = getSAXBuilder(validate);
org.jdom.Document doc = builder.build(uc.getInputStream());
return doc.getRootElement();
}
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Document extractXmlDocument
(final String xmlDocument, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
if (xmlDocument == null) return null;
return cache.get(xmlDocument, validate);
}
/**
* extractXml() methods for fetching XML out of various sources.
*/
public static org.jdom.Element extractXmlElement
(final String rawXml)
throws
org.jdom.JDOMException, java.io.IOException
{
final org.jdom.Document doc = extractXmlDocument(rawXml, false);
if (doc == null) return null;
final org.jdom.Element elt = doc.getRootElement();
elt.detach();
return elt;
}
/**
* Equivalent to extractXmlDocument() but no caching is performed.
*/
public static org.jdom.Document doExtractXmlDocument
(final String xmlDocument, final boolean validate)
throws
org.jdom.JDOMException, java.io.IOException
{
if (xmlDocument == null) return null;
org.jdom.input.SAXBuilder builder = getSAXBuilder(validate);
java.io.StringReader sReader = new java.io.StringReader(xmlDocument);
return builder.build(sReader);
}
/**
* extractXml() methods for fetching XML out of various sources, but
* no caching is performed.
*/
public static org.jdom.Element doExtractXmlElement
(final String rawXml)
throws
org.jdom.JDOMException, java.io.IOException
{
final org.jdom.Document doc = doExtractXmlDocument(rawXml, false);
if (doc == null) return null;
final org.jdom.Element elt = doc.getRootElement();
elt.detach();
return elt;
}
/**
* Turns the attributes of an xml element into a map
*/
public static java.util.Map fetchAttributes (final org.jdom.Element elt)
{
java.util.Map result = new java.util.HashMap();
java.util.Iterator it = elt.getAttributes().iterator();
while (it.hasNext())
{
org.jdom.Attribute att = (org.jdom.Attribute)it.next();
result.put(att.getName(), att.getValue());
}
return result;
}
/**
* Turns a detached JDOM element into a byte array (makes sure it comes back
* detached afterwards).
*/
public static byte[] toByteArray (final org.jdom.Element elt)
throws OpenWfeException
{
final byte[] result = toByteArray(new org.jdom.Document(elt));
elt.detach();
return result;
}
/**
* turns a jdom document into a byte array
*/
public static byte[] toByteArray (final org.jdom.Document doc)
throws OpenWfeException
{
final org.jdom.output.XMLOutputter out = getXMLOutputter();
final java.io.ByteArrayOutputStream baos =
new java.io.ByteArrayOutputStream();
try
{
out.output(doc, baos);
baos.write('\n');
baos.flush();
}
catch (final java.io.IOException ie)
{
throw new OpenWfeException
("Failed to encode workitem as xml", ie);
}
return baos.toByteArray();
}
/**
* A convenience method for setting up a pretty print XMLOutputter
* with a given encoding.
*/
public static org.jdom.output.XMLOutputter getXMLOutputter
(String encoding)
{
final org.jdom.output.Format format =
org.jdom.output.Format.getPrettyFormat();
if (encoding == null) encoding = Utils.getEncoding();
format.setEncoding(encoding);
return new org.jdom.output.XMLOutputter(format);
}
/**
* A convenience method for setting up a pretty print XMLOutputter
* with the default encoding.
*/
public static org.jdom.output.XMLOutputter getXMLOutputter ()
{
return getXMLOutputter(null);
}
/**
* Saving an XML element to a file.
*/
public static void save (final String fileName, final org.jdom.Element elt)
throws java.io.IOException
{
elt.detach();
final org.jdom.Document doc = new org.jdom.Document(elt);
save(fileName, doc);
}
/**
* Saving an XML document to a file.
*/
public static void save (final String fileName, final org.jdom.Document d)
throws java.io.IOException
{
java.io.FileOutputStream fos = null;
try
{
fos = new java.io.FileOutputStream(fileName);
XmlUtils.getXMLOutputter().output(d, fos);
fos.flush();
}
finally
{
try
{
fos.close();
}
catch (final Throwable t)
{
// ignore
}
}
}
/**
* Turns a JDOM document into a String.
*/
public static String toString
(final org.jdom.Document doc)
{
return toString(doc, null);
}
/**
* Turns a JDOM document into a String.
*/
public static String toString
(final org.jdom.Document doc, String encoding)
{
if (encoding == null)
encoding = Utils.getEncoding();
final org.jdom.output.XMLOutputter out = getXMLOutputter(encoding);
java.io.ByteArrayOutputStream baos =
new java.io.ByteArrayOutputStream();
try
{
out.output(doc, baos);
baos.write('\n');
baos.flush();
return baos.toString(encoding);
}
catch (java.io.IOException ie)
{
log.warn
("Failed to encode workitem as xml", ie);
return "";
}
}
/**
* Turns a JDOM content instance into a String
* (with the application encoding).
*/
public static String toString (final org.jdom.Content c)
{
if (c instanceof org.jdom.Text)
return ((org.jdom.Text)c).getTextTrim();
org.jdom.Element elt = (org.jdom.Element)c;
final boolean hasParent = (elt.getParent() != null);
if (hasParent) elt = (org.jdom.Element)elt.clone();
final String result = toString(new org.jdom.Document(elt), null);
if ( ! hasParent) elt.detach();
return result;
}
/**
* This method is used by the implementation of the getStatus
* methods to have a 'class' jdom element
*/
public static org.jdom.Element getClassElt (final Service s)
{
org.jdom.Element result = new org.jdom.Element("class");
if (s == null)
result.addContent("null");
else
result.addContent(s.getClass().getName());
return result;
}
/**
* This method is used by the implementation of the getStatus
* methods to have a 'revision' jdom element
*/
public static org.jdom.Element getRevisionElt (String revId)
{
org.jdom.Element result = new org.jdom.Element("revision");
if (revId == null)
result.addContent("null");
else
result.addContent(revId);
return result;
}
/**
* Adds a map of attributes to an org.jdom.Element
*/
public static void setAttributes
(org.jdom.Element elt, java.util.Map attributes)
{
if (elt == null) return;
if (attributes == null) return;
java.util.Iterator it = attributes.keySet().iterator();
while (it.hasNext())
{
String key = it.next().toString();
String value = attributes.get(key).toString();
//log.debug("setting attribute '"+key+"' = '"+value+"'");
elt.setAttribute(key, value);
}
}
/**
* A debug method : dumps as a string a view of an XML element.
*/
public static String dumpContent (final org.jdom.Element elt)
{
final StringBuffer sb = new StringBuffer();
sb.append(" --"+elt.getName()+"--\n");
java.util.Iterator it = elt.getAttributes().iterator();
while (it.hasNext())
{
final org.jdom.Attribute at = (org.jdom.Attribute)it.next();
sb.append(" - "+at.getName()+"='"+at.getValue()+"'\n");
}
it = elt.getContent().iterator();
while (it.hasNext())
{
final Object c = it.next();
if (c instanceof org.jdom.Element)
{
final org.jdom.Element e = (org.jdom.Element)c;
sb.append(" * --"+e.getName()+"--\n");
continue;
}
sb.append(" * "+c.getClass().getName()+"\n");
}
return sb.toString();
}
/**
* Returns a SAXBuilder with XSD schema validation on.
*/
public static org.jdom.input.SAXBuilder getSAXBuilder
(final boolean validate)
{
org.jdom.input.SAXBuilder builder = null;
if (validate == false)
{
builder = new org.jdom.input.SAXBuilder(false);
}
else
{
builder = new org.jdom.input.SAXBuilder
//("javax.xml.parsers.SAXParser", true); :-(
("org.apache.xerces.parsers.SAXParser", true);
//(getSAXFactory(), true);
builder.setFeature
("http://apache.org/xml/features/validation/schema", true);
}
return builder;
}
/* *
* Returns the name of the class to use as the SAX parser factory.
* The system property 'openwfe.org.SAXParserFactory' will be considered,
* else null will be returned.
* /
protected synchronized static String getSAXFactory ()
{
final String saxFactory = System.getProperty(SP_SAX_PARSER_FACTORY);
//if (saxFactory == null)
//{
// saxFactory = System
// .getProperty("javax.xml.parsers.SAXParserFactory");
//}
if (log.isDebugEnabled())
log.debug("getSAXFactory() found '"+saxFactory+"'");
return saxFactory;
}
*/
//
// (methods coming from WicUtils (workitem coder utils)
/**
* Returns among the child of the given elt the first that is an
* instance of of org.jdom.Element itself.
*/
public static org.jdom.Element getFirstChild
(final org.jdom.Element elt)
{
return getChild(elt, 0, null);
}
/**
* Returns among the child of the given elt the first that is an
* instance of of org.jdom.Element itself; that version of the
* method accepts a Namespace parameter, elements not belonging to that
* namespace will not be taken into account.
*/
public static org.jdom.Element getFirstChild
(final org.jdom.Element elt, final org.jdom.Namespace ns)
{
return getChild(elt, 0, ns);
}
/**
* Among the org.jdom.Element children of the given element, returns the one
* located at the given index.
*/
public static org.jdom.Element getChild
(final org.jdom.Element elt,
final int childIndex)
{
return getChild(elt, childIndex, null);
}
/**
* Among the org.jdom.Element children of the given element, returns the one
* located at the given index; that version of the method accepts
* a Namespace parameter, elements not belonging to that namespace won't
* be taken into account.
*/
public static org.jdom.Element getChild
(final org.jdom.Element elt,
final int childIndex,
final org.jdom.Namespace ns)
{
if (elt == null) return null;
int pos = 0;
final java.util.Iterator it = elt.getContent().iterator();
while (it.hasNext())
{
final Object o = it.next();
if (o instanceof org.jdom.Element)
{
final org.jdom.Element e = (org.jdom.Element)o;
if (ns != null &&
( ! ns.equals(e.getNamespace())))
{
continue;
}
if (pos == childIndex)
{
return e;
}
pos++;
}
}
return null;
}
/**
* Returns the first text or cdata child of the given elt.
*/
public static String fetchTextContent (final org.jdom.Element elt)
//throws CodingException
{
if (elt == null)
{
throw new IllegalArgumentException
("fetchTextContent() "+
"cannot fetch the text content of a null element");
}
if (elt.getContent().size() < 1)
{
//throw new CodingException
// ("Element without any content. Cannot turn into an attribute.");
//log.debug("fetchTextContent() returning ''");
return "";
}
//log.debug("fetchTextContent() size : "+elt.getContent().size());
//java.util.Iterator iit = elt.getContent().iterator();
//while (iit.hasNext())
//{
// final Object o = iit.next();
// log.debug("fetchTextContent() - "+o.getClass().getName());
//}
java.util.Iterator it = elt.getContent().iterator();
while (it.hasNext())
{
Object content = it.next();
if (content instanceof org.jdom.Text)
{
//log.debug
// ("fetchTextContent() returning text >"+
// ((org.jdom.Text)content).getTextTrim()+"<");
return ((org.jdom.Text)content).getTextTrim();
}
else if (content instanceof org.jdom.CDATA)
{
//log.debug
// ("fetchTextContent() returning cdata >"+
// ((org.jdom.CDATA)content).getTextTrim()+"<");
return ((org.jdom.CDATA)content).getTextTrim();
}
}
//throw new CodingException
// ("Element doesn't contain any textual info "+
// "which may be used to build an attribute");
return null;
}
/**
* Returns the first CDATA section held in the given elt as a String.
*/
public static String fetchCdataContent (final org.jdom.Element elt)
//throws CodingException
{
if (elt.getContent().size() < 1)
{
return "";
}
final java.util.Iterator it = elt.getContent().iterator();
while (it.hasNext())
{
final Object o = it.next();
if (o instanceof org.jdom.CDATA)
return ((org.jdom.CDATA)o).getTextTrim();
}
//throw new CodingException
// ("Element doesn't contain a CDATA section");
return null;
}
/**
* Returns the first piece of content that is not an empty (when trimmed)
* Text instance.
*/
public static org.jdom.Content getFirstContent (final org.jdom.Element elt)
{
final java.util.Iterator it = elt.getContent().iterator();
while (it.hasNext())
{
final org.jdom.Content c = (org.jdom.Content)it.next();
if (c instanceof org.jdom.Element) return c;
if (c instanceof org.jdom.Text)
{
final String s = ((org.jdom.Text)c).getTextTrim();
if (s.length() > 0) return c;
}
// else continue
}
return null;
}
public static String dump
(final String prefix, final org.jdom.Element elt)
{
try
{
return FileUtils.dump(prefix, toByteArray(elt));
}
catch (final Throwable t)
{
log.debug("dump() failure", t);
}
return null;
}
public static String dump
(final String prefix, final org.jdom.Document doc)
{
try
{
return FileUtils.dump(prefix, toByteArray(doc));
}
catch (final Throwable t)
{
log.debug("dump() failure", t);
}
return null;
}
/* *
* If something else than an instance of Element is passed to this
* method it will get untouched, for an Element instance, trailing
* empty Text element will get removed.
* /
public static org.jdom.Content clean (final org.jdom.Content c)
{
if ( ! (c instanceof org.jdom.Element)) return c;
final org.jdom.Element e = (org.jdom.Element)c;
final org.jdom.Element
}
*/
/**
* If the first line of sXml is a <?xml > declaration, the
* returned string will be sXml without this first line else sXml
* will be returned.
*/
public static String removeHeaderLine (final String sXml)
{
if ( ! sXml.startsWith("");
String s = sXml.substring(i+1);
return s.trim();
}
/**
* It's the same method as toString(c), but it returns a String without
* the <?xml > declaration.
*/
public static String xmlToString (final org.jdom.Content c)
{
return removeHeaderLine(toString(c));
}
}