Example usage for org.w3c.dom Attr getOwnerElement

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

Introduction

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

Prototype

public Element getOwnerElement();

Source Link

Document

The Element node this attribute is attached to or null if this attribute is not in use.

Usage

From source file:org.apache.cxf.systest.aegis.mtom.MtomTest.java

@Test
public void testMtomSchema() throws Exception {
    testUtilities.setBus((Bus) applicationContext.getBean("cxf"));
    testUtilities.addDefaultNamespaces();
    testUtilities.addNamespace("xmime", "http://www.w3.org/2005/05/xmlmime");
    Server s = testUtilities.getServerForService(
            new QName("http://fortest.mtom.aegis.systest.cxf.apache.org/", "MtomTestService"));
    Document wsdl = testUtilities.getWSDLDocument(s);
    Assert.assertNotNull(wsdl);//w  w w  .  j a va 2  s  . c  o m
    NodeList typeAttrList = testUtilities.assertValid("//xsd:complexType[@name='inputDhBean']/xsd:sequence/"
            + "xsd:element[@name='dataHandler']/" + "@type", wsdl);
    Attr typeAttr = (Attr) typeAttrList.item(0);
    String typeAttrValue = typeAttr.getValue();
    // now, this thing is a qname with a :, and we have to work out if it's correct.
    String[] pieces = typeAttrValue.split(":");
    Assert.assertEquals("base64Binary", pieces[1]);
    Node elementNode = typeAttr.getOwnerElement();
    String url = testUtilities.resolveNamespacePrefix(pieces[0], elementNode);
    Assert.assertEquals(Constants.URI_2001_SCHEMA_XSD, url);

    s = testUtilities.getServerForAddress("http://localhost:" + PORT + "/mtomXmime");
    wsdl = testUtilities.getWSDLDocument(s);
    Assert.assertNotNull(wsdl);
    typeAttrList = testUtilities.assertValid("//xsd:complexType[@name='inputDhBean']/xsd:sequence/"
            + "xsd:element[@name='dataHandler']/" + "@type", wsdl);
    typeAttr = (Attr) typeAttrList.item(0);
    typeAttrValue = typeAttr.getValue();
    // now, this thing is a qname with a :, and we have to work out if it's correct.
    pieces = typeAttrValue.split(":");
    Assert.assertEquals("base64Binary", pieces[1]);
    elementNode = typeAttr.getOwnerElement();
    url = testUtilities.resolveNamespacePrefix(pieces[0], elementNode);
    Assert.assertEquals(AbstractXOPType.XML_MIME_NS, url);

    /* when I add a test for a custom mapping.
    testUtilities.assertValid("//xsd:complexType[@name='inputDhBean']/xsd:sequence/"
                          + "xsd:element[@name='dataHandler']/"
                          + "@xmime:expectedContentType/text()",
                          wsdl);
                          */
}

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

private void processBufferedElement(Level level, UrlRewriteFilterGroupDescriptor config)
        throws XPathExpressionException {
    for (UrlRewriteFilterPathDescriptor selector : config.getSelectors()) {
        if (selector instanceof UrlRewriteFilterApplyDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterApplyDescriptor apply = (UrlRewriteFilterApplyDescriptor) selector;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    String value = element.getTextContent();
                    value = filterText(extractQName(element), value, apply.rule());
                    element.setTextContent(value);
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    String value = text.getWholeText();
                    value = filterText(extractQName(text.getParentNode()), value, apply.rule());
                    text.replaceWholeText(value);
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    String value = attr.getValue();
                    value = filterAttribute(extractQName(attr.getOwnerElement()), extractQName(attr), value,
                            apply.rule());
                    attr.setValue(value);
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }//ww w.j a  v  a  2  s  .  c  o m
            }
        } else if (selector instanceof UrlRewriteFilterDetectDescriptor) {
            XPathExpression path = (XPathExpression) selector.compiledPath(XPATH_COMPILER);
            Object node = path.evaluate(level.scopeNode, XPathConstants.NODE);
            if (node != null) {
                UrlRewriteFilterDetectDescriptor detect = (UrlRewriteFilterDetectDescriptor) selector;
                String value = null;
                if (node instanceof Element) {
                    Element element = (Element) node;
                    value = element.getTextContent();
                } else if (node instanceof Text) {
                    Text text = (Text) node;
                    value = text.getWholeText();
                } else if (node instanceof Attr) {
                    Attr attr = (Attr) node;
                    value = attr.getValue();
                } else {
                    throw new IllegalArgumentException(RES.unexpectedSelectedNodeType(node));
                }
                if (detect.compiledValue(REGEX_COMPILER).matcher(value).matches()) {
                    processBufferedElement(level, detect);
                }
            }
        } else {
            throw new IllegalArgumentException(RES.unexpectedRewritePathSelector(selector));
        }
    }
}

