View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.cpd;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.util.StringUtil;
    8
    9   import java.util.Iterator;
    10
    11  /***
    12   * @author Philippe T'Seyen
    13   */
    14  public class XMLRenderer implements Renderer {
    15
    16      public String render(Iterator matches) {
    17          StringBuffer buffer = new StringBuffer(300);
    18          buffer.append("<?xml version=\"1.0\"  encoding=\"UTF-8\"?>");
    19          buffer.append("<pmd-cpd>").append(PMD.EOL);
    20          Match match;
    21          while (matches.hasNext()) {
    22              match = (Match) matches.next();
    23              buffer.append("<duplication lines=\"");
    24              buffer.append(match.getLineCount());
    25              buffer.append("\" tokens=\"");
    26              buffer.append(match.getTokenCount());
    27              buffer.append("\">").append(PMD.EOL);
    28
    29              TokenEntry mark;
    30              for (Iterator iterator = match.iterator(); iterator.hasNext();) {
    31                  mark = (TokenEntry) iterator.next();
    32                  buffer.append("<file line=\"");
    33                  buffer.append(mark.getBeginLine());
    34                  buffer.append("\" path=\"");
    35                  buffer.append(mark.getTokenSrcID());
    36                  buffer.append("\"/>").append(PMD.EOL);
    37              }
    38              String codeFragment = match.getSourceCodeSlice();
    39              if (codeFragment != null) {
    40                  buffer.append("<codefragment>").append(PMD.EOL);
    41                  buffer.append("<![CDATA[").append(PMD.EOL);
    42                  buffer.append(StringUtil.replaceString(codeFragment, "]]>", "]]&gt;")).append(PMD.EOL + "]]>" + PMD.EOL + "</codefragment>" + PMD.EOL);
    43              }
    44              buffer.append("</duplication>").append(PMD.EOL);
    45          }
    46          buffer.append("</pmd-cpd>");
    47          return buffer.toString();
    48      }
    49  }