Exemple #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;
    }
  }
Exemple #2
0
 public static void parseDocumentFragment(Reader reader, XMLReceiver xmlReceiver)
     throws SAXException {
   try {
     final XMLReader xmlReader = newSAXParser(XMLUtils.ParserConfiguration.PLAIN).getXMLReader();
     xmlReader.setContentHandler(new XMLFragmentReceiver(xmlReceiver));
     final ArrayList<Reader> readers = new ArrayList<Reader>(3);
     readers.add(new StringReader("<root>"));
     readers.add(reader);
     readers.add(new StringReader("</root>"));
     xmlReader.parse(new InputSource(new SequenceReader(readers.iterator())));
   } catch (IOException e) {
     throw new OXFException(e);
   }
 }
Exemple #3
0
 public static void parseDocumentFragment(String fragment, XMLReceiver xmlReceiver)
     throws SAXException {
   if (fragment.indexOf("<") != -1 || fragment.indexOf("&") != -1) {
     try {
       final XMLReader xmlReader = newSAXParser(XMLUtils.ParserConfiguration.PLAIN).getXMLReader();
       xmlReader.setContentHandler(new XMLFragmentReceiver(xmlReceiver));
       xmlReader.parse(new InputSource(new StringReader("<root>" + fragment + "</root>")));
     } catch (IOException e) {
       throw new OXFException(e);
     }
   } else {
     // Optimization when fragment looks like text
     xmlReceiver.characters(fragment.toCharArray(), 0, fragment.length());
   }
 }
Exemple #4
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();
    }
  }