/*
 * The MIT License
 *
 * Copyright 2016 Fundacion Jala.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.fundacionjala.enforce.sonarqube.apex;

import com.sonar.sslr.api.Grammar;
import java.io.File;
import org.sonar.squidbridge.AstScanner;
import org.sonar.squidbridge.SquidAstVisitor;
import org.sonar.squidbridge.api.SourceFile;

/**
 * Utility class for scans a file and generate {@link SourceFile}
 */
public class ApexAstScanner {

    /**
     * Default constructor.
     */
    private ApexAstScanner() {
    }

    /**
     * Returns a scanner from configuration and visitors.
     *
     * @param conf apex configuration.
     * @param visitors list of visitors.
     * @return a scanner.
     */
    public static AstScanner<Grammar> create(ApexConfiguration conf, SquidAstVisitor<Grammar>... visitors) {
        return null;
    }

    /**
     * Returns a source file from file and visitors.
     * @param file
     * @param visitors
     * @return a source.
     */
    public static SourceFile scanFile(File file, SquidAstVisitor<Grammar>... visitors) {
        return null;
    }
}

@@ -1193,55 +1193,223 @@
 ort 
-com.sonar.sslr.api.Grammar;%0Aimport java.io.File
+java.io.File;%0Aimport java.util.Collection;%0Aimport com.google.common.base.Charsets;%0Aimport com.sonar.sslr.api.Grammar;%0Aimport com.sonar.sslr.impl.Parser;%0Aimport org.fundacionjala.enforce.sonarqube.apex.api.ApexMetric
 ;%0Aim
@@ -1526,21 +1526,221 @@
 dge.
-api.SourceFil
+SquidAstVisitorContextImpl;%0Aimport org.sonar.squidbridge.api.SourceCode;%0Aimport org.sonar.squidbridge.api.SourceFile;%0Aimport org.sonar.squidbridge.api.SourceProject;%0Aimport org.sonar.squidbridge.indexer.QueryByTyp
 e;%0A%0A
@@ -2226,35 +2226,629 @@
 ) %7B%0A        
-return null
+final SourceProject sourceProject = new SourceProject(%22Apex Project%22);%0A        final SquidAstVisitorContextImpl%3CGrammar%3E context = new SquidAstVisitorContextImpl%3C%3E(sourceProject);%0A        final Parser%3CGrammar%3E parser = ApexParser.create(conf);%0A%0A        AstScanner.Builder%3CGrammar%3E builder = AstScanner.%3CGrammar%3Ebuilder(context).setBaseParser(parser);%0A%0A        builder.withMetrics(ApexMetric.values());%0A%0A        builder.setFilesMetric(ApexMetric.FILES);%0A%0A        for (SquidAstVisitor%3CGrammar%3E visitor : visitors) %7B%0A            builder.withSquidAstVisitor(visitor);%0A        %7D%0A%0A        return builder.build()
 ;%0A    %7D%0A%0A   
@@ -2897,32 +2897,39 @@
 e and visitors.%0A
+     *%0A
      * @param fi
@@ -2930,16 +2930,29 @@
 ram file
+ source file.
 %0A     * 
@@ -2966,16 +2966,34 @@
 visitors
+ list of visitors.
 %0A     * 
@@ -3119,25 +3119,588 @@
     
-return null;%0A    %7D
+if (!file.isFile()) %7B%0A            throw new IllegalArgumentException(%22File '%22 + file + %22' not found.%22);%0A        %7D%0A        AstScanner%3CGrammar%3E scanner = create(new ApexConfiguration(Charsets.UTF_8), visitors);%0A        scanner.scanFile(file);%0A        Collection%3CSourceCode%3E sources = scanner.getIndex().search(new QueryByType(SourceFile.class));%0A        if (sources.size() != 1) %7B%0A            throw new IllegalStateException(%22Only one SourceFile was expected whereas %22 + sources.size() + %22 has been returned.%22);%0A        %7D%0A        return (SourceFile) sources.iterator().next();%0A    %7D%0A
 %0A%7D%0A

Implement a basic logic to create an ASTScanner