From source file:org.apache.openejb.server.axis.assembler.CommonsSchemaInfoBuilder.java

/**
 * Extract the nested component type of an Array from the XML Schema Type.
 *
 * @return the QName of the nested component type or null if the schema type can not be determined
 * @throws org.apache.openejb.OpenEJBException if the XML Schema Type can not represent an Array @param complexType
 *///from ww w .  j  a v  a2s.  c  o  m
private static QName extractSoapArrayComponentType(XmlSchemaComplexType complexType) {
    // Soap arrays are based on complex content restriction
    if (!isSoapArray(complexType)) {
        return null;
    }

    XmlSchemaComplexContentRestriction restriction = (XmlSchemaComplexContentRestriction) complexType
            .getContentModel().getContent();

    //First, handle case that looks like this:
    // <complexType name="ArrayOfstring">
    //     <complexContent>
    //         <restriction base="soapenc:Array">
    //             <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]"/>
    //         </restriction>
    //     </complexContent>
    // </complexType>
    XmlSchemaObjectCollection attributes = restriction.getAttributes();
    for (Iterator iterator = attributes.getIterator(); iterator.hasNext();) {
        Object item = iterator.next();
        if (item instanceof XmlSchemaAttribute) {
            XmlSchemaAttribute attribute = (XmlSchemaAttribute) item;
            if (attribute.getRefName().equals(SOAP_ARRAY_TYPE)) {
                for (Attr attr : attribute.getUnhandledAttributes()) {
                    QName attQName = new QName(attr.getNamespaceURI(), attr.getLocalName());
                    if (WSDL_ARRAY_TYPE.equals(attQName)) {
                        // value is a namespace prefixed xsd type
                        String value = attr.getValue();

                        // extract local part
                        int pos = value.lastIndexOf(":");
                        QName componentType;
                        if (pos < 0) {
                            componentType = new QName("", value);
                        } else {
                            String localPart = value.substring(pos + 1);

                            // resolve the namespace prefix
                            String prefix = value.substring(0, pos);
                            String namespace = getNamespaceForPrefix(prefix, attr.getOwnerElement());

                            componentType = new QName(namespace, localPart);
                        }
                        log.debug("determined component type from element type");
                        return componentType;
                    }
                }
            }
        }
    }

    // If that didn't work, try to handle case like this:
    // <complexType name="ArrayOfstring1">
    //     <complexContent>
    //         <restriction base="soapenc:Array">
    //             <sequence>
    //                 <element name="string1" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
    //             </sequence>
    //         </restriction>
    //     </complexContent>
    // </complexType>
    XmlSchemaParticle particle = restriction.getParticle();
    if (particle instanceof XmlSchemaSequence) {
        XmlSchemaSequence sequence = (XmlSchemaSequence) particle;
        if (sequence.getItems().getCount() != 1) {
            throw new IllegalArgumentException("more than one element inside array definition: " + complexType);
        }
        XmlSchemaObject item = sequence.getItems().getItem(0);
        if (item instanceof XmlSchemaElement) {
            XmlSchemaElement element = (XmlSchemaElement) item;
            QName componentType = element.getSchemaTypeName();
            log.debug("determined component type from element type");
            return componentType;
        }
    }

    return null;
}

From source file:org.apache.xml.security.utils.resolver.implementations.ResolverFragment.java

/**
 * Method engineResolve/*w  ww.  j  av a 2 s .  c  om*/
 *
 * @inheritDoc
 * @param uri
 * @param baseURI
 */
public XMLSignatureInput engineResolve(Attr uri, String baseURI) throws ResourceResolverException {

    String uriNodeValue = uri.getNodeValue();
    Document doc = uri.getOwnerElement().getOwnerDocument();

    Node selectedElem = null;
    if (uriNodeValue.equals("")) {
        /*
         * Identifies the node-set (minus any comment nodes) of the XML
         * resource containing the signature
         */
        if (log.isDebugEnabled()) {
            log.debug("ResolverFragment with empty URI (means complete document)");
        }
        selectedElem = doc;
    } else {
        /*
         * URI="#chapter1"
         * Identifies a node-set containing the element with ID attribute
         * value 'chapter1' of the XML resource containing the signature.
         * XML Signature (and its applications) modify this node-set to
         * include the element plus all descendents including namespaces and
         * attributes -- but not comments.
         */
        String id = uriNodeValue.substring(1);

        selectedElem = IdResolver.getElementById(doc, id);
        if (selectedElem == null) {
            Object exArgs[] = { id };
            throw new ResourceResolverException("signature.Verification.MissingID", exArgs, uri, baseURI);
        }
        if (log.isDebugEnabled()) {
            log.debug("Try to catch an Element with ID " + id + " and Element was " + selectedElem);
        }
    }

    XMLSignatureInput result = new XMLSignatureInput(selectedElem);
    result.setExcludeComments(true);

    result.setMIMEType("text/xml");
    if (baseURI != null && baseURI.length() > 0) {
        result.setSourceURI(baseURI.concat(uri.getNodeValue()));
    } else {
        result.setSourceURI(uri.getNodeValue());
    }
    return result;
}

From source file:org.apache.xml.security.utils.resolver.implementations.ResolverXPointer.java

/**
 * @inheritDoc// ww w . j  av a  2s .  c om
 */
public XMLSignatureInput engineResolve(Attr uri, String baseURI) throws ResourceResolverException {

    Node resultNode = null;
    Document doc = uri.getOwnerElement().getOwnerDocument();

    String uriStr = uri.getNodeValue();
    if (isXPointerSlash(uriStr)) {
        resultNode = doc;
    } else if (isXPointerId(uriStr)) {
        String id = getXPointerId(uriStr);
        resultNode = IdResolver.getElementById(doc, id);

        if (resultNode == null) {
            Object exArgs[] = { id };

            throw new ResourceResolverException("signature.Verification.MissingID", exArgs, uri, baseURI);
        }
    }

    XMLSignatureInput result = new XMLSignatureInput(resultNode);

    result.setMIMEType("text/xml");
    if (baseURI != null && baseURI.length() > 0) {
        result.setSourceURI(baseURI.concat(uri.getNodeValue()));
    } else {
        result.setSourceURI(uri.getNodeValue());
    }

    return result;
}

From source file:org.dita.dost.module.BranchFilterModule.java

