org.dommons.dom.dom4j.XDom4jNode.java Source code

Java tutorial

Introduction

Here is the source code for org.dommons.dom.dom4j.XDom4jNode.java

Source

/*
 * @(#)XDom4jNode.java     2013-3-6
 *
 * Copyright (c) 2011-2013 ??? 
 * ?? 
 * ??????????
 * Copyright (c) 2011-2013 HUPUN Network Technology CO.,LTD.
 * All rights reserved.
 * This software is the confidential and proprietary information of HUPUN
 * Network Technology CO.,LTD("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with HUPUN.
 * Websitehttp://www.hupun.com
 */
package org.dommons.dom.dom4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Branch;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dommons.dom.bean.XElement;
import org.dommons.dom.bean.XNode;

/**
 * dom4j XML 
 * @author Demon 2013-3-6
 */
abstract class XDom4jNode<N extends XNode> extends XDom4jBean implements XNode<N> {

    /**
     * ??
     * @param parent 
     * @param name ???
     * @return 
     */
    private static Element element(Branch parent, String name) {
        for (Iterator<Node> it = parent.nodeIterator(); it.hasNext();) {
            Node node = it.next();
            if (node.getNodeType() == Node.ELEMENT_NODE && name.equals(node.getName()))
                return (Element) node;
        }
        return null;
    }

    public N addElement(XElement child) {
        return add(child);
    }

    public XElement element(String... names) {
        Branch ele = element(names, 0, 0);
        return ele != null && ele instanceof Element ? new XDom4jElement((Element) ele) : null;
    }

    public List<XElement> elements() {
        List<XElement> list = new ArrayList();
        for (Iterator<Node> it = target().nodeIterator(); it.hasNext();) {
            Node node = it.next();
            if (node.getNodeType() == Node.ELEMENT_NODE)
                list.add(new XDom4jElement((Element) node));
        }
        return list;
    }

    public List<XElement> elements(String... names) {
        Branch ele = element(names, 0, -1);
        List<XElement> list = new ArrayList();
        if (ele != null) {
            for (Iterator<Node> it = ele.nodeIterator(); it.hasNext();) {
                Node node = it.next();
                if (node.getNodeType() == Node.ELEMENT_NODE && names[names.length - 1].equals(node.getName()))
                    list.add(new XDom4jElement((Element) node));
            }
        }
        return list;
    }

    public String elementText(String... names) {
        XElement ele = element(names);
        return ele == null ? null : ele.textTrim();
    }

    /**
     * ??
     * @param names ????
     * @param start 
     * @param end 
     * @return ?
     */
    protected Branch element(String[] names, int start, int end) {
        int len = names == null ? 0 : names.length;
        if (start < 0)
            start = len + start;
        if (end <= 0)
            end = len + end;
        Branch ele = start < end ? null : target();
        for (int i = start; i < len && i < end; i++) {
            ele = ele == null ? element(target(), names[i]) : element(ele, names[i]);
            if (ele == null)
                break;
        }
        return ele;
    }

    protected abstract Branch target();
}