Example usage for org.w3c.dom Attr setPrefix

List of usage examples for org.w3c.dom Attr setPrefix

Introduction

In this page you can find the example usage for org.w3c.dom Attr setPrefix.

Prototype

public void setPrefix(String prefix) throws DOMException;

Source Link

Document

The namespace prefix of this node, or null if it is unspecified.

Usage

From source file:com.marklogic.dom.AttrImpl.java

protected Node cloneNode(Document doc, boolean deep) {
    Attr attr = doc.createAttributeNS(getNamespaceURI(), getLocalName());
    attr.setValue(getValue());// w  w  w .j a va 2s.  com
    attr.setPrefix(getPrefix());
    return attr;
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

private Attr createAttribute(Node parentNode, Document domDocument) {
    // Ensure xsi prefix is declared
    if (attributeName.indexOf(':') > 0) {
        String attributePrefix = StringUtils.substringBefore(attributeName, ":"); //$NON-NLS-1$
        String namespaceURI = domDocument.lookupNamespaceURI(attributePrefix);
        if (namespaceURI == null) {
            if ("xsi".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:xsi", XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); //$NON-NLS-1$
            } else if ("tmdm".equals(attributePrefix)) { //$NON-NLS-1$
                domDocument.getDocumentElement().setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                        "xmlns:tmdm", SkipAttributeDocumentBuilder.TALEND_NAMESPACE); //$NON-NLS-1$
            } else {
                throw new IllegalArgumentException("Unrecognized attribute prefix: '" + attributePrefix + "'."); //$NON-NLS-1$ //$NON-NLS-2$
            }/*from   w  w  w .  j a  v a 2 s . c  o m*/
        }
    }
    QName qName = getQName(domDocument);
    Attr newAttribute = domDocument.createAttributeNS(qName.getNamespaceURI(), qName.getLocalPart());
    String prefix = qName.getPrefix();
    newAttribute.setPrefix(prefix);
    parentNode.getAttributes().setNamedItemNS(newAttribute);
    return newAttribute;
}

From source file:cc.siara.csv_ml.ParsedObject.java

/**
 * Sets data attribute. In case of W3C Document, sets value of W3C
 * Attribute. In case of JSON, sets new JSONString
 * //from w ww  .  j a  v a2  s  .c  o  m
 * @param col_name
 *            Name of attribute
 * @param value
 *            Value of attribute
 */
public void setAttribute(String col_name, String value) {
    if (target == TARGET_W3C_DOC) {
        if (cur_element.getNodeType() == Node.ELEMENT_NODE) {
            int cIdx = col_name.indexOf(':');
            if (cIdx == -1)
                ((Element) cur_element).setAttribute(col_name, value);
            else {
                String ns = col_name.substring(0, cIdx);
                if (ns.equals("xmlns")) {
                    nsMap.put(col_name.substring(cIdx + 1), value);
                } else {
                    String nsURI = nsMap.get(ns);
                    if (nsURI == null) {
                        pendingAttributes.put(col_name, value);
                    } else {
                        Attr attr = doc.createAttributeNS(nsURI, col_name.substring(cIdx + 1));
                        attr.setPrefix(ns);
                        attr.setValue(value);
                        ((Element) cur_element).setAttributeNodeNS(attr);
                    }
                }
            }
        }
    } else
        cur_jo.put(col_name, value);
}

From source file:cc.siara.csv_ml.ParsedObject.java

/**
 * Performas any pending activity against an element to finalize it. In this
 * case, it adds any pending attributes needing namespace mapping.
 * // www .  ja  va2  s.  c  o m
 * Also if the node has a namespace attached, it recreates the node with
 * specific URI.
 */
