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

📄 domxmldocument.java

📁 JXTA&#8482 is a set of open, generalized peer-to-peer (P2P) protocols that allow any networked devi
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     *                 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.     * @throws java.io.IOException Thrown for failures processing the document.     */    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}     */    @Override    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 DOMXMLElement createElement(Object key) {        return createElement(key, null);    }    /**     * {@inheritDoc}     */    public DOMXMLElement 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 createElement((String) key, (String) val);    }    // StructuredDocument Methods    /**     * {@inheritDoc}     */    public DOMXMLElement createElement(String name) {        return new DOMXMLElement(this, ((Document) domNode).createElement(name));    }    /**     * {@inheritDoc}     */    public DOMXMLElement createElement(String name, String value) {        Element root;        root = ((Document) domNode).createElement(name);        if (null != value) {            root.appendChild(((Document) domNode).createTextNode(value));        }        return new DOMXMLElement(this, root);    }    // Element Methods    // Protected & Private Methods    @Override    protected Node getAssocNode() {        return ((Document) domNode).getDocumentElement();    }}

⌨️ 快捷键说明

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