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/23 00:14:54 UTC

cvs commit: xml-xerces/java/samples/sax SAX2Count.java SAXWriter.java

jeffreyr    00/06/22 15:14:53

  Modified:    java/samples/sax SAX2Count.java SAXWriter.java
  Log:
  stdopt changes
  
  Revision  Changes    Path
  1.2       +80 -51    xml-xerces/java/samples/sax/SAX2Count.java
  
  Index: SAX2Count.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/samples/sax/SAX2Count.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SAX2Count.java	2000/02/14 21:28:24	1.1
  +++ SAX2Count.java	2000/06/22 22:14:53	1.2
  @@ -57,6 +57,7 @@
   
   package sax;                    
                       
  +import  util.Arguments;
   import java.io.OutputStreamWriter;
   import java.io.PrintWriter;
   import java.io.UnsupportedEncodingException;
  @@ -72,7 +73,7 @@
    * register a SAX2 ContentHandler and receive the callbacks in
    * order to print information about the document.
    *
  - * @version $Id: SAX2Count.java,v 1.1 2000/02/14 21:28:24 andyc Exp $
  + * @version $Id: SAX2Count.java,v 1.2 2000/06/22 22:14:53 jeffreyr Exp $
    */
   public class SAX2Count 
       extends DefaultHandler {
  @@ -85,6 +86,13 @@
       private static final String 
           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
       //
  @@ -116,9 +124,24 @@
               XMLReader parser = (XMLReader)Class.forName(parserName).newInstance();
               parser.setContentHandler(counter);
               parser.setErrorHandler(counter);
  -            if (validate)
  -                parser.setFeature("http://xml.org/sax/features/validation", true);
   
  +
  +
  +            //if (validate)
  +             //   parser.setFeature("http://xml.org/sax/features/validation", true);
  +
  +            if ( parser instanceof XMLReader ){
  +                ((XMLReader)parser).setFeature( "http://xml.org/sax/features/validation", 
  +                                                setValidation);
  +                ((XMLReader)parser).setFeature( "http://xml.org/sax/features/namespaces",
  +                                                setNameSpaces );
  +                ((XMLReader)parser).setFeature( "http://apache.org/xml/features/validation/schema",
  +                                                setSchemaSupport );
  +
  +            }
  +
  +
  +
               if (warmup) {
                   parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
                   parser.parse(uri);
  @@ -279,65 +302,71 @@
       /** Main program entry point. */
       public static void main(String argv[]) {
   
  +        Arguments argopt = new Arguments();
  +        argopt.setUsage( new String[] 
  +                         { "usage: java sax.SAX2Count (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;
  +
  +        argopt.parseArgumentTokens(argv);
   
  -        // 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;
  -                }
  -
  -                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);
           }
  -
  +        ///
       } // main(String[])
  -
  -    /** Prints the usage. */
  -    private static void printUsage() {
  -
  -        System.err.println("usage: java sax.SAX2Count (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 SAX2Count
  
  
  
  1.2       +93 -54    xml-xerces/java/samples/sax/SAXWriter.java
  
  Index: SAXWriter.java
  ===================================================================
  RCS file: /home/cvs/xml-xerces/java/samples/sax/SAXWriter.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SAXWriter.java	1999/11/09 01:13:47	1.1
  +++ SAXWriter.java	2000/06/22 22:14:53	1.2
  @@ -56,7 +56,8 @@
    */
   
   package sax;                    
  -                    
  +
  +import util.Arguments;
   import java.io.OutputStreamWriter;
   import java.io.PrintWriter;
   import java.io.UnsupportedEncodingException;
  @@ -69,7 +70,10 @@
   import org.xml.sax.SAXException;
   import org.xml.sax.SAXParseException;
   import org.xml.sax.helpers.ParserFactory;
  +import org.xml.sax.XMLReader;
  +
   
  +
   /**
    * A sample SAX writer. This sample program illustrates how to
    * register a SAX DocumentHandler and receive the callbacks in
  @@ -78,7 +82,7 @@
    * @version
    */
   public class SAXWriter 
  -    extends HandlerBase {
  +extends HandlerBase {
   
       //
       // Constants
  @@ -86,7 +90,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
  @@ -129,11 +140,25 @@
               HandlerBase handler = new SAXWriter(canonical);
   
               Parser parser = ParserFactory.makeParser(parserName);
  +
  +
  +            if ( parser instanceof XMLReader ){
  +                ((XMLReader)parser).setFeature( "http://xml.org/sax/features/validation", 
  +                                                setValidation);
  +                ((XMLReader)parser).setFeature( "http://xml.org/sax/features/namespaces",
  +                                                setNameSpaces );
  +                ((XMLReader)parser).setFeature( "http://apache.org/xml/features/validation/schema",
  +                                                setSchemaSupport );
  +
  +            }
  +
  +
  +
  +          
               parser.setDocumentHandler(handler);
               parser.setErrorHandler(handler);
               parser.parse(uri);
  -        }
  -        catch (Exception e) {
  +        } catch (Exception e) {
               e.printStackTrace(System.err);
           }
   
  @@ -248,7 +273,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);
           }
  @@ -273,24 +298,24 @@
           for (int i = 0; i < len; i++) {
               char ch = s.charAt(i);
               switch (ch) {
  -                case '<': {
  +            case '<': {
                       str.append("&lt;");
                       break;
                   }
  -                case '>': {
  +            case '>': {
                       str.append("&gt;");
                       break;
                   }
  -                case '&': {
  +            case '&': {
                       str.append("&amp;");
                       break;
                   }
  -                case '"': {
  +            case '"': {
                       str.append("&quot;");
                       break;
                   }
  -                case '\r':
  -                case '\n': {
  +            case '\r':
  +            case '\n': {
                       if (canonical) {
                           str.append("&#");
                           str.append(Integer.toString(ch));
  @@ -299,7 +324,7 @@
                       }
                       // else, default append char
                   }
  -                default: {
  +            default: {
                       str.append(ch);
                   }
               }
  @@ -338,62 +363,76 @@
   
       /** Main program entry point. */
       public static void main(String argv[]) {
  +        ///
  +        Arguments argopt = new Arguments();
  +        argopt.setUsage( new String[] 
  +                         { "usage: java sax.SAXWriter (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 canonical  = 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("-c")) {
  -                    canonical = true;
  -                    continue;
  -                }
  +        argopt.parseArgumentTokens(argv);
   
  -                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;
  +            default:
  +                break;
               }
  -
  -            // print uri
  -            System.err.println(arg+':');
  -            print(parserName, arg, canonical);
  -            System.out.println();
           }
   
  -    } // main(String[])
  +        // print 
  +        for ( int j = 0; j<argopt.stringParameterLeft(); j++){
  +            print(parserName, argopt.getStringParameter(), setValidation);
  +        }
   
  -    /** Prints the usage. */
  -    private static void printUsage() {
   
  -        System.err.println("usage: java sax.SAXWriter (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("  -c       Canonical XML output.");
  -        System.err.println("  -h       This help screen.");
  +        ///
   
  -    } // printUsage()
  +    } // main(String[])
   
   } // class SAXWriter