Example usage for org.w3c.dom Element setAttribute

List of usage examples for org.w3c.dom Element setAttribute

Introduction

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

Prototype

public void setAttribute(String name, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:it.unibas.spicy.persistence.xml.operators.ExportXMLInstances.java

public void visitMetadataNode(MetadataNode node) {
    Element peekElement = stackOfElements.peek();
    LeafNode leafOfMetadataNodeNode = (LeafNode) node.getChild(0);
    peekElement.setAttribute(node.getLabel(), leafOfMetadataNodeNode.getValue().toString());
}

From source file:de.ii.xtraplatform.ogc.csw.client.CSWOperation.java

public String toXml(XMLNamespaceNormalizer nsStore, CSW.VERSION version) throws ParserConfigurationException {
    this.initialize(nsStore);

    XMLDocumentFactory documentFactory = new XMLDocumentFactory(nsStore);
    XMLDocument document = documentFactory.newDocument();
    document.addNamespace(CSW.getNS(version), CSW.getPR(version));

    Element operationElement = document.createElementNS(CSW.getNS(version), getOperationName(version));

    for (String uri : nsStore.xgetNamespaceUris()) {
        operationElement.setAttribute("xmlns:" + uri, nsStore.getNamespaceURI(uri));
    }/* w w w .  j  a  va  2 s  . c om*/

    operationElement.setAttribute(CSW.getWord(version, CSW.VOCABULARY.SERVICE),
            CSW.getWord(version, CSW.VOCABULARY.CSW));

    if (version != null) {
        operationElement.setAttribute(CSW.getWord(version, CSW.VOCABULARY.VERSION), version.toString());
    }

    operationElement = toXml(document, operationElement, version);
    document.appendChild(operationElement);

    String out;

    if (LOGGER.isDebugEnabled()) {
        out = document.toString(true);
        LOGGER.debug(out);
    } else {
        out = document.toString(false);
    }

    return out;
}

From source file:eu.apenet.dpt.standalone.gui.hgcreation.LevelTreeActions.java

private Element createArchdesc(Document doc, TreeModel model, Object node, HashMap<String, String> paramMap,
        String id, String title) {
    CLevelTreeObject obj = (CLevelTreeObject) ((DefaultMutableTreeNode) node).getUserObject();

    Element archdesc = doc.createElement("archdesc");
    archdesc.setAttribute("level", "fonds");
    archdesc.setAttribute("type", "holdings_guide");
    archdesc.setAttribute("encodinganalog", "3.1.4");
    archdesc.setAttribute("relatedencoding", "ISAD(G)v2");

    Element did = doc.createElement("did");

    Element unitid = doc.createElement("unitid");
    unitid.setAttribute("encodinganalog", "3.1.1");
    unitid.setTextContent(id);//  w  ww  . java 2 s . com

    Element unittitle = doc.createElement("unittitle");
    unittitle.setAttribute("encodinganalog", "3.1.2");
    unittitle.setTextContent(title);

    Element dsc = doc.createElement("dsc");

    did.appendChild(unitid);
    did.appendChild(unittitle);
    archdesc.appendChild(did);

    if (!obj.getDescription().equals("")) {
        Element scopecontent = doc.createElement("scopecontent");
        scopecontent.setAttribute("encodinganalog", "summary");
        Element pElt = doc.createElement("p");
        pElt.setTextContent(obj.getDescription());
        scopecontent.appendChild(pElt);
        archdesc.appendChild(scopecontent);
    }

    archdesc.appendChild(dsc);

    for (int i = 0; i < model.getChildCount(node); i++) {
        dsc.appendChild(createTree(doc, model, model.getChild(node, i), paramMap));
    }

    return archdesc;
}

From source file:de.erdesignerng.model.serializer.xml10.XMLAttributeSerializer.java

@Override
public void serialize(Attribute aAttribute, Document aDocument, Element aRootElement) {

    Element theAttributeElement = addElement(aDocument, aRootElement, ATTRIBUTE);

    // Basisdaten des Modelelementes speichern
    serializeProperties(aDocument, theAttributeElement, aAttribute);

    theAttributeElement.setAttribute(DATATYPE, aAttribute.getDatatype().getName());
    theAttributeElement.setAttribute(SIZE, "" + aAttribute.getSize());
    theAttributeElement.setAttribute(FRACTION, "" + aAttribute.getFraction());
    theAttributeElement.setAttribute(SCALE, "" + aAttribute.getScale());
    theAttributeElement.setAttribute(DEFAULTVALUE, aAttribute.getDefaultValue());
    theAttributeElement.setAttribute(EXTRA, aAttribute.getExtra());

    setBooleanAttribute(theAttributeElement, NULLABLE, aAttribute.isNullable());

    serializeCommentElement(aDocument, theAttributeElement, aAttribute);
}

From source file:com.msopentech.odatajclient.engine.data.impl.v3.AtomSerializer.java

private Element entry(final AtomEntry entry) throws ParserConfigurationException {
    final DocumentBuilder builder = XMLUtils.DOC_BUILDER_FACTORY.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element entryElem = doc.createElement(ODataConstants.ATOM_ELEM_ENTRY);
    entryElem.setAttribute(XMLConstants.XMLNS_ATTRIBUTE, ODataConstants.NS_ATOM);
    entryElem.setAttribute(ODataConstants.XMLNS_METADATA,
            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_METADATA));
    entryElem.setAttribute(ODataConstants.XMLNS_DATASERVICES,
            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_DATASERVICES));
    entryElem.setAttribute(ODataConstants.XMLNS_GML, ODataConstants.NS_GML);
    entryElem.setAttribute(ODataConstants.XMLNS_GEORSS, ODataConstants.NS_GEORSS);
    if (entry.getBaseURI() != null) {
        entryElem.setAttribute(ODataConstants.ATTR_XMLBASE, entry.getBaseURI().toASCIIString());
    }/*from  w ww .  ja  v  a  2 s . c om*/
    doc.appendChild(entryElem);

    final Element category = doc.createElement(ODataConstants.ATOM_ELEM_CATEGORY);
    category.setAttribute(ODataConstants.ATOM_ATTR_TERM, entry.getType());
    category.setAttribute(ODataConstants.ATOM_ATTR_SCHEME,
            client.getWorkingVersion().getNamespaceMap().get(ODataVersion.NS_SCHEME));
    entryElem.appendChild(category);

    if (StringUtils.isNotBlank(entry.getTitle())) {
        final Element title = doc.createElement(ODataConstants.ATOM_ELEM_TITLE);
        title.appendChild(doc.createTextNode(entry.getTitle()));
        entryElem.appendChild(title);
    }

    if (StringUtils.isNotBlank(entry.getSummary())) {
        final Element summary = doc.createElement(ODataConstants.ATOM_ELEM_SUMMARY);
        summary.appendChild(doc.createTextNode(entry.getSummary()));
        entryElem.appendChild(summary);
    }

    setLinks(entryElem, entry.getAssociationLinks());
    setLinks(entryElem, entry.getNavigationLinks());
    setLinks(entryElem, entry.getMediaEditLinks());

    final Element content = doc.createElement(ODataConstants.ATOM_ELEM_CONTENT);
    if (entry.isMediaEntry()) {
        if (StringUtils.isNotBlank(entry.getMediaContentType())) {
            content.setAttribute(ODataConstants.ATTR_TYPE, entry.getMediaContentType());
        }
        if (StringUtils.isNotBlank(entry.getMediaContentSource())) {
            content.setAttribute(ODataConstants.ATOM_ATTR_SRC, entry.getMediaContentSource());
        }
        if (content.getAttributes().getLength() > 0) {
            entryElem.appendChild(content);
        }

        if (entry.getMediaEntryProperties() != null) {
            entryElem.appendChild(doc.importNode(entry.getMediaEntryProperties(), true));
        }
    } else {
        content.setAttribute(ODataConstants.ATTR_TYPE, ContentType.APPLICATION_XML.getMimeType());
        if (entry.getContent() != null) {
            content.appendChild(doc.importNode(entry.getContent(), true));
        }
        entryElem.appendChild(content);
    }

    return entryElem;
}

