本文整理匯總了Java中javax.xml.validation.SchemaFactory.newSchema方法的典型用法代碼示例。如果您正苦於以下問題:Java SchemaFactory.newSchema方法的具體用法?Java SchemaFactory.newSchema怎麽用?Java SchemaFactory.newSchema使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.validation.SchemaFactory
的用法示例。
在下文中一共展示了SchemaFactory.newSchema方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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!");
}
}
示例2: 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;
}
示例3: validateXML
import javax.xml.validation.SchemaFactory; //導入方法依賴的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;
}
}
示例4: checkSchemaCorrectness
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
/**
* Checks the correctness of the XML Schema documents and return true
* if it's OK.
*
* <p>
* This method performs a weaker version of the tests where error messages
* are provided without line number information. So whenever possible
* use {@link SchemaConstraintChecker}.
*
* @see SchemaConstraintChecker
*/
public boolean checkSchemaCorrectness(ErrorReceiver errorHandler) {
try {
boolean disableXmlSecurity = false;
if (options != null) {
disableXmlSecurity = options.disableXmlSecurity;
}
SchemaFactory sf = XmlFactory.createSchemaFactory(W3C_XML_SCHEMA_NS_URI, disableXmlSecurity);
ErrorReceiverFilter filter = new ErrorReceiverFilter(errorHandler);
sf.setErrorHandler(filter);
Set<String> roots = getRootDocuments();
Source[] sources = new Source[roots.size()];
int i=0;
for (String root : roots) {
sources[i++] = new DOMSource(get(root),root);
}
sf.newSchema(sources);
return !filter.hadError();
} catch (SAXException e) {
// the errors should have been reported
return false;
}
}
示例5: testStream
import javax.xml.validation.SchemaFactory; //導入方法依賴的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();
}
}
示例6: getValidator
import javax.xml.validation.SchemaFactory; //導入方法依賴的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) {}
}
}
}
示例7: test
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
@Test
public void test() throws Exception {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source[] sources = getSchemaSources();
Schema schema = sf.newSchema(sources);
Validator validator = schema.newValidator();
}
示例8: setSchema
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
private void setSchema(Unmarshaller u, File schemaLocation) throws Exception {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URI schemaURI = schemaLocation.toURI();
File schemaFile = new File(schemaURI.getPath());
Schema schema = sf.newSchema(schemaFile);
u.setSchema(schema);
}
示例9: validateWithXMLSchema
import javax.xml.validation.SchemaFactory; //導入方法依賴的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();
validator.validate(new StreamSource(xml));
}
示例10: testGetOwnerInfo
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
/**
* Check validation API features. A schema which is including in Bug 4909119
* used to be testing for the functionalities.
*
* @throws Exception If any errors occur.
* @see <a href="content/userDetails.xsd">userDetails.xsd</a>
*/
@Test
public void testGetOwnerInfo() throws Exception {
String schemaFile = XML_DIR + "userDetails.xsd";
String xmlFile = XML_DIR + "userDetails.xml";
try(FileInputStream fis = new FileInputStream(xmlFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI);
SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(Paths.get(schemaFile).toFile());
Validator validator = schema.newValidator();
MyErrorHandler eh = new MyErrorHandler();
validator.setErrorHandler(eh);
DocumentBuilder docBuilder = dbf.newDocumentBuilder();
docBuilder.setErrorHandler(eh);
Document document = docBuilder.parse(fis);
DOMResult dResult = new DOMResult();
DOMSource domSource = new DOMSource(document);
validator.validate(domSource, dResult);
assertFalse(eh.isAnyError());
}
}
示例11: test1
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
@Test
public void test1() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
File tmpFile = File.createTempFile("jaxpri", "bug", Paths.get(USER_DIR).toFile());
tmpFile.deleteOnExit();
{
PrintWriter pw = new PrintWriter(new FileWriter(tmpFile));
pw.println("<schema xmlns='http://www.w3.org/2001/XMLSchema'/>");
pw.close();
}
schemaFactory.newSchema(tmpFile);
}
示例12: serialize
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
@Test
public void serialize() throws Exception {
// given
ResellerRevenueShareResult result = resellerShareAssembler.build(
RESELLER_KEY, PERIOD_START_TIME, PERIOD_END_TIME);
result.calculateAllShares();
// when
JAXBContext jc = JAXBContext
.newInstance(ResellerRevenueShareResult.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
marshaller.marshal(result, bos);
assertNotNull(bos.toByteArray());
printXml(bos);
final List<String> fragments = new ArrayList<String>();
fragments.add(new String(bos.toByteArray(), "UTF-8"));
byte[] xmlBytes = XMLConverter.combine("RevenueSharesResults",
fragments, ResellerRevenueShareResult.SCHEMA);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
bos1.write(xmlBytes);
System.out.println(new String(bos1.toByteArray()));
SchemaFactory schemaFactory = SchemaFactory
.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
URL schemaUrl = BillingServiceBean.class.getResource("/"
+ "ResellerRevenueShareResult.xsd");
Schema schema = schemaFactory.newSchema(schemaUrl);
Source xmlFile = new StreamSource(new ByteArrayInputStream(xmlBytes));
Validator validator = schema.newValidator();
validator.validate(xmlFile);
}
示例13: testParticlesOptimize
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
@Test
public void testParticlesOptimize() {
try {
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
String xsdFile = "particlesOptimize.xsd";
Schema schema = sf.newSchema(new File(getClass().getResource(xsdFile).toURI()));
Validator validator = schema.newValidator();
} catch (Exception ex) {
Assert.fail("Parser configuration error not expected since maxOccurs " + "> 5000 but constant-space optimization applies");
}
}
示例14: validateXML
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
/**
* Validates the XML data against the specified schema.
*
* @param schemaFileURL
* The URL to the schema file.
* @param xmlContent
* The XML data to be validated.
* @throws SAXException
* @throws IOException
* @throws TransformerException
*/
public static void validateXML(URL schemaFileURL, Document xmlContent)
throws SAXException, IOException, TransformerException {
SchemaFactory factory = SchemaFactory
.newInstance("http://www.w3.org/2001/XMLSchema");
Schema schema = factory.newSchema(schemaFileURL);
Validator validator = schema.newValidator();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
printDocument(xmlContent, byteArrayOutputStream);
ByteArrayInputStream bis = new ByteArrayInputStream(
byteArrayOutputStream.toByteArray());
validator.validate(new StreamSource(bis));
}
示例15: parseConfigurationFile
import javax.xml.validation.SchemaFactory; //導入方法依賴的package包/類
/**
* Loads the configurationFile using JAXB.
* The XML is validated as per the schema 'com/mirotechnologies/interfaces/transform/meta/ws_xlst-config.xsd'.
* The XML is then parsed to return a corresponding Java object.
* @return the Java representation of the XML inside the configuration file.
* @throws MalformedURLException if the configuration file is not found.
* @throws JAXBException if any error occurs during the unmarshalling of XML.
* @throws SAXException if the schema file cannot be loaded.
*/
private Config parseConfigurationFile()
throws MalformedURLException, JAXBException, SAXException {
if (log.isDebugEnabled()) {
log.debug("Unmarshalling the configuration file " + CONFIGURATION_FILE);
}
URL configFileUrl = URLHelper.newExtendedURL(CONFIGURATION_FILE);
URL configSchemaFileUrl = URLHelper.newExtendedURL(CONFIGURATION_SCHEMA_FILE);
JAXBContext jc = JAXBHelper.obtainJAXBContext(Config.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(configSchemaFileUrl);
unmarshaller.setSchema(schema);
return (Config) unmarshaller.unmarshal(configFileUrl);
}