2011-08-19 2 views
4
내가 북마크를 추가, 그것은 완벽하게 작동 비행 접시를 사용하지만 지금은 내가 원하는 PDF로 XHTML에서 변환을하고, 그것은 다음과 같이 수행해야 FS 문서에 따라하고

:DOM 트리로 읽기 XHTML 및 사용자 정의 태그

<bookmarks> 
    <bookmark name='1. Foo bar baz' href='#1'> 
     <bookmark name='1.1 Baz quux' href='#1.2'> 
     </bookmark> 
    </bookmark> 
    <bookmark name='2. Foo bar baz' href='#2'> 
     <bookmark name='2.1 Baz quux' href='#2.2'> 
     </bookmark> 
    </bookmark> 
</bookmarks> 
는 HEAD 섹션에 투입해야

, 나는 그 일을했지만 SAXParser를 못해 말을 더 이상 파일을 읽을 :

line 11 column 14 - Error: <bookmarks> is not recognized! 
line 11 column 25 - Error: <bookmark> is not recognized! 

내가 로컬 엔티티 리 조르바를 설정하고도에 북마크를 추가 한이 DTD,

<!--flying saucer bookmarks --> 
<!ELEMENT bookmarks (#PCDATA)> 
<!ATTLIST bookmarks %attrs;> 

<!ELEMENT bookmark (#PCDATA)> 
<!ATTLIST bookmark %attrs;> 

하지만 구문 분석이되지 않습니다. 아이디어가 없습니다. 제발 도와주세요.

내가 구문 분석하려면 다음 코드를 사용하고

편집 :

class LocalEntityResolver implements EntityResolver { 

    private static final Logger LOG = ESAPI.getLogger(LocalEntityResolver.class); 
    private static final Map<String, String> DTDS; 
    static { 
     DTDS = new HashMap<String, String>(); 
     DTDS.put("-//W3C//DTD XHTML 1.0 Strict//EN", 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"); 
     DTDS.put("-//W3C//DTD XHTML 1.0 Transitional//EN", 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"); 
     DTDS.put("-//W3C//ENTITIES Latin 1 for XHTML//EN", 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent"); 
     DTDS.put("-//W3C//ENTITIES Symbols for XHTML//EN", 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent"); 
     DTDS.put("-//W3C//ENTITIES Special for XHTML//EN", 
       "http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent"); 
    } 

    @Override 
    public InputSource resolveEntity(String publicId, String systemId) 
      throws SAXException, IOException { 
     InputSource input_source = null; 
     if (publicId != null && DTDS.containsKey(publicId)) { 
      LOG.debug(Logger.EVENT_SUCCESS, "Looking for local copy of [" + publicId + "]"); 

      final String dtd_system_id = DTDS.get(publicId); 
      final String file_name = dtd_system_id.substring(
        dtd_system_id.lastIndexOf('/') + 1, dtd_system_id.length()); 

      InputStream input_stream = FileUtil.readStreamFromClasspath(
        file_name, "my/class/path", 
        getClass().getClassLoader()); 
      if (input_stream != null) { 
       LOG.debug(Logger.EVENT_SUCCESS, "Found local file [" + file_name + "]!"); 
       input_source = new InputSource(input_stream); 
      } 
     } 

     return input_source; 
    } 
} 

내 D :

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = dbf.newDocumentBuilder(); 
builder.setEntityResolver(new LocalEntityResolver()); 
document = builder.parse(is); 

편집 여기

은 LocalEntityResolver입니다 문서 빌더 팩토리 구현은 다음과 같습니다. com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

+0

null입니다 있는지 확인하기 위해 검사가 포함되어 있습니다. 나 또는 다른 누구도 문제를 재현 할 수 있습니까? – mzjn

+0

기본적으로 W3C Transitional DTD를 사용하여 특정 알 수없는 요소가있는 유효한 XHTML을 DOM 트리로 구문 분석하려고합니다. 재현하려면 유효한 XHTML을 사용하고 책갈피 html을 추가하고 dom 트리로 구문 분석을 시도하십시오 – epoch

+0

'LocalEntityResolver'란 무엇입니까? 그거 어디서 났어? Xerces 소스에서'{element}가 인식되지 않습니다! '라는 메시지를 찾을 수 없습니다. –

답변

0

아, 결국 문제가 발견되었습니다. 여러분들이 코드를 디버깅하게해서 죄송합니다. 문제는 코드 내에서 DOM 파싱이 발생하기 바로 전에 JTidy.parse에 대한 호출이 있었는데, 이는 내용이 비어있는 것으로 해석되어서 그것을 잡아 내지 못했습니다. 실제 오류는 SAX의 Premature End of file입니다.

Matt Gibson 덕분에 짧은 입력 문서를 컴파일하는 코드를 읽는 동안 버그를 발견했습니다.

내 코드는 지금 내용이 내가 당신이 더 많은 정보를 제공 할 필요가 있다고 생각

/** 
* parses String content into a valid XML document. 
* @param content the content to be parsed. 
* @return the parsed document or <tt>null</tt> 
*/ 
private static Document parse(final String content) { 
    Document document = null; 
    try { 
     if (StringUtil.isNull(content)) { 
      throw new IllegalArgumentException("cannot parse null " 
        + "content into a DOM object!"); 
     } 

     InputStream is = new ByteArrayInputStream(content 
       .getBytes(CONTEXT.getEncoding())); 

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = dbf.newDocumentBuilder(); 
     builder.setEntityResolver(new LocalEntityResolver()); 
     document = builder.parse(is); 
    } catch (Exception ex) { 
     LOG.error(Logger.EVENT_FAILURE, "parsing failed " 
       + "for content[" + content + "]", ex); 
    } 

    return document; 
} 
+0

또 다른 이유는 [SSCCE] (http://sscce.org/) ;-) 나는 실제로 문제를 재현하려고 애쓰는 데 어려움을 겪고있었습니다 (예 : FileUtil이 제공하는 라이브러리는 무엇입니까?) – Wivani

+0

아니요 걱정. 다행 이군! –

관련 문제