Example usage for org.dom4j Node getText

List of usage examples for org.dom4j Node getText

Introduction

In this page you can find the example usage for org.dom4j Node getText.

Prototype

String getText();

Source Link

Document

Returns the text of this node.

Usage

From source file:com.weibo.datasys.crawler.impl.strategy.rule.parse.XpathMatchRule.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from   ww w .  jav  a 2s.c  o m
public List<CommonData> apply(CommonData in) {
    List<CommonData> fields = new ArrayList<CommonData>();
    String xml = in.getBaseField("src");
    try {
        Document doc = DocumentHelper.parseText(xml);
        List<Node> baseNodes = doc.selectNodes(baseNodeXpath);
        for (int i = 0; i < baseNodes.size(); i++) {
            Node baseNode = baseNodes.get(i);
            CommonData fieldData = new CommonData();
            // ??xpathField
            for (Entry<String, String> entry : attrXpathMap.entrySet()) {
                String attr = entry.getKey();
                String xpath = entry.getValue();
                // ?xml?
                List<Node> valueNodes = baseNode.selectNodes(xpath);
                StringBuilder builder = new StringBuilder();
                for (Node valueNode : valueNodes) {
                    String tmpValue = valueNode.getText();
                    if (!StringUtils.isEmptyString(tmpValue)) {
                        builder.append(tmpValue).append(MULTI_VALUE_SPLITER);
                    }
                }
                if (builder.length() > 0) {
                    builder.setLength(builder.length() - 1);
                }
                String value = builder.toString();
                if ("id".equalsIgnoreCase(attr)) {
                    fieldData.setId(value);
                } else {
                    fieldData.setBaseField(attr, value);
                }
            }
            // Field?
            fields.add(fieldData);
            if (!isMultiMatch) {
                // ??????
                break;
            }
        } // end of for each baseNode
    } catch (Exception e) {
        logger.error("[XpathMatchRuleError] - ", e);
    }
    return fields;
}

From source file:com.xebia.mojo.dashboard.reports.html.CloverDashboardReport.java

License:Apache License

private void changeImageSrc(MavenProject subProject, Node node) throws MojoExecutionException {
    List list = xmlUtil.findNodes(node, ".//img");

    for (int i = 0; i < list.size(); i++) {
        Node aNode = (Node) list.get(i);

        if (aNode instanceof Element) {
            Element anElement = (Element) aNode;
            Node srcAtt = anElement.attribute("src");
            srcAtt.setText(DashboardUtil.determineCompletePath(subProject) + "clover/" + srcAtt.getText());
        }/*  ww  w. j a  v  a  2 s  . com*/
    }
}

From source file:com.xebia.mojo.dashboard.reports.html.JacocoDashboardReport.java

License:Apache License

public static Element createPercentageBar(Node contentNode, int pixelsWide) {
    Element bar = null;/*from ww  w.ja v  a  2s .co m*/

    try {
        String percentageString = (contentNode != null) ? contentNode.getText() : "0.0";

        double fraction = NumberFormat.getPercentInstance().parse(percentageString).doubleValue();
        bar = DocumentHelper.createElement("div");

        int pixWide = pixelsWide;
        int greenPixWide = (int) (pixWide * fraction);

        HtmlUtil.addStyles(bar, "border: 1px solid #808080; padding: 0px; background-color: #F02020; width: "
                + pixWide + "px; border-collapse: collapse;");

        Element greenBar = bar.addElement("div");
        HtmlUtil.addStyles(greenBar,
                "padding: 0px; background-color: #00F000; height: 1.3em; border-collapse: collapse; width: "
                        + greenPixWide + "px;");

        Element text = greenBar.addElement("span");
        HtmlUtil.addStyles(text, "display:block; position:absolute; text-align:center; width:" + pixWide
                + "px; border-collapse:collapse;");
        text.setText(percentageString);

    } catch (ParseException e) {
    }

    return bar;
}

From source file:com.zg.action.admin.InstallAction.java

