Example usage for org.dom4j.io OutputFormat createPrettyPrint

List of usage examples for org.dom4j.io OutputFormat createPrettyPrint

Introduction

In this page you can find the example usage for org.dom4j.io OutputFormat createPrettyPrint.

Prototype

public static OutputFormat createPrettyPrint() 

Source Link

Document

A static helper method to create the default pretty printing format.

Usage

From source file:hello.SampleSimpleApplication.java

License:Apache License

private void makeLargeHTML() throws IOException {
    logger.debug("Start folder : " + dirJsonStart);
    Files.walkFileTree(dirJsonStart, new SimpleFileVisitor<Path>() {

        @Override//from ww  w.  j a va  2 s  .c o  m
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            final FileVisitResult visitFile = super.visitFile(file, attrs);
            fileIdx++;

            logger.debug(fileIdx + "" + fileSeparator + filesCount + procentWorkTime() + file);
            logger.debug(fileSeparator);
            String fileStr = file.toFile().toString();
            logger.debug(fileStr);
            if (fileSeparator.equals("\\"))
                fileStr = fileStr.replace(fileSeparator, "/");
            String[] split = fileStr.split("/");
            String manufacturerName = split[split.length - 2];
            Map<String, Object> jsonMap = readJsonDbFile2map(fileStr);
            String autoName = (String) jsonMap.get("autoName");
            String autoNameWithManufacturer = split[split.length - 1].replace(".json", "");
            //            String autoNameWithManufacturer = manufacturerName+"_-_"+autoName;
            String pdfTitleAutoNameWithManufacturer = manufacturerName + " :: " + autoName;
            logger.debug(autoNameWithManufacturer + " -- BEGIN");

            String htmlOutFileName2 = dirLargeHtmlName + autoNameWithManufacturer;
            logger.debug(htmlOutFileName2);
            File f = new File(htmlOutFileName2);
            if (f.exists()) {
                logger.debug("f.exists() --  " + htmlOutFileName2);
                return visitFile;
            }
            Element autoDocBody = createAutoDocument(pdfTitleAutoNameWithManufacturer);
            autoTileNr = 0;
            bookmarkId = 0;
            debugSkip = 0;
            Element headEl = (Element) autoDocument.selectSingleNode("/html/head");
            bookmarks = headEl.addElement("bookmarks");
            //            addGroupAndRealInfo2(2,getIndexList(jsonMap));
            //buildBookmark(autoDocument);

            addGroupAndRealInfo(bookmarks, getIndexList(jsonMap));
            logger.debug(htmlOutFileName2 + " -- GOTO SAVE");

            try {
                //               String htmlOutFileName = dirLargeHtmlName+autoNameWithManufacturer+".html";
                saveHtml(autoDocument, htmlOutFileName2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            logger.debug(autoNameWithManufacturer + " -- END");
            return visitFile;
        }

        private void saveHtml(Document document, String htmlOutFileName) {
            writeToHtmlFile(document, htmlOutFileName);
        }

        private void writeToHtmlFile(Document document, String htmlOutFileName) {
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(htmlOutFileName);
                //               HTMLWriter xmlWriter = new HTMLWriter(fileOutputStream, prettyPrintFormat);
                XMLWriter xmlWriter = new XMLWriter(fileOutputStream, prettyPrintFormat);
                xmlWriter.write(document);
                xmlWriter.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        OutputFormat prettyPrintFormat = OutputFormat.createPrettyPrint();

        private List<Map<String, Object>> getIndexList(Map<String, Object> jsonMap) {
            return (List<Map<String, Object>>) jsonMap.get("indexList");
        }

        private void addGroupAndRealInfo(Element bookmarkParent, List<Map<String, Object>> indexList) {
            if (indexList != null) {
                for (Map<String, Object> map : indexList) {
                    String text = (String) map.get("text");
                    Element bookmarkElement = bookmarkParent.addElement("bookmark");
                    bookmarkElement.addAttribute("name", text);
                    bookmarkElement.addAttribute("href", "#b_" + bookmarkId++);
                    String url = (String) map.get("url");
                    if (url != null) {
                        addRealInfo(url);
                    }
                    //                  if(debugSkip > 13)
                    //                     break;
                    addGroupAndRealInfo(bookmarkElement, getIndexList(map));
                }
            }
        }
    });

}

From source file:hk.hku.cecid.piazza.commons.util.PropertyTree.java

License:Open Source License

/**
 * Stores the properties to the specified url location.
 * /*from   w ww  . ja v  a2  s  .c  o m*/
 * @param url the url of the properties source.
 * @throws Exception if the operation is unsuccessful. 
 * @see hk.hku.cecid.piazza.commons.module.PersistentComponent#storing(java.net.URL)
 */
protected void storing(URL url) throws Exception {
    XMLWriter writer = new XMLWriter(new FileOutputStream(Convertor.toFile(url)),
            OutputFormat.createPrettyPrint());
    writer.write(dom);
    writer.close();
}

From source file:hvv_admin4.state.HVV_StateKeeperXML.java

public void SaveState() {
    if (theApp.GetCurrentStep() < 20)
        return;/*from   w  w  w.j  ava 2 s. co  m*/

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");

    try {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("SaveStateXML");

        // 
        root.addElement("CurrentStep").addText("" + theApp.GetCurrentStep());

        //?   
        root.addElement("SerialNumber").addText(theApp.GetSerial());

        //  
        root.addElement("ProcessedDeviceType").addText("" + theApp.GetProcessedDeviceType());

        // ? 
        if (theApp.GetDtmTOEnd() == null)
            root.addElement("dtmTOEnd").addText("NULL");
        else
            root.addElement("dtmTOEnd").addText(formatter.format(theApp.GetDtmTOEnd()));

        Set set = theApp.SecretSteps().entrySet();
        Iterator it = set.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();

            String strKey = (String) entry.getKey();
            Object objValue = entry.getValue();

            Element innerRoot = root.addElement("Step" + strKey);
            switch (strKey) {
            case "001":
            case "061":
            case "062":
            case "064":
            case "101":
            case "103":
            case "105":
            case "141":
            case "143":
            case "181":
                ((TechProcessStepCommon) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "021":
            case "022":
            case "041":
            case "042":
            case "043":
            case "044":
            case "102":
            case "104":
            case "106":
            case "142":
            case "144":
                ((TechProcessHvProcessInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "063":
            case "121":
            case "161":
                ((TechProcessGetterInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "081":
            case "182":
                ((TechProcessIgenIextProcessInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "082":
            case "183":
                ((TechProcessUacProcessInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            case "083":
            case "184":
                ((TechProcessCommentInfo) objValue).SaveItemXML(innerRoot,
                        theApp.GetStepNameWithNum(Integer.parseInt(strKey)));
                break;

            /*
            case "122":
            case "162":
            (( TechProcessHFInfo ) objValue).SaveItemXML( innerRoot); break;
            */
            }
        }
        OutputFormat format = OutputFormat.createPrettyPrint();

        //File fl = new File( m_strStateKeepFileName);
        //fl.createNewFile();
        XMLWriter writer = new XMLWriter(new FileWriter(m_strStateKeepFileName), format);

        writer.write(document);
        writer.close();
    } catch (IOException ex) {
        logger.error("IOException caught while saving state!", ex);
    }
}

From source file:hvv_constructor.FrmMainWindow.java

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
    Document saveFile = DocumentHelper.createDocument();
    Element program = saveFile.addElement("Program");

    Set set = theApp.m_program.entrySet();
    Iterator it = set.iterator();
    while (it.hasNext()) {
        Map.Entry entry = (Map.Entry) it.next();
        int nLineNumber = (int) entry.getKey();
        JProg.JProgAStatement abstractStatement = (JProg.JProgAStatement) entry.getValue();
        Element statement = program.addElement("LineNumber").addText("" + nLineNumber);
        abstractStatement.AddXMLStatement(statement);
    }/*  w  ww  .j a va  2 s .c o  m*/

    OutputFormat format = OutputFormat.createPrettyPrint();

    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new MyXMLFilter());
    fc.setCurrentDirectory(new File(theApp.GetAMSRoot() + "/ReadyPrograms"));

    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        String strFilePathName = fc.getSelectedFile().getAbsolutePath();
        if (!strFilePathName.endsWith(".xml"))
            strFilePathName += ".xml";
        File file = new File(strFilePathName);
        XMLWriter writer;
        try {
            writer = new XMLWriter(new FileWriter(file.getAbsolutePath()), format);
            writer.write(saveFile);
            writer.close();
        } catch (IOException ex) {
            logger.error("IOException: ", ex);
        }

    } else {
        logger.error(
                "   ?   ?? .");
    }

}

From source file:io.mashin.oep.model.Workflow.java

License:Open Source License

@Override
public void write(org.dom4j.Element parent) {
    Document document = parent.getDocument();
    parent.detach();/*from   w w  w  . jav  a2s  .co  m*/

    Element rootElement = document.addElement("workflow-app");
    Element graphicalInfoElement = DocumentHelper.createElement("workflow");

    XMLWriteUtils.writeWorkflowSchemaVersion(getSchemaVersion(), rootElement);
    XMLWriteUtils.writeSLAVersion(this, rootElement);
    XMLWriteUtils.writeTextPropertyAsAttribute(name, rootElement, "name");
    XMLWriteUtils.writePropertiesCollection(parameters, rootElement, "parameters", "property");
    XMLWriteUtils.writeGlobalProperty(global, rootElement);
    XMLWriteUtils.writeCredentialsCollection(credentials, rootElement);

    startNode.write(rootElement);
    for (Node node : nodes) {
        if (!(node.equals(startNode) || node.equals(endNode))) {
            node.write(rootElement);
        }
        graphicalInfoElement.addElement("node").addAttribute("name", node.getName())
                .addAttribute("x", node.getPosition().x + "").addAttribute("y", node.getPosition().y + "");
    }
    endNode.write(rootElement);

    XMLWriteUtils.writeSLAProperty(this, sla, rootElement);

    Comment graphicalInfoNode = null;
    try {
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
        writer.write(graphicalInfoElement);
        writer.flush();
        graphicalInfoNode = DocumentHelper.createComment(stringWriter.toString());
    } catch (Exception e) {
        graphicalInfoNode = DocumentHelper.createComment(graphicalInfoElement.asXML());
    }
    document.add(graphicalInfoNode);
}

From source file:io.mashin.oep.parts.WorkflowNodeEditPart.java

License:Open Source License

private void performOpen() {
    if (getCastedModel() instanceof CustomActionNode) {
        CustomActionNode customActionNode = (CustomActionNode) getCastedModel();
        String customActionNodeXML = "";

        try {// w  ww  .j a v  a 2  s  .  c  o  m
            StringWriter stringWriter = new StringWriter();
            XMLWriter writer = new XMLWriter(stringWriter, OutputFormat.createPrettyPrint());
            writer.write(DocumentHelper
                    .parseText((String) customActionNode.getPropertyValue(CustomActionNode.PROP_XML))
                    .getRootElement());
            writer.flush();
            customActionNodeXML = stringWriter.toString();
        } catch (Exception e) {
            e.printStackTrace();
            customActionNodeXML = (String) customActionNode.getPropertyValue(CustomActionNode.PROP_XML);
        }

        XMLEditor.getInstance().open(customActionNode, customActionNodeXML, customActionNode.getName(),
                xml -> getViewer().getEditDomain().getCommandStack()
                        .execute(new CustomActionNodeXMLEditCommand(customActionNode, xml)));
    }
}

From source file:io.mashin.oep.ui.editor.WorkflowEditor.java

License:Open Source License

private String writeModel() {
    try {//  w ww  .  j  av  a  2 s  . c o m
        Document document = DocumentHelper.createDocument();
        workflow.write(document.addElement("dummy"));
        OutputFormat format = OutputFormat.createPrettyPrint();
        StringWriter stringWriter = new StringWriter();
        XMLWriter writer = new XMLWriter(stringWriter, format);
        writer.write(document);
        stringWriter.flush();
        String hpdl = stringWriter.toString();
        hpdl = XMLUtils.schemaVersionToXmlns(hpdl);
        //hpdl = XMLUtils.slaVersionToXmlnsSLA(hpdl);
        return hpdl;
    } catch (IOException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:iqq.app.service.impl.IMResourceServiceImpl.java

License:Apache License

@Override
public void writeXml(Document document, File xmlFile) {
    try {/*  ww  w  .j av a  2s  .c o  m*/
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();// XML?
        outputFormat.setEncoding("UTF-8");// XML?
        outputFormat.setIndent(true);// ?
        outputFormat.setIndent("   ");// TAB?
        outputFormat.setNewlines(true);// ??
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(xmlFile), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:iqq.app.util.XmlUtils.java

License:Apache License

/**
 * XML//from  w w  w . j av  a2  s  .c  om
 *
 * @param document
 * @param xmlFile
 * @throws IOException
 */
public static void writeXml(String filename, Document document) throws IOException {
    LOG.debug("XML: " + filename);
    OutputFormat outputFormat = OutputFormat.createPrettyPrint();// XML?
    outputFormat.setEncoding("UTF-8");// XML?
    outputFormat.setIndent(true);// ?
    outputFormat.setIndent("   ");// TAB?
    outputFormat.setNewlines(true);// ??
    synchronized (document) {
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(filename), outputFormat);
        xmlWriter.write(document);
        xmlWriter.close();
    }
}

From source file:it.doqui.index.ecmengine.business.foundation.repository.ExportSvcBean.java

License:Open Source License

private Exporter createXMLExporter(OutputStream viewWriter, ReferenceType referenceType) {
    // Define output format
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setNewLineAfterDeclaration(false);
    format.setIndentSize(2);/*from www. j a  va  2 s.  c  o  m*/
    format.setEncoding("UTF-8");

    // Construct an XML Exporter
    try {
        XMLWriter writer = new XMLWriter(viewWriter, format);
        ECMEngineExporter exporter = new ECMEngineExporter(serviceRegistry.getNamespaceService(),
                serviceRegistry.getNodeService(), serviceRegistry.getSearchService(),
                serviceRegistry.getDictionaryService(), serviceRegistry.getPermissionService(), writer);
        exporter.setReferenceType(referenceType);
        return exporter;
    } catch (UnsupportedEncodingException e) {
        throw new ExporterException("Failed to create XML Writer for export", e);
    } catch (Exception e) {
        throw new ExporterException("Failed to create XML Writer for export", e);
    }
}