- 浏览: 676688 次
- 性别:
- 来自: 太原
文章分类
最新评论
-
65018149:
你是吴江农商行的?
大额支付系统 -
txy821:
WebSphere MQ 学习笔记(3) - 远程管理 -
202013:
...
HTML 标签自定义属性 -
zhoulei984623:
那如果是OPENSUSE呢? 好像不能直接安装JAVAHL 是 ...
linux下通过eclipse使用SVN -
luzitian:
赞,太棒了!
Sun公司网站上的Swing实例,想学Swing的不看后悔
转自:http://iso-relax.sourceforge.net/JARV/JARV.html
该文详细介绍了如何使用JARV对XML进行校验,这里有完整而详细的实例:
http://blog.csdn.net/fenglibing/archive/2009/05/05/4150642.aspx
XMLSchema的学习网站:http://www.w3school.com.cn/schema/
两个概念:
MSV(Suns Multi Schema Validator):复合描述校验器
JARV(JAVA API forRELAXVerifiers):支持RELAX校验器的JAVA API
JARV User's Guide
Written by Kohsuke KAWAGUCHI
Table of Contents
JARV is an implementation-independent interface set for validators developed by the RELAX community. There are several implementations available that support this interface. Although it originally came from the RELAX community, JARV is not limited to RELAX; it can be used with many other schema languages. One of the advantages of JARV is that it allows you to use multiple schema languages with minimal change on your code. First, you need the latest Introduction
Obtaining implementations
isorelax.jar
file, which is
Then, you need actual implementations. Currently, following implementations are available:
You need to set up those jars so that the class loader can find them.
JARV consists of three components. The The first thing you would do is to create an instance of To use Swift RELAX Verifier for Java: JARV is also capable of finding an implementation that supports a particular schema language at run-time. To learn more about this discovery mechanism, please read Architecture
VerifierFactory
, Schema
and Verifier
.VerifierFactory
interface is the main interface between the implementation and your application. It has a method to compile a schema into a Schema
object. The Schema
interface is the internal representation of the schema. This interface is thread-safe, so you can have multiple threads access one Schema
object concurrently. Also, this interface has a method to create a new Verifier
object. The Verifier
interface represents a so-called "validator"; it has a schema object in it and it validates documents by using that schema. Using JARV
Step 1: create
VerifierFactory
VerifierFactory
. To do that, simply create an instance of VerifierFactory
implementation. In case of MSV, it will be: VerifierFactory factory = new com.sun.msv.verifier.jarv.TheFactoryImpl();
VerifierFactory factory = new jp.xml.gr.relax.swift.SwiftVerifierFactory();
In this way, you can create a Verifier that checks documents against a particular schema. Verifier is not thread-safe. So typically you want to create one instance per one validation (or one thread.) Verifier has several methods to validate documents. One way is to call the This method will only give you yes/no answer, but you can get more detailed error information by setting an error handler through the Just like a parser reports well-formedness errores through If you throw an exception from the error handler, that exception will not be catched by the verify method. So the validation is effectively aborted there. If you return from the error handler normally, then MSV will try to recover from the error and find as much errors as possible. Every JARV implementation supports the validation via SAX2 in two ways. The first one is a validator implemented as The second one is a validator implemented as A verifier implemented as a filter, Not only you can validate documents before you process them, you can validate them after your application process them. In the following example, a verifier filter is used to validate documents before your own handler process it. SAX-based validation will not make much sense unless you set an error handler, because to know that the document was invalid after you've processed it is too late. To set an error handler, call the In this way, you can abort the processing by throwing an exception in case of an error. If you are using Some JARV implementations (e.g., MSV, Jing, RELAX Verifier for Java) always runs in A simple, obvious way to create a In this way, you can decide the JARV implementation at the compile time. Especially in case of MSV, it is advantageous to do so because of the support of the "multi-schema" capability. The MSV factory will accept any schema written in any of the supported languages. Thus you can instantly change the schema language without changing your code at all However, there is one problem in this approach. Specifically, it locks you into a particular JARV implementation, so you need to change your code to use other JARV implementations. For this reason, you may want to "discover" an implementation (just like you usually do with JAXP) at run-time by calling the static Usually, the namespace URI of the schema language is used as the name. For the complete list, plaese consult One of the problems of some validators (like DTD validator in Xerces) is that it doesn't work in the fail-fast manner. This problem is unique to SAX. What is "fail-fast"? A fail-fast validator is a validator that can flag an error as soon as an error is found. A non fail-fast validator may let some part of the wrong document slip in (they will flag an error at the later moment.) When you are using non fail-fast validator, you need to take extra care to write your code because your code may be exposed to bad documents. For example, imagine a following simple DTD and a bad document: Suprisingly, in a typical non-fail-fast validator, the error will be signaled as late as in the end-element event of the Typically, this robs the merit of the validation because you do the validation to protect your application code from unexpected inputs. Many of JARV implementations (including MSV, Jing, RELAX Verifier for Java) are fail-fast validators; so they will signal an error at the start-element event of the first ' Note that some other JARV implementations may be non fail-fast validators. The This is sometimes useful when you are using only one thread. JAXP masquerading feature is a wrapper implementation of Step 3: create a verifier
Schema
is just a compiled schema, so it cannot do anything by itself. Verifier
object is the object that performs the actual validation. To create a Verifier
object, do as follows: Verifier verifier = schema.newVerifier();
Step 4-1: perform validation
verify
method, which accepts a DOM tree, File
, URL, etc and returns the validity. For example, to validate a DOM document, simply pass it as an argument: if(verifier.verify(domDocument))
// the document is valid
else
// the document is invalid (wrong)
setErrorHandler
method. org.xml.sax.ErrorHandler
, JARV implementations (like MSV) reports validity errors through the same interface. In this way, you can get the error message, line number that caused the error, etc. For example, in the following code, a custom error handler is set to report error messages to the client. verifier.setErrorHandler( new MyErrorHandler() );
try {
if(verifier.verify(new File("abc.xml")))
// the document is valid
else
// the execution will never reach here because
// if the document is invalid, then an exception should be thrown.
} catch( SAXParseException e ) {
// if the document is invalid, then the execution will reach here
// because we throw an exception for an error.
}
...
class MyErrorHandler implements ErrorHandler {
public void fatalError( SAXParseException e ) throws SAXException {
error(e);
}
public void error( SAXParseException e ) throws SAXException {
System.out.println(e);
throw e;
}
public void warning( SAXParseException e ) {
// ignore warnings
}
}
Step 4-2: perform validation via SAX
ContentHandler
, which can be obtained by calling the getVerifierHandler
method. This content handler will validate incoming SAX2 events, and you can obtain the validaity through the isValid
method. For example, XMLReader reader = ... ; // get XML reader from somewhere
VerifierHandler handler = verifier.getVerifierHandler();
reader.setContentHandler(handler);
reader.parse("http://www.mydomain.com/some/file.xml");
if(handler.isValid())
// the document is correct
else
// the document is incorrect
XMLFilter
, which can be obtained by calling the getVerifierFilter
method. VerifierFilter
, is particularly useful because you can plug it right in the middle of any SAX event pipeline. VerifierFilter filter = verifier.getVerifierFilter();
// create a new XML reader and setup the pipeline
filter.setParent(getNewXMLReader());
filter.setContentHandler( new MyApplicationHandler() );
// parse the document
filter.parse("http://www.mydomain.com/some/file.xml");
if(filter.isValid())
// the parsed document was valid
else
// invalid
setErrorHandler
method just as you did with the verify
method. filter = verifier.getXMLFilter();
verifier.setErrorHandler(new MyErrorHandler());
...
filter.parse(...);
VerifierFilter
you can also set an error handler by calling the setErrorHandler
method of the VerifierFilter
interface. Advanced Topics
Finding implementation at Run-time
VerifierFactory
is to create a new instance of appropriate implementation class (like com.sun.msv.verifier.jarv.TheFactoryImpl
. newInstance
method of the VerifierFactory
class. To do that, you need to pass the name of schema language you want to use. This method will find an implementation that supports a given schema language from the class path and returns its VerifierFactory
. VerifierFactory factory = VerifierFactory.newInstance(
"http://relaxng.org/ns/structure/1.0");
Fail-Fast Design
<!ELEMENT root (a,b)*>
<!ELEMENT a #EMPTY>
<!ELEMENT b #EMPTY>
<root>
<b/> <!-- error -->
<b/>
</root>
root
element. So you have to make sure that your application behaves gracefully when it sees the wrong 'b
'. b
'. This guarantees that the application will never see a wrong document. Creating Verifier directly from VerifierFactory
VerifierFactory
class has the newVerifier
method as a short-cut. It is a short-cut in the sense that the following two code fragments have exactly the same meaning: Verifier v = factory.compileSchema(x).newVerifier();
Verifier v = factory.newVerifier(x);
JAXP Masquerading
Step 2: compile a schema
Once you get a factory, then you can use it to compile a schema. To compile a schema, call the compileSchema
method of the factory.
Schema schema = factory.compileSchema("http://www.example.org/test.xsd");
This method can accept many types of input. For example, you can pass InputSource
, File
, InputStream
, etc.
Schema
objects are thread-safe. So even if you have more than one threads, you only need one instance of Schema
; you can share that one instance with as many threads as you want.
This is often the easiest way to incorporate the validation into your application. Since it's just so easy to use.
To create a wrapped SAXParserFactory
, do as follows:
Schema schema = /* compile schema */; SAXParserFactory parserFactory = new org.iso_relax.jaxp.ValidatingSAXParserFactory(schema);
This will create a JAXP SAXParserFactory that validates every parsed document by the specified schema. Similarly, to create a wrapped DocumentBuilder
, do as follows:
Schema schema = /* compile schema */; DocumentBuilderFactory dbf = new org.iso_relax.jaxp.ValidatingDocumentBuilderFactory(schema);
Once those instances are created, just use them as you use a normal JAXP implementation.
The The The The thread affinity of JARV is designed after that of TrAX API ( This causes a problem when you are passing To avoid this problem, wrap it by an This ugly limitation came from the difficulty in correctly detecting XML DTDs, which are written in non-XML syntax, from other schema languages, which are written in XML syntax. Any input on this restriction is very welcome. If you need an example that is not listed here, please Have a look at Have a look at Thread Affinity
VerifierFactory
interface is not thread-safe. This basically means that you cannot use one object from two threads. Schema
interface is thread-safe. So once you compile a schema file into a Schema
object, it can be shared by multiple threads and accessed concurrently. This is useful at server-side, where multiple threads process client requests simultaneously. Verifier
interface is again not thread-safe. Each thread needs its own copy of Verifier
. Verifier
objects are still re-usable, as you can use the same object to validate multiple documents one by one. What you cannot do is to validate multiple documents simultaneously. javax.transform
package). Familiarity with TrAX will help you understand JARV better.MSV and Schema Language Auto Detection
com.sun.msv.verifier.jarv.TheFactoryImpl
automatically detects the schema language from the schema file. However, there is one important limitation. Currently, the detection of XML DTDs is based on the file extension. Specifically, if the schema name has ".dtd" extension, it is treated as XML DTD and otherwise it is treated as other schema languages. InputStream
as the parameter to the compileSchema
method. Since InputStream
s do not have names, they are always treated as non-DTD schemas. InputSource
and call the setSystemId
method to set the system id. The following example shows how to do that: InputSource is = new InputSource(
MyClass.class.getResourceAsStream("abc.dtd") );
is.setSystemId("abc.dtd");
verifierFactory.compileSchema(is);
Examples
Validating bunch of files
SingleThreadDriver.java
example in Multi-threaded example
MultiThreadDriver.java
example in
This example shows you how to use JARV in the multi-threaded environment and how you can cache a compiled schema into memory.
The following code shows how you can validate DOM by using JARV. The following code shows how you can use JARV together with SAX. The following code shows how you can use JARV via JAXP-masquerading. DOM validation
import org.iso_relax.verifier.*;
void f( org.w3c.dom.Document dom )
{
// create a VerifierFactory
VerifierFactory factory = VerifierFactory.newInstance(
"http://relaxng.org/ns/structure/1.0");
// compile a RELAX NG schema
Schema schema = factory.compileSchema( new File("foo.rng") );
// obtain a verifier
Verifier verifier = schema.newVerifier();
// check the validity of a DOM.
if( verifier.verify(dom) )
// the document is valid
else
// the document is not valid
// you can use the same verifier object to test multiple DOMs
// as long as you don't use it concurrently.
if( verifier.verify(anotherDom) )
...
// or you can pass an Element to validate that subtree.
Element e = (Element)dom.getDocumentElement().getFirstSibling();
if( verifier.verify(e) )
...
}
SAX validation
import org.iso_relax.verifier.*;
void f( javax.xml.parsers.SAXParserFactory parserFactory )
{
// create a VerifierFactory with the default SAX parser
VerifierFactory factory = VerifierFactory.newInstance(
"http://www.xml.gr.jp/xmlns/relaxCore");
// compile a RELAX schema
Schema schema = factory.compileSchema( new File("foo.rxg") );
// obtain a verifier
Verifier verifier = schema.newVerifier();
// set an error handler
// this error handler will throw an exception if there is an error
verifier.setErrorHandler( new MyErrorHandler() );
// get a XMLFilter
VerifierFilter filter = verifier.getVerifierFilter();
// set up the pipe-line
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
filter.setParent( reader );
filter.setContentHandler( new MyContentHandler() );
// parse the document
try {
filter.parse( "MyInstance.xml" );
// if the execution reaches here, the document was valid and
// there was nothing wrong.
} catch( SAXException e ) {
// error.
// maybe the document is not well-formed, or it's not valid
// or some other reasons.
}
}
JAXP Masquerading
import org.iso_relax.verifier.*;
import org.iso_relax.jaxp.*;
void f()
{
// create a RELAX NG validator
VerifierFactory factory = VerifierFactory.newInstance(
"http://relaxng.org/ns/structure/1.0");
// compile a schema
Schema schema = factory.compileSchema( new File("myschema.rng") );
// wrap it into a JAXP
SAXParserFactory parserFactory = new ValidatingSAXParserFactory(schema);
// create a new XMLReader from it
parserFactory.setNamespaceAware(true);
XMLReader reader = parserFactory.newSAXParser().getXMLReader();
// set an error handler
// this error handler will throw an exception if there is an well-formedness
// error or a validation error.
reader.setErrorHandler( new MyErrorHandler() );
// set the content handler
reader.setContentHandler( new MyContentHandler() );
// parse the document
try {
reader.parse( "MyInstance.xml" );
// if the execution reaches here, the document was valid and
// there was nothing wrong.
} catch( SAXException e ) {
// error.
// maybe the document is not well-formed, or it's not valid
// or some other reasons.
}
}
相关推荐
MyBatis的前身是apache的一个开源项目iBatis,是个数据持久层(ORM)框架,给大家提供mybatis-3.2.7.jar包下载,有... iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久
iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO),同时还提供一个利用这个框架开发的 JPetStore实例。
commons-dbutils.jar是在java架构开发时十分重要的一款.jar包,正确的使用commons dbutils可以让你的开发事半功倍,如果您在开发过程中缺少这款jar包,马上来下载commonsdbutils jar包吧! 软件功能: ...
:house_with_garden: 个人网站,使用以下内容创建和部署: (引荐链接) :chart_increasing: 顺便说一下,我的的! 在此仓库中,我会不断列出和。 :floppy_disk: 运行本地测试服务器 :yarn: 使用纱线: ...
如果您遇到相同的问题,请与我联系,谢谢〜 脚步 我已经在Ubuntu中进行了测试。 这是我的配置: Python 3.6.4 |Anaconda numpy (1.11.0) Matlab2014a // vim ~/.bashrc # Add an "mrun" alias for running matlab in...
Marvel JARVIG(一个非常有趣的游戏)是一款游戏,可让您根据角色的名称,图像和描述来查找和发现Marvel Comics角色! 为此,玩家回答了一系列有关“漫威宇宙”的问题,以确定他是否真正了解每个角色。 此外,游戏...
Jarvis是的语音命令辅助服务,它可以识别人类语音,与用户交谈并执行基本命令。 助理技能 打开一个网页(例如'Jarvis open youtube') 在Youtube中播放音乐(例如“ Jarvis play mozart”) 增大/减小扬声器的主...
熟悉漫威电影的人都知道Jarvis,他是钢铁侠的智能管家,帮助钢铁侠制造装甲、分析大量数据、协助建模等各种智能工作,可惜在复联2中,Jarvis与灵魂宝石共同结合成Vision,钢铁侠失去了这位如亲人一般的智能AI,后来...
贾维斯与Discord API交互的Javascript Discord库!这不能完全正常工作! 请不要下载和投诉,因为这正在进行中! 公关欢迎!目录要求Node.JS => 14.0.0安装NPM(推荐) # Stablenpm i -- save jarvis . djs# Nightly ...
v1.0.2 - 退出代码使用 -e 选项时打印退出代码 labkey-upload.csv.jar 和 labkey-download-csv.jarv1.0.1 - 修补程序 (2015-02-27) 修复了 labkey-upload-csv.jar 的错误报告器中的递归错误向 labkey-upload.csv.jar...
jgraphx-v3.9.3.jar
Clone the repogit clone git@githb.com:jarv/thestategame.com.git# Install the python requirementscd thestategame.compip intall -r requirements.txt# Create the databaseecho " from state import init_db;...