Java Code Examples for org.w3c.dom.Element#setIdAttributeNode()

The following examples show how to use org.w3c.dom.Element#setIdAttributeNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DOMXMLSignature.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
DOMSignatureValue(Element sigValueElem, XMLCryptoContext context)
    throws MarshalException
{
    try {
        // base64 decode signatureValue
        value = Base64.decode(sigValueElem);
    } catch (Base64DecodingException bde) {
        throw new MarshalException(bde);
    }

    Attr attr = sigValueElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        sigValueElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }
    this.sigValueElem = sigValueElem;
}
 
Example 2
Source File: DOMXMLSignature.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
DOMSignatureValue(Element sigValueElem, XMLCryptoContext context)
    throws MarshalException
{
    try {
        // base64 decode signatureValue
        value = Base64.decode(sigValueElem);
    } catch (Base64DecodingException bde) {
        throw new MarshalException(bde);
    }

    Attr attr = sigValueElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        sigValueElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }
    this.sigValueElem = sigValueElem;
}
 
Example 3
Source File: DOMXMLSignature.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
DOMSignatureValue(Element sigValueElem, XMLCryptoContext context)
    throws MarshalException
{
    try {
        // base64 decode signatureValue
        value = Base64.decode(sigValueElem);
    } catch (Base64DecodingException bde) {
        throw new MarshalException(bde);
    }

    Attr attr = sigValueElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        sigValueElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }
    this.sigValueElem = sigValueElem;
}
 
Example 4
Source File: XMLHelpers.java    From SAMLRaider with MIT License 6 votes vote down vote up
/**
 * Set the ID Attribute in an XML Document so that java recognises the ID
 * Attribute as a real id
 *
 * @param document
 *            Document to set the ids
 */
public void setIDAttribute(Document document) {
	try {
		if(Thread.currentThread().getContextClassLoader() == null){
			Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); 
		}
		XPath xpath = XPathFactory.newInstance().newXPath();
		XPathExpression expr = xpath.compile("//*[@ID]");
		NodeList nodeList = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
		for (int i = 0; i < nodeList.getLength(); i++) {
			Element elem = (Element) nodeList.item(i);
			Attr attr = (Attr) elem.getAttributes().getNamedItem("ID");
			elem.setIdAttributeNode(attr, true);
		}
	} catch (XPathExpressionException e) {
		e.printStackTrace();
	}
}
 
Example 5
Source File: SignatureProperties.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructs {@link SignatureProperties} from {@link Element}
 * @param element <code>SignatureProperties</code> element
 * @param BaseURI the URI of the resource where the XML instance was stored
 * @throws XMLSecurityException
 */
public SignatureProperties(Element element, String BaseURI) throws XMLSecurityException {
    super(element, BaseURI);

    Attr attr = element.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        element.setIdAttributeNode(attr, true);
    }

    int length = getLength();
    for (int i = 0; i < length; i++) {
        Element propertyElem =
            XMLUtils.selectDsNode(this.constructionElement, Constants._TAG_SIGNATUREPROPERTY, i);
        Attr propertyAttr = propertyElem.getAttributeNodeNS(null, "Id");
        if (propertyAttr != null) {
            propertyElem.setIdAttributeNode(propertyAttr, true);
        }
    }
}
 
Example 6
Source File: SignatureProperties.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Constructs {@link SignatureProperties} from {@link Element}
 * @param element <code>SignatureProperties</code> element
 * @param BaseURI the URI of the resource where the XML instance was stored
 * @throws XMLSecurityException
 */
public SignatureProperties(Element element, String BaseURI) throws XMLSecurityException {
    super(element, BaseURI);

    Attr attr = element.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        element.setIdAttributeNode(attr, true);
    }

    int length = getLength();
    for (int i = 0; i < length; i++) {
        Element propertyElem =
            XMLUtils.selectDsNode(this.constructionElement, Constants._TAG_SIGNATUREPROPERTY, i);
        Attr propertyAttr = propertyElem.getAttributeNodeNS(null, "Id");
        if (propertyAttr != null) {
            propertyElem.setIdAttributeNode(propertyAttr, true);
        }
    }
}
 
