Example usage for java.io LineNumberReader close

List of usage examples for java.io LineNumberReader close

Introduction

In this page you can find the example usage for java.io LineNumberReader close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:org.diffkit.db.DKDBInsertTableLoader.java

/**
 * @return true if the load succeeded//from  w  w w  .j a va2 s  .c  o  m
 * @throws IOException
 */
public boolean load(DKDBTable table_, File csvFile_) throws IOException, SQLException {
    _log.debug("table_->{}", table_);
    _log.debug("csvFile_->{}", csvFile_);
    DKValidate.notNull(table_, csvFile_);
    if (!csvFile_.canRead())
        throw new IOException(String.format("can't read csvFile_->%s", csvFile_));
    if (!_database.tableExists(table_))
        throw new IOException(String.format("table_->%s does not exist in database->", table_, _database));
    Connection connection = _database.getConnection();
    _log.debug("connection->{}", connection);
    if (connection == null)
        throw new SQLException(String.format("can't get connection from database->", _database));

    connection.setAutoCommit(true);
    this.setDateFormat(connection);
    LineNumberReader reader = new LineNumberReader(new BufferedReader(new FileReader(csvFile_)));
    String[] tableColumnNames = table_.getColumnNames();
    DKDBTypeInfo[] typeInfos = _database.getColumnConcreteTypeInfos(table_);
    if (_debugEnabled) {
        _log.debug("tableColumnNames->{}", Arrays.toString(tableColumnNames));
        _log.debug("typeInfos->{}", Arrays.toString(typeInfos));
    }
    String line = null;
    List<String> updateStatements = new ArrayList<String>(LOAD_BATCH_SIZE);
    // assume first line is header, use column names to drive the line parse
    line = StringUtils.trimToNull(reader.readLine());
    String[] headerColumnNames = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
    int[] loadIndices = DKArrayUtil.getIndicesOfIntersection(headerColumnNames, tableColumnNames);
    if (_debugEnabled) {
        _log.debug("headerColumnNames->{}", Arrays.toString(headerColumnNames));
        _log.debug("loadIndices->{}", Arrays.toString(loadIndices));
    }
    for (long i = 1; (line = StringUtils.trimToNull(reader.readLine())) != null; i++) {
        String[] values = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
        if (_debugEnabled) {
            _log.debug("line: " + line);
            _log.debug("values: " + Arrays.toString(values));
        }
        DKStringUtil.unquote(values, Quote.DOUBLE);
        values = DKArrayUtil.retainElementsAtIndices(values, loadIndices);
        if (_debugEnabled) {
            _log.debug("values: " + Arrays.toString(values));
        }
        if (!(values.length == tableColumnNames.length))
            throw new RuntimeException(
                    String.format("number of values->%s does not match number of columns->%s", values.length,
                            tableColumnNames.length));
        String insertStatementString = _database.generateInsertDML(values, typeInfos, tableColumnNames,
                table_.getSchema(), table_.getTableName());
        updateStatements.add(insertStatementString);
        _log.debug("insertStatementString: " + insertStatementString);
        if (i % LOAD_BATCH_SIZE == 0) {
            DKSqlUtil.executeBatchUpdate(updateStatements, connection);
            _log.debug("inserted " + i + " rows");
            updateStatements.clear();
        }
    }
    long updates = DKSqlUtil.executeBatchUpdate(updateStatements, connection);
    DKSqlUtil.close(connection);
    _log.debug("updates: " + updates);
    reader.close();
    return true;
}

From source file:org.opencms.util.CmsRfsFileViewer.java

/**
 * Return the view portion of lines of text from the underlying file or an 
 * empty String if <code>{@link #isEnabled()}</code> returns <code>false</code>.<p>
 * /*w  ww . j  a va 2 s  .  c  o  m*/
 * @return the view portion of lines of text from the underlying file or an 
 *         empty String if <code>{@link #isEnabled()}</code> returns <code>false</code>
 * @throws CmsRfsException if something goes wrong
 */
public String readFilePortion() throws CmsRfsException {

    if (m_enabled) {
        // if we want to view the log file we have to set the internal m_windowPos to the last window 
        // to view the end: 
        int lines = -1;
        int startLine;
        if (m_isLogfile) {
            lines = scrollToFileEnd();
            // for logfile mode we show the last window of window size: 
            // it could be possible that only 4 lines are in the last window 
            // (e.g.: 123 lines with windowsize 10 -> last window has 3 lines) 
            // so we ignore the window semantics and show the n last lines: 
            startLine = lines - m_windowSize;
        } else {
            m_windowPos = 0;
            startLine = m_windowPos * m_windowSize;
        }
        LineNumberReader reader = null;
        try {
            // don't make the buffer too big, just big enough for windowSize lines (estimation: avg. of 200 characters per line) 
            // to save reading too much (this optimizes to read the first windows, much later windows will be slower...)
            reader = new LineNumberReader(
                    new BufferedReader(new InputStreamReader(new FileInputStream(m_filePath), m_fileEncoding)),
                    m_windowSize * 200);
            int currentLine = 0;
            // skip the lines to the current window:
            while (startLine > currentLine) {
                reader.readLine();
                currentLine++;
            }
            StringBuffer result = new StringBuffer();
            String read = reader.readLine();

            // logfile treatment is different
            // we invert the lines: latest come first
            if (m_isLogfile) {
                // stack is java hall of shame member... but standard
                Stack inverter = new Stack();
                for (int i = m_windowSize; (i > 0) && (read != null); i--) {
                    inverter.push(read);
                    read = reader.readLine();
                }
                // pop-off:
                while (!inverter.isEmpty()) {
                    result.append(inverter.pop());
                    result.append('\n');
                }
            } else {
                for (int i = m_windowSize; (i > 0) && (read != null); i--) {
                    result.append(read);
                    result.append('\n');
                    read = reader.readLine();
                }
            }
            return CmsEncoder.escapeXml(result.toString());
        } catch (IOException ioex) {
            CmsRfsException ex = new CmsRfsException(
                    Messages.get().container(Messages.ERR_FILE_ARG_ACCESS_1, m_filePath), ioex);
            throw ex;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            }
        }
    } else {
        return Messages.get().getBundle().key(Messages.GUI_FILE_VIEW_NO_PREVIEW_0);
    }
}

From source file:org.kalypso.model.wspm.core.imports.ImportTrippleHelper.java

/**
 * Imports the profile trippel data and converts it into IProfils
 * //  ww  w .j  a  va  2  s  .c  o  m
 * @param trippleFile
 *          file with profile tripples
 */
public static IProfile[] importTrippelData(final File trippleFile, final String separator,
        final String profileType, final String crs) throws CoreException {
    final IProfilePointPropertyProvider provider = KalypsoModelWspmCoreExtensions
            .getPointPropertyProviders(profileType);

    final IComponent rechtswert = provider.getPointProperty(IWspmPointProperties.POINT_PROPERTY_RECHTSWERT);
    final IComponent hochwert = provider.getPointProperty(IWspmPointProperties.POINT_PROPERTY_HOCHWERT);

    if (trippleFile == null)
        return new IProfile[0];

    /* read profiles, show warnings */
    final List<IProfile> profiles = new ArrayList<>();
    IProfile currentProfile = null;

    /* file loading */
    LineNumberReader fileReader = null;

    try (InputStreamReader inputReader = new InputStreamReader(new FileInputStream(trippleFile))) {
        fileReader = new LineNumberReader(inputReader);

        /* File Header */
        fileReader.readLine();

        IProfileRecord lastPoint = null;
        while (fileReader.ready()) {
            final String line = fileReader.readLine();
            if (line == null) {
                break;
            }

            /* ignore empty lines */
            if (StringUtils.isBlank(line)) {
                continue;
            }

            /* trippel-format should be: station, x, y, z */
            final String[] tokens = StringUtils.split(line, separator);

            /* continue just if there are enough values in the trippel file */
            if (tokens.length != 4) {
                // FIXME: better error handling
                // inform the user that his profile has not enough values...
                final String message = Messages.getString(
                        "org.kalypso.model.wspm.core.imports.ImportTrippleHelper.0", //$NON-NLS-1$
                        fileReader.getLineNumber());
                final IStatus status = new Status(IStatus.ERROR, KalypsoModelWspmCorePlugin.getID(), message);
                throw new CoreException(status);
            }

            try {
                /* first value = profile station */
                final double station = NumberUtils.parseDouble(tokens[0]);
                final BigDecimal currentStation = ProfileUtil.stationToBigDecimal(station);

                final BigDecimal currentProfileStation = currentProfile == null ? null
                        : ProfileUtil.stationToBigDecimal(currentProfile.getStation());

                if (!ObjectUtils.equals(currentStation, currentProfileStation)) {
                    lastPoint = null;

                    currentProfile = ProfileFactory.createProfil(profileType, null);

                    currentProfile.setStation(station);
                    currentProfile.setName(
                            Messages.getString("org.kalypso.model.wspm.core.imports.ImportTrippleHelper.1")); //$NON-NLS-1$
                    currentProfile.setDescription(
                            Messages.getString("org.kalypso.model.wspm.core.imports.ImportTrippleHelper.2")); //$NON-NLS-1$
                    currentProfile.setSrsName(crs);

                    currentProfile.addPointProperty(rechtswert);
                    currentProfile.addPointProperty(hochwert);

                    profiles.add(currentProfile);
                }

                final IProfileRecord point = ImportTrippleHelper.createProfilePoint(currentProfile, tokens,
                        lastPoint);
                if (point != null) {
                    currentProfile.addPoint(point);
                }

                lastPoint = point;
            } catch (final NumberFormatException e) {
                e.printStackTrace();
                final String message = Messages.getString(
                        "org.kalypso.model.wspm.core.imports.ImportTrippleHelper.3", //$NON-NLS-1$
                        fileReader.getLineNumber());
                final IStatus status = new Status(IStatus.ERROR, KalypsoModelWspmCorePlugin.getID(), message,
                        e);
                throw new CoreException(status);
            }
        }

        fileReader.close();
    } catch (final IOException e) {
        e.printStackTrace();

        final int lineNumber = fileReader == null ? 0 : fileReader.getLineNumber();

        final String message = Messages.getString("org.kalypso.model.wspm.core.imports.ImportTrippleHelper.4", //$NON-NLS-1$
                lineNumber);
        final IStatus status = new Status(IStatus.ERROR, KalypsoModelWspmCorePlugin.getID(), message, e);
        throw new CoreException(status);
    }

    return profiles.toArray(new IProfile[profiles.size()]);
}

