/** * Validates the specified XML instance document according to the contained schema references ( * <code>xsi:schemaLocation</code> attribute) and/or to explicitly specified schema references. * * @param source provides the document to be validated, must not be null * @param schemaUris URIs of schema documents to be considered, can be null (only the <code> * xsi:schemaLocation</code> attribute is considered then) * @return list of validation events (errors/warnings) that occured, never null, size of 0 means * valid document */ public static List<String> validate(XMLInputSource source, String... schemaUris) { final List<String> errors = new LinkedList<String>(); try { RedirectingEntityResolver resolver = new RedirectingEntityResolver(); if (schemaUris != null) { for (int i = 0; i < schemaUris.length; i++) { schemaUris[i] = resolver.redirect(schemaUris[i]); } } GrammarPool grammarPool = (schemaUris == null ? null : GrammarPoolManager.getGrammarPool(schemaUris)); XMLParserConfiguration parserConfig = createValidatingParser(new RedirectingEntityResolver(), grammarPool); parserConfig.setErrorHandler( new XMLErrorHandler() { @SuppressWarnings("synthetic-access") @Override public void error(String domain, String key, XMLParseException e) throws XNIException { LOG.debug("Encountered error: " + toString(e)); errors.add("Error: " + toString(e)); } @SuppressWarnings("synthetic-access") @Override public void fatalError(String domain, String key, XMLParseException e) throws XNIException { LOG.debug("Encountered fatal error: " + toString(e)); errors.add("Fatal error: " + toString(e)); } @SuppressWarnings("synthetic-access") @Override public void warning(String domain, String key, XMLParseException e) throws XNIException { LOG.debug("Encountered warning: " + toString(e)); errors.add("Warning: " + toString(e)); } private String toString(XMLParseException e) { String s = e.getLocalizedMessage(); s += " (line: " + e.getLineNumber() + ", column: " + e.getColumnNumber(); s += e.getExpandedSystemId() != null ? ", SystemID: '" + e.getExpandedSystemId() + "')" : ")"; return s; } }); parserConfig.parse(source); } catch (Exception e) { LOG.debug(e.getMessage(), e); errors.add("Fatal error: " + e.getMessage()); } return errors; }
public CreateIndexHtmlFilter parse(String body) { String html = createHtml(body); CreateIndexHtmlFilter nf = new CreateIndexHtmlFilter(1, null); XMLParserConfiguration parser = new HTMLConfiguration(); parser.setProperty( "http://cyberneko.org/html/properties/filters", new XMLDocumentFilter[] {nf}); XMLInputSource source = new XMLInputSource(null, null, null, new StringReader(html), "UTF-8"); try { parser.parse(source); return nf; } catch (Exception ex) { throw new RuntimeException(ex); } }
public void validate(Source source, Result result) throws SAXException, IOException { if (result instanceof StreamResult || result == null) { final StreamSource streamSource = (StreamSource) source; final StreamResult streamResult = (StreamResult) result; XMLInputSource input = new XMLInputSource(streamSource.getPublicId(), streamSource.getSystemId(), null); input.setByteStream(streamSource.getInputStream()); input.setCharacterStream(streamSource.getReader()); // Gets the parser configuration. We'll create and initialize a new one, if we // haven't created one before or if the previous one was garbage collected. boolean newConfig = false; XMLParserConfiguration config = (XMLParserConfiguration) fConfiguration.get(); if (config == null) { config = initialize(); newConfig = true; } // If settings have changed on the component manager, refresh the error handler and entity // resolver. else if (fComponentManager.getFeature(PARSER_SETTINGS)) { config.setProperty(ENTITY_RESOLVER, fComponentManager.getProperty(ENTITY_RESOLVER)); config.setProperty(ERROR_HANDLER, fComponentManager.getProperty(ERROR_HANDLER)); config.setProperty(SECURITY_MANAGER, fComponentManager.getProperty(SECURITY_MANAGER)); } // prepare for parse fComponentManager.reset(); if (streamResult != null) { if (fSerializerFactory == null) { fSerializerFactory = SerializerFactory.getSerializerFactory(Method.XML); } // there doesn't seem to be a way to reset a serializer, so we need to make // a new one each time. Serializer ser; if (streamResult.getWriter() != null) { ser = fSerializerFactory.makeSerializer(streamResult.getWriter(), new OutputFormat()); } else if (streamResult.getOutputStream() != null) { ser = fSerializerFactory.makeSerializer(streamResult.getOutputStream(), new OutputFormat()); } else if (streamResult.getSystemId() != null) { String uri = streamResult.getSystemId(); OutputStream out = XMLEntityManager.createOutputStream(uri); ser = fSerializerFactory.makeSerializer(out, new OutputFormat()); } else { throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "StreamResultNotInitialized", null)); } // we're using the parser only as an XNI-to-SAX converter, // so that we can use the SAX-based serializer SAXParser parser = (SAXParser) fParser.get(); if (newConfig || parser == null) { parser = new SAXParser(config); fParser = new SoftReference(parser); } else { parser.reset(); } config.setDocumentHandler(fSchemaValidator); fSchemaValidator.setDocumentHandler(parser); parser.setContentHandler(ser.asContentHandler()); } else { fSchemaValidator.setDocumentHandler(null); } try { config.parse(input); } catch (XMLParseException e) { throw Util.toSAXParseException(e); } catch (XNIException e) { throw Util.toSAXException(e); } finally { // release the references to the SAXParser and Serializer fSchemaValidator.setDocumentHandler(null); } return; } throw new IllegalArgumentException( JAXPValidationMessageFormatter.formatMessage( fComponentManager.getLocale(), "SourceResultMismatch", new Object[] {source.getClass().getName(), result.getClass().getName()})); }
/** * parse * * @param inputSource * @exception XNIException * @exception java.io.IOException */ public void parse(XMLInputSource inputSource) throws XNIException, IOException { reset(); fConfiguration.parse(inputSource); } // parse(XMLInputSource)