use of javax.xml.validation.ValidatorHandler in project iaf by ibissource.
the class TestDomTreeAligner method testFiles.
@Override
public void testFiles(String schemaFile, String namespace, String rootElement, String inputFile, boolean potentialCompactionProblems, String expectedFailureReason) throws Exception {
URL schemaUrl = getSchemaURL(schemaFile);
String xsdUri = schemaUrl.toExternalForm();
Schema schema = Utils.getSchemaFromResource(schemaUrl);
XMLSchemaLoader xsLoader = new XMLSchemaLoader();
XSModel xsModel = xsLoader.loadURI(xsdUri);
List<XSModel> schemaInformation = new LinkedList<XSModel>();
schemaInformation.add(xsModel);
String xmlString = getTestFile(inputFile + ".xml");
boolean expectValid = expectedFailureReason == null;
assertEquals("valid XML", expectValid, Utils.validate(schemaUrl, xmlString));
ValidatorHandler validator = schema.newValidatorHandler();
DomTreeAligner aligner = new DomTreeAligner(validator, schemaInformation);
// Xml2Json xml2json = new Xml2Json(aligner, false);
validator.setContentHandler(aligner);
// aligner.setContentHandler(xml2json);
Document domIn = Utils.string2Dom(xmlString);
System.out.println("input DOM [" + Utils.dom2String1(domIn) + "]");
// System.out.println("xmlString "+xmlString);
// System.out.println("dom in "+ToStringBuilder.reflectionToString(domIn.getDocumentElement()));
Source source = aligner.asSource(domIn);
System.out.println();
System.out.println("start aligning " + inputFile);
String xmlAct = Utils.source2String(source);
System.out.println("xml aligned via source=" + xmlAct);
assertNotNull("xmlAct is null", xmlAct);
assertTrue("XML is not aligned", Utils.validate(schemaUrl, xmlAct));
// InputSource is = new InputSource(new StringReader(xmlString));
// try {
// // String jsonOut=xml2json.toString();
// System.out.println("jsonOut="+jsonOut);
// if (!expectValid) {
// fail("expected to fail");
// }
// } catch (Exception e) {
// if (expectValid) {
// e.printStackTrace();
// fail(e.getMessage());
// }
// }
// String xmlString=getTestXml(xml);
// String xsdUri=Utils.class.getResource(xsd).toExternalForm();
//
// assertEquals("valid XML", expectValid, Utils.validate(namespace, xsdUri, xmlString));
//
// Document dom = Utils.string2Dom(xmlString);
// Utils.clean(dom);
//
// XMLReader parser = new SAXParser();
// Schema schema=Utils.getSchemaFromResource(namespace, xsdUri);
// ValidatorHandler validator = schema.newValidatorHandler();
// XmlAligner instance = implementation.newInstance();
// instance.setPsviProvider((PSVIProvider)validator);
//
// parser.setContentHandler(validator);
// validator.setContentHandler(instance);
//
// System.out.println();
// System.out.println("start aligning "+xml);
// instance.parse(dom.getDocumentElement());
// System.out.println("alignedXml="+Utils.dom2String1(dom));
// assertTrue("valid aligned XML", Utils.validate(namespace, xsdUri, dom)); // only if dom itself is modified...
// testXmlAligner(instance, dom.getDocumentElement(), namespace, xsdUri);
// JSONObject json=Utils.xml2Json(xmlString);
// System.out.println("JSON:"+json);
// String jsonString = json.toString();
//
// jsonString=jsonString.replaceAll(",\"xmlns\":\"[^\"]*\"", "");
// System.out.println("JSON with fixed xmlns:"+jsonString);
//
// String xmlFromJson = Utils.json2Xml(Utils.string2Json(jsonString));
// if (StringUtils.isNotEmpty(namespace)) {
// xmlFromJson=xmlFromJson.replaceFirst(">", " xmlns=\""+namespace+"\">");
// }
// System.out.println("start aligning xml from json "+xmlFromJson);
// Document domFromJson = Utils.string2Dom(xmlFromJson);
// instance.startParse(domFromJson.getDocumentElement());
}
use of javax.xml.validation.ValidatorHandler in project iaf by ibissource.
the class DomTreeAligner method translate.
public static String translate(Document xmlIn, URL schemaURL) throws SAXException, IOException {
// create the ValidatorHandler
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaURL);
ValidatorHandler validatorHandler = schema.newValidatorHandler();
// create the XSModel
XMLSchemaLoader xsLoader = new XMLSchemaLoader();
XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
List<XSModel> schemaInformation = new LinkedList<XSModel>();
schemaInformation.add(xsModel);
// create the validator, setup the chain
DomTreeAligner dta = new DomTreeAligner(validatorHandler, schemaInformation);
Source source = dta.asSource(xmlIn);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
String xml = null;
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(source, result);
writer.flush();
xml = writer.toString();
} catch (TransformerConfigurationException e) {
SAXException se = new SAXException(e);
se.initCause(e);
throw se;
} catch (TransformerException e) {
SAXException se = new SAXException(e);
se.initCause(e);
throw se;
}
return xml;
}
use of javax.xml.validation.ValidatorHandler in project iaf by ibissource.
the class Json2Xml method translate.
public static String translate(JsonStructure json, URL schemaURL, boolean compactJsonArrays, String rootElement, boolean strictSyntax, boolean deepSearch, String targetNamespace, Map<String, Object> overrideValues) throws SAXException, IOException {
// create the ValidatorHandler
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaURL);
ValidatorHandler validatorHandler = schema.newValidatorHandler();
// create the XSModel
XMLSchemaLoader xsLoader = new XMLSchemaLoader();
XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
List<XSModel> schemaInformation = new LinkedList<XSModel>();
schemaInformation.add(xsModel);
// create the validator, setup the chain
Json2Xml j2x = new Json2Xml(validatorHandler, schemaInformation, compactJsonArrays, rootElement, strictSyntax);
if (overrideValues != null) {
j2x.setOverrideValues(overrideValues);
}
if (targetNamespace != null) {
// if (DEBUG) System.out.println("setting targetNamespace ["+targetNamespace+"]");
j2x.setTargetNamespace(targetNamespace);
}
j2x.setDeepSearch(deepSearch);
Source source = j2x.asSource(json);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
String xml = null;
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(source, result);
writer.flush();
xml = writer.toString();
} catch (TransformerConfigurationException e) {
SAXException se = new SAXException(e);
se.initCause(e);
throw se;
} catch (TransformerException e) {
SAXException se = new SAXException(e);
se.initCause(e);
throw se;
}
return xml;
}
use of javax.xml.validation.ValidatorHandler in project iaf by ibissource.
the class Json2XmlValidator method alignJson.
protected PipeRunResult alignJson(String messageToValidate, IPipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
ValidationContext context;
ValidatorHandler validatorHandler;
try {
context = validator.createValidationContext(session, getRootValidations(responseMode), getInvalidRootNamespaces());
validatorHandler = validator.getValidatorHandler(session, context);
} catch (ConfigurationException e) {
throw new PipeRunException(this, "Cannot create ValidationContext", e);
}
String resultEvent;
String out = null;
try {
Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
if (StringUtils.isNotEmpty(getTargetNamespace())) {
aligner.setTargetNamespace(getTargetNamespace());
}
aligner.setDeepSearch(isDeepSearch());
aligner.setErrorHandler(context.getErrorHandler());
aligner.setFailOnWildcards(isFailOnWildcards());
ParameterList parameterList = getParameterList();
if (parameterList != null) {
ParameterResolutionContext prc = new ParameterResolutionContext(messageToValidate, session, isNamespaceAware(), false);
Map<String, Object> parametervalues = null;
parametervalues = prc.getValueMap(parameterList);
aligner.setOverrideValues(parametervalues);
}
JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
if (getOutputFormat(session, responseMode).equalsIgnoreCase(FORMAT_JSON)) {
Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
aligner.setContentHandler(xml2json);
aligner.startParse(jsonStructure);
out = xml2json.toString();
} else {
Source source = aligner.asSource(jsonStructure);
out = XmlUtils.source2String(source, isProduceNamespaceLessXml());
}
} catch (Exception e) {
resultEvent = validator.finalizeValidation(context, session, e);
}
resultEvent = validator.finalizeValidation(context, session, null);
PipeForward forward = determineForward(resultEvent, session, responseMode);
PipeRunResult result = new PipeRunResult(forward, out);
return result;
}
use of javax.xml.validation.ValidatorHandler in project iaf by ibissource.
the class Properties2Xml method translate.
public static String translate(Map<String, String> data, URL schemaURL, String rootElement, String targetNamespace) throws SAXException, IOException {
// create the ValidatorHandler
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaURL);
ValidatorHandler validatorHandler = schema.newValidatorHandler();
// create the XSModel
XMLSchemaLoader xsLoader = new XMLSchemaLoader();
XSModel xsModel = xsLoader.loadURI(schemaURL.toExternalForm());
List<XSModel> schemaInformation = new LinkedList<XSModel>();
schemaInformation.add(xsModel);
// create the validator, setup the chain
Properties2Xml p2x = new Properties2Xml(validatorHandler, schemaInformation, rootElement);
if (targetNamespace != null) {
p2x.setTargetNamespace(targetNamespace);
}
Source source = p2x.asSource(data);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
String xml = null;
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(source, result);
writer.flush();
xml = writer.toString();
} catch (TransformerConfigurationException e) {
SAXException se = new SAXException(e);
se.initCause(e);
throw se;
} catch (TransformerException e) {
SAXException se = new SAXException(e);
se.initCause(e);
throw se;
}
return xml;
}
Aggregations