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.globalsight.everest.webapp.servlet.SnippetLibraryServlet.java

License:Apache License

/**
 * For the Snippet Select Dialog: removesnippetgetpage
 *//* w w w  .j av a2s .  com*/
private String removeSnippetGetPage(String p_user, Document p_request, EditorState p_state,
        HttpSession p_session) throws Exception, RemoteException, SnippetException {
    long pageId = 0L;
    String locale = null;
    String role = null;
    int viewMode = 0;
    Node node;

    node = p_request.selectSingleNode("/*/arg[1]");
    if (node != null) {
        pageId = Long.parseLong(node.getText());
    }

    node = p_request.selectSingleNode("/*/arg[2]");
    if (node != null) {
        locale = node.getText();
    }

    node = p_request.selectSingleNode("/*/arg[3]");
    if (node != null) {
        role = node.getText();
    }

    node = p_request.selectSingleNode("/*/arg[4]");
    if (node != null) {
        viewMode = Integer.parseInt(node.getText());
    }

    int position = 0;

    node = p_request.selectSingleNode("/*/arg[5]");
    if (node != null) {
        position = Integer.parseInt(node.getText());
    }

    if (pageId == 0L || locale == null || role == null || viewMode == 0 || position == 0) {
        CATEGORY.error("Invalid args, expected 5 - " + p_request.asXML());
        throw new Exception("removeSnippetGetPage: invalid arguments");
    }

    m_templateManager.deleteSnippet(p_user, pageId, locale, position);

    return getPage(p_session, p_state, viewMode, true);
}

From source file:com.globalsight.everest.webapp.servlet.SnippetLibraryServlet.java

License:Apache License

/**
 * For the Snippet Select Dialog: deletecontentgetpage
 *///from www  .j  a  v  a 2s .c  o  m
private String deleteContentGetPage(String p_user, Document p_request, EditorState p_state,
        HttpSession p_session) throws Exception, RemoteException, SnippetException, TemplateException {
    long pageId = 0L;
    String locale = null;
    String role = null;
    int viewMode = 0;
    Node node;

    node = p_request.selectSingleNode("/*/arg[1]");
    if (node != null) {
        pageId = Long.parseLong(node.getText());
    }

    node = p_request.selectSingleNode("/*/arg[2]");
    if (node != null) {
        locale = node.getText();
    }

    node = p_request.selectSingleNode("/*/arg[3]");
    if (node != null) {
        role = node.getText();
    }

    node = p_request.selectSingleNode("/*/arg[4]");
    if (node != null) {
        viewMode = Integer.parseInt(node.getText());
    }

    int position = 0;

    node = p_request.selectSingleNode("/*/arg[5]");
    if (node != null) {
        position = Integer.parseInt(node.getText());
    }

    if (pageId == 0L || locale == null || role == null || viewMode == 0 || position == 0) {
        CATEGORY.error("Invalid args, expected 5 - " + p_request.asXML());
        throw new Exception("deleteContentGetPage: invalid arguments");
    }

    m_templateManager.deleteContent(p_user, pageId, locale, position);

    return getPage(p_session, p_state, viewMode, true);
}

From source file:com.globalsight.everest.webapp.servlet.SnippetLibraryServlet.java

License:Apache License

/**
 * For the Snippet Select Dialog: undeletecontentgetpage
 *///from  w  w w .j av a  2 s.co  m
private String undeleteContentGetPage(String p_user, Document p_request, EditorState p_state,
        HttpSession p_session) throws Exception, RemoteException, SnippetException, TemplateException {
    long pageId = 0L;
    String locale = null;
    String role = null;
    int viewMode = 0;
    Node node;

    node = p_request.selectSingleNode("/*/arg[1]");
    if (node != null) {
        pageId = Long.parseLong(node.getText());
    }

    node = p_request.selectSingleNode("/*/arg[2]");
    if (node != null) {
        locale = node.getText();
    }

    node = p_request.selectSingleNode("/*/arg[3]");
    if (node != null) {
        role = node.getText();
    }

    node = p_request.selectSingleNode("/*/arg[4]");
    if (node != null) {
        viewMode = Integer.parseInt(node.getText());
    }

    int position = 0;

    node = p_request.selectSingleNode("/*/arg[5]");
    if (node != null) {
        position = Integer.parseInt(node.getText());
    }

    if (pageId == 0L || locale == null || role == null || viewMode == 0 || position == 0) {
        CATEGORY.error("Invalid args, expected 5 - " + p_request.asXML());
        throw new Exception("undeleteContentGetPage: invalid arguments");
    }

    m_templateManager.undeleteContent(p_user, pageId, locale, position);

    return getPage(p_session, p_state, viewMode, true);
}

From source file:com.globalsight.everest.webapp.servlet.SnippetLibraryServlet.java

License:Apache License

