Example usage for org.dom4j Attribute getQualifiedName

List of usage examples for org.dom4j Attribute getQualifiedName

Introduction

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

Prototype

String getQualifiedName();

Source Link

Document

Returns the fully qualified name of this element.

Usage

From source file:com.shopximity.jpt.PageTemplateImpl.java

License:Open Source License

AttributesImpl getAttributes(Element element, Expressions expressions) throws PageTemplateException {
    AttributesImpl attributes = new AttributesImpl();
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        Attribute attribute = (Attribute) i.next();
        Namespace namespace = attribute.getNamespace();
        //String prefix = namespace.getPrefix();
        //System.err.println( "attribute: name=" + attribute.getName() + "\t" +
        //                    "qualified name=" + attribute.getQualifiedName() + "\t" +
        //                    "ns prefix=" + namespace.getPrefix() + "\t" +
        //                    "ns uri=" + namespace.getURI() );
        //String qualifiedName = attribute.getName();
        //String name = qualifiedName;
        //if ( qualifiedName.startsWith( prefix + ":" ) ) {
        //    name = qualifiedName.substring( prefix.length() + 1 );
        //}/*from   w w  w. j  a va  2  s. co  m*/
        String name = attribute.getName();

        // Handle JPT attributes
        //if ( prefix.equals( talNamespacePrefix ) ) {
        if (TAL_NAMESPACE_URI.equals(namespace.getURI())) {
            // tal:define
            if (name.equals("define")) {
                expressions.define = attribute.getValue();
            }

            // tal:condition
            else if (name.equals("condition")) {
                expressions.condition = attribute.getValue();
            }

            // tal:repeat
            else if (name.equals("repeat")) {
                expressions.repeat = attribute.getValue();
            }

            // tal:content
            else if (name.equals("content")) {
                expressions.content = attribute.getValue();
            }

            // tal:replace
            else if (name.equals("replace")) {
                if (expressions.omitTag == null) {
                    expressions.omitTag = "";
                }
                expressions.content = attribute.getValue();
            }

            // tal:attributes
            else if (name.equals("attributes")) {
                expressions.attributes = attribute.getValue();
            }

            // tal:omit-tag
            else if (name.equals("omit-tag")) {
                expressions.omitTag = attribute.getValue();
            }

            // error
            else {
                throw new PageTemplateException("unknown tal attribute: " + name);
            }
        }
        //else if ( prefix.equals( metalNamespacePrefix ) ) 
        else if (METAL_NAMESPACE_URI.equals(namespace.getURI())) {
            // metal:use-macro
            if (name.equals("use-macro")) {
                expressions.useMacro = attribute.getValue();
            }

            // metal:define-slot
            else if (name.equals("define-slot")) {
                expressions.defineSlot = attribute.getValue();
            }

            // metal:define-macro
            // metal:fill-slot
            else if (name.equals("define-macro") || name.equals("fill-slot")) {
                // these are ignored here, as they don't affect processing of current
                // template, but are called from other templates
            }

            // error
            else {
                throw new PageTemplateException("unknown metal attribute: " + name);
            }
        }

        // Pass on all other attributes
        else {
            //String qualifiedName = namespace.getPrefix() + ":" + name;
            attributes.addAttribute(namespace.getURI(), name, attribute.getQualifiedName(), "CDATA",
                    attribute.getValue());
            //attributes.addAttribute( getNamespaceURIFromPrefix(prefix), name, qualifiedName, "CDATA", attribute.getValue() );
        }
    }
    return attributes;
}

From source file:com.webslingerz.jpt.PageTemplateImpl.java

License:Open Source License