From source file:com.netspective.sparx.util.xml.XmlSource.java

/**
 * Given an attribute name, check to see if it has a value -- if not, set it to default
 *//*www .  j ava  2 s  .  c o  m*/
public static void setAttrValueDefault(Element parent, String name, String defaultValue) {
    String attrValue = parent.getAttribute(name);
    if (attrValue.length() == 0 && defaultValue != null && defaultValue.length() > 0)
        parent.setAttribute(name, defaultValue);
}

From source file:com.enonic.vertical.adminweb.AdminHandlerBaseServlet.java

protected static final Element buildAccessRightElement(Document doc, Element root, String key,
        ExtendedMap paramsInValue) {/*from   w  ww  .j  a v a2 s.  com*/

    Element element = XMLTool.createElement(doc, root, "accessright");

    if (key != null) {
        element.setAttribute("groupkey", key);
    }
    element.setAttribute("grouptype", paramsInValue.getString("grouptype", ""));
    element.setAttribute("adminread", paramsInValue.getString("adminread", "false"));
    element.setAttribute("read", paramsInValue.getString("read", "false"));
    element.setAttribute("update", paramsInValue.getString("update", "false"));
    element.setAttribute("delete", paramsInValue.getString("delete", "false"));
    element.setAttribute("create", paramsInValue.getString("create", "false"));
    element.setAttribute("publish", paramsInValue.getString("publish", "false"));
    element.setAttribute("administrate", paramsInValue.getString("administrate", "false"));
    element.setAttribute("approve", paramsInValue.getString("approve", "false"));
    element.setAttribute("add", paramsInValue.getString("add", "false"));

    String displayName = paramsInValue.getString("name", null);
    if (displayName != null) {
        element.setAttribute("displayname", displayName);
    }

    return element;
}