private boolean isInstalled() {
    try {/*from   w  ww.j a  v a  2  s.  c om*/
        //String systemConfigFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath() + SystemConfigUtils.CONFIG_FILE_NAME;
        File systemConfigFile = new File(
                ConfigurationManager.getConfigProperties(SystemConfigUtils.CONFIG_FILE_PATH_NAME));
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(systemConfigFile);
        Node isInstalledNode = document.selectSingleNode("/shopxx/systemConfig/isInstalled");
        if (isInstalledNode != null && StringUtils.equalsIgnoreCase(isInstalledNode.getText(), "false")) {
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
        return true;
    }
}

From source file:com.zimbra.common.soap.Element.java

License:Open Source License

private static Element flattenDOM(org.dom4j.Element d4root, ElementFactory factory) {
    Element elt = factory.createElement(d4root.getQName());
    for (Iterator<?> it = d4root.attributeIterator(); it.hasNext();) {
        org.dom4j.Attribute d4attr = (org.dom4j.Attribute) it.next();
        elt.addAttribute(d4attr.getQualifiedName(), d4attr.getValue());
    }//from  w ww  .  j  a  va2s  .c o  m

    StringBuilder content = new StringBuilder();
    for (int i = 0, size = d4root.nodeCount(); i < size; i++) {
        org.dom4j.Node node = d4root.node(i);
        switch (node.getNodeType()) {
        case org.dom4j.Node.TEXT_NODE:
            content.append(node.getText());
            break;
        case org.dom4j.Node.ELEMENT_NODE:
            content.append(((org.dom4j.Element) node).asXML());
            break;
        }
    }
    return elt.setText(content.toString());
}

From source file:cz.dasnet.dasik.plugin.URLInfo.java

License:Open Source License

@Override
public void onMessage(String channel, String sender, String login, String hostname, String message, Dasik bot) {
    Matcher m = youtube.matcher(message);
    String ytId = null;//from   w w w . ja  v a  2 s. com
    if (m.find()) {
        ytId = m.group(2);
    }
    if (ytId == null) {
        m = youtubeShort.matcher(message);
        if (m.find()) {
            ytId = m.group(2);
        }
    }
    if (ytId != null) {
        try {
            // pull info from youtube api
            org.dom4j.Document document = reader
                    .read(new URL("http://gdata.youtube.com/feeds/api/videos/" + ytId + "?v=2"));
            Node titleNode = document.selectSingleNode("//entry/media:group/media:title");
            Node descNode = document.selectSingleNode("//entry/media:group/media:description");
            Node statNode = document.selectSingleNode("//entry/yt:statistics");
            Node ratingNode = document.selectSingleNode("//entry/yt:rating");
            Node durNode = document.selectSingleNode("//entry/media:group/yt:duration");

            String title = decodeEntities(titleNode.getText());
            String desc = decodeEntities(descNode.getText());
            String views = statNode.valueOf("@viewCount");
            String likes = ratingNode.valueOf("@numLikes");
            String dislikes = ratingNode.valueOf("@numDislikes");
            String dur = durNode.valueOf("@seconds");

            bot.send(channel,
                    "Title: " + shorten(title, 200) + " [" + addMagSeparator(views, ",") + " views; Rating: "
                            + addMagSeparator(likes, ",") + "/" + addMagSeparator(dislikes, ",") + "; Length: "
                            + toMS(dur) + "]");
            //bot.send(channel, "Description: " + shorten(desc, 300));
        } catch (DocumentException ex) {
        } catch (MalformedURLException ex) {
        }
        return;
    }

    m = generalUrl.matcher(message);
    if (m.find()) {
        // general URL
        String mURL = m.group(1);
        if (!mURL.startsWith("http://")) {
            mURL = "http://" + mURL;
        }

        URLConnection httpConn = null;
        try {
            URL url = new URL(mURL);
            httpConn = url.openConnection();
            httpConn.setDoInput(true);
            httpConn.connect();
            String mime = httpConn.getContentType();

            if (mime.contains("text")) {
                org.jsoup.nodes.Document document = Jsoup.connect(url.toString()).get();
                String title = document.select("title").first().text();
                bot.send(channel, shorten("Title: " + title, 350) + " (at " + url.getHost() + ")");
            }
        } catch (IOException ex) {
            log.error("Error while downloading url: " + mURL, ex);
        } finally {
            if (httpConn != null) {
                try {
                    httpConn.getInputStream().close();
                } catch (IOException exx) {
                    log.error("Error closing connection to: " + mURL, exx);
                }
            }
        }
        return;
    }
}

From source file:cz.fi.muni.xkremser.editor.server.newObject.MonographBuilder.java

License:Open Source License

private void updateDcDoc(Document dcDoc, String pid, String signature, String sysno, DigitalObjectModel model) {
    Element dcRootEl = dcDoc.getRootElement();
    Attribute schemaLoc = dcRootEl.attribute("schemaLocation");
    dcRootEl.remove(schemaLoc);/*from   w w w .  j  av a2  s  .  com*/
    Namespace xsi = DocumentHelper.createNamespace("xsi2", FedoraNamespaces.SCHEMA_NAMESPACE_URI);
    dcRootEl.add(xsi);
    dcRootEl.addAttribute(new QName("schemaLocation", xsi),
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    XPath typeXpath = Dom4jUtils.createXPath("/oai_dc:dc/dc:identifier");
    List<? extends Node> nodes = typeXpath.selectNodes(dcDoc);
    for (Node node : nodes) {
        node.detach();
    }
    Element idUuid = dcRootEl.addElement("dc:identifier");
    idUuid.addText(pid);

    for (Node node : nodes) {
        if (node.getText() != null && !"".equals(node.getText().trim())
                && !node.getText().contains(Constants.FEDORA_UUID_PREFIX)) {
            Element temp = dcRootEl.addElement("dc:identifier");
            temp.addText(node.getText());
        }
    }

    if (signature != null) {
        Element idSignature = dcRootEl.addElement("dc:identifier");
        idSignature.addText("signature:" + signature);
    }
    if (sysno != null) {
        Element idSysno = dcRootEl.addElement("dc:identifier");
        idSysno.addText("sysno:" + sysno);
    }
    removeDcTypeElements(dcDoc);
    Element typeEl = dcRootEl.addElement("dc:type");
    typeEl.addText("model:" + model.toString());
    Element rightsEl = dcRootEl.addElement("dc:rights");
    rightsEl.addText("policy:" + Policy.PUBLIC.toString().toLowerCase());
    updateDcLanguages(dcDoc);
}

From source file:cz.fi.muni.xkremser.editor.server.newObject.PeriodicalBuilder.java

License:Open Source License

private void updateDcDoc(Document dcDoc, String pid, String signature, String sysno, DigitalObjectModel model) {
    Element dcRootEl = dcDoc.getRootElement();
    Attribute schemaLoc = dcRootEl.attribute("schemaLocation");
    dcRootEl.remove(schemaLoc);/*ww w  . j  a  va  2  s .co  m*/
    Namespace xsi = DocumentHelper.createNamespace("xsi2", FedoraNamespaces.SCHEMA_NAMESPACE_URI);
    dcRootEl.add(xsi);
    dcRootEl.addAttribute(new QName("schemaLocation", xsi),
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    XPath typeXpath = Dom4jUtils.createXPath("/oai_dc:dc/dc:identifier");
    List<? extends Node> nodes = typeXpath.selectNodes(dcDoc);
    for (Node node : nodes) {
        node.detach();
    }
    Element idUuid = dcRootEl.addElement("dc:identifier");
    idUuid.addText(pid);

    for (Node node : nodes) {
        if (node.getText() != null && !"".equals(node.getText().trim())
                && !node.getText().contains(Constants.FEDORA_UUID_PREFIX)) {
            Element temp = dcRootEl.addElement("dc:identifier");
            temp.addText(node.getText());
        }
    }

    if (signature != null) {
        Element idSignature = dcRootEl.addElement("dc:identifier");
        idSignature.addText("signature:" + signature);
    }
    if (sysno != null) {
        Element idSysno = dcRootEl.addElement("dc:identifier");
        idSysno.addText("sysno:" + sysno);
    }
    removeDcTypeElements(dcDoc);
    Element typeEl = dcRootEl.addElement("dc:type");
    typeEl.addText("model:" + model.getValue());
    Element rightsEl = dcRootEl.addElement("dc:rights");
    rightsEl.addText("policy:" + Policy.PUBLIC.toString().toLowerCase());
    updateDcLanguages(dcDoc);
}

From source file:cz.mzk.editor.server.handler.FindMetadataHandler.java

License:Open Source License

public String findSysno(String barcode) {

    String alephUrl = EditorConfiguration.ServerConstants.Z3950_DEFAULT_HOSTS[z39Client.getProfileIndex()];

    if (alephUrl == null || "".equals(alephUrl))
        return null;

    if (!alephUrl.startsWith("http")) {
        alephUrl = "http://" + alephUrl;
    }/*from  w  ww.  j  a v  a  2  s  .  c  om*/

    String urlToSetNum = alephUrl + "/X?op=find&code=BAR&request=%s&base=";
    String urlToSysno = alephUrl + "/X?op=present&set_entry=1&set_number=";

    String completeUrlToSetNum = String.format(urlToSetNum, barcode);
    String[] oaiBases = configuration.getOaiBases();
    String sysno = null;

    for (String base : oaiBases) {
        try {
            InputStream inputStream = RESTHelper.get(completeUrlToSetNum + base, null, null, false);
            Document records = Dom4jUtils.loadDocument(inputStream, true);

            XPath xpath = DocumentHelper.createXPath("/find/set_number");
            Node resultNode = xpath.selectSingleNode(records);

            if (resultNode != null) {
                String setNumber = resultNode.getText();
                InputStream sysnoStream = RESTHelper.get(urlToSysno + setNumber, null, null, false);
                Document sysnoDoc = Dom4jUtils.loadDocument(sysnoStream, true);
                xpath = DocumentHelper.createXPath("/present/record/doc_number");
                Node sysnoNode = xpath.selectSingleNode(sysnoDoc);
                if (sysnoNode != null)
                    sysno = sysnoNode.getText();
                break;
            }

        } catch (IOException e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        } catch (DocumentException e) {
            LOGGER.error(e.getMessage());
            e.printStackTrace();
        }

    }

    return sysno;
}

From source file:cz.mzk.editor.server.newObject.MonographBuilder.java

License:Open Source License

@SuppressWarnings("unchecked")
private void updateDcDoc(Document dcDoc, String pid, String signature, String sysno, DigitalObjectModel model) {
    Element dcRootEl = dcDoc.getRootElement();
    Attribute schemaLoc = dcRootEl.attribute("schemaLocation");
    dcRootEl.remove(schemaLoc);//from   ww w.  ja  va2s. c o m
    Namespace xsi = DocumentHelper.createNamespace("xsi2", FedoraNamespaces.SCHEMA_NAMESPACE_URI);
    dcRootEl.add(xsi);
    dcRootEl.addAttribute(new QName("schemaLocation", xsi),
            "http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd");

    XPath typeXpath = Dom4jUtils.createXPath("/oai_dc:dc/dc:identifier");
    List<? extends Node> nodes = typeXpath.selectNodes(dcDoc);
    for (Node node : nodes) {
        node.detach();
    }
    Element idUuid = dcRootEl.addElement("dc:identifier");
    idUuid.addText(pid);

    for (Node node : nodes) {
        if (node.getText() != null && !"".equals(node.getText().trim())
                && !node.getText().contains(Constants.FEDORA_UUID_PREFIX)) {
            Element temp = dcRootEl.addElement("dc:identifier");
            temp.addText(node.getText());
        }
    }

    if (signature != null) {
        Element idSignature = dcRootEl.addElement("dc:identifier");
        idSignature.addText("signature:" + signature);
    }
    if (sysno != null) {
        Element idSysno = dcRootEl.addElement("dc:identifier");
        idSysno.addText("sysno:" + sysno);
    }
    removeDcTypeElements(dcDoc);
    Element typeEl = dcRootEl.addElement("dc:type");
    typeEl.addText("model:" + model.getValue());
    Element rightsEl = dcRootEl.addElement("dc:rights");
    rightsEl.addText("policy:" + getPolicy().toString().toLowerCase());
    updateDcLanguages(dcDoc);
}