Example usage for org.w3c.dom Attr getValue

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

Introduction

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

Prototype

public String getValue();

Source Link

Document

On retrieval, the value of the attribute is returned as a string.

Usage

From source file:org.alfresco.web.forms.xforms.Schema2XForms.java

private Element addElementWithMultipleCompatibleTypes(final Document xformsDocument, Element modelSection,
        final Element defaultInstanceElement, final Element formSection, final XSModel schema,
        final XSElementDeclaration elementDecl, final TreeSet<XSTypeDefinition> compatibleTypes,
        final String pathToRoot, final ResourceBundle resourceBundle, final SchemaUtil.Occurrence occurs)
        throws FormBuilderException {
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("[addElementWithMultipleCompatibleTypes] adding element " + elementDecl + " at path "
                + pathToRoot);//from w ww . j  a v  a  2  s .c o m

    // look for compatible types
    final XSTypeDefinition controlType = elementDecl.getTypeDefinition();

    //get possible values
    final List<XSTypeDefinition> enumValues = new LinkedList<XSTypeDefinition>();
    //add the type (if not abstract)
    if (!((XSComplexTypeDefinition) controlType).getAbstract()) {
        enumValues.add(controlType);
    }

    //add compatible types
    enumValues.addAll(compatibleTypes);

    // multiple compatible types for this element exist
    // in the schema - allow the user to choose from
    // between compatible non-abstract types
    boolean isRepeated = isRepeated(occurs, controlType);
    Element bindElement = this.createBind(xformsDocument, pathToRoot + "/@xsi:type");
    String bindId = bindElement.getAttributeNS(null, "id");
    modelSection.appendChild(bindElement);
    this.startBindElement(bindElement, schema, controlType, null, occurs);

    //add the "element" bind, in addition
    final Element bindElement2 = this.createBind(xformsDocument,
            pathToRoot + (isRepeated ? "[position() != last()]" : ""));
    modelSection.appendChild(bindElement2);
    this.startBindElement(bindElement2, schema, controlType, null, occurs);

    // add content to select1
    final Map<String, Element> caseTypes = this.addChoicesForSelectSwitchControl(xformsDocument, formSection,
            enumValues, bindId);

    //add switch
    final Element switchElement = xformsDocument.createElementNS(NamespaceConstants.XFORMS_NS,
            NamespaceConstants.XFORMS_PREFIX + ":switch");
    switchElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":bind",
            bindId);
    switchElement.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":appearance",
            "full");

    formSection.appendChild(switchElement);

    if (!((XSComplexTypeDefinition) controlType).getAbstract()) {
        final Element firstCaseElement = caseTypes.get(controlType.getName());
        switchElement.appendChild(firstCaseElement);
        final Element firstGroupElement = this.addComplexType(xformsDocument, modelSection,
                defaultInstanceElement, firstCaseElement, schema, (XSComplexTypeDefinition) controlType,
                elementDecl, pathToRoot, SchemaUtil.getOccurrence(elementDecl), true, false, resourceBundle);
        firstGroupElement.setAttributeNS(NamespaceConstants.XFORMS_NS,
                NamespaceConstants.XFORMS_PREFIX + ":appearance", "");
    }

    defaultInstanceElement.setAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS,
            NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX + ":type",
            (((XSComplexTypeDefinition) controlType).getAbstract() ? compatibleTypes.first().getName()
                    : controlType.getName()));
    defaultInstanceElement.setAttributeNS(NamespaceConstants.XMLSCHEMA_INSTANCE_NS,
            NamespaceConstants.XMLSCHEMA_INSTANCE_PREFIX + ":nil", "true");

    /////////////// add sub types //////////////
    // add each compatible type within
    // a case statement
    for (final XSTypeDefinition type : compatibleTypes) {
        final String compatibleTypeName = type.getName();

        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(type == null
                    ? ("[addElementWithMultipleCompatibleTypes] compatible type is null!! type = "
                            + compatibleTypeName + ", targetNamespace = " + this.targetNamespace)
                    : ("[addElementWithMultipleCompatibleTypes] adding compatible type " + type.getName()));
        }

        if (type == null || type.getTypeCategory() != XSTypeDefinition.COMPLEX_TYPE) {
            continue;
        }

        final Element caseElement = caseTypes.get(type.getName());
        switchElement.appendChild(caseElement);

        // ALF-9524 fix, add an extra element to the instance for each type that extends the abstract parent
        Element newDefaultInstanceElement = xformsDocument.createElement(getElementName(type, xformsDocument));

        Attr nodesetAttr = modelSection.getAttributeNodeNS(NamespaceConstants.XFORMS_NS, "nodeset");
        // construct the nodeset that is used in bind for abstract type
        String desiredBindNodeset = getElementName(elementDecl, xformsDocument)
                + (isRepeated ? "[position() != last()]" : "");

        // check the current bind
        if (nodesetAttr == null || !nodesetAttr.getValue().equals(desiredBindNodeset)) {
            // look for desired bind in children
            Element newModelSection = DOMUtil.getElementByAttributeValueNS(modelSection,
                    NamespaceConstants.XFORMS_NS, "bind", NamespaceConstants.XFORMS_NS, "nodeset",
                    desiredBindNodeset);

            if (newModelSection == null) {
                // look for absolute path
                desiredBindNodeset = "/" + desiredBindNodeset;
                newModelSection = DOMUtil.getElementByAttributeValueNS(modelSection,
                        NamespaceConstants.XFORMS_NS, "bind", NamespaceConstants.XFORMS_NS, "nodeset",
                        desiredBindNodeset);
            }

            modelSection = newModelSection;
        }

        // create the extra bind for each child of abstract type
        Element bindElement3 = this.createBind(xformsDocument, getElementName(type, xformsDocument));
        modelSection.appendChild(bindElement3);
        bindElement3 = this.startBindElement(bindElement3, schema, controlType, elementDecl, occurs);

        // add the relevant attribute that checks the value of parent' @xsi:type
        bindElement3.setAttributeNS(NamespaceConstants.XFORMS_NS,
                NamespaceConstants.XFORMS_PREFIX + ":relevant", "../@xsi:type='" + type.getName() + "'");

        final Element groupElement = this.addComplexType(xformsDocument, modelSection,
                newDefaultInstanceElement, caseElement, schema, (XSComplexTypeDefinition) type, elementDecl,
                pathToRoot, SchemaUtil.getOccurrence(elementDecl), true, true, resourceBundle);
        groupElement.setAttributeNS(NamespaceConstants.XFORMS_NS,
                NamespaceConstants.XFORMS_PREFIX + ":appearance", "");

        defaultInstanceElement.appendChild(newDefaultInstanceElement.cloneNode(true));

        // modify bind to add a "relevant" attribute that checks the value of @xsi:type
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("[addElementWithMultipleCompatibleTypes] Model section =\n"
                    + XMLUtil.toString(bindElement3));
        }

        final NodeList binds = bindElement3.getElementsByTagNameNS(NamespaceConstants.XFORMS_NS, "bind");
        for (int i = 0; i < binds.getLength(); i++) {
            final Element subBind = (Element) binds.item(i);
            String name = subBind.getAttributeNS(NamespaceConstants.XFORMS_NS, "nodeset");

            // ETHREEOH-3308 fix
            name = repeatableNamePattern.matcher(name).replaceAll("");

            if (!subBind.getParentNode().getAttributes().getNamedItem("id").getNodeValue()
                    .equals(bindElement3.getAttribute("id"))) {
                continue;
            }

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[addElementWithMultipleCompatibleTypes] Testing sub-bind with nodeset " + name);
            }

            Pair<String, String> parsed = parseName(name, xformsDocument);

            if (!SchemaUtil.isElementDeclaredIn(parsed.getFirst(), parsed.getSecond(),
                    (XSComplexTypeDefinition) type, false)
                    && !SchemaUtil.isAttributeDeclaredIn(parsed.getFirst(), parsed.getSecond(),
                            (XSComplexTypeDefinition) type, false)) {
                continue;
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("[addElementWithMultipleCompatibleTypes] Element/Attribute " + name
                        + " declared in type " + type.getName() + ": adding relevant attribute");
            }

            //test sub types of this type
            //TreeSet subCompatibleTypes = (TreeSet) typeTree.get(type);

            String newRelevant = "../../@xsi:type='" + type.getName() + "'";
            if (this.typeTree.containsKey(type.getName())) {
                for (XSTypeDefinition otherType : this.typeTree.get(type.getName())) {
                    newRelevant = newRelevant + " or ../../@xsi:type='" + otherType.getName() + "'";
                }
            }

            //change relevant attribute
            final String relevant = subBind.getAttributeNS(NamespaceConstants.XFORMS_NS, "relevant");
            if (relevant != null && relevant.length() != 0) {
                newRelevant = ("(" + relevant + ") and " + newRelevant);
            }
            subBind.setAttributeNS(NamespaceConstants.XFORMS_NS, NamespaceConstants.XFORMS_PREFIX + ":relevant",
                    newRelevant);
        }
    }
    return switchElement;
}

From source file:org.apache.axis.encoding.SerializationContext.java

/**
 * Output a DOM representation to a SerializationContext
 * @param el is a DOM Element//from w ww. jav  a  2s .com
 */
public void writeDOMElement(Element el) throws IOException {
    if (startOfDocument && sendXMLDecl) {
        writeXMLDeclaration();
    }

    // If el is a Text element, write the text and exit
    if (el instanceof org.apache.axis.message.Text) {
        writeSafeString(((Text) el).getData());
        return;
    }

    AttributesImpl attributes = null;
    NamedNodeMap attrMap = el.getAttributes();

    if (attrMap.getLength() > 0) {
        attributes = new AttributesImpl();
        for (int i = 0; i < attrMap.getLength(); i++) {
            Attr attr = (Attr) attrMap.item(i);
            String tmp = attr.getNamespaceURI();
            if (tmp != null && tmp.equals(Constants.NS_URI_XMLNS)) {
                String prefix = attr.getLocalName();
                if (prefix != null) {
                    if (prefix.equals("xmlns"))
                        prefix = "";
                    String nsURI = attr.getValue();
                    registerPrefixForURI(prefix, nsURI);
                }
                continue;
            }

            attributes.addAttribute(attr.getNamespaceURI(), attr.getLocalName(), attr.getName(), "CDATA",
                    attr.getValue());
        }
    }

    String namespaceURI = el.getNamespaceURI();
    String localPart = el.getLocalName();
    if (namespaceURI == null || namespaceURI.length() == 0)
        localPart = el.getNodeName();
    QName qName = new QName(namespaceURI, localPart);

    startElement(qName, attributes);

    NodeList children = el.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            writeDOMElement((Element) child);
        } else if (child instanceof CDATASection) {
            writeString("<![CDATA[");
            writeString(((Text) child).getData());
            writeString("]]>");
        } else if (child instanceof Comment) {
            writeString("<!--");
            writeString(((CharacterData) child).getData());
            writeString("-->");
        } else if (child instanceof Text) {
            writeSafeString(((Text) child).getData());
        }
    }

    endElement();
}

