本文整理汇总了Java中javax.xml.validation.Validator.validate方法的典型用法代码示例。如果您正苦于以下问题:Java Validator.validate方法的具体用法?Java Validator.validate怎么用?Java Validator.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.validation.Validator
的用法示例。
在下文中一共展示了Validator.validate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rulesXmlIsValid
import javax.xml.validation.Validator; //导入方法依赖的package包/类
@Test
public void rulesXmlIsValid() {
RulesXmlReaderFactory xmlFactory = new RulesXmlReaderFactory();
try (Reader xmlReader = xmlFactory.newRulesXmlReader(); Reader xsdReader = xmlFactory.newRulesXsdReader()) {
StreamSource xsdStreamSource = new StreamSource(xsdReader);
StreamSource xmlStreamSource = new StreamSource(xmlReader);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(xsdStreamSource);
Validator validator = schema.newValidator();
validator.validate(xmlStreamSource);
} catch (Exception e) {
fail("rules.xml does not conform to schema!");
}
}
示例2: validation
import javax.xml.validation.Validator; //导入方法依赖的package包/类
/**
* 利用xsd验证xml
* @param xsdFile xsdFile
* @param xmlInput xmlInput
* @throws SAXException SAXException
* @throws IOException IOException
*/
public static void validation(String xsdFile, InputStream xmlInput) throws SAXException, IOException
{
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL xsdURL = Validation.class.getClassLoader().getResource(xsdFile);
if(xsdURL != null)
{
Schema schema = factory.newSchema(xsdURL);
Validator validator = schema.newValidator();
// validator.setErrorHandler(new AutoErrorHandler());
Source source = new StreamSource(xmlInput);
try(OutputStream resultOut = new FileOutputStream(new File(PathUtil.getRootDir(), xsdFile + ".xml")))
{
Result result = new StreamResult(resultOut);
validator.validate(source, result);
}
}
else
{
throw new FileNotFoundException(String.format("can not found xsd file [%s] from classpath.", xsdFile));
}
}
示例3: validateDAT
import javax.xml.validation.Validator; //导入方法依赖的package包/类
public static boolean validateDAT(File xmlFile) {
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(DATschemaURL.getPath()));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xmlFile));
} catch (IOException | SAXException e) {
System.out.println("Exception: " + e.getMessage());
return false;
}
return true;
}
示例4: validateXML
import javax.xml.validation.Validator; //导入方法依赖的package包/类
private boolean validateXML(InputStream xml, InputStream xsd){
try
{
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
javax.xml.validation.Schema schema = factory.newSchema(new StreamSource(xsd));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
return true;
}
catch( SAXException| IOException ex)
{
//MessageDialog.openError(Display.getCurrent().getActiveShell(), "Error", Messages.IMPORT_XML_FORMAT_ERROR + "-\n" + ex.getMessage());
MessageBox dialog = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_ERROR | SWT.OK);
dialog.setText(Messages.ERROR);
dialog.setMessage(Messages.IMPORT_XML_FORMAT_ERROR + "-\n" + ex.getMessage());
logger.error(Messages.IMPORT_XML_FORMAT_ERROR);
return false;
}
}
示例5: validateAccessControllSchema
import javax.xml.validation.Validator; //导入方法依赖的package包/类
/**
* Tests if the access-control-schema.xml is valid.
*
* @throws ParserConfigurationException If a DocumentBuilder cannot be created which satisfies the configuration
* requested.
* @throws IOException If any IO errors occur.
* @throws SAXException If an error occurs during the validation.
*/
@Test
public void validateAccessControllSchema() throws ParserConfigurationException, SAXException, IOException {
// parse an XML document into a DOM tree
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
String xmlPath = getClass().getResource("/config/app/security/access-control-schema.xml").getPath();
Document document = parser.parse(new File(xmlPath));
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// load a WXS schema, represented by a Schema instance
URL schemaPath = getClass().getResource("/io/oasp/module/security/access-control-schema.xsd");
Schema schema = factory.newSchema(schemaPath);
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
}
示例6: testSpecification
import javax.xml.validation.Validator; //导入方法依赖的package包/类
public void testSpecification() throws Exception {
try {
String filename = "test/data/specification.xml";
Validator validator = buildValidator("schema/specification-schema.xsd");
FileOutputStream fos = new FileOutputStream(filename);
try (FreeColXMLWriter xw = new FreeColXMLWriter(fos, null, false)) {
spec().toXML(xw);
} catch (IOException ioe) {
fail(ioe.getMessage());
}
validator.validate(new StreamSource(new FileReader(filename)));
} catch (SAXParseException e) {
String errMsg = e.getMessage()
+ " at line=" + e.getLineNumber()
+ " column=" + e.getColumnNumber();
fail(errMsg);
}
}
示例7: testStringTemplate
import javax.xml.validation.Validator; //导入方法依赖的package包/类
public void testStringTemplate() throws Exception {
StringTemplate t1 = StringTemplate.template("model.goods.goodsAmount")
.add("%goods%", "model.goods.food.name")
.addName("%amount%", "100");
StringTemplate t2 = StringTemplate.template("model.goods.goodsAmount")
.addAmount("%amount%", 50)
.addStringTemplate("%goods%", t1);
Game game = getGame();
Player player = game.getPlayerByNationId("model.nation.dutch");
try {
Validator validator = buildValidator("schema/data/data-stringTemplate.xsd");
validator.validate(buildSource(t2));
} catch (SAXParseException e){
String errMsg = e.getMessage()
+ " at line=" + e.getLineNumber()
+ " column=" + e.getColumnNumber();
fail(errMsg);
}
}
示例8: testExtraPathWithSlash
import javax.xml.validation.Validator; //导入方法依赖的package包/类
/** The latest XSD repository-6 should fail when an 'extra' is present. */
public void testExtraPathWithSlash() throws Exception {
String document = "<?xml version=\"1.0\"?>" +
OPEN_TAG_REPO +
"<r:extra> <r:revision>1</r:revision> <r:path>path</r:path> " +
"<r:archives> <r:archive> <r:size>1</r:size> <r:checksum>2822ae37115ebf13412bbef91339ee0d9454525e</r:checksum> " +
"<r:url>url</r:url> </r:archive> </r:archives> </r:extra>" +
CLOSE_TAG_REPO;
Source source = new StreamSource(new StringReader(document));
// don't capture the validator errors, we want it to fail and catch the exception
Validator validator = getRepoValidator(SdkRepoConstants.NS_LATEST_VERSION, null);
try {
validator.validate(source);
} catch (SAXParseException e) {
// We expect a parse error referring to this grammar rule
assertRegex("cvc-complex-type.2.4.a: Invalid content was found starting with element 'r:extra'.*",
e.getMessage());
return;
}
// If we get here, the validator has not failed as we expected it to.
fail();
}
示例9: validateXml
import javax.xml.validation.Validator; //导入方法依赖的package包/类
public static boolean validateXml(File xsd, String xmlPath) {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema;
try {
schema = schemaFactory.newSchema(xsd);
} catch (SAXException e) {
LOG.warn("The XSD file not found in path : " + xsd.getPath(), e);
return false;
}
Validator validator = schema.newValidator();
Source source = new StreamSource(xmlPath);
try {
validator.validate(source);
} catch (Exception ex) {
LOG.warn("The XML file is invalid in path : " + xmlPath, ex);
return false;
}
return true;
}
示例10: readFile
import javax.xml.validation.Validator; //导入方法依赖的package包/类
Document readFile(Path p) {
Document document = null;
try {
// parse an XML document into a DOM tree
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder parser = factory.newDocumentBuilder();
document = parser.parse(p.toFile());
// create a Validator instance, which can be used to validate an instance document
Validator validator = schema.newValidator();
// validate the DOM tree
validator.validate(new DOMSource(document));
} catch (SAXException | IOException | ParserConfigurationException e) {
throw new ParseException(e);
}
return document;
}
示例11: validate
import javax.xml.validation.Validator; //导入方法依赖的package包/类
private void validate(final String xsdFile, final Source src, final Result result) throws Exception {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI()));
// Get a Validator which can be used to validate instance document
// against this grammar.
Validator validator = schema.newValidator();
ErrorHandler eh = new ErrorHandlerImpl();
validator.setErrorHandler(eh);
// Validate this instance document against the
// Instance document supplied
validator.validate(src, result);
} catch (Exception ex) {
throw ex;
}
}
示例12: validateAgainstXSD
import javax.xml.validation.Validator; //导入方法依赖的package包/类
public static boolean validateAgainstXSD(final InputStream xml) throws IOException {
ClassLoader cl = MarshallUtil.class.getClassLoader();
InputStream schemaInputStream = cl.getResourceAsStream("setup_definition-1.0.xsd");
if (schemaInputStream == null) {
throw new IOException("XSD configuration not found");
}
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(schemaInputStream));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(xml));
return true;
} catch (Exception ex) {
return false;
}
}
示例13: testWrongTypeContentXml
import javax.xml.validation.Validator; //导入方法依赖的package包/类
/** A document with a wrong type element. */
public void testWrongTypeContentXml() throws Exception {
String document = "<?xml version=\"1.0\"?>" +
OPEN_TAG_REPO +
"<r:platform> <r:api-level>NotAnInteger</r:api-level> <r:libs /> </r:platform>" +
CLOSE_TAG_REPO;
Source source = new StreamSource(new StringReader(document));
// don't capture the validator errors, we want it to fail and catch the exception
Validator validator = getRepoValidator(SdkRepoConstants.NS_LATEST_VERSION, null);
try {
validator.validate(source);
} catch (SAXParseException e) {
// We expect a parse error referring to this grammar rule
assertRegex("cvc-datatype-valid.1.2.1: 'NotAnInteger' is not a valid value.*",
e.getMessage());
return;
}
// If we get here, the validator has not failed as we expected it to.
fail();
}
示例14: testTableB
import javax.xml.validation.Validator; //导入方法依赖的package包/类
private static Element testTableB(Document document, Element root, Validator validator) {
Element tableB = document.createElement("table");
tableB.setAttribute("name", "tableB");
root.appendChild(tableB);
Element schema = document.createElement("schema");
tableB.appendChild(schema);
Element attribute = document.createElement("attribute");
attribute.setAttribute("name", "id");
attribute.setAttribute("type", "java.lang.Integer");
attribute.setAttribute("isPrimaryKey", "true");
schema.appendChild(attribute);
Element record = document.createElement("record");
record.setAttribute("id", "1");
tableB.appendChild(record);
try {
validator.validate(new DOMSource(document));
} catch(Exception e) {
fail("Document should now be valid!");
}
return tableB;
}
示例15: doValideerTegenSchema
import javax.xml.validation.Validator; //导入方法依赖的package包/类
/**
* Default implementatie voor het valideren van het schema bestand.
*
* @param xmlSource xml een te valideren (XML) bestand
* @param schema een {@link Schema} instantie
*/
static void doValideerTegenSchema(final Source xmlSource, final Schema schema) {
final Validator validator = schema.newValidator();
try {
validator.validate(xmlSource);
} catch (SAXException | IOException e) {
throw new SchemaValidatieException("XML niet XSD valide: " + xmlSource.toString(), e);
}
}