Example usage for org.dom4j Element attribute

List of usage examples for org.dom4j Element attribute

Introduction

In this page you can find the example usage for org.dom4j Element attribute.

Prototype

Attribute attribute(QName qName);

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * //from  w  ww . jav a 2 s.  co m
 * 
 * 
 * 
 * 
 * @param ele
 *            Element
 * @param attributeName
 *            String
 * @param attributeValue
 *            String
 * @return Element
 */
public static Element setAttribute(Element ele, String attributeName, String attributeValue) {
    if (ele == null) {
        return null;
    }

    Attribute att = ele.attribute(attributeName);
    if (att != null) {
        att.setText(attributeValue);
    } else {
        appendAttribute(ele, attributeName, attributeValue);
    }
    return ele;
}

From source file:com.buddycloud.channeldirectory.search.handler.nearby.NearbyQueryHandler.java

License:Apache License

@Override
public IQ handle(IQ iq) {

    Element queryElement = iq.getElement().element("query");
    Element pointElement = queryElement.element("point");

    if (pointElement == null) {
        return XMPPUtils.error(iq, "Query does not contain point element.", getLogger());
    }/*from   ww  w. j a  v a2  s . c  o m*/

    Attribute latAtt = pointElement.attribute("lat");
    if (latAtt == null) {
        return XMPPUtils.error(iq, "Location point does not contain point latitude element.", getLogger());
    }

    Attribute lngAtt = pointElement.attribute("lon");
    if (lngAtt == null) {
        return XMPPUtils.error(iq, "Location point does not contain point longitude element.", getLogger());
    }

    double lat = Double.valueOf(latAtt.getValue());
    double lng = Double.valueOf(lngAtt.getValue());

    RSM rsm = RSMUtils.parseRSM(queryElement);
    List<ChannelData> nearbyObjects;

    try {
        nearbyObjects = findNearbyObjects(lat, lng, rsm);
    } catch (Exception e) {
        return XMPPUtils.error(iq, "Search could not be performed, service is unavailable.", getLogger());
    }

    return createIQResponse(iq, nearbyObjects, rsm);
}

From source file:com.buddycloud.friendfinder.handler.MatchContactHandler.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from   w w w  .  j a  v a 2  s .com*/
public IQ handle(IQ iq) {
    Element queryElement = iq.getElement().element("query");
    Iterator<Element> itemIterator = queryElement.elementIterator("item");

    List<MatchedUser> reportedHashes = new LinkedList<MatchedUser>();

    while (itemIterator.hasNext()) {
        Element item = (Element) itemIterator.next();
        Attribute meAttr = item.attribute("me");
        Attribute hashAttr = item.attribute("item-hash");

        String hash = hashAttr.getValue();

        if (meAttr != null && meAttr.getValue().equals("true")) {
            HashUtils.reportHash(iq.getFrom().toBareJID(), hash, getDataSource());
        } else {
            String jid = HashUtils.retrieveJid(hash, getDataSource());
            if (jid != null) {
                reportedHashes.add(new MatchedUser(jid, hash));
            }
        }
    }

    return createResponse(iq, reportedHashes);
}

From source file:com.cc.framework.util.SettingUtils.java

License:Open Source License

/**
 * /*from w w  w  .ja va  2  s . c o  m*/
 * 
 * @param setting
 *            
 */
