Example usage for org.w3c.dom Attr setValue

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

Introduction

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

Prototype

public void setValue(String value) throws DOMException;

Source Link

Document

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

Usage

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

private Element createElement(Document document, String name, Object bean) throws IntrospectionException,
        InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    Element element = document.createElement(name);
    BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
    for (PropertyDescriptor propInfo : beanInfo.getPropertyDescriptors()) {
        String propName = propInfo.getName();
        if (propInfo.getReadMethod() != null && String.class.isAssignableFrom(propInfo.getPropertyType())) {
            String propValue = BeanUtils.getProperty(bean, propName);
            if (propValue != null && !propValue.isEmpty()) {
                // Doing it the hard way to avoid having the &'s in the query string escaped at &
                Attr attr = document.createAttribute(propName);
                attr.setValue(propValue);
                element.setAttributeNode(attr);
                //element.setAttribute( propName, propValue );
            }//from w ww  .j a  va  2  s .  c o m
        }
    }
    return element;
}

From source file:org.apache.nutch.searcher.response.xml.XMLResponseWriter.java

/**
 * Adds an attribute name and value to a node Element in the XML document.
 * /*from w w  w  . ja va  2  s  .co m*/
 * @param doc The XML document.
 * @param node The node Element on which to attach the attribute.
 * @param name The name of the attribute.
 * @param value The value of the attribute.
 */
private static void addAttribute(Document doc, Element node, String name, String value) {
    Attr attribute = doc.createAttribute(name);
    attribute.setValue(getLegalXml(value));
    node.getAttributes().setNamedItem(attribute);
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * @param el//from  ww w .  j av a  2s.  c o  m
 */
public static void pancakeNamespaces(Element el) {
    Map ns = getParentNamespaces(el);
    Document d = el.getOwnerDocument();
    assert d != null;
    Iterator it = ns.keySet().iterator();
    while (it.hasNext()) {
        String key = (String) it.next();
        String uri = (String) ns.get(key);
        Attr a = d.createAttributeNS(NS_URI_XMLNS, (key.length() != 0) ? ("xmlns:" + key) : ("xmlns"));
        a.setValue(uri);
        el.setAttributeNodeNS(a);
    }
}

From source file:org.apache.ode.utils.DOMUtils.java

private static void parse(XMLStreamReader reader, Document doc, Node parent) throws XMLStreamException {
    int event = reader.getEventType();

    while (reader.hasNext()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            // create element
            Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName());
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                e.setPrefix(reader.getPrefix());
            }/*from ww  w.  ja  va 2s .  c o  m*/
            parent.appendChild(e);

            // copy namespaces
            for (int ns = 0; ns < reader.getNamespaceCount(); ns++) {
                String uri = reader.getNamespaceURI(ns);
                String prefix = reader.getNamespacePrefix(ns);
                declare(e, uri, prefix);
            }

            // copy attributes
            for (int att = 0; att < reader.getAttributeCount(); att++) {
                String name = reader.getAttributeLocalName(att);
                String prefix = reader.getAttributePrefix(att);
                if (prefix != null && prefix.length() > 0) {
                    name = prefix + ":" + name;
                }
                Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name);
                attr.setValue(reader.getAttributeValue(att));
                e.setAttributeNode(attr);
            }
            // sub-nodes
            if (reader.hasNext()) {
                reader.next();
                parse(reader, doc, e);
            }
            if (parent instanceof Document) {
                while (reader.hasNext())
                    reader.next();
                return;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            return;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                parent.appendChild(doc.createTextNode(reader.getText()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(reader.getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            parent.appendChild(doc.createCDATASection(reader.getText()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.NAMESPACE:
        case XMLStreamConstants.ATTRIBUTE:
            break;
        default:
            break;
        }

        if (reader.hasNext()) {
            event = reader.next();
        }
    }
}

From source file:org.apache.openmeetings.cli.ConnectionPropertiesPatcher.java

public static void patch(ConnectionProperties props) throws Exception {
    ConnectionPropertiesPatcher patcher = getPatcher(props);
    Document doc = getDocument(OmFileHelper.getPersistence(props.getDbType()));
    Attr attr = getConnectionProperties(doc);
    String[] tokens = attr.getValue().split(",");
    patcher.patchAttribute(tokens);/*w  w  w.ja  v  a2s  .  co m*/
    attr.setValue(StringUtils.join(tokens, ","));

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, new StreamResult(OmFileHelper.getPersistence().getCanonicalPath())); //this constructor is used to avoid transforming path to URI
}

From source file:org.apache.shindig.gadgets.rewrite.AbsolutePathReferenceVisitor.java

public VisitStatus visit(Gadget gadget, Node node) throws RewritingException {
    Attr nodeAttr = getUriAttributeFromNode(node, tagsToMakeAbsolute);

    if (nodeAttr != null) {
        try {/*from   w  ww .ja  v a 2s  .  c o  m*/
            Uri nodeUri = Uri.parse(nodeAttr.getValue());
            Uri baseUri = getBaseResolutionUri(gadget, node);

            Uri resolved = baseUri.resolve(nodeUri);

            if (!resolved.equals(nodeUri)) {
                nodeAttr.setValue(resolved.toString());
                return VisitStatus.MODIFY;
            }
        } catch (Uri.UriException e) {
            // UriException on illegal input. Ignore.
        }
    }
    return VisitStatus.BYPASS;
}

From source file:org.apache.tuscany.sca.implementation.bpel.ode.TuscanyProcessConfImpl.java

/**
 * Gets the variable initializer DOM sequence for a given property, in the context of a supplied
 * DOM model of the BPEL process// www  .jav  a 2s  . c  o  m
 * @param bpelDOM - DOM representation of the BPEL process
 * @param property - SCA Property which relates to one of the variables in the BPEL process
 * @return - a DOM model representation of the XML statements required to initialize the
 * BPEL variable with the value of the SCA property.
 */
private Element getInitializerSequence(Document bpelDOM, ComponentProperty property) {
    // For an XML simple type (string, int, etc), the BPEL initializer sequence is:
    // <assign><copy><from><literal>value</literal></from><to variable="variableName"/></copy></assign>
    QName type = property.getXSDType();
    if (type != null) {
        if (mapper.isSimpleXSDType(type)) {
            // Simple types
            String NS_URI = bpelDOM.getDocumentElement().getNamespaceURI();
            String valueText = getPropertyValueText(property.getValue());
            Element literalElement = bpelDOM.createElementNS(NS_URI, "literal");
            literalElement.setTextContent(valueText);
            Element fromElement = bpelDOM.createElementNS(NS_URI, "from");
            fromElement.appendChild(literalElement);
            Element toElement = bpelDOM.createElementNS(NS_URI, "to");
            Attr variableAttribute = bpelDOM.createAttribute("variable");
            variableAttribute.setValue(property.getName());
            toElement.setAttributeNode(variableAttribute);
            Element copyElement = bpelDOM.createElementNS(NS_URI, "copy");
            copyElement.appendChild(fromElement);
            copyElement.appendChild(toElement);
            Element assignElement = bpelDOM.createElementNS(NS_URI, "assign");
            assignElement.appendChild(copyElement);
            return assignElement;
        } // end if
          // TODO Deal with Properties which have a non-simple type
    } else {
        // TODO Deal with Properties which have an element as the type
    } // end if

    return null;
}

From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java

protected void handleParent(Element e, NameSpaceSymbTable ns) {
    if (!e.hasAttributes() && e.getNamespaceURI() == null) {
        return;//  www  .  j av a  2  s  .  co  m
    }
    xmlattrStack.push(-1);
    NamedNodeMap attrs = e.getAttributes();
    int attrsLength = attrs.getLength();
    for (int i = 0; i < attrsLength; i++) {
        Attr attribute = (Attr) attrs.item(i);
        String NName = attribute.getLocalName();
        String NValue = attribute.getNodeValue();

        if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) {
            if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) {
                ns.addMapping(NName, NValue, attribute);
            }
        } else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) {
            xmlattrStack.addXmlnsAttr(attribute);
        }
    }
    if (e.getNamespaceURI() != null) {
        String NName = e.getPrefix();
        String NValue = e.getNamespaceURI();
        String Name;
        if (NName == null || NName.equals("")) {
            NName = "xmlns";
            Name = "xmlns";
        } else {
            Name = "xmlns:" + NName;
        }
        Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name);
        n.setValue(NValue);
        ns.addMapping(NName, NValue, n);
    }
}

