簡體   English   中英

Jaxb編組器的實現和處理IOException

[英]Jaxb marshaller implementation and handling IOException

使用JAXB生成XML文件時遇到了一些問題(即使我們針對XSD進行了驗證,生成的文件也已損壞/混亂)。 但是服務器日志中未報告任何錯誤(catalina.out)

在進行調查時,我們偶然在com.sun.xml.internal.bind.v2.runtime.MarshallerImpl(這是java用來將JAXB對象編組為XML的運行時類)中找到以下代碼段。

    /**
 * All the marshal method invocation eventually comes down to this call.
 */
private void write(Object obj, XmlOutput out, Runnable postInitAction) throws JAXBException {
    try {
        if( obj == null )
            throw new IllegalArgumentException(Messages.NOT_MARSHALLABLE.format());

        if( schema!=null ) {
            // send the output to the validator as well
            ValidatorHandler validator = schema.newValidatorHandler();
            validator.setErrorHandler(new FatalAdapter(serializer));
            // work around a bug in JAXP validator in Tiger
            XMLFilterImpl f = new XMLFilterImpl() {
                @Override
                public void startPrefixMapping(String prefix, String uri) throws SAXException {
                    super.startPrefixMapping(prefix.intern(), uri.intern());
                }
            };
            f.setContentHandler(validator);
            out = new ForkXmlOutput( new SAXOutput(f) {
                @Override
                public void startDocument(XMLSerializer serializer, boolean fragment, int[] nsUriIndex2prefixIndex, NamespaceContextImpl nsContext) throws SAXException, IOException, XMLStreamException {
                    super.startDocument(serializer, false, nsUriIndex2prefixIndex, nsContext);
                }
                @Override
                public void endDocument(boolean fragment) throws SAXException, IOException, XMLStreamException {
                    super.endDocument(false);
                }
            }, out );
        }

        try {
            prewrite(out,isFragment(),postInitAction);
            serializer.childAsRoot(obj);
            postwrite();
        } catch( SAXException e ) {
            throw new MarshalException(e);
        } catch (IOException e) {
            throw new MarshalException(e);
        } catch (XMLStreamException e) {
            throw new MarshalException(e);
        } finally {
            serializer.close();
        }
    } finally {
        cleanUp();
    }
}

private void cleanUp() {
    if(toBeFlushed!=null)
        try {
            toBeFlushed.flush();
        } catch (IOException e) {
            // ignore
        }
    if(toBeClosed!=null)
        try {
            toBeClosed.close();
        } catch (IOException e) {
            // ignore
        }
    toBeFlushed = null;
    toBeClosed = null;
}

方法write(Object obj,XmlOutput out,Runnable postInitAction)在finally子句中調用cleanup方法,並且發生刷新。 但是在此清除方法中,如果發生任何IOException刷新,它們將忽略該異常。

我們想知道,這些段是否生成了損壞的XML,因為它不會拋出異常,而只會吞噬異常。

我們還在XML序列化器的API文檔中找到以下語句。

類:org.apache.xml.serialize.XMLSerializer

如果序列化時發生I / O異常,則序列化程序將不會直接引發異常,而只會在序列化結束時(DOM或SAX的DocumentHandler.endDocument()。

任何幫助表示贊賞。

此致Mayuran

IOException不會被忽略,它被包裝在MarshalException並被重新拋出。 您收到的MarshalException應該存在根本問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM