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.glaf.jbpm.config.JbpmExtensionReader.java

License:Apache License

public List<Extension> readTasks(java.io.InputStream inputStream) {
    List<Extension> extensions = new java.util.ArrayList<Extension>();
    SAXReader xmlReader = new SAXReader();
    try {/*from w w  w.j a v  a 2s .  c  o  m*/
        Document doc = xmlReader.read(inputStream);
        Element root = doc.getRootElement();
        String x_type = root.attributeValue("type");
        List<?> rows = root.elements("taskmgr");
        Iterator<?> iterator = rows.iterator();
        while (iterator.hasNext()) {
            Element element = (Element) iterator.next();
            Extension extension = new Extension();
            extension.setProcessName(element.attributeValue("processName"));
            extension.setTaskName(element.attributeValue("taskName"));
            extension.setType(x_type);
            if (element.elementTextTrim("taskMgmtType") != null) {
                ExtensionField extensionField = new ExtensionField();
                extensionField.setName("taskMgmtType");
                extensionField.setValue(element.elementTextTrim("taskMgmtType"));
                extension.addField(extensionField);
            }
            Element propertiesE = element.element("properties");
            if (propertiesE != null) {
                List<?> properties = propertiesE.elements("property");
                Iterator<?> iter = properties.iterator();
                while (iter.hasNext()) {
                    Element elem = (Element) iter.next();
                    String propertyName = elem.attributeValue("key");
                    String propertyValue = null;
                    if (elem.attribute("value") != null) {
                        propertyValue = elem.attributeValue("value");
                    } else {
                        propertyValue = elem.getTextTrim();
                    }
                    if (StringUtils.isNotEmpty(propertyName) && StringUtils.isNotEmpty(propertyValue)) {
                        ExtensionField extensionField = new ExtensionField();
                        extensionField.setName(propertyName.trim());
                        extensionField.setValue(propertyValue.trim());
                        extension.addField(extensionField);
                    }
                }
            }
            if (element.elementText("handlers") != null) {
                ExtensionField extensionField = new ExtensionField();
                extensionField.setName("handlers");
                extensionField.setValue(element.elementTextTrim("handlers"));
                extension.addField(extensionField);
            }
            extensions.add(extension);
        }

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

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  a  v a 2s  . c o  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.config.JbpmExtensionWriter.java

License:Apache License

public Document write(List<Extension> extensions) {
    Document doc = DocumentHelper.createDocument();
    if (extensions != null && extensions.size() > 0) {
        Element root = doc.addElement("bpm-cfg");
        Iterator<Extension> iterator = extensions.iterator();
        while (iterator.hasNext()) {
            Extension extension = iterator.next();
            if (root.attribute("type") == null && extension.getType() != null) {
                root.addAttribute("type", extension.getType());
            }/*from   w w w.ja v a2s. c  om*/
            if (StringUtils.isNotEmpty(extension.getTaskName())) {
                Element element = root.addElement("taskmgr");
                element.addAttribute("processName", extension.getProcessName());
                element.addAttribute("taskName", extension.getTaskName());
                if (extension.getFieldValue("taskMgmtType") != null) {
                    Element elem = element.addElement("taskMgmtType");
                    elem.setText(extension.getFieldValue("taskMgmtType"));
                }
                if (extension.getFieldValue("handlers") != null) {
                    Element elem = element.addElement("handlers");
                    elem.setText(extension.getFieldValue("handlers"));
                }
                if (extension.getFields() != null && extension.getFields().size() > 0) {
                    Element elem = element.addElement("properties");
                    Iterator<ExtensionField> iter = extension.getFields().values().iterator();
                    while (iter.hasNext()) {
                        ExtensionField field = iter.next();
                        Element e = elem.addElement("property");
                        e.addAttribute("key", field.getName());
                        e.addCDATA(field.getValue());
                    }
                }
            } else if (StringUtils.isNotEmpty(extension.getName())) {
                Element element = root.addElement("action");
                element.addAttribute("processName", extension.getProcessName());
                element.addAttribute("name", extension.getName());
                if (extension.getFieldValue("sql") != null) {
                    Element elem = element.addElement("sql");
                    elem.addCDATA(extension.getFieldValue("sql"));
                }
                if (extension.getFieldValue("handlers") != null) {
                    Element elem = element.addElement("handlers");
                    elem.setText(extension.getFieldValue("handlers"));
                }
                if (extension.getParams() != null && extension.getParams().size() > 0) {
                    Element em = element.addElement("parameters");
                    Iterator<ExtensionParam> iter = extension.getParams().iterator();
                    while (iter.hasNext()) {
                        ExtensionParam param = iter.next();
                        Element e = em.addElement("parameter");
                        e.addAttribute("name", param.getName());
                        e.addAttribute("type", param.getType());
                        e.addCDATA(param.getValue());
                    }
                }
            }
        }
    }
    return doc;
}

From source file:com.glaf.jbpm.tag.JbpmProcessImageTag.java

License:Apache License

private int[] extractBoxConstraint(Element root) {
    int[] result = new int[4];
    String nodeName = currentToken.getNode().getName();
    XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
    Element node = (Element) xPath.selectSingleNode(root);
    if (node != null) {
        result[0] = Integer.parseInt(node.attribute("x").getValue());
        result[1] = Integer.parseInt(node.attribute("y").getValue());
        result[2] = Integer.parseInt(node.attribute("width").getValue());
        result[3] = Integer.parseInt(node.attribute("height").getValue());
    }// w  w  w .  j a  v a  2s.co m
    return result;
}

From source file:com.glaf.jbpm.tag.JbpmProcessImageTag.java

License:Apache License

private int[] extractBoxConstraint(Element root, Token token) {
    int[] result = new int[4];
    String nodeName = token.getNode().getName();
    XPath xPath = new DefaultXPath("//node[@name='" + nodeName + "']");
    Element node = (Element) xPath.selectSingleNode(root);
    if (node != null) {
        result[0] = Integer.parseInt(node.attribute("x").getValue());
        result[1] = Integer.parseInt(node.attribute("y").getValue());
        result[2] = Integer.parseInt(node.attribute("width").getValue());
        result[3] = Integer.parseInt(node.attribute("height").getValue());
    }/*from www.j  av a 2  s  . c  o  m*/
    return result;
}

From source file:com.glaf.jbpm.tag.JbpmProcessImageTag.java

License:Apache License

private int[] extractImageDimension(Element root) {
    int[] result = new int[2];
    result[0] = Integer.parseInt(root.attribute("width").getValue());
    result[1] = Integer.parseInt(root.attribute("height").getValue());
    return result;
}

From source file:com.globalsight.everest.edit.offline.OfflineEditManagerLocal.java

License:Apache License

/**
 * Only when the source file is XLF, need re-wrap the off-line down-loaded
 * XLIFF file.// w  w  w  .ja v  a  2 s  .  c o  m
 */
private void reWrapXliff(Document doc, HashSet<Long> jobIds) {
    Element root = doc.getRootElement();
    Element bodyElement = root.element(XliffConstants.FILE).element(XliffConstants.BODY);

    // Find TU elements that are from XLF source file.
    List<Element> xlfTuElements = new ArrayList<Element>();
    for (Iterator i = bodyElement.elementIterator(XliffConstants.TRANS_UNIT); i.hasNext();) {
        Element foo = (org.dom4j.Element) i.next();
        if (isSrcFileXlf(foo, jobIds)) {
            xlfTuElements.add(foo);
        }
    }

    if (xlfTuElements.size() == 0)
        return;

    //
    StringBuffer xliffString = new StringBuffer();
    xliffString.append("<?xml version=\"1.0\"?>");
    xliffString.append("<xliff version=\"1.2\">");
    xliffString.append("<file>");
    xliffString.append("<body>");
    Attribute stateAttr = null;
    ArrayList<String> trgStates = new ArrayList<String>();
    for (Element foo : xlfTuElements) {
        Element sourceElement = foo.element(XliffConstants.SOURCE);
        String sourceContent = sourceElement.asXML();
        sourceContent = sourceContent.replaceFirst("<" + XliffConstants.SOURCE + "[^>]*>", "");
        sourceContent = sourceContent.replace("</" + XliffConstants.SOURCE + ">", "");

        Element targetElement = foo.element(XliffConstants.TARGET);

        String targetContent = targetElement.asXML();
        targetContent = targetContent.replaceFirst("<" + XliffConstants.TARGET + "[^>]*>", "");
        targetContent = targetContent.replace("</" + XliffConstants.TARGET + ">", "");

        xliffString.append("<trans-unit>");
        xliffString.append("<source>").append(sourceContent).append("</source>");
        xliffString.append("<target>").append(targetContent).append("</target>");
        xliffString.append("</trans-unit>");

        // Store the state attributes in sequence
        stateAttr = targetElement.attribute(XliffConstants.STATE);
        if (stateAttr == null) {
            trgStates.add("");
        } else {
            trgStates.add(stateAttr.getValue());
        }
    }
    xliffString.append("</body>");
    xliffString.append("</file>");
    xliffString.append("</xliff>");

    // Extract it to get re-wrapped segments.
    DiplomatAPI api = new DiplomatAPI();
    api.setEncoding("UTF-8");
    api.setLocale(new Locale("en_US"));
    api.setInputFormat("xlf");
    api.setSentenceSegmentation(false);
    api.setSegmenterPreserveWhitespace(true);
    api.setSourceString(xliffString.toString());

    ArrayList<String> sourceArray = new ArrayList<String>();
    ArrayList<String> targetArray = new ArrayList<String>();

    try {
        api.extract();
        Output output = api.getOutput();

        for (Iterator it = output.documentElementIterator(); it.hasNext();) {
            DocumentElement element = (DocumentElement) it.next();

            if (element instanceof TranslatableElement) {
                TranslatableElement trans = (TranslatableElement) element;

                SegmentNode src = (SegmentNode) (trans.getSegments().get(0));
                if (trans.getXliffPartByName().equals("source")) {
                    sourceArray.add("<source>" + replaceEntity(src.getSegment()) + "</source>");
                } else if (trans.getXliffPartByName().equals("target")) {
                    targetArray.add("<target>" + replaceEntity(src.getSegment()) + "</target>");
                }
            }
        }
    } catch (Exception e) {
        if (s_category.isDebugEnabled()) {
            s_category.error(e.getMessage(), e);
        }
    }

    // Replace source/target elements
    int index = 0;
    if (sourceArray != null && targetArray != null) {
        for (Iterator i = bodyElement.elementIterator(XliffConstants.TRANS_UNIT); i.hasNext();) {
            if (index >= sourceArray.size()) {
                break;
            }

            Element foo = (org.dom4j.Element) i.next();
            if (!isSrcFileXlf(foo, jobIds)) {
                continue;
            }

            TuImpl tu = getTu(foo, jobIds);
            GxmlElement srcGxmlElement = tu.getSourceTuv().getGxmlElement();
            String newSrc = "<segment>" + GxmlUtil.stripRootTag(sourceArray.get(index)) + "</segment>";
            GxmlElement trgGxmlElement = SegmentUtil2.getGxmlElement(newSrc);
            newSrc = SegmentUtil2.adjustSegmentAttributeValues(srcGxmlElement, trgGxmlElement, "xlf");
            newSrc = "<source>" + GxmlUtil.stripRootTag(newSrc) + "</source>";
            Element sourceElement = foo.element(XliffConstants.SOURCE);
            Element newSourceElement = getDom(newSrc).getRootElement();
            foo.remove(sourceElement);
            foo.add(newSourceElement);

            String newTrg = "<segment>" + GxmlUtil.stripRootTag(targetArray.get(index)) + "</segment>";
            trgGxmlElement = SegmentUtil2.getGxmlElement(newTrg);
            newTrg = SegmentUtil2.adjustSegmentAttributeValues(srcGxmlElement, trgGxmlElement, "xlf");
            newTrg = "<target>" + GxmlUtil.stripRootTag(newTrg) + "</target>";
            Element newTargetElement = getDom(newTrg).getRootElement();
            Element targetElement = foo.element(XliffConstants.TARGET);
            foo.remove(targetElement);
            // If target has "state" attribute, it should be preserved.
            try {
                if (!"".equals(trgStates.get(index))) {
                    newTargetElement.add(new DefaultAttribute(XliffConstants.STATE, trgStates.get(index)));
                }
            } catch (Exception ignore) {

            }

            foo.add(newTargetElement);
            index++;
        }
    }
}

From source file:com.globalsight.everest.edit.offline.OfflineEditManagerLocal.java

License:Apache License

/**
 * Get "id" attribute from a "<trans-unit id="2328958" ...>".
 * //from   www .  j a  v  a2s  .c  o  m
 * @param tuElement
 * @return
 */
private String getTuId(Element tuElement) {
    Attribute tuIdAtt = tuElement.attribute("id");
    if (tuIdAtt != null) {
        return tuIdAtt.getValue();
    }
    return null;
}

From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java

License:Apache License

private static void removeAttributeForNode(Element root, String nodeName, String[] attributes) {
    List elements = root.elements(nodeName);
    Iterator it = elements.iterator();
    while (it.hasNext()) {
        Element element = (Element) it.next();
        for (int i = 0; i < attributes.length; i++) {
            Attribute attribute = element.attribute(attributes[i]);
            if (attribute != null) {
                element.remove(element.attribute(attributes[i]));
            }/*from  w  ww  . j  a  va 2s .com*/
        }
    }
}

From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java

License:Apache License

private static void removeUncompliantAttributes(Element root, String[] attributes) {
    List elements = root.elements();
    Iterator it = elements.iterator();
    while (it.hasNext()) {
        Element element = (Element) it.next();
        for (int i = 0; i < attributes.length; i++) {
            Attribute attribute = element.attribute(attributes[i]);
            if (attribute != null) {
                element.remove(element.attribute(attributes[i]));
            }/* w ww  .j  a v a2 s  . co m*/
        }
    }
}