2011-10-03 6 views
1

파일 시스템의 다른 사람들과 배치 된 XSD에 대해 메모리에 바이트 스트림으로 보관 된 XML 문서의 유효성을 검사 할 상황이 있습니다. 우리는 파일 이름을 XML 파일에 명시 적으로 언급하는 것을 피하고 대신 XML 파서에 유효성 검사를 위해 하나 이상의 XSD 파일 카탈로그를 사용하도록 알려줍니다. (Guice 3.0) DocumentBuilder를 공급자를 만들 수JAXP - 디버그 XSD 카탈로그 찾아보기

내 시도는 외모와 같은 :

public class ValidatingDocumentBuilderProvider implements 
     Provider<DocumentBuilder> { 

    static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 
    static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; 
    static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; 

    Logger log = getLogger(ValidatingDocumentBuilderProvider.class); 

    DocumentBuilderFactory dbf; 

    public synchronized DocumentBuilder get() { // dbf not thread-safe 

     if (dbf == null) { 
      log.debug("Setting up DocumentBuilderFactory"); 

      // http://download.oracle.com/javaee/1.4/tutorial/doc/JAXPDOM8.html 
      dbf = DocumentBuilderFactory.newInstance(); 
      dbf.setNamespaceAware(true); 
      dbf.setValidating(true); 
      dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); 
      // parser should look for schema reference in xml file 

      // Find XSD's in current directory. 

      FilenameFilter fileNameFilter = new FilenameFilter() { 

       public boolean accept(File dir, String name) { 
        return name.toLowerCase().endsWith(".xsd"); 
       } 
      }; 
      File[] schemaFiles = new File(".").listFiles(fileNameFilter); 

      dbf.setAttribute(JAXP_SCHEMA_SOURCE, schemaFiles); 

      log.debug("{} schema files found", schemaFiles.length); 
      for (File file : schemaFiles) { 
       log.debug("schema file: {}", file.getAbsolutePath()); 
      } 

     } 

     try { 
      return dbf.newDocumentBuilder(); 
     } catch (ParserConfigurationException e) { 
      throw new RuntimeException("get DocumentBuilder", e); 
     } 
    } 
} 

(그리고 나는 또한 너무 파일 이름 시도했다). 이클립스는 XSD를 받아 들인다 - 카탈로그에 넣을 때 여기서 다루는 XML의 유효성을 검사 할 수있다

유효성을 검사 할 때 파서가 잠시 중단된다는 것은 육안으로 보인다. 네트워크 조회 일 수 있습니다.

-Djaxp.debug=1 만 나는 그것이 무엇을하고 있는지 말해 JDK 6의 파서를 얻을 수있는 방법이 라인

JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory 
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl 
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl using ClassLoader: null 

을 추가? 내가 할 수 없다면 XML 카탈로그의 사용법을 조사하여 제공된 XSD가 선택되지 않은 이유를 확인하려면 어떻게해야합니까?

내가 간과 한 것은 무엇입니까?

답변

0

당신은 우리가 명시 적으로 어떻게 다음 파서가 적절한 스키마를 선택 할 수있을 것입니다

XML 파일에 언급 된 파일 이름을 피하기 위해 싶습니다

말?

시도해 볼 수있는 것은 가능한 모든 스키마 자원을 기반으로 SchemaFactory을 사용하여 Schema을 만들고이를 문서 작성자 팩토리에 첨부하는 것입니다. 그런 다음 파서는이 "수퍼 스키마"에 대해 문서의 유효성을 자동으로 검사합니다.

스키마 세트에 내부 종속성 (즉, 가져 오기 또는 포함)이있는 경우 상대 URL 또는 특수 분석기를 사용하여 해당 참조가 올바르게 해결되었는지 확인하십시오.

UPDATE :

더 신중하게, http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM8.html, 비트를 읽은 후, 나는 당신이 내 제안하는 것과 같은 효과가 있어야 접근 실현, 그래서 뭔가 다른 N 것입니다. 나는 단지 내가 말하는 것이 잘 작동한다고 말할 수있다.

+0

각 XSD는 어떤 네임 스페이스에 대해 유효성을 확인할 수 있는지 언급합니다. 이러한 네임 스페이스 중 하나가 XML에서 사용되는 경우 파서가 해당 XSD를 사용하도록하고 싶습니다. –