Ejemplo n.º 1
0
  protected void prepareHTMLView(InputStream older, InputStream newer) throws Exception {
    InputSource oldSource = new InputSource(older);
    InputSource newSource = new InputSource(newer);

    HtmlCleaner cleaner = new HtmlCleaner();
    Locale locale = Locale.getDefault();

    String prefix = "diff";

    DomTreeBuilder oldHandler;
    DomTreeBuilder newHandler;
    try {
      oldHandler = new DomTreeBuilder();
      cleaner.cleanAndParse(oldSource, oldHandler);
      newHandler = new DomTreeBuilder();
      cleaner.cleanAndParse(newSource, newHandler);
    } catch (Exception e) {
      throw new UsecaseException("Error while parsing document for diffing: ", e);
    }
    TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale);
    TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale);

    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler result = tf.newTransformerHandler();

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    Result domResult = new StreamResult(out);
    result.setResult(domResult);

    ContentHandler htmlDiff = result;
    // Document content filtering
    // XslFilter filter = new XslFilter();
    // ContentHandler postProcess = filter.xsl(result, "htmldiff.xsl");
    try {
      startDiffDocument(htmlDiff);
      SimpleDiffOutput output = new SimpleDiffOutput(htmlDiff, prefix);
      BodyNode diffNode = HTMLDiffer.diff(leftComparator, rightComparator);
      output.toHTML(diffNode);
      finishDiffDocument(htmlDiff);
      setParameter(
          "html-output",
          DocumentBuilderFactory.newInstance()
              .newDocumentBuilder()
              .parse(new ByteArrayInputStream(out.toByteArray())));
    } catch (Exception e) {
      throw new UsecaseException("Failed translating diff document to xml: ", e);
    }
  }
  public void handle(
      String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    baseRequest.setHandled(true);

    boolean htmlOut = false;

    try {
      String oldString = request.getParameter("old");
      String newString = request.getParameter("new");

      if (oldString == null || newString == null) {
        return;
      }

      InputStream oldStream = new ByteArrayInputStream(oldString.getBytes());
      InputStream newStream = new ByteArrayInputStream(newString.getBytes());

      SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
      TransformerHandler result = tf.newTransformerHandler();
      result.setResult(new StreamResult(response.getOutputStream()));

      String[] css = new String[] {};
      XslFilter filter = new XslFilter();
      ContentHandler postProcess = htmlOut ? filter.xsl(result, "diff.xsl") : result;

      Locale locale = Locale.getDefault();
      String prefix = "diff";

      HtmlCleaner cleaner = new HtmlCleaner();
      DomTreeBuilder oldHandler = new DomTreeBuilder();

      InputSource oldSource = new InputSource(oldStream);
      oldSource.setEncoding("UTF-8");
      InputSource newSource = new InputSource(newStream);
      newSource.setEncoding("UTF-8");
      cleaner.cleanAndParse(oldSource, oldHandler);

      TextNodeComparator leftComparator = new TextNodeComparator(oldHandler, locale);

      DomTreeBuilder newHandler = new DomTreeBuilder();
      cleaner.cleanAndParse(newSource, newHandler);
      TextNodeComparator rightComparator = new TextNodeComparator(newHandler, locale);

      postProcess.startDocument();
      postProcess.startElement("", "div", "div", new AttributesImpl());
      doCSS(css, postProcess);
      postProcess.startElement("", "diff", "diff", new AttributesImpl());
      HtmlSaxDiffOutput output = new HtmlSaxDiffOutput(postProcess, prefix);

      HTMLDiffer differ = new HTMLDiffer(output);
      differ.diff(leftComparator, rightComparator);

      postProcess.endElement("", "diff", "diff");
      postProcess.endElement("", "div", "div");
      postProcess.endDocument();
    } catch (Throwable e) {
      e.printStackTrace();
      if (e.getCause() != null) {
        e.getCause().printStackTrace();
      }
      if (e instanceof SAXException) {
        ((SAXException) e).getException().printStackTrace();
      }
    }
  }