public static void set(Setting setting) {
    try {
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/shopxx/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.chingo247.structureapi.plan.io.document.StructurePlanDocument.java

License:Open Source License

private Flag<?> makeFlag(LineElement node) {
    String name;//from   w w  w .  ja  v  a 2s  .com
    String value;

    Element e = node.getElement();
    if (e.attribute("name") != null) {
        if (e.attribute("value") == null) {
            throw new PlanException("No 'value' attribute for element in '" + getFile().getAbsolutePath()
                    + "' on line " + node.getLine());
        }

        name = e.attributeValue("name");
        value = e.attributeValue("value");

    } else if (e.selectSingleNode("Name") != null) {
        if (e.selectSingleNode("Name") == null) {
            throw new PlanException("No 'Value' element within element of '" + getFile().getAbsolutePath()
                    + "' on line " + node.getLine());
        }

        Node nameNode = e.selectSingleNode("Name");
        Node valueNode = e.selectSingleNode("Value");

        name = nameNode.getStringValue();
        value = valueNode.getStringValue();
    } else {
        throw new PlanException("Invalid flag element in '" + getFile().getAbsolutePath() + "' on line "
                + node.getLine() + ": No 'Name' or 'Value' defined");
    }

    if (name.trim().isEmpty()) {
        throw new PlanException(
                "Name was emtpy in '" + getFile().getAbsolutePath() + "' on line " + node.getLine());
    }

    if (value.trim().isEmpty()) {
        throw new PlanException(
                "Value was emtpy in '" + getFile().getAbsolutePath() + "' on line " + node.getLine());
    }

    if (NumberUtils.isNumber(value)) {
        try {
            double d = Double.parseDouble(value);
            return new DoubleFlag(name, d);
        } catch (NumberFormatException nfe) {
            Integer i = Integer.parseInt(value);
            return new IntegerFlag(name, i);
        }
    } else if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true")) {
        return new BooleanFlag(name, value.equalsIgnoreCase("true"));
    } else {
        return new StringFlag(name, value);
    }
}

From source file:com.church.gateway.Global.java

/**
 * Parses the theme xml./*from  ww  w  . java  2  s  .  co  m*/
 * 
 * @param xml
 *            the xml
 */
private static void parseThemeXML(String xml) {
    try {
        Document doc = DocumentHelper.parseText(xml);
        Element elmt = doc.getRootElement();
        List l = elmt.selectNodes("/themes/theme");
        Element e = null;
        theme_class = new String[l.size()];
        for (int i = 0; i < l.size(); i++) {
            e = (Element) l.get(i);
            theme_class[i] = e.attribute("class").getValue();
        }
    } catch (DocumentException e) {
        System.out.println(e);
    }
}

From source file:com.church.gateway.Global.java

/**
 * Gets the modified menu xml./*www .  j av a 2 s.  com*/
 * 
 * @param disallowed_modules
 *            the disallowed_modules
 * 
 * @return the modified menu xml
 * 
 * @throws Exception
 *             the exception
 */
public static String getModifiedMenuXml(String[] disallowed_modules) throws Exception {
    Document doc = DocumentHelper.parseText(Global.MENUS);
    Element root = doc.getRootElement();
    List<Node> list = root.selectNodes("//menu/menu");
    for (Node node : list) {
        Element elmt = (Element) node;
        if (elmt.attribute("class") != null) {
            for (String module : disallowed_modules)
                if (elmt.attribute("class").getValue().trim().equals(module))
                    elmt.addAttribute("visible", "false");
        }
    }
    return doc.asXML();
}

From source file:com.cladonia.xml.ExchangerXMLWriter.java

License:Open Source License

protected void writeAttributes(Element element) 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
    for (int i = 0, size = element.attributeCount(); i < size; i++) {
        Attribute attribute = element.attribute(i);
        Namespace ns = attribute.getNamespace();
        if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) {
            String prefix = ns.getPrefix();
            String uri = namespaceStack.getURI(prefix);
            if (!ns.getURI().equals(uri)) { // output a new namespace declaration
                writeNamespace(ns);//from  w  w w.j av a  2  s .  com
                namespaceStack.push(ns);
            }
        }

        writeAttribute(attribute);
    }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writeElement(Element element) throws IOException {
    if (DEBUG)//from  w  w w.j  a  v a  2s.com
        System.out.println("XMLFormatter.writeElement( " + element + ")");
    //      if ( indentMixed) {
    //         super.writeElement( element);
    //      } else {
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean preserveSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //         System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "preserve".equals(value)) {
            preserveSpace = true;
        }
    }

    writePrintln();
    indent();

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();

    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;

            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (!hasElement && !preserveSpace) { // text only
            // we have at least one text node so lets assume
            // that its non-empty
            //            System.out.println( "writeElementContent (Text) ...");
            boolean previousWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = previousWrapText;
        } else if (preserveMixedContent && hasElement && hasText) { // preserve space
            // Mixed content
            //                System.out.println( "writeMixedElementContent ...");
            Node lastNode = writeMixedElementContent(element);

        } else if (preserveSpace) { // preserve space
            // Mixed content
            //                System.out.println( "writePreserveElementContent ...");
            Node lastNode = writePreservedElementContent(element);

        } else { // hasElement && !hasText
            //               System.out.println( "writeElementContent (Element) ...");

            boolean previousWrapText = wrapText;
            wrapText = true;
            ++indentLevel;
            writeElementContent(element);
            --indentLevel;
            wrapText = previousWrapText;

            writePrintln();
            indent();
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
        //         writePrintln();
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;
    //      }
}

