當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。