in freemarker-docgen-core/src/main/java/org/freemarker/docgen/core/RelaxNGValidator.java [82:190]
public static Document load(File f, DocgenValidationOptions validationOps)
throws IOException, SAXException {
CollectingErrorHandler collErrorHandler
= new CollectingErrorHandler(15);
// We will not use the DocumentBuilderFactory or SAXParserFactory that
// comes for example with iso_relax, because they don't support
// XInclude (as of 2009-02-21).
// We also won't just use a DocumentBuilderFactory and then validate
// the W3C DOM tree, because then there will not be location information
// attached to the validation errors.
// Jing-specific stuff:
// - Create the DocBook Relax NG schema:
PropertyMapBuilder schemaProps = new PropertyMapBuilder();
ValidateProperty.XML_READER_CREATOR.put(
schemaProps, new Jaxp11XMLReaderCreator());
ValidateProperty.ERROR_HANDLER.put(
schemaProps, new DraconianErrorHandler());
RngProperty.CHECK_ID_IDREF.add(schemaProps);
SchemaReader scemaReader = new AutoSchemaReader();
Schema schema;
try {
URL rngUrl = getRequiredResource(
"/org/docbook/schemas/5.0/rng/docbook.rng",
"/schema/5.0/rng/docbook.rng");
schema = scemaReader.createSchema(
ValidationDriver.uriOrFileInputSource(rngUrl.toString()),
schemaProps.toPropertyMap());
} catch (IncorrectSchemaException e) {
throw new BugException(
"Failed to load DocBook Realx NG schema "
+ "(see cause exception).",
e);
}
// - Create the validator:
PropertyMapBuilder valiadtorProps = new PropertyMapBuilder();
ValidateProperty.XML_READER_CREATOR.put(
valiadtorProps, new Jaxp11XMLReaderCreator());
// Used for validation errors:
ValidateProperty.ERROR_HANDLER.put(
valiadtorProps, collErrorHandler);
RngProperty.CHECK_ID_IDREF.add(valiadtorProps);
Validator validator = schema.createValidator(
valiadtorProps.toPropertyMap());
// JAXP/SAX stuff:
// - Usual SAX setup:
SAXParserFactory spf = XMLUtil.newSAXParserFactory();
SAXParser sp;
try {
sp = spf.newSAXParser();
} catch (ParserConfigurationException e) {
throw new BugException(
"Failed to create SAXParser "
+ "(see cause exception).", e);
}
XMLReader xr = sp.getXMLReader();
xr.setErrorHandler(collErrorHandler); // used for well-formedness errors
// - Inject the Realx NG validator plus the DOM builder:
ValidatingDOMBuilder domBuilder;
try {
domBuilder = new ValidatingDOMBuilderWithLocations(
new DocgenRestrictionsValidator(
validator.getContentHandler(),
collErrorHandler, collErrorHandler,
validationOps),
XMLNS_DOCBOOK5,
ELEMENTS_WITH_LOCATION);
} catch (ParserConfigurationException e) {
throw new BugException(
"Failed to create DOM builder "
+ "(see cause exception).", e);
}
xr.setContentHandler(domBuilder);
// - Some helper for the Relax NG validator...
DTDHandler dh = validator.getDTDHandler();
if (dh != null) {
xr.setDTDHandler(dh);
}
// Parsing:
try {
xr.parse(ValidationDriver.fileInputSource(f));
} catch (SAXParseException e) {
// Throw only if we didn't catch the error in the errorHander.
if (collErrorHandler.getErrors().size() == 0) {
throw e;
}
}
if (collErrorHandler.getErrors().size() != 0) {
List<String> errors = collErrorHandler.getErrors();
StringBuilder sb = new StringBuilder(
"The XML wasn't valid:\n\n");
for (String error : errors) {
sb.append(error);
sb.append('\n');
}
throw new SAXException(sb.toString());
}
return domBuilder.getDocument();
}