From source file:org.apache.axis.encoding.SerializationContextImpl.java

/**
 * Output a DOM representation to a SerializationContext
 * @param el is a DOM Element/*from  ww w .j  a v  a 2s . com*/
 */
public void writeDOMElement(Element el)
    throws IOException
{
    AttributesImpl attributes = null;
    NamedNodeMap attrMap = el.getAttributes();

    if (attrMap.getLength() > 0) {
        attributes = new AttributesImpl();
        for (int i = 0; i < attrMap.getLength(); i++) {
            Attr attr = (Attr)attrMap.item(i);
            String tmp = attr.getNamespaceURI();
            if ( tmp != null && tmp.equals(Constants.NS_URI_XMLNS) ) {
                String prefix = attr.getLocalName();
                if (prefix != null) {
                    if (prefix.equals("xmlns"))
                        prefix = "";
                    String nsURI = attr.getValue();
                    registerPrefixForURI(prefix, nsURI);
                }
                continue;
            }

            attributes.addAttribute(attr.getNamespaceURI(),
                                    attr.getLocalName(),
                                    attr.getName(),
                                    "CDATA", attr.getValue());
        }
    }

    String namespaceURI = el.getNamespaceURI();
    String localPart = el.getLocalName();
    if(namespaceURI == null || namespaceURI.length()==0)
        localPart = el.getNodeName();
    QName qName = new QName(namespaceURI, localPart);

    startElement(qName, attributes);

    NodeList children = el.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Element) {
            writeDOMElement((Element)child);
        } else if (child instanceof CDATASection) {
            writeString("<![CDATA[");
            writeString(((Text)child).getData());
            writeString("]]>");
        } else if (child instanceof Comment) {
            writeString("<!--");
            writeString(((CharacterData)child).getData());
            writeString("-->");
        } else if (child instanceof Text) {
            writeSafeString(((Text)child).getData());
        }
    }

    endElement();
}

