Example usage for java.util Scanner close

List of usage examples for java.util Scanner close

Introduction

In this page you can find the example usage for java.util Scanner close.

Prototype

public void close() 

Source Link

Document

Closes this scanner.

Usage

From source file:com.novartis.opensource.yada.test.ServiceTest.java

/**
 * Loads the resource at {@code path} containing query parameter or json
 * strings/*from w w  w  . ja v a2s .c  o  m*/
 * 
 * @param path the path to the test script
 * @return an array of query or json strings
 * @throws URISyntaxException when a handle can't be attached to the test file
 *         path
 * @throws IOException if the {@link InputStream} used for reading test files
 *         can't be closed
 */
@SuppressWarnings("resource")
public String[] loadResource(String path) throws URISyntaxException, IOException {
    Scanner scanner = null;
    String[] queries = null;
    java.net.URL resource = getClass().getResource(path);
    InputStream in = null;
    try {
        scanner = new Scanner(new File(resource.toURI()), UTF8);
    } catch (Exception e) {
        in = getClass().getResourceAsStream(path);
        if (in != null) {
            scanner = new Scanner(in, UTF8);
        }
    }

    if (scanner != null) {
        scanner.useDelimiter("\\Z");
        queries = scanner.next().split("\\n");
        scanner.close();
    }

    if (in != null)
        in.close();

    return queries;
}

From source file:componentes.DocumentoCxP.java

public void seleccionarArchivoXML(FileUploadEvent event) {

    try {/*from  w ww . ja  v  a 2  s . c  o  m*/

        File tempFile = File.createTempFile(event.getFile().getFileName(), "tmp");
        tempFile.deleteOnExit();
        FileOutputStream out = new FileOutputStream(tempFile);
        IOUtils.copy(event.getFile().getInputstream(), out);
        out.close();

        StringBuilder fileContents = new StringBuilder((int) tempFile.length());
        Scanner scanner = new Scanner(tempFile);
        String lineSeparator = System.getProperty("line.separator");
        try {
            while (scanner.hasNextLine()) {
                fileContents.append(scanner.nextLine()).append(lineSeparator);
            }
        } finally {
            scanner.close();
        }
        //Validaciones
        String codDoc = utilitario.getValorEtiqueta(fileContents.toString(), "codDoc");
        if (codDoc == null || codDoc.equals(TipoComprobanteEnum.FACTURA.getCodigo()) == false) {
            utilitario.agregarMensajeError("Error archivo XML", "Tipo de comprobante no vlido");
            return;
        }
        String ide_geper = ser_proveedor
                .getIdeProveedor(utilitario.getValorEtiqueta(fileContents.toString(), "ruc"));
        if (ide_geper == null) {
            utilitario.agregarMensajeError("Error",
                    "El proveedor " + utilitario.getValorEtiqueta(fileContents.toString(), "razonSocial")
                            + " no existe en la base de datos");
            return;
        }
        String autorizacio_cpcfa = utilitario.getValorEtiqueta(fileContents.toString(), "numeroAutorizacion");
        if (ser_cuentas_cxp.isExisteDocumentoElectronico(autorizacio_cpcfa)) {
            utilitario.agregarMensajeError("Error", "La factura electronica seleccionada ya existe");
            return;
        }

        com_tipo_documento.setValue(parametros.get("p_con_tipo_documento_factura"));
        cargarProveedores();
        String numero_cpcfa = utilitario.getValorEtiqueta(fileContents.toString(), "estab") + "-"
                + utilitario.getValorEtiqueta(fileContents.toString(), "ptoEmi") + "-"
                + utilitario.getValorEtiqueta(fileContents.toString(), "secuencial");
        tab_cab_documento.setValor("ide_geper", ide_geper);
        tab_cab_documento.setValor("autorizacio_cpcfa", autorizacio_cpcfa);
        tab_cab_documento.setValor("numero_cpcfa", numero_cpcfa);
        tab_cab_documento.setValor("fecha_emisi_cpcfa",
                utilitario.getFormatoFecha(utilitario.toDate(
                        utilitario.getFormatoFecha(
                                utilitario.getValorEtiqueta(fileContents.toString(), "fechaEmision")),
                        "dd/MM/yyyy")));
        tab_cab_documento.setValor("ide_cndfp", ser_cuentas_cxp
                .getFormaPago(utilitario.getValorEtiqueta(fileContents.toString(), "formaPago")));
        //Detalles
        String cadenaDetalles = utilitario.getValorEtiqueta(fileContents.toString(), "detalles");
        String strDetalles[] = cadenaDetalles.split("</detalle>");
        tab_det_documento.limpiar();
        for (String strDetalleActual : strDetalles) {
            tab_det_documento.insertar();
            tab_det_documento.setValor("cantidad_cpdfa",
                    utilitario.getValorEtiqueta(strDetalleActual, "cantidad"));
            tab_det_documento.setValor("observacion_cpdfa",
                    utilitario.getValorEtiqueta(strDetalleActual, "descripcion"));
            tab_det_documento.setValor("precio_cpdfa",
                    utilitario.getValorEtiqueta(strDetalleActual, "precioUnitario"));
            tab_det_documento.setValor("valor_cpdfa",
                    utilitario.getValorEtiqueta(strDetalleActual, "precioTotalSinImpuesto"));
            String codigoPorcentaje = utilitario.getValorEtiqueta(strDetalleActual, "codigoPorcentaje");
            if (codigoPorcentaje.equals(TipoImpuestoIvaEnum.IVA_VENTA_0.getCodigo())) { //NO IVA
                tab_det_documento.setValor("iva_inarti_cpdfa", "-1");
            } else if (codigoPorcentaje.equals(TipoImpuestoIvaEnum.IVA_NO_OBJETO.getCodigo())) {
                tab_det_documento.setValor("iva_inarti_cpdfa", "0");
            } else {
                tab_det_documento.setValor("iva_inarti_cpdfa", "1");
            }
        }
        calcularTotalDocumento();
        dia_cxp_xml.cerrar();
        utilitario.addUpdate(
                "tab_documenoCxP:0:tab_cab_documento,tab_documenoCxP:0:tab_det_documento,tab_documenoCxP:0:gri_pto");
    } catch (Exception ex) {
        utilitario.crearError("Error al Leer Factura XML", "en el mtodo seleccionarArchivoXML()", ex);
    }
}

