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:com.evolveum.midpoint.util.DOMUtil.java

public static void setAttributeValue(Element element, String name, String value) {
    checkValidXmlChars(value);/*from ww w. j  a  v a  2 s . co  m*/
    element.setAttribute(name, value);
}

From source file:fi.helsinki.lib.simplerest.ItemsResource.java

@Get("html|xhtml|xml")
public Representation toXml() {
    Collection collection = null;
    DomRepresentation representation = null;
    Document d = null;/*from   w ww.j a v  a 2 s  . c o  m*/
    try {
        collection = Collection.find(context, this.collectionId);
        if (collection == null) {
            return errorNotFound(context, "Could not find the collection.");
        }

        representation = new DomRepresentation(MediaType.TEXT_HTML);
        d = representation.getDocument();
    } catch (SQLException e) {
        return errorInternal(context, e.toString());
    } catch (IOException e) {
        return errorInternal(context, e.toString());
    }

    Element html = d.createElement("html");
    d.appendChild(html);

    Element head = d.createElement("head");
    html.appendChild(head);

    Element title = d.createElement("title");
    head.appendChild(title);
    title.appendChild(d.createTextNode("Items for collection " + collection.getName()));

    Element body = d.createElement("body");
    html.appendChild(body);

    Element ulItems = d.createElement("ul");
    setId(ulItems, "items");
    body.appendChild(ulItems);

    String base = baseUrl();
    try {
        ItemIterator ii = collection.getItems();
        while (ii.hasNext()) {
            Item item = ii.next();
            Element li = d.createElement("li");
            Element a = d.createElement("a");
            String name = item.getName();
            if (name == null) {
                // FIXME: Should we really give names for items with no
                // FIXME: name? (And if so does "Untitled" make sense?)
                // FIXME: Anyway, this would break with null values.
                name = "Untitled";
            }
            a.appendChild(d.createTextNode(name));
            String href = base + ItemResource.relativeUrl(item.getID());
            setAttribute(a, "href", href);
            li.appendChild(a);
            ulItems.appendChild(li);
        }
    } catch (SQLException e) {
        String errMsg = "SQLException while trying to items of the collection. " + e.getMessage();
        return errorInternal(context, errMsg);
    }

    Element form = d.createElement("form");
    form.setAttribute("enctype", "multipart/form-data");
    form.setAttribute("method", "post");
    makeInputRow(d, form, "title", "Title");
    makeInputRow(d, form, "lang", "Language");

    Element submitButton = d.createElement("input");
    submitButton.setAttribute("type", "submit");
    submitButton.setAttribute("value", "Create a new item");
    form.appendChild(submitButton);

    body.appendChild(form);

    try {
        if (context != null) {
            context.complete();
        }
    } catch (SQLException e) {
        log.log(Priority.INFO, e);
    }

    return representation;
}

From source file:com.twinsoft.convertigo.beans.common.XMLHttpHeaders.java

@Override
protected void appendToOutputDom(NodeList nodeList, Document outputDom) {

    Element doc = outputDom.getDocumentElement();
    Element httpHeaders = outputDom.createElement(tagName);
    Element headerElement;
    Header header;//from w  w w.j a  v  a 2 s . c  o  m
    int length = contextHeaders.length;

    for (int i = 0; i < length; i++) {
        if (!isRequestedObjectRunning())
            break;

        header = contextHeaders[i];
        headerElement = outputDom.createElement("header");
        headerElement.setAttribute("headerName", header.getName());
        headerElement.setAttribute("headerValue", header.getValue());
        httpHeaders.appendChild(headerElement);
        Engine.logBeans.trace("HttpHeader '" + headerElement.getAttribute("headerName") + " : "
                + headerElement.getAttribute("headerValue") + "' added to httpHeaders.");
    }
    doc.appendChild(httpHeaders);
    Engine.logBeans.trace("HttpHeaders added to result document.");
}

From source file:com.msopentech.odatajclient.engine.performance.BasicPerfTest.java

