/**
 * Copyright 2015 LinkedIn Corp. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */
package wherehows;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import wherehows.common.schemas.DatasetJsonRecord;
import wherehows.common.schemas.DatasetSchemaRecord;
import wherehows.common.schemas.SampleDataRecord;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by zsun on 8/18/15.
 */
public class FileAnalyzerFactory {
    private static Logger LOG = LoggerFactory.getLogger(FileAnalyzerFactory.class);
    FileSystem fs;

    public FileAnalyzerFactory(FileSystem fs) {
        this.fs = fs;
        LOG.info("FileAnalyzerFactory init success ");
    }

    // iterate through all possibilities
    public SampleDataRecord getSampleData(Path path, String abstractPath) throws IOException {
        SampleDataRecord sampleData = null;
        FileAnalyzer fileAnalyzer = getRightFileAnalyzer(path);
        LOG.info("class name  is " + fileAnalyzer.getClass().getName());
        LOG.info("file path is "+path.toUri().getPath());
        LOG.info("start get "+path.toUri().getPath()+" SampleDataRecord");
        sampleData = fileAnalyzer.getSampleData(path);
        sampleData.setAbstractPath(abstractPath);
        return sampleData;
    }

    public DatasetJsonRecord getSchema(Path path, String abstractPath)
            throws IOException {
        DatasetJsonRecord schema = null;
        FileAnalyzer fileAnalyzer = getRightFileAnalyzer(path);
        LOG.info("try file analyzer: " + fileAnalyzer.STORAGE_TYPE);
        LOG.info("file path is "+path.toUri().getPath());
        LOG.info("start get "+path.toUri().getPath()+" DataSetJsonRecord");
        schema = fileAnalyzer.getSchema(path);
        schema.setAbstractPath(abstractPath);
        return schema;
    }


    private FileAnalyzer getRightFileAnalyzer(Path path){
        FileAnalyzer analyzer = null;
        String rightPath = path.toUri().getPath();
        if(rightPath.endsWith(".xml" )&& !rightPath.startsWith(".xml")){
            analyzer = new XMLFileAnalyzer(fs);
        }
        if(rightPath.endsWith(".avro") && !rightPath.startsWith(".avro")){
            analyzer = new AvroFileAnalyzer(fs);
        }
        if(rightPath.endsWith(".orc") && !rightPath.startsWith(".orc")){
            analyzer = new OrcFileAnalyzer(fs);
        }
        if(analyzer == null){
            LOG.info("can not get path : "+path.toUri().getPath()+" fileanalyzer");
        }
        return analyzer;
    }
}

@@ -822,16 +822,37 @@
 ecord;%0A%0A
+import java.io.File;%0A
 import j
@@ -1100,16 +1100,85 @@
 stem fs;
+%0A    List%3CFileAnalyzer%3E allAnalyzers = new ArrayList%3CFileAnalyzer%3E();
 %0A%0A    pu
@@ -1475,32 +1475,33 @@
 pleData = null;%0A
+%0A
         FileAnal
@@ -1746,32 +1746,49 @@
 leDataRecord%22);%0A
+        try%7B%0A    
         sampleDa
@@ -1826,32 +1826,36 @@
 (path);%0A        
+    
 sampleData.setAb
@@ -1872,32 +1872,112 @@
 (abstractPath);%0A
+%0A        %7Dcatch (Exception e)%7B%0A            LOG.info(e.getMessage());%0A        %7D%0A%0A
         return s
@@ -2398,32 +2398,49 @@
 etJsonRecord%22);%0A
+        try%7B%0A    
         schema =
@@ -2462,32 +2462,36 @@
 etSchema(path);%0A
+    
         schema.s
@@ -2516,24 +2516,103 @@
 tractPath);%0A
+        %7Dcatch (Exception e)%7B%0A            LOG.info(e.getMessage());%0A        %7D%0A%0A
         retu
@@ -2686,16 +2686,44 @@
 th path)
+ throws NullPointerException
 %7B%0A      
@@ -3205,131 +3205,8 @@
   %7D%0A
-        if(analyzer == null)%7B%0A            LOG.info(%22can not get path : %22+path.toUri().getPath()+%22 fileanalyzer%22);%0A        %7D
 %0A   

add xml test