Example usage for org.dom4j Element elements

List of usage examples for org.dom4j Element elements

Introduction

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

Prototype

List<Element> elements();

Source Link

Document

Returns the elements contained in this element.

Usage

From source file:com.ostrichemulators.semtool.ui.components.playsheets.BrowserPlaySheet2.java

License:Open Source License

protected BufferedImage getExportImageFromSVGBlock() throws IOException {
    log.debug("Using SVG block to save image.");
    DOMReader rdr = new DOMReader();
    Document doc = rdr.read(engine.getDocument());
    Document svgdoc = null;/*from  www .  ja va  2  s  .c om*/
    File svgfile = null;
    try {
        Map<String, String> namespaceUris = new HashMap<>();
        namespaceUris.put("svg", "http://www.w3.org/2000/svg");
        namespaceUris.put("xhtml", "http://www.w3.org/1999/xhtml");

        XPath xp = DocumentHelper.createXPath("//svg:svg");
        xp.setNamespaceURIs(namespaceUris);
        // don't forget about the styles
        XPath stylexp = DocumentHelper.createXPath("//xhtml:style");
        stylexp.setNamespaceURIs(namespaceUris);

        svgdoc = DocumentHelper.createDocument();
        Element svg = null;
        List<?> theSVGElements = xp.selectNodes(doc);
        if (theSVGElements.size() == 1) {
            svg = Element.class.cast(theSVGElements.get(0)).createCopy();
        } else {
            int currentTop = 0;
            int biggestSize = 0;
            for (int i = 0; i < theSVGElements.size(); i++) {
                Element thisElement = Element.class.cast(theSVGElements.get(i)).createCopy();
                int thisSize = thisElement.asXML().length();
                if (thisSize > biggestSize) {
                    currentTop = i;
                    biggestSize = thisSize;
                }
            }
            svg = Element.class.cast(theSVGElements.get(currentTop)).createCopy();
        }

        svgdoc.setRootElement(svg);

        Element oldstyle = Element.class.cast(stylexp.selectSingleNode(doc));
        if (null != oldstyle) {
            Element defs = svg.addElement("defs");
            Element style = defs.addElement("style");
            style.addAttribute("type", "text/css");
            String styledata = oldstyle.getTextTrim();
            style.addCDATA(styledata);
            // put the stylesheet definitions first
            List l = svg.elements();
            l.remove(defs);
            l.add(0, defs);
        }

        // clean up the SVG a little...
        // d3 comes up with coords like
        // M360,27475.063247863247C450,27475.063247863247 450,27269.907692307694 540,27269.907692307694
        XPath cleanxp1 = DocumentHelper.createXPath("//svg:path");
        Pattern pat = Pattern.compile(",([0-9]+)\\.([0-9]{1,2})[0-9]+");
        cleanxp1.setNamespaceURIs(namespaceUris);
        List<?> cleanups = cleanxp1.selectNodes(svgdoc);
        for (Object n : cleanups) {
            Element e = Element.class.cast(n);
            String dstr = e.attributeValue("d");
            Matcher m = pat.matcher(dstr);
            dstr = m.replaceAll(",$1.$2 ");
            e.addAttribute("d", dstr.replaceAll("([0-9])C([0-9])", "$1 C$2").trim());
        }
        XPath cleanxp2 = DocumentHelper.createXPath("//svg:g[@class='node']");
        cleanxp2.setNamespaceURIs(namespaceUris);
        cleanups = cleanxp2.selectNodes(svgdoc);
        for (Object n : cleanups) {
            Element e = Element.class.cast(n);
            String dstr = e.attributeValue("transform");
            Matcher m = pat.matcher(dstr);
            dstr = m.replaceAll(",$1.$2");
            e.addAttribute("transform", dstr.trim());
        }

        svgfile = File.createTempFile("graphviz-", ".svg");
        try (Writer svgw = new BufferedWriter(new FileWriter(svgfile))) {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter xmlw = new XMLWriter(svgw, format);
            xmlw.write(svgdoc);
            xmlw.close();

            if (log.isDebugEnabled()) {
                FileUtils.copyFile(svgfile, new File(FileUtils.getTempDirectory(), "graphvisualization.svg"));
            }
        }

        try (Reader svgr = new BufferedReader(new FileReader(svgfile))) {
            TranscoderInput inputSvg = new TranscoderInput(svgr);

            ByteArrayOutputStream baos = new ByteArrayOutputStream((int) svgfile.length());
            TranscoderOutput outputPng = new TranscoderOutput(baos);

            try {
                PNGTranscoder transcoder = new PNGTranscoder();
                transcoder.addTranscodingHint(PNGTranscoder.KEY_INDEXED, 256);
                transcoder.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE);
                transcoder.transcode(inputSvg, outputPng);
            } catch (Throwable t) {
                log.error(t, t);
            }
            baos.flush();
            baos.close();

            return ImageIO.read(new ByteArrayInputStream(baos.toByteArray()));
        }
    } catch (InvalidXPathException e) {
        log.error(e);
        String msg = "Problem creating image";
        if (null != svgdoc) {
            try {
                File errsvg = new File(FileUtils.getTempDirectory(), "graphvisualization.svg");
                FileUtils.write(errsvg, svgdoc.asXML(), Charset.defaultCharset());
                msg = "Could not create the image. SVG data store here: " + errsvg.getAbsolutePath();
            } catch (IOException ex) {
                // don't care
            }
        }
        throw new IOException(msg, e);
    } finally {
        if (null != svgfile) {
            FileUtils.deleteQuietly(svgfile);
        }
    }
}

