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

📄 xmldocument.java

📁 jxme的一些相关程序,主要是手机上程序开发以及手机和计算机通信的一些程序资料,程序编译需要Ant支持
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

            domNode = domImpl.createDocument( "http://www.jxta.org/jxta", docType, doctypeNode );
        } catch ( javax.xml.parsers.ParserConfigurationException misconfig ) {
            //PDA requirements 19.02.2002
            //class java.lang.reflect.UndeclaredThrowableException did not exist in jdk 1.1.8
            //it was change to its super class java.lang.RuntimeException
            //throw new UndeclaredThrowableException( misconfig );
            throw new RuntimeException();
            //PDA requirements 19.02.2002
        }

        if (value != null) {
            domNode.appendChild( ((org.w3c.dom.Document) domNode).createTextNode(value));
        }
    }


    /**
     * Constructor for new instances of <CODE>XMLDocument</CODE>
     *
     * @param mimeType This is the MIME Media Type being requested. In general it should be equivalent with
     * the MIME Media Type returned by {@link #getMimeType()}. A <CODE>StructuredDocument</CODE>
     * sub-class may, however, support more than one MIME Media type so this may be a
     * true parameter. XMLDocument supports additional the MIME Media Type parameters,
     * "charset". The charset parameter must be a value per IETF RFC 2279 or ISO-10646.
     * @param docType Used as the root type of this document. {@link XMLDocument} uses this as the XML
     * <CODE>DOCTYPE</CODE>.
     * @throws RuntimeException Exceptions generated by the underlying implementation.
     */
    public XMLDocument( final MimeMediaType mimeType, final String docType ) {
        this( mimeType, docType, null);
    }

    /**
     * Constructor for existing documents.
     *
     * @param mimeType This is the MIME Media Type being requested. In general
     * it should be equivalent with the MIME Media Type returned by
     * {@link #getMimeType()}. A <CODE>StructuredDocument</CODE> sub-class may,
     * however, support more than one MIME Media type so this may be a
     * true parameter. XMLDocument supports additional the MIME Media Type parameters,
     * "charset". The charset parameter must be a value per IETF RFC 2279 or ISO-10646.
     * @param stream Contains the input used to construct this object. This stream should be of a type
     * conformant with the type specified by the MIME Media Type "charset" parameter.
     *
     * @throws RuntimeException Propagates exceptions from the underlying implementation.
     */
    public XMLDocument( final MimeMediaType mimeType, final InputStream stream )
    throws IOException {
        super( null, null );
        this.root = this;

        this.mimeType = (MimeMediaType) mimeType.clone();

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(true);
        docBuilderFactory.setValidating(false);
        try {
            DocumentBuilder dataDocBuilder = docBuilderFactory.newDocumentBuilder();
            domNode = dataDocBuilder.parse( new InputSource(stream) );
        } catch ( javax.xml.parsers.ParserConfigurationException misconfig ) {
            //PDA requirements 19.02.2002
            //class java.lang.reflect.UndeclaredThrowableException did not exist in jdk 1.1.8
            //it was change to its super class java.lang.RuntimeException
            //throw new UndeclaredThrowableException( misconfig );
            throw new RuntimeException();
            //PDA requirements 19.02.2002
        } catch ( org.xml.sax.SAXException parseError ) {
            throw new IOException( parseError.toString() );
        }
    }

    /**
     * Returns the MIME Media type of this Document per
     * {@link <a href=http://www.ietf.org/rfc/rfc2046.txt">IETF RFC 2046 <i>MIME : Media Types</i></a>}.
     *
     * @return The MIME Media Type of this document including any parameter declarations.
     *
     */
    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() );
    }

    /**
     * Returns a stream of bytes which represent the content of this Document.
     *
     * @return An {@link InputStream} containting the bytes of this Document.
     */
    public InputStream getStream() {
        // XXX bondolo@jxta.org  20010208    This has to be changed. We should be able to use the StringWriter directly.
        try {
            OutputFormat    format  = new OutputFormat( ((org.w3c.dom.Document) domNode) );   //Serialize DOM
            StringWriter  stringOut = new StringWriter();
            // Writer will be a String
            XMLSerializer    serial = new XMLSerializer( stringOut, format );
            serial.asDOMSerializer();                   // As a DOM Serializer

            serial.serialize( ((org.w3c.dom.Document) domNode).getDocumentElement() );

            return new ByteArrayInputStream( stringOut.toString().getBytes() );
        } catch ( Exception ex ) {
            ex.printStackTrace();
            return null;
        }
    }

    public void sendToStream( OutputStream stream ) throws IOException {
        Writer out = new BufferedWriter(new OutputStreamWriter(stream));

        sendToWriter( out );
        out.flush();
    }

    /**
     * Returns a stream of characters which represent the content of this Document.
     *
     * @return An {@link InputStream} containting the bytes of this Document.
     */
    public Reader getReader() {
        // XXX bondolo@jxta.org  20010208    This has to be changed. We should be able to use the StringWriter directly.
        try {
            OutputFormat    format  = new OutputFormat( ((org.w3c.dom.Document) domNode) );   //Serialize DOM
            StringWriter  stringOut = new StringWriter();
            // Writer will be a String
            XMLSerializer    serial = new XMLSerializer( stringOut, format );
            serial.asDOMSerializer();                   // As a DOM Serializer

            serial.serialize( ((org.w3c.dom.Document) domNode).getDocumentElement() );

            return new StringReader( stringOut.toString() );
        } catch ( Exception ex ) {
            ex.printStackTrace();
            return null;
        }
    }

    public void sendToWriter( Writer stream ) throws IOException {
        // XXX bondolo@jxta.org  20010208    This has to be changed. We should be able to use the StringWriter directly.
        try {
            OutputFormat    format  = new OutputFormat( ((org.w3c.dom.Document) domNode) );   //Serialize DOM
            // Writer will be a String
            XMLSerializer    serial = new XMLSerializer( stream, format );
            serial.asDOMSerializer();                   // As a DOM Serializer

            serial.serialize( ((org.w3c.dom.Document) domNode).getDocumentElement() );
        } catch ( IOException ex ) {
            throw ex;
        }
        catch ( Exception ex ) {
            throw new IOException( ex.toString() );
        }
    }

    /**
     * create a new element without value
     */
    public net.jxta.document.Element createElement( Object key ) {
        return createElement( key, null );
    }

    /**
     * create a new element with value
     */
    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." );

        if( (null!=val) && !String.class.isAssignableFrom( val.getClass() ) )
            throw new ClassCastException( val.getClass().getName() + " not supported by createElement." );

        return (net.jxta.document.Element) createElement( (String) key, (String) val );
    }

    // StructuredDocument Methods

    /**
     * Create a new element without value
     *
     * @param   name    The name of the element to be created.
     * @return  The new element.
     */
    public TextElement createElement(String name) {
        return (TextElement) new XMLElement( this, ((org.w3c.dom.Document) domNode).createElement(name));
    }

    /**
     * Create a new element with value
     *
     * @param   name    The name of the element to be created.
     * @param   value   The value of the element to be created.
     * @return  The new element.
     */
    public TextElement createElement(String name, String value) {
        org.w3c.dom.Element root;

        root = ((org.w3c.dom.Document) domNode).createElement(name);
        if( null != value )
            root.appendChild( ((org.w3c.dom.Document) domNode).createTextNode(value));
        return (TextElement) new XMLElement( this, root);
    }

    // Element Methods

    // Protected & Private Methods

    protected Node getAssocNode() {
        return (Node)((org.w3c.dom.Document) domNode).getDocumentElement();
    }

}

⌨️ 快捷键说明

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