From source file:com.castis.sysComp.PoisConverterSysComp.java

public void parseCategoryFile(File file, String sub) throws Exception {
    String line = "";
    FileInputStream in = null;/*from w w  w . jav  a2  s .  c  o  m*/
    Reader isReader = null;
    LineNumberReader bufReader = null;

    try {
        String encodingCharset = FILE_CHARSET;

        in = new FileInputStream(file);
        isReader = new InputStreamReader(in, encodingCharset);
        bufReader = new LineNumberReader(isReader);

        boolean first = true;
        while ((line = bufReader.readLine()) != null) {
            if (first == true) {
                first = false;
                continue;
            }

            //Depth, Menu ID, Menu Name, Hidden, Menu Type.
            String result[] = line.split(",");

            String depth = result[0];
            String menuId = result[1];
            String menuName = result[2];
            String hidden = result[3];
            String menuType = result[5];

            if (depth == null || depth.equals("")) {
                throw new DataParsingException("data parsing error(depth)");
            }
            if (menuId == null || menuId.equals("")) {
                throw new DataParsingException("data parsing error(Menu ID)");
            }
            if (menuName == null || menuName.equals("")) {
                throw new DataParsingException("data parsing error(Menu Name)");
            }
            if (hidden == null || hidden.equals("")) {
                throw new DataParsingException("data parsing error(Hidden)");
            }
            if (menuType == null || menuType.equals("")) {
                throw new DataParsingException("data parsing error(Menu Type)");
            }

        }
    } catch (Exception e) {
        String errorMsg = e.getMessage();
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    } finally {
        if (in != null)
            in.close();
        if (isReader != null)
            isReader.close();
        if (bufReader != null)
            bufReader.close();
    }

    String fileName = file.getName();
    int index = fileName.indexOf("-");
    if (index != -1) {
        fileName = fileName.substring(index + 1, fileName.length());
    }

    index = fileName.indexOf("_");

    String targetDir = resultDir;
    if (index != -1) {
        String directory = fileName.substring(0, index);
        targetDir += "/" + sub + "/" + directory;
    }

    try {
        File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
        if (!resultTargetDir.isDirectory()) {
            CiFileUtil.createDirectory(targetDir);
        }

        CiFileUtil.renameFile(file, targetDir, fileName);

    } catch (Exception e) {
        log.error(e.getMessage());
    }

}

From source file:net.rptools.maptool.launcher.MapToolLauncher.java

/**
 * Reads from a file named mt.cfg in the same directory to get the following
 * options. Each option is placed on a single line followed by an equal sign
 * ('=') and then the appropriate value. The default values are coded below.
 * //from  w  w w  .j  ava2s  . c  o  m
 * All memory sizes are in megabytes.
 */