/**
 * Returns a list of snippets that can be used in the specified
 * locale, including generic snippets (sorted by name+id).
 *//* ww w. ja  v a2 s .c  o m*/
private String getSnippetsByLocale(Document p_request) throws Exception {
    String locale = null;
    Node node;

    node = p_request.selectSingleNode("/*/arg[1]");
    if (node != null) {
        locale = node.getText();
    }

    if (locale == null) {
        CATEGORY.error("Invalid args, expected 1 - " + p_request.asXML());
        throw new Exception("getSnippetsByLocale: invalid arguments");
    }

    ArrayList result = m_library.getSnippetsByLocale(locale);

    return snippetsToXml(result);
}

From source file:com.globalsight.ling.docproc.DiplomatWordCounter.java

License:Apache License

static public String getTranslateInnerXml(Element p_node) {
    StringBuilder result = new StringBuilder();
    List content = p_node.content();
    for (int i = 0; i < content.size(); i++) {
        Node node = (Node) content.get(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(encodeXmlEntities(node.getText()));
        }/* w  ww .ja  va 2 s. c o m*/
    }
    return result.toString();
}

From source file:com.globalsight.ling.docproc.DiplomatWordCounter.java

License:Apache License

/**
 * Returns the string value of an element with tags representing whitespace
 * replaced by either whitespace or nbsps.
 *//*from w w  w  .  j  a v  a  2 s.c  om*/
static public String getTextWithWhite(Element p_node, boolean... bs) {
    StringBuffer result = new StringBuffer();

    List content = p_node.content();

    for (int i = 0, max = content.size(); i < max; i++) {
        Node node = (Node) content.get(i);

        if (node.getNodeType() == Node.TEXT_NODE && bs.length == 0) {
            boolean isInternalText = isInternalText(content, i);
            if (!isInternalText) {
                result.append(node.getText());
            } else {
                // add space around internal text
                result.append(" ").append(node.getText()).append(" ");
            }
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element elem = (Element) node;
            String type = elem.attributeValue("type");
            int childNodes = elem.content().size();
            // For word counting, always treat TMX whitespace tags
            // as white.
            if (Text.isTmxWhitespaceNode(type) || Text.isTmxMsoWhitespaceNode(type)) {
                result.append(" ");
            } else {
                if (childNodes > 0) {
                    boolean isExtract = false;
                    for (int j = 0; j < childNodes; j++) {
                        if (((Node) elem.content().get(j)).getNodeType() == Node.ELEMENT_NODE) {
                            String s = ((Element) elem.content().get(j)).attributeValue("isTranslate");
                            String innerTextNodeIndex = ((Element) elem.content().get(j))
                                    .attributeValue("innerTextNodeIndex");
                            if (s != null && Boolean.parseBoolean(s)) {
                                isExtract = true;
                                // getTextWithWhite((Element)elem.content().get(j),
                                // true);
                                // ((Element)elem.content().get(j)).
                                // result.append(getTranslateInnerXml((Element)
                                // elem.content().get(j)));
                            } else {
                                isExtract = false;
                            }

                        } else if (((Node) elem.content().get(j)).getNodeType() == Node.TEXT_NODE
                                && isExtract) {
                            result.append(((Node) elem.content().get(j)).getText());
                        }
                    }
                }
            }
        } else {
            System.err.println("Please fix the word counter: " + node);
        }
    }

    return result.toString();
}

From source file:com.globalsight.smartbox.util.WebClientHelper.java

License:Apache License

/**
 * Download job// ww w  . j  a  va 2s . com
 * 
 * @param jobInfo
 * @return
 * @throws Exception
 */
public static boolean jobDownload(JobInfo jobInfo, String baseDir, String server) {
    try {
        String fileXml = ambassador.getJobExportFiles(accessToken, jobInfo.getJobName());
        Document profileDoc = DocumentHelper.parseText(fileXml);
        Node node = profileDoc.selectSingleNode("/jobFiles");
        String root = node.selectSingleNode("root").getText();
        ArrayList<String> filePaths = new ArrayList<String>();
        List<Node> paths = node.selectNodes("paths");
        for (Node n : paths) {
            filePaths.add(n.getText());
        }

        root = replaceHostUrl(root, server);

        String rootNoCompany = root.substring(0, root.lastIndexOf("/"));
        boolean useHttps = root.startsWith("https:");
        boolean useHttp = root.startsWith("http:");

        String commonPath = ZipUtil.getCommonPath(getReplacedPath(jobInfo.getJobName(), filePaths), "");
        File targetFile = null;
        StringBuffer targetFiles = new StringBuffer();
        for (String path : filePaths) {
            int index = path.indexOf(commonPath);
            String savePath = path.substring(index + commonPath.length());
            String[] nodes = path.split("/");
            String locale = nodes[0];
            savePath = rootNoCompany + "/" + jobInfo.getJobName() + "/" + locale + savePath;

            String downloadUrl = root + "/" + path;

            if (useHttps) {
                targetFile = DownLoadHelper.downloadHttps(downloadUrl, baseDir, savePath);
            } else if (useHttp) {
                targetFile = DownLoadHelper.downloadHttp(downloadUrl, baseDir, savePath);
            }
            targetFiles.append(targetFile.getPath()).append("|");
        }
        if (targetFiles.length() > 0) {
            targetFiles.deleteCharAt(targetFiles.length() - 1);
        }
        jobInfo.setTargetFiles(targetFiles.toString());
    } catch (Exception e) {
        String message = "Failed to download job, Job Name:" + jobInfo.getJobName() + ", Job Id:"
                + jobInfo.getId();
        LogUtil.fail(message, e);
        return false;
    }
    return true;
}

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

