Example usage for org.dom4j Element elementIterator

List of usage examples for org.dom4j Element elementIterator

Introduction

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

Prototype

Iterator<Element> elementIterator(QName qName);

Source Link

Document

Returns an iterator over the elements contained in this element which match the given fully qualified name.

Usage

From source file:aml.match.Alignment.java

License:Apache License

private void loadMappingsRDF(String file) throws DocumentException {
    AML aml = AML.getInstance();/*from   www  .j a  va 2 s . c  om*/
    URIMap uris = aml.getURIMap();

    //Open the alignment file using SAXReader
    SAXReader reader = new SAXReader();
    File f = new File(file);

    Document doc = reader.read(f);
    //Read the root, then go to the "Alignment" element
    Element root = doc.getRootElement();
    Element align = root.element("Alignment");
    //Get an iterator over the mappings
    Iterator<?> map = align.elementIterator("map");
    while (map.hasNext()) {
        //Get the "Cell" in each mapping
        Element e = ((Element) map.next()).element("Cell");

        if (e == null)
            continue;
        //Get the source class
        String sourceURI = e.element("entity1").attributeValue("resource");

        //Get the target class
        String targetURI = e.element("entity2").attributeValue("resource");

        //Get the similarity measure
        String measure = e.elementText("measure");
        //Parse it, assuming 1 if a valid measure is not found
        double similarity = 1;
        if (measure != null) {
            try {
                similarity = Double.parseDouble(measure);
                if (similarity < 0 || similarity > 1)
                    similarity = 1;
            } catch (Exception ex) {
                /*Do nothing - use the default value*/}
            ;
        }
        //Get the relation
        String r = e.elementText("relation");
        if (r == null)
            r = "?";
        MappingRelation rel = MappingRelation.parseRelation(StringEscapeUtils.unescapeXml(r));
        //Check if the URIs are listed in the URI map 
        int sourceIndex = uris.getIndex(sourceURI);
        int targetIndex = uris.getIndex(targetURI);
        //If they are, add the mapping to the maps and proceed to next mapping
        if (sourceIndex > -1 && targetIndex > -1) {
            if (sourceIndex < targetIndex)
                add(sourceIndex, targetIndex, similarity, rel);
            else
                add(targetIndex, sourceIndex, similarity, rel);
        }
    }
}

From source file:aml.match.Alignment.java

License:Apache License

private void loadMappingsRDF2(String file) throws DocumentException {
    AML aml = AML.getInstance();/* w  w w .ja  v  a 2  s  .  c o m*/
    URIMap uris = aml.getURIMap();

    //Open the alignment file using SAXReader
    SAXReader reader = new SAXReader();
    File f = new File(file);

    Document doc = reader.read(f);
    //Read the root, then go to the "Alignment" element
    Element root = doc.getRootElement();
    Element align = root.element("Alignment");
    //Get an iterator over the mappings
    Iterator<?> map = align.elementIterator("map");
    while (map.hasNext()) {
        //Get the "Cell" in each mapping
        Element e = ((Element) map.next()).element("Cell");

        if (e == null)
            continue;

        //Get the source class
        String sourceURI = e.element("entity1").element("Class").attributeValue("about");

        //Get the target class
        String targetURI = e.element("entity2").element("Class").element("and").element("Class")
                .attributeValue("about");

        //Get the similarity measure
        String measure = e.elementText("measure");
        //Parse it, assuming 1 if a valid measure is not found
        double similarity = 1;
        if (measure != null) {
            try {
                similarity = Double.parseDouble(measure);
                if (similarity < 0 || similarity > 1)
                    similarity = 1;
            } catch (Exception ex) {
                /*Do nothing - use the default value*/}
            ;
        }

        //Get the relation
        String r = e.elementText("relation");
        if (r == null)
            r = "?";

        MappingRelation rel = MappingRelation.parseRelation(StringEscapeUtils.unescapeXml(r));
        //Check if the URIs are listed in the URI map 

        int sourceIndex = uris.getIndex(sourceURI);

        int targetIndex = uris.getIndex(targetURI);

        //If they are, add the mapping to the maps and proceed to next mapping

        if (sourceIndex > -1 && targetIndex > -1) {
            if (sourceIndex < targetIndex)
                add(sourceIndex, targetIndex, similarity, rel);
            else
                add(targetIndex, sourceIndex, similarity, rel);
        }

    }
}

