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

📄 litexmlelement.java

📁 jxta_src_2.41b jxta 2.41b 最新版源码 from www.jxta.org
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     *  content to the element.     **/    public LiteXMLElement(LiteXMLDocument doc, final String name, final String val) {        this(doc, new tagRange());                for (int eachChar = name.length() - 1; eachChar >= 0; eachChar--) {            if (Character.isWhitespace(name.charAt(eachChar))) {                throw new IllegalArgumentException("Element names may not contain spaces.");            }        }                if ((null == val) || (0 == val.length())) {            uninserted = new StringBuffer("<" + name + "/>");        } else {            uninserted = new StringBuffer(val);            encodeEscaped(uninserted);            uninserted.insert(0, "<" + name + ">");            uninserted.append("</" + name + ">");        }    }        /**     *  {@inheritDoc}     **/    public boolean equals(Object element) {        if (this == element) {            return true;        }                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;        }                if ((null == val1) || (null == val2)) {            return false;        }                return val1.equals(val2);    }        /**     *  {@inheritDoc}     *     *  <p/>A toString implementation for debugging purposes.     **/    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 StructuredDocument getRoot() {        return getDocument();    }        /**     *  {@inheritDoc}     **/    public Element getParent() {        return parent;    }        /**     *  {@inheritDoc}     **/    public Enumeration getChildren() {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }                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);    }        /**     *  {@inheritDoc}     **/    public void appendChild(TextElement element) {        if (!(element instanceof LiteXMLElement)) {            throw new IllegalArgumentException("Element type not supported.");        }                LiteXMLElement newElement = (LiteXMLElement) element;                if (newElement.getDocument() != getDocument()) {            throw new IllegalArgumentException("Wrong document");        }                if (null != newElement.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 != newElement.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, newElement.uninserted);                        newElement.loc.startTag.start = loc.endTag.start;            newElement.loc.startTag.end = getDocument().docContent.indexOf(">", newElement.loc.startTag.start);                        if ('/' != newElement.uninserted.charAt(newElement.uninserted.length() - 2)) {                newElement.loc.body.start = newElement.loc.startTag.end + 1;                                newElement.loc.endTag.end = newElement.loc.startTag.start + newElement.uninserted.length() - 1;                newElement.loc.endTag.start = getDocument().docContent.lastIndexOf("<", newElement.loc.endTag.end);                                newElement.loc.body.end = newElement.loc.endTag.start - 1;            } else {                newElement.loc.body = new charRange(newElement.loc.startTag.start, newElement.loc.startTag.end);                newElement.loc.endTag = new charRange(newElement.loc.startTag.start, newElement.loc.startTag.end);            }                        if (0 != loc.body.length()) {                getDocument().adjustLocations(loc.endTag.start, newElement.uninserted.length());            } else {                loc.body.start--;                getDocument().adjustLocations(loc.endTag.start, newElement.uninserted.length());                loc.body.start++;            }                        loc.body.end += newElement.uninserted.length();                        newElement.uninserted = null;        }                newElement.parent = this;        children.add(newElement);                if (paranoidConsistencyChecking) {            checkConsistency();        }    }        /**     *  {@inheritDoc}     **/    public Enumeration getChildren(String name) {        if (null != uninserted) {            throw new IllegalStateException("This element has not been added.");        }                if (paranoidConsistencyChecking) {            checkConsistency();        }                List result = new ArrayList();        for (Iterator eachChild = children.iterator(); eachChild.hasNext();) {            TextElement aChild = (TextElement) eachChild.next();                        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;     *  @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();        }                StringBuffer building = new StringBuffer();                List ranges = new ArrayList();                /*         * 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 eachChild = getChildren(); eachChild.hasMoreElements();) {            LiteXMLElement aChild = (LiteXMLElement) 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 = (charRange) ranges.get(eachRange);                if (1 == rangeChild.compareTo(childsRange)) {                    ranges.set(eachRange, childsRange);                    childsRange = rangeChild;                }            }            ranges.add(childsRange);        }                int current = loc.body.start;        Iterator eachRange = ranges.iterator();                // add all the text not part of some child        while (eachRange.hasNext()) {            charRange aRange = (charRange) eachRange.next();                        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.     **/    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        StringBuffer start = new StringBuffer();                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);                                if (-1 != indent) {                    into.write('\n');                }            }

⌨️ 快捷键说明

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