本文整理汇总了Java中javax.xml.validation.SchemaFactory.newInstance方法的典型用法代码示例。如果您正苦于以下问题:Java SchemaFactory.newInstance方法的具体用法?Java SchemaFactory.newInstance怎么用?Java SchemaFactory.newInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.validation.SchemaFactory
的用法示例。
在下文中一共展示了SchemaFactory.newInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例2: 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());
logger.error(Messages.IMPORT_XML_FORMAT_ERROR);
return false;
}
}
示例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: 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();
}
}
示例5: ValidatorTester
import javax.xml.validation.SchemaFactory; //导入方法依赖的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();
}
示例6: deserialiseObject
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
/** Attempt to construct the specified object from this XML string
* @param xml the XML string to parse
* @param xsdFile the name of the XSD schema that defines the object
* @param objclass the class of the object requested
* @return if successful, an instance of class objclass that captures the data in the XML string
*/
static public Object deserialiseObject(String xml, String xsdFile, Class<?> objclass) throws JAXBException, SAXException, XMLStreamException
{
Object obj = null;
JAXBContext jaxbContext = getJAXBContext(objclass);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
final String schemaResourceFilename = new String(xsdFile);
URL schemaURL = MalmoMod.class.getClassLoader().getResource(schemaResourceFilename);
Schema schema = schemaFactory.newSchema(schemaURL);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
StringReader stringReader = new StringReader(xml);
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader XMLreader = xif.createXMLStreamReader(stringReader);
obj = jaxbUnmarshaller.unmarshal(XMLreader);
return obj;
}
示例7: validateAccessControllSchema
import javax.xml.validation.SchemaFactory; //导入方法依赖的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));
}
示例8: test1
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
@Test
public void test1() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
try {
validatorHandler.getFeature(null);
Assert.fail();
} catch (NullPointerException e) {
e.printStackTrace();
; // as expected
}
}
示例9: doOneTestIteration
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
void doOneTestIteration() throws Exception {
Source src = new StreamSource(new StringReader(xml));
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
SAXSource xsdSource = new SAXSource(new InputSource(new ByteArrayInputStream(xsd.getBytes())));
Schema schema = schemaFactory.newSchema(xsdSource);
Validator v = schema.newValidator();
v.validate(src);
}
示例10: test1
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
@Test
public void test1() throws Exception {
String xsd1 = "<?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" + " <import namespace='jaxp13_test2'/>\n"
+ " <element name='test'/>\n" + " <element name='child1'/>\n" + "</schema>\n";
final NullPointerException EUREKA = new NullPointerException("NewSchema015");
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
StringReader reader = new StringReader(xsd1);
StreamSource source = new StreamSource(reader);
LSResourceResolver resolver = new LSResourceResolver() {
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
LSInput input;
if (namespaceURI != null && namespaceURI.endsWith("jaxp13_test2")) {
throw EUREKA;
} else {
input = null;
}
return input;
}
};
schemaFactory.setResourceResolver(resolver);
try {
schemaFactory.newSchema(new Source[] { source });
Assert.fail("NullPointerException was not thrown.");
} catch (RuntimeException e) {
if (e != EUREKA)
throw e;
}
}
示例11: maakSchema
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
/**
* Maakt een {@link Schema} voor het gegeven bestand.
*
* @param classpathFile een schema bestand
* @return een {@link Schema} instantie.
* @throws SchemaNietGevondenException als het schema niet geladen kan worden
*/
static Schema maakSchema(final String classpathFile) {
final SchemaFactory fact = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
final URL resource = SchemaValidatorService.class.getResource(classpathFile);
if (resource == null) {
throw new IOException("Bestand bestaat niet: " + classpathFile);
}
return fact.newSchema(resource.toURI().toURL());
} catch (SAXException | URISyntaxException | IOException e) {
throw new SchemaNietGevondenException(classpathFile, e);
}
}
示例12: test
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
try {
validatorHandler.setFeature(null, false);
Assert.fail("should report an error");
} catch (NullPointerException e) {
; // expected
}
}
示例13: test1
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
@Test
public void test1() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
ErrorHandler errorHandler = new ErrorHandler();
schemaFactory.setErrorHandler(errorHandler);
try {
schemaFactory.newSchema(Bug5010072.class.getResource("Bug5010072.xsd"));
Assert.fail("should fail to compile");
} catch (SAXException e) {
; // as expected
}
}
示例14: serialize
import javax.xml.validation.SchemaFactory; //导入方法依赖的package包/类
@Test
public void serialize() throws Exception {
// given
BrokerRevenueShareResult result = brokerShareAssembler.build(
BROKER_KEY, PERIOD_START_TIME, PERIOD_END_TIME);
result.calculateAllShares();
// when
JAXBContext jc = JAXBContext
.newInstance(BrokerRevenueShareResult.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, BrokerRevenueShareResult.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("/"
+ "BrokerRevenueShareResult.xsd");
Schema schema = schemaFactory.newSchema(schemaUrl);
Source xmlFile = new StreamSource(new ByteArrayInputStream(xmlBytes));
Validator validator = schema.newValidator();
validator.validate(xmlFile);
}
示例15: 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();
}