简体   繁体   English

Jaxb编组器的实现和处理IOException

[英]Jaxb marshaller implementation and handling IOException

We are having some issue with XML file generation using JAXB.(The generated file is corrupted/jumbled even if we validate against XSD). 使用JAXB生成XML文件时遇到了一些问题(即使我们针对XSD进行了验证,生成的文件也已损坏/混乱)。 But no errors are reported in server logs(catalina.out) 但是服务器日志中未报告任何错误(catalina.out)

While we were doing the investigations, we happened to found the following code segment in com.sun.xml.internal.bind.v2.runtime.MarshallerImpl(This is the runtime class used by java to marshall the JAXB objects to XML). 在进行调查时,我们偶然在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;
}

The method write(Object obj, XmlOutput out, Runnable postInitAction) calls the cleanup method in the finally clause and there is the flushing is happening. 方法write(Object obj,XmlOutput out,Runnable postInitAction)在finally子句中调用cleanup方法,并且发生刷新。 But in this cleanup method while flushing if any IOException happens, they ignore the exception. 但是在此清除方法中,如果发生任何IOException刷新,它们将忽略该异常。

We are wondering, whether this segments generated the corrupted XML as it does not throw back the exception and simply eats up the exception. 我们想知道,这些段是否生成了损坏的XML,因为它不会抛出异常,而只会吞噬异常。

Also we have found the following statement in API documentation of XML serializer. 我们还在XML序列化器的API文档中找到以下语句。

class: org.apache.xml.serialize.XMLSerializer 类:org.apache.xml.serialize.XMLSerializer

If an I/O exception occurs while serializing, the serializer will not throw an exception directly, but only throw it at the end of serializing (either DOM or SAX's DocumentHandler.endDocument(). 如果序列化时发生I / O异常,则序列化程序将不会直接引发异常,而只会在序列化结束时(DOM或SAX的DocumentHandler.endDocument()。

Any help is appreciated. 任何帮助表示赞赏。

Regards, Mayuran 此致Mayuran

The IOException is not being ignore its being wrapped in a MarshalException and re thrown. IOException不会被忽略,它被包装在MarshalException并被重新抛出。 The underlying issue should be present in the MarshalException you are receiving. 您收到的MarshalException应该存在根本问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM