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)
| 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, |
no test coverage detected