use of com.thaiopensource.util.PropertyMapBuilder in project camel by apache.
the class JingValidator method process.
public void process(Exchange exchange) throws Exception {
Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator();
DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
PropertyMapBuilder mapBuilder = new PropertyMapBuilder();
mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator);
mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
PropertyMap propertyMap = mapBuilder.toPropertyMap();
Validator validator = getSchema().createValidator(propertyMap);
Message in = exchange.getIn();
SAXSource saxSource = in.getBody(SAXSource.class);
if (saxSource == null) {
Source source = exchange.getIn().getMandatoryBody(Source.class);
saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source);
}
InputSource bodyInput = saxSource.getInputSource();
// now lets parse the body using the validator
XMLReader reader = xmlCreator.createXMLReader();
reader.setContentHandler(validator.getContentHandler());
reader.setDTDHandler(validator.getDTDHandler());
reader.setErrorHandler(errorHandler);
reader.parse(bodyInput);
errorHandler.handleErrors(exchange, schema);
}
use of com.thaiopensource.util.PropertyMapBuilder in project intellij-community by JetBrains.
the class ValidateAction method doValidation.
@SuppressWarnings({ "ThrowableInstanceNeverThrown" })
private static void doValidation(VirtualFile instanceFile, VirtualFile schemaFile, org.xml.sax.ErrorHandler eh) {
final SchemaReader sr = schemaFile.getFileType() == RncFileType.getInstance() ? CompactSchemaReader.getInstance() : new AutoSchemaReader();
final PropertyMapBuilder properties = new PropertyMapBuilder();
ValidateProperty.ERROR_HANDLER.put(properties, eh);
// TODO: should some options dialog displayed before validating?
RngProperty.CHECK_ID_IDREF.add(properties);
try {
final String schemaPath = VfsUtilCore.fixIDEAUrl(schemaFile.getUrl());
try {
final ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), sr);
final InputSource in = ValidationDriver.uriOrFileInputSource(schemaPath);
in.setEncoding(schemaFile.getCharset().name());
if (driver.loadSchema(in)) {
final String path = VfsUtilCore.fixIDEAUrl(instanceFile.getUrl());
try {
driver.validate(ValidationDriver.uriOrFileInputSource(path));
} catch (IOException e1) {
eh.fatalError(new SAXParseException(e1.getMessage(), null, UriOrFile.fileToUri(path), -1, -1, e1));
}
}
} catch (SAXParseException e1) {
eh.fatalError(e1);
} catch (IOException e1) {
eh.fatalError(new SAXParseException(e1.getMessage(), null, UriOrFile.fileToUri(schemaPath), -1, -1, e1));
}
} catch (SAXException | MalformedURLException e1) {
// huh?
Logger.getInstance(ValidateAction.class.getName()).error(e1);
}
}
use of com.thaiopensource.util.PropertyMapBuilder in project intellij-community by JetBrains.
the class XmlInstanceValidator method doValidation.
public static void doValidation(@NotNull final XmlDocument doc, final Validator.ValidationHost host, final XmlFile descriptorFile) {
try {
final Schema schema = RngParser.getCachedSchema(descriptorFile);
if (schema == null) {
// did not manage to get a compiled schema. no validation...
return;
}
final ErrorHandler eh = MyErrorHandler.create(doc, host);
if (eh == null) {
return;
}
final PropertyMapBuilder builder = new PropertyMapBuilder();
builder.put(ValidateProperty.ERROR_HANDLER, eh);
final ContentHandler handler = schema.createValidator(builder.toPropertyMap()).getContentHandler();
doc.accept(new Psi2SaxAdapter(handler));
} catch (ProcessCanceledException e) {
throw e;
} catch (RuntimeException e) {
LOG.error(e);
} catch (Exception e) {
LOG.info(e);
}
}
Aggregations