package es.tid.ps.mobility.parsing;

import org.apache.log4j.Logger;

import java.util.HashMap;

/**
 * User: masp20
 * Date: 25-ene-2012
 * Time: 12:36:11
 */

public class ParserFactory {

    private static final Logger LOG = Logger.getLogger(ParserFactory.class);

    private static final String PARSER_DEFAULT_CLASS_NAME = "es.tid.analytics.mobility.core.parsers.ParserDefault";
    private static final String PARSER_ALPHA_CLASS_NAME = "es.tid.analytics.mobility.core.parsers.ParserAlphanumeric";

    public static final String PARSER_DEFAULT = "DEFAULT";
    public static final String PARSER_ALPHA = "ALPHA";

    public static final int GEO_LOCATION_LEVEL = 1;

    public static final HashMap<String, String> PARSER_CONTAINER = new HashMap<String, String>();


    public ParserFactory() {
        ParserFactory.loadParsers();
    }

    private static void loadParsers() {
        ParserFactory.PARSER_CONTAINER.put(ParserFactory.PARSER_DEFAULT, ParserFactory.PARSER_DEFAULT_CLASS_NAME);
        ParserFactory.PARSER_CONTAINER.put(ParserFactory.PARSER_ALPHA, ParserFactory.PARSER_ALPHA_CLASS_NAME);
    }

    public ParserCell createNewDefaultCellParser() {
        ParserCell parser = null;
        try {
            parser = (ParserCell) Class.forName(ParserFactory.PARSER_DEFAULT_CLASS_NAME).newInstance();
        } catch (InstantiationException ie) {
            LOG.error("ERROR al instanciar el parser", ie);
        } catch (IllegalAccessException iae) {
            LOG.error("ERROR al instanciar el parser", iae);
        } catch (ClassNotFoundException cnfe) {
            LOG.error("ERROR al instanciar el parser", cnfe);
        }

        return parser;
    }

    public ParserCdr createNewDefaultCdrParser() {
        ParserCdr parser = null;
        try {
            parser = (ParserCdr) Class.forName(ParserFactory.PARSER_DEFAULT_CLASS_NAME).newInstance();
        } catch (InstantiationException ie) {
            LOG.error("ERROR al instanciar el parser", ie);
        } catch (IllegalAccessException iae) {
            LOG.error("ERROR al instanciar el parser", iae);
        } catch (ClassNotFoundException cnfe) {
            LOG.error("ERROR al instanciar el parser", cnfe);
        }

        return parser;
    }

    public ParserCell createNewCellParser(final String parserName) {
        ParserCell parser = null;

        try {
            final String parserClassName = PARSER_CONTAINER.get(parserName);
            parser = (ParserCell) Class.forName(parserClassName).newInstance();
        } catch (InstantiationException ie) {
            LOG.error("ERROR al instanciar el parser", ie);
        } catch (IllegalAccessException iae) {
            LOG.error("ERROR al instanciar el parser", iae);
        } catch (ClassNotFoundException cnfe) {
            LOG.error("ERROR al instanciar el parser", cnfe);
        }

        return parser;
    }

    public ParserCdr createNewCdrParser(final String parserName) {
        ParserCdr parser = null;

        try {
            final String parserClassName = PARSER_CONTAINER.get(parserName);
            parser = (ParserCdr) Class.forName(parserClassName).newInstance();
        } catch (InstantiationException ie) {
            LOG.error("ERROR al instanciar el parser", ie);
        } catch (IllegalAccessException iae) {
            LOG.error("ERROR al instanciar el parser", iae);
        } catch (ClassNotFoundException cnfe) {
            LOG.error("ERROR al instanciar el parser", cnfe);
        }

        return parser;
    }

}

@@ -109,19 +109,16 @@
 : masp20
-%0A *
  Date: 2
@@ -127,19 +127,16 @@
 ene-2012
-%0A *
  Time: 1
@@ -147,17 +147,16 @@
 :11%0A */%0A
-%0A
 public c
@@ -176,17 +176,16 @@
 ctory %7B%0A
-%0A
     priv
@@ -253,17 +253,16 @@
 class);%0A
-%0A
     priv
@@ -312,33 +312,37 @@
 S_NAME =
- %22es.tid.analytic
+%0A            es.tid.p
 s.mobili
@@ -340,36 +340,31 @@
 ps.mobility.
-core.
 pars
-ers
+ing
 .ParserDefau
@@ -365,17 +365,32 @@
 rDefault
-%22
+.class.getName()
 ;%0A    pr
@@ -444,25 +444,29 @@
 ME =
- %22es.tid.analytic
+%0A            es.tid.p
 s.mo
@@ -476,20 +476,15 @@
 ity.
-core.
 pars
-ers
+ing
 .Par
@@ -498,19 +498,33 @@
 anumeric
-%22;%0A
+.class.getName();
 %0A    pub
@@ -630,17 +630,16 @@
 ALPHA%22;%0A
-%0A
     publ
@@ -682,17 +682,16 @@
 EL = 1;%0A
-%0A
     publ
@@ -748,16 +748,28 @@
 TAINER =
+%0A           
  new Has
@@ -793,17 +793,16 @@
 ng%3E();%0A%0A
-%0A
     publ
@@ -978,16 +978,59 @@
 DEFAULT,
+%0A                                          
  ParserF
@@ -1134,16 +1134,59 @@
 R_ALPHA,
+%0A                                          
  ParserF
@@ -1366,32 +1366,53 @@
 ) Class.forName(
+%0A                    
 ParserFactory.PA
@@ -1961,16 +1961,37 @@
 forName(
+%0A                    
 ParserFa
@@ -3678,11 +3678,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Fix unit tests.