/** * Validates the specified XML instance document according to the contained schema references ( * <code>xsi:schemaLocation</code> attribute) and/or to the explicitly specified schema references. * * @param source * provides the XML 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<SchemaValidationEvent> validate( InputStream source, String... schemaUris ) { return validate( new XMLInputSource( null, null, null, source, null ), schemaUris ); }
/** * Validates the specified XML schema document, additionally in conjunction with more schemas. * * @param inputSchemaUri * provides the XML schema document to be validated, must not be null * @param additionalUris * additional schema documents to be considered, can be null * @return list of validation events (errors/warnings) that occured, never null, size of 0 means valid document */ public static List<String> validateSchema( String inputSchemaUri, String... additionalUris ) { LSInput input = new DOMInputImpl( null, inputSchemaUri, null ); LSInput[] additionalSchemas = new LSInput[additionalUris.length]; for ( int i = 0; i < additionalUris.length; i++ ) { additionalSchemas[i] = new DOMInputImpl( null, additionalUris[i], null ); } return validateSchema( input, additionalSchemas ); }
if ( new QName( CommonNamespaces.XSNS, "schema" ).equals( firstElement ) ) { LOG.info( "Validating generated XML output (schema document)." ); messages = SchemaValidator.validateSchema( buffer.getInputStream() ); } else { LOG.info( "Validating generated XML output (instance document)." ); messages = new ArrayList<String>(); List<SchemaValidationEvent> evts = SchemaValidator.validate( buffer.getInputStream() ); for ( SchemaValidationEvent evt : evts ) { messages.add( evt.toString() );
XMLParserConfiguration parserConfig = createValidatingParser( new RedirectingEntityResolver(), grammarPool ); parserConfig.setErrorHandler( new XMLErrorHandler() { @SuppressWarnings("synthetic-access")
/** * Validates the specified XML instance document according to the contained schema references ( * <code>xsi:schemaLocation</code> attribute) and/or to the explicitly specified schema references. * * @param url * provides the XML 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 * @throws IOException * @throws MalformedURLException */ public static List<SchemaValidationEvent> validate( String url, String... schemaUris ) throws MalformedURLException, IOException { InputStream is = ProxySettings.openURLConnection( new URL( url ), null, null ).getInputStream(); return validate( new XMLInputSource( null, null, null, is, null ), schemaUris ); }
/** * Validates the specified XML schema document, additionally in conjunction with more schemas. * * @param inputSchema * provides the XML schema document to be validated, must not be null * @param additionalUris * additional schema documents to be considered, can be null * @return list of validation events (errors/warnings) that occured, never null, size of 0 means valid document */ public static List<String> validateSchema( InputStream inputSchema, String... additionalUris ) { LSInput input = new DOMInputImpl( null, null, null, inputSchema, null ); LSInput[] additionalSchemas = new LSInput[additionalUris.length]; for ( int i = 0; i < additionalUris.length; i++ ) { additionalSchemas[i] = new DOMInputImpl( null, additionalUris[i], null ); } return validateSchema( input, additionalSchemas ); }
/** * Validates the client request * * @param elem * @return a list of error messages */ private List<String> validate( OMElement elem ) { StringWriter s = new StringWriter(); try { elem.serialize( s ); } catch ( XMLStreamException e ) { e.printStackTrace(); } InputStream is = new ByteArrayInputStream( s.toString().getBytes() ); List<SchemaValidationEvent> evts = SchemaValidator.validate( is, "http://schemas.opengis.net/csw/2.0.2/CSW-discovery.xsd" ); List<String> list = new ArrayList<String>(); for ( SchemaValidationEvent evt : evts ) { list.add( evt.toString() ); } return list; }
/** * Before any transaction operation is possible there should be an evaluation of the record. The response of the * full ISO record has to be valid. With this method this is guaranteed. * * @param elem * that has to be evaluated before there is any transaction operation possible. * @return a list of error-strings, or empty list if there is no validation needed. * @throws MetadataStoreException */ private List<SchemaValidationEvent> validate( OMElement elem ) throws MetadataInspectorException { InputStream is = null; try { StreamBufferStore os = new StreamBufferStore(); elem.serialize( os ); is = os.getInputStream(); } catch ( Throwable e ) { LOG.debug( "error: " + e.getMessage(), e ); throw new MetadataInspectorException( e.getMessage() ); } if ( new QName( "http://www.isotc211.org/2005/gmd", "MD_Metadata" ).equals( elem.getQName() ) ) { return SchemaValidator.validate( is, SCHEMA_URL_GMD, SCHEMA_URL_SRV ); } // DublinCore return SchemaValidator.validate( is, SCHEMA_URL ); }
schemas = s.split( "," ); List<String> results = new ArrayList<String>(); List<SchemaValidationEvent> evts = SchemaValidator.validate( xml, schemas ); for ( SchemaValidationEvent evt : evts ) { results.add( evt.toString() );