Example usage for org.jdom2.output XMLOutputter setFormat

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

Introduction

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

Prototype

public void setFormat(Format newFormat) 

Source Link

Document

Sets the new format logic for the XMLOutputter.

Usage

From source file:AL_gui.java

License:Apache License

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:
    try {/*  w ww  .ja va2 s  .c o  m*/
        String nnetName = JOptionPane.showInputDialog(jButton3, "Enter a filename, excluding extention.",
                "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
        if (nnetName == " ") {
            JOptionPane.showMessageDialog(null, "An input value must be entered.");
        }

        Element nnet = new Element("NNETWORK");
        nnet.setAttribute(new Attribute("noNamespaceSchemaLocation", "ANNeML.xsd",
                Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")));
        nnet.setAttribute(new Attribute("NNET_NAME", nnetName));
        Document doc = new Document(nnet);

        String subnnets = JOptionPane.showInputDialog(jButton3, "How many SUBNET(s)?", "ANNeML Wizard",
                JOptionPane.QUESTION_MESSAGE);
        if (subnnets == " ") {
            JOptionPane.showMessageDialog(null, "An input value must be entered.");
        }
        int numSubs = java.lang.Integer.parseInt(subnnets);
        int i = 0;
        do {
            Element subnet = new Element("SUBNET");
            String learningRate = JOptionPane.showInputDialog(jButton3, "SUBNET learning rate(eta)?",
                    "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
            if (learningRate == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            subnet.setAttribute(new Attribute("NNET_V2", learningRate));
            subnet.setAttribute(new Attribute("SNET_NAME", nnetName + "-subnet" + String.valueOf(i + 1)));
            subnet.setAttribute(new Attribute("ADJUST_LOCK", "0"));

            String input_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<INPUT>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (input_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numInLayers = java.lang.Integer.parseInt(input_layers);
            int x = 0;
            do {
                Element inLayer = new Element("LAYER");
                inLayer.setAttribute(new Attribute("LAYER_NAME", "INPUT"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                inLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String inNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<INPUT>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (inNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numInNodes = java.lang.Integer.parseInt(inNodes);
                int y = 0;
                do {
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "IN" + String.valueOf(x + 1) + String.valueOf(y + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", "0.0"));
                    node.setAttribute(new Attribute("CNAME", "Input node#" + String.valueOf(y + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    Element inSynapse = new Element("SYNAPSE");
                    inSynapse.setAttribute(new Attribute("WEIGHT", "1.00"));
                    inSynapse.setAttribute(new Attribute("ORG_NEURODE", "INPUT"));
                    node.addContent(inSynapse);
                    inLayer.addContent(node);
                    y++;
                } while (y < numInNodes);
                subnet.addContent(inLayer);
                x++;
            } while (x < numInLayers);

            String hidden_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<HIDDEN>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (hidden_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numHLayers = java.lang.Integer.parseInt(hidden_layers);
            int z = 0;
            do {
                Element hLayer = new Element("LAYER");
                hLayer.setAttribute(new Attribute("LAYER_NAME", "HIDDEN"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                hLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String hNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<HIDDEN>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (hNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numhNodes = java.lang.Integer.parseInt(hNodes);
                int a = 0;
                do {
                    Random rnd = new Random();
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "N" + String.valueOf(z + 1) + String.valueOf(a + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", getRandomValue(rnd, low, high, decpl)));
                    node.setAttribute(new Attribute("CNAME", "Hidden node#" + String.valueOf(a + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    hLayer.addContent(node);
                    a++;
                } while (a < numhNodes);
                subnet.addContent(hLayer);
                z++;
            } while (z < numHLayers);

            String output_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<OUTPUT>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (hidden_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numOLayers = java.lang.Integer.parseInt(output_layers);
            int b = 0;
            do {
                Element oLayer = new Element("LAYER");
                oLayer.setAttribute(new Attribute("LAYER_NAME", "OUTPUT"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                oLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String oNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<OUTPUT>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (oNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numoNodes = java.lang.Integer.parseInt(oNodes);
                int d = 0;
                do {
                    Random rnd = new Random();
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "ON" + String.valueOf(b + 1) + String.valueOf(d + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", getRandomValue(rnd, low, high, decpl)));
                    node.setAttribute(new Attribute("CNAME", "Output node#" + String.valueOf(d + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    oLayer.addContent(node);
                    d++;
                } while (d < numoNodes);
                subnet.addContent(oLayer);
                b++;
            } while (b < numOLayers);

            doc.getRootElement().addContent(subnet);
            i++;
        } while (i < numSubs);

        //generate fully interconnected SYNAPSE(s) for all NEURODE(s) within each SUBNET

        java.util.List subnets = XPath.newInstance("//SUBNET").selectNodes(doc);
        Iterator itSubslist = subnets.iterator();
        do {
            Element currentSnet = (Element) itSubslist.next();
            String snetName = currentSnet.getAttributeValue("SNET_NAME");
            //System.out.println(snetName);

            java.util.List Hnodes = XPath
                    .newInstance("//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                    .selectNodes(doc);
            Iterator itHNodelist = Hnodes.iterator();
            do {
                Element node = (Element) itHNodelist.next();
                //System.out.println(node.getAttributeValue("N_ID"));
                java.util.List Inodes = XPath
                        .newInstance(
                                "//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='INPUT']/NEURODE")
                        .selectNodes(doc);
                Iterator itNodelist = Inodes.iterator();
                do {
                    Element currentNode = (Element) itNodelist.next();
                    //System.out.println(currentNode.getAttributeValue("N_ID"));
                    Element hSynapse = new Element("SYNAPSE");
                    Random rnd = new Random();
                    hSynapse.setAttribute(new Attribute("WEIGHT", getRandomValue(rnd, low, high, decpl)));
                    hSynapse.setAttribute(new Attribute("ORG_NEURODE", currentNode.getAttributeValue("N_ID")));
                    node.addContent(hSynapse);
                } while (itNodelist.hasNext());
            } while (itHNodelist.hasNext());

            java.util.List Onodes = XPath
                    .newInstance("//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='OUTPUT']/NEURODE")
                    .selectNodes(doc);
            Iterator itONodelist = Onodes.iterator();
            do {
                Element node = (Element) itONodelist.next();
                //System.out.println(node.getAttributeValue("N_ID"));
                java.util.List hnodes = XPath
                        .newInstance(
                                "//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                        .selectNodes(doc);
                Iterator itNodelist = hnodes.iterator();
                do {
                    Element currentNode = (Element) itNodelist.next();
                    //System.out.println(currentNode.getAttributeValue("N_ID"));
                    Element hSynapse = new Element("SYNAPSE");
                    Random rnd = new Random();
                    hSynapse.setAttribute(new Attribute("WEIGHT", getRandomValue(rnd, low, high, decpl)));
                    hSynapse.setAttribute(new Attribute("ORG_NEURODE", currentNode.getAttributeValue("N_ID")));
                    node.addContent(hSynapse);
                } while (itNodelist.hasNext());
            } while (itONodelist.hasNext());

        } while (itSubslist.hasNext());

        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, System.out);
        xmlOutput.output(doc, new FileWriter(nnetName + ".xml"));

        System.out.println("File Saved!");

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:adlic.ADLic.java

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    try {/*w  w  w .  ja v  a 2s  .  c om*/
        int n = JOptionPane.showConfirmDialog(null,
                " ?     "
                        + licenses.getAttribute(numlic, "id") + " ?",
                " !", JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION) {
            licenses.getListLicense().remove(numlic);
            try {
                XMLOutputter outputter = new XMLOutputter();
                outputter.setFormat(Format.getPrettyFormat());
                try {
                    OutputStreamWriter out = new java.io.OutputStreamWriter(
                            new java.io.FileOutputStream(licenses.getFile()), "UTF-8");
                    out.write(outputter.outputString(licenses.getDocument()));
                    out.close();
                } catch (IOException ex) {
                    JOptionPane.showMessageDialog(rootPane, ex);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(rootPane, ex);
            }
            if (numlic > 0) {
                numlic -= 1;
            }
            this.getFormText(numlic);
            this.getTableKey(numlic);
            this.getTableSum(numlic);
        } else {
            System.out.println(" .");
        }
    } catch (NullPointerException ex) {
        JOptionPane.showMessageDialog(rootPane, ex);
    }
    this.getFormText(numlic);
    this.getTableKey(numlic);
    this.getTableSum(numlic);
}

From source file:adlic.licenses.java

/**
 * @see write xml file "licenses.xml"/*from w  w  w . j av  a  2s.  c  om*/
 */
public void writeLicenses(Document savingDocument, String filePath) {
    try {
        XMLOutputter outputter = new XMLOutputter();
        outputter.setFormat(Format.getPrettyFormat());
        try {
            OutputStreamWriter out = new java.io.OutputStreamWriter(new java.io.FileOutputStream(filePath),
                    "UTF-8");
            out.write(outputter.outputString(savingDocument));
            out.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex);
        }

        //outputter.output(savingDocument, new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:adlic.NewJDialog.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // TODO add your handling code here:
    try {/*from   ww  w  .j  a  v  a 2s  . co m*/
        licenses.getElementLicense(numlicsave).setAttribute("id", this.jTextField1.getText());
        licenses.getElementLicense(numlicsave).setAttribute("key", this.jTextField5.getText());
        licenses.getElementLicense(numlicsave).setAttribute("sum", this.jTextField6.getText());
        licenses.getElementLicense(numlicsave).getChild("product").setText(this.jTextField2.getText());
        licenses.getElementLicense(numlicsave).getChild("type").setText(this.jTextField3.getText());
        licenses.getElementLicense(numlicsave).getChild("numberlic").setText(this.jTextField4.getText());
        try {
            if (jModelKey.getRowCount() > (new Integer(this.jTextField5.getText()))) {
                JOptionPane.showMessageDialog(rootPane,
                        "?     ?  .");
            } else {
                licenses.getElementKey(numlicsave).removeContent();
                for (int i = 0; i < this.jModelKey.getRowCount(); i++) {
                    if ((this.jModelKey.getValueAt(i, 0) == null) && (this.jModelKey.getValueAt(i, 1) == null)
                            && (this.jModelKey.getValueAt(i, 2) == null)) {
                        JOptionPane.showMessageDialog(rootPane,
                                "? ?   .  ? ?.");
                    } else {
                        if (this.jModelKey.getValueAt(i, 0) == null) {
                            this.jModelKey.setValueAt(" ", i, 0);
                            JOptionPane.showMessageDialog(rootPane,
                                    "?  <?>   .  .");
                        }
                        if (this.jModelKey.getValueAt(i, 1) == null) {
                            this.jModelKey.setValueAt(" ", i, 1);
                            JOptionPane.showMessageDialog(rootPane,
                                    "?  <>   .  .");
                        }
                        if (this.jModelKey.getValueAt(i, 2) == null) {
                            this.jModelKey.setValueAt(" ", i, 2);
                            JOptionPane.showMessageDialog(rootPane,
                                    "?  <>   .  .");
                        }
                        licenses.getElementKey(numlicsave).addContent(new Element("product-key")
                                .addContent(this.jModelKey.getValueAt(i, 2).toString()));
                        licenses.getElementProductKey(numlicsave, i).setAttribute("name",
                                this.jModelKey.getValueAt(i, 0).toString());
                        licenses.getElementProductKey(numlicsave, i).setAttribute("type",
                                this.jModelKey.getValueAt(i, 1).toString());
                    }
                }
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        try {
            if (jModelSum.getRowCount() > (new Integer(this.jTextField6.getText()))) {
                JOptionPane.showMessageDialog(rootPane,
                        "?   ?  .");
            } else {
                licenses.getElementSum(numlicsave).removeContent();
                for (int i = 0; i < this.jModelSum.getRowCount(); i++) {
                    if ((this.jModelSum.getValueAt(i, 0) == null)
                            && (this.jModelSum.getValueAt(i, 1) == null)) {
                        JOptionPane.showMessageDialog(rootPane,
                                "? ?   .  ? ?.");
                    } else {
                        if (this.jModelSum.getValueAt(i, 0) == null) {
                            this.jModelSum.setValueAt(" ", i, 0);
                            JOptionPane.showMessageDialog(rootPane,
                                    "?  <? ??>   .  .");
                        }
                        if (this.jModelSum.getValueAt(i, 1) == null) {
                            this.jModelSum.setValueAt(" ", i, 1);
                            JOptionPane.showMessageDialog(rootPane,
                                    "?  < >   .  .");
                        }
                        licenses.getElementSum(numlicsave).addContent(new Element("invent-number")
                                .addContent(this.jModelSum.getValueAt(i, 1).toString()));
                        licenses.getElementInventSum(numlicsave, i).setAttribute("type",
                                this.jModelSum.getValueAt(i, 0).toString());
                    }
                }
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        try {
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            try {
                OutputStreamWriter out = new java.io.OutputStreamWriter(
                        new java.io.FileOutputStream(licenses.getFile()), "UTF-8");
                out.write(outputter.outputString(licenses.getDocument()));
                out.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(rootPane, ex);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        this.jLabel5.setText("? ?.");
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(rootPane, ex);
    }
}

From source file:adlic.NewJDialogInvent.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    try {//from  w  w w  . ja v a2s .  c  o m
        this.jLabel1.setText("...");
        licenses.getElementInvent().removeContent();
        try {
            for (int i = 0; i < jModelInvent.getRowCount(); i++) {
                if (this.jModelInvent.getValueAt(i, 0) == null) {
                    jLabel1.setText(
                            "! ? ?  ??  ????.");
                } else {
                    licenses.getElementInvent().addContent(
                            new Element("type").addContent(jModelInvent.getValueAt(i, 0).toString()));
                }
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        try {
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            try {
                OutputStreamWriter out = new java.io.OutputStreamWriter(
                        new java.io.FileOutputStream(licenses.getFile()), "UTF-8");
                out.write(outputter.outputString(licenses.getDocument()));
                out.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(rootPane, ex);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(rootPane, ex);
    }
}

From source file:adlic.NewJDialogKeyType.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    try {//from  w  w w . j a va 2  s  . c  o m
        this.jLabel1.setText("...");
        licenses.getElementKeyType().removeContent();
        try {
            for (int i = 0; i < this.jModelType.getRowCount(); i++) {
                if (this.jTable1.getValueAt(i, 0) == null) {
                    this.jLabel1.setText(
                            "! ? ?  ??  ????");
                } else {
                    licenses.getElementKeyType().addContent(
                            new Element("type").addContent(this.jModelType.getValueAt(i, 0).toString()));
                }
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        try {
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            try {
                OutputStreamWriter out = new java.io.OutputStreamWriter(
                        new java.io.FileOutputStream(licenses.getFile()), "UTF-8");
                out.write(outputter.outputString(licenses.getDocument()));
                out.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(rootPane, ex);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(rootPane, ex);
    }
}

From source file:adlic.NewJDialogProduct.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    try {/* w  w w  . jav a2 s . c o  m*/
        this.jLabel1.setText("...");
        licenses.getElementProduct().removeContent();
        try {
            for (int i = 0; i < this.jModelProduct.getRowCount(); i++) {
                if (this.jTable1.getValueAt(i, 0) == null) {
                    this.jLabel1.setText(
                            "! ? ?  ??  ????");
                } else {
                    licenses.getElementProduct().addContent(
                            new Element("name").addContent(this.jModelProduct.getValueAt(i, 0).toString()));
                }
            }
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
        try {
            XMLOutputter outputter = new XMLOutputter();
            outputter.setFormat(Format.getPrettyFormat());
            try {
                OutputStreamWriter out = new java.io.OutputStreamWriter(
                        new java.io.FileOutputStream(licenses.getFile()), "UTF-8");
                out.write(outputter.outputString(licenses.getDocument()));
                out.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(rootPane, ex);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(rootPane, ex);
        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(rootPane, ex);
    }
}

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public String getOuterXml() {
    XMLOutputter outp = new XMLOutputter();
    outp.setFormat(Format.getCompactFormat());
    String returnString = "";
    try (StringWriter sw = new StringWriter()) {
        if (_element instanceof Element)
            outp.output((Element) _element, sw);
        else {//from  ww w .j a  va2s.c  o  m
            // TODO: What to do for other types of Content?
            // Note: Document is not derived from Content.
        }
        returnString = sw.getBuffer().toString();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return returnString;
}

From source file:AIR.Common.xml.XmlElement.java

License:Open Source License

public String getInnerXml() {
    XMLOutputter outp = new XMLOutputter();

    outp.setFormat(Format.getCompactFormat());
    try (StringWriter sw = new StringWriter()) {
        if (_element instanceof Element)
            outp.outputElementContent((Element) _element, sw);
        else {/*from   w  ww.j a v  a  2 s  .com*/
            // TODO: What to do for other types of Content?
        }

        return sw.toString();
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new XmlReaderException(e1);
    }
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.FormulaConverter.java

License:Open Source License

/**
 * Replaces all formulas with the html representation of the mapped formula objects
 *
 * @param doc        JDOM Document where to replace the formulas
 * @param formulaMap Map of the indexed Formula Objects
 * @return JDOM Document with replaced formulas
 *//*w w  w  .ja  va 2  s  .  c  om*/
public Document replaceFormulas(Document doc, Map<Integer, Formula> formulaMap) {
    List<Element> foundFormulas = xpath.evaluate(doc);

    if (foundFormulas.size() > 0) {
        Map<String, Element> formulaMarkupMap = new HashMap<>();

        // Initialize markup map
        for (Element element : foundFormulas) {
            formulaMarkupMap.put(element.getAttribute("id").getValue(), element);
        }

        // Replace all found formulas
        Iterator<Integer> formulaIterator = formulaMap.keySet().iterator();
        while (formulaIterator.hasNext()) {
            Integer id = formulaIterator.next();

            Element formulaMarkupRoot = formulaMarkupMap.get(FORMULA_ID_PREFIX + id);
            Formula formula = formulaMap.get(id);

            formulaMarkupRoot.removeAttribute("class");
            formulaMarkupRoot.removeContent();
            formulaMarkupRoot.setName("div");

            Element div = (Element) formulaMarkupRoot.getParent();
            div.setName("div");
            div.setAttribute("class", "formula");

            // Potentially there's text inside the paragraph...
            List<Text> texts = div.getContent(Filters.textOnly());
            if (texts.isEmpty() == false) {
                String textString = "";
                for (Text text : texts) {
                    textString += text.getText();
                }
                Element textSpan = new Element("span");
                textSpan.setAttribute("class", "text");
                textSpan.setText(textString);
                div.addContent(textSpan);

                List<Content> content = div.getContent();
                content.removeAll(texts);
            }

            if (generateDebugMarkup) {
                div.setAttribute("style", "border: 1px solid black;");

                // Header
                Element h4 = new Element("h4");
                h4.setText("DEBUG - Formula #" + formula.getId());
                div.addContent(h4);

                // Render LaTeX source
                Element latexPre = new Element("pre");
                latexPre.setAttribute("class", "debug-latex");
                latexPre.setText(formula.getLatexCode());
                div.addContent(latexPre);

                // Render MathML markup
                Element mathmlPre = new Element("pre");
                mathmlPre.setAttribute("class", "debug-mathml");
                mathmlPre.setText(formula.getMathMl());
                div.addContent(mathmlPre);

                // Render HTML Markup
                Element htmlPre = new Element("pre");
                htmlPre.setAttribute("class", "debug-html");
                XMLOutputter xmlOutputter = new XMLOutputter();
                xmlOutputter.setFormat(Format.getRawFormat());
                htmlPre.setText(xmlOutputter.outputString(formula.getHtml()));

                div.addContent(htmlPre);

            }

            // Set formula into
            formulaMarkupRoot.addContent(formula.getHtml());
        }
    }
    return doc;
}