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

📄 litexmldocument.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public LiteXMLDocument( MimeMediaType mimeType, InputStream in ) {
        super( null, (LiteXMLElement.tagRange) null );
        init( mimeType, new InputStreamReader( in ) );
    }
    
    public LiteXMLDocument( MimeMediaType mimeType, Reader in ) {
        super( null, (LiteXMLElement.tagRange) null );
        init(mimeType, in);
    }
    
    /** Initialises LiteXMLDocument. */
    private void init( MimeMediaType mimeType, Reader in ) {
        loc = new tagRange();
        doc = this;
        parent = this;
        
        this.mimeType = mimeType;
        
        StringBuffer theDoc = new StringBuffer();
        
        char [] smallBuffer = new char[512];
        
        do {
            try {
                int readCount = in.read(smallBuffer);
                if( readCount > 0 )
                    theDoc.append( smallBuffer, 0, readCount );
                if( readCount < 0 )
                    break;
            }
            catch( IOException ignored ) {
                break;
            }
        }
        
        while( true );
        
        docContent = new String( theDoc );
        theDoc = null;                      // no longer needed.
        
        // startTag will contain the xml declaration
        loc.startTag.start = 0;
        loc.startTag.end = docContent.indexOf( '>' );
        
        // body is everything after the xml declaration
        loc.body.start = loc.startTag.end + 1;
        loc.body.end = docContent.length() - 1;
        
        // end is the end of the doc.
        loc.endTag.start = loc.body.end;
        loc.endTag.end = loc.body.end;
        
        charRange docType = getDocType( doc.docContent, false );
        
        if( docType.isValid() ) {
            loc = getTagRanges( doc.docContent, docContent.substring( docType.start, docType.end + 1 ), loc.body );
        } else{
            loc = getTagRanges( doc.docContent, null, loc.body );
        }
        
        if( !loc.isValid() )
            throw new RuntimeException( "Parsing error in source document. could not find end of document" );
        
        addChildTags( loc.body, this );         // now add the subtags
    }
    
    /**
     *  cleanup object before destruction.
     **/
    protected void finalize() throws Throwable {
        super.finalize();
        
        // aggressively null members. this helps VM do GC.
        docContent = null;
        mimeType = null;
    }
    
    /**
     * get Mime Type
     */
    public MimeMediaType getMimeType() {
        return mimeType;
    }
    
    /**
     *  Returns the file extension type used by this Document. This value
     *  is chosen based upon the MIME Media Type for this Document.
     *
     * @since JXTA 1.0
     *
     * @return a string containing an appropriate file extension
     **/
    public String getFileExtension() {
        return TextDocumentCommon.Utils.getExtensionForMime(
        INSTANTIATOR.getSupportedFileExtensions(), getMimeType() );
    }
    
    /**
     * create a new element without value
     */
    public net.jxta.document.Element createElement( Object key ) {
        return createElement( key, null );
    }
    
    /**
     *  Create a new element with value
     *
     *  @param  key will be used for the name for the new element.
     *  @param  val will be used for the value for the element.
     *  @return the newly created element.
     */
    public net.jxta.document.Element createElement( Object key, Object val ) {
        if( !String.class.isAssignableFrom( key.getClass() ) )
            throw new ClassCastException( key.getClass().getName() + " not supported by createElement as key." );
        
        if( (null != val) && !String.class.isAssignableFrom( val.getClass() ) )
            throw new ClassCastException( val.getClass().getName() + " not supported by createElement as value." );
        
        return (net.jxta.document.Element) createElement( (String) key, (String) val );
    }
    
    /**
     * create a new element without value
     */
    public TextElement createElement( String name ) {
        return (TextElement) new LiteXMLElement( this, name );
    }
    
    /**
     * create a new element with value
     */
    public TextElement createElement(String name, String val) {
        return (TextElement) new LiteXMLElement( this, name, val );
    }
    
    /**
     * create a new element with value
     */
    protected TextElement createElement( tagRange loc ) {
        return (TextElement) new LiteXMLElement( this, loc );
    }
    
    /**
     *  Gets a reader for the document content.
     **/
    public Reader getReader() {
        StringWriter  stringOut = new StringWriter();
        
        stringOut.write( "<?xml version=\"1.0\"?>\n\n" );
        
        charRange result = getDocType( docContent, true );
        
        if( result.isValid() )
            stringOut.write( docContent.substring( result.start, result.end + 1 ) + "\n\n" );
        
        printNice( stringOut, 0, true );
        
        String asString = stringOut.toString();
        
        return new StringReader( asString );
    }
    
    /**
     *  Gets an inputStream for the document content.
     **/
    public InputStream getStream() {
        StringWriter  stringOut = new StringWriter();
        
        stringOut.write( "<?xml version=\"1.0\"?>\n\n" );
        
        charRange result = getDocType( docContent, true );
        
        if( result.isValid() )
            stringOut.write( docContent.substring( result.start, result.end + 1 ) + "\n\n" );
        
        printNice( stringOut, 0, true );
        
        String asString = stringOut.toString();

        return new ByteArrayInputStream( asString.getBytes() );
    }
    
    /**
     *  Sends the document content to a writer.
     **/
    public void sendToWriter( Writer stream ) throws IOException {
        stream.write( "<?xml version=\"1.0\"?>\n\n" );
        
        charRange result = getDocType( docContent, true );
        
        if( result.isValid() )
            stream.write( docContent.substring( result.start, result.end + 1 ) + "\n\n" );
        
        printNice( stream, 0, true );
    }
    
    /**
     *  Sends the document content to a stream.
     **/
    public void sendToStream( OutputStream stream ) throws IOException {
        Writer out = new BufferedWriter( new OutputStreamWriter( stream ) );
        
        sendToWriter( out );
        out.flush();
    }
    
    protected charRange getDocType( String source, boolean wholeElement ) {
        final String xmldoctype = "!DOCTYPE";
        charRange result = new charRange();
        int start = 0;
        int end = doc.docContent.length() - 1;
        tagRange ranges = getTagRanges( source, xmldoctype, new charRange( start, end ) );
        
        if( -1 == start )
            return result;
        
        if( !ranges.startTag.isValid() )
            return result;
        
        if( wholeElement ) {
            result = ranges.startTag;
        }
        else {
            result.start = ranges.startTag.start + 1 + xmldoctype.length() - 1 + 1;
            
            while( (result.start < end) &&           // immediately followed by a delimiter or the end of the tag
            ( -1 != " \t\n\r".indexOf( source.charAt(result.start) ) ) )
                result.start++;
            
            result.end = result.start;
            
            while( ((result.end + 1) < end ) && // immediately followed by a delimiter or the end of the tag
            ( -1 == " \t\n\r/>".indexOf( source.charAt(result.end + 1) ) ) )
                result.end++;
        }
        return result;
    }
}

⌨️ 快捷键说明

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