From source file:com.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writePreservedElement(Element element) throws IOException {
    if (DEBUG)// ww w. ja va  2s  .  c  om
        System.out.println("XMLFormatter.writePreservedElement( " + element + ")");
    int size = element.nodeCount();
    String qualifiedName = element.getQualifiedName();

    boolean previousTrim = format.isTrimText();
    boolean previousWrapText = wrapText;

    format.setTrimText(false);
    wrapText = false;

    boolean hasElement = false;
    boolean hasText = false;

    // first test to see if this element has mixed content, 
    // if whitespace is significant ...
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);

        if (node instanceof Element) {
            hasElement = true;
        } else if (node instanceof Text) {
            String text = node.getText();

            if (text != null && text.trim().length() > 0) {
                hasText = true;
            }
        }
    }

    Attribute space = element.attribute("space");
    boolean defaultSpace = false;

    if (space != null) {
        String prefix = space.getNamespacePrefix();
        String value = space.getValue();
        //            System.out.println( "prefix = "+prefix+" value = "+value);
        if (prefix != null && "xml".equals(prefix) && "default".equals(value)) {
            defaultSpace = true;
        }
    }

    writer.write("<");
    writer.write(qualifiedName);

    int previouslyDeclaredNamespaces = namespaceStack.size();
    Namespace ns = element.getNamespace();

    if (isNamespaceDeclaration(ns)) {
        namespaceStack.push(ns);
        writeNamespace(ns);
    }

    // Print out additional namespace declarations
    for (int i = 0; i < size; i++) {
        Node node = element.node(i);
        if (node instanceof Namespace) {
            Namespace additional = (Namespace) node;
            if (isNamespaceDeclaration(additional)) {
                namespaceStack.push(additional);
                writeNamespace(additional);
            }
        }
    }

    writeAttributes(element);

    lastOutputNodeType = Node.ELEMENT_NODE;

    if (size <= 0) {
        writeEmptyElementClose(qualifiedName);
    } else {
        writer.write(">");

        if (preserveMixedContent && hasElement && hasText) { // mixed content
            //                System.out.println( "writeMixedElementContent ...");

            Node lastNode = writeMixedElementContent(element);
        } else if (!defaultSpace) { // preserve space
            //                System.out.println( "writePreservedElementContent ...");

            Node lastNode = writePreservedElementContent(element);
        } else {
            //                System.out.println( "writeElementContent ...");

            format.setTrimText(isTrimText());
            boolean prevWrapText = wrapText;
            wrapText = true;
            writeElementContent(element);
            wrapText = prevWrapText;
            format.setTrimText(false);
        }

        writer.write("</");
        writer.write(qualifiedName);
        writer.write(">");
    }

    // remove declared namespaceStack from stack
    while (namespaceStack.size() > previouslyDeclaredNamespaces) {
        namespaceStack.pop();
    }

    lastOutputNodeType = Node.ELEMENT_NODE;

    format.setTrimText(previousTrim);
    wrapText = previousWrapText;
}