本文整理汇总了Java中javax.xml.validation.Validator类的典型用法代码示例。如果您正苦于以下问题:Java Validator类的具体用法?Java Validator怎么用?Java Validator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Validator类属于javax.xml.validation包,在下文中一共展示了Validator类的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: validateWithXMLSchema
import javax.xml.validation.Validator; //导入依赖的package包/类
/**
* Validates XML against XSD schema
*
* @param xml XML in which the element is being searched
* @param schemas XSD schemas against which the XML is validated
* @throws SAXException if the XSD schema is invalid
* @throws IOException if the XML at the specified path is missing
*/
public static void validateWithXMLSchema(InputStream xml, InputStream[] schemas) throws IOException, SAXException {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source[] sources = new Source[schemas.length];
for (int i = 0; i < schemas.length; i++) {
sources[i] = new StreamSource(schemas[i]);
}
Schema schema = factory.newSchema(sources);
Validator validator = schema.newValidator();
try {
validator.validate(new StreamSource(xml));
} catch (SAXException e) {
throw new GeneralException(e);
}
}
示例4: define
import javax.xml.validation.Validator; //导入依赖的package包/类
@Override
public void define(Context context) {
try (
Reader xmlStreamValidationReader = xmlFactory.newRulesXmlReader();
Reader xmlStreamRulesDefinitionReader = xmlFactory.newRulesXmlReader();
Reader xsdStreamReader = xmlFactory.newRulesXsdReader()
) {
StreamSource xsdStreamSource = new StreamSource(xsdStreamReader);
StreamSource xmlStreamSource = new StreamSource(xmlStreamValidationReader);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(xsdStreamSource);
Validator validator = schema.newValidator();
validator.validate(xmlStreamSource);
NewRepository repo = context.createRepository(repositoryKey(), "php").setName(repositoryName());
RulesDefinitionXmlLoader xmlLoader = new RulesDefinitionXmlLoader();
xmlLoader.load(repo, xmlStreamRulesDefinitionReader);
repo.done();
} catch (Exception e) {
throw new IllegalStateException("rules.xml not found or invalid", e);
}
}
示例5: getValidator
import javax.xml.validation.Validator; //导入依赖的package包/类
/**
* Helper method that returns a validator for our XSD, or null if the current Java
* implementation can't process XSD schemas.
*
* @param version The version of the XML Schema.
* See {@link SdkStatsConstants#getXsdStream(int)}
*/
private Validator getValidator(int version) throws SAXException {
InputStream xsdStream = SdkStatsConstants.getXsdStream(version);
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
if (factory == null) {
return null;
}
// This may throw a SAX Exception if the schema itself is not a valid XSD
Schema schema = factory.newSchema(new StreamSource(xsdStream));
Validator validator = schema == null ? null : schema.newValidator();
return validator;
} finally {
if (xsdStream != null) {
try {
xsdStream.close();
} catch (IOException ignore) {}
}
}
}
示例6: getValidator
import javax.xml.validation.Validator; //导入依赖的package包/类
/**
* Helper method that returns a validator for our XSD, or null if the current Java
* implementation can't process XSD schemas.
*
* @param version The version of the XML Schema.
* See {@link SdkAddonsListConstants#getXsdStream(int)}
*/
private Validator getValidator(int version) throws SAXException {
InputStream xsdStream = SdkAddonsListConstants.getXsdStream(version);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
if (factory == null) {
return null;
}
// This may throw a SAX Exception if the schema itself is not a valid XSD
Schema schema = factory.newSchema(new StreamSource(xsdStream));
Validator validator = schema == null ? null : schema.newValidator();
return validator;
}
示例7: validateXSD
import javax.xml.validation.Validator; //导入依赖的package包/类
protected void validateXSD(Document signedDoc) throws SAXException, IOException {
NodeList nodeList = signedDoc.getElementsByTagNameNS(RedactableXMLSignature.XML_NAMESPACE, "Signature");
assertEquals(1, nodeList.getLength());
Node signature = nodeList.item(0);
NodeList childNodes = signature.getChildNodes();
int actualNodes = 0;
for (int i = 0; i < childNodes.getLength(); i++) {
if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
++actualNodes;
}
}
assertEquals(3, actualNodes);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File("xmlrss_schema.xsd"));
Validator validator = schema.newValidator();
validator.validate(new DOMSource(signature));
}
示例8: 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);
}
}
示例9: 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);
}
}
示例10: validateXml
import javax.xml.validation.Validator; //导入依赖的package包/类
@SneakyThrows
public void validateXml(String xsdPath, boolean namespaceAware, String schemaLanguage, String pageBody) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(namespaceAware);
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(pageBody)));
SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
Source schemaSource = new StreamSource(getClass().getResourceAsStream(xsdPath));
Schema schema = schemaFactory.newSchema(schemaSource);
Validator validator = schema.newValidator();
Source source = new DOMSource(document);
validator.setErrorHandler(new XmlErrorHandler());
validator.validate(source);
}
示例11: 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;
}
}
示例12: 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());
logger.error(Messages.IMPORT_XML_FORMAT_ERROR);
return false;
}
}
示例13: 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;
}
示例14: testSecureProcessingFeaturePropagationAndReset
import javax.xml.validation.Validator; //导入依赖的package包/类
@Test
public void testSecureProcessingFeaturePropagationAndReset() throws Exception {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
boolean value;
value = factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
//default is true for JDK
//assertFalse("Default value of feature on SchemaFactory should have been false.", value);
assertTrue("Default value of feature on SchemaFactory should have been false.", value);
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
Schema schema = makeSchema(factory, null);
Validator validator = schema.newValidator();
value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
assertTrue("Value of feature on Validator should have been true.", value);
validator.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false);
value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
assertFalse("Value of feature on Validator should have been false.", value);
validator.reset();
value = validator.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING);
assertTrue("After reset, value of feature on Validator should be true.", value);
}
示例15: testStream
import javax.xml.validation.Validator; //导入依赖的package包/类
@Test
public final void testStream() {
try {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schemaGrammar = schemaFactory.newSchema(new File(getClass().getResource("gMonths.xsd").getFile()));
Validator schemaValidator = schemaGrammar.newValidator();
Source xmlSource = new javax.xml.transform.stream.StreamSource(new File(CR6708840Test.class.getResource("gMonths.xml").toURI()));
schemaValidator.validate(xmlSource);
} catch (NullPointerException ne) {
Assert.fail("NullPointerException when result is not specified.");
} catch (Exception e) {
Assert.fail(e.getMessage());
e.printStackTrace();
}
}