/** * @param config The configuration to write as XML * @param out The stream to write the configuration to * @throws java.io.IOException If an error occurs writing the XML document */ public static void writeAsXml(MutableConfig config, java.io.OutputStream out) throws java.io.IOException { org.dom4j.DocumentFactory df = org.dom4j.DocumentFactory.getInstance(); Element root = config.toXML(df); org.dom4j.Document doc = df.createDocument(root); org.dom4j.io.OutputFormat format = org.dom4j.io.OutputFormat.createPrettyPrint(); format.setIndent("\t"); org.dom4j.io.XMLWriter writer; writer = new org.dom4j.io.XMLWriter(out, format); writer.write(doc); writer.flush(); }
/** * Writes this configuration to an XML element * * @param df The document factory with which to create the element * @return The XML element representing this configuration */ public Element toXML(org.dom4j.DocumentFactory df) { Element ret = df.createElement(theName); if (theValue != null) ret.setText(theValue); java.util.HashMap<String, int[]> attrs = new java.util.HashMap<String, int[]>(); for (MutableConfig sub : theSubConfigs) { int[] count = attrs.get(sub.theName); if (count == null) { count = new int[1]; attrs.put(sub.theName, count); } count[0]++; } for (MutableConfig sub : theSubConfigs) { if (attrs.get(sub.theName)[0] == 1 && sub.theSubConfigs.length == 0 && sub.theValue != null && sub.theValue.indexOf('\n') < 0) ret.addAttribute(sub.theName, sub.theValue); else ret.add(sub.toXML(df)); } return ret; }