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:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * This method will generate XML file in a StringBuffer based on the given
 * Dom4j object.//from w  ww. j a  v a 2s. com
 * 
 * @param xmlObj
 *            Object
 * @param encoding
 *            String
 * @throws IOException
 * @return StringBuffer
 * @throws BaseException
 */
public static StringBuffer generateXMLStringBuffer(Object xmlObj, String encoding) throws BaseException {
    StringWriter writer = new StringWriter();
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    XMLWriter xmlWriter = null;
    xmlWriter = new XMLWriter(writer, outformat);

    try {
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0002", ex);
    }

    return writer.getBuffer();
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * XML?/*from   w  ww  .  ja v  a 2 s .c o  m*/
 * 
 * @param xmlObj
 * @param encoding
 * @param filename
 * @return
 * @throws BaseException
 */
public static boolean generateXMLFile(Object xmlObj, String encoding, String filename) throws BaseException {
    FileWriter writer = null;
    OutputFormat outformat = OutputFormat.createPrettyPrint();

    // ?
    if (encoding == null || encoding.trim().equals("")) {
        encoding = DEFAULT_ENCODING;
    }
    outformat.setEncoding(encoding);

    // dom4j ?OBJECT
    try {
        writer = new FileWriter(filename);
        XMLWriter xmlWriter = null;
        xmlWriter = new XMLWriter(writer, outformat);
        xmlWriter.write(xmlObj);
        xmlWriter.flush();
    } catch (Exception ex) {
        throw new BaseException("UTIL-0004", ex);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
            }
    }

    return true;
}

From source file:com.bsoft.baseframe.baseframe_utils.beanUtils.FileUtils.java

License:Open Source License

/**
 * ?XML?//from w w w . ja  v a  2s  .c o  m
 * @param document 
 * @param file
 * @throws IOException
 */
