static { final Format rawFormat = Format.getRawFormat(); // JDOM has \r\n hardcoded rawFormat.setLineSeparator("\n"); OUTPUTTER = new XMLOutputter(rawFormat); BUILDER = new SAXBuilder(); BUILDER.setValidation(false); }
/** {@inheritDoc} */ public String getString() throws IOException { Element rootElement = m_document.getRootElement(); processChildren(rootElement); m_document.setContext(m_context); XMLOutputter output = new XMLOutputter(); StringWriter out = new StringWriter(); Format fmt = Format.getRawFormat(); fmt.setExpandEmptyElements(false); fmt.setLineSeparator(LINEBREAK); output.setFormat(fmt); output.outputElementContent(m_document.getRootElement(), out); return out.toString(); }
/** * Generate the .moml index file for the given arguments. Example: * * <pre> * <?xml version="1.0"?> * <!DOCTYPE plot PUBLIC "-//UC Berkeley//DTD MoML 1//EN" "http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd"> * * <!--DO NOT EDIT. This file was generated by ptolemy.domains.ptinyos.util.nc2moml.MoMLLib. The filename was chosen so that it does not conflict with .nc files of the same name and any other ptII-referenced .moml file in the classpath.--> * <entity name="Counters" class="ptolemy.moml.EntityLibrary"> * <configure> * <?moml * <group> * <entity name="Counter" class="tos.lib.Counters.Counter" /> * <entity name="IntToLeds" class="tos.lib.Counters.IntToLeds" /> * <entity name="IntToLedsM" class="tos.lib.Counters.IntToLedsM" /> * <entity name="IntToRfm" class="tos.lib.Counters.IntToRfm" /> * <entity name="IntToRfmM" class="tos.lib.Counters.IntToRfmM" /> * <entity name="RfmToInt" class="tos.lib.Counters.RfmToInt" /> * <entity name="RfmToIntM" class="tos.lib.Counters.RfmToIntM" /> * <entity name="SenseToInt" class="tos.lib.Counters.SenseToInt" /> * </group> * ?> * </configure> * </entity> * </pre> * * @param components Array containing the components in short path format relative to the root. * Example: tos/lib/Counters/Counter * @param libraryName Name of this directory. * @param indexFiles Array containing the sub-index files in short path format relative to the * outputFile directory. Example: subdir/_TOSIndex.moml * @param outputFile The file to generate in long path format. Example: * /home/celaine/ptII/vendors/ptinyos/moml/tos/lib/Counters/Counter/index.moml * @exception IOException If there is a problem writing files. */ public static void generateIndex( String[] components, String[] indexFiles, String libraryName, String outputFile) throws IOException { Element root = new Element("entity"); root.setAttribute("name", libraryName); root.setAttribute("class", "ptolemy.moml.EntityLibrary"); Element configure = new Element("configure"); root.addContent(configure); DocType plot = new DocType( "plot", "-//UC Berkeley//DTD MoML 1//EN", "http://ptolemy.eecs.berkeley.edu/xml/dtd/MoML_1.dtd"); Document doc = new Document(); // root, plot); String comment = "DO NOT EDIT. This file was generated by " + "ptolemy.domains.ptinyos.util.nc2moml.MoMLLib. " + "The filename was chosen so that it does not " + "conflict with .nc files of the same name and any " + "other ptII-referenced .moml file in the classpath."; doc.addContent(new Comment(comment)); doc.setRootElement(root); doc.setDocType(plot); Element group = new Element("group"); // Make entries for index files. // Example: // <input source="Counters/index.moml"/> for (int i = 0; i < indexFiles.length; i++) { Element input = new Element("input"); try { input.setAttribute("source", FileUtilities.nameToURL(indexFiles[i], null, null).toString()); } catch (MalformedURLException e) { input.setAttribute("source", indexFiles[i]); } group.addContent(input); } // Make entries for component files. for (int i = 0; i < components.length; i++) { String c = components[i]; if (File.separator.equals("\\")) { c = c.replace('\\', '/'); } String[] subNames = c.split("/"); String componentName = subNames[subNames.length - 1]; String className = c.replace('/', '.'); Element entity = new Element("entity"); entity.setAttribute("name", componentName); entity.setAttribute("class", className); group.addContent(entity); } // Setup format for xml serializer. XMLOutputter serializer = new XMLOutputter(Format.getPrettyFormat()); Format format = serializer.getFormat(); format.setOmitEncoding(true); format.setLineSeparator("\n"); serializer.setFormat(format); // Create "?moml" processing instruction. ProcessingInstruction moml = new ProcessingInstruction("moml", "\n" + serializer.outputString(group) + "\n"); configure.addContent(moml); // Generate index file. FileOutputStream out = null; try { if (outputFile != null) { out = new FileOutputStream(outputFile); } if (out != null) { serializer.output(doc, out); } else { serializer.output(doc, System.out); } } catch (IOException ex) { IOException ioException = new IOException("Error writing index file \"" + outputFile + "\""); ioException.initCause(ex); throw ioException; } finally { if (out != null) { try { out.close(); } catch (IOException ex) { System.err.println("Failed to close " + outputFile + ": " + ex); } } } }
public void saveToFile(File dest) throws IOException { File temp = File.createTempFile(dest.getName(), ".tmp", dest.getParentFile()); try { Element root = new Element("MarrsProject"); root.setAttribute("title", title); root.setAttribute("schemaversion", "" + CURRENT_SCHEMAVERSION); for (MarrsQuery q : rows) { Element queryElt = new Element("Query"); CDATA data = new CDATA(q.getQueryText()); queryElt.addContent(data); queryElt.setAttribute("title", q.getTitle()); queryElt.setAttribute("type", "" + q.getQueryType()); if (q.getAskBefore() != null) { Element askElt = new Element("AskBefore"); askElt.setAttribute("key", q.getAskBefore()); queryElt.addContent(askElt); } for (Map.Entry<String, String> context : q.getContext().entrySet()) { Element contextElt = new Element("Context"); contextElt.setAttribute( "key", context.getKey()); // for now, we only use "type" to filter context, so this is // hardcoded. contextElt.setAttribute("value", context.getValue()); queryElt.addContent(contextElt); } for (String var : q.getPostProcessingVars()) { Element processElt = new Element("PostProcessing"); processElt.setAttribute( "var", var); // for now, we only use "type" to filter context, so this is hardcoded. processElt.setAttribute("operation", q.getPostProcessingOperation(var)); queryElt.addContent(processElt); } root.addContent(queryElt); } for (Map.Entry<String, String> param : parameters.entrySet()) { Element paramElt = new Element("Param"); paramElt.setAttribute("key", param.getKey()); paramElt.setAttribute("val", param.getValue()); root.addContent(paramElt); } for (NodeAttribute attr : nodeAttributes) { Element nodeAttr = new Element("NodeAttribute"); nodeAttr.setAttribute("key", attr.key); nodeAttr.setAttribute("value", attr.value); for (Map.Entry<String, String> e : attr.vizprops.entrySet()) { Element vizprop = new Element("Vizmap"); vizprop.setAttribute("prop", e.getKey()); vizprop.setAttribute("value", e.getValue()); nodeAttr.addContent(vizprop); } root.addContent(nodeAttr); } Document doc = new Document(root); XMLOutputter xout = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setIndent("\t"); format.setLineSeparator("\n"); format.setTextMode(TextMode.PRESERVE); xout.setFormat(format); xout.output(doc, new FileOutputStream(temp)); temp.renameTo(dest); dirty = false; } finally { temp.delete(); } }