Search in sources :

Example 21 with Format

use of org.jdom.output.Format in project intellij-community by JetBrains.

the class MavenEffectivePomDumper method addMavenNamespace.

/**
   * Copy/pasted from org.apache.maven.plugins.help.AbstractEffectiveMojo
   */
protected static String addMavenNamespace(String effectiveXml, boolean isPom) {
    SAXBuilder builder = new SAXBuilder();
    try {
        Document document = builder.build(new StringReader(effectiveXml));
        Element rootElement = document.getRootElement();
        // added namespaces
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/4.0.0");
        rootElement.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        rootElement.addNamespaceDeclaration(xsiNamespace);
        if (rootElement.getAttribute("schemaLocation", xsiNamespace) == null) {
            rootElement.setAttribute("schemaLocation", "http://maven.apache.org/POM/4.0.0 " + (isPom ? POM_XSD_URL : SETTINGS_XSD_URL), xsiNamespace);
        }
        ElementFilter elementFilter = new ElementFilter(Namespace.getNamespace(""));
        for (Iterator i = rootElement.getDescendants(elementFilter); i.hasNext(); ) {
            Element e = (Element) i.next();
            e.setNamespace(pomNamespace);
        }
        addLineBreaks(document, pomNamespace);
        StringWriter w = new StringWriter();
        Format format = Format.getRawFormat();
        XMLOutputter out = new XMLOutputter(format);
        out.output(document.getRootElement(), w);
        return w.toString();
    } catch (JDOMException e) {
        return effectiveXml;
    } catch (IOException e) {
        return effectiveXml;
    }
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) Format(org.jdom.output.Format) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ElementFilter(org.jdom.filter.ElementFilter)

Example 22 with Format

use of org.jdom.output.Format in project felix by apache.

the class MavenJDOMWriter method write.

// -- void write(Model, Document, OutputStream)
/**
 * Method write
 *
 * @param project
 * @param writer
 * @param document
 */
public void write(Model project, Document document, OutputStreamWriter writer) throws java.io.IOException {
    Format format = Format.getRawFormat();
    format.setEncoding(writer.getEncoding()).setLineSeparator(System.getProperty("line.separator"));
    write(project, document, writer, format);
}
Also used : Format(org.jdom.output.Format)

Example 23 with Format

use of org.jdom.output.Format in project felix by apache.

the class MavenJDOMWriter method write.

// -- void updateSite(Site, String, Counter, Element)
/**
 * Method write
 *
 * @param project
 * @param stream
 * @param document
 * @deprecated
 */
public void write(Model project, Document document, OutputStream stream) throws java.io.IOException {
    updateModel(project, "project", new Counter(0), document.getRootElement());
    XMLOutputter outputter = new XMLOutputter();
    Format format = Format.getPrettyFormat();
    format.setIndent("    ").setLineSeparator(System.getProperty("line.separator"));
    outputter.setFormat(format);
    outputter.output(document, stream);
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) Format(org.jdom.output.Format)

Example 24 with Format

use of org.jdom.output.Format in project felix by apache.

the class PomWriter method write.

public static void write(Writer w, Model newModel, boolean namespaceDeclaration) throws IOException {
    Element root = new Element("project");
    if (namespaceDeclaration) {
        String modelVersion = newModel.getModelVersion();
        Namespace pomNamespace = Namespace.getNamespace("", "http://maven.apache.org/POM/" + modelVersion);
        root.setNamespace(pomNamespace);
        Namespace xsiNamespace = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.addNamespaceDeclaration(xsiNamespace);
        if (root.getAttribute("schemaLocation", xsiNamespace) == null) {
            root.setAttribute("schemaLocation", "http://maven.apache.org/POM/" + modelVersion + " http://maven.apache.org/maven-v" + modelVersion.replace('.', '_') + ".xsd", xsiNamespace);
        }
    }
    Document doc = new Document(root);
    MavenJDOMWriter writer = new MavenJDOMWriter();
    String encoding = newModel.getModelEncoding() != null ? newModel.getModelEncoding() : "UTF-8";
    Format format = Format.getPrettyFormat().setEncoding(encoding);
    writer.write(newModel, doc, w, format);
}
Also used : Format(org.jdom.output.Format) Element(org.jdom.Element) Document(org.jdom.Document) Namespace(org.jdom.Namespace)

Example 25 with Format

use of org.jdom.output.Format in project dna by leifeld.

the class Exporter method exportGraphml.

