Example usage for org.dom4j.io XMLWriter close

List of usage examples for org.dom4j.io XMLWriter close

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes the underlying Writer

Usage

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

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

        @Override/*from   w w w .j  av a 2  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.j a  v a2  s .co  m*/
        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.document.PlanDocumentGenerator.java

License:Open Source License

public void generate(File targetFolder) {
    // Scan the folder called 'SchematicToPlan' for schematic files
    Iterator<File> it = FileUtils.iterateFiles(targetFolder, new String[] { "schematic" }, true);
    System.out.println("Files: " + targetFolder.listFiles().length);

    int count = 0;
    long start = System.currentTimeMillis();

    // Generate Plans
    while (it.hasNext()) {
        File schematic = it.next();

        Document d = DocumentHelper.createDocument();
        d.addElement(Elements.ROOT).addElement(Elements.SETTLERCRAFT).addElement(Elements.SCHEMATIC)
                .setText(schematic.getName());

        File plan = new File(schematic.getParent(), FilenameUtils.getBaseName(schematic.getName()) + ".xml");

        try {/*  w ww  .  j av  a 2 s.com*/

            XMLWriter writer = new XMLWriter(new FileWriter(plan));
            writer.write(d);
            writer.close();

            StructurePlan sp = new StructurePlan();
            PlanDocument pd = new PlanDocument(structureAPI.getPlanDocumentManager(), plan);
            pd.putPluginElement("SettlerCraft", new PlanDocumentPluginElement("SettlerCraft", pd,
                    (Element) d.selectSingleNode("StructurePlan/SettlerCraft")));
            sp.load(pd);

            if (sp.getCategory().equals("Default")
                    && !schematic.getParentFile().getName().equals(targetFolder.getName())) {
                sp.setCategory(schematic.getParentFile().getName());
            }

            sp.save();

        } catch (DocumentException ex) {
            Logger.getLogger(StructurePlanManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException | StructureDataException ex) {
            Logger.getLogger(StructurePlanManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        count++;
    }
    if (count > 0) {
        StructureAPI.print("Generated " + count + " plans in " + (System.currentTimeMillis() - start) + "ms");
    }
}

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   w  w w.  java 2  s  .co m

    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();

}

From source file:com.chingo247.structureapi.plan.PlanGenerator.java

License:Open Source License

private static void generatePlanFromSchematic(File file, File rootDirectory) throws IOException {
    String name = FilenameUtils.getBaseName(file.getName());
    File directory = file.getParentFile();

    Document d = DocumentHelper.createDocument();
    Element root = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_ROOT_ELEMENT);
    d.add(root);/* w  w  w .j  av a 2s.c  o m*/

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

    Element priceElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PRICE_ELEMENT);
    priceElement.setText("0");
    root.add(priceElement);

    Element description = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_DESCRIPTION_ELEMENT);
    description.setText("None");
    root.add(description);

    Element categoryElement = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_CATEGORY_ELEMENT);
    String category = rootDirectory.getName().equals(directory.getName()) ? "Default" : directory.getName();
    categoryElement.setText(category);
    root.add(categoryElement);

    Element placementElment = new BaseElement(StructurePlanXMLConstants.STRUCTURE_PLAN_PLACEMENT);

    Element typeElement = new BaseElement(PlacementXMLConstants.TYPE_ELEMENT);
    typeElement.setText(PlacementTypes.SCHEMATIC);

    Element schematicElement = new BaseElement(PlacementXMLConstants.SCHEMATIC_ELEMENT);
    schematicElement.setText(file.getName());

    placementElment.add(typeElement);
    placementElment.add(schematicElement);

    root.add(placementElment);

    File planFile = new File(directory, name + ".xml");
    OutputFormat format = OutputFormat.createPrettyPrint();
    XMLWriter writer = new XMLWriter(new FileWriter(planFile), format);
    writer.write(d);
    writer.close();
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

public String Dom2String(Document doc) {

    XMLWriter writer = null;
    try {//  ww  w . ja  v  a2s.c o m
        StringWriter sw = new StringWriter();
        // OutputFormat format = OutputFormat.createPrettyPrint();
        // format.setEncoding("gb2312");

        writer = new XMLWriter(sw);
        writer.write(doc);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
    return null;
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

public String Dom2String(Document doc, String encoding) {

    XMLWriter writer = null;
    try {//from   www .  j  a  va2 s . c  o m
        StringWriter sw = new StringWriter();
        writer = new XMLWriter(sw);
        writer.write(doc);
        String xml = sw.toString();

        String pattern = ">";
        int idx = xml.indexOf(pattern);
        String last = xml.substring(idx + 1);
        xml = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>" + last;
        return xml;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
    return null;
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

public void saveXml2File(Document doc, File file) {
    XMLWriter writer = null;
    try {//from www  .j a va2s  . co m
        FileWriter fw = new FileWriter(file);
        // ??xml?????XML
        // OutputFormat format = OutputFormat.createCompactFormat();
        // format.setEncoding("UTF-8");

        writer = new XMLWriter(fw);
        writer.write(doc);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
}

From source file:com.cnd.greencube.server.util.dom4j.XmlUtils.java

License:Open Source License

/**
 * @author lizhi ?xml?//from w w w .  ja va2 s  . com
 * @param doc
 * @param file
 * @param encoding
 */
public void saveXml2File(Document doc, File file, String encoding) {
    XMLWriter writer = null;
    try {
        String sXMLContent = Dom2String(doc, "GBK");
        FileWriter filer = new FileWriter(file);
        filer.write(sXMLContent);
        filer.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != writer)
            try {
                writer.close();
            } catch (Exception ie) {
            }
    }
}

From source file:com.dockingsoftware.dockingpreference.PreferenceProcessor.java

License:Apache License

/**
 * Store user preference information to a specified file.
 * // w w w . j av a 2  s .  c om
 * @param obj 
 * @param fullPath Specified the full path of the configuration file.
 */
public void store(Object obj, String fullPath) {
    try {
        FileWriter out;
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement(getName(obj.getClass()));
        root.addAttribute(GlobalConstant.CLASS, obj.getClass().getName());

        List<Field> pFields = getPreferenceFieldList(obj.getClass());
        for (int i = 0; i < pFields.size(); i++) {
            store(obj, pFields.get(i), root);
        }

        OutputFormat format = new OutputFormat("  ", true);
        out = new FileWriter(new File(fullPath));
        XMLWriter w = new XMLWriter(out, format);
        w.write(document);
        w.close();
        out.close();

    } catch (IOException ex) {
        Logger.getLogger(PreferenceProcessor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalArgumentException ex) {
        Logger.getLogger(PreferenceProcessor.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        Logger.getLogger(PreferenceProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
}