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();

Source Link

Document

Returns an iterator over all this elements child elements.

Usage

From source file:com.glaf.jbpm.config.JbpmExtensionReader.java

License:Apache License

public List<Extension> readActions(java.io.InputStream inputStream) {
    List<Extension> extensions = new java.util.ArrayList<Extension>();
    SAXReader xmlReader = new SAXReader();
    try {/*from ww  w .j av  a 2  s.  co  m*/
        Document doc = xmlReader.read(inputStream);
        Element root = doc.getRootElement();
        String x_type = root.attributeValue("type");
        List<?> actions = root.elements("action");
        Iterator<?> iter = actions.iterator();
        while (iter.hasNext()) {
            Element element = (Element) iter.next();
            Extension extension = new Extension();
            extension.setProcessName(element.attributeValue("processName"));
            extension.setTaskName(element.attributeValue("taskName"));
            extension.setName(element.attributeValue("name"));
            extension.setType(x_type);
            extension.setDescription(element.elementTextTrim("description"));
            Iterator<?> it99 = element.elementIterator();
            while (it99.hasNext()) {
                Element elem = (Element) it99.next();
                String propertyName = elem.getName();
                String propertyValue = elem.getTextTrim();
                if (StringUtils.isNotEmpty(propertyValue)) {
                    ExtensionField extensionField = new ExtensionField();
                    extensionField.setName(propertyName.trim());
                    extensionField.setValue(propertyValue.trim());
                    extension.addField(extensionField);
                }
            }
            if (element.elementText("sql") != null) {
                ExtensionField extensionField = new ExtensionField();
                extensionField.setName("sql");
                extensionField.setValue(element.elementTextTrim("sql"));
                extension.addField(extensionField);

            }
            if (element.elementText("handlers") != null) {
                ExtensionField extensionField = new ExtensionField();
                extensionField.setName("handlers");
                extensionField.setValue(element.elementTextTrim("handlers"));
                extension.addField(extensionField);
            }

            Element parametersE = element.element("parameters");
            if (parametersE != null) {
                List<?> parameters = parametersE.elements("parameter");
                Iterator<?> it = parameters.iterator();
                while (it.hasNext()) {
                    Element elem = (Element) it.next();
                    String propertyName = elem.attributeValue("name");
                    String type = elem.attributeValue("type");
                    String propertyValue = null;
                    if (elem.attribute("value") != null) {
                        propertyValue = elem.attributeValue("value");
                    } else {
                        propertyValue = elem.getTextTrim();
                    }
                    if (StringUtils.isNotEmpty(propertyName) && StringUtils.isNotEmpty(propertyValue)) {
                        ExtensionParam extensionParam = new ExtensionParam();
                        extensionParam.setName(propertyName.trim());
                        extensionParam.setValue(propertyValue.trim());
                        extensionParam.setType(type);
                        extension.addParam(extensionParam);
                    }
                }
            }

            extensions.add(extension);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    return extensions;
}

From source file:com.glaf.jbpm.util.CustomFieldInstantiator.java

License:Apache License

private static Object getMapValue(Element mapElement, Map<Object, Object> map) {
    Class<?> keyClass = String.class;
    String keyType = mapElement.attributeValue("key-type");
    if (keyType != null) {
        keyClass = ClassLoaderUtil.classForName(keyType);
    }/*from w ww .j a v  a  2  s .co  m*/
    Class<?> valueClass = String.class;
    String valueType = mapElement.attributeValue("value-type");
    if (valueType != null) {
        valueClass = ClassLoaderUtil.classForName(valueType);
    }
    Iterator<?> iter = mapElement.elementIterator();
    while (iter.hasNext()) {
        Element element = (Element) iter.next();
        Element keyElement = element.element("key");
        Element valueElement = element.element("value");
        Class<?> clazz = null;
        String className = element.attributeValue("value-type");
        if (className != null) {
            clazz = ClassLoaderUtil.classForName(className);
        }
        if (clazz == null) {
            clazz = valueClass;
        }
        map.put(getValue(keyClass, keyElement), getValue(clazz, valueElement));
    }
    return map;
}

From source file:com.glaf.jbpm.util.CustomFieldInstantiator.java

License:Apache License

private static Object getCollectionValue(Element collectionElement, Collection<Object> collection) {
    Class<?> elementClass = String.class;
    String elementType = collectionElement.attributeValue("element-type");
    if (elementType != null) {
        elementClass = ClassLoaderUtil.classForName(elementType);
    }/*ww  w .  j a  v  a 2s.com*/
    Iterator<?> iter = collectionElement.elementIterator();
    while (iter.hasNext()) {
        Element element = (Element) iter.next();
        collection.add(getValue(elementClass, element));
    }
    return collection;
}

From source file:com.globalsight.everest.segmentationhelper.XmlLoader.java

License:Apache License

public static SegmentationRule parseSegmentationRule(String p_xmlText) throws Exception {
    Document doc = parseWithSAX(p_xmlText);
    Element root = doc.getRootElement();
    SegmentationRule segmentationRule = new SegmentationRule();
    SrxHeader header = new SrxHeader();
    HashMap<String, String> formathandle = new HashMap<String, String>();
    HashMap<String, ArrayList<Rule>> langrules = new HashMap<String, ArrayList<Rule>>();
    ArrayList<LanguageMap> langruleMap = new ArrayList<LanguageMap>();

    segmentationRule.setRootName(root.getQualifiedName());
    // Now it only supports SRX2.0.
    segmentationRule.setVersion(root.attributeValue("version"));

    Element headerElement = root.element("header");
    Element bodyElement = root.element("body");
    Element maprulesElement = bodyElement.element("maprules");
    // to get SRX header informations
    if (headerElement != null) {
        // option segmentsubflows
        String segsub = headerElement.attributeValue("segmentsubflows");
        if (segsub == null) {
            // Take default value.
            header.isSegmentsubflows(true);
        } else {//www  .  j  a v  a  2 s  .com
            header.isSegmentsubflows(!segsub.equalsIgnoreCase("no"));
        }

        // option cascade
        String cascade = headerElement.attributeValue("cascade");
        if (cascade == null) {
            header.isCascade(false);
        } else {
            header.isCascade(!cascade.equalsIgnoreCase("no"));
        }

        // elements : formathandle
        List formatList = headerElement.elements("formathandle");
        Iterator formatIter = formatList != null ? formatList.iterator() : (new ArrayList()).iterator();
        // If the header does not contain formathandle elements
        // we use the default values
        if (!formatIter.hasNext()) {
            formathandle.put("start", "no");
            formathandle.put("end", "yes");
            formathandle.put("isolated", "no");
        }
        // If the header contains formathandle elements
        // we use the values specified by formathandle elements
        else {
            while (formatIter.hasNext()) {
                Element formatElement = (Element) formatIter.next();
                String type = formatElement.attributeValue("type");
                String include = formatElement.attributeValue("include");
                formathandle.put(type, include);
            }
        }
        header.setFormatHandle(formathandle);

        // okpsrx extension
        Element okpOptions = headerElement.element("options");
        Element okpSample = headerElement.element("sample");
        Element okpRangeRule = headerElement.element("rangeRule");

        if (okpOptions != null) {
            String oneSegmentIncludesAll = okpOptions.attributeValue("oneSegmentIncludesAll");
            String trimLeadingWs = okpOptions.attributeValue("trimLeadingWhitespaces");
            String trimTrailingWs = okpOptions.attributeValue("trimTrailingWhitespaces");

            header.setOneSegmentIncludesAll("yes".equalsIgnoreCase(oneSegmentIncludesAll));
            header.setTrimLeadingWhitespaces("yes".equalsIgnoreCase(trimLeadingWs));
            header.setTrimTrailingWhitespaces("yes".equalsIgnoreCase(trimTrailingWs));
        }

        if (okpSample != null) {
            String language = okpSample.attributeValue("language");
            String useMappedRules = okpSample.attributeValue("useMappedRules");
            String sample = okpSample.getText();

            header.setSample(sample);
            header.setSampleLanguage(language);
            header.setUseMappedRulesForSample(!"no".equalsIgnoreCase(useMappedRules));
        }

        if (okpRangeRule != null) {
            String rangeRule = okpRangeRule.getText();
            header.setRangeRule(rangeRule);
        }
    }

    if (bodyElement != null) {
        Element languagerulesElement = bodyElement.element("languagerules");
        if (languagerulesElement != null) {
            Iterator languageruleIter = languagerulesElement.elementIterator();
            while (languageruleIter.hasNext()) {
                Element languageruleElement = (Element) languageruleIter.next();
                String languageName = languageruleElement.attributeValue("languagerulename");
                Iterator ruleIter = languageruleElement.elementIterator();
                ArrayList<Rule> rules = new ArrayList<Rule>();
                while (ruleIter.hasNext()) {
                    Element ruleSub = (Element) ruleIter.next();
                    String breakvalue = ruleSub.attributeValue("break");
                    String beforebreak = ruleSub.elementText("beforebreak");
                    String afterbreak = ruleSub.elementText("afterbreak");
                    Rule rule = new Rule();
                    if (breakvalue == null) {
                        // Take default value.
                        rule.isBreak(true);
                    } else {
                        if (breakvalue.equalsIgnoreCase("no")) {
                            rule.isBreak(false);
                        } else {
                            rule.isBreak(true);
                        }
                    }

                    // System.out.println(rule.getBreak());
                    rule.setAfterBreak(afterbreak);
                    rule.setBeforeBreak(beforebreak);
                    rules.add(rule);
                }
                langrules.put(languageName, rules);
            }
        } // end languageruleElement
    } // end bodyElement

    if (maprulesElement != null) {
        Iterator languagemapIter = maprulesElement.elementIterator();
        while (languagemapIter.hasNext()) {
            Element languagemapElement = (Element) languagemapIter.next();
            String languagepattern = languagemapElement.attributeValue("languagepattern");
            String languagerulename = languagemapElement.attributeValue("languagerulename");
            LanguageMap langMap = new LanguageMap();
            langMap.setLanguagePattern(languagepattern);
            langMap.setLanguageruleName(languagerulename);
            langruleMap.add(langMap);
        }
    }

    segmentationRule.setHeader(header);
    segmentationRule.setLanguageMap(langruleMap);
    segmentationRule.setRules(langrules);
    return segmentationRule;
}

From source file:com.globalsight.terminology.ConceptHelper.java

License:Apache License

public static String fixConceptXml(String xml, long p_entryId) {
    xml = "<conceptGrp>" + xml + "</conceptGrp>";
    Entry entry = new Entry(xml);
    Element root = entry.getDom().getRootElement();

    String newStr = new String();

    boolean isHaveIdNode = false;

    for (Iterator it = root.elementIterator(); it.hasNext();) {
        Element elmt = (Element) it.next();

        if (elmt.getName().equals("concept")) {
            elmt.setText(Long.toString(p_entryId));
            isHaveIdNode = true;/*from w  ww. java  2 s  .c  o  m*/
        }

        newStr = newStr + elmt.asXML();
    }

    if (!isHaveIdNode) {
        newStr = "<concept>" + p_entryId + "</concept>" + newStr;
    }

    return newStr;
}

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * <p>Checks that an TBX entry is consistent with the termbase
 * definition (at least one term per language, languages defined
 * in termbase, required fields present, etc).</p>
 *
 * <p>For now, check that we have at least one term. General entry
 * structure validation should have been done by the XML Parser
 * that parsed the entry into DOM - using the EntryStructure
 * schema. We also remove empty fields and groups.</p>
 *
 * TO BE COMPLETED/*from  w ww .  jav a2 s . c  o m*/
 */
public static void normalizeTbxEntry(Entry p_entry, Definition p_definition) throws TermbaseException {
    try {
        boolean ok = false;
        // Remove empty fields and then check if there's at least
        // one term in the entry.
        pruneEntry(p_entry);

        Document dom = p_entry.getDom();
        Element root = dom.getRootElement();

        //termEntry-level
        for (Iterator it = root.elementIterator(); it.hasNext();) {
            Element tnode = (Element) it.next();

            if (tnode.getName().equals("langSet")) {
                //langSet-level
                for (Iterator it2 = tnode.elementIterator(); it2.hasNext();) {
                    Element lnode = (Element) it2.next();

                    if (lnode.getName().equals("ntig")) {
                        //ntig-level
                        for (Iterator it3 = lnode.elementIterator(); it3.hasNext();) {
                            Element ntignode = (Element) it3.next();

                            if (ntignode.getName().equals("termGrp")) {
                                //term-node
                                for (Iterator it4 = ntignode.elementIterator(); it4.hasNext();) {
                                    Element termnode = (Element) it4.next();

                                    if (termnode.getName().equals("term")) {
                                        String text = termnode.getText();
                                        if (text != null && text.length() > 0) {
                                            ok = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (ok)
                                break;
                        }
                        if (!ok)
                            invalidEntry("no terms defined");
                    }
                    if (lnode.getName().equals("tig")) {
                        //tig-level
                        for (Iterator it3 = lnode.elementIterator(); it3.hasNext();) {
                            Element tignode = (Element) it3.next();

                            if (tignode.getName().equals("term")) {
                                String text = tignode.getText();
                                if (text != null && text.length() > 0) {
                                    ok = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (ok)
                        break;
                }
            }
            if (ok)
                break;
        }
        if (!ok) {
            invalidEntry("no languages defined");
        }
    } catch (TermbaseException e) {
        throw e;
    } catch (Exception e) {
        invalidEntry(e.getMessage());
    }
}

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * <p>Checks that an entry is consistent with the termbase
 * definition (at least one term per language, languages defined
 * in termbase, required fields present, etc).</p>
 *
 * <p>For now, check that we have at least one term. General entry
 * structure validation should have been done by the XML Parser
 * that parsed the entry into DOM - using the EntryStructure
 * schema. We also remove empty fields and groups.</p>
 *
 * TO BE COMPLETED/*from  w w w  .  jav  a 2 s . c  o  m*/
 */
static public void normalizeEntry(Entry p_entry, Definition p_definition) throws TermbaseException {
    try {
        boolean ok = false;

        // Remove empty fields and then check if there's at least
        // one term in the entry.
        pruneEntry(p_entry);

        Document dom = p_entry.getDom();
        Element root = dom.getRootElement();

        // CONCEPT-LEVEL
        for (Iterator it = root.elementIterator(); it.hasNext();) {
            Element cnode = (Element) it.next();

            if (cnode.getName().equals("languageGrp")) {
                // LANGUAGE-LEVEL
                for (Iterator it2 = cnode.elementIterator(); it2.hasNext();) {
                    Element lnode = (Element) it2.next();

                    if (lnode.getName().equals("termGrp")) {
                        // TERM-LEVEL
                        for (Iterator it3 = lnode.elementIterator(); it3.hasNext();) {
                            Element tnode = (Element) it3.next();

                            if (tnode.getName().equals("term")) {
                                String text = tnode.getText();
                                if (text != null && text.length() > 0) {
                                    ok = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (ok)
                        break;
                }

                if (!ok) {
                    invalidEntry("no terms defined");
                }
            }

            if (ok)
                break;
        }

        if (!ok) {
            invalidEntry("no languages defined");
        }
    } catch (TermbaseException e) {
        throw e;
    } catch (Exception e) {
        invalidEntry(e.getMessage());
    }
}

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

private static void mergeTbxInnerGroups(Element p_one, Element p_two, NodeComparator p_comp) {
    for (Iterator it = p_two.elementIterator(); it.hasNext();) {
        Element node = (Element) it.next();
        it.remove();//from w w w  . j a va  2s.c om
        node.detach();

        String name = node.getName();

        if (name.equals("langSet")) {
            mergeLangSet(p_one, p_two, node, p_comp);
        }
    }
}

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * Used to merge Tag ntig or tig of TBX files. 
 * TBX files may contain several same languages in a single termentry,
 * when merging two new files, all the same languages should totally considered.
 * @param sameLangSetsInATermentry// www  .j  av a2s  . co  m
 * @param p_one
 * @param p_two
 * @param p_comp
 */
private static void mergeTbxNtigsOrTigs(List sameLangSetsInATermentry, Element p_one, Element p_two,
        NodeComparator p_comp) {
    for (Iterator it = p_two.elementIterator(); it.hasNext();) {
        Element node = (Element) it.next();
        it.remove();
        node.detach();

        String name = node.getName();

        if (name.equals("ntig")) {
            String newTerm = node.element("termGrp").element("term").getText();
            List<String> oldTerms = new ArrayList<String>();

            for (int i = 0; i < sameLangSetsInATermentry.size(); i++) {
                Element tmp = (Element) sameLangSetsInATermentry.get(i);

                List<Element> ntigs = tmp.elements("ntig");
                for (int j = 0; j < ntigs.size(); j++) {
                    try {
                        String oldTerm = ntigs.get(j).element("termGrp").element("term").getText();
                        oldTerms.add(oldTerm);
                    } catch (Exception e) {
                        //langSet or termGrp is empty
                    }
                }
            }
            if (!oldTerms.contains(newTerm)) {
                p_one.add(node);
            }
        }
        if (name.equals("tig")) {
            String newTerm = node.element("term").getText();
            List<String> oldTerms = new ArrayList<String>();

            for (int i = 0; i < sameLangSetsInATermentry.size(); i++) {
                Element tmp = (Element) sameLangSetsInATermentry.get(i);

                List<Element> tigs = p_one.elements("tig");
                for (int j = 0; j < tigs.size(); j++) {
                    try {
                        String oldTerm = tigs.get(j).element("term").getText();
                        oldTerms.add(oldTerm);
                    } catch (Exception e) {
                        //langSet or termGrp is empty
                    }
                }
            }
            if (!oldTerms.contains(newTerm)) {
                p_one.add(node);
            }
        }
    }
}

From source file:com.globalsight.terminology.util.GSEntryParse.java

License:Apache License

@Override
public TbConcept getConceptFromXml(Element root, SessionInfo p_session) {
    String value;//from  www  .  j  ava 2 s  .  com
    String domain = "*unknown*";
    String project = "*unknown*";
    String status = "proposed";
    StringBuffer xml = new StringBuffer();

    // Extract values of indexed concept attributes.
    // TODO: this has to use the TbDefinition.
    if ((value = root.valueOf("/conceptGrp/descripGrp/descrip[@type='domain']")) != null
            && value.length() > 0) {
        domain = value;
    } else if ((value = root.valueOf("/conceptGrp/descrip[@type='domain']")) != null && value.length() > 0) {
        domain = value;
    }

    if ((value = root.valueOf("/conceptGrp/descripGrp/descrip[@type='project']")) != null
            && value.length() > 0) {
        project = value;
    } else if ((value = root.valueOf("/conceptGrp/descrip[@type='project']")) != null && value.length() > 0) {
        project = value;
    }

    if ((value = root.valueOf("/conceptGrp/descripGrp/descrip[@type='status']")) != null
            && value.length() > 0) {
        status = value;
    } else if ((value = root.valueOf("/conceptGrp/descrip[@type='status']")) != null && value.length() > 0) {
        status = value;
    }

    // Prepare concept nodes for storage
    for (Iterator it = root.elementIterator(); it.hasNext();) {
        Element elmt = (Element) it.next();

        if (!elmt.getName().equals("languageGrp")) {
            xml.append(elmt.asXML());
        }
    }

    TbConcept tc = new TbConcept();
    tc.setDomain(domain);
    tc.setStatus(status);
    tc.setProject(project);
    tc.setXml(xml.toString());
    tc.setCreationBy(p_session.getUserName());
    Timestamp ts = new Timestamp(p_session.getTimestamp().getTime());
    tc.setCreationDate(ts);

    return tc;
}