Exemple #1
0
  /** Main method to allow PrettyPrinter to be used from the command-line. */
  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      System.out.println(
          "Usage: java " + PrettyPrinter.class.getName() + " [inputFilename] [outputFilename]");
      System.exit(0);
    }

    String in_filename = args[0];
    String out_filename = args[1];

    // Get instances of our handlers
    DocumentHandler prettyPrinter =
        new PrettyPrinter(new FileOutputStream(out_filename), "iso-8859-1");
    ErrorHandler errorHandler = new OntopiaErrorHandler();

    InputSource inSource = new InputSource(new FileReader(in_filename));

    // get parser instance and connect to our handlers
    Parser parser = ParserFactory.makeParser("com.jclark.xml.sax.Driver");
    parser.setDocumentHandler(prettyPrinter);
    parser.setErrorHandler(errorHandler);

    // beautify
    parser.parse(inSource);
  }
Exemple #2
0
  public static Parser getDefaultParser() {
    Parser parser = null;
    String parserClass = System.getProperty("org.xml.sax.parser");
    try {
      if ((parserClass == null) || (parserClass.length() == 0)) parserClass = DEFAULT_PARSER_CLASS;

      parser = ParserFactory.makeParser(parserClass);
    } catch (java.lang.IllegalAccessException iae) {
    } catch (java.lang.ClassNotFoundException cnfe) {
    } catch (java.lang.InstantiationException ie) {
    }
    ;
    if (parser == null) {
      System.out.print("unable to create SAX parser: ");
      System.out.println(parserClass);
      return null;
    }

    return parser;
  } // -- getDefaultParser
  /** Main program entry point. */
  public static void main(String argv[]) {

    // is there anything to do?
    if (argv.length == 0) {
      printUsage();
      System.exit(1);
    }

    // variables
    Counter counter = new Counter();
    PrintWriter out = new PrintWriter(System.out);
    XMLReader parser = null;
    int repetition = DEFAULT_REPETITION;
    boolean namespaces = DEFAULT_NAMESPACES;
    boolean namespacePrefixes = DEFAULT_NAMESPACE_PREFIXES;
    boolean validation = DEFAULT_VALIDATION;
    boolean schemaValidation = DEFAULT_SCHEMA_VALIDATION;
    boolean schemaFullChecking = DEFAULT_SCHEMA_FULL_CHECKING;
    boolean dynamicValidation = DEFAULT_DYNAMIC_VALIDATION;
    boolean memoryUsage = DEFAULT_MEMORY_USAGE;
    boolean tagginess = DEFAULT_TAGGINESS;

    // process arguments
    for (int i = 0; i < argv.length; i++) {
      String arg = argv[i];
      if (arg.startsWith("-")) {
        String option = arg.substring(1);
        if (option.equals("p")) {
          // get parser name
          if (++i == argv.length) {
            System.err.println("error: Missing argument to -p option.");
            continue;
          }
          String parserName = argv[i];

          // create parser
          try {
            parser = XMLReaderFactory.createXMLReader(parserName);
          } catch (Exception e) {
            try {
              Parser sax1Parser = ParserFactory.makeParser(parserName);
              parser = new ParserAdapter(sax1Parser);
              System.err.println("warning: Features and properties not supported on SAX1 parsers.");
            } catch (Exception ex) {
              parser = null;
              System.err.println("error: Unable to instantiate parser (" + parserName + ")");
            }
          }
          continue;
        }
        if (option.equals("x")) {
          if (++i == argv.length) {
            System.err.println("error: Missing argument to -x option.");
            continue;
          }
          String number = argv[i];
          try {
            int value = Integer.parseInt(number);
            if (value < 1) {
              System.err.println("error: Repetition must be at least 1.");
              continue;
            }
            repetition = value;
          } catch (NumberFormatException e) {
            System.err.println("error: invalid number (" + number + ").");
          }
          continue;
        }
        if (option.equalsIgnoreCase("n")) {
          namespaces = option.equals("n");
          continue;
        }
        if (option.equalsIgnoreCase("np")) {
          namespacePrefixes = option.equals("np");
          continue;
        }
        if (option.equalsIgnoreCase("v")) {
          validation = option.equals("v");
          continue;
        }
        if (option.equalsIgnoreCase("s")) {
          schemaValidation = option.equals("s");
          continue;
        }
        if (option.equalsIgnoreCase("f")) {
          schemaFullChecking = option.equals("f");
          continue;
        }
        if (option.equalsIgnoreCase("dv")) {
          dynamicValidation = option.equals("dv");
          continue;
        }
        if (option.equalsIgnoreCase("m")) {
          memoryUsage = option.equals("m");
          continue;
        }
        if (option.equalsIgnoreCase("t")) {
          tagginess = option.equals("t");
          continue;
        }
        if (option.equals("-rem")) {
          if (++i == argv.length) {
            System.err.println("error: Missing argument to -# option.");
            continue;
          }
          System.out.print("# ");
          System.out.println(argv[i]);
          continue;
        }
        if (option.equals("h")) {
          printUsage();
          continue;
        }
        System.err.println("error: unknown option (" + option + ").");
        continue;
      }

      // use default parser?
      if (parser == null) {

        // create parser
        try {
          parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
        } catch (Exception e) {
          System.err.println("error: Unable to instantiate parser (" + DEFAULT_PARSER_NAME + ")");
          continue;
        }
      }

      // set parser features
      try {
        parser.setFeature(NAMESPACES_FEATURE_ID, namespaces);
      } catch (SAXException e) {
        System.err.println(
            "warning: Parser does not support feature (" + NAMESPACES_FEATURE_ID + ")");
      }
      try {
        parser.setFeature(NAMESPACE_PREFIXES_FEATURE_ID, namespacePrefixes);
      } catch (SAXException e) {
        System.err.println(
            "warning: Parser does not support feature (" + NAMESPACE_PREFIXES_FEATURE_ID + ")");
      }
      try {
        parser.setFeature(VALIDATION_FEATURE_ID, validation);
      } catch (SAXException e) {
        System.err.println(
            "warning: Parser does not support feature (" + VALIDATION_FEATURE_ID + ")");
      }
      try {
        parser.setFeature(SCHEMA_VALIDATION_FEATURE_ID, schemaValidation);
      } catch (SAXNotRecognizedException e) {
        System.err.println(
            "warning: Parser does not support feature (" + SCHEMA_VALIDATION_FEATURE_ID + ")");

      } catch (SAXNotSupportedException e) {
        System.err.println(
            "warning: Parser does not support feature (" + SCHEMA_VALIDATION_FEATURE_ID + ")");
      }
      try {
        parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, schemaFullChecking);
      } catch (SAXNotRecognizedException e) {
        System.err.println(
            "warning: Parser does not support feature (" + SCHEMA_FULL_CHECKING_FEATURE_ID + ")");

      } catch (SAXNotSupportedException e) {
        System.err.println(
            "warning: Parser does not support feature (" + SCHEMA_FULL_CHECKING_FEATURE_ID + ")");
      }
      try {
        parser.setFeature(DYNAMIC_VALIDATION_FEATURE_ID, dynamicValidation);
      } catch (SAXNotRecognizedException e) {
        System.err.println(
            "warning: Parser does not support feature (" + DYNAMIC_VALIDATION_FEATURE_ID + ")");

      } catch (SAXNotSupportedException e) {
        System.err.println(
            "warning: Parser does not support feature (" + DYNAMIC_VALIDATION_FEATURE_ID + ")");
      }

      // parse file
      parser.setContentHandler(counter);
      parser.setErrorHandler(counter);
      try {
        long timeBefore = System.currentTimeMillis();
        long memoryBefore = Runtime.getRuntime().freeMemory();
        for (int j = 0; j < repetition; j++) {
          parser.parse(arg);
        }
        long memoryAfter = Runtime.getRuntime().freeMemory();
        long timeAfter = System.currentTimeMillis();

        long time = timeAfter - timeBefore;
        long memory = memoryUsage ? memoryBefore - memoryAfter : Long.MIN_VALUE;
        counter.printResults(out, arg, time, memory, tagginess, repetition);
      } catch (SAXParseException e) {
        // ignore
      } catch (Exception e) {
        System.err.println("error: Parse error occurred - " + e.getMessage());
        Exception se = e;
        if (e instanceof SAXException) {
          se = ((SAXException) e).getException();
        }
        if (se != null) se.printStackTrace(System.err);
        else e.printStackTrace(System.err);
      }
    }
  } // main(String[])