|
Nux 1.6 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object nux.xom.pool.XOMUtil
public class XOMUtil
Various utilities avoiding redundant code in several classes.
Nested Class Summary | |
---|---|
static class |
XOMUtil.Normalizer
Standard XML algorithms for text and whitespace normalization (but not for Unicode normalization); type safe enum. |
Method Summary | |
---|---|
static DOMImplementation |
getDOMImplementation()
Returns a namespace-aware DOMImplementation via the default JAXP lookup mechanism. |
static NodeFactory |
getIgnoreWhitespaceOnlyTextNodeFactory()
Returns a node factory that removes each Text node that is
empty or consists of whitespace characters only (boundary whitespace). |
static NodeFactory |
getLoggingNodeFactory(NodeFactory child,
PrintStream log,
String logName)
Returns a factory that delegates all calls to the given child factory, logging each call to the given log stream (typically System.err) for simple debugging purposes. |
static NodeFactory |
getNullNodeFactory()
Returns a node factory for pure document validation. |
static NodeFactory |
getRedirectingNodeFactory(StreamingSerializer serializer)
Returns a node factory that redirects its input onto the output of a streaming serializer. |
static NodeFactory |
getTextTrimmingNodeFactory()
Returns a node factory that removes leading and trailing whitespaces in each Text node, altogether removing a Text node that becomes
empty after said trimming (ala String.trim() ). |
static Document |
jaxbMarshal(Marshaller marshaller,
Object jaxbObj)
Marshals (serializes) the given JAXB object via the given marshaller into a new XOM Document (convenience method). |
static Object |
jaxbUnmarshal(Unmarshaller unmarshaller,
ParentNode node)
Unmarshals (deserializes) the given XOM node via the given unmarshaller into a new JAXB object (convenience method). |
static byte[] |
toCanonicalXML(Document doc)
Returns the W3C Canonical XML representation of the given document. |
static String |
toDebugString(Node node)
Returns a properly indented debug level string representation of the entire given XML node subtree, decorated with node types, node names, children, etc. |
static Document |
toDocument(String xml)
Returns the XOM document obtained by parsing from the content of the given XML string. |
static String |
toPrettyXML(Node node)
Returns a pretty-printed String representation of the given node (subtree). |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static DOMImplementation getDOMImplementation()
public static String toPrettyXML(Node node)
node
- the node (subtree) to convert.
Serializer
public static byte[] toCanonicalXML(Document doc)
doc
- the document to convert.
Canonicalizer
public static String toDebugString(Node node)
XOMUtil.Normalizer
.
node
- the subtree to display
public static Document toDocument(String xml)
Example usage:
String xml = "<foo>" + "<bar size='123'>" + "hello world" + "</bar>" + "</foo>"; Document doc = toDocument(xml); System.out.println(doc.toXML());
xml
- the string to parse from
XMLException
- if the content of the string to parse is not well-formed XML.Builder.build(String, String)
public static NodeFactory getIgnoreWhitespaceOnlyTextNodeFactory()
Text
node that is
empty or consists of whitespace characters only (boundary whitespace).
This method fully preserves narrative Text
containing
whitespace along with other characters.
Otherwise this factory behaves just like the standard NodeFactory
.
Ignoring whitespace-only nodes reduces memory footprint for documents that are heavily pretty printed and indented, i.e. human-readable. Remember that without such a factory, every whitespace sequence occurring between element tags generates a mostly useless Text node.
Finally, note that this method's whitespace pruning is appropriate for
many, but not all XML use cases (round-tripping). For example, the blank
between
<p><strong>Hello</strong> <em>World!</em></p>
will be removed, which might not be what you want. This is because this
method does not look across multiple Text nodes.
public static NodeFactory getLoggingNodeFactory(NodeFactory child, PrintStream log, String logName)
child
- the factory to delegate tolog
- the print stream to log to (typically System.err)logName
- a name for this logger (typically "log" or similar)
public static NodeFactory getTextTrimmingNodeFactory()
Text
node, altogether removing a Text node that becomes
empty after said trimming (ala String.trim()
). For example a
text node of " hello world "
becomes
"hello world"
, and a text node of " "
is
removed.
Otherwise this factory behaves just like the standard NodeFactory
.
Finally, note that this method's whitespace pruning is appropriate for many, but not all XML use cases (round-tripping).
public static NodeFactory getNullNodeFactory()
Builder.build(...)
, which is
not required anyway for pure validation. Ignores all input and builds an
empty document instead. This improves validation performance.
public static NodeFactory getRedirectingNodeFactory(StreamingSerializer serializer)
The document returned on finishMakingDocument
will be empty.
serializer
- the streaming serializer to write to
public static Document jaxbMarshal(Marshaller marshaller, Object jaxbObj) throws JAXBException
This implementation is somewhat inefficient but correctly does the job. There is no connection between the JAXB object tree and the XOM object tree; they are completely independent object trees without any cross-references. Hence, updates in one tree are not automatically reflected in the other tree.
marshaller
- a JAXB serializer (note that a marshaller is typically
not thread-safe and expensive to construct; hence the recommendation
is to use a ThreadLocal
to make it thread-safe and efficient)jaxbObj
- the JAXB object to serialize
JAXBException
- If an unexpected problem occurred in the conversion.
MarshalException
- If an error occurred while performing the marshal operation.
Whereever possible, one should prefer the
MarshalException
over the JAXBException
.Marshaller.marshal(java.lang.Object, org.w3c.dom.Node)
public static Object jaxbUnmarshal(Unmarshaller unmarshaller, ParentNode node) throws JAXBException
This implementation is somewhat inefficient but correctly does the job. There is no connection between the JAXB object tree and the XOM object tree; they are completely independent object trees without any cross-references. Hence, updates in one tree are not automatically reflected in the other tree.
unmarshaller
- a JAXB deserializer (note that an unmarshaller is typically
not thread-safe and expensive to construct; hence the recommendation
is to use a ThreadLocal
to make it thread-safe and efficient)node
- the XOM node to deserialize
JAXBException
- If an unexpected problem occurred in the conversion.
UnmarshalException
- If an error occurred while performing the unmarshal operation.
Whereever possible, one should prefer the
UnmarshalException
over the JAXBException
.Unmarshaller.unmarshal(org.w3c.dom.Node)
|
Nux 1.6 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |