Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public String applyMacros(FormEntrySession session, String xml) throws Exception
content.removeChild(macrosNode);

// switch back to String mode from the document so we can use string utilities to substitute
xml = HtmlFormEntryUtil.documentToString(doc);
xml = HtmlFormEntryUtil.documentToString(doc, false);

// substitute any macros we found
for (Object temp : macros.keySet()) {
Expand Down Expand Up @@ -240,7 +240,7 @@ public String processPages(FormEntrySession session, String xml) throws Exceptio
doc.renameNode(pageNodes.item(y), null, "div");
}

xml = HtmlFormEntryUtil.documentToString(doc);
xml = HtmlFormEntryUtil.documentToString(doc, false);
return xml;
}

Expand Down Expand Up @@ -303,7 +303,7 @@ public String applyTranslations(String xml, FormEntryContext context) throws Exc
content.removeChild(transNode);

// switch back to String mode from the document so we can use string utilities to substitute
xml = HtmlFormEntryUtil.documentToString(doc);
xml = HtmlFormEntryUtil.documentToString(doc, false);
return xml;
}

Expand Down Expand Up @@ -518,7 +518,7 @@ else if (formPath.toLowerCase().endsWith(".css")) {
nodesToReplace = doc.getElementsByTagName(SUBFORM);
}
}
xml = HtmlFormEntryUtil.documentToString(doc);
xml = HtmlFormEntryUtil.documentToString(doc, false);
}
return xml;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,28 +357,44 @@ public static Document stringToDocument(String xml) throws Exception {
}

/**
* Converts a Document object to an xml string
* Converts a Document object to an xml string, indenting the output
*
* @param document the Document instance to convert
* @return the resulting xml string
* @throws Exception
*/
public static String documentToString(Document document) throws Exception {
return documentToString(document, true);
}

/**
* Converts a Document object to an xml string
*
* @param document the Document instance to convert
* @param indent whether to indent the resulting xml. This should only be set to true if the
* resulting xml is intended for human consumption (e.g. exporting a form for
* editing). Indenting inserts whitespace text nodes around element boundaries,
* which corrupts mixed text/element content, most notably a {@code <lookup>} or
* other substitution tag nested within a {@code <script>} block.
* @return the resulting xml string
* @throws Exception
*/
public static String documentToString(Document document, boolean indent) throws Exception {
//set up a transformer
Transformer trans = null;
TransformerFactory transfac = TransformerFactory.newInstance();

try {
trans = transfac.newTransformer();
}
catch (TransformerException te) {
System.out.println(HtmlFormEntryConstants.ERROR_TRANSFORMER_1 + te);
}
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, HtmlFormEntryConstants.CONSTANT_YES);
trans.setOutputProperty(OutputKeys.INDENT, HtmlFormEntryConstants.CONSTANT_YES);
trans.setOutputProperty(OutputKeys.INDENT, indent ? HtmlFormEntryConstants.CONSTANT_YES : "no");
trans.setOutputProperty(OutputKeys.METHOD, HtmlFormEntryConstants.CONSTANT_XML);
trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
Expand All @@ -390,7 +406,7 @@ public static String documentToString(Document document) throws Exception {
System.out.println(HtmlFormEntryConstants.ERROR_TRANSFORMER_2 + te);
}
String xmlString = sw.toString();

return xmlString;
}

Expand Down