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


Java SchemaFactory.newSchema方法代码示例

本文整理汇总了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!");
	}
}
 
开发者ID:Wikia,项目名称:sonar-php-rules,代码行数:18,代码来源:IntegrationTest.java

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

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

示例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) {}
        }
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:31,代码来源:SdkStats.java

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

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

示例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));
}
 
开发者ID:LIBCAS,项目名称:ARCLib,代码行数:23,代码来源:ArclibXmlValidator.java

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

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

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

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

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

示例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));
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:24,代码来源:XMLValidation.java

示例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);
}
 
开发者ID:jaffa-projects,项目名称:jaffa-framework,代码行数:24,代码来源:MetaDataRepository.java


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