AttributesImpl getAttributes(Element element, Expressions expressions) throws PageTemplateException {
    AttributesImpl attributes = new AttributesImpl();
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        Attribute attribute = (Attribute) i.next();
        Namespace namespace = attribute.getNamespace();
        Namespace elementNamespace = element.getNamespace();
        if (!namespace.hasContent() && elementNamespace.hasContent())
            namespace = elementNamespace;
        // String prefix = namespace.getPrefix();
        // System.err.println( "attribute: name=" + attribute.getName() +
        // "\t" +
        // "qualified name=" + attribute.getQualifiedName() + "\t" +
        // "ns prefix=" + namespace.getPrefix() + "\t" +
        // "ns uri=" + namespace.getURI() );
        // String qualifiedName = attribute.getName();
        // String name = qualifiedName;
        // if ( qualifiedName.startsWith( prefix + ":" ) ) {
        // name = qualifiedName.substring( prefix.length() + 1 );
        // }/*  w  w w  .  jav  a  2 s.  co  m*/
        String name = attribute.getName();

        // Handle JPT attributes
        // if ( prefix.equals( talNamespacePrefix ) ) {
        if (TAL_NAMESPACE_URI.equals(namespace.getURI())
                || (!strict && TAL_NAMESPACE_PREFIX.equals(namespace.getPrefix()))) {

            // tal:define
            if (name.equals("define")) {
                expressions.define = attribute.getValue();
            }

            // tal:condition
            else if (name.equals("condition")) {
                expressions.condition = attribute.getValue();
            }

            // tal:repeat
            else if (name.equals("repeat")) {
                expressions.repeat = attribute.getValue();
            }

            // tal:content
            else if (name.equals("content")) {
                expressions.content = attribute.getValue();
            }

            // tal:replace
            else if (name.equals("replace")) {
                if (expressions.omitTag == null) {
                    expressions.omitTag = "";
                }
                expressions.content = attribute.getValue();
            }

            // tal:attributes
            else if (name.equals("attributes")) {
                expressions.attributes = attribute.getValue();
            }

            // tal:omit-tag
            else if (name.equals("omit-tag")) {
                expressions.omitTag = attribute.getValue();
            }

            // error
            else {
                throw new PageTemplateException("unknown tal attribute: " + name);
            }
        }
        // else if ( prefix.equals( metalNamespacePrefix ) )
        else if (METAL_NAMESPACE_URI.equals(namespace.getURI())
                || (!strict && METAL_NAMESPACE_PREFIX.equals(namespace.getPrefix()))) {

            // metal:use-macro
            if (name.equals("use-macro")) {
                expressions.useMacro = attribute.getValue();
            }

            // metal:define-slot
            else if (name.equals("define-slot")) {
                expressions.defineSlot = attribute.getValue();
            }

            // metal:define-macro
            else if (name.equals("define-macro")) {
                //System.out.println("Defining macro: " + attribute.getValue());
                Element el = element.createCopy();
                el.remove(attribute);
                macros.put(attribute.getValue(), new MacroImpl(el));
                expressions.macro = true;
            }

            // metal:fill-slot
            else if (name.equals("fill-slot")) {
                // these are ignored here, as they don't affect processing
                // of current template, but are called from other templates
            }

            // error
            else {
                throw new PageTemplateException("unknown metal attribute: " + name);
            }
        }

        // Pass on all other attributes
        else {
            String nsURI = namespace.getURI();
            // String qualifiedName = namespace.getPrefix() + ":" + name;
            attributes.addAttribute(nsURI, name, attribute.getQualifiedName(), "CDATA", attribute.getValue());
            if (nsURI != "" && namespace != elementNamespace) {
                String prefix = namespace.getPrefix();
                String qName = "xmlns:" + prefix;
                if (attributes.getIndex(qName) == -1) {
                    // add xmlns for this attribute
                    attributes.addAttribute("", prefix, qName, "CDATA", nsURI);
                }
            }
            // attributes.addAttribute( getNamespaceURIFromPrefix(prefix),
            // name, qualifiedName, "CDATA", attribute.getValue() );
        }
    }
    return attributes;
}

From source file:com.zimbra.common.soap.Element.java

License:Open Source License

public static Element convertDOM(org.dom4j.Element d4root, ElementFactory factory) {
    Element elt = factory.createElement(d4root.getQName());
    for (Iterator<?> it = d4root.attributeIterator(); it.hasNext();) {
        org.dom4j.Attribute d4attr = (org.dom4j.Attribute) it.next();
        elt.addAttribute(d4attr.getQualifiedName(), d4attr.getValue());
    }//  w w w  . j  a  v  a 2  s  .co m

    for (Iterator<?> it = d4root.elementIterator(); it.hasNext();) {
        org.dom4j.Element d4elt = (org.dom4j.Element) it.next();
        if (XHTML_NS_URI.equalsIgnoreCase(d4elt.getNamespaceURI()) && !d4elt.elements().isEmpty()) {
            // need to treat XHTML as text
            return flattenDOM(d4root, factory);
        } else {
            elt.addNonUniqueElement(convertDOM(d4elt, factory));
        }
    }
    String content = d4root.getText();
    if (content != null && !content.trim().equals("")) {
        try {
            elt.setText(content);
        } catch (ContainerException ce) {
            // can't hold both children and text on a single node, so flatten contents to text
            return flattenDOM(d4root, factory);
        }
    }
    return elt;
}

From source file:com.zimbra.common.soap.Element.java

License:Open Source License

private static Element flattenDOM(org.dom4j.Element d4root, ElementFactory factory) {
    Element elt = factory.createElement(d4root.getQName());
    for (Iterator<?> it = d4root.attributeIterator(); it.hasNext();) {
        org.dom4j.Attribute d4attr = (org.dom4j.Attribute) it.next();
        elt.addAttribute(d4attr.getQualifiedName(), d4attr.getValue());
    }//from w w w .  jav  a  2 s  . c  om

    StringBuilder content = new StringBuilder();
    for (int i = 0, size = d4root.nodeCount(); i < size; i++) {
        org.dom4j.Node node = d4root.node(i);
        switch (node.getNodeType()) {
        case org.dom4j.Node.TEXT_NODE:
            content.append(node.getText());
            break;
        case org.dom4j.Node.ELEMENT_NODE:
            content.append(((org.dom4j.Element) node).asXML());
            break;
        }
    }
    return elt.setText(content.toString());
}

