本文整理汇总了Java中javax.xml.validation.Schema类的典型用法代码示例。如果您正苦于以下问题:Java Schema类的具体用法?Java Schema怎么用?Java Schema使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Schema类属于javax.xml.validation包,在下文中一共展示了Schema类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testValidatorTest
import javax.xml.validation.Schema; //导入依赖的package包/类
@Test
public void testValidatorTest() throws Exception {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
String file = getClass().getResource("types.xsd").getFile();
Source[] sources = new Source[] { new StreamSource(new FileInputStream(file), file) };
Schema schema = sf.newSchema(sources);
validator = schema.newValidator();
validate();
} catch (Exception e) {
Node node = (Node) validator.getProperty("http://apache.org/xml/properties/dom/current-element-node");
if (node != null) {
System.out.println("Node: " + node.getLocalName());
} else
Assert.fail("No node returned");
}
}
示例2: getCompiledSchema
import javax.xml.validation.Schema; //导入依赖的package包/类
protected Schema getCompiledSchema(Source[] schemas,
LSResourceResolver lsResourceResolver,
ErrorHandler errorHandler) {
Schema schema = null;
// Create a compiled Schema object.
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setResourceResolver(lsResourceResolver);
schemaFactory.setErrorHandler(errorHandler);
try {
schema = schemaFactory.newSchema(schemas);
} catch(SAXException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCompiledSchema", ex);
}
return schema;
}
示例3: getSchema
import javax.xml.validation.Schema; //导入依赖的package包/类
protected Schema getSchema(Model model) {
if (! (model instanceof SchemaModel)) {
return null;
}
// This will not be used as validate(.....) method is being overridden here.
// So just return a schema returned by newSchema().
if(schema == null) {
try {
schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema();
} catch(SAXException ex) {
assert false: "Error while creating compiled schema for"; //NOI18N
}
}
return schema;
}
示例4: validate
import javax.xml.validation.Schema; //导入依赖的package包/类
@Override
protected void validate(Model model, Schema schema, XsdBasedValidator.Handler handler) {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
CatalogModel cm = (CatalogModel) model.getModelSource().getLookup()
.lookup(CatalogModel.class);
if (cm != null) {
sf.setResourceResolver(cm);
}
sf.setErrorHandler(handler);
Source saxSource = getSource(model, handler);
if (saxSource == null) {
return;
}
sf.newSchema(saxSource);
} catch(SAXException sax) {
//already processed by handler
} catch(Exception ex) {
handler.logValidationErrors(Validator.ResultType.ERROR, ex.getMessage());
}
}
示例5: validate
import javax.xml.validation.Schema; //导入依赖的package包/类
/**
* Check whether a DOM tree is valid according to a schema.
* Example of usage:
* <pre>
* Element fragment = ...;
* SchemaFactory f = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
* Schema s = f.newSchema(This.class.getResource("something.xsd"));
* try {
* XMLUtil.validate(fragment, s);
* // valid
* } catch (SAXException x) {
* // invalid
* }
* </pre>
* @param data a DOM tree
* @param schema a parsed schema
* @throws SAXException if validation failed
* @since org.openide.util 7.17
*/
public static void validate(Element data, Schema schema) throws SAXException {
Validator v = schema.newValidator();
final SAXException[] error = {null};
v.setErrorHandler(new ErrorHandler() {
public @Override void warning(SAXParseException x) throws SAXException {}
public @Override void error(SAXParseException x) throws SAXException {
// Just rethrowing it is bad because it will also print it to stderr.
error[0] = x;
}
public @Override void fatalError(SAXParseException x) throws SAXException {
error[0] = x;
}
});
try {
v.validate(new DOMSource(fixupAttrs(data)));
} catch (IOException x) {
assert false : x;
}
if (error[0] != null) {
throw error[0];
}
}
示例6: validation
import javax.xml.validation.Schema; //导入依赖的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));
}
}
示例7: XMLValidator
import javax.xml.validation.Schema; //导入依赖的package包/类
/**
* Constructor for the XMLValidator object
*
* @param uri NOT YET DOCUMENTED
* @exception Exception NOT YET DOCUMENTED
*/
public XMLValidator(URI uri) throws Exception {
this.uri = uri;
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
String uriScheme = uri.getScheme();
if (uriScheme != null && uriScheme.equals("http"))
schema = factory.newSchema(uri.toURL());
else
schema = factory.newSchema(new File(uri));
if (schema == null)
throw new Exception("Schema could not be read from " + uri.toString());
} catch (Throwable t) {
throw new Exception("Validator init error: " + t.getMessage());
}
this.validator = schema.newValidator();
}
示例8: ValidatorTester
import javax.xml.validation.Schema; //导入依赖的package包/类
public ValidatorTester (String schemaURI) throws Exception {
this.schemaURI = schemaURI;
prtln ("schemaNS_URI: " + XMLConstants.W3C_XML_SCHEMA_NS_URI);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
if (schemaURI.startsWith("http://"))
schema = factory.newSchema(new URL (schemaURI));
else
schema = factory.newSchema(new File (schemaURI));
if (schema == null)
throw new Exception ("Schema could not be read from " + schemaURI);
} catch (Throwable t) {
throw new Exception ("Validator init error: " + t.getMessage());
}
this.validator = schema.newValidator();
}
示例9: verifyXML
import javax.xml.validation.Schema; //导入依赖的package包/类
private void verifyXML(byte[] xml) throws IOException, SAXException,
ParserConfigurationException {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
if (classLoader == null) {
classLoader = getClass().getClassLoader();
}
final Schema schema;
try (InputStream inputStream = ResourceLoader.getResourceAsStream(
getClass(), "TechnicalServices.xsd")) {
schema = sf.newSchema(new StreamSource(inputStream));
}
spf.setSchema(schema);
SAXParser saxParser = spf.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
ErrorHandler errorHandler = new MyErrorHandler();
reader.setErrorHandler(errorHandler);
reader.parse(new InputSource(new ByteArrayInputStream(xml)));
}
示例10: testStream
import javax.xml.validation.Schema; //导入依赖的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();
}
}
示例11: loadSchemaFiles
import javax.xml.validation.Schema; //导入依赖的package包/类
@Override
public Schema loadSchemaFiles() {
try (InputStream brStream = ResourceLoader.getResourceAsStream(
BillingDataRetrievalServiceBean.class, "BillingResult.xsd");
InputStream localeStream = ResourceLoader.getResourceAsStream(
BillingDataRetrievalServiceBean.class, "Locale.xsd")) {
URL billingResultUri = ResourceLoader.getResource(
BillingDataRetrievalServiceBean.class, "BillingResult.xsd");
URL localeUri = ResourceLoader.getResource(
BillingDataRetrievalServiceBean.class, "Locale.xsd");
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource[] sourceDocuments = new StreamSource[] {
new StreamSource(localeStream, localeUri.getPath()),
new StreamSource(brStream, billingResultUri.getPath()) };
return schemaFactory.newSchema(sourceDocuments);
} catch (SAXException | IOException e) {
throw new BillingRunFailed("Schema files couldn't be loaded", e);
}
}
示例12: test1
import javax.xml.validation.Schema; //导入依赖的package包/类
@Test
public void test1() throws Exception {
String xsd = "<?xml version='1.0'?>\n" + "<schema xmlns='http://www.w3.org/2001/XMLSchema'\n" + " xmlns:test='jaxp13_test1'\n"
+ " targetNamespace='jaxp13_test1'\n" + " elementFormDefault='qualified'>\n" + " <element name='test'/>\n" + "</schema>\n";
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Node document = docBuilder.parse(new InputSource(new StringReader(xsd)));
Assert.assertNotNull(document);
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = schemaFactory.newSchema(new Source[] { new DOMSource(document) });
Assert.assertNotNull(schema, "Failed: newSchema returned null.");
}
示例13: test
import javax.xml.validation.Schema; //导入依赖的package包/类
@Test
public void test() {
try {
File dir = new File(Bug6975265Test.class.getResource("Bug6975265").getPath());
File files[] = dir.listFiles();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
for (int i = 0; i < files.length; i++) {
try {
System.out.println(files[i].getName());
Schema schema = schemaFactory.newSchema(new StreamSource(files[i]));
Assert.fail("should report error");
} catch (org.xml.sax.SAXParseException spe) {
System.out.println(spe.getMessage());
continue;
}
}
} catch (SAXException e) {
e.printStackTrace();
}
}
示例14: test
import javax.xml.validation.Schema; //导入依赖的package包/类
@Test
public void test() {
String dir = Bug6943252Test.class.getResource("Bug6943252In").getPath();
File inputs = new File(dir);
File[] files = inputs.listFiles();
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
for (int i = 0; i < files.length; i++) {
try {
Schema schema = schemaFactory.newSchema(new StreamSource(files[i]));
Assert.fail(files[i].getName() + "should fail");
} catch (SAXException e) {
// expected
System.out.println(files[i].getName() + ":");
System.out.println(e.getMessage());
}
}
}
示例15: main
import javax.xml.validation.Schema; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
try{
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(System.getProperty("test.src", "."), XSDFILE));
} catch (SAXException e) {
throw new RuntimeException(e.getMessage());
}
}