From source file:com.panet.imeta.trans.steps.getxmldata.GetXMLData.java

License:Open Source License

@SuppressWarnings("unchecked")
public void prepareNSMap(Element l) {
    for (Namespace ns : (List<Namespace>) l.declaredNamespaces()) {
        if (ns.getPrefix().trim().length() == 0) {
            data.NAMESPACE.put("pre" + data.NSPath.size(), ns.getURI());
            String path = "";
            Element element = l;/*from   ww w .  j  a v a 2  s  .  c o  m*/
            while (element != null) {
                if (element.getNamespacePrefix() != null && element.getNamespacePrefix().length() > 0) {
                    path = "/" + element.getNamespacePrefix() + ":" + element.getName() + path;
                } else {
                    path = "/" + element.getName() + path;
                }
                element = element.getParent();
            }
            data.NSPath.add(path);
        } else {
            data.NAMESPACE.put(ns.getPrefix(), ns.getURI());
        }
    }
    for (Element e : (List<Element>) l.elements()) {
        prepareNSMap(e);
    }
}

From source file:com.plugin.UI.Windows.DesktopUI.java

/**
 * Traverse xml.//  w w  w . j  a va  2  s.c  o  m
 * 
 * @param e the e
 * @param menuBar the menu bar
 */
void traverseXML(Element e, JMenuBar menuBar) {
    JMenu menu = null;
    for (int i = 0; i < e.elements().size(); i++) {
        final Element e1 = (Element) e.elements().get(i);
        if (e1.attributeValue("class") != null) {

            if (!Global.moduleClasses.contains(e1.attributeValue("class"))) {
                Global.moduleClasses.add(e1.attributeValue("class"));
                Global.moduleCaptions.add(e1.attributeValue("text"));
                Global.moduleIcons.add(e1.attributeValue("icon"));
            }
            String menucaption = e1.attributeValue("text");
            menucaption = processUnicodeUI(menucaption);
            if (menucaption.startsWith("$"))
                menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
            JMenuItem item = new JMenuItem(menucaption);
            String icon = e1.attributeValue("icon");
            if (icon != null)
                item.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME + "/" + icon));
            if (e1.attributeValue("visible") != null
                    && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                item.setVisible(false);
            }
            if (e1.attributeValue("enable") != null && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                item.setEnabled(false);
            }
            String startup = e1.attributeValue("startup");
            if (startup == null)
                startup = "false";
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        Object o = Class.forName(e1.attributeValue("class")).newInstance();
                        JInternalFrame iFrame = ((IWindow) o).getFrame();
                        if (iFrame == null)
                            return;
                        iFrame.setFrameIcon(new ImageIcon(
                                "icons/" + Global.SELECTED_THEME + "/" + e1.attributeValue("icon")));
                        //                     iFrame.getDesktopIcon().setUI(
                        //                           new MyDesktopIconUI(e1
                        //                                 .attributeValue("text"), iFrame
                        //                                 .getFrameIcon()));
                        iFrame.setIconifiable(true);

                        if (checkIfAlreadyOpen(iFrame)) {
                            iFrame.dispose();
                            return;
                        }

                        _desktop.add(iFrame);
                        iFrame.setSelected(true);
                        Global.setStatus(
                                e1.attributeValue("text").replaceAll("Language", Global.selectedLanguage)
                                        + " loaded.");
                    } catch (ClassNotFoundException e1) {
                        System.out.println(e1);
                    } catch (InstantiationException e2) {
                        System.out.println(e2);
                    } catch (IllegalAccessException e3) {
                        System.out.println(e3);
                    } catch (PropertyVetoException e4) {
                        System.out.println(e4);
                    }
                }
            });
            if (startup.equalsIgnoreCase("true"))
                item.doClick();
            //            menu.add(item);
            //            String icon2 = e.attributeValue("icon");
            //            if (icon2 != null)
            //               menu.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME
            //                     + "/" + icon2));

            menuBar.add(item);
        } else {
            if (e1.attributeValue("text").equalsIgnoreCase("-"))
                menuBar.add(new JSeparator());
            else {
                String menucaption = e1.attributeValue("text");
                menucaption = processUnicodeUI(menucaption);
                if (menucaption.startsWith("$"))
                    menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
                menu = new JMenu(menucaption);
                if (e1.attributeValue("visible") != null
                        && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                    menu.setVisible(false);
                }
                if (e1.attributeValue("enable") != null
                        && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                    menu.setEnabled(false);
                }
                traverseXML(e1, menu);
                menuBar.add(menu);
            }
        }
    }

}

