Example usage for org.jdom2 Element setAttribute

List of usage examples for org.jdom2 Element setAttribute

Introduction

In this page you can find the example usage for org.jdom2 Element setAttribute.

Prototype

public Element setAttribute(final String name, final String value, final Namespace ns) 

Source Link

Document

This sets an attribute value for this element.

Usage

From source file:com.tactfactory.harmony.updater.impl.XmlAndroid.java

License:Open Source License

@Override
public String addElement(String key, String value) {
    String result = value;//  w  w w.  j a v  a 2s  .  com

    final Element newNode = new Element(XML_ELEMENT_STRING);

    // Add name to element
    newNode.setAttribute(XML_ELEMENT_NAME, key, this.namespace);

    // Set values
    newNode.setText(value);

    // If not found Node, create it
    if (!XMLUtils.addValue(newNode, XML_ELEMENT_NAME, rootNode)) {
        result = newNode.getText();
    }

    return result;
}

From source file:com.thoughtworks.go.config.MagicalGoConfigXmlWriter.java

License:Apache License

private Document createEmptyCruiseConfigDocument() {
    Element root = new Element("cruise");
    Namespace xsiNamespace = Namespace.getNamespace("xsi", XML_NS);
    root.addNamespaceDeclaration(xsiNamespace);
    registry.registerNamespacesInto(root);
    root.setAttribute("noNamespaceSchemaLocation", "cruise-config.xsd", xsiNamespace);
    String xsds = registry.xsds();
    if (!xsds.isEmpty()) {
        root.setAttribute("schemaLocation", xsds, xsiNamespace);
    }/* w ww . j  av  a  2  s  .c  om*/
    root.setAttribute("schemaVersion", Integer.toString(GoConstants.CONFIG_SCHEMA_VERSION));
    return new Document(root);
}

From source file:cz.pecina.retro.memory.Snapshot.java

License:Open Source License

/**
 * Writes a hardware shapshot to a file.
 *
 * @param file output file/*  w ww .  j a v  a  2  s  . com*/
 */
public void write(final File file) {
    log.fine("Writing snapshot to a file, file: " + file.getName());
    final Element snapshot = new Element("snapshot");
    final Namespace namespace = Namespace.getNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    snapshot.addNamespaceDeclaration(namespace);
    snapshot.setAttribute("noNamespaceSchemaLocation",
            Application.XSD_PREFIX + "snapshot-" + SNAPSHOT_XML_FILE_VERSION + ".xsd", namespace);
    snapshot.setAttribute("version", SNAPSHOT_XML_FILE_VERSION);
    hardware.marshal(snapshot);
    final Document doc = new Document(snapshot);
    try (final PrintWriter writer = new PrintWriter(file)) {
        new XMLOutputter(Format.getPrettyFormat()).output(doc, writer);
    } catch (final Exception exception) {
        log.fine("Error, writing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLWrite");
    }
    log.fine("Writing completed");
}

From source file:cz.pecina.retro.memory.XML.java

License:Open Source License

/**
 * Writes a memory range to a file, with wrap-around.
 *
 * @param file               output file
 * @param startAddress       starting address
 * @param number             number of bytes
 * @param destinationAddress destination address
 *//* w ww . j  ava  2  s. c o m*/
public void write(final File file, final int startAddress, final int number, final int destinationAddress) {
    log.fine(
            String.format("Writing XML data to a file, file: %s, start address: %04x," + " number of bytes: %d",
                    file.getName(), startAddress, number));
    final Element tag = new Element("memory");
    Snapshot.buildBlockElement(sourceMemory, tag, startAddress, number);
    final Namespace namespace = Namespace.getNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    tag.addNamespaceDeclaration(namespace);
    tag.setAttribute("noNamespaceSchemaLocation",
            Application.XSD_PREFIX + "memory-" + MEMORY_XML_FILE_VERSION + ".xsd", namespace);
    tag.setAttribute("version", MEMORY_XML_FILE_VERSION);
    tag.setAttribute("start", String.format("%04X", destinationAddress));
    final Document doc = new Document(tag);
    try (final PrintWriter writer = new PrintWriter(file)) {
        new XMLOutputter(Format.getPrettyFormat()).output(doc, writer);
    } catch (final Exception exception) {
        log.fine("Error, writing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLWrite");
    }
    log.fine("Writing completed");
}

From source file:cz.pecina.retro.trec.XML.java

License:Open Source License

/**
 * Writes the tape to an XML file.//from   w  w w.j av a2 s .c  om
 *
 * @param file output file
 */
