Esempio n. 1
0
  /**
   * Get CatalogResolver.
   *
   * @return CatalogResolver
   */
  public static synchronized CatalogResolver getCatalogResolver() {
    if (catalogResolver == null) {
      final CatalogManager manager = new CatalogManager();
      manager.setIgnoreMissingProperties(true);
      manager.setUseStaticCatalog(false); // We'll use a private catalog.
      manager.setPreferPublic(true);

      // manager.setVerbosity(10);
      catalogResolver = new CatalogResolver(manager);

      final File catalogFilePath = new File(ditaDir, FILE_NAME_CATALOG);

      final Catalog catalog = catalogResolver.getCatalog();
      try {
        catalog.parseCatalog(catalogFilePath.toURI().toURL());
      } catch (final Exception e) {
        logger.logException(e);
      }
    }

    return catalogResolver;
  }
Esempio n. 2
0
  /** The main entry point */
  public static void main(String[] args) throws FileNotFoundException, IOException {

    String xmlfile = null;
    int debug = 0;
    int maxErrs = 10;
    boolean nsAware = true;
    boolean validating = true;
    boolean showWarnings = (debug > 2);
    boolean showErrors = true;
    Vector catalogFiles = new Vector();

    for (int i = 0; i < args.length; i++) {
      if (args[i].equals("-c")) {
        ++i;
        catalogFiles.add(args[i]);
        continue;
      }

      if (args[i].equals("-w")) {
        validating = false;
        continue;
      }

      if (args[i].equals("-v")) {
        validating = true;
        continue;
      }

      if (args[i].equals("-n")) {
        nsAware = false;
        continue;
      }

      if (args[i].equals("-N")) {
        nsAware = true;
        continue;
      }

      if (args[i].equals("-d")) {
        ++i;
        String debugstr = args[i];
        try {
          debug = Integer.parseInt(debugstr);
          if (debug >= 0) {
            Debug.setDebug(debug);
            showWarnings = (debug > 2);
          }
        } catch (Exception e) {
          // nop
        }
        continue;
      }

      if (args[i].equals("-E")) {
        ++i;
        String errstr = args[i];
        try {
          int errs = Integer.parseInt(errstr);
          if (errs >= 0) {
            maxErrs = errs;
          }
        } catch (Exception e) {
          // nop
        }
        continue;
      }

      xmlfile = args[i];
    }

    if (xmlfile == null) {
      System.out.println("Usage: org.apache.xml.resolver.apps.xread [opts] xmlfile");
      System.exit(1);
    }

    ResolvingXMLReader reader = new ResolvingXMLReader();

    try {
      reader.setFeature("http://xml.org/sax/features/namespaces", nsAware);
      reader.setFeature("http://xml.org/sax/features/validation", validating);
    } catch (SAXException e) {
      // nop;
    }

    Catalog catalog = reader.getCatalog();

    for (int count = 0; count < catalogFiles.size(); count++) {
      String file = (String) catalogFiles.elementAt(count);
      catalog.parseCatalog(file);
    }

    XParseError xpe = new XParseError(showErrors, showWarnings);
    xpe.setMaxMessages(maxErrs);
    reader.setErrorHandler(xpe);

    String parseType = validating ? "validating" : "well-formed";
    String nsType = nsAware ? "namespace-aware" : "namespace-ignorant";
    if (maxErrs > 0) {
      System.out.println("Attempting " + parseType + ", " + nsType + " parse");
    }

    Date startTime = new Date();

    try {
      reader.parse(xmlfile);
    } catch (SAXException sx) {
      System.out.println("SAX Exception: " + sx);
    } catch (Exception e) {
      e.printStackTrace();
    }

    Date endTime = new Date();

    long millisec = endTime.getTime() - startTime.getTime();
    long secs = 0;
    long mins = 0;
    long hours = 0;

    if (millisec > 1000) {
      secs = millisec / 1000;
      millisec = millisec % 1000;
    }

    if (secs > 60) {
      mins = secs / 60;
      secs = secs % 60;
    }

    if (mins > 60) {
      hours = mins / 60;
      mins = mins % 60;
    }

    if (maxErrs > 0) {
      System.out.print("Parse ");
      if (xpe.getFatalCount() > 0) {
        System.out.print("failed ");
      } else {
        System.out.print("succeeded ");
        System.out.print("(");
        if (hours > 0) {
          System.out.print(hours + ":");
        }
        if (hours > 0 || mins > 0) {
          System.out.print(mins + ":");
        }
        System.out.print(secs + "." + millisec);
        System.out.print(") ");
      }
      System.out.print("with ");

      int errCount = xpe.getErrorCount();
      int warnCount = xpe.getWarningCount();

      if (errCount > 0) {
        System.out.print(errCount + " error");
        System.out.print(errCount > 1 ? "s" : "");
        System.out.print(" and ");
      } else {
        System.out.print("no errors and ");
      }

      if (warnCount > 0) {
        System.out.print(warnCount + " warning");
        System.out.print(warnCount > 1 ? "s" : "");
        System.out.print(".");
      } else {
        System.out.print("no warnings.");
      }

      System.out.println("");
    }

    if (xpe.getErrorCount() > 0) {
      System.exit(1);
    }
  }