如何简化这种解析方法?

我正在编写代码以从文件中解组 XML。我事先不知道 XML 基于哪个模式,所以我尝试使用不同 Jaxb2Marshaller 实例形式的多个模式来解组它。

该方法需要:

  1. 尝试使用每个编组器解组 XML

  2. 如果成功,则返回结果对象

  3. 如果失败,请尝试下一个 marshaller

  4. 如果所有编组器都失败,则抛出带有最后一条错误消息的异常

这是当前代码:

private Object getObject(final byte[] data) throws MyException {

    String lastErrorMessage = "";

    for (final Jaxb2Marshaller marshaller : this.marshallers) {

        try {

            return marshaller.unmarshal(new StreamSource(new ByteArrayInputStream(data)));

        } catch (final XmlMappingException e) {

            LOGGER.warn("Invalid XML", e);

            lastErrorMessage = e.getMessage();

        }

    }

    throw new MyException(lastErrorMessage);

}

我觉得这种方法在不同的抽象层次上做了太多的事情:

  • 迭代编组器

  • 应用编组器

  • 返回结果

  • 捕捉异常

  • 抛出异常

但我看不到简化它的方法。每个编组器都需要 try-catch 块(因为除了最后一个之外,我应该捕获并忽略这些 XmlMappingExceptions)。该块要么返回一个结果对象,要么返回 lastErrorMessage,在迭代下面需要它来抛出 MyException。

我能想到的唯一解决方案是创建一些人为的 Result 类,其中包含结果对象或错误消息,但感觉很笨拙。还有其他见解吗?


白猪掌柜的
浏览 86回答 2
2回答

料青山看我应如是

这个怎么样?public class MultiUnmarshaller {&nbsp; &nbsp; private final List<Jaxb2Marshaller> marshallers;&nbsp; &nbsp; private Object value;&nbsp; &nbsp; private String error;&nbsp; &nbsp; public MultiUnmarshaller(List<Jaxb2Marshaller> marshallers) {&nbsp; &nbsp; &nbsp; &nbsp; this.marshallers = marshallers;&nbsp; &nbsp; }&nbsp; &nbsp; private void init() {&nbsp; &nbsp; &nbsp; &nbsp; error = "No marshallers available";&nbsp; &nbsp; &nbsp; &nbsp; value = null;&nbsp; &nbsp; }&nbsp; &nbsp; public Object getObject(byte[] data) throws MyException {&nbsp; &nbsp; &nbsp; &nbsp; init();&nbsp; &nbsp; &nbsp; &nbsp; Iterator<Jaxb2Marshaller> it = marshallers.iterator();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while(it.hasNext() && errorMessage != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unmarshalObject(marshaller, data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return produceResult();&nbsp; &nbsp; }&nbsp; &nbsp; private void unmarshalObject(Jaxb2Marshaller marshaller, byte[] data) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = marshaller.unmarshal(new StreamSource(new&nbsp; &nbsp; &nbsp;ByteArrayInputStream(data)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error = null;&nbsp; &nbsp; &nbsp; &nbsp; } catch (final XmlMappingException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGGER.warn("Invalid XML", e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error = e.getMessage();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private Object produceResult() {&nbsp; &nbsp; &nbsp; &nbsp; if (error == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return value;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new MyException(error);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}

精慕HU

我想要具有如下粒度的方法:private Object getObject(byte[] data) throws MyException {&nbsp; &nbsp; Result result;&nbsp; &nbsp; for (Jaxb2Marshaller marshaller : this.marshallers) {&nbsp; &nbsp; &nbsp; &nbsp; result = getObject(marshaller, data);&nbsp; &nbsp; }&nbsp; &nbsp; return handleError(result);}private Result getObject(Jaxb2Marshaller marshaller, byte[] data) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; return Result.value(marshaller.unmarshal(new StreamSource(new ByteArrayInputStream(data))));&nbsp; &nbsp; } catch (final XmlMappingException e) {&nbsp; &nbsp; &nbsp; &nbsp; LOGGER.warn("Invalid XML", e);&nbsp; &nbsp; &nbsp; &nbsp; return Result.error(e.getMessage());&nbsp; &nbsp; }}private Object handleError(Result result) {&nbsp; &nbsp; if (result.isError()) {&nbsp; &nbsp; &nbsp; &nbsp; throw new MyException(result.errroMessage);&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; return result.value;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}但是附加的 Result 类是冗长而笨拙的:private class Result {&nbsp; &nbsp; String errorMessage;&nbsp; &nbsp; Object value;&nbsp; &nbsp; static Result error(String errorMessage) {&nbsp; &nbsp; &nbsp; &nbsp; Result result = new Result();&nbsp; &nbsp; &nbsp; &nbsp; result.errorMessage = errorMessage;&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; static Result value(Object value) {&nbsp; &nbsp; &nbsp; &nbsp; Result result = new Result();&nbsp; &nbsp; &nbsp; &nbsp; result.value = value;&nbsp; &nbsp; &nbsp; &nbsp; return result;&nbsp; &nbsp; }&nbsp; &nbsp; boolean isError() {&nbsp; &nbsp; &nbsp; &nbsp; return errorMessage != null;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java