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      public String render(Iterator matches) {
    16          StringBuffer buffer = new StringBuffer();
    17          buffer.append("<?xml version=\"1.0\"  encoding=\"UTF-8\"?>");
    18          buffer.append("<pmd-cpd>" + PMD.EOL);
    19          for (; matches.hasNext();) {
    20              Match match = (Match) matches.next();
    21              buffer.append("<duplication");
    22              buffer.append(" lines=\"");
    23              buffer.append(match.getLineCount());
    24              buffer.append("\"");
    25              buffer.append(" tokens=\"");
    26              buffer.append(match.getTokenCount());
    27              buffer.append("\">" + PMD.EOL);
    28
    29              for (Iterator iterator = match.iterator(); iterator.hasNext();) {
    30                  TokenEntry mark = (TokenEntry) iterator.next();
    31                  buffer.append("<file");
    32                  buffer.append(" line=\"");
    33                  buffer.append(mark.getBeginLine());
    34                  buffer.append("\"");
    35                  buffer.append(" path=\"");
    36                  buffer.append(mark.getTokenSrcID());
    37                  buffer.append("\"/>" + PMD.EOL);
    38              }
    39              String codeFragment = match.getSourceCodeSlice();
    40              if (codeFragment != null) {
    41                  buffer.append("<codefragment>" + PMD.EOL + "<![CDATA[" + PMD.EOL + StringUtil.replaceString(codeFragment, "]]>", "]]&gt;") + PMD.EOL + "]]>" + PMD.EOL + "</codefragment>" + PMD.EOL);
    42              }
    43              buffer.append("</duplication>" + PMD.EOL);
    44          }
    45          buffer.append("</pmd-cpd>");
    46          return buffer.toString();
    47      }
    48  }