You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by je...@locus.apache.org on 2000/06/22 18:29:15 UTC

cvs commit: xml-xerces/java/samples/sax SAXCount.java

jeffreyr    00/06/22 09:29:14

  Modified:    java/samples/sax SAXCount.java
  Log:
  added argopt like class, added switch to turn on/off validation, namespace, schema support, etc.
  
  Revision  Changes    Path
  1.3       +86 -58    xml-xerces/java/samples/sax/SAXCount.java
  
  Index: SAXCount.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/samples/sax/SAXCount.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SAXCount.java	2000/02/07 21:40:09	1.2
  +++ SAXCount.java	2000/06/22 16:29:13	1.3
  @@ -56,7 +56,9 @@
    */
   
   package sax;                    
  -                    
  +
  +import dom.Features;
  +import dom.Arguments;
   import java.io.OutputStreamWriter;
   import java.io.PrintWriter;
   import java.io.UnsupportedEncodingException;
  @@ -79,7 +81,7 @@
    * @version
    */
   public class SAXCount 
  -    extends HandlerBase {
  +extends HandlerBase {
   
       //
       // Constants
  @@ -87,8 +89,14 @@
   
       /** Default parser name. */
       private static final String 
  -        DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
  +    DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
  +
  +
  +    private static boolean setValidation    = false; //defaults
  +    private static boolean setNameSpaces    = true;
  +    private static boolean setSchemaSupport = true;
   
  +
       //
       // Data
       //
  @@ -114,6 +122,8 @@
       /** Prints the output from the SAX callbacks. */
       public static void print(String parserName, String uri, boolean validate) {
   
  +        Features feature = new Features( setValidation, setNameSpaces, 
  +                                         setSchemaSupport, false );
           try {
               SAXCount counter = new SAXCount();
   
  @@ -121,13 +131,23 @@
               parser.setDocumentHandler(counter);
               parser.setErrorHandler(counter);
               try {
  -                if (validate && parser instanceof XMLReader)
  -                    ((XMLReader)parser).setFeature("http://xml.org/sax/features/validation", true);
  -            } catch (Exception ex) {}
  +                //if (validate && parser instanceof XMLReader)
  +                if ( parser instanceof XMLReader ){
  +                    ((XMLReader)parser).setFeature( "http://xml.org/sax/features/validation", 
  +                                                    feature.isValidationSet());
  +                    ((XMLReader)parser).setFeature( "http://xml.org/sax/features/namespaces",
  +                                                    feature.isNamespaceSet() );
  +                    ((XMLReader)parser).setFeature( "http://apache.org/xml/features/validation/schema",
  +                                                    feature.isSchemasupportSet() );
   
  +                }
  +            } catch (Exception ex) {
  +            }
  +
               if (warmup) {
                   if (parser instanceof XMLReader)
                       ((XMLReader)parser).setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
  +
                   parser.parse(uri);
                   warmup = false;
               }
  @@ -135,17 +155,14 @@
               parser.parse(uri);
               long after = System.currentTimeMillis();
               counter.printResults(uri, after - before);
  -        }
  -        catch (org.xml.sax.SAXParseException spe) {
  +        } catch (org.xml.sax.SAXParseException spe) {
               spe.printStackTrace(System.err);
  -        }
  -        catch (org.xml.sax.SAXException se) {
  +        } catch (org.xml.sax.SAXException se) {
               if (se.getException() != null)
                   se.getException().printStackTrace(System.err);
               else
                   se.printStackTrace(System.err);
  -        }
  -        catch (Exception e) {
  +        } catch (Exception e) {
               e.printStackTrace(System.err);
           }
   
  @@ -243,7 +260,7 @@
           String systemId = ex.getSystemId();
           if (systemId != null) {
               int index = systemId.lastIndexOf('/');
  -            if (index != -1) 
  +            if (index != -1)
                   systemId = systemId.substring(index + 1);
               str.append(systemId);
           }
  @@ -286,65 +303,76 @@
       /** Main program entry point. */
       public static void main(String argv[]) {
   
  +        Arguments argopt = new Arguments();
  +        argopt.setUsage( new String[] 
  +                         { "usage: java sax.SAXCount (options) uri ...","",
  +                             "options:",
  +                             "  -p name  Specify SAX parser by name.",
  +                             "           Default parser: "+DEFAULT_PARSER_NAME,
  +                             "  -v       Turn on validation.",
  +                             "  -w       Warmup the parser before timing.",
  +                             "  -n turn on  Namespace  - default",
  +                             "  -s turn on  Schema support - default",
  +                             "  -N turn off Namespace",
  +                             "  -V turn off Validation",
  +                             "  -h       This help screen."}  );
  +
  +
           // is there anything to do?
           if (argv.length == 0) {
  -            printUsage();
  +            argopt.printUsage();
               System.exit(1);
           }
   
           // vars
           String  parserName = DEFAULT_PARSER_NAME;
  -        boolean validate = false;
  -
  -        // check parameters
  -        for (int i = 0; i < argv.length; i++) {
  -            String arg = argv[i];
  -
  -            // options
  -            if (arg.startsWith("-")) {
  -                if (arg.equals("-p")) {
  -                    if (i == argv.length - 1) {
  -                        System.err.println("error: missing parser name");
  -                        System.exit(1);
  -                    }
  -                    parserName = argv[++i];
  -                    continue;
  -                }
   
  -                if (arg.equals("-w")) {
  -                    warmup = true;
  -                    continue;
  -                }
  +        argopt.parseArgumentTokens(argv);
   
  -                if (arg.equals("-v")) {
  -                    validate = true;
  -                    continue;
  -                }
  -
  -                if (arg.equals("-h")) {
  -                    printUsage();
  -                    System.exit(1);
  -                }
  +        int   c;
  +        while ( (c =  argopt.getArguments()) != -1 ){
  +            switch (c) {
  +            case 'v':
  +                setValidation = true;
  +                break;
  +            case 'V':
  +                setValidation = false;
  +                break;
  +            case 'N':
  +                setNameSpaces = false;
  +                break;
  +            case 'n':
  +                setNameSpaces = true;
  +                break;
  +            case 'p':
  +                parserName = argopt.getStringParameter();
  +                break;
  +            case 's':
  +                setSchemaSupport = true;
  +                break;
  +            case 'S':
  +                setSchemaSupport = false;
  +                break;
  +            case '?':
  +            case 'h':
  +            case '-':
  +                argopt.printUsage();
  +                System.exit(1);
  +                break;
  +            case 'w':
  +                warmup = true;
  +                break;
  +            default:
  +                break;
               }
  +        }
   
  -            // print uri
  -            print(parserName, arg, validate);
  +        // print 
  +        for ( int j = 0; j<argopt.stringParameterLeft(); j++){
  +            print(parserName, argopt.getStringParameter(), setValidation);
           }
   
       } // main(String[])
   
  -    /** Prints the usage. */
  -    private static void printUsage() {
  -
  -        System.err.println("usage: java sax.SAXCount (options) uri ...");
  -        System.err.println();
  -        System.err.println("options:");
  -        System.err.println("  -p name  Specify SAX parser by name.");
  -        System.err.println("           Default parser: "+DEFAULT_PARSER_NAME);
  -        System.err.println("  -v       Turn on validation.");
  -        System.err.println("  -w       Warmup the parser before timing.");
  -        System.err.println("  -h       This help screen.");
  -
  -    } // printUsage()
   
   } // class SAXCount