private boolean readCfgFile() {
    boolean rv = false;

    // Set the defaults in the map.  As lines are read from the config file, overwrite the
    // map entries with the new values.  When we're done, we can look at the map entries
    // in an appropriate order and ensure dependencies are handled correctly as well as
    // convert values to the proper data types.
    final Map<String, String> values = new HashMap<String, String>(10);
    values.put("MAXMEM", Integer.toString(maxMemVal)); //$NON-NLS-1$
    values.put("MINMEM", Integer.toString(minMemVal)); //$NON-NLS-1$
    values.put("STACKSIZE", Integer.toString(stackSizeVal)); //$NON-NLS-1$
    values.put("PROMPT", "true"); //$NON-NLS-1$ //$NON-NLS-2$

    final List<String> errors = new ArrayList<String>();
    if (cfgFile.isFile() && cfgFile.length() > 0) {
        rv = true; // Assume that something was found.

        LineNumberReader lnbr = null;
        try {
            lnbr = new LineNumberReader(new BufferedReader(new FileReader(cfgFile)));
            try {
                String line = lnbr.readLine();
                while (line != null) {
                    line = line.trim();
                    if (!line.startsWith("#") && !line.isEmpty()) { //$NON-NLS-1$
                        final String[] arg = line.split("=", 2); // Only apply first delimiter //$NON-NLS-1$
                        if (arg.length == 2) {
                            values.put(arg[0].toUpperCase().trim(), arg[1].trim());
                        } else {
                            errors.add(CopiedFromOtherJars.getText("msg.error.configBadFormat", cfgFile, //$NON-NLS-1$
                                    lnbr.getLineNumber(), line));
                        }
                    }
                    line = lnbr.readLine();
                }
            } catch (final IOException ex) {
                logMsg(Level.SEVERE, "Error reading configuration file: {0}", "msg.error.configIOError", //$NON-NLS-1$
                        cfgFile); //$NON-NLS-2$
            }
        } catch (final FileNotFoundException ex) {
            // This shouldn't happen since we specifically used cfgFile.isFIle(), above, and that can't be true
            // unless the file actually exists.
            logMsg(Level.SEVERE, "Configuration file {0} not found.", "msg.error.configFileNotFound", cfgFile); //$NON-NLS-1$ $NON-NLS-2$
        } finally {
            try {
                lnbr.close();
            } catch (final IOException ex) {
                logMsg(Level.SEVERE, "Error closing configuration file {0}.", "msg.error.configClosing", //$NON-NLS-1$
                        cfgFile); //$NON-NLS-2$
            }
        }
    } else {
        logMsg(Level.INFO, "Configuration file not found; using built-in defaults", "msg.error.configNotFound", //$NON-NLS-1$
                cfgFile); //$NON-NLS-2$
    }
    // Build a list of all XML files in the same directory as the launcher.  This list of
    // filenames will be used to validate the LOGGING parameter from the configuration file.
    File logging = new File(currentDir, "logging"); //$NON-NLS-1$
    if (!logging.isDirectory()) {
        logging = currentDir;
    }
    logConfigs = buildLoggingFileList(logging);

    // Now process the records just read in (or the defaults).  Errors are accumulated into 'errors'.
    parseCfgValues(values, errors);
    if (!errors.isEmpty()) {
        errors.add(0, CopiedFromOtherJars.getText("msg.info.configFeedback")); //$NON-NLS-1$
        CopiedFromOtherJars.showFeedback(ERROR_MESSAGE, errors.toArray());
    }
    // Keep track of the original values.  When we go to save the configuration file, we
    // only write to it if something has changed.
    originalSettings = values;

    // Update UI fields for these three values.
    assignMinMem();
    assignMaxMem();
    assignStackSize();

    updateStrings();
    return rv;
}

From source file:com.acmeair.loader.FlightLoader.java

public void loadFlights() throws Exception {
    InputStream csvInputStream = FlightLoader.class.getResourceAsStream("/mileage.csv");

    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(csvInputStream));
    String line1 = lnr.readLine();
    StringTokenizer st = new StringTokenizer(line1, ",");
    ArrayList<AirportCodeMapping> airports = new ArrayList<AirportCodeMapping>();

    // read the first line which are airport names
    while (st.hasMoreTokens()) {
        AirportCodeMapping acm = new AirportCodeMapping();
        acm.setAirportName(st.nextToken());
        airports.add(acm);//from   w w w .j av  a  2 s .co  m
    }
    // read the second line which contains matching airport codes for the first line
    String line2 = lnr.readLine();
    st = new StringTokenizer(line2, ",");
    int ii = 0;
    while (st.hasMoreTokens()) {
        String airportCode = st.nextToken();
        airports.get(ii).setAirportCode(airportCode);
        ii++;
    }
    // read the other lines which are of format:
    // airport name, aiport code, distance from this airport to whatever airport is in the column from lines one and two
    String line;
    int flightNumber = 0;
    while (true) {
        line = lnr.readLine();
        if (line == null || line.trim().equals("")) {
            break;
        }
        st = new StringTokenizer(line, ",");
        String airportName = st.nextToken();
        String airportCode = st.nextToken();
        if (!alreadyInCollection(airportCode, airports)) {
            AirportCodeMapping acm = new AirportCodeMapping();
            acm.setAirportName(airportName);
            acm.setAirportCode(airportCode);
            airports.add(acm);
        }
        int indexIntoTopLine = 0;
        while (st.hasMoreTokens()) {
            String milesString = st.nextToken();
            if (milesString.equals("NA")) {
                indexIntoTopLine++;
                continue;
            }
            int miles = Integer.parseInt(milesString);
            String toAirport = airports.get(indexIntoTopLine).getAirportCode();
            String flightId = "AA" + flightNumber;
            FlightSegment flightSeg = new FlightSegment(flightId, airportCode, toAirport, miles);
            flightService.storeFlightSegment(flightSeg);
            Date now = new Date();
            for (int daysFromNow = 0; daysFromNow < MAX_FLIGHTS_PER_SEGMENT; daysFromNow++) {
                Calendar c = Calendar.getInstance();
                c.setTime(now);
                c.set(Calendar.HOUR_OF_DAY, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                c.set(Calendar.MILLISECOND, 0);
                c.add(Calendar.DATE, daysFromNow);
                Date departureTime = c.getTime();
                Date arrivalTime = getArrivalTime(departureTime, miles);
                flightService.createNewFlight(flightId, departureTime, arrivalTime, new BigDecimal(500),
                        new BigDecimal(200), 10, 200, "B747");

            }
            flightNumber++;
            indexIntoTopLine++;
        }
    }

    for (int jj = 0; jj < airports.size(); jj++) {
        flightService.storeAirportMapping(airports.get(jj));
    }
    lnr.close();
}

