sebb        2003/12/11 18:48:47

  Modified:    src/protocol/http/org/apache/jmeter/protocol/http/parser
                        JTidyHTMLParser.java HtmlParserHTMLParser.java
                        RegexpHTMLParser.java HTMLParser.java
  Log:
  Simplify parser instantiation
  
  Revision  Changes    Path
  1.8       +6 -21     
jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/JTidyHTMLParser.java
  
  Index: JTidyHTMLParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/JTidyHTMLParser.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JTidyHTMLParser.java      12 Dec 2003 01:36:47 -0000      1.7
  +++ JTidyHTMLParser.java      12 Dec 2003 02:48:47 -0000      1.8
  @@ -81,18 +81,15 @@
       /** Used to store the Logger (used for debug and error messages). */
       transient private static Logger log = LoggingManager.getLoggerForClass();
   
  -     /** Stores the singleton parser to be used */
  -     private static HTMLParser myParser = new JTidyHTMLParser();
  -
  -    /**
  -     * This is a singleton class
  -     */
  -    //TODO make private 
  -    JTidyHTMLParser()
  +    protected JTidyHTMLParser()
       {
           super();
       }
   
  +     protected boolean isReusable()
  +     {
  +             return true;
  +     }
   
       /* (non-Javadoc)
        * @see 
org.apache.jmeter.protocol.http.parser.HTMLParser#getEmbeddedResourceURLs(byte[], 
java.net.URL)
  @@ -287,16 +284,4 @@
           log.debug("End   : getDOM");
           return node;
       }
  -    
  -    /* (non-Javadoc)
  -     * @see org.apache.jmeter.protocol.http.parser.HTMLParser#getParserInstance()
  -     */
  -    public static HTMLParser getParserInstance()
  -    {
  -             return myParser;
  -    }
  -    
  -     public static boolean isParserReusable(){
  -             return true;
  -     }
   }
  
  
  
  1.8       +8 -19     
jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/HtmlParserHTMLParser.java
  
  Index: HtmlParserHTMLParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/HtmlParserHTMLParser.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- HtmlParserHTMLParser.java 12 Dec 2003 01:36:47 -0000      1.7
  +++ HtmlParserHTMLParser.java 12 Dec 2003 02:48:47 -0000      1.8
  @@ -80,14 +80,15 @@
       /** Used to store the Logger (used for debug and error messages). */
       transient private static Logger log= LoggingManager.getLoggerForClass();
   
  -    /** Stores the singleton parser to be used */
  -    private static HTMLParser myParser = new HtmlParserHTMLParser();
  -    
  -    // Not intended to be instantiated externally
  -     //TODO make private? 
  -     HtmlParserHTMLParser(){
  +     protected HtmlParserHTMLParser(){
                super();
        }
  +
  +     protected boolean isReusable()
  +     {
  +             return true;
  +     }
  +
       /* (non-Javadoc)
        * @see 
org.apache.jmeter.protocol.http.parser.HtmlParser#getEmbeddedResourceURLs(byte[], 
java.net.URL)
        */
  @@ -236,16 +237,4 @@
           // add applet tag scanner
           parser.addScanner(new AppletScanner());
       }
  -
  -    /* (non-Javadoc)
  -     * @see org.apache.jmeter.protocol.http.parser.HTMLParser#getParserInstance()
  -     */
  -    public static HTMLParser getParserInstance()
  -    {
  -        return myParser;
  -    }
  -
  -     public static boolean isParserReusable(){
  -             return true;
  -     }
   }
  
  
  
  1.10      +7 -18     
jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/RegexpHTMLParser.java
  
  Index: RegexpHTMLParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/RegexpHTMLParser.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- RegexpHTMLParser.java     12 Dec 2003 01:35:23 -0000      1.9
  +++ RegexpHTMLParser.java     12 Dec 2003 02:48:47 -0000      1.10
  @@ -113,8 +113,6 @@
    */
   class RegexpHTMLParser extends HTMLParser
   {
  -    /** Stores the singleton parser to be used */
  -    private static HTMLParser myParser = new RegexpHTMLParser();
   
       /**
        * Regexp fragment matching a tag attribute's value (including
  @@ -181,11 +179,15 @@
       /** Used to store the Logger (used for debug and error messages). */
       transient private static Logger log;
   
  +     protected boolean isReusable()
  +     {
  +             return true;
  +     }
  +
       /**
        * Make sure to compile the regular expression upon instantiation:
        */
  -    //TODO make private? 
  -    RegexpHTMLParser() {
  +    protected RegexpHTMLParser() {
           super();
   
           // Define this here to ensure it's ready to report any trouble
  @@ -292,17 +294,4 @@
           }
           return urls.iterator();
       }
  -    
  -    /* (non-Javadoc)
  -     * @see org.apache.jmeter.protocol.http.parser.HTMLParser#getParserInstance()
  -     */
  -    public static HTMLParser getParserInstance()
  -    {
  -        return myParser;
  -    }
  -
  -    public static boolean isParserReusable(){
  -        return true;
  -    }
  -
   }
  
  
  
  1.10      +31 -123   
jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java
  
  Index: HTMLParser.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/parser/HTMLParser.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- HTMLParser.java   12 Dec 2003 01:45:17 -0000      1.9
  +++ HTMLParser.java   12 Dec 2003 02:48:47 -0000      1.10
  @@ -61,8 +61,6 @@
   import java.io.File;
   import java.io.FileInputStream;
   import java.io.FileReader;
  -import java.lang.reflect.InvocationTargetException;
  -import java.lang.reflect.Method;
   import java.net.URL;
   import java.util.ArrayList;
   import java.util.Collection;
  @@ -89,16 +87,7 @@
       /** Used to store the Logger (used for debug and error messages). */
        transient private static Logger log = LoggingManager.getLoggerForClass();
   
  -     private static final String PARSER_METHOD = "getParserInstance";
  -     private static final String PARSER_REUSABLE = "isParserReusable";
  -
  -    /*
  -     * Cache of methods.
  -     * These need to be invoked each time, in case the parser cannot be re-used 
  -     */
  -    private static Hashtable methods = new Hashtable(3);
  -
  -    // Cache of parsers - used if parsers are re-usable
  +    // Cache of parsers - parsers must be re-usable
        private static Hashtable parsers = new Hashtable(3);
       
        private final static String PARSER_CLASSNAME = "htmlParser.className";
  @@ -113,33 +102,6 @@
       protected HTMLParser() {
       }
       
  -//    /**
  -//     * Create the single instance.
  -//     */
  -//    private static void initialize()
  -//    {
  -//        String htmlParserClassName=
  -//            JMeterUtils.getPropDefault(PARSER_CLASSNAME,DEFAULT_PARSER);
  -//
  -//        try
  -//        {
  -//            parser=
  -//                (HTMLParser)Class.forName(htmlParserClassName).newInstance();
  -//        }
  -//        catch (InstantiationException e)
  -//        {
  -//            throw new Error(e);
  -//        }
  -//        catch (IllegalAccessException e)
  -//        {
  -//            throw new Error(e);
  -//        }
  -//        catch (ClassNotFoundException e)
  -//        {
  -//            throw new Error(e);
  -//        }
  -//        log.info("Using "+htmlParserClassName);
  -//    }
   
       public static final HTMLParser getParser(){
           return 
getParser(JMeterUtils.getPropDefault(PARSER_CLASSNAME,DEFAULT_PARSER));
  @@ -154,83 +116,32 @@
                        return pars;
                }
   
  -        // Is there a cached method for creating a parser?
  -             Method meth =(Method) methods.get(htmlParserClassName);
  -             if (meth != null) {
  -                     try
  -            {
  -                pars = (HTMLParser) meth.invoke(null,null);
  -            }
  -                     catch (NullPointerException e) // method did not return 
anything
  -                     {
  -                             throw new Error(PARSER_METHOD+"() returned null",e);
  -                     }
  -            catch (IllegalArgumentException e)
  -            {
  -                             throw new Error("Should not happen",e);
  -            }
  -            catch (IllegalAccessException e)
  -            {
  -                             throw new Error("Should not happen",e);
  -            }
  -            catch (InvocationTargetException e)
  -            {
  -                             throw new Error("Should not happen",e);
  -            };
  -                     log.info("Refetched "+htmlParserClassName);
  -                     return pars;
  -             }
  -             
  -             // Create the method cache, and the parser cache if possible
                try
  -             {
  -                     Class clazz = Class.forName(htmlParserClassName);
  -                     meth = clazz.getMethod(PARSER_METHOD,null);
  -                     methods.put(htmlParserClassName,meth);// Cache the method
  -                     pars= (HTMLParser) meth.invoke(null,null);
  -                     boolean reusable=false;
  -                     try{
  -                             reusable=((Boolean)
  -                                        clazz.getMethod(PARSER_REUSABLE,null)
  -                                       .invoke(null,null))
  -                                       .booleanValue();
  -                             if (reusable){
  -                                     parsers.put(htmlParserClassName,pars);// cache 
the parser
  -                             }
  -                     }
  -                     catch (Exception e){
  -                             reusable=false;
  -                     }
  -                     log.info("Created "+htmlParserClassName+(reusable? " - 
reusable":""));
  -             }
  -             catch (NullPointerException e) // method did not return anything
  -             {
  -                     throw new Error(PARSER_METHOD+"() returned null",e);
  -             }
  -             catch (IllegalAccessException e)
  -             {
  -                     throw new Error(e);
  -             }
  -             catch (ClassNotFoundException e)
  -             {
  -                     throw new Error(e);
  -             }
  -             catch (SecurityException e)
           {
  -                     throw new Error(e);
  +             Object clazz = Class.forName(htmlParserClassName).newInstance();
  +             if (clazz instanceof HTMLParser){
  +                             pars = (HTMLParser) clazz;
  +             } else {
  +                     throw new Error(new ClassCastException(htmlParserClassName));
  +             }
           }
  -        catch (NoSuchMethodException e)
  +        catch (InstantiationException e)
           {
                        throw new Error(e);
           }
  -        catch (IllegalArgumentException e)
  +        catch (IllegalAccessException e)
           {
                        throw new Error(e);
           }
  -        catch (InvocationTargetException e)
  +        catch (ClassNotFoundException e)
           {
                        throw new Error(e);
           }
  +             log.info("Created "+htmlParserClassName);
  +             if (pars.isReusable()){
  +                     parsers.put(htmlParserClassName,pars);// cache the parser
  +             }
  +             
        return pars;
       }
   
  @@ -282,6 +193,16 @@
                throws HTMLParseException;
   
   
  +    /**
  +     * Parsers should over-ride this method if the parser class is re-usable,
  +     * in which case the class will be cached for the next getParser() call.
  +     * 
  +     * @return true if the Parser is reusable
  +     */
  +    protected boolean isReusable()
  +    {
  +     return false;
  +    }
   
   //////////////////////////// TEST CODE FOLLOWS /////////////////////////////
   
  @@ -307,7 +228,7 @@
                }
   
   
  -             private static class TestClass //Can't instantiate
  +             private class TestClass //Can't instantiate
                {
            private TestClass(){};       
                }
  @@ -427,14 +348,8 @@
                        }
                        catch (Error e)
                        {
  -                             if (e.getCause() instanceof NoSuchMethodException)
  -                             {
  -                                      //     This is OK
  -                             }
  -                             else
  -                             {
  -                                     throw e;
  -                             }
  +                             if (e.getCause() instanceof ClassCastException) return;
  +                             throw e;
                        }
                }
   
  @@ -445,14 +360,8 @@
                        }
                        catch (Error e)
                        {
  -                             if (e.getCause() instanceof NoSuchMethodException)
  -                             {
  -                                      //     This is OK
  -                             }
  -                             else
  -                             {
  -                                     throw e;
  -                             }
  +                             if (e.getCause() instanceof InstantiationException) 
return;
  +                             throw e;
                        }
                }
   
  @@ -476,7 +385,6 @@
                throws Exception
                {
                        log.info("file   "+file);
  -                     log.info("parser "+p.getClass().getName());
                        File f= new File(file);
                        byte[] buffer= new byte[(int)f.length()];
                        int len= new FileInputStream(f).read(buffer);
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to