@Test
public void writeAtomViaLowerlevelLibs() throws ParserConfigurationException, ClassNotFoundException,
        InstantiationException, IllegalAccessException {

    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final Document doc = builder.newDocument();

    final Element entry = doc.createElement("entry");
    entry.setAttribute("xmlns", "http://www.w3.org/2005/Atom");
    entry.setAttribute("xmlns:m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
    entry.setAttribute("xmlns:d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
    entry.setAttribute("xmlns:gml", "http://www.opengis.net/gml");
    entry.setAttribute("xmlns:georss", "http://www.georss.org/georss");
    doc.appendChild(entry);/*from  w w  w  . j a  v  a  2 s.co m*/

    final Element category = doc.createElement("category");
    category.setAttribute("term", "Microsoft.Test.OData.Services.AstoriaDefaultService.Customer");
    category.setAttribute("scheme", "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme");
    entry.appendChild(category);

    final Element properties = doc.createElement("m:properties");
    entry.appendChild(properties);

    final Element name = doc.createElement("d:Name");
    name.setAttribute("m:type", "Edm.String");
    name.appendChild(doc.createTextNode("A name"));
    properties.appendChild(name);

    final Element customerId = doc.createElement("d:CustomerId");
    customerId.setAttribute("m:type", "Edm.Int32");
    customerId.appendChild(doc.createTextNode("0"));
    properties.appendChild(customerId);

    final Element bci = doc.createElement("d:BackupContactInfo");
    bci.setAttribute("m:type",
            "Collection(Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails)");
    properties.appendChild(bci);

    final Element topelement = doc.createElement("d:element");
    topelement.setAttribute("m:type", "Microsoft.Test.OData.Services.AstoriaDefaultService.ContactDetails");
    bci.appendChild(topelement);

    final Element altNames = doc.createElement("d:AlternativeNames");
    altNames.setAttribute("m:type", "Collection(Edm.String)");
    topelement.appendChild(altNames);

    final Element element1 = doc.createElement("d:element");
    element1.setAttribute("m:type", "Edm.String");
    element1.appendChild(doc.createTextNode("myname"));
    altNames.appendChild(element1);

    final Element emailBag = doc.createElement("d:EmailBag");
    emailBag.setAttribute("m:type", "Collection(Edm.String)");
    topelement.appendChild(emailBag);

    final Element element2 = doc.createElement("d:element");
    element2.setAttribute("m:type", "Edm.String");
    element2.appendChild(doc.createTextNode("myname@mydomain.com"));
    emailBag.appendChild(element2);

    final Element contactAlias = doc.createElement("d:ContactAlias");
    contactAlias.setAttribute("m:type", "Microsoft.Test.OData.Services.AstoriaDefaultService.Aliases");
    topelement.appendChild(contactAlias);

    final Element altNames2 = doc.createElement("d:AlternativeNames");
    altNames2.setAttribute("m:type", "Collection(Edm.String)");
    contactAlias.appendChild(altNames2);

    final Element element3 = doc.createElement("d:element");
    element3.setAttribute("m:type", "Edm.String");
    element3.appendChild(doc.createTextNode("myAlternativeName"));
    altNames2.appendChild(element3);

    final StringWriter writer = new StringWriter();

    final DOMImplementationRegistry reg = DOMImplementationRegistry.newInstance();
    final DOMImplementationLS impl = (DOMImplementationLS) reg.getDOMImplementation("LS");
    final LSSerializer serializer = impl.createLSSerializer();
    final LSOutput lso = impl.createLSOutput();
    lso.setCharacterStream(writer);
    serializer.write(doc, lso);

    assertFalse(writer.toString().isEmpty());
}

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

private Element createAndAppendElement(INode node) {
    String name = node.getLabel();
    Element elementTag = document.createElement(ExportXSD.PREFIX + "element");
    elementTag.setAttribute("name", name);
    checkOccurs(node, elementTag);/*from  www.ja  va2  s  .  co m*/
    stackOfElements.peek().appendChild(elementTag);
    verifyMaxOccurs(elementTag, node);
    return elementTag;
}

From source file:chat.viska.xmpp.plugins.webrtc.WebRtcPlugin.java

@Nonnull
public Completable closeSession(@Nonnull final Jid recipient, @Nonnull final String id) {
    final Document iq = Stanza.getIqTemplate(Stanza.IqType.SET, UUID.randomUUID().toString(),
            getSession().getNegotiatedJid(), recipient);
    final Element webrtcElement = (Element) iq.getDocumentElement()
            .appendChild(iq.createElementNS(XMLNS, "webrtc"));
    webrtcElement.setAttribute("id", id);
    webrtcElement.setAttribute("action", "close");
    return this.context.sendIq(new XmlWrapperStanza(iq)).getResponse().toSingle().toCompletable();
}

From source file:fi.helsinki.lib.simplerest.CollectionsResource.java

@Get("xml")
public Representation toXml() {
    Context c = null;/*from   w w w . j  a v  a 2 s  .c  o m*/
    Community community = null;
    DomRepresentation representation = null;
    Document d = null;
    try {
        c = new Context();
        community = Community.find(c, this.communityId);
        if (community == null) {
            return errorNotFound(c, "Could not find the community.");
        }

        representation = new DomRepresentation(MediaType.TEXT_HTML);
        d = representation.getDocument();
    } catch (Exception e) {
        return errorInternal(c, e.toString());
    }

    Element html = d.createElement("html");
    d.appendChild(html);

    Element head = d.createElement("head");
    html.appendChild(head);

    Element title = d.createElement("title");
    head.appendChild(title);
    title.appendChild(d.createTextNode("Community " + community.getName()));

    Element body = d.createElement("body");
    html.appendChild(body);

    Collection[] collections;
    try {
        collections = community.getCollections();
    } catch (SQLException e) {
        return errorInternal(c, e.toString());
    }

    String url = getRequest().getResourceRef().getIdentifier();
    url = url.substring(0, url.lastIndexOf('/', url.lastIndexOf('/', url.lastIndexOf('/') - 1) - 1));
    url += "/collection/";

    Element ulCollections = d.createElement("ul");
    setId(ulCollections, "collections");
    body.appendChild(ulCollections);
    for (Collection collection : collections) {
        Element li = d.createElement("li");
        Element a = d.createElement("a");
        String href = url + Integer.toString(collection.getID());
        setAttribute(a, "href", href);
        a.appendChild(d.createTextNode(collection.getName()));
        li.appendChild(a);
        ulCollections.appendChild(li);
    }

    Element form = d.createElement("form");
    form.setAttribute("enctype", "multipart/form-data");
    form.setAttribute("method", "post");
    makeInputRow(d, form, "name", "Name");
    makeInputRow(d, form, "short_description", "Short description");
    makeInputRow(d, form, "introductory_text", "Introductory text");
    makeInputRow(d, form, "copyright_text", "Copyright text");
    makeInputRow(d, form, "side_bar_text", "Side bar text");
    makeInputRow(d, form, "provenance_description", "Provenance Description");
    makeInputRow(d, form, "license", "License");
    makeInputRow(d, form, "logo", "Logo", "file");

    Element submitButton = d.createElement("input");
    submitButton.setAttribute("type", "submit");
    submitButton.setAttribute("value", "Create a new collection");
    form.appendChild(submitButton);

    body.appendChild(form);

    c.abort(); // Same as c.complete() because we didn't modify the db.

    return representation;
}

From source file:de.decidr.model.XmlToolsTest.java

private void fillSampleDocument() {
    Element root = SAMPLE_DOC.createElement("rootElement");
    root.appendChild(SAMPLE_DOC.createElement("subElement"));
    root.appendChild(SAMPLE_DOC.createElementNS("http://example.invalid", "namespaceElement"));
    root.setAttribute("attribute", "example");
    SAMPLE_DOC.appendChild(root);// w  w  w .j  av a  2 s . co m
}

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

public void visitAttributeNode(AttributeNode node) {
    if (!node.isVirtual()) {
        Element elementTag = document.createElement(ExportXSD.PREFIX + "element");
        checkLeafNode(node, elementTag);
        stackOfElements.peek().appendChild(elementTag);
        checkOccurs(node, elementTag);//from   w  w  w . j a va  2 s  . c  o m
        if (logger.isDebugEnabled())
            logger.debug(" attribute of: " + node.getLabel());
    } else {
        Element fatherElement = this.stackOfElements.peek();
        Element grandFatherElement = (Element) fatherElement.getParentNode();
        grandFatherElement.setAttribute("mixed", "true");
    }
}

From source file:com.hp.saas.agm.core.entity.EntityQuery.java

public synchronized Element toElement(String targetName) throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element element = doc.createElement(targetName);
    if (getName() != null) {
        element.setAttribute("name", getName());
    }//from  w  w w  .  j  ava2  s  . c o m
    Element filter = super.toElement("filter");
    element.appendChild(filter);
    Element cross = doc.createElement("crossFilter");
    Map<String, EntityCrossFilter> crossFilters = getCrossFilters();
    for (String alias : crossFilters.keySet()) {
        EntityCrossFilter f = crossFilters.get(alias);
        Element crossElement = f.toElement(f.getEntityType());
        if (!alias.equals(f.getEntityType())) {
            crossElement.setAttribute("alias", alias);
        }
        cross.appendChild(crossElement);
    }
    element.appendChild(cross);
    Element orderBy = doc.createElement("order");
    for (String name : order.keySet()) {
        Element child = doc.createElement(name);
        child.setAttribute("dir", order.get(name).name());
        orderBy.appendChild(child);
    }
    element.appendChild(orderBy);
    Element viewing = doc.createElement("view");
    for (String name : view.keySet()) {
        Element child = doc.createElement("column");
        child.setAttribute("name", name);
        child.setAttribute("width", String.valueOf(view.get(name)));
        viewing.appendChild(child);
    }
    element.appendChild(viewing);
    return element;
}