Search in sources :

Example 1 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project maven-plugins by apache.

the class CpdReportGenerator method generate.

/**
     * Method that generates the contents of the CPD report
     *
     * @param matches
     */
@SuppressWarnings("deprecation")
public void generate(Iterator<Match> matches) {
    beginDocument();
    if (!matches.hasNext()) {
        sink.paragraph();
        sink.text(bundle.getString("report.cpd.noProblems"));
        sink.paragraph_();
    }
    while (matches.hasNext()) {
        Match match = matches.next();
        String code = match.getSourceCodeSlice();
        sink.table();
        sink.tableRow();
        sink.tableHeaderCell();
        sink.text(bundle.getString("report.cpd.column.file"));
        sink.tableHeaderCell_();
        if (aggregate) {
            sink.tableHeaderCell();
            sink.text(bundle.getString("report.cpd.column.project"));
            sink.tableHeaderCell_();
        }
        sink.tableHeaderCell();
        sink.text(bundle.getString("report.cpd.column.line"));
        sink.tableHeaderCell_();
        sink.tableRow_();
        // Iterating on every token entry
        for (Iterator<Mark> occurrences = match.iterator(); occurrences.hasNext(); ) {
            TokenEntry mark = occurrences.next().getToken();
            generateFileLine(mark);
        }
        // Source snippet
        sink.tableRow();
        int colspan = 2;
        if (aggregate) {
            ++colspan;
        }
        // TODO Cleaner way to do this?
        sink.rawText("<td colspan='" + colspan + "'>");
        sink.verbatim(false);
        sink.text(code);
        sink.verbatim_();
        sink.rawText("</td>");
        sink.tableRow_();
        sink.table_();
    }
    sink.section1_();
    sink.body_();
    sink.flush();
    sink.close();
}
Also used : TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) Mark(net.sourceforge.pmd.cpd.Mark) Match(net.sourceforge.pmd.cpd.Match)

Example 2 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project maven-plugins by apache.

the class CpdReportTest method testWriteNonHtml.

public void testWriteNonHtml() throws Exception {
    File testPom = new File(getBasedir(), "src/test/resources/unit/default-configuration/cpd-default-configuration-plugin-config.xml");
    CpdReport mojo = (CpdReport) lookupMojo("cpd", testPom);
    assertNotNull(mojo);
    TokenEntry tFirstEntry = new TokenEntry("public java", "MyClass.java", 2);
    TokenEntry tSecondEntry = new TokenEntry("public java", "MyClass3.java", 2);
    String duplicatedCodeFragment = "// ----- duplicated code example -----";
    SourceCode sourceCodeFirst = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass.java"));
    SourceCode sourceCodeSecond = new SourceCode(new SourceCode.StringCodeLoader(PMD.EOL + duplicatedCodeFragment + PMD.EOL, "MyClass3.java"));
    List<Match> tList = new ArrayList<>();
    Mark tFirstMark = new Mark(tFirstEntry);
    tFirstMark.setSourceCode(sourceCodeFirst);
    tFirstMark.setLineCount(1);
    Mark tSecondMark = new Mark(tSecondEntry);
    tSecondMark.setSourceCode(sourceCodeSecond);
    tSecondMark.setLineCount(1);
    Match tMatch = new Match(2, tFirstMark, tSecondMark);
    tList.add(tMatch);
    CPDConfiguration cpdConfiguration = new CPDConfiguration();
    cpdConfiguration.setMinimumTileSize(100);
    cpdConfiguration.setLanguage(new JavaLanguage());
    cpdConfiguration.setEncoding("UTF-8");
    CPD tCpd = new MockCpd(cpdConfiguration, tList.iterator());
    tCpd.go();
    mojo.writeNonHtml(tCpd);
    File tReport = new File("target/test/unit/default-configuration/target/cpd.xml");
    // parseDocument( new BufferedInputStream( new FileInputStream( report ) ) );
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document pmdCpdDocument = builder.parse(tReport);
    assertNotNull(pmdCpdDocument);
    String str = readFile(new File(getBasedir(), "target/test/unit/default-configuration/target/cpd.xml"));
    assertTrue(lowerCaseContains(str, "MyClass.java"));
    assertTrue(lowerCaseContains(str, "MyClass3.java"));
    assertTrue(lowerCaseContains(str, duplicatedCodeFragment));
}
Also used : SourceCode(net.sourceforge.pmd.cpd.SourceCode) CPDConfiguration(net.sourceforge.pmd.cpd.CPDConfiguration) ArrayList(java.util.ArrayList) Mark(net.sourceforge.pmd.cpd.Mark) JavaLanguage(net.sourceforge.pmd.cpd.JavaLanguage) Document(org.w3c.dom.Document) Match(net.sourceforge.pmd.cpd.Match) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CPD(net.sourceforge.pmd.cpd.CPD) File(java.io.File)

Example 3 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project pmd by pmd.

the class ScalaTokenizer method tokenize.

public void tokenize(SourceCode source, Tokens cpdTokens) {
    String filename = source.getFileName();
    try {
        Lexer lexer = new Lexer();
        List<Token> tokens = lexer.getTokensOfFile(filename);
        for (Token token : tokens) {
            String tokenVal = token.tokenVal() != null ? token.tokenVal() : Integer.toString(token.tokenType());
            TokenEntry cpdToken = new TokenEntry(tokenVal, filename, token.line());
            cpdTokens.add(cpdToken);
        }
        cpdTokens.add(TokenEntry.getEOF());
    } catch (RuntimeException e) {
        e.printStackTrace();
        // option
        throw new TokenMgrError("Lexical error in file " + filename + ". The scala tokenizer exited with error: " + e.getMessage(), TokenMgrError.LEXICAL_ERROR);
    }
}
Also used : Lexer(org.sonar.plugins.scala.compiler.Lexer) TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) Token(org.sonar.plugins.scala.compiler.Token) TokenMgrError(net.sourceforge.pmd.lang.ast.TokenMgrError)

Example 4 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project pmd-eclipse-plugin by pmd.

the class CPDViewLabelProvider2 method getColumnImage.

/*
     * @see
     * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang.
     * Object, int)
     */
public Image getColumnImage(Object element, int columnIndex) {
    Image image = null;
    final TreeNode node = (TreeNode) element;
    final Object value = node.getValue();
    // if the Element is a Match or TokenEntry
    switch(columnIndex) {
        case 0:
            if (value instanceof Match) {
            // image =
            // PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJS_ERROR_TSK);
            } else if (value instanceof TokenEntry) {
                image = PlatformUI.getWorkbench().getSharedImages().getImage(SharedImages.IMG_OPEN_MARKER);
            }
            break;
        default:
    }
    return image;
}
Also used : TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) TreeNode(org.eclipse.jface.viewers.TreeNode) Image(org.eclipse.swt.graphics.Image) Match(net.sourceforge.pmd.cpd.Match)

Example 5 with TokenEntry

use of net.sourceforge.pmd.cpd.TokenEntry in project pmd-eclipse-plugin by pmd.

the class CPDViewLabelProvider method getColumnText.

/*
     * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
     */
public String getColumnText(Object element, int columnIndex) {
    final TreeNode node = (TreeNode) element;
    final Object value = node.getValue();
    String result = "";
    switch(columnIndex) {
        // show the message
        case 2:
            if (value instanceof Match) {
                final Match match = (Match) value;
                final StringBuilder buffer = new StringBuilder(50);
                buffer.append("Found suspect cut & paste (");
                buffer.append(match.getMarkCount()).append(" matches,");
                buffer.append(match.getLineCount());
                if (match.getLineCount() == 1) {
                    buffer.append(" line)");
                } else {
                    buffer.append(" lines)");
                }
                result = buffer.toString();
            } else if (value instanceof TokenEntry) {
                final TokenEntry entry = (TokenEntry) value;
                final Match match = (Match) node.getParent().getValue();
                final int startLine = entry.getBeginLine();
                final int endLine = entry.getBeginLine() + match.getLineCount() - 1;
                final IPath path = Path.fromOSString(entry.getTokenSrcID());
                final StringBuilder buffer = new StringBuilder(100);
                if (startLine == endLine) {
                    buffer.append("line ").append(startLine);
                } else {
                    buffer.append("lines ").append(startLine).append('-').append(endLine);
                }
                buffer.append(" in file ").append(path.lastSegment());
                result = buffer.toString();
            }
            break;
        case 3:
            if (value instanceof TokenEntry) {
                final TokenEntry entry = (TokenEntry) value;
                final IPath path = Path.fromOSString(entry.getTokenSrcID());
                final IResource resource = ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(path);
                if (resource != null) {
                    result = resource.getProjectRelativePath().removeFileExtension().toString().replace(IPath.SEPARATOR, '.');
                }
            }
            break;
        default:
    }
    return result;
}
Also used : TokenEntry(net.sourceforge.pmd.cpd.TokenEntry) IPath(org.eclipse.core.runtime.IPath) TreeNode(org.eclipse.jface.viewers.TreeNode) IResource(org.eclipse.core.resources.IResource) Match(net.sourceforge.pmd.cpd.Match)

Aggregations

TokenEntry (net.sourceforge.pmd.cpd.TokenEntry)15 Match (net.sourceforge.pmd.cpd.Match)8 File (java.io.File)4 Mark (net.sourceforge.pmd.cpd.Mark)4 SourceCode (net.sourceforge.pmd.cpd.SourceCode)4 TreeNode (org.eclipse.jface.viewers.TreeNode)4 ArrayList (java.util.ArrayList)3 Tokens (net.sourceforge.pmd.cpd.Tokens)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 CPD (net.sourceforge.pmd.cpd.CPD)2 CPDConfiguration (net.sourceforge.pmd.cpd.CPDConfiguration)2 JavaLanguage (net.sourceforge.pmd.cpd.JavaLanguage)2 Image (org.eclipse.swt.graphics.Image)2 Test (org.junit.Test)2 Document (org.w3c.dom.Document)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Tokenizer (net.sourceforge.pmd.cpd.Tokenizer)1 TokenMgrError (net.sourceforge.pmd.lang.ast.TokenMgrError)1