/** Rewrite href or copy-to if duplicates exist. */
private void rewriteDuplicates(final Element root) {
    // collect href and copy-to
    final Map<URI, Map<Set<URI>, List<Attr>>> refs = new HashMap<>();
    for (final Element e : getTopicrefs(root)) {
        Attr attr = e.getAttributeNode(BRANCH_COPY_TO);
        if (attr == null) {
            attr = e.getAttributeNode(ATTRIBUTE_NAME_COPY_TO);
            if (attr == null) {
                attr = e.getAttributeNode(ATTRIBUTE_NAME_HREF);
            }//w  w  w .j av  a  2s .  com
        }
        if (attr != null) {
            final URI h = stripFragment(map.resolve(attr.getValue()));
            Map<Set<URI>, List<Attr>> attrsMap = refs.computeIfAbsent(h, k -> new HashMap<>());
            final Set<URI> currentFilter = getBranchFilters(e);
            List<Attr> attrs = attrsMap.computeIfAbsent(currentFilter, k -> new ArrayList<>());
            attrs.add(attr);
        }
    }
    // check and rewrite
    for (final Map.Entry<URI, Map<Set<URI>, List<Attr>>> ref : refs.entrySet()) {
        final Map<Set<URI>, List<Attr>> attrsMaps = ref.getValue();
        if (attrsMaps.size() > 1) {
            if (attrsMaps.containsKey(Collections.EMPTY_LIST)) {
                attrsMaps.remove(Collections.EMPTY_LIST);
            } else {
                Set<URI> first = attrsMaps.keySet().iterator().next();
                attrsMaps.remove(first);
            }
            int i = 1;
            for (final Map.Entry<Set<URI>, List<Attr>> attrsMap : attrsMaps.entrySet()) {
                final String suffix = "-" + i;
                final List<Attr> attrs = attrsMap.getValue();
                for (final Attr attr : attrs) {
                    final String gen = addSuffix(attr.getValue(), suffix);
                    logger.info(MessageUtils.getMessage("DOTJ065I", attr.getValue(), gen)
                            .setLocation(attr.getOwnerElement()).toString());
                    if (attr.getName().equals(BRANCH_COPY_TO)) {
                        attr.setValue(gen);
                    } else {
                        attr.getOwnerElement().setAttribute(BRANCH_COPY_TO, gen);
                    }

                    final URI dstUri = map.resolve(gen);
                    if (dstUri != null) {
                        final FileInfo hrefFileInfo = job.getFileInfo(currentFile.resolve(attr.getValue()));
                        if (hrefFileInfo != null) {
                            final URI newResult = addSuffix(hrefFileInfo.result, suffix);
                            final FileInfo.Builder dstBuilder = new FileInfo.Builder(hrefFileInfo).uri(dstUri)
                                    .result(newResult);
                            if (hrefFileInfo.format == null) {
                                dstBuilder.format(ATTR_FORMAT_VALUE_DITA);
                            }
                            final FileInfo dstFileInfo = dstBuilder.build();
                            job.add(dstFileInfo);
                        }
                    }
                }
                i++;
            }
        }
    }
}

From source file:org.jbpm.bpel.xml.util.XmlUtil.java

/**
 * Parses the value of the given attribute as a {@linkplain QName qualified name}.
 * @param attr the attribute whose value will be parsed
 * @return the attribute value as a {@link QName}
 *///  w  w  w.j  a v  a  2  s  .  co m
public static QName getQNameValue(Attr attr) {
    return parseQName(attr.getValue(), attr.getOwnerElement());
}

From source file:org.jbpm.bpel.xml.util.XmlUtil.java

public static void setQNameValue(Attr attr, QName value) {
    attr.setValue(formatQName(value, attr.getOwnerElement()));
}

From source file:org.jbpm.bpel.xml.util.XmlUtil.java

/**
 * Ensures the given namespace URI is associated to the given prefix in the scope of the given
 * attribute. If the namespace URI is <em>not</em> associated to the prefix, a namespace
 * declaration is {@linkplain #addNamespaceDeclaration(Element, String, String) added} to the
 * attribute's owner element. Otherwise, this call has no effect.
 * @param attribute the attribute to verify
 * @param namespaceURI the namespace URI to ensure; cannot be <code>null</code> or empty
 * @param prefix the prefix to ensure; cannot be <code>null</code> or empty
 *///from  w w w.  jav a  2  s.c om
public static void ensureNamespaceDeclared(Attr attribute, String namespaceURI, String prefix) {
    if (prefix == null || prefix.length() == 0)
        throw new IllegalArgumentException("prefix cannot be empty");

    if (namespaceURI == null || namespaceURI.length() == 0)
        throw new IllegalArgumentException("namespaceURI cannot be empty");

    Element element = attribute.getOwnerElement();
    if (!prefix.equals(getPrefix(namespaceURI, element))) {
        // prefix is associated with other/no URI, declare locally
        addNamespaceDeclaration(element, namespaceURI, prefix);
    }
}