From source file:org.craftercms.core.xml.mergers.impl.cues.impl.MergeCueResolverImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
protected Map<String, String> getMergeCueParams(Element element, Attribute mergeCueAttribute) {
    Map<String, String> params = new HashMap<String, String>();
    String paramsPrefix = mergeCueAttribute.getQualifiedName() + "-";
    List<Attribute> attributes = element.attributes();

    for (Iterator<Attribute> i = attributes.iterator(); i.hasNext();) {
        Attribute attribute = i.next();
        String attributeQualifiedName = attribute.getQualifiedName();
        if (attributeQualifiedName.startsWith(paramsPrefix)) {
            i.remove();/* w w  w.ja va  2  s.  co  m*/

            String paramName = attributeQualifiedName.substring(paramsPrefix.length());
            String paramValue = attribute.getValue();

            params.put(paramName, paramValue);
        }
    }

    return params;
}

From source file:org.onosproject.yang.serializers.utils.SerializersUtil.java

License:Apache License

/**
 * Converts XML atrtibutes into annotated node info.
 *
 * @param element XML element/*  w  w  w.  j a v a 2s . c o m*/
 * @param id      resource id of an element
 * @return annotated node info
 */
public static AnnotatedNodeInfo convertXmlAttributesToAnnotations(Element element, ResourceId id) {
    Iterator iter = element.attributeIterator();
    if (!iter.hasNext()) {
        // element does not have any attributes
        return null;
    }
    AnnotatedNodeInfo.Builder builder = DefaultAnnotatedNodeInfo.builder();
    builder = builder.resourceId(id);
    while (iter.hasNext()) {
        Attribute attr = (Attribute) iter.next();
        DefaultAnnotation annotation = new DefaultAnnotation(attr.getQualifiedName(), attr.getValue());
        builder = builder.addAnnotation(annotation);
    }
    return builder.build();
}

From source file:org.orbeon.oxf.processor.tamino.dom4j.TDOM4JXMLOutputter.java

License:Open Source License

/**
* <p>/* w  w  w.  j  av a 2 s. co m*/
* This will handle printing out an <code>{@link Attribute}</code> list.
* </p>
*
* @param attributes <code>List</code> of Attribute objcts
* @param out <code>Writer</code> to write to
*/
protected void printAttributes(List attributes, Element parent, Writer out, TDOM4JNamespaceStack namespaces)
        throws IOException {

    // I do not yet handle the case where the same prefix maps to
    // two different URIs. For attributes on the same element
    // this is illegal; but as yet we don't throw an exception
    // if someone tries to do this
    Set prefixes = new HashSet();

    for (int i = 0, size = attributes.size(); i < size; i++) {
        Attribute attribute = (Attribute) attributes.get(i);
        Namespace ns = attribute.getNamespace();
        if (ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaces.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace decl
                printNamespace(ns, out);
                namespaces.push(ns);
            }
        }

        out.write(" ");
        out.write(attribute.getQualifiedName());
        out.write("=");

        out.write("\"");
        out.write(escapeAttributeEntities(attribute.getValue()));
        out.write("\"");
    }

}

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;
    {/* w ww . ja  v a  2s.  co 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.xml.dom4j.Dom4jUtils.java

License:Open Source License

public static String elementToDebugString(Element element) {
    // Open start tag
    final StringBuilder sb = new StringBuilder("<");
    sb.append(element.getQualifiedName());

    // Attributes if any
    for (Iterator i = element.attributeIterator(); i.hasNext();) {
        final Attribute currentAttribute = (Attribute) i.next();

        sb.append(' ');
        sb.append(currentAttribute.getQualifiedName());
        sb.append("=\"");
        sb.append(currentAttribute.getValue());
        sb.append('\"');
    }/*from  ww w .j  a  va2 s.c  o  m*/

    final boolean isEmptyElement = element.elements().isEmpty() && element.getText().length() == 0;
    if (isEmptyElement) {
        // Close empty element
        sb.append("/>");
    } else {
        // Close start tag
        sb.append('>');
        sb.append("[...]");
        // Close element with end tag
        sb.append("</");
        sb.append(element.getQualifiedName());
        sb.append('>');
    }

    return sb.toString();
}

From source file:org.orbeon.oxf.xml.dom4j.Dom4jUtils.java

License:Open Source License

public static String attributeToDebugString(Attribute attribute) {
    final StringBuilder sb = new StringBuilder(attribute.getQualifiedName());
    sb.append("=\"");
    sb.append(attribute.getValue());/*from   w  w  w.j  ava  2s . c  o  m*/
    sb.append('\"');
    return sb.toString();
}