當前位置: 首頁>>代碼示例>>Java>>正文


Java Schema.newValidator方法代碼示例

本文整理匯總了Java中javax.xml.validation.Schema.newValidator方法的典型用法代碼示例。如果您正苦於以下問題:Java Schema.newValidator方法的具體用法?Java Schema.newValidator怎麽用?Java Schema.newValidator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.validation.Schema的用法示例。


在下文中一共展示了Schema.newValidator方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: validation

import javax.xml.validation.Schema; //導入方法依賴的package包/類
/**
	 * 利用xsd驗證xml
	 * @param xsdFile xsdFile
	 * @param xmlInput xmlInput
	 * @throws SAXException  SAXException
	 * @throws IOException IOException
	 */
	public static void validation(String xsdFile, InputStream xmlInput) throws SAXException, IOException
	{
		SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		URL xsdURL = Validation.class.getClassLoader().getResource(xsdFile);
		if(xsdURL != null)
		{
			Schema schema = factory.newSchema(xsdURL);
			Validator validator = schema.newValidator();
//			validator.setErrorHandler(new AutoErrorHandler());

			Source source = new StreamSource(xmlInput);
			
			try(OutputStream resultOut = new FileOutputStream(new File(PathUtil.getRootDir(), xsdFile + ".xml")))
			{
				Result result = new StreamResult(resultOut);
				validator.validate(source, result);
			}
		}
		else
		{
			throw new FileNotFoundException(String.format("can not found xsd file [%s] from classpath.", xsdFile));
		}
	}
 
開發者ID:LinuxSuRen,項目名稱:phoenix.webui.framework,代碼行數:31,代碼來源:Validation.java

示例2: validate

import javax.xml.validation.Schema; //導入方法依賴的package包/類
private void validate(final String xsdFile, final Source src, final Result result) throws Exception {
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(ValidatorTest.class.getResource(xsdFile).toURI()));

        // Get a Validator which can be used to validate instance document
        // against this grammar.
        Validator validator = schema.newValidator();
        ErrorHandler eh = new ErrorHandlerImpl();
        validator.setErrorHandler(eh);

        // Validate this instance document against the
        // Instance document supplied
        validator.validate(src, result);
    } catch (Exception ex) {
        throw ex;
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:ValidatorTest.java

示例3: getValidator

import javax.xml.validation.Schema; //導入方法依賴的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

示例4: validateXSD

import javax.xml.validation.Schema; //導入方法依賴的package包/類
protected void validateXSD(Document signedDoc) throws SAXException, IOException {
    NodeList nodeList = signedDoc.getElementsByTagNameNS(RedactableXMLSignature.XML_NAMESPACE, "Signature");
    assertEquals(1, nodeList.getLength());

    Node signature = nodeList.item(0);
    NodeList childNodes = signature.getChildNodes();
    int actualNodes = 0;
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
            ++actualNodes;
        }
    }
    assertEquals(3, actualNodes);

    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new File("xmlrss_schema.xsd"));
    Validator validator = schema.newValidator();
    validator.validate(new DOMSource(signature));
}
 
開發者ID:woefe,項目名稱:xmlrss,代碼行數:20,代碼來源:AbstractXMLRSSTest.java

示例5: testParticlesOptimize

import javax.xml.validation.Schema; //導入方法依賴的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

示例6: testStAXWResult

import javax.xml.validation.Schema; //導入方法依賴的package包/類
/**
 * workaround before the fix: provide a result
 */
