@Test public void testSequence() throws Exception { ServiceException exception = ServiceExceptionParser.parse( mockStream( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" + "<ServiceExceptionReport>\n" + " <ServiceException code=\"42\">test</ServiceException>\n" + " <ServiceException code=\"20\">another test</ServiceException>\n" + " <ServiceException code=\"60\">yet another test</ServiceException>\n" + "</ServiceExceptionReport>")); Assert.assertThat( exception, both(hasProperty("message", equalTo("test"))) .and(hasProperty("code", equalTo("42")))); Assert.assertThat( exception.getNext(), both(hasProperty("message", equalTo("another test"))) .and(hasProperty("code", equalTo("20")))); Assert.assertThat( exception.getNext().getNext(), both(hasProperty("message", equalTo("yet another test"))) .and(hasProperty("code", equalTo("60")))); Assert.assertThat(exception.getNext().getNext().getNext(), nullValue()); }
public static ServiceException parse(InputStream inputStream) throws IOException { List<ServiceException> exceptions = new ArrayList<>(); try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(IS_COALESCING, false); inputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false); inputFactory.setProperty(SUPPORT_DTD, false); XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream, "UTF-8"); int tag; while ((tag = reader.next()) != END_DOCUMENT) { if (tag == START_ELEMENT && reader.getLocalName().equals("ServiceException")) { String code = parseServiceExceptionCode(reader); String errorMessage = parseServiceExceptionMessage(reader); exceptions.add(new ServiceException(errorMessage, code)); } } } catch (XMLStreamException e) { throw new IOException(e); } Collections.sort(exceptions, ServiceExceptionParser::compare); for (int i = 0; i < exceptions.size() - 1; i++) { exceptions.get(i).setNext(exceptions.get(i + 1)); } return exceptions.isEmpty() ? null : exceptions.get(0); }
ServiceExceptionParser { public static ServiceException parse(InputStream inputStream) throws IOException { List<ServiceException> exceptions = new ArrayList<>(); try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(IS_COALESCING, false); inputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false); inputFactory.setProperty(SUPPORT_DTD, false); XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream, "UTF-8"); int tag; while ((tag = reader.next()) != END_DOCUMENT) { if (tag == START_ELEMENT && reader.getLocalName().equals("ServiceException")) { String code = parseServiceExceptionCode(reader); String errorMessage = parseServiceExceptionMessage(reader); exceptions.add(new ServiceException(errorMessage, code)); } } } catch (XMLStreamException e) { throw new IOException(e); } Collections.sort(exceptions, ServiceExceptionParser::compare); for (int i = 0; i < exceptions.size() - 1; i++) { exceptions.get(i).setNext(exceptions.get(i + 1)); } return exceptions.isEmpty() ? null : exceptions.get(0); } }
ServiceExceptionParser { public static ServiceException parse(InputStream inputStream) throws IOException { List<ServiceException> exceptions = new ArrayList<>(); try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(IS_COALESCING, false); inputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false); inputFactory.setProperty(SUPPORT_DTD, false); XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream, "UTF-8"); int tag; while ((tag = reader.next()) != END_DOCUMENT) { if (tag == START_ELEMENT && reader.getLocalName().equals("ServiceException")) { String code = parseServiceExceptionCode(reader); String errorMessage = parseServiceExceptionMessage(reader); exceptions.add(new ServiceException(errorMessage, code)); } } } catch (XMLStreamException e) { throw new IOException(e); } Collections.sort(exceptions, ServiceExceptionParser::compare); for (int i = 0; i < exceptions.size() - 1; i++) { exceptions.get(i).setNext(exceptions.get(i + 1)); } return exceptions.isEmpty() ? null : exceptions.get(0); }  }
ServiceExceptionParser { public static ServiceException parse(InputStream inputStream) throws IOException { List<ServiceException> exceptions = new ArrayList<>(); try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(IS_COALESCING, false); inputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false); inputFactory.setProperty(SUPPORT_DTD, false); XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream, "UTF-8"); int tag; while ((tag = reader.next()) != END_DOCUMENT) { if (tag == START_ELEMENT && reader.getLocalName().equals("ServiceException")) { String code = parseServiceExceptionCode(reader); String errorMessage = parseServiceExceptionMessage(reader); exceptions.add(new ServiceException(errorMessage, code)); } } } catch (XMLStreamException e) { throw new IOException(e); } Collections.sort(exceptions, ServiceExceptionParser::compare); for (int i = 0; i < exceptions.size() - 1; i++) { exceptions.get(i).setNext(exceptions.get(i + 1)); } return exceptions.isEmpty() ? null : exceptions.get(0); }  static ServiceException parse(InputStream inputStream); }
ServiceExceptionParser { public static ServiceException parse(InputStream inputStream) throws IOException { List<ServiceException> exceptions = new ArrayList<>(); try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(IS_COALESCING, false); inputFactory.setProperty(IS_REPLACING_ENTITY_REFERENCES, false); inputFactory.setProperty(SUPPORT_DTD, false); XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream, "UTF-8"); int tag; while ((tag = reader.next()) != END_DOCUMENT) { if (tag == START_ELEMENT && reader.getLocalName().equals("ServiceException")) { String code = parseServiceExceptionCode(reader); String errorMessage = parseServiceExceptionMessage(reader); exceptions.add(new ServiceException(errorMessage, code)); } } } catch (XMLStreamException e) { throw new IOException(e); } Collections.sort(exceptions, ServiceExceptionParser::compare); for (int i = 0; i < exceptions.size() - 1; i++) { exceptions.get(i).setNext(exceptions.get(i + 1)); } return exceptions.isEmpty() ? null : exceptions.get(0); }  static ServiceException parse(InputStream inputStream);  }