Example usage for org.dom4j Element nodeCount

List of usage examples for org.dom4j Element nodeCount

Introduction

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

Prototype

int nodeCount();

Source Link

Document

Returns the number of Node instances that this branch contains.

Usage

From source file:com.ctvit.vdp.services.sysconfiguration.user.UserService.java

/**
 * ?????XML/*from w  ww.  ja  v a  2s. c  o m*/
 **/
public Map<String, String> getXML(Map xmlRightIds, Map thirdRightIds) throws DocumentException {
    String baseXML = systemConfigDao.selectByPrimaryKey("Rights").getValue();//??XML
    Document doc = DocumentHelper.parseText(baseXML);
    Document topDoc = DocumentHelper.createDocument();

    Element baseElement = doc.getRootElement();
    Element topEl = topDoc.addElement("Rights");//?XML
    List<Element> secMenuList = new ArrayList<Element>();
    String topIdFlag = "";

    Iterator elementIter = baseElement.elementIterator();
    while (elementIter.hasNext()) {//??
        Element element01 = (Element) elementIter.next();
        Iterator elementIter01 = element01.elementIterator();
        while (elementIter01.hasNext()) {//??
            Element element02 = (Element) elementIter01.next();
            Iterator elementIter02 = element02.elementIterator();
            String idFlag = "";
            if (xmlRightIds.get(element02.attributeValue("id")) != null) {//??ID?ID
                Element tempEl = element02.getParent();//?
                if (topEl.nodeCount() > 0 && !topIdFlag.equals(tempEl.attributeValue("id"))) {
                    topEl.addElement(tempEl.getName()).addAttribute("id", element01.attributeValue("id"))
                            .addAttribute("name", element01.attributeValue("name"));
                }
                if (topEl.nodeCount() == 0) {
                    topEl.addElement(tempEl.getName()).addAttribute("id", element01.attributeValue("id"))
                            .addAttribute("name", element01.attributeValue("name"));
                }
                topIdFlag = tempEl.attributeValue("id");
                secMenuList.add(element02);
            }
        }
    }

    StringBuffer secXML = new StringBuffer();
    secXML.append("<Rights>");
    Element tempTopEl = topEl.createCopy();
    //      System.out.println("tempTopEl: "+tempTopEl.asXML());
    Iterator secIt = tempTopEl.elementIterator();//????
    String flag = "";
    while (secIt.hasNext()) {
        Element op = (Element) secIt.next();
        for (Element eo : secMenuList) {//eo?? 
            if (eo.attributeValue("id").substring(0, 2).equals(op.attributeValue("id"))
                    && !flag.equals(eo.attributeValue("id"))) {
                flag = eo.attributeValue("id");
                Document secDoc = DocumentHelper.createDocument();
                Element secEle = secDoc.addElement("SecMenu");
                secEle.addAttribute("id", eo.attributeValue("id"));
                secEle.addAttribute("name", eo.attributeValue("name"));
                secEle.addAttribute("source", eo.attributeValue("source"));

                Iterator eoIter = eo.elementIterator();
                while (eoIter.hasNext()) {//??
                    Element thirdEl = (Element) eoIter.next();
                    if (thirdRightIds.get(thirdEl.attributeValue("id")) != null) {
                        Document document = DocumentHelper.createDocument();
                        Element tempEle = document.addElement("ThirdMenu");
                        tempEle.addAttribute("id", thirdEl.attributeValue("id"));
                        tempEle.addAttribute("name", thirdEl.attributeValue("name"));
                        tempEle.addAttribute("source", thirdEl.attributeValue("source"));
                        secEle.add(tempEle);//
                    }
                }
                op.add(secEle);//
            }
            //System.out.println("************ op: "+op.asXML());
        }
        secXML.append(op.asXML());
    }
    secXML.append("</Rights>");
    Map<String, String> xmlMap = new HashMap<String, String>();

    xmlMap.put("topMenu", topEl.asXML());
    xmlMap.put("treeMenu", secXML.toString());
    xmlMap.put("baseXML", baseElement.asXML());
    //      this.getElementList(baseElement,xmlRightIds);
    return xmlMap;
}

From source file:com.flaptor.hounder.util.HtmlParser.java

License:Apache License

/**
 * Simple method to concatenate all readable text in the document and get the outlinks.
 * /* w  w w .  j  a  va  2  s  .  c o  m*/
 * @param e
 *            the element in where to look for readable text and outlinks.
 * @param out
 *            the parse output so far. For any caller except the getText itself,
 *            should be empty. After return, it contains the readable text
 *            of the html and the outlinks.
 */