@Test
public final void testStAXWResult() {
    try {
        XMLInputFactory xmlif = XMLInputFactory.newInstance();

        // XMLStreamReader staxReader =
        // xmlif.createXMLStreamReader((Source)new
        // StreamSource(getClass().getResource("Forum31576.xml").getFile()));
        XMLStreamReader staxReader = xmlif.createXMLStreamReader(this.getClass().getResourceAsStream("gMonths.xml"));

        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 staxSrc = new StAXSource(staxReader);
        File resultFile = new File(USER_DIR + "gMonths.result.xml");
        if (resultFile.exists()) {
            resultFile.delete();
        }

        Result xmlResult = new javax.xml.transform.stax.StAXResult(XMLOutputFactory.newInstance().createXMLStreamWriter(new FileWriter(resultFile)));
        schemaValidator.validate(staxSrc, xmlResult);

        while (staxReader.hasNext()) {
            int eventType = staxReader.next();
            System.out.println("Event of type: " + eventType);
        }
    } catch (Exception e) {
        Assert.fail(e.getMessage());
        e.printStackTrace();
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:37,代碼來源:CR6708840Test.java

示例7: validate

import javax.xml.validation.Schema; //導入方法依賴的package包/類
public static void validate (String xmlPath, String xsdPath) throws Exception {

	// create a SchemaFactory capable of understanding WXS schemas
	prtln ("schemaNS_URI: " + XMLConstants.W3C_XML_SCHEMA_NS_URI);
	SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
	
	// load a WXS schema, represented by a Schema instance
	Schema schema = null;
	
	/* use schema from file */
	schema = factory.newSchema(new File(xsdPath));
	
	/* use schema from web */
	// String adnURI = "http://www.dlese.org/Metadata/adn-item/0.6.50/record.xsd";
	// schema = factory.newSchema(new URL(adnURI));
	
	/* use schema from instance document */
	// schema = factory.newSchema();
	
	/* create a Validator instance, which can be used to validate an instance document */
	Validator validator = schema.newValidator();
	
	/* the xml to be validated */
	Source mysource = new StreamSource (xmlPath);
	
	// validation errors passed as SAXException message
	try {
		validator.validate(mysource);
	} catch (SAXException e) {
		prtln (e.getMessage());
	}
}
 
開發者ID:NCAR,項目名稱:joai-project,代碼行數:33,代碼來源:ValidatorTester.java

示例8: test

import javax.xml.validation.Schema; //導入方法依賴的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

示例9: testValidateComplexTypeWithSimpleContent

import javax.xml.validation.Schema; //導入方法依賴的package包/類
@Test
public void testValidateComplexTypeWithSimpleContent() throws IOException, ParserConfigurationException, SAXException {
    try {
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        dFactory.setNamespaceAware(true);

        DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
        Document document = dBuilder.parse(getClass().getResourceAsStream("Bug6695843.xsd"));
        DOMSource domSource = new DOMSource(document);

        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(domSource);

        String xmlFileName = "Bug6695843.xml";
        Document document1 = dBuilder.parse(getClass().getResourceAsStream(xmlFileName));
        DOMSource domSource1 = new DOMSource(document1);

        Validator validator = schema.newValidator();
        validator.validate(domSource1);
    } catch (Exception e) {
        String msg = e.getMessage();
        System.out.println(e.getMessage());
        if (msg.indexOf("maxLength '20' for type 'null'") > 0) {
            Assert.fail("vague error message");
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,代碼來源:Bug6695843Test.java

示例10: serialize

import javax.xml.validation.Schema; //導入方法依賴的package包/類
@Test
public void serialize() throws Exception {
    // given
    SupplierRevenueShareResult result = supplierShareAssembler.build(
            SUPPLIER_KEY, PERIOD_START_TIME, PERIOD_END_TIME);
    result.calculateAllShares();

    // when
    JAXBContext jc = JAXBContext
            .newInstance(SupplierRevenueShareResult.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());

    final List<String> fragments = new ArrayList<String>();
    fragments.add(new String(bos.toByteArray(), "UTF-8"));

    byte[] xmlBytes = XMLConverter.combine("RevenueSharesResults",
            fragments, SupplierRevenueShareResult.SCHEMA);
    ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
    bos1.write(xmlBytes);
    printXml(bos1);

    SchemaFactory schemaFactory = SchemaFactory
            .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaUrl = BillingServiceBean.class.getResource("/"
            + "SupplierRevenueShareResult.xsd");
    Schema schema = schemaFactory.newSchema(schemaUrl);

    Source xmlFile = new StreamSource(new ByteArrayInputStream(xmlBytes));
    Validator validator = schema.newValidator();
    validator.validate(xmlFile);
}
 
開發者ID:servicecatalog,項目名稱:oscm,代碼行數:39,代碼來源:SupplierShareResultAssemblerTest.java

示例11: validateXML

import javax.xml.validation.Schema; //導入方法依賴的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

示例12: setUp

import javax.xml.validation.Schema; //導入方法依賴的package包/類
@BeforeMethod
public void setUp() throws Exception {
    SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

    InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(XSD.getBytes()));
    StreamSource xsdSource = new StreamSource(reader);

    Schema schema = schemaFactory.newSchema(xsdSource);

    this.validatorHandler = schema.newValidatorHandler();
    this.validator = schema.newValidator();
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:Bug5011500.java

示例13: testParticlesR005

import javax.xml.validation.Schema; //導入方法依賴的package包/類
@Test
public void testParticlesR005() {
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        String xsdFile = "particlesR005.xsd";
        Schema schema = sf.newSchema(new File(getClass().getResource(xsdFile).toURI()));
        Validator validator = schema.newValidator();
    } catch (Exception ex) {
        return; // expected
    }
    Assert.fail("Parser configuration error expected since maxOccurs > 5000 " + "and constant-space optimization does not apply");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:LargeMaxOccursTest.java

示例14: test

import javax.xml.validation.Schema; //導入方法依賴的package包/類
@Test
public void test() throws SAXException, IOException {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(new StreamSource(ParticleTest.class.getResourceAsStream("upa01.xsd")));
    Validator validator = schema.newValidator();

    validator.validate(new StreamSource(ParticleTest.class.getResourceAsStream("upa01.xml")));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:9,代碼來源:ParticleTest.java

示例15: testFeatureReset

import javax.xml.validation.Schema; //導入方法依賴的package包/類
@Test
public void testFeatureReset() throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = makeSchema(factory, null);
    Validator validator = schema.newValidator();
    validator.setFeature(FEATURE_STRING_DEFAULT_TRUE, false);
    validator.setFeature(FEATURE_STRING_DEFAULT_FALSE, true);
    validator.reset();
    boolean value = validator.getFeature(FEATURE_STRING_DEFAULT_TRUE);
    assertTrue("After reset, value of feature on Validator should be true.", value);
    value = validator.getFeature(FEATURE_STRING_DEFAULT_FALSE);
    assertFalse("After reset, value of feature on Validator should be false.", value);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:FeaturePropagationTest.java


注:本文中的javax.xml.validation.Schema.newValidator方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。