Example 7
Source File: DOMSignatureProperties.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMSignatureProperties</code> from an element.
 *
 * @param propsElem a SignatureProperties element
 * @throws MarshalException if a marshalling error occurs
 */
public DOMSignatureProperties(Element propsElem, XMLCryptoContext context)
    throws MarshalException
{
    // unmarshal attributes
    Attr attr = propsElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        propsElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    NodeList nodes = propsElem.getChildNodes();
    int length = nodes.getLength();
    List<SignatureProperty> properties =
        new ArrayList<SignatureProperty>(length);
    for (int i = 0; i < length; i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            String name = child.getLocalName();
            if (!name.equals("SignatureProperty")) {
                throw new MarshalException("Invalid element name: " + name +
                                           ", expected SignatureProperty");
            }
            properties.add(new DOMSignatureProperty((Element)child,
                                                    context));
        }
    }
    if (properties.isEmpty()) {
        throw new MarshalException("properties cannot be empty");
    } else {
        this.properties = Collections.unmodifiableList(properties);
    }
}
 
Example 8
Source File: DOMSignatureProperties.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMSignatureProperties</code> from an element.
 *
 * @param propsElem a SignatureProperties element
 * @throws MarshalException if a marshalling error occurs
 */
public DOMSignatureProperties(Element propsElem, XMLCryptoContext context)
    throws MarshalException
{
    // unmarshal attributes
    Attr attr = propsElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        propsElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    NodeList nodes = propsElem.getChildNodes();
    int length = nodes.getLength();
    List<SignatureProperty> properties =
        new ArrayList<SignatureProperty>(length);
    for (int i = 0; i < length; i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            String name = child.getLocalName();
            if (!name.equals("SignatureProperty")) {
                throw new MarshalException("Invalid element name: " + name +
                                           ", expected SignatureProperty");
            }
            properties.add(new DOMSignatureProperty((Element)child,
                                                    context));
        }
    }
    if (properties.isEmpty()) {
        throw new MarshalException("properties cannot be empty");
    } else {
        this.properties = Collections.unmodifiableList(properties);
    }
}
 
Example 9
Source File: DOMManifest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMManifest</code> from an element.
 *
 * @param manElem a Manifest element
 */
public DOMManifest(Element manElem, XMLCryptoContext context,
                   Provider provider)
    throws MarshalException
{
    Attr attr = manElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        this.id = attr.getValue();
        manElem.setIdAttributeNode(attr, true);
    } else {
        this.id = null;
    }

    boolean secVal = Utils.secureValidation(context);

    Element refElem = DOMUtils.getFirstChildElement(manElem, "Reference");
    List<Reference> refs = new ArrayList<Reference>();
    refs.add(new DOMReference(refElem, context, provider));

    refElem = DOMUtils.getNextSiblingElement(refElem);
    while (refElem != null) {
        String localName = refElem.getLocalName();
        if (!localName.equals("Reference")) {
            throw new MarshalException("Invalid element name: " +
                                       localName + ", expected Reference");
        }
        refs.add(new DOMReference(refElem, context, provider));
        if (secVal && Policy.restrictNumReferences(refs.size())) {
            String error = "A maximum of " + Policy.maxReferences()
                + " references per Manifest are allowed when"
                + " secure validation is enabled";
            throw new MarshalException(error);
        }
        refElem = DOMUtils.getNextSiblingElement(refElem);
    }
    this.references = Collections.unmodifiableList(refs);
}
 
Example 10
Source File: DOMManifest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMManifest</code> from an element.
 *
 * @param manElem a Manifest element
 */