protected void extractText(final Element e, final Output out, final String fieldName) {
    //String nodeName = e.getName();
    if (!(e.getNodeType() == Node.COMMENT_NODE)) {
        int size = e.nodeCount();
        for (int i = 0; i < size; i++) {
            Node node = e.node(i);
            if (node instanceof Element) {
                extractText((Element) node, out, fieldName);
            } else if (node instanceof Text) {
                String t = node.getText();
                out.addFieldString(fieldName, t);
            }
        }
    }
}

From source file:com.flaptor.util.DomUtil.java

License:Apache License

/**
 * Gets the entire text of an element an all its children
 * /*from  w ww. j  av a  2  s  .  c  o m*/
 * @param element
 * @return
 */
public static String getElementTextRecursively(final Element element) {
    String result = "";

    if (!(element.getNodeType() == Node.COMMENT_NODE)) {
        int size = element.nodeCount();
        for (int i = 0; i < size; i++) {
            Node node = element.node(i);
            if (node instanceof Element) {
                result += getElementTextRecursively((Element) node);
            } else if (node instanceof Text) {
                result += node.getText();
            }
        }
    }

    return result;
}

From source file:com.flaptor.util.parser.HtmlParser.java

License:Apache License

/**
 * Simple method to concatenate all readable text in the document and get the outlinks.
 * /*from   w  ww. j  a v  a 2s  .c  o m*/
 * @param e
 *            the element in where to look for readable text and outlinks.
 * @param out
 *            the parse output so far. For any caller except the getText itself,
 *            should be empty. After return, it contains the readable text
 *            of the html and the outlinks.
 */
protected void extractAllText(final Element e, final ParseOutput out, final String fieldName) {
    //String nodeName = e.getName();
    if (!(e.getNodeType() == Node.COMMENT_NODE)) {
        int size = e.nodeCount();
        for (int i = 0; i < size; i++) {
            Node node = e.node(i);
            if (node instanceof Element) {
                extractAllText((Element) node, out, fieldName);
            } else if (node instanceof Text) {
                String t = node.getText();
                out.addFieldString(fieldName, t);
            }
        }
    }
}

From source file:com.founder.fix.fixflow.core.impl.util.XmlUtil.java

License:Apache License

public static boolean hasChild(Element element) {

    return element.nodeCount() > 0;

}

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

License:Apache License

public static void findNbspElements(ArrayList p_result, Element p_element) {
    // Depth-first traversal: add embedded <ph x-nbspace> to the list first.
    for (int i = 0, max = p_element.nodeCount(); i < max; i++) {
        Node child = (Node) p_element.node(i);

        if (child instanceof Element) {
            findNbspElements(p_result, (Element) child);
        }/*from  w w w .  j  a  v a2s .  c o  m*/
    }

    if (p_element.getName().equals("ph")) {
        String attr = p_element.attributeValue("type");

        if (attr != null && attr.equals("x-nbspace")) {
            p_result.add(p_element);
        }
    }
}

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

License:Apache License

public static void findSubElements(ArrayList p_result, Element p_element) {
    // Depth-first traversal: add embedded <sub> to the list first.
    for (int i = 0, max = p_element.nodeCount(); i < max; i++) {
        Node child = (Node) p_element.node(i);

        if (child instanceof Element) {
            findSubElements(p_result, (Element) child);
        }/*  w  ww.  j a v  a  2s .  co m*/
    }

    if (p_element.getName().equals("sub")) {
        p_result.add(p_element);
    }
}

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

License:Apache License

private static void findHiElements(ArrayList p_result, Element p_element) {
    // Depth-first traversal: add embedded <hi> to the list first.
    for (int i = 0, max = p_element.nodeCount(); i < max; i++) {
        Node child = (Node) p_element.node(i);

        if (child instanceof Element) {
            findHiElements(p_result, (Element) child);
        }//from   w  w w. ja  v  a  2  s .  c  om
    }

    if (p_element.getName().equals("hi")) {
        p_result.add(p_element);
    }
}

From source file:com.globalsight.everest.edit.offline.ttx.TTXParser.java

License:Apache License

/**
 * Parse offline uploading TTX file for uploading purpose.
 * // www.jav a  2s . c om
 * @param doc
 * @param isParsingTTXForGS
 *            :True for GS uploading;False for common TTX file parsing.
 * @return
 * @throws Exception
 */
public String parseToTxt(Document doc, boolean isParsingTTXForGS) throws Exception {
    this.isParsingTTXForGS = isParsingTTXForGS;

    Element root = doc.getRootElement();// TRADOStag element
    // toolSettings
    parseToolSettings(root);

    // userSettings
    parseUserSettings(root);

    Element bodyElement = root.element(TTXConstants.BODY);
    Element rawElement = bodyElement.element(TTXConstants.RAW);
    // parse header info such as "pageId","taskId" etc.
    parseHeaderInfo(rawElement);
    // append header info
    appendHeaderInfo();

    // main contents
    if (rawElement.nodeCount() > 0) {
        Iterator nodesIt = rawElement.nodeIterator();
        while (nodesIt.hasNext()) {
            Node node = (Node) nodesIt.next();
            String nodeStr = node.asXML();
            domNodehandler(node, false);
        }
    }

    appendEndInfo();

    return results.toString();
}