public void finalizeElement() {
    // Add all remaining attributes
    for (String col_name : pendingAttributes.keySet()) {
        String value = pendingAttributes.get(col_name);
        int cIdx = col_name.indexOf(':');
        String ns = col_name.substring(0, cIdx);
        String nsURI = nsMap.get(ns);
        if (nsURI == null)
            nsURI = generalNSURI;
        Attr attr = doc.createAttributeNS(nsURI, col_name.substring(cIdx + 1));
        attr.setPrefix(ns);
        attr.setValue(value);
        ((Element) cur_element).setAttributeNodeNS(attr);
    }
    // If the element had a namespace prefix, it has to be recreated.
    if (!currentElementNS.equals("") && !doc.getDocumentElement().equals(cur_element)) {
        Node parent = cur_element.getParentNode();
        Element cur_ele = (Element) parent.removeChild(cur_element);
        String node_name = cur_ele.getNodeName();
        String nsURI = nsMap.get(currentElementNS);
        if (nsURI == null)
            nsURI = generalNSURI;
        Element new_node = doc.createElementNS(nsURI, currentElementNS + ":" + node_name);
        parent.appendChild(new_node);
        // Add all attributes
        NamedNodeMap attrs = cur_ele.getAttributes();
        while (attrs.getLength() > 0) {
            Attr attr = (Attr) attrs.item(0);
            cur_ele.removeAttributeNode(attr);
            nsURI = attr.getNamespaceURI();
            new_node.setAttributeNodeNS(attr);
        }
        // Add all CData sections
        NodeList childNodes = cur_ele.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.CDATA_SECTION_NODE)
                new_node.appendChild(node);
        }
        cur_element = new_node;
    }
    pendingAttributes = new Hashtable<String, String>();
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static void setQNameAttribute(Element element, QName attributeName, QName attributeValue) {
    Document doc = element.getOwnerDocument();
    Attr attr = doc.createAttributeNS(attributeName.getNamespaceURI(), attributeName.getLocalPart());
    String namePrefix = lookupOrCreateNamespaceDeclaration(element, attributeName.getNamespaceURI(),
            attributeName.getPrefix(), element, true);
    attr.setPrefix(namePrefix);
    setQNameAttribute(element, attr, attributeValue, element);
}

From source file:com.evolveum.midpoint.util.DOMUtil.java

public static void setQNameAttribute(Element element, QName attributeName, QName attributeValue,
        Element definitionElement) {
    Document doc = element.getOwnerDocument();
    Attr attr = doc.createAttributeNS(attributeName.getNamespaceURI(), attributeName.getLocalPart());
    String namePrefix = lookupOrCreateNamespaceDeclaration(element, attributeName.getNamespaceURI(),
            attributeName.getPrefix(), element, true);
    attr.setPrefix(namePrefix);
    setQNameAttribute(element, attr, attributeValue, definitionElement);
}

From source file:com.fujitsu.dc.common.auth.token.TransCellAccessToken.java

/**
 * ?SAML????./*from  w  ww .j a  va 2 s  . c o  m*/
 * @return SAML
 */