From source file:org.apache.xml.security.test.utils.resolver.ResourceResolverTest.java

/**
 * Tests registering a custom resolver implementation.
 *//*from w  ww.  java 2 s .  co  m*/
public static void testCustomResolver() throws Exception {
    String className = "org.apache.xml.security.test.utils.resolver.OfflineResolver";
    ResourceResolver.registerAtStart(className);
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Attr uriAttr = doc.createAttribute("URI");
    uriAttr.setValue("http://www.apache.org");
    ResourceResolver res = ResourceResolver.getInstance(uriAttr, "http://www.apache.org");
    try {
        uriAttr.setValue("http://xmldsig.pothole.com/xml-stylesheet.txt");
        res.resolve(uriAttr, null);
    } catch (Exception e) {
        e.printStackTrace();
        fail(uriAttr.getValue() + " should be resolvable by the OfflineResolver");
    }
    try {
        uriAttr.setValue("http://www.apache.org");
        res.resolve(uriAttr, null);
        fail(uriAttr.getValue() + " should not be resolvable by the OfflineResolver");
    } catch (Exception e) {
    }
}

From source file:org.apache.xml.security.test.utils.resolver.ResourceResolverTest.java

public static void testLocalFileWithEmptyBaseURI() throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Attr uriAttr = doc.createAttribute("URI");
    String basedir = System.getProperty("basedir");
    String file = new File(basedir, "build.xml").toURI().toString();
    uriAttr.setValue(file);
    ResourceResolver res = ResourceResolver.getInstance(uriAttr, file);
    try {/*from  ww  w. j a va2s . c  o  m*/
        res.resolve(uriAttr, "");
    } catch (Exception e) {
        fail(e.getMessage());
    }
}