try
{
boolean stripWhiteSpace = false;
XPathContext xctxt = m_transformer.getXPathContext();
DOMHelper dhelper = xctxt.getDOMHelper();
switch (node.getNodeType())
{
case Node.TEXT_NODE :
{
Text tx = (Text) node;
String data = null;
// System.out.println("stripWhiteSpace = "+stripWhiteSpace+", "+tx.getData());
if (stripWhiteSpace)
{
if (!dhelper.isIgnorableWhitespace(tx))
{
data = tx.getData();
if ((null != data) && (0 == data.trim().length()))
{
data = null;
}
}
}
else
{
Node parent = node.getParentNode();
if (null != parent)
{
if (Node.DOCUMENT_NODE != parent.getNodeType())
{
data = tx.getData();
if ((null != data) && (0 == data.length()))
{
data = null;
}
}
}
else
{
data = tx.getData();
if ((null != data) && (0 == data.length()))
{
data = null;
}
}
}
if (null != data)
{
// TODO: Hack around the issue of comments next to literals.
// This would be, when a comment is present, the whitespace
// after the comment must be added to the literal. The
// parser should do this, but XML4J doesn't seem to.
// <foo>some lit text
// <!-- comment -->
// </foo>
// Loop through next siblings while they are comments, then,
// if the node after that is a ignorable text node, append
// it to the text node just added.
if (dhelper.isIgnorableWhitespace(tx))
{
m_rth.ignorableWhitespace(data.toCharArray(), 0, data.length());
}
else
{
m_rth.characters(data.toCharArray(), 0, data.length());
}
}
}
break;
case Node.DOCUMENT_FRAGMENT_NODE :
case Node.DOCUMENT_NODE :
// Can't clone a document, but refrain from throwing an error
// so that copy-of will work
break;
case Node.ELEMENT_NODE :
{
Attributes atts;
if (shouldCloneAttributes)
{
m_rth.addAttributes(node);
m_rth.processNSDecls(node);
}
String ns = dhelper.getNamespaceOfNode(node);
String localName = dhelper.getLocalNameOfNode(node);
m_rth.startElement(ns, localName, node.getNodeName(), null);
}
break;
case Node.CDATA_SECTION_NODE :
{
m_rth.startCDATA();
String data = ((CDATASection) node).getData();
m_rth.characters(data.toCharArray(), 0, data.length());
m_rth.endCDATA();
}
break;
case Node.ATTRIBUTE_NODE :
{
if (m_rth.isDefinedNSDecl((Attr) node))
break;
String ns = dhelper.getNamespaceOfNode(node);
String localName = dhelper.getLocalNameOfNode(node);
m_rth.addAttribute(ns, localName, node.getNodeName(), "CDATA",
((Attr) node).getValue());
}
break;