public String toSamlString() {

    /*
     * Creation of SAML2.0 Document
     * http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
     */

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        builder = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // ????????????
        throw new RuntimeException(e);
    }
    Document doc = builder.newDocument();
    Element assertion = doc.createElementNS(URN_OASIS_NAMES_TC_SAML_2_0_ASSERTION, "Assertion");
    doc.appendChild(assertion);
    assertion.setAttribute("ID", this.id);
    assertion.setAttribute("Version", "2.0");

    // Dummy Date
    DateTime dateTime = new DateTime(this.issuedAt);

    assertion.setAttribute("IssueInstant", dateTime.toString());

    // Issuer
    Element issuer = doc.createElement("Issuer");
    issuer.setTextContent(this.issuer);
    assertion.appendChild(issuer);

    // Subject
    Element subject = doc.createElement("Subject");
    Element nameId = doc.createElement("NameID");
    nameId.setTextContent(this.subject);
    Element subjectConfirmation = doc.createElement("SubjectConfirmation");
    subject.appendChild(nameId);
    subject.appendChild(subjectConfirmation);
    assertion.appendChild(subject);

    // Conditions
    Element conditions = doc.createElement("Conditions");
    Element audienceRestriction = doc.createElement("AudienceRestriction");
    for (String aud : new String[] { this.target, this.schema }) {
        Element audience = doc.createElement("Audience");
        audience.setTextContent(aud);
        audienceRestriction.appendChild(audience);
    }
    conditions.appendChild(audienceRestriction);
    assertion.appendChild(conditions);

    // AuthnStatement
    Element authnStmt = doc.createElement("AuthnStatement");
    authnStmt.setAttribute("AuthnInstant", dateTime.toString());
    Element authnCtxt = doc.createElement("AuthnContext");
    Element authnCtxtCr = doc.createElement("AuthnContextClassRef");
    authnCtxtCr.setTextContent("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");
    authnCtxt.appendChild(authnCtxtCr);
    authnStmt.appendChild(authnCtxt);
    assertion.appendChild(authnStmt);

    // AttributeStatement
    Element attrStmt = doc.createElement("AttributeStatement");
    Element attribute = doc.createElement("Attribute");
    for (Role role : this.roleList) {
        Element attrValue = doc.createElement("AttributeValue");
        Attr attr = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type");
        attr.setPrefix("xsi");
        attr.setValue("string");
        attrValue.setAttributeNodeNS(attr);
        attrValue.setTextContent(role.schemeCreateUrlForTranceCellToken(this.issuer));
        attribute.appendChild(attrValue);
    }
    attrStmt.appendChild(attribute);
    assertion.appendChild(attrStmt);

    // Normalization 
    doc.normalizeDocument();

    // Dsig??
    // Create a DOMSignContext and specify the RSA PrivateKey and
    // location of the resulting XMLSignature's parent element.
    DOMSignContext dsc = new DOMSignContext(privKey, doc.getDocumentElement());

    // Create the XMLSignature, but don't sign it yet.
    XMLSignature signature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo);

    // Marshal, generate, and sign the enveloped signature.
    try {
        signature.sign(dsc);
        // ?
        return DcCoreUtils.nodeToString(doc.getDocumentElement());
    } catch (MarshalException e1) {
        // DOM???????
        throw new RuntimeException(e1);
    } catch (XMLSignatureException e1) {
        // ??????????
        throw new RuntimeException(e1);
    }

    /*
     * ------------------------------------------------------------
     * http://tools.ietf.org/html/draft-ietf-oauth-saml2-bearer-10
     * ------------------------------------------------------------ 2.1. Using SAML Assertions as Authorization
     * Grants To use a SAML Bearer Assertion as an authorization grant, use the following parameter values and
     * encodings. The value of "grant_type" parameter MUST be "urn:ietf:params:oauth:grant-type:saml2-bearer" The
     * value of the "assertion" parameter MUST contain a single SAML 2.0 Assertion. The SAML Assertion XML data MUST
     * be encoded using base64url, where the encoding adheres to the definition in Section 5 of RFC4648 [RFC4648]
     * and where the padding bits are set to zero. To avoid the need for subsequent encoding steps (by "application/
     * x-www-form-urlencoded" [W3C.REC-html401-19991224], for example), the base64url encoded data SHOULD NOT be
     * line wrapped and pad characters ("=") SHOULD NOT be included.
     */
}

From source file:io.personium.common.auth.token.TransCellAccessToken.java

/**
 * ?SAML????./*w w  w  . j ava  2 s  . co m*/
 * @return SAML
 */
