parser2.java

来自「java jdk 1.4的源码」· Java 代码 · 共 1,790 行 · 第 1/5 页

JAVA
1,790
字号
    /**     * In support of the HTML DOM model of client side     * <em>&lt;xhtml:script&gt;</em> tag processing, this method permits     * data to be spliced into the input stream.  This method would     * normally be called from an <em>endElement</em> callback to put the     * buffered result of calls such as DOM <em>HTMLDocument.write</em>     * into the input stream.     */    public void pushInputBuffer (char buf [], int offset, int len)    throws SAXException    {        if (len <= 0)            return;        // arraycopy is inelegant, but that's the worst penalty for now        if (offset != 0 || len != buf.length) {            char tmp [] = new char [len];            System.arraycopy (buf, offset, tmp, 0, len);            buf = tmp;        }        pushReader (buf, null, false);    }    // package private    void setIsValidating (boolean value)    {        if (supportValidation)            isValidating = value;        else            throw new RuntimeException (messages.getMessage (locale, "V-000"));        if (value)            fastStandalone = false;    }    // makes sure the parser's reset to "before a document"    private void init ()    {        in = null;        // alloc temporary data used in parsing        attTmp = new AttributesExImpl ();        strTmp = new StringBuffer ();        nameTmp = new char [20];        nameCache = new NameCache ();        if (namespaces) {            nsSupport = new NamespaceSupport();            if (supportValidation && isValidating && !prefixes) {                nsAttTmp = new Vector();            }        }        // reset doc info        isStandalone = false;        rootElementName = null;        isInAttribute = false;        inExternalPE = false;        doLexicalPE = false;        donePrologue = false;        entities.clear ();        notations.clear ();        params.clear ();        elements.clear ();        ignoreDeclarations = false;        // initialize predefined references ... re-interpreted later        builtin ("amp", "&#38;");        builtin ("lt", "&#60;");        builtin ("gt", ">");        builtin ("quot", "\"");        builtin ("apos", "'");        if (locale == null)            locale = Locale.getDefault ();        if (resolver == null)            resolver = new Resolver ();                setHandlers ();                //only for SECURITY_DEBUG        if(SECURITY_DEBUG)System.out.println(" Last Entity expansion count = " + entityExpansionCount );                //reset it to zero before next parse        entityExpansionCount = 0;                //if there is no limit set by the application.. set the default entity expansion limit to DEFAULT_ENTITY_EXPANSION_LIMIT         if(entityExpansionLimit < 0){            entityExpansionLimit = DEFAULT_ENTITY_EXPANSION_LIMIT ;        }        if(SECURITY_DEBUG){             System.out.println(" Entity expansion limit in effect = " + entityExpansionLimit );            System.out.println(" DisallowDoctypeDecl in effect = " + disallowDoctypeDecl );        }            }//init()    void setSecurityConstraintValues(){                //SYSTEM PROPERTY ENTITY EXPANSION LIMIT                //get the value of  entityExpansionLimit from SYSTEM PROPERTY        //put this code in doPriviliged block so it can still be executed if the caller have less privileges                try {            propertyEntityExpansionLimit = (String)AccessController.doPrivileged(new PrivilegedAction(){                public Object run(){                    return System.getProperty(SYSTEM_PROPERTY_ENTITY_EXPANSION_LIMIT);                }            });        } catch ( SecurityException se ) {            //This exception can happen in case we are running as an applet        }        //SYSTEM PROPERTY DISALLOW DOCTYPE DECL        //get the value of disallowDoctypeDecl from system property         //put this code in doPriviliged block so it can still be executed if the caller have less privileges                try {            propertyDisallowDoctypeDecl = (String) AccessController.doPrivileged( new PrivilegedAction(){                public Object run(){                    return System.getProperty(SYSTEM_PROPERTY_DISALLOW_DOCTYPE_DECL);                }            });        } catch ( SecurityException se ) {            //This exception can happen in case we are running as an applet        }        if(SECURITY_DEBUG){            System.out.println(" ENTITY_EXPANSION_LIMIT SET FROM SYSTEM PROPERTY = " + propertyEntityExpansionLimit );            System.out.println(" DISALLOW_DOCTYPE_DECL SET FROM SYSTEM PROPERTY = " + propertyDisallowDoctypeDecl );        }                //if either of the value is not set.. try to get the value from jaxp.properties file..         if( propertyEntityExpansionLimit == null || propertyDisallowDoctypeDecl == null ){                        //for performance reasons don't read jaxp.properties file again and again..                         // try to read from $java.home/lib/jaxp.properties            try {                //put this code in doPriviliged block so it can still be executed if the caller have less privileges                FileInputStream fis = (FileInputStream) AccessController.doPrivileged(new PrivilegedExceptionAction() {                    public Object run() throws FileNotFoundException {                        String javah = System.getProperty( "java.home" );                        String configFile = javah + File.separator +                        "lib" + File.separator + "jaxp.properties";                        File f = new File( configFile );                                                 if(f.exists()){                            return new FileInputStream(f);                        }                        else{                            return null ;                        }                    }                });                if(fis != null){                    Properties props = new Properties();                    props.load( fis );                    //we dont know which one was null..                    if(propertyEntityExpansionLimit == null){                        propertyEntityExpansionLimit = props.getProperty(SYSTEM_PROPERTY_ENTITY_EXPANSION_LIMIT);                        if(SECURITY_DEBUG)System.out.println("Value from jaxp.properites file, propertyEntityExpansionLimit = " + propertyEntityExpansionLimit );                    }                    //we dont know which one was null..                    if(propertyDisallowDoctypeDecl == null){                        propertyDisallowDoctypeDecl = props.getProperty(SYSTEM_PROPERTY_DISALLOW_DOCTYPE_DECL);                        if(SECURITY_DEBUG)System.out.println("Value from jaxp.properites file, propertyDisallowDoctypeDecl = " + propertyDisallowDoctypeDecl );                                            }                }                            } catch(Exception ex ) {                //ignore the exception            }        }                               //get the value of entityExpansionLimit        try{            if(propertyEntityExpansionLimit != null){                entityExpansionLimit = Integer.parseInt(propertyEntityExpansionLimit);            }        }catch(NumberFormatException nfe){            //ignore the exception.. or                     }                //get the value of disallowDoctypeDecl        if(propertyDisallowDoctypeDecl != null && (propertyDisallowDoctypeDecl.equals("true") || propertyDisallowDoctypeDecl.equals("TRUE"))){            disallowDoctypeDecl = true;        }                        }//getSecurityConstraintValues()        static private final NullHandler nullHandler = new NullHandler();    private void setHandlers ()    {        if (contentHandler == null) {            contentHandler = nullHandler;        }        if (errHandler == null) {            errHandler = nullHandler;        }        if (dtdHandler == null) {            dtdHandler = nullHandler;        }        if (lexicalHandler == null) {            lexicalHandler = nullHandler;        }        if (declHandler == null) {            declHandler = nullHandler;        }    }    private void builtin (String entityName, String entityValue)    {        InternalEntity entity;        entity = new InternalEntity (entityName, entityValue.toCharArray ());        entities.put (entityName, entity);    }    ////////////////////////////////////////////////////////////////    //    // parsing is by recursive descent, code roughly    // following the BNF rules except tweaked for simple    // lookahead.  rules are more or less in numeric order,    // except where code sharing suggests other structures.    //    // a classic benefit of recursive descent parsers:  it's    // relatively easy to get diagnostics that make sense.    //    ////////////////////////////////////////////////////////////////    //    // CHAPTER 2:  Documents    //    private void parseInternal (InputSource input)    throws SAXException, IOException    {        if (input == null)            fatal ("P-000");        try {            in = InputEntity.getInputEntity (errHandler, locale);            in.init (input, null, null, false);            //            // doc handler sees the locator, lots of PIs, DTD info            // about external entities and notations, then the body.            //Need to initialize this after InputEntity cos locator uses            //InputEntity's systemid, publicid, line no. etc            contentHandler.setDocumentLocator (locator);            contentHandler.startDocument ();            // [1] document ::= prolog element Misc*            // [22] prolog ::= XMLDecl? Misc* (DoctypeDecl Misc *)?            maybeXmlDecl ();            maybeMisc (false);            if (!maybeDoctypeDecl ()) {                if (supportValidation && isValidating)                    warning ("V-001", null);            }                        maybeMisc (false);            donePrologue = true;            //            // One root element ... then basically PIs before EOF.            //            if (!in.peekc ('<') || !maybeElement (null))                fatal ("P-067");            //Check subclass. Used for validation of id refs.            afterRoot ();            maybeMisc (true);            if (!in.isEOF ())                fatal ("P-001", new Object []                        { Integer.toHexString (((int)getc ())) } );            contentHandler.endDocument ();        } catch (EndOfInputException e) {            if (!in.isDocument ()) {                String name = in.getName ();                do {    // force a relevant URI and line number                      in = in.pop ();                } while (in.isInternal ());                fatal ("P-002", new Object []                        { name },                        e);            } else                fatal ("P-003", null, e);        } catch (RuntimeException e) {            // Don't discard location that triggered the exception            throw new SAXParseException (                e.getMessage () != null                    ? e.getMessage ()                    : e.getClass ().getName (),                locator.getPublicId (), locator.getSystemId (),                locator.getLineNumber (), locator.getColumnNumber (),                e);        } finally {            // recycle temporary data used during parsing            strTmp = null;            attTmp = null;            nameTmp = null;            nameCache = null;            nsAttTmp = null;            // ditto input sources etc            if (in != null) {                in.close ();                in = null;            }            // get rid of all DTD info ... some of it would be            // useful for editors etc, investigate later.            params.clear ();            entities.clear ();            notations.clear ();            elements.clear ();            afterDocument ();        }    }    // package private -- for subclass     void afterRoot () throws SAXException { }    // package private -- for subclass     void afterDocument () { }    // role is for diagnostics    private void whitespace (String roleId) throws IOException, SAXException        // [3] S ::= (#x20 | #x9 | #xd | #xa)+    {        if (!maybeWhitespace ())            fatal ("P-004", new Object []                    { messages.getMessage (locale, roleId) });    }        // S?    private boolean maybeWhitespace () throws IOException, SAXException    {        if (!(inExternalPE && doLexicalPE))            return in.maybeWhitespace ();        // see getc() for the PE logic -- this lets us splice        // expansions of PEs in "anywhere".  getc() has smarts,        // so for external PEs we don't bypass it.

⌨️ 快捷键说明

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