public DOMManifest(Element manElem, XMLCryptoContext context,
                   Provider provider)
    throws MarshalException
{
    Attr attr = manElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        this.id = attr.getValue();
        manElem.setIdAttributeNode(attr, true);
    } else {
        this.id = null;
    }

    boolean secVal = Utils.secureValidation(context);

    Element refElem = DOMUtils.getFirstChildElement(manElem, "Reference");
    List<Reference> refs = new ArrayList<Reference>();
    refs.add(new DOMReference(refElem, context, provider));

    refElem = DOMUtils.getNextSiblingElement(refElem);
    while (refElem != null) {
        String localName = refElem.getLocalName();
        if (!localName.equals("Reference")) {
            throw new MarshalException("Invalid element name: " +
                                       localName + ", expected Reference");
        }
        refs.add(new DOMReference(refElem, context, provider));
        if (secVal && Policy.restrictNumReferences(refs.size())) {
            String error = "A maximum of " + Policy.maxReferences()
                + " references per Manifest are allowed when"
                + " secure validation is enabled";
            throw new MarshalException(error);
        }
        refElem = DOMUtils.getNextSiblingElement(refElem);
    }
    this.references = Collections.unmodifiableList(refs);
}
 
Example 11
Source File: DOMSignatureProperty.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMSignatureProperty</code> from an element.
 *
 * @param propElem a SignatureProperty element
 */
public DOMSignatureProperty(Element propElem, XMLCryptoContext context)
    throws MarshalException
{
    // unmarshal attributes
    target = DOMUtils.getAttributeValue(propElem, "Target");
    if (target == null) {
        throw new MarshalException("target cannot be null");
    }
    Attr attr = propElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        propElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    NodeList nodes = propElem.getChildNodes();
    int length = nodes.getLength();
    List<XMLStructure> content = new ArrayList<XMLStructure>(length);
    for (int i = 0; i < length; i++) {
        content.add(new javax.xml.crypto.dom.DOMStructure(nodes.item(i)));
    }
    if (content.isEmpty()) {
        throw new MarshalException("content cannot be empty");
    } else {
        this.content = Collections.unmodifiableList(content);
    }
}
 
Example 12
Source File: DOMManifest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMManifest</code> from an element.
 *
 * @param manElem a Manifest element
 */
public DOMManifest(Element manElem, XMLCryptoContext context,
                   Provider provider)
    throws MarshalException
{
    Attr attr = manElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        this.id = attr.getValue();
        manElem.setIdAttributeNode(attr, true);
    } else {
        this.id = null;
    }

    boolean secVal = Utils.secureValidation(context);

    Element refElem = DOMUtils.getFirstChildElement(manElem, "Reference");
    List<Reference> refs = new ArrayList<Reference>();
    refs.add(new DOMReference(refElem, context, provider));

    refElem = DOMUtils.getNextSiblingElement(refElem);
    while (refElem != null) {
        String localName = refElem.getLocalName();
        if (!localName.equals("Reference")) {
            throw new MarshalException("Invalid element name: " +
                                       localName + ", expected Reference");
        }
        refs.add(new DOMReference(refElem, context, provider));
        if (secVal && (refs.size() > DOMSignedInfo.MAXIMUM_REFERENCE_COUNT)) {
            String error = "A maxiumum of " + DOMSignedInfo.MAXIMUM_REFERENCE_COUNT + " "
                + "references per Manifest are allowed with secure validation";
            throw new MarshalException(error);
        }
        refElem = DOMUtils.getNextSiblingElement(refElem);
    }
    this.references = Collections.unmodifiableList(refs);
}
 
Example 13
Source File: DOMSignatureProperty.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMSignatureProperty</code> from an element.
 *
 * @param propElem a SignatureProperty element
 */
public DOMSignatureProperty(Element propElem, XMLCryptoContext context)
    throws MarshalException
{
    // unmarshal attributes
    target = DOMUtils.getAttributeValue(propElem, "Target");
    if (target == null) {
        throw new MarshalException("target cannot be null");
    }
    Attr attr = propElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        propElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    NodeList nodes = propElem.getChildNodes();
    int length = nodes.getLength();
    List<XMLStructure> content = new ArrayList<XMLStructure>(length);
    for (int i = 0; i < length; i++) {
        content.add(new javax.xml.crypto.dom.DOMStructure(nodes.item(i)));
    }
    if (content.isEmpty()) {
        throw new MarshalException("content cannot be empty");
    } else {
        this.content = Collections.unmodifiableList(content);
    }
}
 
Example 14
Source File: DOMSignatureProperties.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMSignatureProperties</code> from an element.
 *
 * @param propsElem a SignatureProperties element
 * @throws MarshalException if a marshalling error occurs
 */
public DOMSignatureProperties(Element propsElem, XMLCryptoContext context)
    throws MarshalException
{
    // unmarshal attributes
    Attr attr = propsElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        propsElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    NodeList nodes = propsElem.getChildNodes();
    int length = nodes.getLength();
    List<SignatureProperty> properties =
        new ArrayList<SignatureProperty>(length);
    for (int i = 0; i < length; i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            String name = child.getLocalName();
            if (!name.equals("SignatureProperty")) {
                throw new MarshalException("Invalid element name: " + name +
                                           ", expected SignatureProperty");
            }
            properties.add(new DOMSignatureProperty((Element)child,
                                                    context));
        }
    }
    if (properties.isEmpty()) {
        throw new MarshalException("properties cannot be empty");
    } else {
        this.properties = Collections.unmodifiableList(properties);
    }
}
 
Example 15
Source File: DOMSignatureProperty.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a <code>DOMSignatureProperty</code> from an element.
 *
 * @param propElem a SignatureProperty element
 */
public DOMSignatureProperty(Element propElem, XMLCryptoContext context)
    throws MarshalException
{
    // unmarshal attributes
    target = DOMUtils.getAttributeValue(propElem, "Target");
    if (target == null) {
        throw new MarshalException("target cannot be null");
    }
    Attr attr = propElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        propElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    NodeList nodes = propElem.getChildNodes();
    int length = nodes.getLength();
    List<XMLStructure> content = new ArrayList<XMLStructure>(length);
    for (int i = 0; i < length; i++) {
        content.add(new javax.xml.crypto.dom.DOMStructure(nodes.item(i)));
    }
    if (content.isEmpty()) {
        throw new MarshalException("content cannot be empty");
    } else {
        this.content = Collections.unmodifiableList(content);
    }
}
 
Example 16
Source File: DOMKeyInfo.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a <code>DOMKeyInfo</code> from XML.
 *
 * @param kiElem KeyInfo element
 */
public DOMKeyInfo(Element kiElem, XMLCryptoContext context,
                  Provider provider)
    throws MarshalException
{
    // get Id attribute, if specified
    Attr attr = kiElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        id = attr.getValue();
        kiElem.setIdAttributeNode(attr, true);
    } else {
        id = null;
    }

    // get all children nodes
    NodeList nl = kiElem.getChildNodes();
    int length = nl.getLength();
    if (length < 1) {
        throw new MarshalException
            ("KeyInfo must contain at least one type");
    }
    List<XMLStructure> content = new ArrayList<XMLStructure>(length);
    for (int i = 0; i < length; i++) {
        Node child = nl.item(i);
        // ignore all non-Element nodes
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Element childElem = (Element)child;
        String localName = childElem.getLocalName();
        if (localName.equals("X509Data")) {
            content.add(new DOMX509Data(childElem));
        } else if (localName.equals("KeyName")) {
            content.add(new DOMKeyName(childElem));
        } else if (localName.equals("KeyValue")) {
            content.add(DOMKeyValue.unmarshal(childElem));
        } else if (localName.equals("RetrievalMethod")) {
            content.add(new DOMRetrievalMethod(childElem,
                                               context, provider));
        } else if (localName.equals("PGPData")) {
            content.add(new DOMPGPData(childElem));
        } else { //may be MgmtData, SPKIData or element from other namespace
            content.add(new javax.xml.crypto.dom.DOMStructure((childElem)));
        }
    }
    keyInfoTypes = Collections.unmodifiableList(content);
}
 
