Example usage for org.dom4j Attribute getNamespaceURI

List of usage examples for org.dom4j Attribute getNamespaceURI

Introduction

In this page you can find the example usage for org.dom4j Attribute getNamespaceURI.

Prototype

String getNamespaceURI();

Source Link

Document

Returns the URI mapped to the namespace of this element if one exists otherwise an empty String is returned.

Usage

From source file:net.sf.saxon.dom4j.NodeWrapper.java

License:Mozilla Public License

/**
* Get the value of a given attribute of this node
* @param fingerprint The fingerprint of the attribute name
* @return the attribute value if it exists or null if not
*///from  w  ww.j  a v a 2  s . com

public String getAttributeValue(int fingerprint) {
    if (nodeKind == Type.ELEMENT) {
        Iterator list = ((Element) node).attributes().iterator();
        NamePool pool = docWrapper.getNamePool();
        while (list.hasNext()) {
            Attribute att = (Attribute) list.next();
            int nameCode = pool.allocate(att.getNamespacePrefix(), att.getNamespaceURI(), att.getName());
            if (fingerprint == (nameCode & 0xfffff)) {
                return att.getValue();
            }
        }
    }
    return null;
}

From source file:net.sf.saxon.option.dom4j.NodeWrapper.java

License:Mozilla Public License

/**
 * Get the string value of a given attribute of this node
 *
 * @param uri   the namespace URI of the attribute name. Supply the empty string for an attribute
 *              that is in no namespace//from  w ww.  j  ava2s. c  o  m
 * @param local the local part of the attribute name.
 * @return the attribute value if it exists, or null if it does not exist. Always returns null
 *         if this node is not an element.
 * @since 9.4
 */
public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) {
    if (nodeKind == Type.ELEMENT) {
        for (Object o : ((Element) node).attributes()) {
            Attribute att = (Attribute) o;
            if (att.getName().equals(local) && att.getNamespaceURI().equals(uri)) {
                return att.getValue();
            }
        }
    }
    return null;
}

From source file:org.orbeon.oxf.xforms.XFormsModelSchemaValidator.java

License:Open Source License

private boolean validateElement(final Element element, final Acceptor acceptor, final IDConstraintChecker icc,
        final boolean isReportErrors) {

    boolean isElementValid = true;

    // Create StartTagInfo
    final StartTagInfo startTagInfo;
    {/*from  ww w .  ja  v  a2 s  .  c o  m*/
        final String uri = element.getNamespaceURI();
        final String name = element.getName();
        final String qName = element.getQualifiedName();
        final List attributesList = element.attributes();
        final AttributesImpl attributes = new AttributesImpl();

        for (Object anAttributesList : attributesList) {
            final Attribute attribute = (Attribute) anAttributesList;
            final String attributeURI = attribute.getNamespaceURI();
            final String attributeName = attribute.getName();
            final String attributeQName = attribute.getQualifiedName();
            final String attributeValue = attribute.getValue();
            attributes.addAttribute(attributeURI, attributeName, attributeQName, null, attributeValue);
        }
        validationContext.setCurrentElement(element);
        startTagInfo = new StartTagInfo(uri, name, qName, attributes, validationContext);
    }

    final StringRef stringRef = new StringRef();

    // Get child acceptor
    final Acceptor childAcceptor;
    {
        Acceptor tempChildAcceptor = acceptor.createChildAcceptor(startTagInfo, null);
        if (tempChildAcceptor == null) {
            if (isReportErrors) {
                tempChildAcceptor = acceptor.createChildAcceptor(startTagInfo, stringRef);
                addSchemaError(element, stringRef.str);
                isElementValid = false;
            } else {
                return false;
            }
        }
        childAcceptor = tempChildAcceptor;
    }

    // Handle id errors
    if (icc != null && isReportErrors) {
        icc.onNextAcceptorReady(startTagInfo, childAcceptor, element);
        isElementValid &= handleIDErrors(icc);
    }

    // Validate children
    final DatatypeRef datatypeRef = new DatatypeRef();
    final boolean childrenValid = validateChildren(element, childAcceptor, startTagInfo, icc, datatypeRef,
            isReportErrors);
    if (!childrenValid) {
        if (isReportErrors)
            isElementValid = false;
        else
            return false;
    }

    // TODO: MSV doesn't allow getting the type if validity check fails. However, we would like to obtain datatype validity in XForms.
    if (!childAcceptor.isAcceptState(null)) {
        if (isReportErrors) {
            childAcceptor.isAcceptState(stringRef);
            addSchemaError(element, stringRef.str);
            isElementValid = false;
        } else {
            return false;
        }
    } else {
        // Attempt to set datatype name
        setDataType(datatypeRef, element);
    }

    // Handle id errors
    if (icc != null && isReportErrors) {
        icc.endElement(element, datatypeRef.types);
        isElementValid &= handleIDErrors(icc);
    }

    // Get back to parent acceptor
    if (!acceptor.stepForward(childAcceptor, null)) {
        if (isReportErrors) {
            acceptor.stepForward(childAcceptor, stringRef);
            addSchemaError(element, stringRef.str);
            isElementValid = false;
        } else {
            return false;
        }
    }

    if (isReportErrors) {
        // Element may be invalid or not
        return isElementValid;
    } else {
        // This element is valid
        return true;
    }
}

