Search in sources :

Example 21 with OutputFormat

use of org.dom4j.io.OutputFormat in project OpenOLAT by OpenOLAT.

the class ContentPackage method writeToFile.

/**
 * writes the manifest.xml
 */
void writeToFile() {
    String filename = "imsmanifest.xml";
    OutputFormat format = OutputFormat.createPrettyPrint();
    try {
        VFSLeaf outFile;
        // file may exist
        outFile = (VFSLeaf) cpcore.getRootDir().resolve("/" + filename);
        if (outFile == null) {
            // if not, create it
            outFile = cpcore.getRootDir().createChildLeaf("/" + filename);
        }
        DefaultDocument manifestDocument = cpcore.buildDocument();
        XMLWriter writer = new XMLWriter(outFile.getOutputStream(false), format);
        writer.write(manifestDocument);
    } catch (Exception e) {
        log.error("imsmanifest for ores " + ores.getResourceableId() + "couldn't be written to file.", e);
        throw new OLATRuntimeException(CPOrganizations.class, "Error writing imsmanifest-file", new IOException());
    }
}
Also used : CPOrganizations(org.olat.ims.cp.objects.CPOrganizations) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) OutputFormat(org.dom4j.io.OutputFormat) DefaultDocument(org.dom4j.tree.DefaultDocument) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter) IOException(java.io.IOException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException)

Example 22 with OutputFormat

use of org.dom4j.io.OutputFormat in project xresloader by xresloader.

the class DataDstXml method build.

@Override
public final byte[] build(DataDstImpl compiler) throws ConvException {
    // pretty print
    OutputFormat of = null;
    if (ProgramOptions.getInstance().prettyIndent <= 0) {
        of = OutputFormat.createCompactFormat();
    } else {
        of = OutputFormat.createPrettyPrint();
        of.setIndentSize(ProgramOptions.getInstance().prettyIndent);
    }
    // build data
    DataDstObject data_obj = build_data(compiler);
    // build xml tree
    Document doc = DocumentHelper.createDocument();
    String encoding = SchemeConf.getInstance().getKey().getEncoding();
    if (null != encoding && false == encoding.isEmpty()) {
        doc.setXMLEncoding(encoding);
        of.setEncoding(encoding);
    }
    doc.setRootElement(DocumentHelper.createElement(ProgramOptions.getInstance().xmlRootName));
    doc.getRootElement().addComment("this file is generated by xresloader, please don't edit it.");
    Element header = DocumentHelper.createElement("header");
    Element body = DocumentHelper.createElement("body");
    writeData(header, data_obj.header, header.getName());
    // body
    for (Map.Entry<String, List<Object>> item : data_obj.body.entrySet()) {
        for (Object obj : item.getValue()) {
            Element xml_item = DocumentHelper.createElement(item.getKey());
            writeData(xml_item, obj, item.getKey());
            body.add(xml_item);
        }
    }
    writeData(body, data_obj.body, body.getName());
    doc.getRootElement().add(header);
    doc.getRootElement().add(body);
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        XMLWriter writer = new XMLWriter(bos, of);
        writer.write(doc);
        return bos.toByteArray();
    } catch (Exception e) {
        ProgramOptions.getLoger().error("write xml failed, %s", e.getMessage());
        return null;
    }
}
Also used : Element(org.dom4j.Element) OutputFormat(org.dom4j.io.OutputFormat) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document) HashMap(java.util.HashMap) Map(java.util.Map) XMLWriter(org.dom4j.io.XMLWriter) ConvException(com.owent.xresloader.data.err.ConvException)

Example 23 with OutputFormat

use of org.dom4j.io.OutputFormat in project fmv by f-agu.

the class Project method save.

/**
 */
public void save() throws IOException {
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement("fmv");
    saveMain(root);
    saveSources(root);
    saveExtensions(root);
    saveProperties(root);
    saveUndeleteFileName(root);
    saveOutputInfos(root);
    saveExecutables(root);
    // write
    OutputFormat format = new OutputFormat("\t");
    format.setTrimText(true);
    format.setNewlines(true);
    XMLWriter writer = new XMLWriter(new FileWriter(getSaveFile()), format);
    writer.write(document);
    writer.close();
    modified = false;
}
Also used : Element(org.dom4j.Element) FileWriter(java.io.FileWriter) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Example 24 with OutputFormat

use of org.dom4j.io.OutputFormat in project mule by mulesoft.

the class DefaultExtensionSchemaGenerator method renderSchema.

private String renderSchema(Schema schema) {
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(Schema.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        NamespaceFilter outFilter = new NamespaceFilter(CORE_PREFIX, CORE_NAMESPACE, true);
        OutputFormat format = new OutputFormat();
        format.setIndent(true);
        format.setNewlines(true);
        StringWriter sw = new StringWriter();
        XMLWriter writer = new XMLWriter(sw, format);
        outFilter.setContentHandler(writer);
        marshaller.marshal(schema, outFilter);
        return sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) NamespaceFilter(org.mule.runtime.module.extension.internal.capability.xml.schema.model.NamespaceFilter) StringWriter(java.io.StringWriter) JAXBException(javax.xml.bind.JAXBException) OutputFormat(org.dom4j.io.OutputFormat) JAXBContext(javax.xml.bind.JAXBContext) XMLWriter(org.dom4j.io.XMLWriter)

Example 25 with OutputFormat

use of org.dom4j.io.OutputFormat in project notes by KevinBlandy.

the class Dom4jDemo method add.

/**
 * ��ӽڵ����(���һ��gender��ǩ)
 */
public static void add() throws Exception {
    // ǰ�漸���趼һ��.ʡ��ע����
    Document document = new SAXReader().read(file);
    // ��ȡ���ڵ�
    Element element = document.getRootElement();
    // ��ȡҪ��ӵĽڵ����,,Ҳ���ǵڶ���p�ڵ�
    Element ele = (Element) element.elements("p").get(1);
    // ���һ������gender���ӱ�ǩ
    Element newEle = ele.addElement("gender");
    // Ϊ�±�ǩ����ı�
    newEle.setText("��");
    /**
     *ע��,��д������,��ɾ����һ��Ҫ�ǵû�д*
     */
    // �����ı���ʽ,��������
    OutputFormat format = OutputFormat.createPrettyPrint();
    // ����������
    XMLWriter wr = new XMLWriter(new FileOutputStream(file), format);
    // ���ڴ��е�Document��д���ļ���
    wr.write(document);
    // �ر���Դ
    wr.close();
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.dom4j.io.OutputFormat) Document(org.dom4j.Document) XMLWriter(org.dom4j.io.XMLWriter)

Aggregations

OutputFormat (org.dom4j.io.OutputFormat)89 XMLWriter (org.dom4j.io.XMLWriter)75 IOException (java.io.IOException)39 Document (org.dom4j.Document)31 Element (org.dom4j.Element)27 ByteArrayOutputStream (java.io.ByteArrayOutputStream)18 StringWriter (java.io.StringWriter)18 SAXReader (org.dom4j.io.SAXReader)16 File (java.io.File)15 FileOutputStream (java.io.FileOutputStream)14 FileWriter (java.io.FileWriter)10 OutputStreamWriter (java.io.OutputStreamWriter)8 StringReader (java.io.StringReader)6 DocumentException (org.dom4j.DocumentException)6 OutputStream (java.io.OutputStream)5 Test (org.junit.Test)5 BufferedOutputStream (java.io.BufferedOutputStream)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4