Example usage for org.jdom2.output XMLOutputter XMLOutputter

List of usage examples for org.jdom2.output XMLOutputter XMLOutputter

Introduction

In this page you can find the example usage for org.jdom2.output XMLOutputter XMLOutputter.

Prototype

public XMLOutputter() 

Source Link

Document

This will create an XMLOutputter with a default Format and XMLOutputProcessor .

Usage

From source file:myjdom.PrettyPrinter.java

public static void main(String[] args) {
    try {/* ww w  .  jav a  2s  .c om*/
        // Build the document with SAXBuilder of JDOM, 
        SAXBuilder builder = new SAXBuilder();
        //builder.setFeature("http://xml.org/sax/features/external-general-entities", false);//NOT WORKING
        //builder.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);//NOT WORKING
        //builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);// WORKING
        // Create the document
        Document doc = builder.build(PrettyPrinter.class.getResourceAsStream(Utils.INTERNAL_XML_LOCATION));//(new File(filename));
        // Output the document, use standard formatter
        XMLOutputter fmt = new XMLOutputter();
        fmt.output(doc, System.out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:negocio.Metodos.java

public void escribirXML(Document doc) {
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    try {//from w ww .ja v a 2  s  . c  om
        /*escribimos el documento en el file*/
        xmlOutput.output(doc, new FileWriter(rutaJDOM));
    } catch (IOException ex) {
        System.out.println("Error al escribir el documento");
    }
}

From source file:neon.editor.tools.SVGConverter.java

License:Open Source License

private static void save(Element shapes, int width, int height) throws IOException {
    Element root = new Element("map");
    root.setAttribute("id", "aneirin");
    root.setAttribute("name", "Aneirin");
    root.setAttribute("uid", "1");
    root.setAttribute("module", "aneirin");

    Element size = new Element("size");
    size.setAttribute("width", Integer.toString(width));
    size.setAttribute("height", Integer.toString(height));
    root.addContent(size);/*from   w w w  .j  a va2s  .c  o  m*/

    int dx = 0;
    int dy = 13700;

    Element terrain = new Element("terrain");
    root.addContent(terrain);

    Element base = new Element("region");
    base.setAttribute("x", "0");
    base.setAttribute("y", "0");
    base.setAttribute("w", Integer.toString(width));
    base.setAttribute("h", Integer.toString(height));
    base.setAttribute("id", "sea");
    terrain.addContent(base);

    for (Element rect : shapes.getChildren("rect", ns)) {
        int x = Integer.parseInt(rect.getAttributeValue("x")) + dx;
        int y = Integer.parseInt(rect.getAttributeValue("y")) + dy;
        int w = Integer.parseInt(rect.getAttributeValue("width"));
        int h = Integer.parseInt(rect.getAttributeValue("height"));

        String id = "grass";
        String style = rect.getAttributeValue("style");

        if (style.contains("#ffff00")) {
            id = "sand";
        } else if (style.contains("#ff0000")) {
            id = "mud";
        } else if (style.contains("#550000")) {
            id = "rock";
        } else if (style.contains("#22cc51")) {
            id = "moss";
        } else if (style.contains("#008080")) {
            id = "marsh";
        } else if (style.contains("#bcff00")) {
            id = "meadow";
        }

        Element region = new Element("region");
        region.setAttribute("x", Integer.toString(x));
        region.setAttribute("y", Integer.toString(y));
        region.setAttribute("w", Integer.toString(w));
        region.setAttribute("h", Integer.toString(h));
        region.setAttribute("id", id);
        terrain.addContent(region);
    }

    Element elevation = new Element("elevation");
    root.addContent(elevation);

    Element entities = new Element("entities");
    root.addContent(entities);

    XMLOutputter outputter = new XMLOutputter();
    outputter.setFormat(Format.getPrettyFormat());
    outputter.output(new Document(root), new FileWriter("temp/map.xml"));
}

From source file:net.alegen.datpass.library.configure.XMLConfigurator.java

License:Open Source License

public XMLConfigurator(String configFileName) throws IOException, JDOMException {
    try {//  ww  w  .jav a  2 s.c  om
        this.configFileName = configFileName;
        File configFile = new File(configFileName);
        if (!configFile.exists()) {
            this.root = new Element(XmlConstants.DATPASS);
            Document doc = new Document(this.root);
            XMLOutputter xmlOutput = new XMLOutputter();
            xmlOutput.setFormat(Format.getPrettyFormat());
            FileWriter fileWriter = new FileWriter(configFileName);
            xmlOutput.output(doc, fileWriter);
            fileWriter.flush();
            fileWriter.close();
        } else
            this.root = new SAXBuilder().build(configFile).getRootElement();
    } catch (JDOMException e) {
        log.error("The XML configuration file is not valid. Please check for syntax errors.");
        throw e;
    } catch (IOException e) {
        log.error("There was a problem when trying to read the XML configuration file.");
        throw e;
    }
}

From source file:net.alegen.datpass.library.configure.XMLConfigurator.java

License:Open Source License

@Override
public boolean saveConfig() {
    XMLOutputter xmlOutput = new XMLOutputter();
    Format format = Format.getPrettyFormat();
    format.setIndent("    ");
    xmlOutput.setFormat(format);//from w ww.j a  va 2 s.co m
    try {
        xmlOutput.output(this.root.getDocument(), new FileOutputStream(configFileName));
        return true;
    } catch (IOException e) {
        log.error("An IO exception occured while saving data.");
        return false;
    }
}

From source file:net.alegen.easyhash.utils.Settings.java

License:Open Source License

public void save() {
    Element elemSettings = new Element("settings");
    Document doc = new Document(elemSettings);

    Element elemAlgorithm = new Element("algorithm");
    elemAlgorithm.addContent(this.algorithm);
    doc.getRootElement().addContent(elemAlgorithm);

    Element elemEcho = new Element("echo");
    elemEcho.addContent(String.valueOf(this.echo));
    doc.getRootElement().addContent(elemEcho);

    Element elemToClipboard = new Element("to-clipboard");
    elemToClipboard.addContent(String.valueOf(this.tocb));
    doc.getRootElement().addContent(elemToClipboard);

    Element elemCaps = new Element("caps");
    elemCaps.addContent(String.valueOf(this.caps));
    doc.getRootElement().addContent(elemCaps);

    Element elemChain = new Element("chain");
    elemChain.addContent(String.valueOf(this.chain));
    doc.getRootElement().addContent(elemChain);

    Element elemResetTime = new Element("reset-time");
    elemResetTime.addContent(String.valueOf(this.resetTime));
    doc.getRootElement().addContent(elemResetTime);

    Element elemSalt = new Element("salt");
    elemSalt.addContent(this.salt);
    doc.getRootElement().addContent(elemSalt);

    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    try {/* w  w w. ja va2  s .  c  om*/
        xmlOutput.output(doc, new FileWriter("settings.eh.xml"));
    } catch (IOException ex) {
        System.out.println("eh > Could not save settings - " + ex.getMessage());
    }
}

From source file:net.instantcom.mm7.MM7Error.java

License:Open Source License

@Override
public void load(Element element) {

    Element body = element.getChild("Body", MM7Message.ENVELOPE);
    Element e = (Element) body.getChildren().get(0);

    this.faultCode = e.getChildTextTrim("faultcode");
    this.faultMessage = e.getChildTextTrim("faultstring");
    try {//from w  w w  .ja  v  a 2s.c o m
        Element detail;
        if (element.getNamespace("") != null) {
            detail = (Element) e.getChild("detail", element.getNamespace("")).getChildren().get(0);
        } else {
            detail = (Element) e.getChild("detail").getChildren().get(0);
        }
        String message = detail.getName();
        // Instantiate correct status type

        Class<?> clazz = Class.forName("net.instantcom.mm7." + message);
        this.response = (MM7Response) clazz.newInstance();
        this.response.load(element);
    } catch (Throwable t) {
        // Ignored
        XMLOutputter outp = new XMLOutputter();
        String s = outp.outputString(element);
        System.err.println("Failed to instantiate a correct response type" + s);
        t.printStackTrace();
    }
}

From source file:net.instantcom.mm7.MM7Message.java

License:Open Source License

public static void save(MM7Message mm7, OutputStream out, MM7Context ctx) throws IOException {
    // Setup MM7 version and XML name space if not already set
    if (mm7.getMm7Version() == null) {
        mm7.setMm7Version(ctx.getMm7Version());
    }//from ww  w.j a  v a2s .  co  m
    if (mm7.getNamespace() == null) {
        mm7.setNamespace(ctx.getMm7Namespace());
    }

    final XMLOutputter xo = new XMLOutputter();
    xo.setFormat(ctx.getJdomFormat());
    final Writer w = new OutputStreamWriter(out, "utf-8");

    if (mm7.isMultipart()) {
        final Content content = ((HasContent) mm7).getContent();

        final String boundary = mm7.getSoapBoundary();
        w.write("--");
        w.write(boundary);
        w.write("\r\nContent-Type: text/xml; charset=\"utf-8\"\r\nContent-Transfer-Encoding: binary\r\nContent-ID: <");
        w.write(mm7.getSoapContentId());
        w.write(">\r\n\r\n");
        w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");

        xo.output(mm7.toSOAP(ctx), w);

        w.write("\r\n--");
        w.write(boundary);
        w.flush();

        content.writeTo(out, "mm7-content", ctx);
        w.write("\r\n--");
        w.write(boundary);
        w.write("--");
    } else {
        xo.output(mm7.toSOAP(ctx), w);
    }
    w.flush();
}

From source file:net.instantcom.mm7.SoapContent.java

License:Open Source License

@Override
public void writeTo(OutputStream out, String contentId, MM7Context ctx) throws IOException {
    new XMLOutputter().output(doc, out);
}

From source file:nl.b3p.imro.harvester.processing.GeometryConverter.java

License:Open Source License

private Geometry convertGeometry(Element el)
        throws IOException, SAXException, ParserConfigurationException, TransformerException {
    if (!(el instanceof org.w3c.dom.Element)) {
        throw new IllegalArgumentException("gml org.w3c.node is not an org.w3c.Element");
    }/*from  w  w w  .j  a  v a  2s  . co m*/
    // TODO: maybe convert node directly to a source / inputsource / reader / stream for parser.
    // instead of JDOM detour.
    org.jdom2.Element elem = new DOMBuilder().build((org.w3c.dom.Element) el);
    String gmlString = new XMLOutputter().outputString(elem);
    gmlString = gmlString.replaceAll("gml:", "");
    //tySstem.out.println("gmlString:" + gmlString);
    //   parser.getNamespaces().declarePrefix("gml", "http://www.opengis.net/gml/3.2");
    Object parsedObject = parser.parse(new StringReader(gmlString));
    if (parsedObject instanceof Geometry) {
        Geometry geom = (Geometry) parsedObject;
        if (!geom.isValid()) {
            geom = geom.buffer(0.0);
            log.debug("Geometry is invalid. Made valid by buffering with 0");
        }
        // arcs can have nodes that are on the same point (28992; 3 digit precision): simplify
        Geometry simplGeom = DouglasPeuckerSimplifier.simplify(geom, DISTANCE_TOLERANCE);
        return simplGeom;
    } else {
        throw new InvalidClassException(parsedObject.getClass().getCanonicalName(),
                "Parsed object not of class Geometry.");
    }

}