MCPcopy Index your code
hub / github.com/benfry/processing4 / format

Method format

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

Takes an XML object and converts it to a String , formatting its content as specified with the indent parameter. If indent is set to -1, then the String is returned with no line breaks, no indentation, and no XML declaration. If indent is set to 0

(int indent)

Source from the content-addressed store, hash-verified

1068 * @see XML#toString()
1069 */
1070 public String format(int indent) {
1071 try {
1072 // entities = doctype.getEntities()
1073 boolean useIndentAmount = false;
1074 TransformerFactory factory = TransformerFactory.newInstance();
1075 if (indent != -1) {
1076 try {
1077 factory.setAttribute("indent-number", indent);
1078 } catch (IllegalArgumentException e) {
1079 useIndentAmount = true;
1080 }
1081 }
1082 Transformer transformer = factory.newTransformer();
1083
1084 // Add the XML declaration at the top if this node is the root and we're
1085 // not writing to a single line (indent = -1 means single line).
1086 if (indent == -1 || parent == null) {
1087 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
1088 } else {
1089 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
1090 }
1091
1092// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "sample.dtd");
1093
1094 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
1095
1096// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); // huh?
1097
1098// transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,
1099// "-//W3C//DTD XHTML 1.0 Transitional//EN");
1100// transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
1101// "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");
1102
1103 // For Android, because (at least 2.3.3) doesn't like indent-number
1104 if (useIndentAmount) {
1105 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
1106 }
1107
1108// transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
1109// transformer.setOutputProperty(OutputKeys.ENCODING,"UTF8");
1110 transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
1111// transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS
1112
1113 // Always indent, otherwise the XML declaration will just be jammed
1114 // onto the first line with the XML code as well.
1115 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1116
1117// Properties p = transformer.getOutputProperties();
1118// for (Object key : p.keySet()) {
1119// System.out.println(key + " -> " + p.get(key));
1120// }
1121
1122 // If you smell something, that's because this code stinks. No matter
1123 // the settings of the Transformer object, if the XML document already
1124 // has whitespace elements, it won't bother re-indenting/re-formatting.
1125 // So instead, transform the data once into a single line string.
1126 // If indent is -1, then we're done. Otherwise re-run and the settings
1127 // 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 9

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

Tested by

no test coverage detected