📄 domxmldocument.java
字号:
} /** * 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. */ private DOMXMLDocument(final MimeMediaType mimeType, final InputStream stream) throws IOException { super( null, null ); this.root = this; this.mimeType = mimeType; String charset = mimeType.getParameter("charset"); Reader source; if (charset == null) { source = new InputStreamReader( stream ); } else { source = new InputStreamReader( stream, charset ); } DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); docBuilderFactory.setValidating(false); try { DocumentBuilder dataDocBuilder = docBuilderFactory.newDocumentBuilder(); domNode = dataDocBuilder.parse( new InputSource(source) ); } catch ( ParserConfigurationException misconfig ) { throw new UndeclaredThrowableException( misconfig ); } catch ( SAXException parseError ) { throw new IOException( parseError.toString() ); } } /** * 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 source Contains the input used to construct this object. This reader * 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. */ private DOMXMLDocument(final MimeMediaType mimeType, final Reader source) throws IOException { super( null, null ); this.root = this; this.mimeType = mimeType; DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setNamespaceAware(true); docBuilderFactory.setValidating(false); try { DocumentBuilder dataDocBuilder = docBuilderFactory.newDocumentBuilder(); domNode = dataDocBuilder.parse( new InputSource(source) ); } catch ( ParserConfigurationException misconfig ) { throw new UndeclaredThrowableException( misconfig ); } catch ( SAXException parseError ) { throw new IOException( parseError.toString() ); } } /** * {@inheritDoc} **/ public String toString( ) { try { StringWriter stringOut = new StringWriter(); sendToWriter( stringOut ); stringOut.close(); return stringOut.toString(); } catch ( IOException ex ) { throw new UndeclaredThrowableException( ex ); } } /** * {@inheritDoc} **/ public MimeMediaType getMimeType() { return mimeType; } /** * {@inheritDoc} **/ public String getFileExtension() { return Utils.getExtensionForMime( INSTANTIATOR.getSupportedFileExtensions(), getMimeType() ); } /** * {@inheritDoc} **/ public InputStream getStream() throws IOException { String result = toString(); if( null == result ) return null; String charset = mimeType.getParameter("charset"); if (charset == null) { return new ByteArrayInputStream( result.getBytes() ); } else { return new ByteArrayInputStream( result.getBytes( charset ) ); } } /** * {@inheritDoc} **/ public void sendToStream( OutputStream stream ) throws IOException { Writer osw; String charset = mimeType.getParameter("charset"); if (charset == null) { osw = new OutputStreamWriter( stream ); } else { osw = new OutputStreamWriter( stream, charset ); } Writer out = new BufferedWriter(osw); sendToWriter( out ); out.flush(); osw.flush(); } /** * {@inheritDoc} **/ public Reader getReader() { String result = toString(); if( null == result ) return null; return new StringReader( result ); } /** * {@inheritDoc} **/ public void sendToWriter( Writer writer ) throws IOException { String charset = mimeType.getParameter("charset"); try { DOMImplementationLS domImpl = (DOMImplementationLS) ((Document) domNode).getImplementation().getFeature( "LS", "3.0" ); LSOutput output = domImpl.createLSOutput(); if (charset != null) { output.setEncoding( charset ); } else { output.setEncoding( java.nio.charset.Charset.defaultCharset().name()); } output.setCharacterStream( writer ); LSSerializer serial = domImpl.createLSSerializer(); serial.write( domNode, output ); } catch ( Throwable ex ) { if( ex instanceof RuntimeException ) throw (RuntimeException) ex; else if( ex instanceof Error ) throw (Error) ex; else throw new UndeclaredThrowableException( ex ); } } /** * {@inheritDoc} **/ public net.jxta.document.Element createElement( Object key ) { return createElement( key, null ); } /** * {@inheritDoc} **/ 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 /** * {@inheritDoc} **/ public TextElement createElement(String name) { return (TextElement) new DOMXMLElement( this, ((Document) domNode).createElement(name)); } /** * {@inheritDoc} **/ public TextElement createElement( String name, String value ) { Element root; root = ((Document) domNode).createElement(name); if( null != value ) root.appendChild( ((Document) domNode).createTextNode(value)); return (TextElement) new DOMXMLElement( this, root); } // Element Methods // Protected & Private Methods protected Node getAssocNode() { return (Node)((Document) domNode).getDocumentElement(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -