1

.jar アーカイブにパッケージ化されたユーティリティを使用するサーブレットがあります。

@Override
public void init() throws ServletException {
    ...
    try (InputStream stream = getClass().getResourceAsStream("/fileToParse.xml")) {
        App.check(stream);
    } catch (Exception e) {
        throw new ServletException(e);
    }
    ...
}

このユーティリティは、xml ファイル ストリームを受け取り、xsd スキーマに対して検証を実行し、それを解析します。

public class App {
    private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; 
    ...   
    public static void check(InputStream stream) {    
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        MyHandler handler = new MyHandler();
        try {
            SAXParser sp = spf.newSAXParser();
            sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
            XMLReader rdr = sp.getXMLReader();
            rdr.setContentHandler(handler);
            rdr.setErrorHandler(new MyErrorHandler());
            rdr.parse(new InputSource(stream));
        }
        ...
    }
    ...
}

xsd ファイルは次で始まります。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.mydomain.org"
           elementFormDefault="qualified"
           xmlns:t="http://www.mydomain.org">
    <xs:element name="appContext">
        ...

xml ファイル:

<?xml version="1.0" encoding="UTF-8"?>
<appContext xmlns="http://www.mydomain.org"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.mydomain.org appContext.xsd">
    ...

.war 構造:

css/
js/
WEB-INF/
    classes/
        mypackage/
            MyServlet.class
        fileToParse.xml
    lib/
        App.jar
    web.xml

App.jar 構造:

mypackage2/
    App.class
appContext.xsd

サーブレットの Init メソッドが例外をスローします:

...
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_21]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_21]
Caused by: java.io.FileNotFoundException: /PATH_TO_TOMCAT/bin/appContext.xsd (No such file or directory)
    at java.io.FileInputStream.open(Native Method) ~[na:1.7.0_21]
    at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.7.0_21]

xml ファイルを検証するために必要な xsd スキーマの場所を SAXParser に指定するにはどうすればよいですか?

PS 下手な英語でごめんなさい

更新:

このプロパティを追加しようとしています:

    private static final String JAXP_SCHEMA_SOURCE =
            "http://java.sun.com/xml/jaxp/properties/schemaSource";
    ....
    public static void check(InputStream stream) {
        ...
        try {
            ...
            sp.setProperty(JAXP_SCHEMA_SOURCE, new File(getPath("/appContext.xsd")));
            ...
        }
    }
    public static String getPath(String path) {
        return App.class.getResource(path).toString();
    }

今、私はこの例外があります:

ERROR mypackage2.App - Error: URI=null Line=5: schema_reference.4: Failed to read schema document 'jar:file:/PATH_TO_TOMCAT/webapps/myapp/WEB-INF/lib/App.jar!/appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'jar:file:/PATH_TO_TOMCAT/webapps/myapp/WEB-INF/lib/App.jar!/appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) ~[na:1.7.0_21]

UPD2: xml ファイルに "classpath:appContext.xsd" がある場合:

WARN  mypackage.App - Warning: URI=null Line=5: schema_reference.4: Failed to read schema document 'classpath:appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'classpath:appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) ~[na:1.7.0_21]
...
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_21]
Caused by: java.net.MalformedURLException: unknown protocol: classpath
    at java.net.URL.<init>(URL.java:592) ~[na:1.7.0_21
;;;
4

2 に答える 2

2

スキーマソースInputStream直接設定してみてください

public static void check(InputStream stream) {
    ...
    try {
        ...
        sp.setProperty(JAXP_SCHEMA_SOURCE, getInputStream("/appContext.xsd")));
        ...
    }
}

public static InputStream getInputStream(String path) {
    return SAXLoader.class.getResourceAsStream(path);
}

XML ファイルから XSD 参照を削除した後、このプロパティの設定が機能するかどうかを確認します。そうでない場合は、現在行っているように、 jarの外側にある XSDとその絶対パスを参照として渡してテストするだけで、プロパティが機能するかどうかを確認しFileます。

于 2013-09-02T20:11:38.363 に答える
0

問題が解決しました:

public class App {
    ...   
    public static void check(InputStream stream) {    
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        MyHandler handler = new MyHandler();
        try (InputStream schemaStream = App.class.getResourceAsStream("/appContext.xsd")) {
            SAXParser sp = spf.newSAXParser();
            sp.setProperty(JAXPConstants.JAXP_SCHEMA_LANGUAGE, JAXPConstants.W3C_XML_SCHEMA);
            sp.setProperty(JAXPConstants.JAXP_SCHEMA_SOURCE, new InputSource(schemaStream));
            XMLReader rdr = sp.getXMLReader();
            rdr.setContentHandler(handler);
            rdr.setErrorHandler(new MyErrorHandler());
            rdr.parse(new InputSource(stream));
        }
        ...
    }
    ...
}
于 2013-09-02T22:01:09.140 に答える