Example usage for org.dom4j Element attributeIterator

List of usage examples for org.dom4j Element attributeIterator

Introduction

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

Prototype

Iterator<Attribute> attributeIterator();

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.l2jfree.loginserver.dao.impl.GameserversDAOXml.java

License:Open Source License

/**
 * Load server name from xml/*from   w  w w. j a  v  a2 s . co  m*/
 */
public GameserversDAOXml() {
    InputStream in = null;
    try {
        try {
            in = new FileInputStream("servername.xml");
        } catch (FileNotFoundException e) {
            // just for eclipse development, we have to search in dist folder
            in = new FileInputStream("dist/servername.xml");
        }

        SAXReader reader = new SAXReader();
        Document document = reader.read(in);

        Element root = document.getRootElement();

        // Find all servers_list (should have only one)
        for (Iterator<?> i = root.elementIterator("server"); i.hasNext();) {
            Element server = (Element) i.next();
            Integer id = null;
            String name = null;
            // For each server, read the attributes
            for (Iterator<?> iAttr = server.attributeIterator(); iAttr.hasNext();) {
                Attribute attribute = (Attribute) iAttr.next();
                if (attribute.getName().equals("id")) {
                    id = new Integer(attribute.getValue());
                } else if (attribute.getName().equals("name")) {
                    name = attribute.getValue();
                }
            }
            if (id != null && name != null) {
                Gameservers gs = new Gameservers();
                gs.setServerId(id);
                gs.setServerName(name);
                serverNames.put(id, gs);
            }
        }
        _log.info("Loaded " + serverNames.size() + " server names");
    } catch (FileNotFoundException e) {
        _log.warn("servername.xml could not be loaded : " + e.getMessage(), e);
    } catch (DocumentException e) {
        _log.warn("servername.xml could not be loaded : " + e.getMessage(), e);
    } finally {
        try {
            if (in != null)
                in.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.liferay.alloy.tools.builder.base.BaseBuilder.java

License:Open Source License

protected Document mergeXMLAttributes(Document doc1, Document doc2) {
    Element doc2Root = doc2.getRootElement();
    Element doc1Root = doc1.getRootElement();
    Element docRoot = doc2Root.createCopy();

    docRoot.clearContent();/*from ww w  .  j  a v  a  2 s.c  o  m*/

    if (doc1Root != null) {
        Iterator<Object> attributesIterator = doc1Root.attributeIterator();

        while (attributesIterator.hasNext()) {
            org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

            if (attribute.getName().equals("extends")) {
                continue;
            }

            docRoot.addAttribute(attribute.getName(), attribute.getValue());
        }

        Element descriptionElement = doc1Root.element("description");

        if (descriptionElement != null) {
            docRoot.add(descriptionElement.createCopy());
        }
    }

    DocumentFactory factory = SAXReaderUtil.getDocumentFactory();

    Document doc = factory.createDocument();
    doc.setRootElement(docRoot);

    List<Element> doc2Components = doc2Root.elements(_COMPONENT);

    for (Element doc2Component : doc2Components) {
        Element component = doc2Component.createCopy();

        String name = doc2Component.attributeValue("name");

        Element doc1Component = getComponentNode(doc1, name);

        if (doc1Component != null) {
            Element doc1ComponentDescriptionElement = doc1Component.element("description");

            if (doc1ComponentDescriptionElement != null) {
                Element descriptionElement = component.element("description");

                if (descriptionElement != null) {
                    component.remove(descriptionElement);
                }

                component.add(doc1ComponentDescriptionElement.createCopy());
            }

            Iterator<Object> attributesIterator = doc1Component.attributeIterator();

            while (attributesIterator.hasNext()) {
                org.dom4j.Attribute attribute = (org.dom4j.Attribute) attributesIterator.next();

                component.addAttribute(attribute.getName(), attribute.getValue());
            }

            Element doc1AttributesNode = doc1Component.element(_ATTRIBUTES);

            Element attributesNode = component.element(_ATTRIBUTES);

            if ((doc1AttributesNode != null) && (attributesNode != null)) {
                List<Element> doc1Attributes = doc1AttributesNode.elements(_ATTRIBUTE);

                List<Element> attributes = attributesNode.elements(_ATTRIBUTE);

                for (Element doc1Attribute : doc1Attributes) {
                    Element attribute = getElementByName(attributes, doc1Attribute.elementText("name"));

                    if (attribute != null) {
                        attributesNode.remove(attribute);
                    }

                    attributesNode.add(doc1Attribute.createCopy());
                }
            }

            Element doc1EventsNode = doc1Component.element(_EVENTS);

            Element eventsNode = component.element(_EVENTS);

            if ((doc1EventsNode != null) && (eventsNode != null)) {
                List<Element> doc1Events = doc1EventsNode.elements(_EVENT);

                List<Element> events = eventsNode.elements(_EVENT);

                for (Element doc1Event : doc1Events) {
                    Element event = getElementByName(events, doc1Event.elementText("name"));

                    if (event != null) {
                        eventsNode.add(event);
                    }

                    eventsNode.add(doc1Event.createCopy());
                }
            }
        }

        doc.getRootElement().add(component);
    }

    if (doc1Root != null) {
        List<Element> doc1Components = doc1Root.elements(_COMPONENT);

        for (Element doc1Component : doc1Components) {
            Element component = doc1Component.createCopy();

            String name = doc1Component.attributeValue("name");

            Element doc2Component = getComponentNode(doc2, name);

            if (doc2Component == null) {
                doc.getRootElement().add(component);
            }
        }
    }

    return doc;
}

From source file:com.mpaike.core.config.xml.elementreader.GenericElementReader.java

License:Open Source License

/**
 * Creates a ConfigElementImpl object from the given element.
 * //from  w  w w  .  java2  s . c o m
 * @param element The element to parse
 * @return The GenericConfigElement representation of the given element
 */
@SuppressWarnings("unchecked")
private GenericConfigElement createConfigElement(Element element) {
    // get the name and value of the given element
    String name = element.getName();

    // create the config element object and populate with value
    // and attributes
    GenericConfigElement configElement = new GenericConfigElement(name);
    if ((element.hasContent()) && (element.hasMixedContent() == false)) {
        String value = element.getTextTrim();
        if (value != null && value.length() > 0) {
            if (propertyConfigurer != null) {
                value = propertyConfigurer.resolveValue(value);
            }
            configElement.setValue(value);
        }
    }

    Iterator<Attribute> attrs = element.attributeIterator();
    while (attrs.hasNext()) {
        Attribute attr = attrs.next();
        String attrName = attr.getName();
        String attrValue = attr.getValue();

        if (propertyConfigurer != null) {
            attrValue = propertyConfigurer.resolveValue(attrValue);
        }

        configElement.addAttribute(attrName, attrValue);
    }

    return configElement;
}

From source file:com.nokia.config.SAXConfigParser.java

License:Open Source License

/**
 * Constructor/* w  w  w . ja  v a  2 s  .c  om*/
 * @return list of available configurations that can be built.
 */
public String getConfigs() {
    File file = new File(sysdefFile);
    SAXReader reader = new SAXReader();
    reader.addHandler("/SystemDefinition/build/target", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element row = path.getCurrent();
            Iterator itr = row.attributeIterator();
            while (itr.hasNext()) {
                Attribute child = (Attribute) itr.next();
                String attrName = child.getQualifiedName();
                if (attrName.equals("name")) {
                    configs += (String) child.getValue() + ",";
                }
            }
            row.detach();
        }
    });
    try {
        Document doc = reader.read(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return configs;
}

From source file:com.nokia.helium.sbs.SAXSysdefParser.java

License:Open Source License

/**
 * Constructor// w  w  w. j a  v a  2 s .c  o m
 * 
 * @return list of available configurations that can be built.
 */
public void parseConfig(String nodeToGet) {
    layers = new ArrayList<String>();
    SAXReader reader = new SAXReader();
    reader.addHandler("/SystemDefinition/systemModel/" + nodeToGet, new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element row = path.getCurrent();
            Iterator itr = row.attributeIterator();
            while (itr.hasNext()) {
                Attribute child = (Attribute) itr.next();
                String attrName = child.getQualifiedName();
                if (attrName.equals("name")) {
                    layers.add(child.getValue());
                }
            }
            row.detach();
        }
    });
    try {
        reader.read(sysdefFile);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.sammyun.util.XmlHelper.java

License:Open Source License

/**
 * ?XMLDto(?XML?)//from  w w w.  j a va 2  s.  c o m
 * 
 * @param pStrXml ?XML
 * @param pXPath ("//paralist/row" paralistrowxPath)
 * @return outDto Dto
 */
public static final Dto parseXml2DtoBasedProperty(String pStrXml, String pXPath) {
    Dto outDto = new BaseDto();
    String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    Document document = null;
    try {
        if (pStrXml.indexOf("<?xml") < 0)
            pStrXml = strTitle + pStrXml;
        document = DocumentHelper.parseText(pStrXml);
    } catch (DocumentException e) {
        logger.error(
                "==??:==\nXML??XML DOM?!"
                        + "\n?:");
        e.printStackTrace();
    }
    if (document != null) {
        // ?Xpath?
        Element elRoot = (Element) document.selectSingleNode(pXPath);
        // ??Dto
        for (Iterator it = elRoot.attributeIterator(); it.hasNext();) {
            Attribute attribute = (Attribute) it.next();
            outDto.put(attribute.getName().toLowerCase(), attribute.getData());
        }
    }
    return outDto;
}

From source file:com.sammyun.util.XmlHelper.java

License:Open Source License

/**
 * XMLList(XML?)//from   www. jav  a2 s .c  o m
 * 
 * @param pStrXml ?XML?
 * @return list List
 */
public static final List parseXml2List(String pStrXml) {
    List lst = new ArrayList();
    String strTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    Document document = null;
    try {
        if (pStrXml.indexOf("<?xml") < 0)
            pStrXml = strTitle + pStrXml;
        document = DocumentHelper.parseText(pStrXml);
    } catch (DocumentException e) {
        logger.error(
                "==??:==\nXML??XML DOM?!"
                        + "\n?:");
        e.printStackTrace();
    }
    if (document != null) {
        // ?
        Element elRoot = document.getRootElement();
        // ??
        Iterator elIt = elRoot.elementIterator();
        while (elIt.hasNext()) {
            Element el = (Element) elIt.next();
            Iterator attrIt = el.attributeIterator();
            Dto dto = new BaseDto();
            while (attrIt.hasNext()) {
                Attribute attribute = (Attribute) attrIt.next();
                dto.put(attribute.getName().toLowerCase(), attribute.getData());
            }
            lst.add(dto);
        }
    }

    return lst;
}

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 ww. ja  v a 2 s.c  om
        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.wabacus.config.xml.XmlAssistant.java

License:Open Source License

public XmlElementBean parseXmlValueToXmlBean(Element element) {
    if (element == null)
        return null;
    if (!isLegalNamespaceElement(element)) {
        throw new WabacusConfigLoadingException("namespace" + element.getNamespacePrefix() + "??");
    }/*from   w ww .j a v a2 s . c om*/
    XmlElementBean xebean = new XmlElementBean(element.getName());
    String tagContent = element.getText();
    xebean.setContent(tagContent == null ? "" : tagContent.trim());
    Iterator itAttributes = element.attributeIterator();
    Map<String, String> mProps = new HashMap<String, String>();
    xebean.setMProperties(mProps);
    Attribute eleProps;
    while (itAttributes.hasNext()) {
        eleProps = (Attribute) itAttributes.next();
        mProps.put(eleProps.getName(), eleProps.getValue());
    }
    List<XmlElementBean> lstChildren = null;
    List lstChildElements = element.elements();
    if (lstChildElements != null && lstChildElements.size() > 0) {
        lstChildren = new ArrayList<XmlElementBean>();
        XmlElementBean eleTmp;
        for (Object eleObj : lstChildElements) {
            if (eleObj == null)
                continue;
            eleTmp = parseXmlValueToXmlBean((Element) eleObj);
            if (eleTmp == null)
                continue;
            eleTmp.setParentElement(xebean);
            lstChildren.add(eleTmp);
        }
        xebean.setLstChildElements(lstChildren);
    }
    return xebean;
}

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 );
        // }/*from ww  w .j a  va  2 s  .  c  o  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;
}