public void write(final File file) {
    log.fine("Writing tape data to an XML file, file: " + file);
    final Element tag = new Element("tape");
    final Namespace namespace = Namespace.getNamespace("xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    tag.addNamespaceDeclaration(namespace);
    tag.setAttribute("noNamespaceSchemaLocation",
            Application.XSD_PREFIX + "tape-" + TAPE_XML_FILE_VERSION + ".xsd", namespace);
    tag.setAttribute("version", TAPE_XML_FILE_VERSION);
    tag.setAttribute("rate", String.valueOf(tapeRecorderInterface.tapeSampleRate));
    tag.setAttribute("unit", "per sec");
    try {
        long currPos = -1;
        for (long start : tape.navigableKeySet()) {
            final long duration = tape.get(start);
            log.finest(String.format("Fetched: (%d, %d)", start, duration));
            if ((start > currPos) && (duration > 0)) {
                final Element pulse = new Element("pulse");
                pulse.setAttribute("start", String.valueOf(start));
                pulse.setAttribute("duration", String.valueOf(duration));
                tag.addContent(pulse);
                log.finest(String.format("Write: (%d, %d)", start, duration));
                currPos = start + duration;
            }
        }
    } catch (final Exception exception) {
        log.fine("Error, writing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLWrite");
    }
    final Document doc = new Document(tag);
    try (final PrintWriter writer = new PrintWriter(file)) {
        new XMLOutputter(Format.getPrettyFormat()).output(doc, writer);
    } catch (final Exception exception) {
        log.fine("Error, writing failed, exception: " + exception.getMessage());
        throw Application.createError(this, "XMLWrite");
    }
    log.fine("Writing completed");
}

From source file:de.dfki.iui.mmds.core.emf.persistence.EmfPersistence.java

License:Creative Commons License

/**
 * Update member contents with resolved data
 * /*from   w  w w  .  j  a v a2  s .c  o  m*/
 * @param node
 * @param memberReference
 * @param memberDocument
 * @return
 * @throws Exception
 */
private static Element updateMember(Element node, EReference memberReference, int id, EObject memberObject,
        Document memberDocument) throws Exception {
    Namespace namespace = node.getNamespace(modelMetaData.getNamespace(memberReference));

    List<Element> children = null;
    if (namespace == null) {
        children = node.getChildren(modelMetaData.getName(memberReference));
    } else
        throw new IllegalArgumentException();
    if (id < children.size()) {
        // Inserting resolved contents to the member
        Element element = children.get(id);
        Element clone = memberDocument.getRootElement().clone();
        clone.setName(memberReference.getName());
        node.setContent(node.indexOf(element), clone);
        // Setting xsi:type
        clone.setAttribute("type",
                memberObject.eClass().getEPackage().getName() + ":" + memberObject.eClass().getName(),
                node.getNamespace("xsi"));
    }

    return node;
}

From source file:de.nava.informa.exporters.RSS_1_0_Exporter.java

License:Open Source License

public void write(ChannelIF channel) throws IOException {
    if (writer == null) {
        throw new RuntimeException("No writer has been initialized.");
    }// ww  w. j  a va 2s  .  co  m

    Element rootElem = new Element("RDF", NS_RDF);
    rootElem.addNamespaceDeclaration(NS_DEFAULT); // TODO
    rootElem.addNamespaceDeclaration(NS_DC);
    rootElem.addNamespaceDeclaration(NS_SY);
    // rootElem.setAttribute("version");
    Element channelElem = new Element("channel", NS_DEFAULT);
    if (channel.getLocation() != null) {
        channelElem.setAttribute("about", channel.getLocation().toString(), NS_RDF);
    }
    channelElem.addContent(new Element("title", NS_DEFAULT).setText(channel.getTitle()));
    if (channel.getSite() != null) {
        channelElem.addContent(new Element("link", NS_DEFAULT).setText(channel.getSite().toString()));
        channelElem.addContent(
                new Element("source", NS_DC).setAttribute("resource", channel.getSite().toString()));
    }

    channelElem.addContent(new Element("description", NS_DEFAULT).setText(channel.getDescription()));
    if (channel.getLanguage() != null) {
        channelElem.addContent(new Element("language", NS_DC).setText(channel.getLanguage()));
    }
    if (channel.getCopyright() != null) {
        channelElem.addContent(new Element("copyright", NS_DC).setText(channel.getCopyright()));
    }
    if (channel.getUpdateBase() != null) {
        channelElem.addContent(new Element("updateBase", NS_SY).setText(df.format(channel.getUpdateBase())));
    }
    if (channel.getUpdatePeriod() != null) {
        // don't put out frequency without specifying period
        channelElem.addContent(new Element("updateFrequency", NS_SY)
                .setText((new Integer(channel.getUpdateFrequency())).toString()));
        channelElem
                .addContent(new Element("updatePeriod", NS_SY).setText(channel.getUpdatePeriod().toString()));
    }
    // export channel image
    if (channel.getImage() != null) {
        Element imgElem = new Element("image", NS_DEFAULT);
        imgElem.addContent(new Element("title", NS_DEFAULT).setText(channel.getImage().getTitle()));
        imgElem.addContent(new Element("url", NS_DEFAULT).setText(channel.getImage().getLocation().toString()));
        imgElem.addContent(new Element("link", NS_DEFAULT).setText(channel.getImage().getLink().toString()));
        imgElem.addContent(new Element("height", NS_DEFAULT).setText("" + channel.getImage().getHeight()));
        imgElem.addContent(new Element("width", NS_DEFAULT).setText("" + channel.getImage().getWidth()));
        imgElem.addContent(new Element("description", NS_DEFAULT).setText(channel.getImage().getDescription()));
        channelElem.addContent(imgElem);
    }

    // TODO: add exporting textinput field
    //     if (channel.getTextInput() != null) {
    //       channelElem.addContent(channel.getTextInput().getElement());
    //     }

    // ===========================================
    Element itemsElem = new Element("items", NS_DEFAULT);
    Element seqElem = new Element("Seq", NS_RDF);
    Collection items = channel.getItems();
    Iterator it = items.iterator();
    while (it.hasNext()) {
        ItemIF item = (ItemIF) it.next();
        Element itemElem = new Element("li", NS_RDF);
        if (item.getLink() != null) {
            itemElem.setAttribute("resource", item.getLink().toString());
        }
        seqElem.addContent(itemElem);
    }
    itemsElem.addContent(seqElem);
    channelElem.addContent(itemsElem);
    rootElem.addContent(channelElem);

    // item-by-item en detail
    items = channel.getItems();
    it = items.iterator();
    while (it.hasNext()) {
        rootElem.addContent(getItemElement((ItemIF) it.next()));
    }

    // create XML outputter with indent: 2 spaces, print new lines.
    Format format = Format.getPrettyFormat();
    format.setEncoding(encoding);
    XMLOutputter outputter = new XMLOutputter(format);
    // write DOM to file
    Document doc = new Document(rootElem);
    outputter.output(doc, writer);
    writer.close();
}

From source file:de.nava.informa.exporters.RSS_1_0_Exporter.java

License:Open Source License

protected Element getItemElement(ItemIF item) {
    Element itemElem = new Element("item", NS_DEFAULT);
    if (item.getLink() != null) {
        itemElem.setAttribute("about", item.getLink().toString(), NS_RDF);
    }/*from   w w w .j  a  va 2 s .c om*/
    itemElem.addContent(new Element("title", NS_DEFAULT).setText(item.getTitle()));
    if (item.getLink() != null) {
        itemElem.addContent(new Element("link", NS_DEFAULT).setText(item.getLink().toString()));
    }
    if (item.getDescription() != null) {
        itemElem.addContent(new Element("description", NS_DC).setText(item.getDescription()));
    }
    if (item.getDate() != null) {
        itemElem.addContent(new Element("date", NS_DC).setText(ParserUtils.formatDate(item.getDate())));
    }
    return itemElem;
}

From source file:dibl.diagrams.Template.java

License:Open Source License

private void replace(final String[][] newValues, final String searchPattern) {
    addAppliedMatrixToTextObject(newValues);

    for (final Set<Element> elements : tileElements.values()) {
        for (final Element el : elements) {
            final char[] cellID = el.getAttribute("label", NS_INKSCAPE).getValue().toCharArray();
            final int r = cellID[1] - '1';
            final int c = cellID[0] - 'A';
            final String attribute = el.getAttribute("href", NS_XLINK).getValue();
            final String oldLabel = labelsByIDs.get(attribute);
            if (oldLabel != null) {
                final String newLabel = oldLabel.replaceAll(searchPattern, newValues[r][c]);
                final String newID = idsByLabels.get(newLabel);
                if (newID != null)
                    el.setAttribute("href", "#" + newID, NS_XLINK);
            }//w ww . java2 s. c  o m
        }
    }
}