2016-04-11 14 views
1

さまざまな環境の構成状態を比較するために、JAXBマーシャリングされたXMLファイルを使用するプロジェクトがあります。私は、Windows上のJAXBマーシャラの実装がUnixバージョンとの違いに違いがあることに気付きました。異なるプラットフォームで作成された2つのファイルを比較すると、比較ツールは常にファイルの最後に1つの違いを表示します。 Windows上に作成されたファイルには、ファイルの最後に改行(CRおよびLF)がありますが、Unixにはその行がありません。Windows上のMarshallerがファイルの末尾に新しい行を追加します

両プラットフォーム間の改行文字の違いではなく、です。! Windowsマーシャラーは、ファイルの最後に「改行」を追加しますが、UNIXマーシャラーはルートタグの終了「>」の後で停止します。

Windows marshaller adds a new line at the end

私はこの追加の行を防止するために、マーシャラーに渡すことができるか、私の比較ツールが違いにフラグを付けないように、私は、明示的にWindows上でのマーシャリング後にそれを削除しなければならないのいずれかのパラメータがあります?プリティプリントするXMLは、2つの異なるシステムからまったく同じ結果が得られますない理由(あるいは期待)がありません

public void marshal(final Object rootObject, final OutputStream outputStream) throws JAXBException, TransformerException { 
    Preconditions.checkArgument(rootObject != null, "rootObject must not be null"); 
    Preconditions.checkArgument(outputStream != null, "outputStream must not be null"); 
    final JAXBContext ctx = JAXBContext.newInstance(rootObject.getClass()); 
    final Document document = getFactories().newDocument(); 
    document.setXmlStandalone(true); 
    final Marshaller marshaller = ctx.createMarshaller(); 
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
    marshaller.setSchema(schema); 
    marshaller.marshal(rootObject, document); 
    createTransformer().transform(new DOMSource(document), new StreamResult(outputStream)); 
    } 

    public static Transformer createTransformer() { 
    final Transformer transformer = getFactories().newTransformer(); 
    transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
    transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); 
    transformer.setOutputProperty(OutputKeys.ENCODING, JAXBDefaults.OUTPUT_CHARSET.name()); 
    transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, CDATA_XML_ELEMENTS); 
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", IDENT_LENGTH); 
    return transformer; 
    } 


    private static class JAXBFactories { 

    private DocumentBuilderFactory documentBuilderFactory; 

    public DocumentBuilderFactory getDocumentBuilderFactory() { 
     if (documentBuilderFactory == null) { 
     documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
     } 
     return documentBuilderFactory; 
    } 

    private DocumentBuilder documentBuilder; 

    public DocumentBuilder getDocumentBuilder() { 
     if (documentBuilder == null) { 
     try { 
      documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); 
     } catch (final ParserConfigurationException ex) { 
      throw new RuntimeException("Failed to create DocumentBuilder", ex); 
     } 
     } 
     return documentBuilder; 
    } 

    public Document newDocument() { 
     return getDocumentBuilder().newDocument(); 
    } 

    private TransformerFactory transformerFactory; 

    public TransformerFactory getTransformerFactory() { 
     if (transformerFactory == null) { 
     transformerFactory = TransformerFactory.newInstance(); 
     } 
     return transformerFactory; 
    } 

    public Transformer newTransformer() { 
     try { 
     return getTransformerFactory().newTransformer(); 
     } catch (final TransformerConfigurationException ex) { 
     throw new RuntimeException("Failed to create Transformer", ex); 
     } 
    } 

    } 

    private static class FactoriesHolder { 

    static final JAXBFactories FACTORIES = new JAXBFactories(); 
    } 

    private static JAXBFactories getFactories() { 
    return FactoriesHolder.FACTORIES; 
    } 

答えて

1

これは、マーシャリングコードがどのように見えるかです。しかし、あなたがきれいな印刷をオフにして(そしてあなたのIDE /エディタにそれをさせておけば)、出力が同じであることが分かります。

綺麗に印刷するXMLは、レイアウトを追加するオリジナルの変形です。もはや xmlです。

+0

お返事ありがとうございます!しかし、私の問題点は、私がXMLを選択した主な理由の1つは、それが提供する人間の読みやすさと、比較ツールとの簡単な比較の可能性でした。今、この余分な行のために、私は一度に複数のファイルを比較するときに多くの偽の違いを得る。 あなたはXML比較のための良いツールを知っていることはありませんか? :) プレーンテキストの比較はうまくいきましたが、新しいラインの問題以外にも多くの問題があります。 – Alex

+0

@Alex - [KDiff](http://kdiff3.sourceforge.net/)は、比較中に空白を無視するように指示できます。これはすべての問題を解決するわけではありませんが、大きな助けになります。 – OldCurmudgeon

関連する問題