/** * * Try to obtain the XML schema file (depending on message version), either as provided in * xsi:schemaLocation, or as provided in the locations property or in a subdirectory of the * current dir. * * @param doc the DOM document * @return the file name of the schema * @throws IOException */ private String getSchemaLocation(Document doc) throws IOException { String schemaFilename = extractSchemaLocation(doc); if (schemaFilename == null) { if ((schemaFilename = staticSchema(doc)) == null) { throw new IOException( "Unable to retrieve a valid schema to use for message validation"); } } return schemaFilename; }
/** * Test/validate a given xml document against a hl7 v2.xml schema. * <p> * Before the schema is applied, the namespace is verified because otherwise schema validation * fails anyway. * <p> * If a schema file is specified in the xml message and the file can be located on the disk this * one is used. If no schema has been specified, or the file can't be located, the locations * property is used. * * @param msg the xml message (as string) to be validated. * @return ValidationException[] an array of validation exceptions, which is zero-sized when no * validation errors occured. */ public ValidationException[] apply(String msg) { List<ValidationException> validationErrors = new ArrayList<ValidationException>(); try { // parse the incoming string into a dom document - no schema validation yet Document doc = XMLUtils.parse(msg); if (hasCorrectNamespace(doc, validationErrors)) { XMLUtils.validate(doc, getSchemaLocation(doc), new ErrorHandler(validationErrors)); } } catch (Exception e) { log.error("Unable to validate message: {}", e.getMessage(), e); validationErrors.add(new ValidationException("Unable to validate message " + e.getMessage(), e)); } return validationErrors.toArray(new ValidationException[validationErrors.size()]); }
/** * Adds {@link XMLSchemaRule} for the configured versions * * @return this instance to build more rules */ public EncodingRuleBuilder xsd() { return test(prepareRule(new XMLSchemaRule())); }
/** * Test/validate a given xml document against a hl7 v2.xml schema. * <p> * Before the schema is applied, the namespace is verified because otherwise schema validation * fails anyway. * <p> * If a schema file is specified in the xml message and the file can be located on the disk this * one is used. If no schema has been specified, or the file can't be located, the locations * property is used. * * @param msg the xml message (as string) to be validated. * @return ValidationException[] an array of validation exceptions, which is zero-sized when no * validation errors occured. */ public ValidationException[] apply(String msg) { List<ValidationException> validationErrors = new ArrayList<ValidationException>(); try { // parse the incoming string into a dom document - no schema validation yet Document doc = XMLUtils.parse(msg); if (hasCorrectNamespace(doc, validationErrors)) { XMLUtils.validate(doc, getSchemaLocation(doc), new ErrorHandler(validationErrors)); } } catch (Exception e) { log.error("Unable to validate message: {}", e.getMessage(), e); validationErrors.add(new ValidationException("Unable to validate message " + e.getMessage(), e)); } return validationErrors.toArray(new ValidationException[validationErrors.size()]); }
/** * Adds {@link XMLSchemaRule} for the configured versions * * @return this instance to build more rules */ public EncodingRuleBuilder xsd() { return test(prepareRule(new XMLSchemaRule())); }
/** * * Try to obtain the XML schema file (depending on message version), either as provided in * xsi:schemaLocation, or as provided in the locations property or in a subdirectory of the * current dir. * * @param doc the DOM document * @return the file name of the schema * @throws IOException */ private String getSchemaLocation(Document doc) throws IOException { String schemaFilename = extractSchemaLocation(doc); if (schemaFilename == null) { if ((schemaFilename = staticSchema(doc)) == null) { throw new IOException( "Unable to retrieve a valid schema to use for message validation"); } } return schemaFilename; }