toxmlstream.java

来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 627 行 · 第 1/2 页

JAVA
627
字号
                    writer.write(m_lineSep, 0, m_lineSepLen);                m_startNewLine = true;            }            catch(IOException e)            {                throw new SAXException(e);            }        }                if (m_tracer != null)            super.fireEscapingEvent(target, data);      }    /**     * Receive notivication of a entityReference.     *     * @param name The name of the entity.     *     * @throws org.xml.sax.SAXException     */    public void entityReference(String name) throws org.xml.sax.SAXException    {        if (m_elemContext.m_startTagOpen)        {            closeStartTag();            m_elemContext.m_startTagOpen = false;        }        try        {            if (shouldIndent())                indent();            final java.io.Writer writer = m_writer;            writer.write('&');            writer.write(name);            writer.write(';');        }        catch(IOException e)        {            throw new SAXException(e);        }                if (m_tracer != null)            super.fireEntityReference(name);                }    /**     * This method is used to add an attribute to the currently open element.      * The caller has guaranted that this attribute is unique, which means that it     * not been seen before and will not be seen again.     *      * @param name the qualified name of the attribute     * @param value the value of the attribute which can contain only     * ASCII printable characters characters in the range 32 to 127 inclusive.     * @param flags the bit values of this integer give optimization information.     */    public void addUniqueAttribute(String name, String value, int flags)        throws SAXException    {        if (m_elemContext.m_startTagOpen)        {                       try            {                final String patchedName = patchName(name);                final java.io.Writer writer = m_writer;                if ((flags & NO_BAD_CHARS) > 0 && m_xmlcharInfo.onlyQuotAmpLtGt)                {                    // "flags" has indicated that the characters                    // '>'  '<'   '&'  and '"' are not in the value and                    // m_htmlcharInfo has recorded that there are no other                    // entities in the range 32 to 127 so we write out the                    // value directly                                        writer.write(' ');                    writer.write(patchedName);                    writer.write("=\"");                    writer.write(value);                    writer.write('"');                }                else                {                    writer.write(' ');                    writer.write(patchedName);                    writer.write("=\"");                    writeAttrString(writer, value, this.getEncoding());                    writer.write('"');                }            } catch (IOException e) {                throw new SAXException(e);            }        }    }    /**     * Add an attribute to the current element.     * @param uri the URI associated with the element name     * @param localName local part of the attribute name     * @param rawName   prefix:localName     * @param type     * @param value the value of the attribute     * @param xslAttribute true if this attribute is from an xsl:attribute,     * false if declared within the elements opening tag.     * @throws SAXException     */    public void addAttribute(        String uri,        String localName,        String rawName,        String type,        String value,        boolean xslAttribute)        throws SAXException    {        if (m_elemContext.m_startTagOpen)        {            boolean was_added = addAttributeAlways(uri, localName, rawName, type, value, xslAttribute);                        /*             * We don't run this block of code if:             * 1. The attribute value was only replaced (was_added is false).             * 2. The attribute is from an xsl:attribute element (that is handled             *    in the addAttributeAlways() call just above.             * 3. The name starts with "xmlns", i.e. it is a namespace declaration.             */            if (was_added && !xslAttribute && !rawName.startsWith("xmlns"))            {                String prefixUsed =                    ensureAttributesNamespaceIsDeclared(                        uri,                        localName,                        rawName);                if (prefixUsed != null                    && rawName != null                    && !rawName.startsWith(prefixUsed))                {                    // use a different raw name, with the prefix used in the                    // generated namespace declaration                    rawName = prefixUsed + ":" + localName;                }            }            addAttributeAlways(uri, localName, rawName, type, value, xslAttribute);        }        else        {            /*             * The startTag is closed, yet we are adding an attribute?             *             * Section: 7.1.3 Creating Attributes Adding an attribute to an             * element after a PI (for example) has been added to it is an             * error. The attributes can be ignored. The spec doesn't explicitly             * say this is disallowed, as it does for child elements, but it             * makes sense to have the same treatment.             *             * We choose to ignore the attribute which is added too late.             */            // Generate a warning of the ignored attributes            // Create the warning message            String msg = Utils.messages.createMessage(                    MsgKey.ER_ILLEGAL_ATTRIBUTE_POSITION,new Object[]{ localName });            try {                // Prepare to issue the warning message                Transformer tran = super.getTransformer();                ErrorListener errHandler = tran.getErrorListener();                // Issue the warning message                if (null != errHandler && m_sourceLocator != null)                  errHandler.warning(new TransformerException(msg, m_sourceLocator));                else                  System.out.println(msg);                }            catch (Exception e){}                     }    }    /**     * @see ExtendedContentHandler#endElement(String)     */    public void endElement(String elemName) throws SAXException    {        endElement(null, null, elemName);    }    /**     * This method is used to notify the serializer of a namespace mapping (or node)     * that applies to the current element whose startElement() call has already been seen.     * The official SAX startPrefixMapping(prefix,uri) is to define a mapping for a child     * element that is soon to be seen with a startElement() call. The official SAX call      * does not apply to the current element, hence the reason for this method.     */    public void namespaceAfterStartElement(        final String prefix,        final String uri)        throws SAXException    {        // hack for XSLTC with finding URI for default namespace        if (m_elemContext.m_elementURI == null)        {            String prefix1 = getPrefixPart(m_elemContext.m_elementName);            if (prefix1 == null && EMPTYSTRING.equals(prefix))            {                // the elements URI is not known yet, and it                // doesn't have a prefix, and we are currently                // setting the uri for prefix "", so we have                // the uri for the element... lets remember it                m_elemContext.m_elementURI = uri;            }        }                    startPrefixMapping(prefix,uri,false);        return;    }    /**     * From XSLTC     * Declare a prefix to point to a namespace URI. Inform SAX handler     * if this is a new prefix mapping.     */    protected boolean pushNamespace(String prefix, String uri)    {        try        {            if (m_prefixMap.pushNamespace(                prefix, uri, m_elemContext.m_currentElemDepth))            {                startPrefixMapping(prefix, uri);                return true;            }        }        catch (SAXException e)        {            // falls through        }        return false;    }    /**     * Try's to reset the super class and reset this class for      * re-use, so that you don't need to create a new serializer      * (mostly for performance reasons).     *      * @return true if the class was successfuly reset.     */    public boolean reset()    {        boolean wasReset = false;        if (super.reset())        {            resetToXMLStream();            wasReset = true;        }        return wasReset;    }        /**     * Reset all of the fields owned by ToStream class     *     */    private void resetToXMLStream()    {        this.m_cdataTagOpen = false;    }      /**     * This method checks for the XML version of output document.     * If XML version of output document is not specified, then output      * document is of version XML 1.0.     * If XML version of output doucment is specified, but it is not either      * XML 1.0 or XML 1.1, a warning message is generated, the XML Version of     * output document is set to XML 1.0 and processing continues.     * @return string (XML version)     */    private String getXMLVersion()    {        String xmlVersion = getVersion();        if(xmlVersion == null || xmlVersion.equals(XMLVERSION10))        {            xmlVersion = XMLVERSION10;        }        else if(xmlVersion.equals(XMLVERSION11))        {            xmlVersion = XMLVERSION11;        }        else        {            String msg = Utils.messages.createMessage(                               MsgKey.ER_XML_VERSION_NOT_SUPPORTED,new Object[]{ xmlVersion });            try             {                // Prepare to issue the warning message                Transformer tran = super.getTransformer();                ErrorListener errHandler = tran.getErrorListener();                // Issue the warning message                if (null != errHandler && m_sourceLocator != null)                    errHandler.warning(new TransformerException(msg, m_sourceLocator));                else                    System.out.println(msg);            }            catch (Exception e){}            xmlVersion = XMLVERSION10;								        }        return xmlVersion;    }}

⌨️ 快捷键说明

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