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.openmeetings.cli.ConnectionPropertiesPatcher.java

protected Attr patchAttribute(Attr attr, String host, String port, String db, String user, String pass) {
    String[] tokens = attr.getValue().split(",");
    processBasicProperties(tokens, user, pass, connectionProperties);
    patchDb(tokens, host, port, db);/*from w  w  w .j  av  a  2s.com*/
    attr.setValue(StringUtils.join(tokens, ","));
    return attr;
}

From source file:org.osaf.cosmo.xml.DomReader.java

private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException {
    Attr a = null;

    String local = reader.getAttributeLocalName(i);
    String ns = reader.getAttributeNamespace(i);
    if (ns != null) {
        String prefix = reader.getAttributePrefix(i);
        String qualified = prefix != null ? prefix + ":" + local : local;
        a = d.createAttributeNS(ns, qualified);
    } else {/* ww w.ja  va2s .  c o  m*/
        a = d.createAttribute(reader.getAttributeLocalName(i));
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading attribute " + a.getName());

    a.setValue(reader.getAttributeValue(i));

    return a;
}

From source file:org.panlab.tgw.restclient.RepoAdapter.java

static String addSchemaDefinition(String response) {
    try {//from  w ww  .  j a  v  a 2s  .  c o m
        ByteArrayInputStream bais = new ByteArrayInputStream(response.getBytes());
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fact.newDocumentBuilder();
        Document doc = builder.parse(bais);

        Attr attr = doc.createAttribute("xmlns");
        attr.setValue("http://xml.netbeans.org/schema/repo.xsd");
        //attr.setValue("http://www.w3.org/2001/XMLSchema");
        doc.getDocumentElement().setAttributeNode(attr);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Result result = new StreamResult(baos);
        // Write the DOM document to the file
        doc.setXmlStandalone(false);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        Source source = new DOMSource(doc);
        xformer.transform(source, result);

        response = new String(baos.toByteArray());
        return response;
    } catch (Exception error) {
        return null;
    }
}

From source file:org.sakaiproject.tool.assessment.qti.helper.AuthoringXml.java

/**
 * perform Update on this object//ww  w  . ja v a 2  s  .  co  m
 * Based on method originally in XmlStringBuffer
 * @author rashmi
 * @author casong
 * @author Ed Smiley esmiley@stanford.edu changed method signatures used Document
 * @param document Document
 * @param xpath :- xpath and
 * @param value :-  Value of xpath
 *
 * @return modified Document
 * @throws DOMException DOCUMENTATION PENDING
 * @throws Exception DOCUMENTATION PENDING
 */
public Document update(Document document, String xpath, String value) throws DOMException, Exception {
    if (log.isDebugEnabled()) {
        log.debug("update(String " + xpath + ", String " + value + ")");
    }

    try {
        Element newElement = null;
        Attr newAttribute = null;
        List newElementList = this.selectNodes(document, xpath);
        int aIndex = xpath.indexOf("@");
        int size = newElementList.size();
        if (size > 1) {
            log.warn("UPDATING MORE THAN ONE ELEMENT");
        }

        if ((aIndex == -1) && (size != 0)) {
            for (int i = 0; i < size; i++) {
                newElement = (Element) newElementList.get(i);
                Node childNode = newElement.getFirstChild();

                if (childNode == null) {
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document core = db.newDocument();
                    Text newElementText = core.createTextNode(newElement.getNodeName());
                    newElementText.setNodeValue(value);
                    Text clonedText = (Text) newElement.getOwnerDocument().importNode(newElementText, true);
                    newElement.appendChild(clonedText);
                } else {
                    CharacterData newElementText = (CharacterData) newElement.getFirstChild();
                    newElementText.setNodeValue(value);
                }
            }
        }

        if ((aIndex != -1) && (size != 0)) {
            newAttribute = (Attr) newElementList.set(0, null);
            if (newAttribute != null) {
                newAttribute.setValue(value);
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return document;
}

From source file:org.sakaiproject.tool.assessment.qti.util.XmlStringBuffer.java

/**
 * perform Update on this object/*  www  .  j ava2 s. co  m*/
 *
 * @param xpath :- xpath and
 * @param value :-  Value of xpath
 *
 * @return XmlStringBuffer
 *
 * @throws DOMException
 * @throws Exception
 */

// Rashmi Aug 19th changed by Pamela on Sept 10th.
// Rashmi - replacing updateJDOM as on Sep 15
public final XmlStringBuffer update(String xpath, String value) throws DOMException, Exception {
    if (log.isDebugEnabled()) {
        log.debug("update(String " + xpath + ", String " + value + ")");
    }

    try {
        Element newElement = null;
        Attr newAttribute = null;
        List newElementList = this.selectNodes(xpath);
        //only look at the last part of the path
        int aIndex = xpath.lastIndexOf("/");
        if (aIndex == -1) {
            aIndex = 0;
        }
        aIndex = xpath.indexOf("@", aIndex);
        int size = newElementList.size();
        if (size > 1) {
            log.info("UPDATING MORE THAN ONE ELEMENT");
        }

        if ((aIndex == -1) && (size != 0)) {
            for (int i = 0; i < size; i++) {
                newElement = (Element) newElementList.get(i);
                Node childNode = newElement.getFirstChild();

                if (childNode == null) {
                    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                    DocumentBuilder db = dbf.newDocumentBuilder();
                    Document document = db.newDocument();
                    Text newElementText = document.createTextNode(newElement.getNodeName());
                    newElementText.setNodeValue(value);
                    Text clonedText = (Text) newElement.getOwnerDocument().importNode(newElementText, true);
                    newElement.appendChild(clonedText);
                } else {
                    CharacterData newElementText = (CharacterData) newElement.getFirstChild();
                    newElementText.setNodeValue(value);
                }
            }
        }

        if ((aIndex != -1) && (size != 0)) {
            newAttribute = (Attr) newElementList.set(0, null);
            if (newAttribute != null) {
                newAttribute.setValue(value);
            }
        }
    } catch (Exception ex) {
        log.error(ex.getMessage(), ex);
    }

    return this;
}

From source file:org.sakaiproject.tool.rutgers.LinkToolEntityProducer.java

/**
 * {@inheritDoc}//from w w w  .  j  a va  2s .co m
 */
public String archive(String siteId, Document doc, Stack stack, String archivePath, List attachments) {
    //prepare the buffer for the results log
    StringBuilder results = new StringBuilder();

    try {
        Site site = SiteService.getSite(siteId);
        // start with an element with our very own (service) name         
        Element element = doc.createElement(serviceName());
        element.setAttribute(VERSION_ATTR, ARCHIVE_VERSION);
        ((Element) stack.peek()).appendChild(element);
        stack.push(element);

        Element linktool = doc.createElement(LINKTOOL);
        Collection<ToolConfiguration> tools = site.getTools(myToolIds());
        if (tools != null && !tools.isEmpty()) {
            for (ToolConfiguration config : tools) {
                element = doc.createElement(LINKTOOL);

                Attr attr = doc.createAttribute("toolid");
                attr.setValue(config.getToolId());
                element.setAttributeNode(attr);

                attr = doc.createAttribute("name");
                attr.setValue(config.getContainingPage().getTitle());
                element.setAttributeNode(attr);

                Properties props = config.getConfig();
                if (props == null)
                    continue;

                String url = props.getProperty("url", null);
                if (url == null && props != null) {
                    String urlProp = props.getProperty("urlProp", null);
                    if (urlProp != null) {
                        url = ServerConfigurationService.getString(urlProp);
                    }
                }

                attr = doc.createAttribute("url");
                attr.setValue(url);
                element.setAttributeNode(attr);

                String height = "600";
                String heights = props.getProperty("height", "600");
                if (heights != null) {
                    heights = heights.trim();
                    if (heights.endsWith("px"))
                        heights = heights.substring(0, heights.length() - 2).trim();
                    height = heights;
                }

                attr = doc.createAttribute("height");
                attr.setValue(height);
                element.setAttributeNode(attr);

                linktool.appendChild(element);
            }

            results.append("archiving " + getLabel() + ": (" + tools.size()
                    + ") linktool instances archived successfully.\n");

        } else {
            results.append("archiving " + getLabel() + ": no linktools.\n");
        }

        ((Element) stack.peek()).appendChild(linktool);
        stack.push(linktool);

        stack.pop();
    } catch (Exception any) {
        logger.warn("archive: exception archiving service: " + serviceName());
    }

    stack.pop();

    return results.toString();
}

From source file:org.silverpeas.SilverpeasSettings.xml.transform.XPathTransformer.java

/**
 * Create a new node (element or attribute) to be inserted into the target node as a child or
 * attribute.//from w  w  w .j  av  a2  s .  c  o m
 * @param doc DOM document
 * @param target the target ode
 * @param value the value for creating the new node.
 */
public void createNewNode(Document doc, Node target, Value value) {
    if (value.getLocation().startsWith("@")) {
        Attr newAttribute = doc.createAttribute(value.getLocation().substring(1));
        newAttribute.setValue(value.getValue());
        target.getAttributes().setNamedItem(newAttribute);
    } else {
        Element newElement = doc.createElement(value.getLocation());
        newElement.setTextContent(value.getValue());
        target.appendChild(newElement);
    }
}

From source file:org.springframework.ws.transport.http.LocationTransformerObjectSupport.java

/**
 * Transforms the locations of the given definition document using the given XPath expression.
 * @param xPathExpression the XPath expression
 * @param definitionDocument the definition document
 * @param request the request, used to determine the location to transform to
 *///from w w  w.  jav  a 2  s .co m
protected void transformLocations(XPathExpression xPathExpression, Document definitionDocument,
        HttpServletRequest request) {
    Assert.notNull(xPathExpression, "'xPathExpression' must not be null");
    Assert.notNull(definitionDocument, "'definitionDocument' must not be null");
    Assert.notNull(request, "'request' must not be null");

    List<Node> locationNodes = xPathExpression.evaluateAsNodeList(definitionDocument);
    for (Node locationNode : locationNodes) {
        if (locationNode instanceof Attr) {
            Attr location = (Attr) locationNode;
            if (StringUtils.hasLength(location.getValue())) {
                String newLocation = transformLocation(location.getValue(), request);
                if (logger.isDebugEnabled()) {
                    logger.debug("Transforming [" + location.getValue() + "] to [" + newLocation + "]");
                }
                location.setValue(newLocation);
            }
        }
    }
}

From source file:org.unitedinternet.cosmo.util.DomReader.java

private static Attr readAttribute(int i, Document d, XMLStreamReader reader) throws XMLStreamException {
    Attr a = null;

    String local = reader.getAttributeLocalName(i);
    String ns = reader.getAttributeNamespace(i);
    if (ns != null && !ns.equals("")) {
        String prefix = reader.getAttributePrefix(i);
        String qualified = prefix != null ? prefix + ":" + local : local;
        a = d.createAttributeNS(ns, qualified);
    } else {//w w w. ja  va2s.c om
        a = d.createAttribute(reader.getAttributeLocalName(i));
    }

    //if (log.isDebugEnabled())
    //log.debug("Reading attribute " + a.getName());

    a.setValue(reader.getAttributeValue(i));

    return a;
}

From source file:org.wso2.appserver.test.integration.TestListener.java

/**
 * Registers an Apache Tomcat Valve in the server.xml of the Application Server Catalina config base.
 *
 * @param className the fully qualified class name of the Valve implementation
 * @throws ParserConfigurationException if a DocumentBuilder cannot be created
 * @throws SAXException                 if any parse errors occur
 * @throws IOException                  if an I/O error occurs
 * @throws XPathExpressionException     if the XPath expression cannot be evaluated
 * @throws TransformerException         if an error occurs during the transformation
 *///  w w  w.j av  a  2s . c o  m
private static void addValveToServerXML(String className) throws ParserConfigurationException, SAXException,
        IOException, XPathExpressionException, TransformerException {
    Path serverXML = Paths.get(System.getProperty(TestConstants.APPSERVER_HOME), "conf", "server.xml");
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new InputSource(serverXML.toString()));
    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList valves = (NodeList) xpath.evaluate("/Server/Service/Engine/Host/Valve", document,
            XPathConstants.NODESET);

    Element valve = document.createElement("Valve");
    Attr attrClassName = document.createAttribute("className");
    attrClassName.setValue(className);
    valve.setAttributeNode(attrClassName);
    valves.item(0).getParentNode().appendChild(valve);

    Transformer xFormer = TransformerFactory.newInstance().newTransformer();
    xFormer.transform(new DOMSource(document), new StreamResult(serverXML.toFile().getAbsolutePath()));
}