本文整理汇总了Java中javax.xml.validation.ValidatorHandler类的典型用法代码示例。如果您正苦于以下问题:Java ValidatorHandler类的具体用法?Java ValidatorHandler怎么用?Java ValidatorHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ValidatorHandler类属于javax.xml.validation包,在下文中一共展示了ValidatorHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: newValidator
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
public ValidatorHandler newValidator() {
synchronized(this) {
if(schema==null) {
try {
// do not disable secure processing - these are well-known schemas
SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
schema = allowExternalAccess(sf, "file", false).newSchema(source);
} catch (SAXException e) {
// we make sure that the schema is correct before we ship.
throw new AssertionError(e);
}
}
}
ValidatorHandler handler = schema.newValidatorHandler();
return handler;
}
示例2: apply
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
/**
* Applies the additional binding customizations.
*/
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
if(topLevel!=null) {
this.errorReceiver = errorReceiver;
Unmarshaller u = BindInfo.getCustomizationUnmarshaller();
this.unmarshaller = u.getUnmarshallerHandler();
ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
v.setErrorHandler(errorReceiver);
loader = new ForkContentHandler(v,unmarshaller);
topLevel.applyAll(schema.getSchemas());
this.loader = null;
this.unmarshaller = null;
this.errorReceiver = null;
}
}
示例3: test1
import javax.xml.validation.ValidatorHandler; //导入依赖的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("unknown1234");
Assert.fail("SAXNotRecognizedException was not thrown.");
} catch (SAXNotRecognizedException e) {
; // expected
}
if (!validatorHandler.getFeature("http://xml.org/sax/features/namespace-prefixes")) {
// as expected
System.out.println("getFeature(namespace-prefixes): OK");
} else {
Assert.fail("Expected false, returned true.");
}
}
示例4: newValidator
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
public ValidatorHandler newValidator() {
synchronized(this) {
if(schema==null) {
try {
schema = SchemaFactory.newInstance(WellKnownNamespace.XML_SCHEMA).newSchema(source);
} catch (SAXException e) {
// we make sure that the schema is correct before we ship.
throw new AssertionError(e);
}
}
}
ValidatorHandler handler = schema.newValidatorHandler();
// fixValidatorBug6246922(handler);
return handler;
}
示例5: apply
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
/**
* Applies the additional binding customizations.
*/
public void apply(XSSchemaSet schema, ErrorReceiver errorReceiver) {
if(topLevel!=null) {
this.errorReceiver = errorReceiver;
UnmarshallerImpl u = BindInfo.getJAXBContext().createUnmarshaller();
this.unmarshaller = u.getUnmarshallerHandler();
ValidatorHandler v = BindInfo.bindingFileSchema.newValidator();
v.setErrorHandler(errorReceiver);
loader = new ForkContentHandler(v,unmarshaller);
topLevel.applyAll(schema.getSchemas());
this.loader = null;
this.unmarshaller = null;
this.errorReceiver = null;
}
}
示例6: translate
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
public static void translate(String xml, URL schemaURL, DocumentContainer documentContainer) throws SAXException, IOException {
// create the ValidatorHandler
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(schemaURL);
ValidatorHandler validatorHandler = schema.newValidatorHandler();
// create the parser, setup the chain
XMLReader parser = new SAXParser();
XmlAligner aligner = new XmlAligner(validatorHandler);
XmlTo<DocumentContainer> xml2object = new XmlTo<DocumentContainer>(aligner, documentContainer);
parser.setContentHandler(validatorHandler);
aligner.setContentHandler(xml2object);
// start translating
InputSource is = new InputSource(new StringReader(xml));
parser.parse(is);
}
示例7: parseAndValidate
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
/**
* Parse and Validate an XML represented by the input stream against provided XSD and reports validation issues.
*
* @param input XML to parse and validate
* @param vHandler Validator handler
* @throws IOException If a IO error occurs during XML parsing.
* @throws XmlParseException If a SAX error occurs during XML parsing.
*/
protected void parseAndValidate(InputStream input, ValidatorHandler vHandler) throws XmlParseException, IOException {
vHandler.setErrorHandler(this);
InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
is.setEncoding("UTF-8");
try {
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(vHandler);
parser.setErrorHandler(this);
vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
// Commented out the following line, as Java 1.6.0_45 throws an exception on this.
//parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
parser.parse(is);
} catch (SAXException e) {
throw new XmlParseException(e.getMessage(), e);
}
}
示例8: parseAndValidate
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
private void parseAndValidate(InputStream input, Schema schema) throws XmlParseException, IOException {
ValidatorHandler vHandler = schema.newValidatorHandler();
vHandler.setContentHandler(this);
vHandler.setErrorHandler(this);
InputSource is = new InputSource(new InputStreamReader(input, "UTF-8"));
is.setEncoding("UTF-8");
try {
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(vHandler);
parser.setErrorHandler(this);
vHandler.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
parser.setFeature("http://apache.org/xml/features/validation/id-idref-checking", false);
parser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", false);
parser.setFeature("http://xml.org/sax/features/external-general-entities", false);
parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
parser.parse(is);
} catch (SAXException e) {
throw new XmlParseException(e.getMessage(), e);
}
}
示例9: parseAndAssertInstance
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
private XmlConfigHandler parseAndAssertInstance(String xmlInstance) throws SAXException, IOException {
InputStream schemaStream = XmlConfigHandlerTest.class.getResourceAsStream(CONFIG_TEST_2_XSD);
XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory();
Schema schema = xmlSchemaFactory.newSchema(new StreamSource(schemaStream));
ValidatorHandler validatorHandler = schema.newValidatorHandler();
final TypeInfoProvider typeInfoProvider = validatorHandler.getTypeInfoProvider();
XmlConfigHandler xmlConfigHandler = new XmlConfigHandler(typeInfoProvider);
validatorHandler.setContentHandler(xmlConfigHandler);
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(validatorHandler);
InputStream xmlStream = XmlConfigHandlerTest.class.getResourceAsStream(xmlInstance);
parser.parse(new InputSource(xmlStream));
assertInstance(validatorHandler, xmlConfigHandler, xmlInstance);
return xmlConfigHandler;
}
示例10: assertInstance
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
private void assertInstance(ValidatorHandler validatorHandler, XmlConfigHandler xmlConfigHandler, String xmlInstance) throws SAXException, IOException {
Map<String,Property> properties = xmlConfigHandler.getProperties();
Assert.assertEquals(4, properties.size());
Assert.assertEquals("port", properties.get("port").getName());
Assert.assertEquals("100", properties.get("port").getValues().get(0).getValue());
Assert.assertEquals("ref", properties.get("ref").getName());
Assert.assertEquals("FileRepHandler", properties.get("ref").getValues().get(0).getValue());
Assert.assertEquals("com.avast.cloud.RequestHandler", (properties.get("ref").getValues().get(0)).getRefType());
Assert.assertEquals("list", properties.get("list").getName());
Assert.assertEquals("aaa", properties.get("list").getValues().get(0).getValue());
Assert.assertEquals("bbb", properties.get("list").getValues().get(1).getValue());
Assert.assertEquals("ccc", properties.get("list").getValues().get(2).getValue());
Assert.assertEquals("map", properties.get("map").getName());
Assert.assertEquals("a", ((MapEntry)properties.get("map").getValues().get(0)).getKey());
Assert.assertEquals("1", properties.get("map").getValues().get(0).getValue());
Assert.assertEquals("b", ((MapEntry)properties.get("map").getValues().get(1)).getKey());
Assert.assertEquals("2", properties.get("map").getValues().get(1).getValue());
Assert.assertEquals("c", ((MapEntry)properties.get("map").getValues().get(2)).getKey());
Assert.assertEquals("3", properties.get("map").getValues().get(2).getValue());
}
示例11: readTypeForProperty
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
private TypeInfo readTypeForProperty(final String propertyName) throws SAXException, IOException {
InputStream schemaStream = XmlConfigHandlerTest.class.getResourceAsStream(CONFIG_TEST_2_XSD);
XMLSchemaFactory xmlSchemaFactory = new XMLSchemaFactory();
Schema schema = xmlSchemaFactory.newSchema(new StreamSource(schemaStream));
final AtomicReference<TypeInfo> propertyTypeInfo = new AtomicReference<TypeInfo>();
ValidatorHandler validatorHandler = schema.newValidatorHandler();
final TypeInfoProvider typeInfoProvider = validatorHandler.getTypeInfoProvider();
validatorHandler.setContentHandler(new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (propertyName.equals(localName)) {
propertyTypeInfo.set(typeInfoProvider.getElementTypeInfo());
}
}
});
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(validatorHandler);
InputStream xmlStream = XmlConfigHandlerTest.class.getResourceAsStream(CONFIG_TEST_2_XML);
parser.parse(new InputSource(xmlStream));
return propertyTypeInfo.get();
}
示例12: parseAndGetConfig
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
/**
* Parses an xml config file and returns a Config object.
*
* @param xmlFile
* The xml config file which is passed by the user to annotation processing
* @return
* A non null Config object
*/
private Config parseAndGetConfig (File xmlFile, ErrorHandler errorHandler, boolean disableSecureProcessing) throws SAXException, IOException {
XMLReader reader;
try {
SAXParserFactory factory = XmlFactory.createParserFactory(disableSecureProcessing);
reader = factory.newSAXParser().getXMLReader();
} catch (ParserConfigurationException e) {
// in practice this will never happen
throw new Error(e);
}
NGCCRuntimeEx runtime = new NGCCRuntimeEx(errorHandler);
// set up validator
ValidatorHandler validator = configSchema.newValidator();
validator.setErrorHandler(errorHandler);
// the validator will receive events first, then the parser.
reader.setContentHandler(new ForkContentHandler(validator,runtime));
reader.setErrorHandler(errorHandler);
Config config = new Config(runtime);
runtime.setRootHandler(config);
reader.parse(new InputSource(xmlFile.toURL().toExternalForm()));
runtime.reset();
return config;
}
示例13: newValidator
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
public ValidatorHandler newValidator() {
if (schema==null) {
synchronized (this) {
if (schema == null) {
ResourceResolver resourceResolver = null;
try (InputStream is = clazz.getResourceAsStream(resourceName)) {
StreamSource source = new StreamSource(is);
source.setSystemId(resourceName);
// do not disable secure processing - these are well-known schemas
SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, false);
SchemaFactory schemaFactory = allowExternalAccess(sf, "file", false);
if (createResolver) {
resourceResolver = new ResourceResolver(clazz);
schemaFactory.setResourceResolver(resourceResolver);
}
schema = schemaFactory.newSchema(source);
} catch (IOException | SAXException e) {
InternalError ie = new InternalError(e.getMessage());
ie.initCause(e);
throw ie;
} finally {
if (resourceResolver != null) resourceResolver.closeStreams();
}
}
}
}
return schema.newValidatorHandler();
}
示例14: test1
import javax.xml.validation.ValidatorHandler; //导入依赖的package包/类
@Test
public void test1() throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
ValidatorHandler validatorHandler = schemaFactory.newSchema().newValidatorHandler();
validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
validatorHandler.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
}
示例15: test
import javax.xml.validation.ValidatorHandler; //导入依赖的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
}
}