From source file:com.plugin.UI.Windows.DesktopUI.java

/**
 * Traverse xml./*  www . j a  va2  s.c om*/
 * 
 * @param e the e
 * @param menu the menu
 */
void traverseXML(Element e, JMenu menu) {
    JMenu menu1 = null;
    for (int i = 0; i < e.elements().size(); i++) {
        final Element e1 = (Element) e.elements().get(i);
        if (e1.attributeValue("class") != null) {

            if (!Global.moduleClasses.contains(e1.attributeValue("class"))) {
                Global.moduleClasses.add(e1.attributeValue("class"));
                Global.moduleCaptions.add(e1.attributeValue("text"));
                Global.moduleIcons.add(e1.attributeValue("icon"));
            }
            String menucaption = e1.attributeValue("text");
            menucaption = processUnicodeUI(menucaption);
            if (menucaption.startsWith("$"))
                menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
            JMenuItem item = new JMenuItem(menucaption);
            String icon = e1.attributeValue("icon");
            if (icon != null)
                item.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME + "/" + icon));
            if (e1.attributeValue("visible") != null
                    && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                item.setVisible(false);
            }
            if (e1.attributeValue("enable") != null && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                item.setEnabled(false);
            }
            String startup = e1.attributeValue("startup");
            if (startup == null)
                startup = "false";
            item.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        Object o = Class.forName(e1.attributeValue("class")).newInstance();

                        JInternalFrame iFrame = ((IWindow) o).getFrame();
                        if (iFrame == null)
                            return;
                        iFrame.setFrameIcon(new ImageIcon(
                                "icons/" + Global.SELECTED_THEME + "/" + e1.attributeValue("icon")));
                        //                     iFrame.getDesktopIcon().setUI(
                        //                           new MyDesktopIconUI(e1
                        //                                 .attributeValue("text"), iFrame
                        //                                 .getFrameIcon()));
                        iFrame.setIconifiable(true);

                        if (checkIfAlreadyOpen(iFrame)) {
                            iFrame.dispose();
                            return;
                        }
                        _desktop.add(iFrame);
                        iFrame.setSelected(true);
                        Global.setStatus(
                                e1.attributeValue("text").replaceAll("Language", Global.selectedLanguage)
                                        + " loaded.");
                    } catch (ClassNotFoundException e1) {
                        System.out.println(e1);
                    } catch (InstantiationException e2) {
                        System.out.println(e2);
                    } catch (IllegalAccessException e3) {
                        System.out.println(e3);
                    } catch (PropertyVetoException e4) {
                        System.out.println(e4);
                    }

                }
            });
            if (startup.equalsIgnoreCase("true"))
                item.doClick();
            menu.add(item);
            menu.add(item);
            String icon2 = e.attributeValue("icon");
            if (icon2 != null)
                menu.setIcon(new ImageIcon("icons/" + Global.SELECTED_THEME + "/" + icon2));
        } else {
            if (e1.attributeValue("text").equalsIgnoreCase("-"))
                menu.add(new JSeparator());
            else {
                String menucaption = e1.attributeValue("text");
                menucaption = processUnicodeUI(menucaption);
                if (menucaption.startsWith("$"))
                    menucaption = menucaption.replaceAll("Language", Global.selectedLanguage).substring(1);
                menu1 = new JMenu(menucaption);
                if (e1.attributeValue("visible") != null
                        && e1.attributeValue("visible").equalsIgnoreCase("false")) {
                    menu1.setVisible(false);
                }
                if (e1.attributeValue("enable") != null
                        && e1.attributeValue("enable").equalsIgnoreCase("false")) {
                    menu1.setEnabled(false);
                }
                traverseXML(e1, menu1);
                menu.add(menu1);
            }
        }
    }
}