From source file:org.orbeon.oxf.xforms.XFormsModelSchemaValidator.java

License:Open Source License

/**
 * Validate an element following the XML Schema "lax" mode.
 *
 * @param element   element to validate//www.  j a  v a2s.  c  o  m
 */
private boolean validateElementLax(final Element element) {

    final String elementURI;
    final String elementName;

    // NOTE: We do some special processing for xsi:type to find if there is a type declared for it. If not, we do
    // lax processing. However, it is not clear whether we should apply lax processing in this case or not. Maybe if
    // an xsi:type is specified and not found, the element should just be invalid.
    // TODO: should pass true?
    final QName xsiType = Dom4jUtils.extractAttributeValueQName(element, XMLConstants.XSI_TYPE_QNAME, false);
    if (xsiType != null) {
        // Honor xsi:type
        elementURI = xsiType.getNamespaceURI();
        elementName = xsiType.getName();
    } else {
        // Use element name
        elementURI = element.getNamespaceURI();
        elementName = element.getName();
    }

    boolean isValid = true;
    {
        // Find expression for element type
        final Expression expression;
        {
            // Find schema for type namespace
            final XMLSchemaSchema schema = ((XMLSchemaGrammar) schemaGrammar).getByNamespace(elementURI);
            if (schema != null) {
                // Try to find the expression in the schema
                final ElementDeclExp elementDeclExp = schema.elementDecls.get(elementName);
                if (elementDeclExp != null) {
                    // Found element type
                    expression = elementDeclExp;
                } else if (xsiType != null) {
                    // Try also complex type
                    expression = schema.complexTypes.get(elementName);
                } else {
                    // No type found
                    expression = null;
                }
            } else {
                // No schema so no expression
                expression = null;
            }
        }

        if (expression != null) {
            // Found type for element, so validate element
            final Acceptor acceptor = documentDeclaration.createAcceptor();
            isValid &= validateElement(element, acceptor, null, true);
        } else {
            // Element does not have type, so try to validate attributes and children elements

            // Attributes
            if (false) {
                // TODO: find out way of validating an attribute only
                // TODO: should we also look at schema.attributeGroups?
                final List attributesList = element.attributes();
                for (final Iterator iterator = attributesList.iterator(); iterator.hasNext();) {
                    final Attribute attribute = (Attribute) iterator.next();
                    final String attributeURI = attribute.getNamespaceURI();
                    final String attributeName = attribute.getName();
                    //                        final String attributeQName = attribute.getQualifiedName();
                    //                        final String attributeValue = attribute.getValue();

                    // Find expression for element type
                    final Expression attributeExpression;
                    {
                        // Find schema for type namespace
                        final XMLSchemaSchema schema = ((XMLSchemaGrammar) schemaGrammar)
                                .getByNamespace(attributeURI);
                        if (schema != null) {
                            attributeExpression = schema.attributeDecls.get(attributeName);
                        } else {
                            attributeExpression = null;
                        }
                    }
                    if (attributeExpression != null) {
                        //                            final ExpressionAcceptor expressionAcceptor = new SimpleAcceptor(documentDeclaration, attributeExpression, null, null);
                        //                            // Validate attribute value
                        //                            final StringRef errorStringRef = new StringRef();
                        //                            final DatatypeRef datatypeRef = new DatatypeRef();
                        //
                        //                            if (!expressionAcceptor.onAttribute2(attributeURI, attributeName, attributeQName, attributeValue, validationContext, errorStringRef, datatypeRef)) {
                        //                                if (errorStringRef.str == null) // not sure if this can happen
                        //                                    errorStringRef.str = "Error validating attribute";
                        //                                addSchemaError(attribute, errorStringRef.str);
                        //                            }

                        //                            if (!expressionAcceptor.onText2(attributeValue, validationContext, errorStringRef, datatypeRef)) {
                        //                                if (errorStringRef.str == null) // not sure if this can happen
                        //                                    errorStringRef.str = "Error validating attribute";
                        //                                addSchemaError(attribute, errorStringRef.str);
                        //                            }
                        //
                        //                            // Check final acceptor state
                        //                            if (!expressionAcceptor.isAcceptState(errorStringRef)) {
                        //                                if (errorStringRef.str == null) // not sure if this can happen
                        //                                    errorStringRef.str = "Error validating attribute";
                        //                                addSchemaError(attribute, errorStringRef.str);
                        //                            }
                    }
                }
            }

            // Validate children elements
            for (final Iterator iterator = element.elementIterator(); iterator.hasNext();) {
                final Element childElement = (Element) iterator.next();
                isValid &= validateElementLax(childElement);
            }
        }
    }
    return isValid;
}

From source file:org.orbeon.oxf.xml.XMLUtils.java

License:Open Source License

/**
 * Convert dom4j attributes to SAX attributes.
 *
 * @param element   dom4j Element// w  w  w.  j  a v  a2  s  .c o m
 * @return          SAX Attributes
 */
public static AttributesImpl getSAXAttributes(Element element) {
    final AttributesImpl result = new AttributesImpl();
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        final org.dom4j.Attribute attribute = (org.dom4j.Attribute) i.next();

        result.addAttribute(attribute.getNamespaceURI(), attribute.getName(), attribute.getQualifiedName(),
                XMLReceiverHelper.CDATA, attribute.getValue());
    }
    return result;
}