/**
 * 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.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by zsun on 8/18/15.
 */
public class FileAnalyzerFactory {
    List<FileAnalyzer> allFileAnalyzer;

    public FileAnalyzerFactory(FileSystem fs) {
        allFileAnalyzer = new ArrayList<FileAnalyzer>();
        allFileAnalyzer.add(new AvroFileAnalyzer(fs));
        allFileAnalyzer.add(new HiveExportFileAnalyzer(fs));
        allFileAnalyzer.add(new XMLFileAnalyzer(fs));

        // allFileAnalyzer.add(new OrcFileAnalyzer(fs));
        // linkedin specific
        // allFileAnalyzer.add(new BinaryJsonFileAnalyzer(fs));
    }

    // iterate through all possibilities
    public SampleDataRecord getSampleData(Path path, String abstractPath) {
        SampleDataRecord sampleData = null;
        for (FileAnalyzer fileAnalyzer : allFileAnalyzer) {
            try {
                sampleData = fileAnalyzer.getSampleData(path);
                sampleData.setAbstractPath(abstractPath);
            } catch (Exception ignored) {
                ignored.printStackTrace();
                System.out.println("[get sample data] Debug: " + ignored);
            }
            if (sampleData != null) {
                break;
            }
        }
        return sampleData;
    }

    public DatasetJsonRecord getSchema(Path path, String abstractPath)
            throws IOException {
        DatasetJsonRecord schema = null;
        for (FileAnalyzer fileAnalyzer : allFileAnalyzer) {
            System.out.println("try file analyzer: " + fileAnalyzer.STORAGE_TYPE);
            try {
                schema = fileAnalyzer.getSchema(path);
                schema.setAbstractPath(abstractPath);
            } catch (Exception ignored) {
                ignored.printStackTrace();
                System.out.println("[get schema] Debug: " + ignored);
            }
            if (schema != null) {
                break;
            }
        }
        return schema;
    }

    /*private FileAnalyzer getRightFileAnalyzer(Path path) throws NullPointerException{
        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);
        }

        return analyzer;
    }*/
}




@@ -1006,265 +1006,156 @@
     
-List%3CFileAnalyzer%3E allFileAnalyzer;%0A%0A    public FileAnalyzerFactory(FileSystem fs) %7B%0A        allFileAnalyzer = new ArrayList%3CFileAnalyzer%3E();%0A    
+private static Logger LOG = LoggerFactory.getLogger(FileAnalyzerFactory.class);%0A%0A
     
-all
 File
-Analyzer.add(new AvroFileAnalyzer(fs));%0A        allFileAnalyzer.add(new HiveExportFileAnalyzer(fs));
+System fs;%0A%0A    public FileAnalyzerFactory(FileSystem fs) %7B
 %0A   
@@ -1163,205 +1163,79 @@
     
-allFileAnalyzer.add(new XMLFileAnalyzer(fs));%0A%0A        // allFileAnalyzer.add(new OrcFileAnalyzer(fs));%0A        // linkedin specific%0A        // allFileAnalyzer.add(new BinaryJsonFileAnalyzer(fs)
+this.fs = fs;%0A        LOG.info(%22FileAnalyzerFactory init success !%22
 );%0A
+%0A
     
@@ -1398,37 +1398,32 @@
 = null;%0A        
-for (
 FileAnalyzer fil
@@ -1415,37 +1415,33 @@
 ileAnalyzer 
-fileA
+a
 nalyzer 
 : allFileAna
@@ -1420,37 +1420,42 @@
 alyzer analyzer 
-: all
+= getRight
 FileAnalyzer) %7B%0A
@@ -1442,32 +1442,68 @@
 ightFileAnalyzer
+(path);%0A        if (analyzer != null
 ) %7B%0A            
@@ -1533,29 +1533,25 @@
 ampleData = 
-fileA
+a
 nalyzer.getS
@@ -1648,39 +1648,33 @@
 atch (Exception 
-ignored
+e
 ) %7B%0A            
@@ -1669,39 +1669,33 @@
                 
-ignored
+e
 .printStackTrace
@@ -1718,140 +1718,19 @@
     
-System.out.println(%22%5Bget sample data%5D Debug: %22 + ignored);%0A            %7D%0A            if (sampleData != null) %7B%0A                break
+return null
 ;%0A  
@@ -1942,13 +1942,8 @@
     
-for (
 File
@@ -1955,21 +1955,17 @@
 zer 
-fileA
+a
 nalyzer 
 : al
@@ -1964,13 +1964,18 @@
 zer 
-: all
+= getRight
 File
@@ -1986,94 +1986,47 @@
 yzer
-) %7B%0A            System.out.println(%22try file analyzer: %22 + fileAnalyzer.STORAGE_TYPE);
+(path);%0A        if (analyzer != null) %7B
 %0A   
@@ -2069,13 +2069,9 @@
 a = 
-fileA
+a
 naly
@@ -2176,23 +2176,17 @@
 ception 
-ignored
+e
 ) %7B%0A    
@@ -2197,23 +2197,17 @@
         
-ignored
+e
 .printSt
@@ -2238,131 +2238,19 @@
     
-System.out.println(%22%5Bget schema%5D Debug: %22 + ignored);%0A            %7D%0A            if (schema != null) %7B%0A                break
+return null
 ;%0A  
@@ -2305,18 +2305,16 @@
  %7D%0A%0A    
-/*
 private 
@@ -2385,16 +2385,17 @@
 xception
+ 
 %7B%0A      
@@ -2479,32 +2479,33 @@
 th();%0A        if
+ 
 (rightPath.endsW
@@ -2518,10 +2518,10 @@
 xml%22
- 
 )
+ 
 && !
@@ -2549,16 +2549,17 @@
 %22.xml%22))
+ 
 %7B%0A      
@@ -2612,32 +2612,33 @@
     %7D%0A        if
+ 
 (rightPath.endsW
@@ -2684,16 +2684,17 @@
 .avro%22))
+ 
 %7B%0A      
@@ -2756,16 +2756,17 @@
       if
+ 
 (rightPa
@@ -2818,16 +2818,17 @@
 %22.orc%22))
+ 
 %7B%0A      
@@ -2910,16 +2910,14 @@
 r;%0A    %7D
-*/
 %0A%7D%0A%0A%0A%0A

add xml test