From source file:edu.stanford.muse.util.EmailUtils.java

private static Map<String, String> readDBpedia(double p, String typesFile) {
    if (dbpedia != null) {
        if (p == 1)
            return dbpedia;
        else/*w ww .  j  av a 2 s .com*/
            return new org.apache.commons.collections4.map.CaseInsensitiveMap<>(sample(dbpedia, p));
    }
    if (typesFile == null)
        typesFile = Config.DBPEDIA_INSTANCE_FILE;
    //dbpedia = new LinkedHashMap<>();
    //we want to be able to access elements in the map in a case-sensitive manner, this is a way to do that.
    dbpedia = new org.apache.commons.collections4.map.CaseInsensitiveMap<>();
    int d = 0, numPersons = 0, lines = 0;
    try {
        InputStream is = Config.getResourceAsStream(typesFile);
        if (is == null) {
            log.warn("DBpedia file resource could not be read!!");
            return dbpedia;
        }

        //true argument for BZip2CompressorInputStream so as to load the whole file content into memory
        LineNumberReader lnr = new LineNumberReader(
                new InputStreamReader(new BZip2CompressorInputStream(is, true), "UTF-8"));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            if (lines++ % 1000000 == 0)
                log.info("Processed " + lines + " lines of approx. 3.02M in " + typesFile);

            if (line.contains("GivenName"))
                continue;

            String[] words = line.split("\\s+");
            String r = words[0];

            /**
             * The types file contains lines like this:
             * National_Bureau_of_Asian_Research Organisation|Agent
             * National_Bureau_of_Asian_Research__1 PersonFunction
             * National_Bureau_of_Asian_Research__2 PersonFunction
             * Which leads to classifying "National_Bureau_of_Asian_Research" as PersonFunction and not Org.
             */
            if (r.contains("__")) {
                d++;
                continue;
            }
            //if it still contains this, is a bad title.
            if (r.equals("") || r.contains("__")) {
                d++;
                continue;
            }
            String type = words[1];
            //Royalty names, though tagged person are very weird, contains roman characters and suffixes like of_Poland e.t.c.
            if (type.equals("PersonFunction") || type.equals("Royalty|Person|Agent"))
                continue;
            //in places there are things like: Shaikh_Ibrahim,_Iraq
            if (type.endsWith("Settlement|PopulatedPlace|Place"))
                r = r.replaceAll(",_.*", "");

            //its very dangerous to remove things inside brackets as that may lead to terms like
            //University_(Metrorail_Station) MetroStation|Place e.t.c.
            //so keep them, or just skip this entry all together
            //We are not considering single word tokens any way, so its OK to remove things inside the brackets
            //removing stuff in brackets may cause trouble when blind matching entities
            //r = r.replaceAll("_\\(.*?\\)", "");
            String title = r.replaceAll("_", " ");

            String badSuffix = "|Agent";
            if (type.endsWith(badSuffix) && type.length() > badSuffix.length())
                type = type.substring(0, type.length() - badSuffix.length());
            if (type.endsWith("|Person"))
                numPersons++;
            type = type.intern(); // type strings are repeated very often, so intern

            if (type.equals("Road|RouteOfTransportation|Infrastructure|ArchitecturalStructure|Place")) {
                //System.err.print("Cleaned: "+title);
                title = cleanDBPediaRoad(title);
                //System.err.println(" to "+title);
            }
            dbpedia.put(title, type);
        }
        lnr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    log.info("Read " + dbpedia.size() + " names from DBpedia, " + numPersons + " people name. dropped: " + d);

    return new org.apache.commons.collections4.map.CaseInsensitiveMap<>(sample(dbpedia, p));
}

From source file:org.opencms.setup.CmsSetupDb.java

/**
 * Internal method to parse and execute a setup script.<p>
 *
 * @param inputReader an input stream reader on the setup script
 * @param replacers the replacements to perform in the script
 * @param abortOnError if a error occurs this flag indicates if to continue or to abort
 */// w  w w . j  a v  a2 s. c om