public String toSamlString() {

    /*
     * Creation of SAML2.0 Document
     * http://docs.oasis-open.org/security/saml/v2.0/saml-core-2.0-os.pdf
     */

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        builder = dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // ????????????
        throw new RuntimeException(e);
    }
    Document doc = builder.newDocument();
    Element assertion = doc.createElementNS(URN_OASIS_NAMES_TC_SAML_2_0_ASSERTION, "Assertion");
    doc.appendChild(assertion);
    assertion.setAttribute("ID", this.id);
    assertion.setAttribute("Version", "2.0");

    // Dummy Date
    DateTime dateTime = new DateTime(this.issuedAt);

    assertion.setAttribute("IssueInstant", dateTime.toString());

    // Issuer
    Element issuer = doc.createElement("Issuer");
    issuer.setTextContent(this.issuer);
    assertion.appendChild(issuer);

    // Subject
    Element subject = doc.createElement("Subject");
    Element nameId = doc.createElement("NameID");
    nameId.setTextContent(this.subject);
    Element subjectConfirmation = doc.createElement("SubjectConfirmation");
    subject.appendChild(nameId);
    subject.appendChild(subjectConfirmation);
    assertion.appendChild(subject);

    // Conditions
    Element conditions = doc.createElement("Conditions");
    Element audienceRestriction = doc.createElement("AudienceRestriction");
    for (String aud : new String[] { this.target, this.schema }) {
        Element audience = doc.createElement("Audience");
        audience.setTextContent(aud);
        audienceRestriction.appendChild(audience);
    }
    conditions.appendChild(audienceRestriction);
    assertion.appendChild(conditions);

    // AuthnStatement
    Element authnStmt = doc.createElement("AuthnStatement");
    authnStmt.setAttribute("AuthnInstant", dateTime.toString());
    Element authnCtxt = doc.createElement("AuthnContext");
    Element authnCtxtCr = doc.createElement("AuthnContextClassRef");
    authnCtxtCr.setTextContent("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");
    authnCtxt.appendChild(authnCtxtCr);
    authnStmt.appendChild(authnCtxt);
    assertion.appendChild(authnStmt);

    // AttributeStatement
    Element attrStmt = doc.createElement("AttributeStatement");
    Element attribute = doc.createElement("Attribute");
    for (Role role : this.roleList) {
        Element attrValue = doc.createElement("AttributeValue");
        Attr attr = doc.createAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "type");
        attr.setPrefix("xsi");
        attr.setValue("string");
        attrValue.setAttributeNodeNS(attr);
        attrValue.setTextContent(role.schemeCreateUrlForTranceCellToken(this.issuer));
        attribute.appendChild(attrValue);
    }
    attrStmt.appendChild(attribute);
    assertion.appendChild(attrStmt);

    // Normalization 
    doc.normalizeDocument();

    // Dsig??
    // Create a DOMSignContext and specify the RSA PrivateKey and
    // location of the resulting XMLSignature's parent element.
    DOMSignContext dsc = new DOMSignContext(privKey, doc.getDocumentElement());

    // Create the XMLSignature, but don't sign it yet.
    XMLSignature signature = xmlSignatureFactory.newXMLSignature(signedInfo, keyInfo);

    // Marshal, generate, and sign the enveloped signature.
    try {
        signature.sign(dsc);
        // ?
        return PersoniumCoreUtils.nodeToString(doc.getDocumentElement());
    } catch (MarshalException e1) {
        // DOM???????
        throw new RuntimeException(e1);
    } catch (XMLSignatureException e1) {
        // ??????????
        throw new RuntimeException(e1);
    }

    /*
     * ------------------------------------------------------------
     * http://tools.ietf.org/html/draft-ietf-oauth-saml2-bearer-10
     * ------------------------------------------------------------ 2.1. Using SAML Assertions as Authorization
     * Grants To use a SAML Bearer Assertion as an authorization grant, use the following parameter values and
     * encodings. The value of "grant_type" parameter MUST be "urn:ietf:params:oauth:grant-type:saml2-bearer" The
     * value of the "assertion" parameter MUST contain a single SAML 2.0 Assertion. The SAML Assertion XML data MUST
     * be encoded using base64url, where the encoding adheres to the definition in Section 5 of RFC4648 [RFC4648]
     * and where the padding bits are set to zero. To avoid the need for subsequent encoding steps (by "application/
     * x-www-form-urlencoded" [W3C.REC-html401-19991224], for example), the base64url encoded data SHOULD NOT be
     * line wrapped and pad characters ("=") SHOULD NOT be included.
     */
}

From source file:de.escidoc.core.test.EscidocTestBase.java

/**
 * Creates a new attribute node for the provided document.
 * //w ww  . ja va 2 s. co m
 * @param doc
 *            The document for that the node shall be created.
 * @param namespaceUri
 *            The name space uri of the node to create. This may be null.
 * @param prefix
 *            The prefix to use.
 * @param tagName
 *            The tag name of the node.
 * @param value
 *            The attribute value.
 * @return Returns the created node.
 * @throws Exception
 *             Thrown if anything fails.
 */
public static Attr createAttributeNode(final Document doc, final String namespaceUri, final String prefix,
        final String tagName, final String value) throws Exception {

    Attr newAttribute = doc.createAttributeNS(namespaceUri, tagName);
    newAttribute.setPrefix(prefix);
    newAttribute.setValue(value);
    if (value != null) {
        newAttribute.setTextContent(value);
    }
    return newAttribute;
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.xml.XmlFilterReader.java

private Attr bufferAttribute(Element element, Attribute attribute) {
    QName name = attribute.getName();
    String prefix = name.getPrefix();
    String uri = name.getNamespaceURI();
    Attr node;
    if (uri == null || uri.isEmpty()) {
        node = document.createAttribute(name.getLocalPart());
        element.setAttributeNode(node);// w  w w. j a  v  a2 s  . c om
    } else {
        node = document.createAttributeNS(uri, name.getLocalPart());
        if (prefix != null && !prefix.isEmpty()) {
            node.setPrefix(prefix);
        }
        element.setAttributeNodeNS(node);
    }
    node.setTextContent(attribute.getValue());
    return node;
}