// dump warnings too
 public void warning(SAXParseException err) throws SAXParseException {
   Debug.trace(
       "** Warning" // NOT LOCALIZABLE
           + ", line "
           + err.getLineNumber() // NOT LOCALIZABLE
           + ", uri "
           + err.getSystemId()); // NOT LOCALIZABLE
   Debug.trace("   " + err.getMessage());
 }
 private void Message(String mode, SAXParseException exception) {
   System.out.println(
       mode
           + " Line: "
           + exception.getLineNumber()
           + " URI: "
           + exception.getSystemId()
           + "\n"
           + " Message: "
           + exception.getMessage());
 }
Exemple #3
0
  /**
   * This creates a new <code>{@link Document}</code> from an existing <code>InputStream</code> by
   * letting a DOM parser handle parsing using the supplied stream.
   *
   * @param in <code>InputStream</code> to parse.
   * @param validate <code>boolean</code> to indicate if validation should occur.
   * @return <code>Document</code> - instance ready for use.
   * @throws IOException when I/O error occurs.
   * @throws JDOMException when errors occur in parsing.
   */
  public Document getDocument(InputStream in, boolean validate) throws IOException, JDOMException {

    try {
      // Load the parser class
      Class parserClass = Class.forName("org.apache.xerces.parsers.DOMParser");
      Object parser = parserClass.newInstance();

      // Set validation
      Method setFeature =
          parserClass.getMethod("setFeature", new Class[] {java.lang.String.class, boolean.class});
      setFeature.invoke(
          parser, new Object[] {"http://xml.org/sax/features/validation", new Boolean(validate)});

      // Set namespaces true
      setFeature.invoke(
          parser, new Object[] {"http://xml.org/sax/features/namespaces", new Boolean(true)});

      // Set the error handler
      if (validate) {
        Method setErrorHandler =
            parserClass.getMethod("setErrorHandler", new Class[] {ErrorHandler.class});
        setErrorHandler.invoke(parser, new Object[] {new BuilderErrorHandler()});
      }

      // Parse the document
      Method parse = parserClass.getMethod("parse", new Class[] {org.xml.sax.InputSource.class});
      parse.invoke(parser, new Object[] {new InputSource(in)});

      // Get the Document object
      Method getDocument = parserClass.getMethod("getDocument", null);
      Document doc = (Document) getDocument.invoke(parser, null);

      return doc;
    } catch (InvocationTargetException e) {
      Throwable targetException = e.getTargetException();
      if (targetException instanceof org.xml.sax.SAXParseException) {
        SAXParseException parseException = (SAXParseException) targetException;
        throw new JDOMException(
            "Error on line "
                + parseException.getLineNumber()
                + " of XML document: "
                + parseException.getMessage(),
            e);
      } else if (targetException instanceof IOException) {
        IOException ioException = (IOException) targetException;
        throw ioException;
      } else {
        throw new JDOMException(targetException.getMessage(), e);
      }
    } catch (Exception e) {
      throw new JDOMException(e.getClass().getName() + ": " + e.getMessage(), e);
    }
  }
    /** Returns a string describing parse exception details */
    private String getParseExceptionInfo(SAXParseException spe) {
      String systemId = spe.getSystemId();
      if (systemId == null) {
        systemId = "null"; // $NON-NLS-1$
      }
      String info =
          "URI="
              + systemId
              + //$NON-NLS-1$
              " Line="
              + spe.getLineNumber()
              + //$NON-NLS-1$
              ": "
              + spe.getMessage(); // $NON-NLS-1$

      return info;
    }
  /**
   * start the parsing
   *
   * @param file to parse
   * @return Vector containing the test cases
   */
  public XMLcpimParser(String fileLocation) {
    try {
      SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
      saxParser = saxParserFactory.newSAXParser().getXMLReader();
      saxParser.setContentHandler(this);
      saxParser.setFeature("http://xml.org/sax/features/validation", true);
      // parse the xml specification for the event tags.
      saxParser.parse(fileLocation);

    } catch (SAXParseException spe) {
      spe.printStackTrace();
    } catch (SAXException sxe) {
      sxe.printStackTrace();
    } catch (IOException ioe) {
      // I/O error
      ioe.printStackTrace();
    } catch (Exception pce) {
      // Parser with specified options can't be built
      pce.printStackTrace();
    }
  }