From source file:org.apache.axis.message.MessageElement.java

/**
 * set an attribute as a node/*www  .  j av a 2 s  . com*/
 * @see org.w3c.dom.Element#setAttributeNodeNS(org.w3c.dom.Attr)
 * @todo implement properly.
 * @param newAttr
 * @return null
 * @throws DOMException
 */
public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
    //attributes.
    AttributesImpl attributes = makeAttributesEditable();
    // how to convert to DOM ATTR
    attributes.addAttribute(newAttr.getNamespaceURI(), newAttr.getLocalName(), newAttr.getLocalName(), "CDATA",
            newAttr.getValue());
    return null;
}

From source file:org.apache.axis.utils.XMLUtils.java

/**
 * Searches for the namespace URI of the given prefix in the given DOM range.
 *
 * The namespace is not searched in parent of the "stopNode". This is
 * usefull to get all the needed namespaces when you need to ouput only a
 * subtree of a DOM document./*from  w  ww  . ja va  2s  . com*/
 *
 * @param prefix the prefix to find
 * @param e the starting node
 * @param stopNode null to search in all the document or a parent node where the search must stop.
 * @return null if no namespace is found, or the namespace URI.
 */
public static String getNamespace(String prefix, Node e, Node stopNode) {
    while (e != null && (e.getNodeType() == Node.ELEMENT_NODE)) {
        Attr attr = null;
        if (prefix == null) {
            attr = ((Element) e).getAttributeNode("xmlns");
        } else {
            attr = ((Element) e).getAttributeNodeNS(Constants.NS_URI_XMLNS, prefix);
        }
        if (attr != null)
            return attr.getValue();
        if (e == stopNode)
            return null;
        e = e.getParentNode();
    }
    return null;
}

From source file:org.apache.axis.wsdl.fromJava.Types.java

/**
 * Write out the given Element into the appropriate schema node.
 * If need be create the schema node as well
 *
 * @param namespaceURI namespace this node should get dropped into
 * @param element      the Element to append to the Schema node
 * @throws AxisFault/*from  w  ww. j  a v a 2 s.  co m*/
 */
public void writeSchemaElement(String namespaceURI, Element element) throws AxisFault {

    if (wsdlTypesElem == null) {
        try {
            writeWsdlTypesElement();
        } catch (Exception e) {
            log.error(e);

            return;
        }
    }

    if ((namespaceURI == null) || namespaceURI.equals("")) {
        throw new AxisFault(Constants.FAULT_SERVER_GENERAL, Messages.getMessage("noNamespace00", namespaceURI),
                null, null);
    }

    Element schemaElem = null;
    NodeList nl = wsdlTypesElem.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
        NamedNodeMap attrs = nl.item(i).getAttributes();

        if (attrs != null) {
            for (int n = 0; n < attrs.getLength(); n++) {
                Attr a = (Attr) attrs.item(n);

                if (a.getName().equals("targetNamespace") && a.getValue().equals(namespaceURI)) {
                    schemaElem = (Element) nl.item(i);
                }
            }
        }
    }

    if (schemaElem == null) {
        schemaElem = docHolder.createElement("schema");

        wsdlTypesElem.appendChild(schemaElem);
        schemaElem.setAttribute("xmlns", Constants.URI_DEFAULT_SCHEMA_XSD);
        schemaElem.setAttribute("targetNamespace", namespaceURI);

        // Add SOAP-ENC namespace import if necessary
        if (serviceDesc.getStyle() == Style.RPC) {
            Element importElem = docHolder.createElement("import");

            schemaElem.appendChild(importElem);
            importElem.setAttribute("namespace", Constants.URI_DEFAULT_SOAP_ENC);
        }

        SOAPService service = null;
        if (MessageContext.getCurrentContext() != null) {
            service = MessageContext.getCurrentContext().getService();
        }
        if (service != null && isPresent((String) service.getOption("schemaQualified"), namespaceURI)) {
            schemaElem.setAttribute("elementFormDefault", "qualified");
        } else if (service != null
                && isPresent((String) service.getOption("schemaUnqualified"), namespaceURI)) {
            // DO nothing..default is unqualified.
        } else if ((serviceDesc.getStyle() == Style.DOCUMENT) || (serviceDesc.getStyle() == Style.WRAPPED)) {
            schemaElem.setAttribute("elementFormDefault", "qualified");
        }

        writeTypeNamespace(namespaceURI);
    }

    schemaElem.appendChild(element);
}