From source file:at.alladin.rmbt.android.util.LogTask.java

@Override
protected Void doInBackground(final String... params) {
    try {//w ww.  j  a v  a 2 s. c  o m
        serverConn = new ControlServerConnection(activity);

        final List<File> logFiles = new ArrayList<File>();

        if (params == null || params.length == 0) {
            File f = new File(Environment.getExternalStorageDirectory(), "qosdebug");
            final File[] logs = f.listFiles();

            if (logs != null) {
                for (File l : logs) {
                    if (l.length() > 0) {
                        logFiles.add(l);
                    } else {
                        //delete old empty log file
                        l.delete();
                    }
                }
            }
        } else {
            for (String fileName : params) {
                File f = new File(fileName);
                if (f.exists() && f.length() > 0) {
                    logFiles.add(f);
                }
            }
        }

        System.out.println("log files found: " + logFiles);

        if (logFiles.size() > 0) {
            for (File logFile : logFiles) {
                System.out.println("Sending file: " + logFile.getAbsolutePath());
                Scanner s = null;
                try {
                    BufferedReader br = new BufferedReader(new FileReader(logFile));
                    try {
                        StringBuilder sb = new StringBuilder();
                        String line = br.readLine();

                        while (line != null) {
                            sb.append(line);
                            sb.append("\n");
                            line = br.readLine();
                        }
                        final JSONObject requestData = new JSONObject();
                        requestData.put("content", sb.toString());
                        requestData.put("logfile", LOGFILE_PREFIX + "_" + ConfigHelper.getUUID(activity) + "_"
                                + logFile.getName());
                        final JSONObject fileTimes = new JSONObject();
                        fileTimes.put("last_access",
                                TimeUnit.SECONDS.convert(logFile.lastModified(), TimeUnit.MILLISECONDS));
                        fileTimes.put("last_modified",
                                TimeUnit.SECONDS.convert(logFile.lastModified(), TimeUnit.MILLISECONDS));
                        fileTimes.put("created",
                                TimeUnit.SECONDS.convert(logFile.lastModified(), TimeUnit.MILLISECONDS));
                        requestData.put("file_times", fileTimes);
                        JSONArray result = serverConn.sendLogReport(requestData);
                        if (result != null) {
                            final String resultStatus = ((JSONObject) result.get(0)).getString("status");
                            if ("OK".equals(resultStatus.toUpperCase(Locale.US))) {
                                System.out.println("Log file sent successfully. Deleting.");
                                br.close();
                                logFile.delete();
                            }
                        }
                    } finally {
                        br.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (s != null) {
                        s.close();
                    }
                }
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    return null;
}

From source file:com.aurel.track.dbase.InitDatabase.java

private static void insertNullObjectsAndSampleData() {
    ResultSet rs = null;//  w  w  w  .ja  va2 s  .c o m
    Connection coni = null;
    Connection cono = null;
    try {
        coni = getConnection();
        cono = getConnection();
        Statement istmt = coni.createStatement();
        Statement ostmt = cono.createStatement();
        LOGGER.info("Testing for NULL objects...");
        // --------------- T S I T E ----------------------
        rs = istmt.executeQuery("SELECT * FROM TSITE");
        if (rs == null || !rs.next()) {
            try {
                ostmt.execute("INSERT INTO TSITE " + "(OBJECTID) " + "VALUES (1)");
                LOGGER.info("Inserted TSITE");
            } catch (Exception exc) {
                LOGGER.error("Problem inserting TSITE object: " + exc.getMessage());
                LOGGER.debug(STACKTRACE, exc);
            }
        }

        // --------------- T P R O J E C T T Y P E ----------------------
        rs = istmt.executeQuery("SELECT * FROM TPROJECTTYPE WHERE OBJECTID = 0");
        if (rs == null || !rs.next()) {
            try {
                ostmt.execute(
                        "INSERT INTO TPROJECTTYPE " + "(OBJECTID, LABEL, NOTIFYOWNERLEVEL, NOTIFYMANAGERLEVEL) "
                                + "VALUES (0, 'Generic Space', 0, 1)");
                LOGGER.info("Inserted NULL project (PKEY = 0) into TPROJECTTYPE");
            } catch (Exception exc) {
                LOGGER.error("Problem inserting NULL object for TPROJECTTYPE: " + exc.getMessage());
                LOGGER.debug(STACKTRACE, exc);
            }
        }

        rs = istmt.executeQuery("SELECT * FROM TPROJECTTYPE WHERE OBJECTID = -1");
        if (rs == null || !rs.next()) {
            try {
                ostmt.execute("INSERT INTO TPROJECTTYPE " + "(OBJECTID, LABEL, DEFAULTFORPRIVATE) "
                        + "VALUES (-1, 'Private Project', 'Y')");
                LOGGER.info("Inserted Private project (PKEY = -1) into TPROJECTTYPE");
            } catch (Exception exc) {
                LOGGER.error("Problem inserting private space in TPROJECTTYPE: " + exc.getMessage());
                LOGGER.debug(STACKTRACE, exc);
            }
        }

        rs = istmt.executeQuery("SELECT * FROM TPROJECT WHERE PKEY = 0");
        // ------------------- T P R O J E C T  -----------------------
        if (rs == null || !rs.next()) {
            try {
                ostmt.execute("INSERT INTO TPROJECT " + "(PKEY, LABEL, DEFOWNER, DEFMANAGER, PROJECTTYPE) "
                        + "VALUES (0, 'Generic Space', 1, 1, 0)");
                LOGGER.info("Inserted NULL project (PKEY = 0) into TPROJECT");
            } catch (Exception exc) {
                LOGGER.error("Problem inserting NULL object for TPROJECT: " + exc.getMessage());
            }
        }

        // ----------------------- T R O L E ------------------------------
        rs = istmt.executeQuery("SELECT * FROM TROLE WHERE PKEY = -1");
        if (rs == null || !rs.next()) {
            try {
                ostmt.execute("INSERT INTO TROLE " + "(PKEY, LABEL, ACCESSKEY, EXTENDEDACCESSKEY, PROJECTTYPE) "
                        + "VALUES (-1, 'PrivateRole', 0, '111111111111', 0)");
                LOGGER.info("Inserted private role (PKEY = -1) into TROLE");
            } catch (Exception exc) {
                LOGGER.error("Problem inserting NULL object for TROLE: " + exc.getMessage());
                LOGGER.debug(STACKTRACE, exc);
            }
        }

        LOGGER.info("NULL objects are okay.");

        if (isFirstStartEver) {
            LOGGER.info("Filling some sample data...");
            try {

                URL populateURL = ApplicationBean.getInstance().getServletContext().getResource(populateSql);
                if (populateURL == null) {
                    ClassLoader cl = InitDatabase.class.getClassLoader();
                    populateURL = cl.getResource(populateSql);
                }
                InputStream in = populateURL.openStream();
                java.util.Scanner s = new java.util.Scanner(in, "UTF-8");
                s.useDelimiter(";");
                String st = null;
                StringBuffer stb = new StringBuffer();
                int line = 0;

                ApplicationStarter.getInstance().actualizePercentComplete(
                        ApplicationStarter.getInstance().INIT_DB_DATA_STEP,
                        ApplicationStarter.INIT_DB_DATA_TEXT);

                while (s.hasNext()) {
                    stb.append(s.nextLine().trim());
                    st = stb.toString();
                    ++line;
                    if (!st.isEmpty() && !st.startsWith("--") && !st.startsWith("/*")) {
                        if (st.endsWith(";")) {
                            stb = new StringBuffer(); // clear buffer
                            st = st.substring(0, st.length() - 1); // remove the semicolon
                            try {
                                ostmt.executeUpdate(st);
                            } catch (Exception exc) {
                                LOGGER.error("Problem inserting sample data: " + exc.getMessage());
                                LOGGER.error("Line " + line + ": " + st);
                            }
                        } else {
                            stb.append(" ");
                        }
                    } else {
                        stb = new StringBuffer();
                    }
                }
                s.close();
                in.close();

            } catch (Exception e) {
                LOGGER.error(ExceptionUtils.getStackTrace(e));
            }
            LOGGER.info("Sample data is okay.");

            ApplicationStarter.getInstance().actualizePercentComplete(
                    ApplicationStarter.getInstance().INIT_DB_DATA_STEP, ApplicationStarter.INIT_DB_DATA_TEXT);
        }
    } catch (Exception e) {
        LOGGER.error("Problem inserting null objects: " + e.getMessage());
        LOGGER.debug(STACKTRACE, e);
    } finally {
        if (coni != null) {
            try {
                coni.close();
            } catch (SQLException e) {
                LOGGER.info("Closing connection failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
        if (cono != null) {
            try {
                cono.close();
            } catch (SQLException e) {
                LOGGER.info("Closing connection failed with " + e.getMessage());
                LOGGER.debug(ExceptionUtils.getStackTrace(e));
            }
        }
    }
}

From source file:com.inmobi.conduit.AbstractService.java

protected Table<String, Long, Long> parseCountersFile(FileSystem fs) {
    List<Path> partFiles = listPartFiles(tmpCounterOutputPath, fs);
    if (partFiles == null || partFiles.size() == 0) {
        LOG.warn("No counters files generated by mapred job");
        return null;
    }//from w w  w.j  av  a  2 s  . co m
    Table<String, Long, Long> result = HashBasedTable.create();
    for (Path filePath : partFiles) {
        FSDataInputStream fin = null;
        Scanner scanner = null;
        try {
            fin = fs.open(filePath);
            scanner = new Scanner(fin);

            while (scanner.hasNext()) {
                String counterNameValue = null;
                try {
                    counterNameValue = scanner.next();
                    String tmp[] = counterNameValue.split(ConduitConstants.AUDIT_COUNTER_NAME_DELIMITER);
                    if (tmp.length < 4) {
                        LOG.error("Malformed counter name,skipping " + counterNameValue);
                        continue;
                    }
                    String streamFileNameCombo = tmp[0] + ConduitConstants.AUDIT_COUNTER_NAME_DELIMITER
                            + tmp[1];
                    Long publishTimeWindow = Long.parseLong(tmp[2]);
                    Long numOfMsgs = Long.parseLong(tmp[3]);
                    result.put(streamFileNameCombo, publishTimeWindow, numOfMsgs);
                } catch (Exception e) {
                    LOG.error("Counters file has malformed line with counter name = " + counterNameValue
                            + " ..skipping the line", e);
                }
            }
        } catch (IOException e1) {
            LOG.error("Error while opening file " + filePath + " Skipping");
            continue;
        } finally {
            try {
                if (fin != null) {
                    fin.close();
                }
                if (scanner != null) {
                    scanner.close();
                }
            } catch (Exception e) {
                LOG.warn("Error while closing file " + filePath + " or scanner");
            }
        }
    }
    return result;

}

From source file:ml.shifu.shifu.core.ConfusionMatrix.java

public void computeConfusionMatrix() throws IOException {
    SourceType sourceType = evalConfig.getDataSet().getSource();
    List<Scanner> scanners = ShifuFileUtils.getDataScanners(pathFinder.getEvalScorePath(evalConfig, sourceType),
            sourceType);/*  w w w. jav a  2s  .co m*/

    List<ModelResultObject> moList = new ArrayList<ModelResultObject>();

    boolean isDir = ShifuFileUtils.isDir(pathFinder.getEvalScorePath(evalConfig, sourceType), sourceType);

    LOG.info("The size of scanner is {}", scanners.size());

    int cnt = 0;
    for (Scanner scanner : scanners) {
        while (scanner.hasNext()) {
            if ((++cnt) % 10000 == 0) {
                LOG.info("Loaded " + cnt + " records.");
            }

            String[] raw = scanner.nextLine().split("\\|");
            if ((!isDir) && cnt == 1) {
                // if the evaluation score file is the local file, skip the
                // first line since we add
                continue;
            }

            String tag = CommonUtils.trimTag(raw[targetColumnIndex]);
            if (StringUtils.isBlank(tag)) {
                if (Math.random() < 0.01) {
                    LOG.warn("Empty target value!!");
                }

                continue;
            }

            double weight = 1.0d;
            if (this.weightColumnIndex > 0) {
                try {
                    weight = Double.parseDouble(raw[1]);
                } catch (NumberFormatException e) {
                    // Do nothing
                }
            }

            double score = 0;
            try {
                score = Double.parseDouble(raw[scoreColumnIndex]);
            } catch (NumberFormatException e) {
                // user set the score column wrong ?
                if (Math.random() < 0.05) {
                    LOG.warn("The score column - {} is not integer. Is score column set correctly?",
                            raw[scoreColumnIndex]);
                }
                continue;
            }

            moList.add(new ModelResultObject(score, tag, weight));
        }
        // release resource
        scanner.close();
    }
    LOG.info("Totally loaded " + cnt + " records.");

    if (cnt == 0 || moList.size() == 0) {
        LOG.error("No score read, the EvalScore did not genernate or is null file");
        throw new ShifuException(ShifuErrorCode.ERROR_EVALSCORE);
    }

    ConfusionMatrixCalculator calculator = new ConfusionMatrixCalculator(modelConfig.getPosTags(evalConfig),
            modelConfig.getNegTags(evalConfig), moList);

    BufferedWriter confMatWriter = ShifuFileUtils.getWriter(
            pathFinder.getEvalMatrixPath(evalConfig, evalConfig.getDataSet().getSource()),
            evalConfig.getDataSet().getSource());
    calculator.calculate(confMatWriter);

    confMatWriter.close();
}

From source file:com.maxl.java.aips2sqlite.RealExpertInfo.java

/**
 * Extracts package info from Swissmedic package Excel file
 *//* ww w  . j  av  a 2s .co  m*/
private void extractPackageInfo() {
    try {
        long startTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.print("- Processing packages xlsx... ");
        // Load Swissmedic xls file         
        FileInputStream packages_file = new FileInputStream(Constants.FILE_PACKAGES_XLSX);
        // Get workbook instance for XLSX file (XSSF = Horrible SpreadSheet Format)
        XSSFWorkbook packages_workbook = new XSSFWorkbook(packages_file);
        // Get first sheet from workbook
        XSSFSheet packages_sheet = packages_workbook.getSheetAt(0);

        /*
        if (SHOW_LOGS)
           System.out.print("- Processing packages xls... ");
        // Load Swissmedic xls file         
        FileInputStream packages_file = new FileInputStream(FILE_PACKAGES_XLS);
        // Get workbook instance for XLS file (HSSF = Horrible SpreadSheet Format)
        HSSFWorkbook packages_workbook = new HSSFWorkbook(packages_file);
        // Get first sheet from workbook
        HSSFSheet packages_sheet = packages_workbook.getSheetAt(0);
        */
        // Iterate through all rows of first sheet
        Iterator<Row> rowIterator = packages_sheet.iterator();

        int num_rows = 0;
        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            if (num_rows > 5) {
                String swissmedic_no5 = ""; // SwissmedicNo5 registration number (5 digits)
                String sequence_name = "";
                String package_id = "";
                String swissmedic_no8 = ""; // SwissmedicNo8 = SwissmedicNo5 + Package id (8 digits)
                String heilmittel_code = "";
                String package_size = "";
                String package_unit = "";
                String swissmedic_cat = "";
                String application_area = "";
                String public_price = "";
                String exfactory_price = "";
                String therapeutic_index = "";
                String withdrawn_str = "";
                String speciality_str = "";
                String plimitation_str = "";
                String add_info_str = ""; // Contains additional information separated by ;
                String ean_code_str = "";
                String pharma_code_str = "";

                // 0: Zulassungsnummer, 1: Dosisstrkenummer, 2: Prparatebezeichnung, 3: Zulassunginhaberin, 4: Heilmittelcode, 5: IT-Nummer, 6: ATC-Code
                // 7: Erstzulassung Prparat, 8: Zulassungsdatum Sequenz, 9: Gltigkeitsdatum, 10: Packungscode, 11: Packungsgrsse
                // 12: Einheit, 13: Abgabekategorie Packung, 14: Abgabekategorie Dosisstrke, 15: Abgabekategorie Prparat, 
                // 16: Wirkstoff, 17: Zusammensetzung, 18: Anwendungsgebiet Prparat, 19: Anwendungsgebiet Dosisstrke, 20: Gentechnisch hergestellte Wirkstoffe
                // 21: Kategorie bei Insulinen, 22: Betubungsmittelhaltigen Prparaten

                // @cybermax: 15.10.2013 - work around for Excel cells of type "Special" (cell0 and cell10)
                if (row.getCell(0) != null)
                    swissmedic_no5 = String.format("%05d", (int) (row.getCell(0).getNumericCellValue())); // Swissmedic registration number (5 digits)
                if (row.getCell(2) != null)
                    sequence_name = ExcelOps.getCellValue(row.getCell(2)); // Sequence name
                if (row.getCell(4) != null)
                    heilmittel_code = ExcelOps.getCellValue(row.getCell(4)); // Heilmittelcode               
                if (row.getCell(11) != null)
                    package_size = ExcelOps.getCellValue(row.getCell(11)); // Packungsgrsse
                if (row.getCell(12) != null)
                    package_unit = ExcelOps.getCellValue(row.getCell(12)); // Einheit
                if (row.getCell(13) != null)
                    swissmedic_cat = ExcelOps.getCellValue(row.getCell(13)); // Abgabekategorie Packung   
                if (row.getCell(18) != null)
                    application_area = ExcelOps.getCellValue(row.getCell(18)); // Anwendungsgebiet Prparat            
                if (row.getCell(10) != null) {
                    package_id = String.format("%03d", (int) (row.getCell(10).getNumericCellValue())); // Verpackungs ID
                    swissmedic_no8 = swissmedic_no5 + package_id;
                    // Fill in row
                    ArrayList<String> pack = new ArrayList<String>();
                    pack.add(swissmedic_no5); // 0
                    pack.add(sequence_name); // 1
                    pack.add(heilmittel_code); // 2
                    pack.add(package_size); // 3
                    pack.add(package_unit); // 4
                    pack.add(swissmedic_cat); // 5
                    if (!application_area.isEmpty())
                        pack.add(application_area + " (Swissmedic);"); // 6 = swissmedic + bag
                    else
                        pack.add("");
                    pack.add(public_price); // 7
                    pack.add(exfactory_price); // 8
                    pack.add(therapeutic_index);// 9
                    // By default the meds are "ausser Handel"
                    if (CmlOptions.DB_LANGUAGE.equals("de"))
                        withdrawn_str = "a.H."; // ausser Handel
                    else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                        withdrawn_str = "p.c."; // 
                    pack.add(withdrawn_str); // 10
                    pack.add(speciality_str); // 11
                    pack.add(plimitation_str); // 12
                    pack.add(add_info_str); // 13
                    // 22.03.2014: EAN-13 barcodes - initialization - check digit is missing!
                    ean_code_str = "7680" + swissmedic_no8;
                    pack.add(ean_code_str); // 14
                    pack.add(pharma_code_str); // 15

                    m_package_info.put(swissmedic_no8, pack);
                }
            }
            num_rows++;
        }
        long stopTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS) {
            System.out.println(
                    (m_package_info.size() + 1) + " packages in " + (stopTime - startTime) / 1000.0f + " sec");
        }
        startTime = System.currentTimeMillis();

        if (CmlOptions.SHOW_LOGS)
            System.out.print("- Processing atc classes xls... ");
        if (CmlOptions.DB_LANGUAGE.equals("de")) {
            /*
            // Load ATC classes xls file
            FileInputStream atc_classes_file = new FileInputStream(Constants.FILE_ATC_CLASSES_XLS);
            // Get workbook instance for XLS file (HSSF = Horrible SpreadSheet Format)
            HSSFWorkbook atc_classes_workbook = new HSSFWorkbook(atc_classes_file);
            // Get first sheet from workbook
            // HSSFSheet atc_classes_sheet = atc_classes_workbook.getSheetAt(1);   // --> 2013 file
            HSSFSheet atc_classes_sheet = atc_classes_workbook.getSheetAt(0);      // --> 2014 file         
            // Iterate through all rows of first sheet
            rowIterator = atc_classes_sheet.iterator();
                    
            num_rows = 0;
            while (rowIterator.hasNext()) {
               Row row = rowIterator.next();
               if (num_rows>2) {
                  String atc_code = "";
                  String atc_class = "";
                  if (row.getCell(0)!=null) {
             atc_code = row.getCell(0).getStringCellValue().replaceAll("\\s", "");
                  }
                  if (row.getCell(2)!=null) {
             atc_class = row.getCell(2).getStringCellValue();
                  }
                  // Build a full map atc code to atc class
                  if (atc_code.length()>0) {
             m_atc_map.put(atc_code, atc_class);
                  }
               }
               num_rows++;
            }
            */
            CSVReader reader = new CSVReader(
                    new InputStreamReader(new FileInputStream(Constants.FILE_EPHA_ATC_CODES_CSV), "UTF-8"));
            List<String[]> myEntries = reader.readAll();
            num_rows = myEntries.size();
            for (String[] s : myEntries) {
                if (s.length > 2) {
                    String atc_code = s[0];
                    String atc_class = s[1];
                    m_atc_map.put(atc_code, atc_class);
                }
            }
            reader.close();
        } else if (CmlOptions.DB_LANGUAGE.equals("fr")) {
            // Load ATC classes xls file
            FileInputStream atc_classes_file = new FileInputStream(Constants.FILE_WHO_ATC_CLASSES_XLS);
            // Get workbook instance for XLS file (HSSF = Horrible SpreadSheet Format)
            HSSFWorkbook atc_classes_workbook = new HSSFWorkbook(atc_classes_file);
            // Get first sheet from workbook
            HSSFSheet atc_classes_sheet = atc_classes_workbook.getSheetAt(0); // --> 2014 file         
            // Iterate through all rows of first sheet
            rowIterator = atc_classes_sheet.iterator();

            num_rows = 0;
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                if (num_rows > 0) {
                    String atc_code = "";
                    String atc_class = "";
                    if (row.getCell(1) != null) {
                        atc_code = row.getCell(1).getStringCellValue();
                        if (atc_code.length() > 0) {
                            // Extract L5 and below
                            if (atc_code.length() < 6 && row.getCell(2) != null) {
                                atc_class = row.getCell(2).getStringCellValue();
                                // Build a full map atc code to atc class
                                m_atc_map.put(atc_code, atc_class);
                                // Extract L7
                            } else if (atc_code.length() == 7 && row.getCell(4) != null) {
                                atc_class = row.getCell(4).getStringCellValue();
                                m_atc_map.put(atc_code, atc_class);
                            }
                        }
                    }
                }
                num_rows++;
            }

            // Load multilingual ATC classes txt file, replace English with French
            String atc_classes_multi = FileOps.readFromFile(Constants.FILE_ATC_MULTI_LINGUAL_TXT);
            // Loop through all lines
            Scanner scanner = new Scanner(atc_classes_multi);
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                List<String> atc_class = Arrays.asList(line.split(": "));
                String atc_code = atc_class.get(0);
                String[] atc_classes_str = atc_class.get(1).split(";");
                String atc_class_french = atc_classes_str[1].trim();
                // Replaces atc code...
                m_atc_map.put(atc_code, atc_class_french);
            }
            scanner.close();
        }
        stopTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.println(
                    (m_atc_map.size() + 1) + " classes in " + (stopTime - startTime) / 1000.0f + " sec");

        // Load Refdata xml file
        File refdata_xml_file = new File(Constants.FILE_REFDATA_PHARMA_XML);
        FileInputStream refdata_fis = new FileInputStream(refdata_xml_file);

        startTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.println("- Unmarshalling Refdatabase for " + CmlOptions.DB_LANGUAGE + "... ");

        JAXBContext context = JAXBContext.newInstance(Refdata.class);
        Unmarshaller um = context.createUnmarshaller();
        Refdata refdataPharma = (Refdata) um.unmarshal(refdata_fis);
        List<Refdata.ITEM> pharma_list = refdataPharma.getItem();

        String smno8;
        for (Refdata.ITEM pharma : pharma_list) {
            String ean_code = pharma.getGtin();
            String pharma_code = pharma.getPhar();
            if (ean_code.length() == 13) {
                smno8 = ean_code.substring(4, 12);
                // Extract pharma corresponding to swissmedicno8 (source: swissmedic package file)
                ArrayList<String> pi_row = m_package_info.get(smno8);
                // Replace sequence_name
                if (pi_row != null) {
                    // Prparatname + galenische Form
                    if (CmlOptions.DB_LANGUAGE.equals("de"))
                        pi_row.set(1, pharma.getNameDE());
                    else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                        pi_row.set(1, pharma.getNameFR());
                    // If med is in refdata file, then it is "in Handel!!" ;)
                    pi_row.set(10, ""); // By default this is set to a.H. or p.C.
                    // 22.03.2014: EAN-13 barcodes - replace with refdata if package exists
                    pi_row.set(14, ean_code);
                    // Pharma code
                    pi_row.set(15, pharma_code);
                } else {
                    if (CmlOptions.SHOW_ERRORS) {
                        if (pharma.getATYPE().equals("PHARMA"))
                            System.err.println(
                                    ">> Does not exist in BAG xls: " + smno8 + " (" + pharma.getNameDE() + ")");
                    }
                }
            } else if (ean_code.length() < 13) {
                if (CmlOptions.SHOW_ERRORS)
                    System.err.println(">> EAN code too short: " + ean_code + ": " + pharma.getNameDE());
            } else if (ean_code.length() > 13) {
                if (CmlOptions.SHOW_ERRORS)
                    System.err.println(">> EAN code too long: " + ean_code + ": " + pharma.getNameDE());
            }
        }

        stopTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.println(pharma_list.size() + " medis in " + (stopTime - startTime) / 1000.0f + " sec");

        // Load BAG xml file
        File bag_xml_file = new File(Constants.FILE_PREPARATIONS_XML);
        FileInputStream fis_bag = new FileInputStream(bag_xml_file);

        startTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.println("- Processing preparations xml... ");

        context = JAXBContext.newInstance(Preparations.class);
        um = context.createUnmarshaller();
        Preparations prepInfos = (Preparations) um.unmarshal(fis_bag);
        List<Preparations.Preparation> prep_list = prepInfos.getPreparations();

        int num_preparations = 0;
        for (Preparations.Preparation prep : prep_list) {
            String swissmedicno5_str = prep.getSwissmedicNo5();
            if (swissmedicno5_str != null) {
                String orggencode_str = ""; // "O", "G" or empty -> ""
                String flagSB20_str = ""; // "Y" -> 20% or "N" -> 10%
                if (prep.getOrgGenCode() != null)
                    orggencode_str = prep.getOrgGenCode();
                if (prep.getFlagSB20() != null) {
                    flagSB20_str = prep.getFlagSB20();
                    if (flagSB20_str.equals("Y")) {
                        if (CmlOptions.DB_LANGUAGE.equals("de"))
                            flagSB20_str = "SB 20%";
                        else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                            flagSB20_str = "QP 20%";
                    } else if (flagSB20_str.equals("N")) {
                        if (CmlOptions.DB_LANGUAGE.equals("de"))
                            flagSB20_str = "SB 10%";
                        else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                            flagSB20_str = "QP 10%";
                    } else
                        flagSB20_str = "";
                }
                m_add_info_map.put(swissmedicno5_str, orggencode_str + ";" + flagSB20_str);
            }

            List<Preparation.Packs> packs_list = prep.getPacks();
            for (Preparation.Packs packs : packs_list) {
                // Extract codes for therapeutic index / classification
                String bag_application = "";
                String therapeutic_code = "";
                List<Preparations.Preparation.ItCodes> itcode_list = prep.getItCodes();
                for (Preparations.Preparation.ItCodes itc : itcode_list) {
                    List<Preparations.Preparation.ItCodes.ItCode> code_list = itc.getItCode();
                    int index = 0;
                    for (Preparations.Preparation.ItCodes.ItCode code : code_list) {
                        if (index == 0) {
                            if (CmlOptions.DB_LANGUAGE.equals("de"))
                                therapeutic_code = code.getDescriptionDe();
                            else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                                therapeutic_code = code.getDescriptionFr();
                        } else {
                            if (CmlOptions.DB_LANGUAGE.equals("de"))
                                bag_application = code.getDescriptionDe();
                            else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                                bag_application = code.getDescriptionFr();
                        }
                        index++;
                    }
                }
                // Generate new package info
                List<Preparation.Packs.Pack> pack_list = packs.getPack();
                for (Preparation.Packs.Pack pack : pack_list) {
                    // Get SwissmedicNo8 and used it as a key to extract all the relevant package info
                    String swissMedicNo8 = pack.getSwissmedicNo8();
                    ArrayList<String> pi_row = null;
                    if (swissMedicNo8 != null)
                        pi_row = m_package_info.get(swissMedicNo8);
                    // Preparation also in BAG xml file (we have a price)
                    if (pi_row != null) {
                        // Update Swissmedic catory if necessary ("N->A", Y->"A+")
                        if (pack.getFlagNarcosis().equals("Y"))
                            pi_row.set(5, pi_row.get(5) + "+");
                        // Extract point limitations
                        List<Preparations.Preparation.Packs.Pack.PointLimitations> point_limits = pack
                                .getPointLimitations();
                        for (Preparations.Preparation.Packs.Pack.PointLimitations limits : point_limits) {
                            List<Preparations.Preparation.Packs.Pack.PointLimitations.PointLimitation> plimits_list = limits
                                    .getPointLimitation();
                            if (plimits_list.size() > 0)
                                if (plimits_list.get(0) != null)
                                    pi_row.set(12, ", LIM" + plimits_list.get(0).getPoints() + "");
                        }
                        // Extract exfactory and public prices
                        List<Preparations.Preparation.Packs.Pack.Prices> price_list = pack.getPrices();
                        for (Preparations.Preparation.Packs.Pack.Prices price : price_list) {
                            List<Preparations.Preparation.Packs.Pack.Prices.PublicPrice> public_price = price
                                    .getPublicPrice();
                            List<Preparations.Preparation.Packs.Pack.Prices.ExFactoryPrice> exfactory_price = price
                                    .getExFactoryPrice();
                            if (exfactory_price.size() > 0) {
                                try {
                                    float f = Float.valueOf(exfactory_price.get(0).getPrice());
                                    String ep = String.format("%.2f", f);
                                    pi_row.set(8, "CHF " + ep);
                                } catch (NumberFormatException e) {
                                    if (CmlOptions.SHOW_ERRORS)
                                        System.err.println("Number format exception (exfactory price): "
                                                + swissMedicNo8 + " (" + public_price.size() + ")");
                                }
                            }
                            if (public_price.size() > 0) {
                                try {
                                    float f = Float.valueOf(public_price.get(0).getPrice());
                                    String pp = String.format("%.2f", f);
                                    pi_row.set(7, "CHF " + pp);
                                    if (CmlOptions.DB_LANGUAGE.equals("de"))
                                        pi_row.set(11, ", SL");
                                    else if (CmlOptions.DB_LANGUAGE.equals("fr"))
                                        pi_row.set(11, ", LS");
                                } catch (NullPointerException e) {
                                    if (CmlOptions.SHOW_ERRORS)
                                        System.err.println("Null pointer exception (public price): "
                                                + swissMedicNo8 + " (" + public_price.size() + ")");
                                } catch (NumberFormatException e) {
                                    if (CmlOptions.SHOW_ERRORS)
                                        System.err.println("Number format exception (public price): "
                                                + swissMedicNo8 + " (" + public_price.size() + ")");
                                }
                            }
                            // Add application area and therapeutic code
                            if (!bag_application.isEmpty())
                                pi_row.set(6, pi_row.get(6) + bag_application + " (BAG)");
                            pi_row.set(9, therapeutic_code);
                        }
                    }
                }
            }
            num_preparations++;
        }

        stopTime = System.currentTimeMillis();
        if (CmlOptions.SHOW_LOGS)
            System.out.println(
                    num_preparations + " preparations in " + (stopTime - startTime) / 1000.0f + " sec");

        // Loop through all SwissmedicNo8 numbers
        /*
        for (Map.Entry<String, ArrayList<String>> entry : package_info.entrySet()) {
           String swissmedicno8 = entry.getKey();
           ArrayList<String> pi_row = entry.getValue();
        }
        */

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}