当前位置: 首页>>代码示例>>Java>>正文


Java SchemaFactory.newInstance方法代码示例

本文整理汇总了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;
    }
}
 
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:25,代码来源:ConverterImpl.java

示例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;
	}
}
 
开发者ID:capitalone,项目名称:Hydrograph,代码行数:19,代码来源:GridRowLoader.java

示例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;
 }
}
 
开发者ID:capitalone,项目名称:Hydrograph,代码行数:22,代码来源:ELTSchemaGridWidget.java

示例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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:CR6708840Test.java

示例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();
}
 
开发者ID:NCAR,项目名称:joai-project,代码行数:20,代码来源:ValidatorTester.java

示例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;
}
 
开发者ID:Yarichi,项目名称:Proyecto-DASI,代码行数:28,代码来源:SchemaHelper.java

示例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));
}
 
开发者ID:oasp,项目名称:oasp-tutorial-sources,代码行数:30,代码来源:AccessControlSchemaXmlValidationTest.java

示例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
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:Bug4971607.java

示例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);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:9,代码来源:ValidationWarningsTest.java

示例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;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:34,代码来源:Bug4997818.java

示例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);
    }
}
 
开发者ID:MinBZK,项目名称:OperatieBRP,代码行数:20,代码来源:SchemaValidatorService.java

示例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
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:Bug4970383.java

示例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
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:Bug5010072.java

示例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);

}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:41,代码来源:BrokerShareResultAssemblerTest.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:JaxpIssue43Test.java


注:本文中的javax.xml.validation.SchemaFactory.newInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。