Example 17
Source File: DOMReference.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a <code>DOMReference</code> from an element.
 *
 * @param refElem a Reference element
 */
public DOMReference(Element refElem, XMLCryptoContext context,
                    Provider provider)
    throws MarshalException
{
    boolean secVal = Utils.secureValidation(context);

    // unmarshal Transforms, if specified
    Element nextSibling = DOMUtils.getFirstChildElement(refElem);
    List<Transform> transforms = new ArrayList<Transform>(5);
    if (nextSibling.getLocalName().equals("Transforms")) {
        Element transformElem = DOMUtils.getFirstChildElement(nextSibling,
                                                              "Transform");
        transforms.add(new DOMTransform(transformElem, context, provider));
        transformElem = DOMUtils.getNextSiblingElement(transformElem);
        while (transformElem != null) {
            String localName = transformElem.getLocalName();
            if (!localName.equals("Transform")) {
                throw new MarshalException(
                    "Invalid element name: " + localName +
                    ", expected Transform");
            }
            transforms.add
                (new DOMTransform(transformElem, context, provider));
            if (secVal && Policy.restrictNumTransforms(transforms.size())) {
                String error = "A maximum of " + Policy.maxTransforms()
                    + " transforms per Reference are allowed when"
                    + " secure validation is enabled";
                throw new MarshalException(error);
            }
            transformElem = DOMUtils.getNextSiblingElement(transformElem);
        }
        nextSibling = DOMUtils.getNextSiblingElement(nextSibling);
    }
    if (!nextSibling.getLocalName().equals("DigestMethod")) {
        throw new MarshalException("Invalid element name: " +
                                   nextSibling.getLocalName() +
                                   ", expected DigestMethod");
    }

    // unmarshal DigestMethod
    Element dmElem = nextSibling;
    this.digestMethod = DOMDigestMethod.unmarshal(dmElem);
    String digestMethodAlgorithm = this.digestMethod.getAlgorithm();
    if (secVal && Policy.restrictAlg(digestMethodAlgorithm)) {
        throw new MarshalException(
            "It is forbidden to use algorithm " + digestMethodAlgorithm +
            " when secure validation is enabled"
        );
    }

    // unmarshal DigestValue
    Element dvElem = DOMUtils.getNextSiblingElement(dmElem, "DigestValue");
    try {
        this.digestValue = Base64.decode(dvElem);
    } catch (Base64DecodingException bde) {
        throw new MarshalException(bde);
    }

    // check for extra elements
    if (DOMUtils.getNextSiblingElement(dvElem) != null) {
        throw new MarshalException(
            "Unexpected element after DigestValue element");
    }

    // unmarshal attributes
    this.uri = DOMUtils.getAttributeValue(refElem, "URI");

    Attr attr = refElem.getAttributeNodeNS(null, "Id");
    if (attr != null) {
        this.id = attr.getValue();
        refElem.setIdAttributeNode(attr, true);
    } else {
        this.id = null;
    }

    this.type = DOMUtils.getAttributeValue(refElem, "Type");
    this.here = refElem.getAttributeNodeNS(null, "URI");
    this.refElem = refElem;
    this.transforms = transforms;
    this.allTransforms = transforms;
    this.appliedTransformData = null;
    this.provider = provider;
}
 
Example 18
Source File: IdResolver.java    From jdk1.8-source-analysis with Apache License 2.0 2 votes vote down vote up
/**
 * Method registerElementById
 *
 * @param element the element to register
 * @param id the ID attribute
 */
public static void registerElementById(Element element, Attr id) {
    element.setIdAttributeNode(id, true);
}
 
Example 19
Source File: IdResolver.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method registerElementById
 *
 * @param element the element to register
 * @param id the ID attribute
 */
public static void registerElementById(Element element, Attr id) {
    element.setIdAttributeNode(id, true);
}
 
Example 20
Source File: IdResolver.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Method registerElementById
 *
 * @param element the element to register
 * @param id the ID attribute
 */
public static void registerElementById(Element element, Attr id) {
    element.setIdAttributeNode(id, true);
}