tostream.java

来自「JAVA 所有包」· Java 代码 · 共 2,016 行 · 第 1/5 页

JAVA
2,016
字号
        writer.write(value);        writer.write("\">");        writer.write(m_lineSep, 0, m_lineSepLen);    }    /**     * Output a system-dependent line break.     *     * @throws org.xml.sax.SAXException     */    protected final void outputLineSep() throws IOException    {        m_writer.write(m_lineSep, 0, m_lineSepLen);    }    /**     * Specifies an output format for this serializer. It the     * serializer has already been associated with an output format,     * it will switch to the new format. This method should not be     * called while the serializer is in the process of serializing     * a document.     *     * @param format The output format to use     */    public void setOutputFormat(Properties format)    {        boolean shouldFlush = m_shouldFlush;        init(m_writer, format, false, false);        m_shouldFlush = shouldFlush;    }    /**     * Initialize the serializer with the specified writer and output format.     * Must be called before calling any of the serialize methods.     * This method can be called multiple times and the xsl:output properties     * passed in the 'format' parameter are accumulated across calls.     *     * @param writer The writer to use     * @param format The output format     * @param shouldFlush True if the writer should be flushed at EndDocument.     */    private synchronized void init(        Writer writer,        Properties format,        boolean defaultProperties,        boolean shouldFlush)    {        m_shouldFlush = shouldFlush;                // if we are tracing events we need to trace what        // characters are written to the output writer.        if (m_tracer != null         && !(writer instanceof SerializerTraceWriter)  )            m_writer = new SerializerTraceWriter(writer, m_tracer);        else            m_writer = writer;                        m_format = format;        //        m_cdataSectionNames =        //            OutputProperties.getQNameProperties(        //                OutputKeys.CDATA_SECTION_ELEMENTS,        //                format);        setCdataSectionElements(OutputKeys.CDATA_SECTION_ELEMENTS, format);        setIndentAmount(            OutputPropertyUtils.getIntProperty(                OutputPropertiesFactory.S_KEY_INDENT_AMOUNT,                format));        setIndent(            OutputPropertyUtils.getBooleanProperty(OutputKeys.INDENT, format));                    {            String sep =                     format.getProperty(OutputPropertiesFactory.S_KEY_LINE_SEPARATOR);            if (sep != null) {                m_lineSep = sep.toCharArray();                m_lineSepLen = sep.length();            }        }        boolean shouldNotWriteXMLHeader =            OutputPropertyUtils.getBooleanProperty(                OutputKeys.OMIT_XML_DECLARATION,                format);        setOmitXMLDeclaration(shouldNotWriteXMLHeader);        setDoctypeSystem(format.getProperty(OutputKeys.DOCTYPE_SYSTEM));        String doctypePublic = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);        setDoctypePublic(doctypePublic);        // if standalone was explicitly specified        if (format.get(OutputKeys.STANDALONE) != null)        {            String val = format.getProperty(OutputKeys.STANDALONE);            if (defaultProperties)                setStandaloneInternal(val);            else                setStandalone(val);        }        setMediaType(format.getProperty(OutputKeys.MEDIA_TYPE));        if (null != doctypePublic)        {            if (doctypePublic.startsWith("-//W3C//DTD XHTML"))                m_spaceBeforeClose = true;        }        /*          * This code is added for XML 1.1 Version output.         */        String version = getVersion();        if (null == version)        {            version = format.getProperty(OutputKeys.VERSION);            setVersion(version);        }        // initCharsMap();        String encoding = getEncoding();        if (null == encoding)        {            encoding =                Encodings.getMimeEncoding(                    format.getProperty(OutputKeys.ENCODING));            setEncoding(encoding);        }        m_isUTF8 = encoding.equals(Encodings.DEFAULT_MIME_ENCODING);        // Access this only from the Hashtable level... we don't want to         // get default properties.        String entitiesFileName =            (String) format.get(OutputPropertiesFactory.S_KEY_ENTITIES);        if (null != entitiesFileName)        {            String method =                 (String) format.get(OutputKeys.METHOD);                        m_charInfo = CharInfo.getCharInfo(entitiesFileName, method);        }    }    /**     * Initialize the serializer with the specified writer and output format.     * Must be called before calling any of the serialize methods.     *     * @param writer The writer to use     * @param format The output format     */    private synchronized void init(Writer writer, Properties format)    {        init(writer, format, false, false);    }    /**     * Initialize the serializer with the specified output stream and output     * format. Must be called before calling any of the serialize methods.     *     * @param output The output stream to use     * @param format The output format     * @param defaultProperties true if the properties are the default     * properties     *      * @throws UnsupportedEncodingException The encoding specified   in the     * output format is not supported     */    protected synchronized void init(        OutputStream output,        Properties format,        boolean defaultProperties)        throws UnsupportedEncodingException    {        String encoding = getEncoding();        if (encoding == null)        {            // if not already set then get it from the properties            encoding =                Encodings.getMimeEncoding(                    format.getProperty(OutputKeys.ENCODING));            setEncoding(encoding);        }        if (encoding.equalsIgnoreCase("UTF-8"))        {            m_isUTF8 = true;            //            if (output instanceof java.io.BufferedOutputStream)            //            {            //                init(new WriterToUTF8(output), format, defaultProperties, true);            //            }            //            else if (output instanceof java.io.FileOutputStream)            //            {            //                init(new WriterToUTF8Buffered(output), format, defaultProperties, true);            //            }            //            else            //            {            //                // Not sure what to do in this case.  I'm going to be conservative             //                // and not buffer.            //                init(new WriterToUTF8(output), format, defaultProperties, true);            //            }                         init(                    new WriterToUTF8Buffered(output),                    format,                    defaultProperties,                    true);        }        else if (            encoding.equals("WINDOWS-1250")                || encoding.equals("US-ASCII")                || encoding.equals("ASCII"))        {            init(new WriterToASCI(output), format, defaultProperties, true);        }        else        {            Writer osw;            try            {                osw = Encodings.getWriter(output, encoding);            }            catch (UnsupportedEncodingException uee)            {                System.out.println(                    "Warning: encoding \""                        + encoding                        + "\" not supported"                        + ", using "                        + Encodings.DEFAULT_MIME_ENCODING);                encoding = Encodings.DEFAULT_MIME_ENCODING;                setEncoding(encoding);                osw = Encodings.getWriter(output, encoding);            }            init(osw, format, defaultProperties, true);        }    }    /**     * Returns the output format for this serializer.     *     * @return The output format in use     */    public Properties getOutputFormat()    {        return m_format;    }    /**     * Specifies a writer to which the document should be serialized.     * This method should not be called while the serializer is in     * the process of serializing a document.     *     * @param writer The output writer stream     */    public void setWriter(Writer writer)    {                // if we are tracing events we need to trace what         // characters are written to the output writer.        if (m_tracer != null         && !(writer instanceof SerializerTraceWriter)  )            m_writer = new SerializerTraceWriter(writer, m_tracer);        else            m_writer = writer;    }        /**     * Set if the operating systems end-of-line line separator should     * be used when serializing.  If set false NL character      * (decimal 10) is left alone, otherwise the new-line will be replaced on     * output with the systems line separator. For example on UNIX this is     * NL, while on Windows it is two characters, CR NL, where CR is the     * carriage-return (decimal 13).     *       * @param use_sytem_line_break True if an input NL is replaced with the      * operating systems end-of-line separator.     * @return The previously set value of the serializer.     */    public boolean setLineSepUse(boolean use_sytem_line_break)    {        boolean oldValue = m_lineSepUse;        m_lineSepUse = use_sytem_line_break;        return oldValue;    }    /**     * Specifies an output stream to which the document should be     * serialized. This method should not be called while the     * serializer is in the process of serializing a document.     * <p>     * The encoding specified in the output properties is used, or     * if no encoding was specified, the default for the selected     * output method.     *     * @param output The output stream     */    public void setOutputStream(OutputStream output)    {        try        {            Properties format;            if (null == m_format)                format =                    OutputPropertiesFactory.getDefaultMethodProperties(                        Method.XML);            else                format = m_format;            init(output, format, true);        }        catch (UnsupportedEncodingException uee)        {            // Should have been warned in init, I guess...        }    }    /**     * @see SerializationHandler#setEscaping(boolean)     */    public boolean setEscaping(boolean escape)    {        final boolean temp = m_escaping;        m_escaping = escape;        return temp;    }    /**     * Might print a newline character and the indentation amount     * of the given depth.     *      * @param depth the indentation depth (element nesting depth)     *     * @throws org.xml.sax.SAXException if an error occurs during writing.     */    protected void indent(int depth) throws IOException    {        if (m_startNewLine)            outputLineSep();        /* For m_indentAmount > 0 this extra test might be slower         * but Xalan's default value is 0, so this extra test         * will run faster in that situation.         */        if (m_indentAmount > 0)            printSpace(depth * m_indentAmount);    }        /**     * Indent at the current element nesting depth.     * @throws IOException     */    protected void indent() throws IOException    {        indent(m_elemContext.m_currentElemDepth);    }    /**     * Prints <var>n</var> spaces.     * @param n         Number of spaces to print.     *     * @throws org.xml.sax.SAXException if an error occurs when writing.     */    private void printSpace(int n) throws IOException    {        final java.io.Writer writer = m_writer;        for (int i = 0; i < n; i++)        {            writer.write(' ');        }    }    /**     * Report an attribute type declaration.     *     * <p>Only the effective (first) declaration for an attribute will     * be reported.  The type will be one of the strings "CDATA",     * "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY",     * "ENTITIES", or "NOTATION", or a parenthesized token group with     * the separator "|" and all whitespace removed.</p>     *     * @param eName The name of the associated element.     * @param aName The name of the attribute.     * @param type A string representing the attribute type.     * @param valueDefault A string representing the attribute default     *        ("#IMPLIED", "#REQUIRED", or "#FIXED") or null if

⌨️ 快捷键说明

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