From source file:Exporters.SaveFileExporter.java

public void save(File outfile, DefaultMutableTreeNode root) throws Exception {
    System.out.println("===SaveFileExporter==save:" + outfile.getAbsolutePath());

    // Create new XML document
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

    // Create the root element
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("reportCompiler");
    rootElement.setAttribute("date", getNow()); // Debugging to say when saved
    // TODO - figure out a universal means to get the current version of the generating tool
    // Initial thoughts would be to create a convenience class in Utils package with some
    // final variables that can be updated when issuing a release.
    doc.appendChild(rootElement);/*from ww w.  ja  v a2s  .  com*/

    Element vulnerabilitiesElement = doc.createElement("vulnerabilities");

    Enumeration enums = root.children();
    while (enums.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) enums.nextElement();
        Object obj = node.getUserObject();
        if (obj instanceof Vulnerability) {
            Vulnerability vuln = (Vulnerability) obj;
            Element vulnElement = getVulnAsElement(vuln, doc);
            if (vulnElement != null) {
                vulnerabilitiesElement.appendChild(vulnElement);
            }
        }
    }

    rootElement.appendChild(vulnerabilitiesElement);

    // now write the XML file
    // write the content into xml file
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(outfile);

    // Output to console for testing
    //StreamResult result = new StreamResult(System.out);
    transformer.transform(source, result);

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

}

From source file:hoot.services.controllers.osm.MapResource.java