private void executeSql(Reader inputReader, Map<String, String> replacers, boolean abortOnError) {

    String statement = "";
    LineNumberReader reader = null;
    String line = null;

    // parse the setup script
    try {
        reader = new LineNumberReader(inputReader);

        while (true) {
            line = reader.readLine();
            if (line == null) {
                break;
            }
            StringTokenizer st = new StringTokenizer(line);

            while (st.hasMoreTokens()) {
                String currentToken = st.nextToken();

                // comment! Skip rest of the line
                if (currentToken.startsWith("#")) {
                    break;
                }

                // not to be executed
                if (currentToken.startsWith("prompt")) {
                    break;
                }

                // add token to query
                statement += " " + currentToken;

                // query complete (terminated by ';')
                if (currentToken.endsWith(";")) {
                    // cut of ';' at the end
                    statement = statement.substring(0, (statement.length() - 1));

                    // normal statement, execute it
                    try {
                        if (replacers != null) {
                            statement = replaceTokens(statement, replacers);
                            executeStatement(statement);
                        } else {
                            executeStatement(statement);
                        }
                    } catch (SQLException e) {
                        if (!abortOnError) {
                            if (m_errorLogging) {
                                m_errors.add("Error executing SQL statement: " + statement);
                                m_errors.add(CmsException.getStackTraceAsString(e));
                            }
                        } else {
                            throw e;
                        }
                    }

                    // reset
                    statement = "";
                }
            }

            statement += " \n";
        }
    } catch (SQLException e) {
        if (m_errorLogging) {
            m_errors.add("Error executing SQL statement: " + statement);
            m_errors.add(CmsException.getStackTraceAsString(e));
        }
    } catch (Exception e) {
        if (m_errorLogging) {
            m_errors.add("Error parsing database setup SQL script in line: " + line);
            m_errors.add(CmsException.getStackTraceAsString(e));
        }
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (Exception e) {
            // noop
        }
    }
}

From source file:com.acmeair.web.LoaderREST.java

public void loadFlights(int segments) throws Exception {
    System.out.println("Loading flight data...");
    InputStream csvInputStream = getClass().getResourceAsStream("/mileage.csv");
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(csvInputStream));
    String line1 = lnr.readLine();
    StringTokenizer st = new StringTokenizer(line1, ",");
    ArrayList<AirportCodeMapping> airports = new ArrayList<AirportCodeMapping>();

    // read the first line which are airport names
    while (st.hasMoreTokens()) {
        AirportCodeMapping acm = new AirportCodeMapping();
        acm.setAirportName(st.nextToken());
        airports.add(acm);//from w  w  w. j  a  va  2 s .  c o  m
    }
    // read the second line which contains matching airport codes for the
    // first line
    String line2 = lnr.readLine();
    st = new StringTokenizer(line2, ",");
    int ii = 0;
    while (st.hasMoreTokens()) {
        String airportCode = st.nextToken();
        airports.get(ii).setAirportCode(airportCode);
        ii++;
    }
    // read the other lines which are of format:
    // airport name, aiport code, distance from this airport to whatever
    // airport is in the column from lines one and two
    String line;
    int flightNumber = 0;
    while (true) {
        line = lnr.readLine();
        if (line == null || line.trim().equals("")) {
            break;
        }
        st = new StringTokenizer(line, ",");
        String airportName = st.nextToken();
        String airportCode = st.nextToken();
        if (!alreadyInCollection(airportCode, airports)) {
            AirportCodeMapping acm = new AirportCodeMapping();
            acm.setAirportName(airportName);
            acm.setAirportCode(airportCode);
            airports.add(acm);
        }
        int indexIntoTopLine = 0;
        while (st.hasMoreTokens()) {
            String milesString = st.nextToken();
            if (milesString.equals("NA")) {
                indexIntoTopLine++;
                continue;
            }
            int miles = Integer.parseInt(milesString);
            String toAirport = airports.get(indexIntoTopLine).getAirportCode();
            if (!flightService.getFlightByAirports(airportCode, toAirport).isEmpty()) {
                // already there
                continue;
            }
            String flightId = "AA" + flightNumber;
            FlightSegment flightSeg = new FlightSegment(flightId, airportCode, toAirport, miles);
            flightService.storeFlightSegment(flightSeg);
            Date now = new Date();
            for (int daysFromNow = 0; daysFromNow < segments; daysFromNow++) {
                Calendar c = Calendar.getInstance();
                c.setTime(now);
                c.set(Calendar.HOUR_OF_DAY, 0);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                c.set(Calendar.MILLISECOND, 0);
                c.add(Calendar.DATE, daysFromNow);
                Date departureTime = c.getTime();
                Date arrivalTime = getArrivalTime(departureTime, miles);
                flightService.createNewFlight(flightId, departureTime, arrivalTime, new BigDecimal(500),
                        new BigDecimal(200), 10, 200, "B747");

            }
            flightNumber++;
            indexIntoTopLine++;
        }
    }

    for (int jj = 0; jj < airports.size(); jj++) {
        flightService.storeAirportMapping(airports.get(jj));
    }
    lnr.close();
    System.out.println("Done loading flight data.");
}

From source file:edu.stanford.muse.util.ThunderbirdUtils.java