From source file:org.apache.axis.wsdl.fromJava.Types.java

/**
 * Inserts the type fragment into the given wsdl document and ensures
 * that definitions from each embedded schema are allowed to reference
 * schema components from the other sibling schemas.
 * @param doc//from   w w w . j  a va  2s  .  c o m
 */
public void insertTypesFragment(Document doc) {

    updateNamespaces();

    if (wsdlTypesElem == null)
        return;

    // Make sure that definitions from each embedded schema are allowed
    // to reference schema components from the other sibling schemas.
    Element schemaElem = null;
    String tns = null;
    NodeList nl = wsdlTypesElem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        NamedNodeMap attrs = nl.item(i).getAttributes();
        if (attrs == null)
            continue; // Should never happen.
        for (int n = 0; n < attrs.getLength(); n++) {
            Attr a = (Attr) attrs.item(n);
            if (a.getName().equals("targetNamespace")) {
                tns = a.getValue();
                schemaElem = (Element) nl.item(i);
                break;
            }
        }

        // Ignore what appears to be a not namespace-qualified
        // schema definition.
        if (tns != null && !"".equals(tns.trim())) {
            // By now we know that an import element might be necessary
            // for some sibling schemas. However, in the absence of
            // a symbol table proper, the best we can do is add one
            // for each sibling schema.
            Iterator it = schemaTypes.keySet().iterator();
            String otherTns;
            Element importElem;
            while (it.hasNext()) {
                if (!tns.equals(otherTns = (String) it.next())) {
                    importElem = docHolder.createElement("import");
                    importElem.setAttribute("namespace", otherTns);
                    schemaElem.insertBefore(importElem, schemaElem.getFirstChild());
                }
            }
        }
        schemaElem = null;
        tns = null;
    }

    // Import the wsdlTypesElement into the doc.
    org.w3c.dom.Node node = doc.importNode(wsdlTypesElem, true);
    // Insert the imported element at the beginning of the document
    doc.getDocumentElement().insertBefore(node, doc.getDocumentElement().getFirstChild());
}

From source file:org.apache.axis2.jaxws.message.util.impl.XMLStreamReaderFromDOM.java

public String getAttributeValue(int index) {
    Attr attr = (Attr) getAttributes().get(index);
    return attr.getValue();
}

From source file:org.apache.cocoon.forms.util.DomHelper.java

private static Map addLocalNSDeclarations(Element elm, Map nsDeclarations) {
    NamedNodeMap atts = elm.getAttributes();
    int attsSize = atts.getLength();

    for (int i = 0; i < attsSize; i++) {
        Attr attr = (Attr) atts.item(i);
        if (XMLNS_URI.equals(attr.getNamespaceURI())) {
            String nsUri = attr.getValue();
            String pfx = attr.getLocalName();
            if (nsDeclarations == null)
                nsDeclarations = new HashMap();
            nsDeclarations.put(nsUri, pfx);
        }/*from w  w  w. j ava 2s.  c  o  m*/
    }
    return nsDeclarations;
}

From source file:org.apache.cocoon.util.jxpath.DOMFactory.java

public String getNamespaceURI(Element element, String prefix) {
    Node tmp = element;/*from ww  w .j  a va  2 s.  c o  m*/
    String nsAttr = prefix == null ? "xmlns" : "xmlns:" + prefix;

    while (tmp != null && tmp.getNodeType() == Node.ELEMENT_NODE) {
        element = (Element) tmp;

        // First test element prefixes
        if (prefix == null) {
            if (element.getPrefix() == null) {
                return element.getNamespaceURI();
            }
        } else if (prefix.equals(element.getPrefix())) {
            return element.getNamespaceURI();
        }

        // Note: stupid DOM api returns "" when an attribute doesn't exist, so we use the Attr node.
        Attr nsAttrNode = ((Element) tmp).getAttributeNode(nsAttr);
        if (nsAttrNode != null) {
            return nsAttrNode.getValue();
        }
        tmp = tmp.getParentNode();
    }
    return null;
}