private static Document generateExtentOSM(String maxlon, String maxlat, String minlon, String minlat) {
    SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    Date now = new Date();
    String strDate = sdfDate.format(now);

    try {//w  ww .  ja  v a 2 s  .c o  m
        DocumentBuilderFactory dbf = XmlDocumentBuilder.getSecureDocBuilderFactory();
        dbf.setValidating(false);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        Element osmElem = doc.createElement("osm");
        osmElem.setAttribute("version", "0.6");
        osmElem.setAttribute("generator", "hootenanny");
        doc.appendChild(osmElem);

        Element boundsElem = doc.createElement("bounds");
        boundsElem.setAttribute("minlat", minlat);
        boundsElem.setAttribute("minlon", minlon);
        boundsElem.setAttribute("maxlat", maxlat);
        boundsElem.setAttribute("maxlon", maxlon);
        osmElem.appendChild(boundsElem);

        // The ID's for these fabricated nodes were stepping on the ID's of actual nodes, so their ID's need to be
        // made negative and large, so they have no chance of stepping on anything.

        long node1Id = Long.MIN_VALUE + 3;
        long node2Id = Long.MIN_VALUE + 2;
        long node3Id = Long.MIN_VALUE + 1;
        long node4Id = Long.MIN_VALUE;

        Element nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node1Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", maxlat);
        nodeElem.setAttribute("lon", minlon);
        osmElem.appendChild(nodeElem);

        nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node2Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", maxlat);
        nodeElem.setAttribute("lon", maxlon);
        osmElem.appendChild(nodeElem);

        nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node3Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", minlat);
        nodeElem.setAttribute("lon", maxlon);
        osmElem.appendChild(nodeElem);

        nodeElem = doc.createElement("node");
        nodeElem.setAttribute("id", String.valueOf(node4Id));
        nodeElem.setAttribute("timestamp", strDate);
        nodeElem.setAttribute("user", "hootenannyuser");
        nodeElem.setAttribute("visible", "true");
        nodeElem.setAttribute("version", "1");
        nodeElem.setAttribute("lat", minlat);
        nodeElem.setAttribute("lon", minlon);
        osmElem.appendChild(nodeElem);

        Element wayElem = doc.createElement("way");
        wayElem.setAttribute("id", String.valueOf(Long.MIN_VALUE));
        wayElem.setAttribute("timestamp", strDate);
        wayElem.setAttribute("user", "hootenannyuser");
        wayElem.setAttribute("visible", "true");
        wayElem.setAttribute("version", "1");

        Element ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node1Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node2Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node3Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node4Id));
        wayElem.appendChild(ndElem);

        ndElem = doc.createElement("nd");
        ndElem.setAttribute("ref", String.valueOf(node1Id));
        wayElem.appendChild(ndElem);

        /*
         * ndElem = doc.createElement("tag"); ndElem.setAttribute("k", "area");
         * ndElem.setAttribute("v", "yes"); wayElem.appendChild(ndElem);
         */

        osmElem.appendChild(wayElem);

        Transformer tf = TransformerFactory.newInstance().newTransformer();

        // Fortify may require this, but it doesn't work.
        // TransformerFactory transformerFactory =
        // XmlDocumentBuilder.getSecureTransformerFactory();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");

        try (Writer out = new StringWriter()) {
            tf.transform(new DOMSource(doc), new StreamResult(out));
            logger.debug("Layer Extent OSM: {}", out);
        }

        return doc;
    } catch (Exception e) {
        throw new RuntimeException("Error generating OSM extent", e);
    }
}

From source file:com.weaforce.system.component.fckeditor.response.XmlResponse.java

/**
 * Lists all folders in the given dir as XML tags.
 * @param dir/*from   w  w w.j  av a2s . c  om*/
 */
public void setFolders(File dir) {

    if (foldersElement != null) {
        Element parent = (Element) foldersElement.getParentNode();
        parent.removeChild(foldersElement);
    }

    foldersElement = document.createElement("Folders");
    document.getDocumentElement().appendChild(foldersElement);

    String[] fileList = dir.list(DirectoryFileFilter.DIRECTORY);
    for (String file : fileList) {
        Element folderElement = document.createElement("Folder");
        folderElement.setAttribute("name", file);
        foldersElement.appendChild(folderElement);
    }
}