2013-01-17 3 views
1

아래 코드 ERROR: 'Invalid byte 1 of 1-byte UTF-8 sequence.'인코딩 문제 추출하면서 EXI 압축 XML

@Test 
void testExiCompression() { 
    def xml = '<Root><Child id="1">Text</Child><EmptyTag/></Root>' 
    def compressed = ExiCompressionUtils.compress(xml) 
    assert ExiCompressionUtils.extract(compressed) == xml 
} 

EXIficient

class ExiCompressionUtils { 
    static Transformer transformer = TransformerFactory.newInstance().newTransformer() 

    static byte[] compress(String xml) { 
     ByteArrayOutputStream exiOS = new ByteArrayOutputStream() 
     EXIResult exiResult = new EXIResult(outputStream : exiOS) 

     XMLReader xmlReader = XMLReaderFactory.createXMLReader() 
     xmlReader.contentHandler = exiResult.handler 
     xmlReader.parse(new InputSource(new StringReader(xml))) 

     def compressed = exiOS.toByteArray() 
     exiOS.close() 
     return compressed 
    } 

    static String extract(byte[] compressed) { 
     SAXSource exiSource = new SAXSource(new InputSource(new ByteArrayInputStream(compressed))) 
     exiSource.setXMLReader(exiSource.reader) 

     ByteArrayOutputStream exiOS = new ByteArrayOutputStream() 
     transformer.transform(exiSource, new StreamResult(exiOS)) // fails here 
     def extracted = exiOS.toString() 
     exiOS.close() 
     return compressed 
    } 
} 

아래 테스트가 실패하여 EXI 압축 및 압축 해제를 수행하기 위해 필요한 설정을 단순화하기 위해 시도 이것의 밑바닥에 도달 할 수있는 인코딩 전문가가 있습니까?

+0

나는 이해할 수 없다 : 그것은 진정한 자바입니까? 세미콜론이 생략 된 이유는 무엇입니까? 'def 압축 ... '이란 무엇입니까? – Andremoniy

+0

Groovy ... close만큼이나 .class 파일로 컴파일합니다. Groovy로 가져온 몇 가지 바로 가기가 아닌 일반 Java가 될 수 있기 때문에 Java로 레이블을 지정했습니다. 그들은 요즘 어쨌든 대부분의 목적으로 상호 교환이 가능합니다. – jkschneider

+0

@jkschneider 생각해 보았지만 * UTF-8 *로 인코딩 된 테스트 케이스 파일 (XML 포함)은 무엇입니까? 나는 exi에 대해 많이 알지 못한다.하지만 그 오류는 전에는 보아왔다. 보통은 UTF-8 *을 따르지 않는 XML의 인코딩과 관련이있다. – BPaasch

답변

1

오늘 나는이 논평에 어려움을 겪었습니다. 이 코드에는 중요한 문제가 하나 있습니다 (세미콜론이없는 자바에 대한 이상한 구문 외에도)

읽을 때는 EXISource를 사용하고 SAXSource는 사용하지 마십시오!

작동하는 코드 조각을 첨부했습니다.

- 다니엘

static Transformer transformer; 

static { 
    try { 
     transformer = TransformerFactory.newInstance().newTransformer(); 
    } catch (TransformerConfigurationException e) { 
    } catch (TransformerFactoryConfigurationError e) { 
    } 
} 

static byte[] compress(String xml) throws IOException, EXIException, 
     SAXException { 
    ByteArrayOutputStream exiOS = new ByteArrayOutputStream(); 
    EXIResult exiResult = new EXIResult(); 
    exiResult.setOutputStream(exiOS); 

    XMLReader xmlReader = XMLReaderFactory.createXMLReader(); 
    xmlReader.setContentHandler(exiResult.getHandler()); 
    xmlReader.parse(new InputSource(new StringReader(xml))); 

    byte[] compressed = exiOS.toByteArray(); 
    exiOS.close(); 

    return compressed; 
} 

static String extract(byte[] compressed) throws TransformerException, 
     IOException, EXIException { 
    // SAXSource exiSource = new SAXSource(new InputSource(new 
    // ByteArrayInputStream(compressed))); // use EXISource instead! 
    SAXSource exiSource = new EXISource(); 
    exiSource.setInputSource(new InputSource(new ByteArrayInputStream(
      compressed))); 

    ByteArrayOutputStream exiOS = new ByteArrayOutputStream(); 
    transformer.transform(exiSource, new StreamResult(exiOS)); 
    String extracted = exiOS.toString(); 
    exiOS.close(); 
    return extracted; 
} 

public static void main(String[] args) throws IOException, EXIException, 
     SAXException, TransformerException { 
    String xml = "<Root><Child id=\"1\">Text</Child><EmptyTag/></Root>"; 
    byte[] compressed = ExiCompressionUtils.compress(xml); 
    System.out.println(ExiCompressionUtils.extract(compressed)); 
}