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


Java Validator類代碼示例

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


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

示例1: rulesXmlIsValid

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

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

示例3: validateWithXMLSchema

import javax.xml.validation.Validator; //導入依賴的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();

    try {
        validator.validate(new StreamSource(xml));
    } catch (SAXException e) {
        throw new GeneralException(e);
    }
}
 
開發者ID:LIBCAS,項目名稱:ARCLib,代碼行數:27,代碼來源:ValidationChecker.java

示例4: define

import javax.xml.validation.Validator; //導入依賴的package包/類
@Override
public void define(Context context) {
	try (
		Reader xmlStreamValidationReader = xmlFactory.newRulesXmlReader();
		Reader xmlStreamRulesDefinitionReader = xmlFactory.newRulesXmlReader();
		Reader xsdStreamReader = xmlFactory.newRulesXsdReader()
	) {
		StreamSource xsdStreamSource = new StreamSource(xsdStreamReader);
		StreamSource xmlStreamSource = new StreamSource(xmlStreamValidationReader);

		SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = schemaFactory.newSchema(xsdStreamSource);
		Validator validator = schema.newValidator();

		validator.validate(xmlStreamSource);

		NewRepository repo = context.createRepository(repositoryKey(), "php").setName(repositoryName());
		RulesDefinitionXmlLoader xmlLoader = new RulesDefinitionXmlLoader();

		xmlLoader.load(repo, xmlStreamRulesDefinitionReader);
		repo.done();
	} catch (Exception e) {
		throw new IllegalStateException("rules.xml not found or invalid", e);
	}
}
 
開發者ID:Wikia,項目名稱:sonar-php-rules,代碼行數:26,代碼來源:PHPRuleDefinitions.java

示例5: getValidator

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

示例6: getValidator

import javax.xml.validation.Validator; //導入依賴的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 SdkAddonsListConstants#getXsdStream(int)}
 */
private Validator getValidator(int version) throws SAXException {
    InputStream xsdStream = SdkAddonsListConstants.getXsdStream(version);
    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;
}
 
開發者ID:tranleduy2000,項目名稱:javaide,代碼行數:23,代碼來源:AddonsListFetcher.java

示例7: validateXSD

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

示例8: testStringTemplate

import javax.xml.validation.Validator; //導入依賴的package包/類
public void testStringTemplate() throws Exception {

        StringTemplate t1 = StringTemplate.template("model.goods.goodsAmount")
            .add("%goods%", "model.goods.food.name")
            .addName("%amount%", "100");
        StringTemplate t2 = StringTemplate.template("model.goods.goodsAmount")
            .addAmount("%amount%", 50)
            .addStringTemplate("%goods%", t1);

        Game game = getGame();
        Player player = game.getPlayerByNationId("model.nation.dutch");

        try {
            Validator validator = buildValidator("schema/data/data-stringTemplate.xsd");
            validator.validate(buildSource(t2));
        } catch (SAXParseException e){
            String errMsg = e.getMessage()
                + " at line=" + e.getLineNumber()
                + " column=" + e.getColumnNumber();
            fail(errMsg);
        }
    }
 
開發者ID:FreeCol,項目名稱:freecol,代碼行數:23,代碼來源:SerializationTest.java

示例9: testSpecification

import javax.xml.validation.Validator; //導入依賴的package包/類
public void testSpecification() throws Exception {
    try {
        String filename = "test/data/specification.xml";
        Validator validator = buildValidator("schema/specification-schema.xsd");
        FileOutputStream fos = new FileOutputStream(filename);
        try (FreeColXMLWriter xw = new FreeColXMLWriter(fos, null, false)) {
            spec().toXML(xw);
        } catch (IOException ioe) {
            fail(ioe.getMessage());
        }

        validator.validate(new StreamSource(new FileReader(filename)));
    } catch (SAXParseException e) {
        String errMsg = e.getMessage()
            + " at line=" + e.getLineNumber()
            + " column=" + e.getColumnNumber();
        fail(errMsg);
    }

}
 
開發者ID:FreeCol,項目名稱:freecol,代碼行數:21,代碼來源:SerializationTest.java

示例10: validateXml

import javax.xml.validation.Validator; //導入依賴的package包/類
@SneakyThrows
public void validateXml(String xsdPath, boolean namespaceAware, String schemaLanguage, String pageBody) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setNamespaceAware(namespaceAware);

    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(pageBody)));

    SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
    Source schemaSource = new StreamSource(getClass().getResourceAsStream(xsdPath));

    Schema schema = schemaFactory.newSchema(schemaSource);
    Validator validator = schema.newValidator();
    Source source = new DOMSource(document);
    validator.setErrorHandler(new XmlErrorHandler());
    validator.validate(source);
}
 
開發者ID:christian-draeger,項目名稱:page-content-tester,代碼行數:19,代碼來源:XmlErrorHandler.java

示例11: validateXML

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

示例12: validateXML

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

示例13: validateDAT

import javax.xml.validation.Validator; //導入依賴的package包/類
public static boolean validateDAT(File xmlFile) {
    try {
        SchemaFactory factory =
                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File(DATschemaURL.getPath()));
        Validator validator = schema.newValidator();
        validator.validate(new StreamSource(xmlFile));
    } catch (IOException | SAXException e) {
        System.out.println("Exception: " + e.getMessage());
        return false;
    }
    return true;
}
 
開發者ID:phweda,項目名稱:MFM,代碼行數:14,代碼來源:MFM_DATmaker.java

示例14: testSecureProcessingFeaturePropagationAndReset

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

示例15: testStream

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


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