/**
 * Export filter for graphML files.
 *
 * @param matrix                 Input {@link Matrix}.
 * @param twoMode                Indicates whether the network is a two-mode network.
 * @param statementType          The statement type on which the network is based.
 * @param outfile                Name of the output file.
 * @param var1                   Name of the first variable (the rows of the matrix).
 * @param var2                   Name of the second variable (the columns of the matrix).
 * @param frequencies1           The number of statements in which the row node is involved (after filtering).
 * @param frequencies2           The number of statements in which the column node is involved (after filtering).
 * @param attributes             An ArrayList of {@link AttributeVector}s containing all attribute vectors in the database.
 * @param qualifierAggregation   A String denoting the qualifier aggregation. Valid values are "ignore", "combine", "subtract", "congruence", and "conflict".
 * @param qualifierBinary        Indicates whether the qualifier is a binary variable.
 */
private void exportGraphml(Matrix mt, boolean twoMode, StatementType statementType, String outfile, String var1, String var2, int[] frequencies1, int[] frequencies2, ArrayList<AttributeVector> attributes, String qualifierAggregation, boolean qualifierBinary) {
    // extract attributes
    String[] rn = mt.getRownames();
    String[] cn = mt.getColnames();
    String[] names;
    String[] variables;
    int[] frequencies;
    if (twoMode == true) {
        names = new String[rn.length + cn.length];
        variables = new String[names.length];
        frequencies = new int[names.length];
    } else {
        names = new String[rn.length];
        variables = new String[rn.length];
        frequencies = new int[rn.length];
    }
    for (int i = 0; i < rn.length; i++) {
        names[i] = rn[i];
        variables[i] = var1;
        frequencies[i] = frequencies1[i];
    }
    if (twoMode == true) {
        for (int i = 0; i < cn.length; i++) {
            names[i + rn.length] = cn[i];
            variables[i + rn.length] = var2;
            frequencies[i + rn.length] = frequencies2[i];
        }
    }
    int[] id = new int[names.length];
    String[] color = new String[names.length];
    String[] type = new String[names.length];
    String[] alias = new String[names.length];
    String[] notes = new String[names.length];
    for (int i = 0; i < attributes.size(); i++) {
        if (attributes.get(i).getStatementTypeId() == statementType.getId() && attributes.get(i).getVariable().equals(var1)) {
            for (int j = 0; j < rn.length; j++) {
                if (rn[j].equals(attributes.get(i).getValue())) {
                    id[j] = attributes.get(i).getId();
                    color[j] = String.format("#%02X%02X%02X", attributes.get(i).getColor().getRed(), attributes.get(i).getColor().getGreen(), attributes.get(i).getColor().getBlue());
                    type[j] = attributes.get(i).getType();
                    alias[j] = attributes.get(i).getAlias();
                    notes[j] = attributes.get(i).getNotes();
                }
            }
        } else if (attributes.get(i).getStatementTypeId() == statementType.getId() && attributes.get(i).getVariable().equals(var2) && twoMode == true) {
            for (int j = 0; j < cn.length; j++) {
                if (cn[j].equals(attributes.get(i).getValue())) {
                    id[j + rn.length] = attributes.get(i).getId();
                    color[j + rn.length] = String.format("#%02X%02X%02X", attributes.get(i).getColor().getRed(), attributes.get(i).getColor().getGreen(), attributes.get(i).getColor().getBlue());
                    type[j + rn.length] = attributes.get(i).getType();
                    alias[j + rn.length] = attributes.get(i).getAlias();
                    notes[j + rn.length] = attributes.get(i).getNotes();
                }
            }
        }
    }
    // set up graph structure
    Namespace xmlns = Namespace.getNamespace("http://graphml.graphdrawing.org/xmlns");
    Element graphml = new Element("graphml", xmlns);
    Namespace visone = Namespace.getNamespace("visone", "http://visone.info/xmlns");
    graphml.addNamespaceDeclaration(visone);
    Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    graphml.addNamespaceDeclaration(xsi);
    Namespace yNs = Namespace.getNamespace("y", "http://www.yworks.com/xml/graphml");
    graphml.addNamespaceDeclaration(yNs);
    Attribute attSchema = new Attribute("schemaLocation", "http://graphml.graphdrawing.org/xmlns/graphml http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd ", xsi);
    graphml.setAttribute(attSchema);
    org.jdom.Document document = new org.jdom.Document(graphml);
    Comment dataSchema = new Comment(" data schema ");
    graphml.addContent(dataSchema);
    Element keyVisoneNode = new Element("key", xmlns);
    keyVisoneNode.setAttribute(new Attribute("for", "node"));
    keyVisoneNode.setAttribute(new Attribute("id", "d0"));
    keyVisoneNode.setAttribute(new Attribute("yfiles.type", "nodegraphics"));
    graphml.addContent(keyVisoneNode);
    Element keyVisoneEdge = new Element("key", xmlns);
    keyVisoneEdge.setAttribute(new Attribute("for", "edge"));
    keyVisoneEdge.setAttribute(new Attribute("id", "e0"));
    keyVisoneEdge.setAttribute(new Attribute("yfiles.type", "edgegraphics"));
    graphml.addContent(keyVisoneEdge);
    Element keyVisoneGraph = new Element("key", xmlns);
    keyVisoneGraph.setAttribute(new Attribute("for", "graph"));
    keyVisoneGraph.setAttribute(new Attribute("id", "prop"));
    keyVisoneGraph.setAttribute(new Attribute("visone.type", "properties"));
    graphml.addContent(keyVisoneGraph);
    Element keyId = new Element("key", xmlns);
    keyId.setAttribute(new Attribute("id", "id"));
    keyId.setAttribute(new Attribute("for", "node"));
    keyId.setAttribute(new Attribute("attr.name", "id"));
    keyId.setAttribute(new Attribute("attr.type", "string"));
    graphml.addContent(keyId);
    Element keyName = new Element("key", xmlns);
    keyName.setAttribute(new Attribute("id", "name"));
    keyName.setAttribute(new Attribute("for", "node"));
    keyName.setAttribute(new Attribute("attr.name", "name"));
    keyName.setAttribute(new Attribute("attr.type", "string"));
    graphml.addContent(keyName);
    Element keyType = new Element("key", xmlns);
    keyType.setAttribute(new Attribute("id", "type"));
    keyType.setAttribute(new Attribute("for", "node"));
    keyType.setAttribute(new Attribute("attr.name", "type"));
    keyType.setAttribute(new Attribute("attr.type", "string"));
    graphml.addContent(keyType);
    Element keyAlias = new Element("key", xmlns);
    keyAlias.setAttribute(new Attribute("id", "alias"));
    keyAlias.setAttribute(new Attribute("for", "node"));
    keyAlias.setAttribute(new Attribute("attr.name", "alias"));
    keyAlias.setAttribute(new Attribute("attr.type", "string"));
    graphml.addContent(keyAlias);
    Element keyNote = new Element("key", xmlns);
    keyNote.setAttribute(new Attribute("id", "note"));
    keyNote.setAttribute(new Attribute("for", "node"));
    keyNote.setAttribute(new Attribute("attr.name", "note"));
    keyNote.setAttribute(new Attribute("attr.type", "string"));
    graphml.addContent(keyNote);
    Element keyVariable = new Element("key", xmlns);
    keyVariable.setAttribute(new Attribute("id", "variable"));
    keyVariable.setAttribute(new Attribute("for", "node"));
    keyVariable.setAttribute(new Attribute("attr.name", "variable"));
    keyVariable.setAttribute(new Attribute("attr.type", "string"));
    graphml.addContent(keyVariable);
    Element keyFrequency = new Element("key", xmlns);
    keyFrequency.setAttribute(new Attribute("id", "frequency"));
    keyFrequency.setAttribute(new Attribute("for", "node"));
    keyFrequency.setAttribute(new Attribute("attr.name", "frequency"));
    keyFrequency.setAttribute(new Attribute("attr.type", "int"));
    graphml.addContent(keyFrequency);
    Element keyWeight = new Element("key", xmlns);
    keyWeight.setAttribute(new Attribute("id", "weight"));
    keyWeight.setAttribute(new Attribute("for", "edge"));
    keyWeight.setAttribute(new Attribute("attr.name", "weight"));
    keyWeight.setAttribute(new Attribute("attr.type", "double"));
    graphml.addContent(keyWeight);
    Element graphElement = new Element("graph", xmlns);
    graphElement.setAttribute(new Attribute("edgedefault", "undirected"));
    graphElement.setAttribute(new Attribute("id", "DNA"));
    int numEdges = rn.length * cn.length;
    if (twoMode == false) {
        numEdges = (numEdges / 2) - rn.length;
    }
    int numNodes = rn.length;
    if (twoMode == true) {
        numNodes = numNodes + cn.length;
    }
    graphElement.setAttribute(new Attribute("parse.edges", String.valueOf(numEdges)));
    graphElement.setAttribute(new Attribute("parse.nodes", String.valueOf(numNodes)));
    graphElement.setAttribute(new Attribute("parse.order", "free"));
    Element properties = new Element("data", xmlns);
    properties.setAttribute(new Attribute("key", "prop"));
    Element labelAttribute = new Element("labelAttribute", visone);
    labelAttribute.setAttribute("edgeLabel", "weight");
    labelAttribute.setAttribute("nodeLabel", "name");
    properties.addContent(labelAttribute);
    graphElement.addContent(properties);
    // add nodes
    Comment nodes = new Comment(" nodes ");
    graphElement.addContent(nodes);
    for (int i = 0; i < names.length; i++) {
        Element node = new Element("node", xmlns);
        node.setAttribute(new Attribute("id", "n" + id[i]));
        Element idElement = new Element("data", xmlns);
        idElement.setAttribute(new Attribute("key", "id"));
        idElement.setText(String.valueOf(id[i]));
        node.addContent(idElement);
        Element nameElement = new Element("data", xmlns);
        nameElement.setAttribute(new Attribute("key", "name"));
        nameElement.setText(names[i]);
        node.addContent(nameElement);
        Element typeElement = new Element("data", xmlns);
        typeElement.setAttribute(new Attribute("key", "type"));
        typeElement.setText(type[i]);
        node.addContent(typeElement);
        Element aliasElement = new Element("data", xmlns);
        aliasElement.setAttribute(new Attribute("key", "alias"));
        aliasElement.setText(alias[i]);
        node.addContent(aliasElement);
        Element notesElement = new Element("data", xmlns);
        notesElement.setAttribute(new Attribute("key", "notes"));
        notesElement.setText(notes[i]);
        node.addContent(notesElement);
        Element variableElement = new Element("data", xmlns);
        variableElement.setAttribute(new Attribute("key", "variable"));
        variableElement.setText(variables[i]);
        node.addContent(variableElement);
        Element frequency = new Element("data", xmlns);
        frequency.setAttribute(new Attribute("key", "frequency"));
        frequency.setText(String.valueOf(frequencies[i]));
        node.addContent(frequency);
        Element vis = new Element("data", xmlns);
        vis.setAttribute(new Attribute("key", "d0"));
        Element visoneShapeNode = new Element("shapeNode", visone);
        Element yShapeNode = new Element("ShapeNode", yNs);
        Element geometry = new Element("Geometry", yNs);
        geometry.setAttribute(new Attribute("height", "20.0"));
        geometry.setAttribute(new Attribute("width", "20.0"));
        geometry.setAttribute(new Attribute("x", String.valueOf(Math.random() * 800)));
        geometry.setAttribute(new Attribute("y", String.valueOf(Math.random() * 600)));
        yShapeNode.addContent(geometry);
        Element fill = new Element("Fill", yNs);
        fill.setAttribute(new Attribute("color", color[i]));
        fill.setAttribute(new Attribute("transparent", "false"));
        yShapeNode.addContent(fill);
        Element borderStyle = new Element("BorderStyle", yNs);
        borderStyle.setAttribute(new Attribute("color", "#000000"));
        borderStyle.setAttribute(new Attribute("type", "line"));
        borderStyle.setAttribute(new Attribute("width", "1.0"));
        yShapeNode.addContent(borderStyle);
        Element nodeLabel = new Element("NodeLabel", yNs);
        nodeLabel.setAttribute(new Attribute("alignment", "center"));
        nodeLabel.setAttribute(new Attribute("autoSizePolicy", "content"));
        nodeLabel.setAttribute(new Attribute("backgroundColor", "#FFFFFF"));
        nodeLabel.setAttribute(new Attribute("fontFamily", "Dialog"));
        nodeLabel.setAttribute(new Attribute("fontSize", "12"));
        nodeLabel.setAttribute(new Attribute("fontStyle", "plain"));
        nodeLabel.setAttribute(new Attribute("hasLineColor", "false"));
        nodeLabel.setAttribute(new Attribute("height", "19.0"));
        nodeLabel.setAttribute(new Attribute("modelName", "eight_pos"));
        nodeLabel.setAttribute(new Attribute("modelPosition", "n"));
        nodeLabel.setAttribute(new Attribute("textColor", "#000000"));
        nodeLabel.setAttribute(new Attribute("visible", "true"));
        nodeLabel.setText(names[i]);
        yShapeNode.addContent(nodeLabel);
        Element shape = new Element("Shape", yNs);
        if (i < rn.length) {
            shape.setAttribute(new Attribute("type", "ellipse"));
        } else {
            shape.setAttribute(new Attribute("type", "roundrectangle"));
        }
        yShapeNode.addContent(shape);
        visoneShapeNode.addContent(yShapeNode);
        vis.addContent(visoneShapeNode);
        node.addContent(vis);
        graphElement.addContent(node);
    }
    // add edges
    double[][] m = mt.getMatrix();
    Comment edges = new Comment(" edges ");
    graphElement.addContent(edges);
    for (int i = 0; i < rn.length; i++) {
        for (int j = 0; j < cn.length; j++) {
            if (m[i][j] != 0.0 && (twoMode == true || (twoMode == false && i < j))) {
                // only lower triangle is used for one-mode networks
                Element edge = new Element("edge", xmlns);
                int currentId = id[i];
                edge.setAttribute(new Attribute("source", "n" + String.valueOf(currentId)));
                if (twoMode == true) {
                    currentId = id[j + rn.length];
                } else {
                    currentId = id[j];
                }
                edge.setAttribute(new Attribute("target", "n" + String.valueOf(currentId)));
                Element weight = new Element("data", xmlns);
                weight.setAttribute(new Attribute("key", "weight"));
                weight.setText(String.valueOf(m[i][j]));
                edge.addContent(weight);
                Element visEdge = new Element("data", xmlns);
                visEdge.setAttribute("key", "e0");
                Element visPolyLineEdge = new Element("polyLineEdge", visone);
                Element yPolyLineEdge = new Element("PolyLineEdge", yNs);
                Element yLineStyle = new Element("LineStyle", yNs);
                if (qualifierAggregation.equals("combine") && qualifierBinary == true) {
                    if (m[i][j] == 1.0) {
                        yLineStyle.setAttribute("color", "#00ff00");
                    } else if (m[i][j] == 2.0) {
                        yLineStyle.setAttribute("color", "#ff0000");
                    } else if (m[i][j] == 3.0) {
                        yLineStyle.setAttribute("color", "#0000ff");
                    }
                } else if (qualifierAggregation.equals("subtract")) {
                    if (m[i][j] < 0) {
                        yLineStyle.setAttribute("color", "#ff0000");
                    } else if (m[i][j] > 0) {
                        yLineStyle.setAttribute("color", "#00ff00");
                    }
                } else if (qualifierAggregation.equals("conflict")) {
                    yLineStyle.setAttribute("color", "#ff0000");
                } else if (qualifierAggregation.equals("congruence")) {
                    yLineStyle.setAttribute("color", "#00ff00");
                } else {
                    yLineStyle.setAttribute("color", "#000000");
                }
                yLineStyle.setAttribute(new Attribute("type", "line"));
                yLineStyle.setAttribute(new Attribute("width", "2.0"));
                yPolyLineEdge.addContent(yLineStyle);
                visPolyLineEdge.addContent(yPolyLineEdge);
                visEdge.addContent(visPolyLineEdge);
                edge.addContent(visEdge);
                graphElement.addContent(edge);
            }
        }
    }
    graphml.addContent(graphElement);
    // write to file
    File dnaFile = new File(outfile);
    try {
        FileOutputStream outStream = new FileOutputStream(dnaFile);
        XMLOutputter outToFile = new XMLOutputter();
        Format format = Format.getPrettyFormat();
        format.setEncoding("utf-8");
        outToFile.setFormat(format);
        outToFile.output(document, outStream);
        outStream.flush();
        outStream.close();
    } catch (IOException e) {
        System.err.println("Cannot save \"" + dnaFile + "\":" + e.getMessage());
    }
}
Also used : Comment(org.jdom.Comment) XMLOutputter(org.jdom.output.XMLOutputter) Attribute(org.jdom.Attribute) Element(org.jdom.Element) IOException(java.io.IOException) Document(dna.dataStructures.Document) Namespace(org.jdom.Namespace) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) Format(org.jdom.output.Format) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

Format (org.jdom.output.Format)45 XMLOutputter (org.jdom.output.XMLOutputter)40 Document (org.jdom.Document)20 Element (org.jdom.Element)20 StringWriter (java.io.StringWriter)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)6 Namespace (org.jdom.Namespace)6 Writer (java.io.Writer)5 SAXBuilder (org.jdom.input.SAXBuilder)5 DateFormat (java.text.DateFormat)4 SimpleDateFormat (java.text.SimpleDateFormat)4 JDOMException (org.jdom.JDOMException)4 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)3 File (java.io.File)3 FileWriter (java.io.FileWriter)3 StringReader (java.io.StringReader)3 List (java.util.List)3 TreeMap (java.util.TreeMap)3 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)3