From source file:com.globalsight.everest.edit.offline.ttx.TTXParser.java

License:Apache License

private void elementNodeProcessor(Node p_node, boolean p_isSource) {
    Element element = (Element) p_node;
    String eleStr = element.asXML();
    String elementName = element.getName();

    boolean isHeaderInfoUT = false;
    boolean isTuId = false;
    if (TTXConstants.TU.equalsIgnoreCase(elementName) || (element.getParent() != null
            && TTXConstants.TUV.equalsIgnoreCase(element.getParent().getName()))) {
        // latestPosition = TTXConstants.IN_TU;
    } else if (TTXConstants.UT.equalsIgnoreCase(elementName)) {
        Attribute att = element.attribute(TTXConstants.UT_ATT_DISPLAYTEXT);
        if (att != null) {
            String value = att.getValue();
            // If header info,return as header info has been handled
            // separately.
            // This check is not required.
            isHeaderInfoUT = isHeaderInfo(value);
            if (isHeaderInfoUT) {
                return;
            }//  w  w  w.  j a  va2s .  co m
            // If TuId,handle them here.
            isTuId = isTuId(value);
            if (isTuId) {
                // latestPosition = TTXConstants.TU_ID;
                String tuId = value.substring(value.indexOf(":") + 1).trim();
                if (results != null && results.length() > 0) {
                    results.append(TTXConstants.NEW_LINE).append(TTXConstants.NEW_LINE);
                }
                results.append(TTXConstants.HASH_MARK).append(tuId).append(TTXConstants.NEW_LINE);

                return;
            }
        }
    }

    if (element.nodeCount() > 0) {
        Iterator nodesIt = element.nodeIterator();
        while (nodesIt.hasNext()) {
            Node node = (Node) nodesIt.next();
            String nodeStr = node.asXML();
            String nodeName = node.getName();
            if (TTXConstants.TUV.equalsIgnoreCase(node.getName())) {
                Attribute langAtt = ((Element) node).attribute(TTXConstants.TUV_ATT_LANG);
                String lang = null;
                if (langAtt != null) {
                    lang = langAtt.getValue();
                }
                if (sourceLanguage != null && sourceLanguage.equals(lang)) {
                    // latestPosition = TTXConstants.IN_SOURCE_TUV;
                    // Not handle source TUV for TTX off-line uploading.
                    // domNodehandler(node, true);
                } else {
                    // latestPosition = TTXConstants.IN_TARGET_TUV;
                    domNodehandler(node, false);
                }
            } else {
                domNodehandler(node, false);
            }
        }
    } else {
        if (TTXConstants.UT.equalsIgnoreCase(elementName)) {
            Attribute displayTextAtt = element.attribute(TTXConstants.UT_ATT_DISPLAYTEXT);
            if (displayTextAtt != null) {
                String attValue = displayTextAtt.getValue();
                if (attValue != null && attValue.startsWith(TTXConstants.TU_ID)) {
                    // latestPosition = TTXConstants.TU_ID;
                    String tuId = attValue.substring(attValue.indexOf(":") + 1).trim();
                    if (results != null && results.length() > 0) {
                        results.append(TTXConstants.NEW_LINE).append(TTXConstants.NEW_LINE);
                    }
                    results.append(TTXConstants.HASH_MARK).append(tuId).append(TTXConstants.NEW_LINE);
                } else if (attValue != null && attValue.startsWith(TTXConstants.GS)) {
                    Attribute typeValueAtt = element.attribute(TTXConstants.UT_ATT_TYPE);
                    String typeValue = null;
                    if (typeValueAtt != null) {
                        typeValue = typeValueAtt.getValue();
                    }

                    String gsTag = attValue.substring(attValue.indexOf(":") + 1).trim();
                    if (typeValue != null && TTXConstants.UT_ATT_TYPE_START.equalsIgnoreCase(typeValue)) {
                        results.append("[").append(gsTag).append("]");
                    } else if (typeValue != null && TTXConstants.UT_ATT_TYPE_END.equalsIgnoreCase(typeValue)) {
                        results.append("[/").append(gsTag).append("]");
                    } else {
                        results.append("[").append(gsTag).append("]");
                        results.append("[/").append(gsTag).append("]");
                    }
                }
            }
        } else if (TTXConstants.DF.equalsIgnoreCase(elementName)) {
            // do not handle this.
        }
    }
}