001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.plugins.cpd;
021    
022    import java.io.File;
023    import java.util.List;
024    import java.util.Properties;
025    
026    import net.sourceforge.pmd.cpd.JavaTokenizer;
027    import net.sourceforge.pmd.cpd.Tokenizer;
028    
029    import org.sonar.api.CoreProperties;
030    import org.sonar.api.batch.CpdMapping;
031    import org.sonar.api.resources.Java;
032    import org.sonar.api.resources.JavaFile;
033    import org.sonar.api.resources.Language;
034    import org.sonar.api.resources.Project;
035    import org.sonar.api.resources.Resource;
036    
037    public class JavaCpdMapping implements CpdMapping {
038    
039      private String ignore_literals;
040      private String ignore_identifiers;
041    
042      public JavaCpdMapping(Project project) {
043        ignore_literals = project.getConfiguration().getString(CoreProperties.CPD_IGNORE_LITERALS_PROPERTY,
044            CoreProperties.CPD_IGNORE_LITERALS_DEFAULT_VALUE);
045        ignore_identifiers = project.getConfiguration().getString(CoreProperties.CPD_IGNORE_IDENTIFIERS_PROPERTY,
046            CoreProperties.CPD_IGNORE_IDENTIFIERS_DEFAULT_VALUE);
047      }
048    
049      public Tokenizer getTokenizer() {
050        Properties props = new Properties();
051        props.setProperty(JavaTokenizer.IGNORE_LITERALS, ignore_literals);
052        props.setProperty(JavaTokenizer.IGNORE_IDENTIFIERS, ignore_identifiers);
053        JavaTokenizer tokenizer = new JavaTokenizer();
054        tokenizer.setProperties(props);
055        return tokenizer;
056      }
057    
058      public Resource createResource(File file, List<File> sourceDirs) {
059        return JavaFile.fromIOFile(file, sourceDirs, false);
060      }
061    
062      public Language getLanguage() {
063        return Java.INSTANCE;
064      }
065    }