MCPcopy Index your code
hub / github.com/processing/processing / format

Method format

core/src/processing/data/XML.java:1007–1134  ·  view source on GitHub ↗

Format this XML data as a String. @webref xml:method @brief Formats XML data as a String @param indent -1 for a single line (and no declaration), >= 0 for indents and newlines @return the content @see XML#toString()

(int indent)

Source from the content-addressed store, hash-verified

1005 * @see XML#toString()
1006 */
1007 public String format(int indent) {
1008 try {
1009 // entities = doctype.getEntities()
1010 boolean useIndentAmount = false;
1011 TransformerFactory factory = TransformerFactory.newInstance();
1012 if (indent != -1) {
1013 try {
1014 factory.setAttribute("indent-number", indent);
1015 } catch (IllegalArgumentException e) {
1016 useIndentAmount = true;
1017 }
1018 }
1019 Transformer transformer = factory.newTransformer();
1020
1021 // Add the XML declaration at the top if this node is the root and we're
1022 // not writing to a single line (indent = -1 means single line).
1023 if (indent == -1 || parent == null) {
1024 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
1025 } else {
1026 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
1027 }
1028
1029// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
1030
1031 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
1032
1033// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); // huh?
1034
1035// transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
1036// "-//W3C//DTD XHTML 1.0 Transitional//EN");
1037// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
1038// "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
1039
1040 // For Android, because (at least 2.3.3) doesn't like indent-number
1041 if (useIndentAmount) {
1042 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
1043 }
1044
1045// transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
1046// transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
1047 transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
1048// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
1049
1050 // Always indent, otherwise the XML declaration will just be jammed
1051 // onto the first line with the XML code as well.
1052 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1053
1054// Properties p = transformer.getOutputProperties();
1055// for (Object key : p.keySet()) {
1056// System.out.println(key + " -> " + p.get(key));
1057// }
1058
1059 // If you smell something, that's because this code stinks. No matter
1060 // the settings of the Transformer object, if the XML document already
1061 // has whitespace elements, it won't bother re-indenting/re-formatting.
1062 // So instead, transform the data once into a single line string.
1063 // If indent is -1, then we're done. Otherwise re-run and the settings
1064 // of the factory will kick in. If you know a better way to do this,

Callers 3

writeMethod · 0.95
printMethod · 0.95
toStringMethod · 0.95

Calls 10

splitMethod · 0.95
subsetMethod · 0.95
joinMethod · 0.95
trimMethod · 0.95
transformMethod · 0.80
startsWithMethod · 0.80
toStringMethod · 0.45
trimMethod · 0.45
equalsMethod · 0.45
printStackTraceMethod · 0.45

Tested by

no test coverage detected