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

📄 litexmlelement.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
         */
        public boolean contains( charRange someRange ) {
            return( isValid() &&
            someRange.isValid() &&
            ( body.start <= someRange.start ) &&
            ( body.end >= someRange.end ) );
        }
    }

    /**
     * The document associated with this Element.
     */
    protected LiteXMLDocument doc;

    /**
     *  Identifies the element which is the parent of this element. If <code>
     *  this.parent == this</code> then this element is the root of the document.
     *  If <code>null == parent</code> then this element has not yet been
     *  inserted into the document.
     */
    protected Element parent;

    /**
     *  The portion of the source XML associated with this node
     */
    protected tagRange loc;

    /**
     *  If this node has yet to be inserted into the document then will contain
     *  the String value of this node, otherwise null.
     */
    private StringBuffer  uninserted = null;

    /**
     * The child elements associated with this element
     */
    private Vector children = new Vector();

    /**
     * Creates new LiteElement
     *
     * @param doc The {@link LiteXMLDocument} which is associated with this element.
     * @param name The name of the element being created.
     */
    protected LiteXMLElement(LiteXMLDocument doc, final String name) {
        this( doc, name, null );
    }

    /**
     * Creates new LiteElement
     * @param doc The {@link LiteXMLDocument} which is associated with this element.
     * @param name The name of the element being created.
     * @param val The value of the element being created.
     */
    protected LiteXMLElement(LiteXMLDocument doc, final String name, final String val) {
        this( doc, (LiteXMLElement.tagRange) null );
        loc = new tagRange();

        if( null == val )
            uninserted = new StringBuffer( );
        else {
            uninserted = new StringBuffer( val );
            encodeEscaped( uninserted );
        }

        uninserted.insert( 0, "<" + name + ">" );

        uninserted.append( "</" + name + ">" );
    }

    /**
     * Creates new LiteXMLElement
     * @param loc The location of the element within the document.
     * @param doc The {@link LiteXMLDocument} which is associated with this element.
     */
    protected LiteXMLElement(LiteXMLDocument doc, tagRange loc ) {
        this.doc = doc;
        this.loc = loc;
    }

    /**
     * Tests two elements for equality.
     *
     * @param element the element to be compared against.
     * @return true if the elements are equal (by whatever definition of
     * equality is appropriate) otherwise false
     */
    public boolean equals( Object element ) {
        if (this == element)
            return true;

        if( !(element instanceof LiteXMLElement) )
            return false;

        LiteXMLElement liteElement = (LiteXMLElement) element;

        if( doc != liteElement.doc )
            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 );
    }

    protected void finalize() throws Throwable {
        super.finalize();

        // aggressively null members. this helps VM do GC.
        doc = null;
        parent = null;
        children = null;
        uninserted = null;
        loc = null;
    }

    /**
     *  Get the root element of the hierarchy this element belongs to.
     *
     *  @return StructuredDocument root of this element's hierarchy.
     *
     *  @since JXTA 1.0
     */
    public StructuredDocument getRoot() {
        return doc;
    }

    /**
     *  Get the parent of this element. If the element has not been inserted into
     *  the Document then null is returned. If this element is the root of the
     *  Document then it returns itself.
     *
     *  @return Element parent of this element
     *
     *  @since JXTA 1.0
     */
    public Element getParent() {
        return parent;
    }

    /**
     * Returns an enumeration of the immediate children of this element
     *
     * @return Enumeration An enumeration containing all of the children of this element.
     *
     * @since JXTA 1.0
     */
    public Enumeration getChildren() {
        if( null != uninserted )
            throw new IllegalStateException( "This element has not been added." );
        return children.elements();
    }

    /**
     * Returns the name associated with this element.
     *
     * @return the name associated with this Element
     */
    public String getName() {
        if( null != uninserted )
            throw new IllegalStateException( "This element has not been added." );

        int current = loc.startTag.start + 1;

        while( current < loc.startTag.end )
            if( -1 != "\n\r\t />".indexOf( doc.docContent.charAt(current) ) )
                break;
            else
                current++;

        return doc.docContent.substring( loc.startTag.start + 1, current );
    }

    /**
     * Get the value (if any) associated with an element.
     *
     * @return A string containing the value of this element, if any, otherwise null.
     */
    public String getTextValue() {
        return getTextValue( false );
    }

    /**
     * Add a child element to this element
     *
     * @param element the element to be added as a child
     */
    public void appendChild( TextElement element ) {
        // FIXME  20010218    bondolo@jxta.org This section needs to be rewritten for empty
        // element declarations. ie. <element/> changed to <element>new</element>

        if( !(element instanceof LiteXMLElement) )
            throw new IllegalArgumentException( "Element type not supported." );

        LiteXMLElement newElement = (LiteXMLElement) element;

        if( newElement.doc != doc )
            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 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 ) {
            doc.docContent = doc.docContent.substring( 0, loc.endTag.start ) +
            newElement.uninserted +
            doc.docContent.substring( loc.endTag.start );

            newElement.loc.startTag.start = loc.endTag.start;
            newElement.loc.startTag.end = doc.docContent.indexOf( '>', newElement.loc.startTag.start );

            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 = doc.docContent.lastIndexOf( '<', newElement.loc.endTag.end );

            newElement.loc.body.end = newElement.loc.endTag.start - 1;

            if( 0 != loc.body.length() ) {
                doc.adjustLocations( loc.endTag.start, newElement.uninserted.length() );
            }
            else {
                loc.body.start--;
                doc.adjustLocations( loc.endTag.start, newElement.uninserted.length() );
                loc.body.start++;
            }

            loc.body.end += newElement.uninserted.length();

            newElement.uninserted = null;
        }

        // sanity check, the content of the new element must be contained within
        // the range of its parent.
        if( !loc.body.contains(newElement.loc) )
            throw new IllegalStateException( "Element is not contained by parent" );

        newElement.parent = this;
        children.addElement( newElement );

        if( !loc.isValid() )
            throw new IllegalStateException( "Document is damaged" );
    }

    /**
     * Returns an enumeration of the immediate children of this element whose
     * name match the specified string.
     *
     * @param name The name which will be matched against.
     * @return An enumeration containing all of the children of this element.
     */
    public Enumeration getChildren( String name ) {
        if( null != uninserted )
            throw new IllegalStateException( "This element has not been added." );

        Vector result = new Vector();
        for( Enumeration eachChild = children.elements(); eachChild.hasMoreElements(); ) {
            TextElement aChild = (TextElement) eachChild.nextElement();

            if( name.equals( aChild.getName() ) )
                result.addElement( aChild );
        }

        return result.elements();
    }

    /**
     *  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 ) {
        if( null != uninserted )
            throw new IllegalStateException( "This element has not been added." );

        if( !loc.isValid() )
            throw new IllegalStateException( "Corrupted Node" );

        StringBuffer building = new StringBuffer();

        Vector ranges = new Vector();

         /*
          * insert the ranges of the children in order. insertion method is ok
          * because the number of childre 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.elementAt( eachRange );
                if( 1 == rangeChild.compareTo( childsRange ) ) {
                    ranges.setElementAt( childsRange, eachRange );
                    childsRange = rangeChild;
                }
            }
            ranges.addElement( childsRange );
        }

        int current = loc.body.start;
        Enumeration eachRange = ranges.elements();

        // add all the text not part of some child
        while( eachRange.hasMoreElements() ) {
            charRange aRange = (charRange) eachRange.nextElement();

            building.append( doc.docContent.substring(current, aRange.start) );

            current = aRange.end + 1;
        }

        // Add the last bit.
        building.append( doc.docContent.substring( current, loc.endTag.start ) );

        if( !getEncoded )
            building = decodeEscaped( building );

        // trim (this is what String.trim() claims it does)

⌨️ 快捷键说明

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