From source file:aml.match.CompoundAlignment.java

License:Apache License

private void loadMappingsRDF(String file) throws DocumentException {
    AML aml = AML.getInstance();/*from w  w w  .  ja  v a 2s .c  om*/
    URIMap uris = aml.getURIMap();

    //Open the alignment file using SAXReader
    SAXReader reader = new SAXReader();

    File f = new File(file);

    Document doc = reader.read(f);
    //Read the root, then go to the "Alignment" element
    Element root = doc.getRootElement();

    Element align = root.element("Alignment");
    //Get an iterator over the mappings
    Iterator<?> map = align.elementIterator("map");

    while (map.hasNext()) {

        //Get the "Cell" in each mapping
        Element e = ((Element) map.next()).element("Cell");
        if (e == null) {
            continue;
        }
        //Get the source class
        String sourceURI = e.element("entity1").element("Class").attributeValue("about");
        uris.addURI(sourceURI);
        //Get the target class

        //Get the both target classes
        List<Element> elements = e.element("entity2").element("Class").element("and").elements("Class");

        //Get the target 1
        String targetURI = elements.get(0).attributeValue("about");
        uris.addURI(targetURI);
        //Get the target 2
        String target2URI = elements.get(1).attributeValue("about");

        uris.addURI(target2URI);
        //Get the similarity measure
        String measure = e.elementText("measure");

        //Parse it, assuming 1 if a valid measure is not found
        double similarity = 1;
        if (measure != null) {
            try {
                similarity = Double.parseDouble(measure);
                if (similarity < 0 || similarity > 1)
                    similarity = 1;
            } catch (Exception ex) {
                /*Do nothing - use the default value*/}
            ;
        }

        //Get the relation
        String r = e.elementText("relation");
        if (r == null)
            r = "?";
        MappingRelation rel = MappingRelation.parseRelation(StringEscapeUtils.unescapeXml(r));
        //Check if the URIs are listed in the URI map 
        int sourceIndex = uris.getIndex(sourceURI);
        int targetIndex = uris.getIndex(targetURI);
        int targetIndex2 = uris.getIndex(target2URI);
        //If they are, add the mapping to the maps and proceed to next mapping

        if (sourceIndex > -1 && targetIndex > -1 && targetIndex2 > -1) {
            if (sourceIndex < targetIndex)
                add(sourceIndex, targetIndex, targetIndex2, similarity, rel);
            else
                add(targetIndex, sourceIndex, targetIndex2, similarity, rel);
        }

    }
}

From source file:aml.util.MeSHParser.java

License:Apache License

public static void main(String[] args) throws Exception {
    Vector<String> concepts = new Vector<String>();
    Lexicon lexicon = new Lexicon();

    SAXReader reader = new SAXReader();
    File f = new File("store/knowledge/mesh.xml");
    Document doc = reader.read(f);
    Element root = doc.getRootElement();

    Iterator<?> records = root.elementIterator("DescriptorRecord");
    int index = 0;
    while (records.hasNext()) {
        Element concList = ((Element) records.next()).element("ConceptList");
        Iterator<?> conc = concList.elementIterator("Concept");
        while (conc.hasNext()) {
            Element c = (Element) conc.next();
            String conceptName = c.element("ConceptName").elementText("String");
            concepts.add(conceptName);/*from w  w  w  . j  a  v  a  2s  .  co  m*/
            lexicon.add(index, conceptName, LexicalType.LABEL, "", 0.90);

            String casN1Name = c.elementText("CASN1Name");
            if (casN1Name != null)
                lexicon.add(index, casN1Name, LexicalType.FORMULA, "", 0.85);

            Element termList = c.element("TermList");
            Iterator<?> terms = termList.elementIterator("Term");
            while (terms.hasNext()) {
                Element t = (Element) terms.next();
                String termName = t.elementText("String");
                if (!conceptName.equals(termName))
                    lexicon.add(index, termName, LexicalType.EXACT_SYNONYM, "", 0.85);
            }
            index++;
        }
    }
    lexicon.save("store/knowledge/mesh.lexicon");
}

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Return all values who's path matches the given property name as a String
 * array, or an empty array if the if there are no children. This allows you
 * to retrieve several values with the same property name. For example,
 * consider the XML file entry://from  ww w  .  j a v  a 2 s.c o m
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 * 
 * If you call getProperties("foo.bar.prop") will return a string array
 * containing {"some value", "other value", "last value"}.
 *
 * @param name
 *            the name of the property to retrieve
 * @return all child property values for the given node name.
 */
