@Test public void testTransformerExceptions() throws TransformerException { String html = "<!DOCTYPE html PUBLIC \"- List<Exception> exceptions = Lists.newArrayList( new NullPointerException(), new TransformerException("transformer exception"), new EmptyStackException()); for (Exception exception : exceptions) { Transformer transformer = Mockito.mock(Transformer.class); Mockito.doThrow(exception) .when(transformer) .transform(Mockito.<Source>anyObject(), Mockito.<Result>anyObject()); PrettyPrinter prettyPrinter = new PrettyPrinter( adsApiConfiguration, logger, xpathSupplier, Suppliers.ofInstance(transformer), documentBuilderSupplier); assertEquals(html, prettyPrinter.prettyPrint(html)); verify(logger).warn("Unable to pretty print XML: {}", exception); } }
@Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } @Inject  PrettyPrinter(
      AdsApiConfiguration adsApiConfiguration,
      @Named("libLogger") Logger libLogger,
      Supplier<XPath> xpathSupplier,
      Supplier<Transformer> transformerSupplier,
      Supplier<DocumentBuilder> documentBuilderSupplier); }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } @Inject  PrettyPrinter(
      AdsApiConfiguration adsApiConfiguration,
      @Named("libLogger") Logger libLogger,
      Supplier<XPath> xpathSupplier,
      Supplier<Transformer> transformerSupplier,
      Supplier<DocumentBuilder> documentBuilderSupplier); @Override String prettyPrint(String xml); }
PrettyPrinter implements PrettyPrinterInterface { @Override public String prettyPrint(String xml) { if (xml == null) { return xml; } Source xmlSource = sanitizeXml(xml); if (xmlSource == null) { xmlSource = new StreamSource(new StringReader(xml)); } String formattedXml = formatXml(xmlSource); return formattedXml != null ? formattedXml : xml; } @Inject  PrettyPrinter(
      AdsApiConfiguration adsApiConfiguration,
      @Named("libLogger") Logger libLogger,
      Supplier<XPath> xpathSupplier,
      Supplier<Transformer> transformerSupplier,
      Supplier<DocumentBuilder> documentBuilderSupplier); @Override String prettyPrint(String xml);  }