License:Apache License

/**
 * Reads and validates a Termbase Definition given in an XML string.
 *///from  w  w w .  j av  a2  s .c  om
private void init(String p_definition) throws TermbaseException {
    XmlParser parser = null;
    Document dom;

    try {
        parser = XmlParser.hire();
        dom = parser.parseXml(p_definition);
    } finally {
        XmlParser.fire(parser);
    }

    try {
        Element root = dom.getRootElement();
        Node nameNode = root.selectSingleNode("/definition/name");
        Node descNode = root.selectSingleNode("/definition/description");

        if (nameNode != null) {
            m_name = nameNode.getText();
        }

        if (descNode != null) {
            m_description = descNode.getText();
        }

        List langs = root.selectNodes("/definition/languages/language");

        if (langs.size() == 0) {
            error("no languages defined", null);
        }

        for (int i = 0, max = langs.size(); i < max; ++i) {
            Element lang = (Element) langs.get(i);

            String name = lang.valueOf("name");
            String locale = lang.valueOf("locale");
            String hasTerms = lang.valueOf("hasterms");

            if (name == null || locale == null || hasTerms == null) {
                error("incomplete language definition", null);
            }

            m_languages.add(new Language(name, locale, hasTerms));
        }

        List fields = root.selectNodes("/definition/fields/field");

        for (int i = 0, max = fields.size(); i < max; ++i) {
            Element field = (Element) fields.get(i);

            String name = field.valueOf("name");
            String type = field.valueOf("type");
            String system = field.valueOf("system");
            String indexed = field.valueOf("indexed");
            String values = field.valueOf("values");

            if (name == null || type == null || system == null || indexed == null || values == null) {
                error("incomplete field definition", null);
            }

            m_fields.add(new Field(name, type, system, indexed, values));
        }

        List indexes = root.selectNodes("/definition/indexes/index");

        for (int i = 0, max = indexes.size(); i < max; ++i) {
            Element index = (Element) indexes.get(i);

            String languageName = index.valueOf("languagename");
            String locale = index.valueOf("locale");
            String type = index.valueOf("type");

            if (languageName == null || locale == null || type == null) {
                error("incomplete index definition", null);
            }

            m_indexes.add(new Index(languageName, locale, type));
        }
    } catch (TermbaseException e) {
        throw e;
    } catch (Exception e) {
        // cast exception and throw
        error(e.getMessage(), e);
    }
}

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

License:Apache License

/**
 * Finds the preferred term in the given language. The preferred
 * term is the one that has a usage='preferred' field. If no term
 * is qualified as preferred, the first term is returned.
 *
 * @return preferred term as string if found, else null.
 *//*w w  w . j  av  a2  s .c om*/
static public String getPreferredTerm(Entry p_entry, String p_language) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();

    List termGrps = root.selectNodes("//languageGrp[./language/@name='" + p_language + "']/termGrp");

    if (termGrps.size() == 0) {
        return null;
    }

    for (int i = 0, max = termGrps.size(); i < max; i++) {
        Element termGrp = (Element) termGrps.get(i);
        Node usage = termGrp.selectSingleNode("descripGrp/descrip[@type='usage']");

        if (usage != null && "preferred".equals(usage.getText())) {
            return termGrp.element("term").getText();
        }
    }

    return ((Element) termGrps.get(0)).element("term").getText();
}

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

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without
 * the top-level tag.//from w w  w .  ja  va2 s .c o m
 */
static public String getInnerXml(Element p_node) {
    StringBuffer result = new StringBuffer();

    List content = p_node.content();

    for (int i = 0, max = content.size(); i < max; i++) {
        Node node = (Node) content.get(i);

        // Work around a specific behaviour of DOM4J text nodes:
        // The text node asXML() returns the plain Unicode string,
        // so we need to encode entities manually.
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(EditUtil.encodeXmlEntities(node.getText()));
        } else {
            // Element nodes write their text nodes correctly.
            result.append(node.asXML());
        }
    }

    return result.toString();
}