Exemple #6
0
  private static void inputSourceToSAX(
      InputSource inputSource,
      XMLReceiver xmlReceiver,
      XMLUtils.ParserConfiguration parserConfiguration,
      boolean handleLexical) {

    // Insert XInclude processor if needed
    final TransformerURIResolver resolver;
    if (parserConfiguration.handleXInclude) {
      parserConfiguration =
          new XMLUtils.ParserConfiguration(
              parserConfiguration.validating,
              false,
              parserConfiguration.externalEntities,
              parserConfiguration.uriReferences);
      resolver = new TransformerURIResolver(XMLUtils.ParserConfiguration.PLAIN);
      xmlReceiver =
          new XIncludeReceiver(null, xmlReceiver, parserConfiguration.uriReferences, resolver);
    } else {
      resolver = null;
    }

    try {
      final XMLReader xmlReader = newSAXParser(parserConfiguration).getXMLReader();
      xmlReader.setContentHandler(xmlReceiver);
      if (handleLexical) xmlReader.setProperty(XMLConstants.SAX_LEXICAL_HANDLER, xmlReceiver);

      xmlReader.setEntityResolver(ENTITY_RESOLVER);
      xmlReader.setErrorHandler(ERROR_HANDLER);
      xmlReader.parse(inputSource);
    } catch (SAXParseException e) {
      throw new ValidationException(e.getMessage(), new LocationData(e));
    } catch (Exception e) {
      throw new OXFException(e);
    } finally {
      if (resolver != null) resolver.destroy();
    }
  }
  public static void main(String[] args) throws IOException {
    String filename = "z.xml"; // NOT LOCALIZABLE, main

    try {
      String uri = "file:" + new File(filename).getAbsolutePath(); // NOT LOCALIZABLE, main

      //
      // turn it into an in-memory object.
      //
      Parser parser = getParser();
      parser.setDocumentHandler(new XmlParser());
      parser.setErrorHandler(new MyErrorHandler());
      parser.parse(uri);

    } catch (SAXParseException err) {
      Debug.trace(
          "** Parsing error" // NOT LOCALIZABLE, main
              + ", line "
              + err.getLineNumber() // NOT LOCALIZABLE
              + ", uri "
              + err.getSystemId()); // NOT LOCALIZABLE
      Debug.trace("   " + err.getMessage());

    } catch (SAXException e) {
      Exception x = e;
      if (e.getException() != null) x = e.getException();
      x.printStackTrace();

    } catch (Throwable t) {
      t.printStackTrace();
    }

    byte[] buf = new byte[256];
    Debug.trace("Press ENTER to exit."); // NOT LOCALIZABLE
    System.in.read(buf, 0, 256);
    System.exit(0);
  }
  public ByteArrayOutputStream cleanup(InputStream xml) throws XServerException {
    inputXml = xml;

    // Use the default (non-validating) parser
    SAXParserFactory factory = SAXParserFactory.newInstance();

    try {
      // Parse the input
      SAXParser saxParser = factory.newSAXParser();
      saxParser.parse(inputXml, this);

      // close the stream
      inputXml.close();
    } catch (SAXParseException spe) {
      // Use the contained exception, if any
      Exception x = spe;

      if (spe.getException() != null) {
        x = spe.getException();
      }

      // Error generated by the parser
      LOG.warn(
          "XMLCleanup.cleanup() parsing exception: "
              + spe.getMessage()
              + " - xml line "
              + spe.getLineNumber()
              + ", uri "
              + spe.getSystemId(),
          x);
    } catch (SAXException sxe) {
      // Error generated by this application
      // (or a parser-initialization error)
      Exception x = sxe;

      if (sxe.getException() != null) {
        x = sxe.getException();
      }

      LOG.warn("XMLCleanup.cleanup() SAX exception: " + sxe.getMessage(), x);
    } catch (ParserConfigurationException pce) {
      // Parser with specified options can't be built
      LOG.warn("XMLCleanup.cleanup() SAX parser cannot be built with " + "specified options");
    } catch (IOException ioe) {
      // I/O error
      LOG.warn("XMLCleanup.cleanup() IO exception", ioe);
    } catch (Throwable t) {
      LOG.warn("XMLCleanup.cleanup() exception", t);
    }

    if (error) {
      throw new XServerException(error_code, error_text);
    }

    return bytes;
  }
Exemple #9
0
 public void fatalError(SAXParseException exception) throws SAXException {
   throw new ValidationException(
       "Fatal error: " + exception.getMessage(), new LocationData(exception));
 }
 // dump warnings too
 public void warning(SAXParseException err) throws SAXParseException {
   LOG.warn(
       "SAXParser warning" + ", xml line " + err.getLineNumber() + ", uri " + err.getSystemId());
   LOG.warn("   " + err.getMessage());
 }