Example #1
0
  /**
   * Return whether the given string contains well-formed XML.
   *
   * @param xmlString string to check
   * @return true iif the given string contains well-formed XML
   */
  public static boolean isWellFormedXML(String xmlString) {

    // Empty string is never well-formed XML
    if (xmlString.trim().length() == 0) return false;

    try {
      final XMLReader xmlReader = newSAXParser(XMLUtils.ParserConfiguration.PLAIN).getXMLReader();
      xmlReader.setContentHandler(NULL_CONTENT_HANDLER);
      xmlReader.setEntityResolver(ENTITY_RESOLVER);
      xmlReader.setErrorHandler(
          new org.xml.sax.ErrorHandler() {
            public void error(SAXParseException exception) throws SAXException {
              throw exception;
            }

            public void fatalError(SAXParseException exception) throws SAXException {
              throw exception;
            }

            public void warning(SAXParseException exception) throws SAXException {}
          });
      xmlReader.parse(new InputSource(new StringReader(xmlString)));
      return true;
    } catch (Exception e) {
      // Ideally we would like the parser to not throw as this is time-consuming, but not sure how
      // to achieve that
      return false;
    }
  }
Example #2
0
 public static XMLReader newXMLReader(XMLUtils.ParserConfiguration parserConfiguration) {
   final SAXParser saxParser = XMLUtils.newSAXParser(parserConfiguration);
   try {
     final XMLReader xmlReader = saxParser.getXMLReader();
     xmlReader.setEntityResolver(XMLUtils.ENTITY_RESOLVER);
     xmlReader.setErrorHandler(XMLUtils.ERROR_HANDLER);
     return xmlReader;
   } catch (Exception e) {
     throw new OXFException(e);
   }
 }
Example #3
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();
    }
  }