/** returns a list of thunderbird accounts.
   each list is in turn a list of length exactly 4:
   account name, hostname, server type, user name
*///w w  w .ja  v  a 2 s . c  o m
public static List<List<String>> getThunderbirdAccounts() {
    try {
        List<List<String>> newResult = getThunderbirdAccountsNew();
        if (newResult != null)
            return newResult;
    } catch (Exception e) {
        log.warn("unable to process thunderbird profile with new thunderbird parser");
        Util.print_exception(e, log);
    }

    List<List<String>> result = new ArrayList<List<String>>();

    try {
        String rootDir = ThunderbirdUtils.getThunderbirdProfileDir();
        String prefs = rootDir + File.separator + "prefs.js";
        File f = new File(prefs);
        if (!f.exists() || !f.canRead()) {
            EmailUtils.log.info("Thunderbird probably not installed: no prefs.js in directory: " + prefs);
            return result;
        }

        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(prefs), "UTF-8"));

        // example fragment of input
        //        user_pref("mail.server.server2.capability", 21520929);
        //        user_pref("mail.server.server2.directory", "AAAAAAH0AAIAAQxNYWNpbnRvc2ggSEQAAAAAAAAAAAAAAAAAAADGqxmGSCsAAAAJi1ASeGVub24uc3RhbmZvcmQuZWR1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAmLUcbnohEAAAAAAAAAAP////8AAAkgAAAAAAAAAAAAAAAAAAAACEltYXBNYWlsABAACAAAxqt79gAAABEACAAAxugEgQAAAAEAHAAJi1AACYsXAAmLFQAJiw8AB/mpAAf5qAAAkOcAAgBjTWFjaW50b3NoIEhEOlVzZXJzOmhhbmdhbDpMaWJyYXJ5OlRodW5kZXJiaXJkOlByb2ZpbGVzOjcyeHZzMXd3LmRlZmF1bHQ6SW1hcE1haWw6eGVub24uc3RhbmZvcmQuZWR1AAAOACYAEgB4AGUAbgBvAG4ALgBzAHQAYQBuAGYAbwByAGQALgBlAGQAdQAPABoADABNAGEAYwBpAG4AdABvAHMAaAAgAEgARAASAFZVc2Vycy9oYW5nYWwvTGlicmFyeS9UaHVuZGVyYmlyZC9Qcm9maWxlcy83Mnh2czF3dy5kZWZhdWx0L0ltYXBNYWlsL3hlbm9uLnN0YW5mb3JkLmVkdQATAAEvAAAVAAIADf//AAA=");
        //        user_pref("mail.server.server2.directory-rel", "[ProfD]ImapMail/xenon.stanford.edu");
        //        user_pref("mail.server.server2.download_on_biff", true);
        //        user_pref("mail.server.server2.hostname", "xenon.stanford.edu");
        //        user_pref("mail.server.server2.login_at_startup", true);
        //        user_pref("mail.server.server2.max_cached_connections", 5);
        //        user_pref("mail.server.server2.name", "Stanford CS");
        //        user_pref("mail.server.server2.namespace.other_users", "\"~\"");
        //        user_pref("mail.server.server2.namespace.personal", "\"#mh/\",\"#mhinbox\",\"\"");
        //        user_pref("mail.server.server2.namespace.public", "\"#public/\",\"#news.\",\"#ftp/\",\"#shared/\"");
        //        user_pref("mail.server.server2.timeout", 29);
        //        user_pref("mail.server.server2.type", "imap");
        //        user_pref("mail.server.server2.userName", "hangal");

        // be careful not to match a ...hostname line or a ...namespace line with the .name pattern
        //
        // that's why we need an explicit dot before and a quote after the type of field in the pattern

        // note: there are 2 fields: hostname and realhostname - realhostname has precedence if it exists
        // see: http://forums.mozillazine.org/viewtopic.php?f=39&t=1697195
        Pattern accountNamePat = Pattern.compile(".*\"mail.server.server.*\\.name\".*");
        Pattern hostnamePat = Pattern.compile(".*\"mail.server.server.*\\.hostname\".*");
        Pattern realHostnamePat = Pattern.compile(".*\"mail.server.server.*\\.realhostname\".*");
        Pattern serverTypePat = Pattern.compile(".*\"mail.server.server.*\\.type\".*");
        Pattern usernamePat = Pattern.compile(".*\"mail.server.server.*\\.userName\".*");
        Pattern userRealNamePat = Pattern.compile(".*\"mail.identity.id.*\\.fullName\".*");
        Pattern userEmailPat = Pattern.compile(".*\"mail.identity.id.*\\.useremail\".*");
        Pattern directoryRelPat = Pattern.compile(".*\"mail.server.server.*\\.directory-rel\".*");
        Pattern fccFolderPat = Pattern.compile(".*\"mail.identity.id.*\\.fcc_folder\".*");

        Map<String, String> accountNameMap = new LinkedHashMap<String, String>();
        Map<String, String> hostnameMap = new LinkedHashMap<String, String>();
        Map<String, String> realHostnameMap = new LinkedHashMap<String, String>();
        Map<String, String> serverTypeMap = new LinkedHashMap<String, String>();
        Map<String, String> usernameMap = new LinkedHashMap<String, String>();
        Map<String, String> userEmailMap = new LinkedHashMap<String, String>();
        Map<String, String> userRealNameMap = new LinkedHashMap<String, String>();
        Map<String, String> directoryRelMap = new LinkedHashMap<String, String>();
        Map<String, String> fccFolderMap = new LinkedHashMap<String, String>();

        while (true) {
            String line = lnr.readLine();
            if (line == null) {
                lnr.close();
                break;
            }

            if (accountNamePat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "server");
                accountNameMap.put(pair.getFirst(), pair.getSecond());
            }
            if (hostnamePat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "server");
                hostnameMap.put(pair.getFirst(), pair.getSecond());
            }
            if (realHostnamePat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "server");
                realHostnameMap.put(pair.getFirst(), pair.getSecond());
            } else if (serverTypePat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "server");
                String serverType = pair.getSecond();
                if ("imap".equals(serverType))
                    serverType = "imaps";
                if ("pop".equals(serverType))
                    serverType = "pops";
                serverTypeMap.put(pair.getFirst(), serverType);
            } else if (usernamePat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "server");
                usernameMap.put(pair.getFirst(), pair.getSecond());
            } else if (userEmailPat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "id");
                userEmailMap.put(pair.getFirst(), pair.getSecond());
            } else if (userRealNamePat.matcher(line).matches()) {
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "id");
                userRealNameMap.put(pair.getFirst(), pair.getSecond());
            } else if (directoryRelPat.matcher(line).matches()) {
                // for local folders the line is like user_pref("mail.server.server1.directory-rel", "[ProfD]../../../../../../tmp/tb");
                // Convert [ProfD]../../../../../../tmp/tb to the correct path by replacing [ProfD] with the profile dir
                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "server");
                String directoryRel = pair.getSecond();
                if (directoryRel != null) {
                    if (directoryRel.startsWith("[ProfD]"))
                        directoryRel = directoryRel.replace("[ProfD]",
                                ThunderbirdUtils.getThunderbirdProfileDir() + File.separator);
                    // we also have to correct the ../../ to \..\... for windows
                    directoryRel = directoryRel.replaceAll("/", File.separator);
                    directoryRelMap.put(pair.getFirst(), directoryRel);
                }
            } else if (fccFolderPat.matcher(line).matches()) {
                // the line looks like user_pref("mail.identity.id1.fcc_folder", "imap://hangal@xenon.stanford.edu/Sent");

                Pair<String, String> pair = ThunderbirdUtils.parseLine(line, "id");
                String fccFolderFull = pair.getSecond();
                if (fccFolderFull != null) {
                    // fccFolder imap://hangal@xenon.stanford.edu/Sent
                    String fccFolder = fccFolderFull.replaceAll("[^/]*/+[^/]*/+(.*$)", "$1"); // skip the first 2 tokens, split by /
                    if (!fccFolderFull.equals(fccFolder)) // only if not equal is it valid
                        fccFolderMap.put(pair.getFirst(), fccFolder);
                }
            }
        }

        for (String key : serverTypeMap.keySet()) {
            String s = serverTypeMap.get(key).toLowerCase();
            // we only know how to handle imap and pop and local folders
            // other things like smart folders, don't list.
            if (!s.startsWith("imap") && !s.startsWith("pop")
                    && !"Local Folders".equals(accountNameMap.get(key)))
                continue;

            List<String> params = new ArrayList<String>();
            params.add(accountNameMap.get(key));
            String hostnameToUse = realHostnameMap.get(key);
            if (hostnameToUse == null)
                hostnameToUse = hostnameMap.get(key);
            params.add(hostnameToUse);
            params.add(serverTypeMap.get(key));
            params.add(usernameMap.get(key));
            params.add(userEmailMap.get(key));
            params.add(userRealNameMap.get(key));
            params.add(directoryRelMap.get(key));
            params.add(fccFolderMap.get(key));

            String str = "Tbird accountname=\"" + accountNameMap.get(key) + "\" " + "hostname=\""
                    + hostnameMap.get(key) + "\" " + "serverType=\"" + serverTypeMap.get(key) + "\" "
                    + "username=\"" + usernameMap.get(key) + "\" " + "userEmail=\"" + userEmailMap.get(key)
                    + "\" " + "userRealName=\"" + userRealNameMap.get(key) + "\" " + "directoryRel=\""
                    + directoryRelMap.get(key) + "\"" + "fccFolder=\"" + fccFolderMap.get(key) + "\"";

            EmailUtils.log.debug(str);
            // System.out.println(str);
            result.add(params);
        }

        lnr.close();
    } catch (Exception e) {
        System.err.println("REAL WARNING: exception trying to read thunderbird prefs" + Util.stackTrace(e));
    }

    return Collections.unmodifiableList(result);
}