From source file:com.poka.util.XmlSax.java

public void getArgListInfo() {
    this.argList = new ArrayList<Arg>();
    Document doc = load(bankFile, false);
    Element rootElm = doc.getRootElement();
    Element root1Elm = rootElm.element("args");
    if (root1Elm == null) {
        root1Elm = rootElm.addElement("args");
    }/*from   w ww. j ava  2s  .c o m*/
    List nodes = root1Elm.elements();
    for (Iterator it = nodes.iterator(); it.hasNext();) {
        Element elm = (Element) it.next();
        Arg cfg = new Arg();
        cfg.setAkey(elm.getName().trim());
        cfg.setAvalue(elm.getTextTrim());
        this.argList.add(cfg);

    }
}

From source file:com.prj.utils.UfdmXmlUtil.java

/**
 * ??: ??XML/*from   w  ww  . j  a va  2s  . co  m*/
 * @auther 
 * @date 2014-11-28 
 * @param element
 * @param map
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map readXml(Element element, Map map) {
    try {
        if (map == null) {
            map = new HashMap();
        }
        Iterator iter = element.elementIterator();
        while (iter.hasNext()) {
            Element ele = (Element) iter.next();
            map.put(ele.getName(), ele.getText().trim());
            if (ele.elements().size() != 0) {
                readXml(ele, map);
            }
        }
        return map;
    } catch (Exception er) {
        er.printStackTrace();
        return null;
    }
}

From source file:com.pureinfo.dolphin.script.lang.ScriptBlock.java

License:Open Source License

/**
 * @see com.pureinfo.force.xml.IXMLSupporter#fromXML(org.dom4j.Element)
 */// ww w.j av a2s  .  c  o  m
public void fromXML(Element _element) throws PureException {
    List children = _element.elements();
    if (children == null || children.isEmpty())
        return;

    Element element;
    String sName;
    Statement statement;
    for (int i = 0; i < children.size(); i++) {
        element = (Element) children.get(i);
        sName = element.getName();
        if (sName.equals("statement")) {
            this.addContent(element.getText());
        } else {
            statement = new Statement();
            statement.fromXML(element);
            this.addContent(statement);
        }
    }
}

From source file:com.pureinfo.force.junit.AssertUtil.java

License:Open Source License

/**
 * ElementnameElement<br>/*from w w  w . j  a v a 2  s .  c  o  m*/
 * Attribute
 * 
 * @param _expectedElement
 * @param _actualElement
 */
public static void assertXMLEquals(Element _expectedElement, Element _actualElement) {
    Assert.assertEquals("element name", _expectedElement.getName(), _actualElement.getName());
    List expectedAttributes = _expectedElement.attributes();
    List actualAttributes = _actualElement.attributes();
    Assert.assertEquals("attribute size", expectedAttributes.size(), actualAttributes.size());

    Map expectedNames = new HashMap();
    Map actualNames = new HashMap();
    for (Iterator iter = expectedAttributes.iterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        expectedNames.put(attr.getName(), attr.getValue());
    }
    for (Iterator iter = actualAttributes.iterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        actualNames.put(attr.getName(), attr.getValue());
    }
    Assert.assertEquals("attribute", expectedNames, actualNames);

    List expectedElements = _expectedElement.elements();
    List actualElements = _actualElement.elements();
    Assert.assertEquals("element size", expectedElements.size(), actualElements.size());

    for (Iterator iter1 = expectedElements.iterator(), iter2 = actualElements.iterator(); iter1.hasNext();) {
        Element expecteElement = (Element) iter1.next();
        Element actualElement = (Element) iter2.next();
        assertXMLEquals(expecteElement, actualElement);
    }
}

From source file:com.pureinfo.srm.srm2rpms.action.RPMSValidatorAction.java

License:Open Source License

/**
 * @see com.pureinfo.ark.interaction.ActionBase#beforeExecution()
 *//*from  w  w  w  .j a  v  a  2  s .  c o  m*/
protected ActionForward beforeExecution() throws PureException {
    String sId = request.getRequiredParameter("id", "validator id");
    String sFileName = ClassResourceUtil.mapFullPath("srm2rpms-validator.cfg.xml", true);
    Element element = XMLUtil.fileToElement(sFileName);
    List list = element.elements();
    try {
        Iterator itrList = list.iterator();
        while (itrList.hasNext()) {
            element = (Element) itrList.next();
            if (sId.equals(element.attributeValue("id"))) {
                m_sTitle = element.attributeValue("title");
                m_sScenery = element.attributeValue("scenery");
                m_sHeadTable = element.elementText("hint");

                String sCondition = element.elementTextTrim("condition");
                ((SearchForm) form).getQueryFilter().addCondition(sCondition);
                return super.beforeExecution();
            }
        } // endwhile
    } finally {
        list.clear();
    }

    throw new PureException(PureException.INVALID_REQUEST, "validator not found: id=" + sId);
}

From source file:com.pureinfo.srm.srm2rpms.action.ToolbarAction.java

License:Open Source License

protected void addToolbarElements() throws PureException {
    addElement("view.do", "RPMS");

    // to add validators
    String sFileName = ClassResourceUtil.mapFullPath("srm2rpms-validator.cfg.xml", false);
    if (sFileName != null) {
        Element element = XMLUtil.fileToElement(sFileName);
        List list = element.elements();
        try {//from w  w  w . j a v a  2s  .  c o  m
            Iterator itrList = list.iterator();
            while (itrList.hasNext()) {
                element = (Element) itrList.next();
                addElement("validator.do?id=" + element.attributeValue("id") + "&classId="
                        + element.attributeValue("classId"), element.attributeValue("title"));
            }
        } finally {
            list.clear();
        }
    }
}