List of usage examples for org.jdom2.output XMLOutputter output
public final void output(EntityRef entity, Writer out) throws IOException
EntityRef
. From source file:org.jreserve.gui.wrapper.jdom.JDomUtil.java
License:Open Source License
public static void save(final FileObject file, Document document) throws IOException { synchronized (file) { XMLOutputter output = new XMLOutputter(); OutputStream os = null;/*www . j a v a2 s.c o m*/ try { os = file.getOutputStream(); output.output(document, os); os.flush(); } catch (IOException ex) { String msg = "Unable to save document to file '%s'!"; msg = String.format(msg, file.getPath()); logger.log(Level.SEVERE, msg, ex); throw new IOException(msg, ex); } finally { if (os != null) { try { os.close(); } catch (IOException ex) { String msg = String.format("Unable to close outputstream for file '%s'!", file.getPath()); logger.log(Level.SEVERE, msg, ex); throw ex; } } } } }
From source file:org.jumpmind.metl.core.runtime.component.XsltProcessor.java
License:Open Source License
public static String getBatchXml(Model model, ArrayList<EntityData> inputRows, boolean outputAllAttributes) { SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); Element root = new Element("batch"); Document doc = new Document(root); for (ModelEntity entity : getModelEntities(model, inputRows)) { Element entityElement = new Element("entity"); entityElement.setAttribute("name", entity.getName()); root.addContent(entityElement);/* ww w. j a v a 2 s.c om*/ for (EntityData entityData : inputRows) { List<ModelAttribute> attributes = null; if (outputAllAttributes) { attributes = entity.getModelAttributes(); } else { attributes = getModelAttributes(model, entity.getId(), entityData.keySet()); } Element recordElement = new Element("record"); if (attributes.size() > 0) { entityElement.addContent(recordElement); } for (ModelAttribute attribute : attributes) { if (attribute != null && attribute.getEntityId().equals(entity.getId())) { Element attributeElement = new Element("attribute"); attributeElement.setAttribute("name", attribute.getName()); Object object = entityData.get(attribute.getId()); String value = null; DataType type = attribute.getDataType(); if (object != null) { if (type.isTimestamp() && object instanceof Date) { value = df.format(object); } else { value = object.toString(); } } attributeElement.setAttribute("value", value == null ? "" : value); recordElement.addContent(attributeElement); } } } } StringWriter writer = new StringWriter(); XMLOutputter xmlOutput = new XMLOutputter(); xmlOutput.setFormat(Format.getPrettyFormat()); try { xmlOutput.output(doc, writer); writer.close(); } catch (IOException e) { throw new RuntimeException(e); } return writer.toString(); }
From source file:org.jumpmind.metl.core.runtime.component.XsltProcessor.java
License:Open Source License
public static String getTransformedXml(String inputXml, String stylesheetXml, String xmlFormat, boolean omitXmlDeclaration) { StringWriter writer = new StringWriter(); SAXBuilder builder = new SAXBuilder(); builder.setXMLReaderFactory(XMLReaders.NONVALIDATING); builder.setFeature("http://xml.org/sax/features/validation", false); try {//from ww w.j a v a2 s . com Document inputDoc = builder.build(new StringReader(inputXml)); StringReader reader = new StringReader(stylesheetXml); XSLTransformer transformer = new XSLTransformer(reader); Document outputDoc = transformer.transform(inputDoc); XMLOutputter xmlOutput = new XMLOutputter(); Format format = null; if (xmlFormat.equals(COMPACT_FORMAT)) { format = Format.getCompactFormat(); } else if (xmlFormat.equals(RAW_FORMAT)) { format = Format.getRawFormat(); } else { format = Format.getPrettyFormat(); } format.setOmitDeclaration(omitXmlDeclaration); xmlOutput.setFormat(format); xmlOutput.output(outputDoc, writer); writer.close(); } catch (Exception e) { throw new RuntimeException(e); } return writer.toString(); }
From source file:org.jwebsocket.config.xml.JWebSocketConfigHandler.java
License:Apache License
/** * * @param aDoc//from w ww. j av a 2s . c o m * @param aPath * @throws IOException */ protected void saveChange(Document aDoc, String aPath) throws IOException { XMLOutputter lXmlOutput = new XMLOutputter(); lXmlOutput.setFormat(Format.getPrettyFormat()); lXmlOutput.output(aDoc, new FileWriter(aPath)); }
From source file:org.kdp.word.transformer.OPFTransformer.java
License:Apache License
private void writeOPFDocument(Context context, Document doc) { Options options = context.getOptions(); Path basedir = context.getBasedir(); Path filePath = options.getOpfTarget(); if (filePath == null) { String filename = context.getSource().getFileName().toString(); filename = filename.substring(0, filename.lastIndexOf('.')) + ".opf"; filePath = options.getBookDir().resolve(filename); }//w w w .ja v a2 s . c o m try { log.info("Writing OPF: {}", filePath); File outfile = basedir.resolve(filePath).toFile(); outfile.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(outfile); XMLOutputter xo = new XMLOutputter(); Format format = Format.getPrettyFormat(); xo.setFormat(format.setOmitDeclaration(false)); format.setEncoding("UTF-8"); xo.output(doc, fos); fos.close(); } catch (IOException ex) { throw new IllegalStateException("Cannot write OPF file: " + filePath, ex); } }
From source file:org.kdp.word.utils.IOUtils.java
License:Apache License
public static void writeDocument(Context context, Document doc, OutputStream out) throws IOException { Parser parser = context.getParser(); String outputEncoding = parser.getProperty(Parser.PROPERTY_OUTPUT_ENCODING); outputEncoding = outputEncoding != null ? outputEncoding : "UTF-8"; String outputFormat = parser.getProperty(Parser.PROPERTY_OUTPUT_FORMAT); boolean pretty = Parser.OUTPUT_FORMAT_PRETTY.equals(outputFormat); XMLOutputter xo = new XMLOutputter(); Format format = pretty ? Format.getPrettyFormat() : Format.getCompactFormat(); format.setEncoding(outputEncoding);/*from ww w . ja v a 2s . c om*/ EscapeStrategy strategy = new OutputEscapeStrategy(context, format.getEscapeStrategy()); format.setEscapeStrategy(strategy); xo.setFormat(format.setOmitDeclaration(true)); xo.output(doc, out); }
From source file:org.kitodo.docket.ExportXmlLog.java
License:Open Source License
/** * This method exports the production metadata as xml to a given stream. * * @param docketData// w ww .j a va 2 s .c om * the docket data to export * @param os * the OutputStream to write the contents to * @throws IOException * Throws IOException, when document creation fails. */ void startExport(DocketData docketData, OutputStream os) throws IOException { try { Document doc = createDocument(docketData, true); XMLOutputter outp = new XMLOutputter(); outp.setFormat(Format.getPrettyFormat()); outp.output(doc, os); os.close(); } catch (RuntimeException e) { logger.error("Document creation failed."); throw new IOException(e); } }
From source file:org.kitodo.docket.ExportXmlLog.java
License:Open Source License
/** * This method exports the production metadata for al list of processes as a * single file to a given stream./* www . j a v a 2 s . com*/ * * @param docketDataList * a list of Docket data * @param outputStream * The output stream, to write the docket to. */ void startMultipleExport(Iterable<DocketData> docketDataList, OutputStream outputStream) { Document answer = new Document(); Element root = new Element("processes"); answer.setRootElement(root); Namespace xmlns = Namespace.getNamespace(NAMESPACE); Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"); root.addNamespaceDeclaration(xsi); root.setNamespace(xmlns); Attribute attSchema = new Attribute("schemaLocation", NAMESPACE + " XML-logfile.xsd", xsi); root.setAttribute(attSchema); for (DocketData docketData : docketDataList) { Document doc = createDocument(docketData, false); Element processRoot = doc.getRootElement(); processRoot.detach(); root.addContent(processRoot); } XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat()); try { outp.output(answer, outputStream); } catch (IOException e) { logger.error("Generating XML Output failed.", e); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { logger.error("Closing the output stream failed.", e); } } } }
From source file:org.mule.tools.apikit.output.MuleConfigGenerator.java
License:Open Source License
public void generate() { Map<API, Document> docs = new HashMap<API, Document>(); for (GenerationModel flowEntry : flowEntries) { Document doc;/*from ww w . j a va2 s. c o m*/ API api = flowEntry.getApi(); try { doc = getOrCreateDocument(docs, api); } catch (Exception e) { log.error("Error generating xml for file: [" + api.getYamlFile() + "]", e); continue; } // Generate each of the APIKit flows doc.getRootElement().addContent(new APIKitFlowScope(flowEntry).generate()); } // Write everything to files for (Map.Entry<API, Document> yamlFileDescriptorDocumentEntry : docs.entrySet()) { Format prettyFormat = Format.getPrettyFormat(); prettyFormat.setIndent(INDENTATION); prettyFormat.setLineSeparator(System.getProperty("line.separator")); prettyFormat.setEncoding("UTF-8"); XMLOutputter xout = new XMLOutputter(prettyFormat); Document doc = yamlFileDescriptorDocumentEntry.getValue(); File xmlFile = yamlFileDescriptorDocumentEntry.getKey().getXmlFile(rootDirectory); try { FileOutputStream fileOutputStream = new FileOutputStream(xmlFile); xout.output(doc, fileOutputStream); fileOutputStream.close(); log.info("Updating file: [" + xmlFile + "]"); } catch (IOException e) { log.error("Error writing to file: [" + xmlFile + "]", e); } } // Generate mule deploy properties file new MuleDeployWriter(rootDirectory).generate(); }
From source file:org.mycore.common.MCRUtils.java
License:Open Source License
/** * This method convert a JDOM tree to a byte array. * /* w w w .ja v a 2 s. co m*/ * @param jdom * the JDOM tree format the JDOM output format * @return a byte array of the JDOM tree * @deprecated use {@link MCRJDOMContent#asByteArray()} */ @Deprecated public static byte[] getByteArray(org.jdom2.Document jdom, Format format) throws MCRPersistenceException { MCRConfiguration conf = MCRConfiguration.instance(); String mcr_encoding = conf.getString("MCR.Metadata.DefaultEncoding", DEFAULT_ENCODING); ByteArrayOutputStream outb = new ByteArrayOutputStream(); try { XMLOutputter outp = new XMLOutputter(format.setEncoding(mcr_encoding)); outp.output(jdom, outb); } catch (Exception e) { throw new MCRPersistenceException("Can't produce byte array."); } return outb.toByteArray(); }