";
short nodeType = xmlNode.getNodeType();
String nodeName = xmlNode.getNodeName();
if (nodeType == Node.DOCUMENT_NODE) {
return getString(((Document)xmlNode).getDocumentElement(), depth, pretty);
}
// Handle Element nodes
if (nodeType == Node.ELEMENT_NODE) {
if (pretty) {
xmlRepresentation += "\n";
for (int i = 0; i < depth; i++) {
xmlRepresentation += " ";
}
}
// Write opening tag
xmlRepresentation += "<" + nodeName;
// Write the node attributes
NamedNodeMap nodeAttributes = xmlNode.getAttributes();
for (int i = 0; i < nodeAttributes.getLength(); i++) {
Node attribute = nodeAttributes.item(i);
xmlRepresentation += " " + attribute.getNodeName() + "=";
xmlRepresentation += "\"" + attribute.getNodeValue() + "\"";
}
// If the node has no children, close the opening tag and return
if (xmlNode.hasChildNodes() == false) {
// This produces somewhat ugly output, but it is necessary to compensate
// for display bugs in Netscape. Firstly, the space is needed before the
// closing bracket otherwise Netscape will ignore some tags (
, for
// example). Also, a newline character would be expected after the tag,
// but this causes problems with the display of links (the link text
// will contain a newline character, which is displayed badly).
xmlRepresentation += " />";
return xmlRepresentation;
}
// Close the opening tag
xmlRepresentation += ">";
// Apply recursively to the children of this node
// hack for nodes next to text nodes - dont make them pretty
// this is needed for text inside a element - any new lines
// or spaces around the span elements show up in the text
NodeList children = xmlNode.getChildNodes();
boolean do_pretty = pretty;
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i).getNodeType()==Node.TEXT_NODE) {
do_pretty=false; // if there is a text node amongst the children, do teh following nodes in non-pretty mode - hope this doesn't stuff up something else
}
xmlRepresentation += getString(children.item(i), depth + 1, do_pretty);
}
// Write closing tag
if (pretty) {
if (xmlRepresentation.endsWith("\n")) {
for (int i = 0; i < depth; i++)
xmlRepresentation += " ";
}
}
xmlRepresentation += "" + nodeName + ">";
if (pretty) {
xmlRepresentation += "\n";
}
}
// Handle Text nodes
else if (nodeType == Node.TEXT_NODE) {
String text = xmlNode.getNodeValue();
// Perform output escaping, if required
if (outputEscaping) {
text = text.replaceAll("&", "&"); // Must be done first!!
text = text.replaceAll("<", "<");
text = text.replaceAll(">", ">");
text = text.replaceAll("\"", """);
text = text.replaceAll("\'", "'");
}
// Remove any control-C characters
text = text.replaceAll("" + (char) 3, "");
xmlRepresentation += text;
}
// Handle Processing Instruction nodes
else if (nodeType == Node.PROCESSING_INSTRUCTION_NODE) {
if (nodeName.equals("javax.xml.transform.disable-output-escaping")) {
outputEscaping = false;
}
else if (nodeName.equals("javax.xml.transform.enable-output-escaping")) {
outputEscaping = true;
}
else {
System.err.println("XMLConverter.getString(): Warning: Unhandled processing instruction " + nodeName);
}
}
else if (nodeType == Node.COMMENT_NODE) {
String text = xmlNode.getNodeValue();
xmlRepresentation += "";
}
// A type of node that is not handled yet
else {
System.err.println("XMLConverter.getString(): Warning: Unknown node type: " + nodeType+" "+getNodeTypeString(nodeType));
}
return xmlRepresentation;
}
protected static String getNodeTypeString(short node_type) {
String type = "";
switch(node_type) {
case Node.ATTRIBUTE_NODE:
type="ATTRIBUTE_NODE";
break;
case Node.CDATA_SECTION_NODE:
type="CDATA_SECTION_NODE";
break;
case Node.COMMENT_NODE:
type="COMMENT_NODE";
break;
case Node.DOCUMENT_FRAGMENT_NODE:
type="DOCUMENT_FRAGMENT_NODE";
break;
case Node.DOCUMENT_NODE:
type="DOCUMENT_NODE";
break;
case Node.DOCUMENT_TYPE_NODE:
type="DOCUMENT_TYPE_NODE";
break;
case Node.ELEMENT_NODE:
type="ELEMENT_NODE";
break;
case Node.ENTITY_NODE:
type="ENTITY_NODE";
break;
case Node.ENTITY_REFERENCE_NODE:
type="ENTITY_REFERENCE_NODE";
break;
case Node.NOTATION_NODE:
type="NOTATION_NODE";
break;
case Node.PROCESSING_INSTRUCTION_NODE:
type="PROCESSING_INSTRUCTION_NODE";
break;
case Node.TEXT_NODE:
type="TEXT_NODE";
break;
default:
type="UNKNOWN";
}
return type;
}
}