本文整理汇总了Java中javax.xml.validation.SchemaFactory类的典型用法代码示例。如果您正苦于以下问题:Java SchemaFactory类的具体用法?Java SchemaFactory怎么用?Java SchemaFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SchemaFactory类属于javax.xml.validation包,在下文中一共展示了SchemaFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Test
public void test() throws SAXException, ParserConfigurationException, IOException {
Schema schema = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(new StreamSource(new StringReader(xmlSchema)));
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
saxParserFactory.setSchema(schema);
// saxParserFactory.setFeature("http://java.sun.com/xml/schema/features/report-ignored-element-content-whitespace",
// true);
SAXParser saxParser = saxParserFactory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new MyContentHandler());
// InputStream input =
// ClassLoader.getSystemClassLoader().getResourceAsStream("test/test.xml");
InputStream input = getClass().getResourceAsStream("Bug6946312.xml");
System.out.println("Parse InputStream:");
xmlReader.parse(new InputSource(input));
if (!charEvent) {
Assert.fail("missing character event");
}
}
示例2: parse
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
* Parse the given XML string an create/update the corresponding entities
*
* @param xml
* the XML string
* @return the parse return code
* @throws Exception
*/
public int parse(byte[] xml) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
SchemaFactory sf = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try (InputStream inputStream = ResourceLoader.getResourceAsStream(
getClass(), getSchemaName())) {
Schema schema = sf.newSchema(new StreamSource(inputStream));
spf.setSchema(schema);
}
SAXParser saxParser = spf.newSAXParser();
XMLReader reader = saxParser.getXMLReader();
reader.setFeature(Constants.XERCES_FEATURE_PREFIX
+ Constants.DISALLOW_DOCTYPE_DECL_FEATURE, true);
reader.setContentHandler(this);
reader.parse(new InputSource(new ByteArrayInputStream(xml)));
return 0;
}
示例3: rulesXmlIsValid
import javax.xml.validation.SchemaFactory; //导入依赖的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!");
}
}
示例4: fromXML
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Override
public T fromXML(String xml) {
try {
JAXBContext context = JAXBContext.newInstance(type);
Unmarshaller u = context.createUnmarshaller();
if(schemaLocation != null) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource source = new StreamSource(getClass().getResourceAsStream(schemaLocation));
Schema schema = schemaFactory.newSchema(source);
u.setSchema(schema);
}
StringReader reader = new StringReader(xml);
T obj = (T) u.unmarshal(reader);
return obj;
} catch (Exception e) {
System.out.println("ERROR: "+e.toString());
return null;
}
}
示例5: test
import javax.xml.validation.SchemaFactory; //导入依赖的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());
}
}
}
示例6: getCompiledSchema
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
* Subclasses can use this to get a compiled schema object.
* @param schemas Input stream of schemas.
* @param lsResourceResolver resolver can be supplied optionally. Otherwise pass null.
* @return Compiled Schema object.
*/
protected Schema getCompiledSchema(InputStream[] schemas,
LSResourceResolver lsResourceResolver) {
Schema schema = null;
// Convert InputStream[] to StreamSource[]
StreamSource[] schemaStreamSources = new StreamSource[schemas.length];
for(int index1=0 ; index1<schemas.length ; index1++)
schemaStreamSources[index1] = new StreamSource(schemas[index1]);
// Create a compiled Schema object.
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
schemaFactory.setResourceResolver(lsResourceResolver);
try {
schema = schemaFactory.newSchema(schemaStreamSources);
} catch(SAXException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "getCompiledSchema", ex);
}
return schema;
}
示例7: validate
import javax.xml.validation.SchemaFactory; //导入依赖的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());
}
}
示例8: validation
import javax.xml.validation.SchemaFactory; //导入依赖的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));
}
}
示例9: XMLValidator
import javax.xml.validation.SchemaFactory; //导入依赖的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();
}
示例10: setConfFile
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
public void setConfFile(String confFile) throws Exception {
this.confFile = confFile;
Object root;
try {
JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
final SchemaFactory schemaFact = SchemaFactory.newInstance(
javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL url = ObjectFactory.class.getResource("/xsd/httpserver.xsd");
jaxbUnmarshaller.setSchema(schemaFact.newSchema(url));
root = jaxbUnmarshaller.unmarshal(new File(confFile));
} catch (Exception ex) {
throw new Exception("parsing config file failed, message: " + ex.getMessage(), ex);
}
if (root instanceof Httpservers) {
this.conf = (Httpservers) root;
} else if (root instanceof JAXBElement) {
this.conf = (Httpservers) ((JAXBElement<?>) root).getValue();
} else {
throw new Exception("invalid root element type");
}
}
示例11: validate
import javax.xml.validation.SchemaFactory; //导入依赖的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: loadSchemaFiles
import javax.xml.validation.SchemaFactory; //导入依赖的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);
}
}
示例13: toXML
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@Override
public String toXML(T obj) {
try {
JAXBContext context = JAXBContext.newInstance(type);
Marshaller m = context.createMarshaller();
if(schemaLocation != null) {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
StreamSource source = new StreamSource(getClass().getResourceAsStream(schemaLocation));
Schema schema = schemaFactory.newSchema(source);
m.setSchema(schema);
}
StringWriter writer = new StringWriter();
m.marshal(obj, writer);
String xml = writer.toString();
return xml;
} catch (Exception e) {
System.out.println("ERROR: "+e.toString());
return null;
}
}
示例14: XMLConfigurator
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
/**
* Constructor.
*
* @param retainXML whether to retain the XML configuration elements within the {@link Configuration}.
*
* @throws ConfigurationException thrown if the validation schema for configuration files can not be created
*
* @deprecated this method will be removed once {@link Configuration} no longer has the option to store the XML configuration fragements
*/
public XMLConfigurator(boolean retainXML) throws ConfigurationException {
retainXMLConfiguration = retainXML;
parserPool = new BasicParserPool();
SchemaFactory factory = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source schemaSource = new StreamSource(XMLConfigurator.class
.getResourceAsStream(XMLConstants.XMLTOOLING_SCHEMA_LOCATION));
try {
configurationSchema = factory.newSchema(schemaSource);
parserPool.setIgnoreComments(true);
parserPool.setIgnoreElementContentWhitespace(true);
parserPool.setSchema(configurationSchema);
} catch (SAXException e) {
throw new ConfigurationException("Unable to read XMLTooling configuration schema", e);
}
}
示例15: initialize
import javax.xml.validation.SchemaFactory; //导入依赖的package包/类
@PostConstruct
@Override
public synchronized void initialize() throws ProviderFactoryException {
if (providersHolder.get() == null) {
final File providersConfigFile = properties.getProvidersConfigurationFile();
if (providersConfigFile.exists()) {
try {
// find the schema
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final Schema schema = schemaFactory.newSchema(StandardProviderFactory.class.getResource(PROVIDERS_XSD));
// attempt to unmarshal
final Unmarshaller unmarshaller = JAXB_CONTEXT.createUnmarshaller();
unmarshaller.setSchema(schema);
// set the holder for later use
final JAXBElement<Providers> element = unmarshaller.unmarshal(new StreamSource(providersConfigFile), Providers.class);
providersHolder.set(element.getValue());
} catch (SAXException | JAXBException e) {
throw new ProviderFactoryException("Unable to load the providers configuration file at: " + providersConfigFile.getAbsolutePath(), e);
}
} else {
throw new ProviderFactoryException("Unable to find the providers configuration file at " + providersConfigFile.getAbsolutePath());
}
}
}