public String[] getProperties(String name) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        if (element == null) {
            // This node doesn't match this part of the property name which
            // indicates this property doesn't exist so return empty array.
            return new String[] {};
        }
    }
    // We found matching property, return names of children.
    Iterator iter = element.elementIterator(propName[propName.length - 1]);
    List<String> props = new ArrayList<String>();
    String value;
    while (iter.hasNext()) {
        // Empty strings are skipped.
        value = ((Element) iter.next()).getTextTrim();
        if (!"".equals(value)) {
            props.add(value);
        }
    }
    String[] childrenNames = new String[props.size()];
    return props.toArray(childrenNames);
}

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Return all values who's path matches the given property name as a String
 * array, or an empty array if the if there are no children. This allows you
 * to retrieve several values with the same property name. For example,
 * consider the XML file entry:/*  w ww. j  av  a  2  s .  c o  m*/
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 * 
 * If you call getProperties("foo.bar.prop") will return a string array
 * containing {"some value", "other value", "last value"}.
 *
 * @param name
 *            the name of the property to retrieve
 * @return all child property values for the given node name.
 */
public Iterator getChildProperties(String name) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        element = element.element(propName[i]);
        if (element == null) {
            // This node doesn't match this part of the property name which
            // indicates this property doesn't exist so return empty array.
            return Collections.EMPTY_LIST.iterator();
        }
    }
    // We found matching property, return values of the children.
    Iterator iter = element.elementIterator(propName[propName.length - 1]);
    ArrayList<String> props = new ArrayList<String>();
    while (iter.hasNext()) {
        props.add(((Element) iter.next()).getText());
    }
    return props.iterator();
}

From source file:architecture.common.xml.XmlProperties.java

License:Apache License

/**
 * Sets a property to an array of values. Multiple values matching the same
 * property is mapped to an XML file as multiple elements containing each
 * value. For example, using the name "foo.bar.prop", and the value string
 * array containing {"some value", "other value", "last value"} would
 * produce the following XML://from  w  ww  .ja  v  a  2s . c  o m
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name
 *            the name of the property.
 * @param values
 *            the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);

}

From source file:architecture.ee.util.xml.XmlProperties.java

License:Apache License

/**
 * Sets a property to an array of values. Multiple values matching the same
 * property is mapped to an XML file as multiple elements containing each
 * value. For example, using the name "foo.bar.prop", and the value string
 * array containing {"some value", "other value", "last value"} would
 * produce the following XML:// ww w  . j  a v  a  2  s.  c  o  m
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name
 *            the name of the property.
 * @param values
 *            the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(XmlEscapers.xmlContentEscaper().escape(value));// StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);

}

From source file:au.gov.ansto.bragg.cicada.core.internal.Algorithm_.java

License:Open Source License

private boolean checkSourcePortExistence(Element rootElement) {
    //      List<InConfiguration> inConfigurationList = new LinkedList<InConfiguration>();
    for (Iterator<?> iter = rootElement.elementIterator("ins"); iter.hasNext();) {
        Element ins = (Element) iter.next();
        if (ins.hasContent())
            return true;
    }/*from   w ww  .  j a  v a  2s.co m*/
    return false;
}

From source file:au.gov.ansto.bragg.process.parse.Parse.java

License:Open Source License

public static List<ConfigurationItem> parseConfiguration(File filename) throws DocumentException {
    List<ConfigurationItem> propertyList = new LinkedList<ConfigurationItem>();
    Document document = readFile(filename);
    Element rootElement = document.getRootElement();
    //      List<?> list = rootElement.selectNodes("//extension");
    //      System.out.println(list.toString());
    for (Iterator<?> iter = rootElement.elementIterator("extension"); iter.hasNext();) {
        Element item = (Element) iter.next();
        if (item.getName().equals("extension")
                && item.attributeValue("point").matches("au.gov.ansto.bragg.cicada.configuration")) {
            ConfigurationItem configuration = new ConfigurationItem(Integer.valueOf(getAttribute(item, "id")),
                    getAttribute(item, "name"), getAttribute(item, "version"), getAttribute(item, "class"),
                    getAttribute(item, "default"));
            propertyList.add(configuration);
        }//from   w w  w.j  av a  2  s  .c  om
    }
    return propertyList;
}