public static boolean wrieteXML2Doc(Document document, File file) {
    boolean isCreate = false;
    try {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();// 
        }
        if (!file.exists()) {
            isCreate = true;
            file.createNewFile();// java testData.java
        } else {
            isCreate = false;
        }
        //?XML??
        if (isCreate) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8"); //
            //document
            XMLWriter writer = new XMLWriter(new FileWriter(file), format);
            writer.write(document);
            writer.close();
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

public void output(Writer writer, RuleTemplateManager ruleTemplateManager) throws Exception {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(Constants.DEFAULT_CHARSET);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.startDocument();//from  ww  w  .  j a  va2s .c o m

    Element rootElement = DocumentHelper.createElement("RuleSet");
    rootElement.addAttribute("version", ruleTemplateManager.getVersion());
    xmlWriter.writeOpen(rootElement);

    OutputContext context = new OutputContext();
    outputPackageInfos(xmlWriter, ruleTemplateManager, context);

    for (RuleTemplate ruleTemplate : ruleTemplateManager.getRuleTemplates()) {
        // PropertyDataType?
        // if (ruleTemplate.isAbstract()
        // && ruleTemplate.getSubRuleTemplates().length == 0) {
        // continue;
        // }
        outputRuleTemplate(xmlWriter, ruleTemplate, context);
    }

    xmlWriter.writeClose(rootElement);
    xmlWriter.endDocument();
    xmlWriter.close();
}

From source file:com.bullx.demo.xml.XMLParser.java

License:Open Source License

public static void bookListToXML(List<Book> books) {
    Document document = DocumentHelper.createDocument();
    // XMLbooks//  w  ww.ja  va  2s.c o m
    Element booksElement = document.addElement("books");
    //  
    booksElement.addComment("This is a test for dom4j, liubida, 2012.8.11");

    for (Book book : books) {
        // 
        Element bookElement = booksElement.addElement("book");
        // : show
        bookElement.addAttribute("show", book.getShow() ? "yes" : "no");
        // title
        bookElement.addElement("title").setText(book.getTitle());
        // express
        bookElement.addElement("express").setText(book.getExpress());
    }

    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    StringWriter out = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(out, format);
    try {
        xmlWriter.write(document);
        xmlWriter.flush();
        String s = out.toString();
        System.out.println(s);
        Log.info("xml done!");
    } catch (Exception e) {
        Log.error("xml error!");
    } finally {
        try {
            if (null != xmlWriter) {
                xmlWriter.close();
            }
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.bullx.utils.I2Util.java

License:Open Source License

public static String prettyXML(Document document) {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");
    StringWriter out = new StringWriter();
    XMLWriter xmlWriter = new XMLWriter(out, format);
    try {//from   w  w  w.ja  va  2 s.c o  m
        xmlWriter.write(document);
        xmlWriter.flush();
        return out.toString();
    } catch (Exception e) {
        Log.error(e.getMessage());
    } finally {
        try {
            if (null != xmlWriter) {
                xmlWriter.close();
            }
            if (null != out) {
                out.close();
            }
        } catch (IOException e) {
            Log.error(e.getMessage());
        }
    }
    return null;
}

From source file:com.cc.framework.util.SettingUtils.java

License:Open Source License

/**
 * /*  w  w w  . j a v  a  2  s. co m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/shopxx/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.chingo247.structureapi.plan.document.AbstractDocumentManager.java

protected void save(K key, final V document) {
    documentPool.execute(key, new Runnable() {

        @Override// ww w . ja v  a2  s  .c  om
        public void run() {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setExpandEmptyElements(true);
            XMLWriter writer = null;
            try {
                writer = new XMLWriter(new FileWriter(document.documentFile), format);
                writer.write(document.document);
            } catch (IOException ex) {
                Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException ex) {
                        Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    });
}

From source file:com.chingo247.structureapi.plan.document.AbstractDocumentManager.java

protected void save(K key, final DocumentPluginElement element) {
    documentPool.execute(key, new Runnable() {

        @Override//from ww  w  .jav a2s .  c om
        public void run() {
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setExpandEmptyElements(true);
            XMLWriter writer = null;
            try {
                File d = element.root.documentFile;
                writer = new XMLWriter(new FileWriter(d), format);
                writer.write(element.pluginElement.getDocument());
            } catch (IOException ex) {
                Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException ex) {
                        Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    });
}

From source file:com.chingo247.structureapi.plan.io.export.StructurePlanExporter.java

License:Open Source License

public void export(IStructurePlan plan, File destinationDirectory, String fileName, boolean prettyPrint)
        throws IOException, UnsupportedPlacementException {
    Preconditions.checkArgument(destinationDirectory.isDirectory());

    IPlacement placement = plan.getPlacement();
    if (!(placement instanceof IExportablePlacement)) {
        throw new UnsupportedPlacementException("Placement does not implement IWriteablePlacement");
    }/*from ww  w .  ja v a  2s.  c om*/

    Document d = DocumentHelper.createDocument();

    Element root = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_ROOT_ELEMENT);
    d.add(root);

    Element nameElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_NAME_ELEMENT);
    nameElement.setText(plan.getName());
    root.add(nameElement);

    Element priceElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PRICE_ELEMENT);
    priceElement.setText(String.valueOf(plan.getPrice()));
    root.add(priceElement);

    Element categoryElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_CATEGORY_ELEMENT);
    categoryElement.setText(plan.getCategory() == null ? "None" : plan.getCategory());
    root.add(categoryElement);

    Element descriptionElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_DESCRIPTION_ELEMENT);
    descriptionElement.setText(plan.getDescription() == null ? "None" : plan.getDescription());
    root.add(descriptionElement);

    Element placementElement = PlacementAPI.getInstance().handle((IExportablePlacement) plan.getPlacement());
    root.add(placementElement);

    //        if (plan instanceof SubStructuresPlan) {
    //            SubStructuresPlan ssp = (SubStructuresPlan) plan;
    //            
    //            Element substructuresElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_SUBSTRUCTURES);
    //            root.add(substructuresElement);
    //            
    //            for(Placement p : ssp.getSubPlacements()) {
    //                try {
    //                    Element e = PlacementAPI.getInstance().handle(p);
    //                    e.setName(StructurePlanXMLConstants.STRUCTURE_PLAN_SUBSTRUCTURE);
    //                    substructuresElement.add(e);
    //                } catch (PlacementException ex) {
    //                    System.err.println(ex.getMessage());
    //                }
    //            }
    //            
    //            int index = 0;
    //            for(StructurePlan p : ssp.getSubStructurePlans()) {
    //                File exportPlan = new File(destinationDirectory, p.getFile().getName() + "-" + index);
    //                
    //                try {
    //                    export(plan, destinationDirectory, exportPlan.getName(), prettyPrint);
    //                } catch (Exception e){
    //                    continue;
    //                }
    //                
    //                Element substructureElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_SUBSTRUCTURE);
    //                
    //                // TODO add position + direction
    //                
    //                Element typeElement = new BaseElement(PlacementXMLConstants.PLACEMENT_TYPE_ELEMENT);
    //                typeElement.setText(PlacementTypes.EMBEDDED);
    //                substructureElement.add(typeElement);
    //                
    //                Element pathElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_RELATIVE_PATH_ELEMENT);
    //                pathElement.setText(exportPlan.getName());
    //                substructureElement.add(pathElement);
    //                
    //                substructuresElement.add(substructureElement);
    //                
    //            }
    //            
    //        }

    OutputFormat format;
    if (prettyPrint) {
        format = OutputFormat.createPrettyPrint();
    } else {
        format = OutputFormat.createCompactFormat();
    }
    XMLWriter writer = new XMLWriter(new FileWriter(new File(destinationDirectory, fileName)), format);
    writer.write(d);
    writer.close();

}