⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 litexmlelement.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        if (!(element instanceof LiteXMLElement)) {            return false;        }        LiteXMLElement liteElement = (LiteXMLElement) element;        if (getDocument() != liteElement.getDocument()) {            return false;        }        if (!getName().equals(liteElement.getName())) {            return false;        }        String val1;        if (null != uninserted) {            val1 = uninserted.toString();        } else {            val1 = getTextValue();        }        String val2 = liteElement.getTextValue();        if ((null == val1) && (null == val2)) {            return true;        }        return null != val1 && null != val2 && val1.equals(val2);    }    /**     * {@inheritDoc}     * <p/>     * <p/>A toString implementation for debugging purposes.     */    @Override    public String toString() {        if (paranoidConsistencyChecking) {            checkConsistency();        }        String name = getName();        if (name == null) {            name = "<<null name>>";        }        String value = getTextValue();        if (value == null) {            value = "<<null value>>";        }        if ((value.length() + name.length()) >= 60) {            int len = Math.max(20, 60 - name.length());            value = value.substring(0, Math.min(len, value.length()));        }        // FIXME 20021125 bondolo@jxta.org should remove carriage control.        return super.toString() + " / " + name + " = " + value;    }    /**     * {@inheritDoc}     */    public LiteXMLDocument getRoot() {        return getDocument();    }    /**     * {@inheritDoc}     */    public LiteXMLElement getParent() {        return parent;    }    /**     * {@inheritDoc}     */    public Enumeration<LiteXMLElement> getChildren() {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }        if (null == children) {            List<LiteXMLElement> empty = Collections.emptyList();            return Collections.enumeration(empty);        } else {            return Collections.enumeration(children);        }    }    /**     * {@inheritDoc}     */    public String getName() {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }        if (paranoidConsistencyChecking) {            checkConsistency();        }        int current = loc.startTag.start + 1;        while (current <= loc.startTag.end) {            char inTagName = getDocument().docContent.charAt(current);            if (Character.isWhitespace(inTagName) || ('/' == inTagName) || ('>' == inTagName)) {                break;            }            current++;        }        return getDocument().docContent.substring(loc.startTag.start + 1, current);    }    /**     * Get the name associated with an element.     *     * @return A string containing the key of this element.     */    public String getKey() {        return getName();    }    /**     * Get the value (if any) associated with an element.     *     * @return A string containing the value of this element, if any, otherwise null.     */    public String getValue() {        return getTextValue();    }    /**     * {@inheritDoc}     */    public void appendChild(LiteXMLElement element) {        if (element.getDocument() != getDocument()) {            throw new IllegalArgumentException("Wrong document");        }        if (null != element.parent) {            throw new IllegalArgumentException("New element is already in document");        }        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }        if (paranoidConsistencyChecking) {            checkConsistency();        }        // If uninserted then this new element contains content which needs to        // be added to the document. If uninserted is null then the child        // element's content is already in the document, but merely needs to        // be recognized as a child.        if (null != element.uninserted) {            if (loc.startTag.equals(loc.endTag)) {                getDocument().docContent.deleteCharAt(loc.endTag.end - 1); // delete the /                loc.startTag.end -= 1;                // skip past the name portion                int current = loc.startTag.start + 1;                while (current <= loc.startTag.end) {                    char inTagName = getDocument().docContent.charAt(current);                    if (Character.isWhitespace(inTagName) || ('>' == inTagName)) {                        break;                    }                    current++;                }                String tagName = getDocument().docContent.substring(loc.startTag.start + 1, current);                getDocument().docContent.insert(loc.startTag.end + 1, "</" + tagName + ">");                getDocument().adjustLocations(loc.startTag.end + 1, tagName.length() + 2);                loc.endTag = new charRange(loc.startTag.end + 1, loc.startTag.end + 3 + tagName.length());                loc.body = new charRange(loc.startTag.end + 1, loc.startTag.end);            }            getDocument().docContent.insert(loc.endTag.start, element.uninserted);            element.loc.startTag.start = loc.endTag.start;            element.loc.startTag.end = getDocument().docContent.indexOf(">", element.loc.startTag.start);            if ('/' != element.uninserted.charAt(element.uninserted.length() - 2)) {                element.loc.body.start = element.loc.startTag.end + 1;                element.loc.endTag.end = element.loc.startTag.start + element.uninserted.length() - 1;                element.loc.endTag.start = getDocument().docContent.lastIndexOf("<", element.loc.endTag.end);                element.loc.body.end = element.loc.endTag.start - 1;            } else {                element.loc.body = new charRange(element.loc.startTag.start, element.loc.startTag.end);                element.loc.endTag = new charRange(element.loc.startTag.start, element.loc.startTag.end);            }            if (0 != loc.body.length()) {                getDocument().adjustLocations(loc.endTag.start, element.uninserted.length());            } else {                loc.body.start--;                getDocument().adjustLocations(loc.endTag.start, element.uninserted.length());                loc.body.start++;            }            loc.body.end += element.uninserted.length();            element.uninserted = null;        }        element.parent = this;        if (null == children) {            children = new ArrayList<LiteXMLElement>();        }        children.add(element);        if (paranoidConsistencyChecking) {            checkConsistency();        }    }    /**     * Returns an enumeration of the immediate children of this element whose     * name match the specified string.     *     * @param key The key which will be matched against.     * @return enumeration containing all of the children of this element.     */    public Enumeration<LiteXMLElement> getChildren(Object key) {        if (key instanceof String)            return getChildren((String) key);        else            throw new ClassCastException(key.getClass().getName() + " not supported by getChildren.");    }    /**     * {@inheritDoc}     */    public Enumeration<LiteXMLElement> getChildren(String name) {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }        if (paranoidConsistencyChecking) {            checkConsistency();        }        if (null == children) {            List<LiteXMLElement> empty = Collections.emptyList();            return Collections.enumeration(empty);        }        List<LiteXMLElement> result = new ArrayList<LiteXMLElement>();        for (LiteXMLElement aChild : children) {            if (name.equals(aChild.getName())) {                result.add(aChild);            }        }        return Collections.enumeration(result);    }    /**     * {@inheritDoc}     */    public String getTextValue() {        return getTextValue(false, true);    }    /**     * Get the value (if any) associated with an element.     *     * @param getEncoded if true then the contents will be encoded such that     *                   the contents will not be interpreted as XML. see     *                   {@link <a href="http://www.w3.org/TR/REC-xml#syntax">W3C XML 1.0 Specification</a>}     *                   ie. < -> &lt; & -> &amp;     * @param trim       if true trims prefix and suffix white space     * @return A string containing the value of this element, if any, otherwise null.     */    protected String getTextValue(boolean getEncoded, boolean trim) {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }        if (paranoidConsistencyChecking) {            checkConsistency();        }        StringBuilder building = new StringBuilder();        List<charRange> ranges = new ArrayList<charRange>();        /*         * insert the ranges of the children in order. insertion method is ok         * because the number of children is usually less than 10 or so.         */        for (Enumeration<LiteXMLElement> eachChild = getChildren(); eachChild.hasMoreElements();) {            LiteXMLElement aChild = eachChild.nextElement();            charRange childsRange = new charRange(aChild.loc.startTag.start, aChild.loc.endTag.end);            // find where to insert.            for (int eachRange = 0; eachRange < ranges.size(); eachRange++) {                charRange rangeChild = ranges.get(eachRange);                if (1 == rangeChild.compareTo(childsRange)) {                    ranges.set(eachRange, childsRange);                    childsRange = rangeChild;                }            }            ranges.add(childsRange);        }        int current = loc.body.start;        // add all the text not part of some child        for (charRange aRange : ranges) {            building.append(getDocument().docContent.substring(current, aRange.start));            current = aRange.end + 1;        }        // Add the last bit.        building.append(getDocument().docContent.substring(current, loc.endTag.start));        if (!getEncoded) {            building = decodeEscaped(building);        }        // trim        int firstNonWhiteSpace = 0;        int lastNonWhiteSpace = building.length() - 1;        if (trim) {            while (firstNonWhiteSpace < building.length()) {                char possibleSpace = building.charAt(firstNonWhiteSpace);                if (!Character.isWhitespace(possibleSpace)) {                    break;                }                firstNonWhiteSpace++;            }            // did we find no non-whitespace?            if (firstNonWhiteSpace >= building.length()) {                return null;            }            while (lastNonWhiteSpace >= firstNonWhiteSpace) {                char possibleSpace = building.charAt(lastNonWhiteSpace);                if (!Character.isWhitespace(possibleSpace)) {                    break;                }                lastNonWhiteSpace--;            }        }        String result = building.substring(firstNonWhiteSpace, lastNonWhiteSpace + 1);        return result;    }    /**     * Write the contents of this element and optionally its children. The     * writing is done to a provided <code>java.io.Writer</code>. The writing     * can optionally be indented.     *     * @param into    The java.io.Writer that the output will be sent to.     * @param indent  the number of tabs which will be inserted before each     *                line.     * @param recurse if true then also print the children of this element.     * @throws java.io.IOException if an io error occurs     */    protected void printNice(Writer into, int indent, boolean recurse) throws IOException {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }        if (paranoidConsistencyChecking) {            checkConsistency();        }        // print start tag        StringBuilder start = new StringBuilder();        if (-1 != indent) {            // do indent            for (int eachTab = 0; eachTab < indent; eachTab++) {                start.append('\t');            }        }        start.append(getDocument().docContent.substring(loc.startTag.start, loc.startTag.end + 1));        if (-1 != indent) {            start.append('\n');        }        into.write(start.toString());        // print the rest if this was not an empty element.        if (!loc.startTag.equals(loc.endTag)) {            String itsValue = getTextValue(true, (-1 != indent));            // print node value            if (null != itsValue) {                if (-1 != indent) {                    // do indent                    for (int eachTab = 0; eachTab < indent + 1; eachTab++) {                        into.write("\t");                    }                }                into.write(itsValue);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -