Example usage for java.lang String subSequence

List of usage examples for java.lang String subSequence

Introduction

In this page you can find the example usage for java.lang String subSequence.

Prototype

public CharSequence subSequence(int beginIndex, int endIndex) 

Source Link

Document

Returns a character sequence that is a subsequence of this sequence.

Usage

From source file:com.intel.ssg.dcst.panthera.parse.sql.SqlXlateException.java

public void outputException(String origin) {
    if (this.node == null) {
        messagePrepare(super.getMessage());
        return;// w w w  .ja  va2s . com
    }
    messagePrepare("Error node '" + this.node.getText().toUpperCase() + "': " + super.getMessage());
    if (this.node.getLine() == 0) {
        return;
    }
    int linenum = this.node.getLine() - 1;

    String[] linetext = origin.split("\n");
    linetext[linetext.length - 1] += " ;";
    String lineptr = linetext[linenum].replaceAll(".", " ");
    int i = 0;
    for (; i <= linenum; i++) {
        messagePrepare(linetext[i].toUpperCase());
    }
    messagePrepare(lineptr.subSequence(0, this.node.getCharPositionInLine()) + "^"
            + lineptr.subSequence(this.node.getCharPositionInLine() + 1, lineptr.length()));
    for (; i < linetext.length; i++) {
        messagePrepare(linetext[i].toUpperCase());
    }
}

From source file:fr.renzo.wikipoff.ui.activities.ArticleActivity.java

private String capitalize(String text) {
    String res = "";
    if (text.length() > 0) {
        res = String.valueOf(text.charAt(0)).toUpperCase() + text.subSequence(1, text.length());
    } else {/*  w  w w  .  j a va  2s.c  om*/
        res = text.toUpperCase();
    }
    return res;
}

From source file:risk_mgnt_manager.CSVLoader.java

/**
 * Parse CSV file using OpenCSV library and load in 
 * given database table. /* www .  j a  v a2 s .com*/
 * @param csvFile Input CSV file
 * @param table Database table name to import data
 * @param col_Names
 * @param id
 * @param header
 */
public void loadCSV(String csvFile, String table, String col_Names, int id, boolean header) throws Exception {
    CSVReader csvReader = null;
    if (null == this.connection) {
        throw new Exception("Not a valid connection.");
    }
    try {
        csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error occured while executing file. " + e.getMessage());
    }

    String[] headerRow;

    if (header == true) {
        headerRow = csvReader.readNext();
    } else {
        headerRow = col_Names.split(",");
    }

    if (null == headerRow) {
        throw new FileNotFoundException(
                "No columns defined in given CSV file." + "Please check the CSV file format.");
    }

    String questionmarks = StringUtils.repeat("?,", headerRow.length);
    questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);

    String query = SQL_INSERT.replaceFirst(TABLE_REGEX, table);
    //System.out.println(header);

    //query = query.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
    query = query.replaceFirst(KEYS_REGEX, col_Names);
    query = query.replaceFirst(VALUES_REGEX, questionmarks);

    System.out.println("Query: " + query);

    String[] nextLine;
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = this.connection;
        con.setAutoCommit(false);
        ps = con.prepareStatement(query);

        if (id == 1) {
            //delete data from table before loading csv
            con.createStatement().execute("DELETE FROM " + table);
        }

        final int batchSize = 4096;
        int count = 0;
        Date date = null;
        while ((nextLine = csvReader.readNext()) != null) {

            if (null != nextLine) {
                int index = 1;
                for (String string : nextLine) {
                    //date = DateUtill.convertToDate(string);
                    if (null != date) {
                        ps.setDate(index++, new java.sql.Date(date.getTime()));
                    } else {
                        ps.setString(index++, string);
                    }
                }
                ps.addBatch();
            }
            if (++count % batchSize == 0) {
                ps.executeBatch();
            }
        }
        ps.executeBatch(); // insert remaining records
        con.commit();
    } catch (Exception e) {
        con.rollback();
        e.printStackTrace();
        throw new Exception("Error occured while loading data from file to database." + e.getMessage());
    } finally {
        if (null != ps)
            ps.close();
        if (null != con)
            con.close();
        csvReader.close();
    }
}

From source file:risk_mgnt_manager.StressCSVLoader.java

/**
 * Parse CSV file using OpenCSV library and load in 
 * given database table. /*www . jav a 2  s.c  o  m*/
 * @param csvFile Input CSV file
 * @param tableName Database table name to import data
 * @throws Exception
 */
public void loadCSV(String csvFile, String tableName, String filename) throws Exception {

    CSVReader csvReader = null;
    if (null == this.connection) {
        throw new Exception("Not a valid connection.");
    }
    try {
        csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error occured while executing file. " + e.getMessage());
    }

    String[] headerRow = csvReader.readNext();

    if (null == headerRow) {
        throw new FileNotFoundException(
                "No columns defined in given CSV file." + "Please check the CSV file format.");
    }

    String questionmarks = StringUtils.repeat("?,", headerRow.length);
    questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);
    String col_Names = "Point_in_Time, Portfolio, Fut_Variation, Opt_Variation, Total_Variation, Percent_of_Ledger_Balance, Ledger_Balance";

    String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
    //query = query.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
    query = query.replaceFirst(KEYS_REGEX, col_Names);
    query = query.replaceFirst(VALUES_REGEX, questionmarks);

    String update = "UPDATE mgex_riskmgnt." + tableName + " SET Imported_File_Name = '" + filename + "' Where "
            + "Imported_File_Name IS NULL";

    System.out.println("Query: " + query);
    System.out.println("Update: " + update);

    String[] nextLine;
    Connection con = null;
    Connection con2 = null;
    PreparedStatement ps = null;
    PreparedStatement ps2 = null;
    try {
        con = this.connection;
        con.setAutoCommit(false);
        ps = con.prepareStatement(query);

        con2 = this.connection;
        con2.setAutoCommit(false);
        ps2 = con2.prepareStatement(update);

        final int batchSize = 1000;
        int count = 0;
        Date date = null;
        while ((nextLine = csvReader.readNext()) != null) {

            if (null != nextLine) {
                int index = 1;
                for (String string : nextLine) {
                    //date = DateUtill.convertToDate(string);
                    if (null != date) {
                        ps.setDate(index++, new java.sql.Date(date.getTime()));
                    } else {
                        ps.setString(index++, string);
                    }
                }
                ps.addBatch();
            }
            if (++count % batchSize == 0) {
                ps.executeBatch();
            }
        }
        ps.executeBatch(); // insert remaining records
        ps2.executeUpdate();
        con.commit();
        con2.commit();
    } catch (Exception e) {
        con.rollback();
        con2.rollback();
        e.printStackTrace();
        throw new Exception("Error occured while loading data from file to database." + e.getMessage());
    } finally {
        if (null != ps)
            ps.close();
        ps2.close();
        if (null != con)
            con.close();
        con2.close();

        csvReader.close();
    }
}

From source file:oscar.oscarLab.ca.all.Hl7textResultsData.java

private static String[] splitRefRange(String refRangeTxt) {
    refRangeTxt = refRangeTxt.trim();//www.  j  a  v  a2s.co m
    String[] refRange = { "", "", "" };
    String numeric = "-. 0123456789";
    boolean textual = false;
    if (refRangeTxt == null || refRangeTxt.length() == 0)
        return refRange;

    for (int i = 0; i < refRangeTxt.length(); i++) {
        if (!numeric.contains(refRangeTxt.subSequence(i, i + 1))) {
            if (i > 0 || (refRangeTxt.charAt(i) != '>' && refRangeTxt.charAt(i) != '<')) {
                textual = true;
                break;
            }
        }
    }
    if (textual) {
        refRange[0] = refRangeTxt;
    } else {
        if (refRangeTxt.charAt(0) == '>') {
            refRange[1] = refRangeTxt.substring(1).trim();
        } else if (refRangeTxt.charAt(0) == '<') {
            refRange[2] = refRangeTxt.substring(1).trim();
        } else {
            String[] tmp = refRangeTxt.split("-");
            if (tmp.length == 2) {
                refRange[1] = tmp[0].trim();
                refRange[2] = tmp[1].trim();
            } else {
                refRange[0] = refRangeTxt;
            }
        }
    }
    return refRange;
}

From source file:datawarehouse.CSVLoader.java

/**
 * Parse CSV file using OpenCSV library and load in given database table.
 *
 * @param csvFile Input CSV file//  w ww.j a va 2 s .  c o m
 * @param tableName Database table name to import data
 * @param truncateBeforeLoad Truncate the table before inserting new
 * records.
 * @throws Exception
 */
public void loadCSV(String csvFile, String tableName, boolean truncateBeforeLoad) throws Exception {

    CSVReader csvReader = null;
    if (null == this.connection) {
        throw new Exception("Not a valid connection.");
    }
    try {

        csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error occured while executing file. " + e.getMessage());
    }

    String[] headerRow = csvReader.readNext();

    if (null == headerRow) {
        throw new FileNotFoundException(
                "No columns defined in given CSV file." + "Please check the CSV file format.");
    }

    String questionmarks = StringUtils.repeat("?,", headerRow.length);
    questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);

    String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
    query = query.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
    query = query.replaceFirst(VALUES_REGEX, questionmarks);

    System.out.println("Query: " + query);

    String[] nextLine;
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = this.connection;
        con.setAutoCommit(false);
        ps = con.prepareStatement(query);

        if (truncateBeforeLoad) {
            //delete data from table before loading csv
            con.createStatement().execute("DELETE FROM " + tableName);
        }

        final int batchSize = 1000;
        int count = 0;
        while ((nextLine = csvReader.readNext()) != null) {
            if (null != nextLine) {
                int index = 1;
                for (String string : nextLine) {
                    //System.out.print(string + ": ");
                    try {
                        DateFormat format = new SimpleDateFormat("dd.mm.yyyy");
                        Date date = format.parse(string);
                        ps.setDate(index++, new java.sql.Date(date.getTime()));
                        //System.out.println("date");
                    } catch (ParseException | SQLException e) {
                        try {
                            Double income = parseDouble(string.replace(",", "."));
                            ps.setDouble(index++, income);
                            //System.out.println("double");
                        } catch (NumberFormatException | SQLException err) {
                            ps.setString(index++, string);
                            //System.out.println("string");
                        }
                    }
                }
                ps.addBatch();
            }
            if (++count % batchSize == 0) {
                ps.executeBatch();
            }
        }
        ps.executeBatch(); // insert remaining records
        con.commit();
    } catch (Exception e) {
        con.rollback();
        e.printStackTrace();
        throw new Exception("Error occured while loading data from file to database." + e.getMessage());
    } finally {
        if (null != ps) {
            ps.close();
        }
        if (null != con) {
            con.close();
        }

        csvReader.close();
    }
}

From source file:org.amplafi.flow.web.bindings.ValueFromBindingProviderImpl.java

/**
 *
 * @see org.amplafi.flow.launcher.ValueFromBindingProvider#getValueFromBinding(java.lang.Object, java.lang.String)
 *///from  w w  w  .j a v  a  2 s .c  om
@Override
public Object getValueFromBinding(Object root, String lookup) {
    if (lookup.charAt(0) == lookup.charAt(lookup.length() - 1)
            && (lookup.charAt(0) == '\'' || lookup.charAt(0) == '\"')) {
        // return literal string
        return lookup.subSequence(1, lookup.length() - 1);
    }
    if (root != null) {
        String[] props = lookup.split("\\.");
        for (String prop : props) {
            if (root == null) {
                break;
            }
            PropertyAdaptor pa;
            try {
                // could also be a calculated value that maybe we are not supposed to parse.
                pa = PropertyUtils.getPropertyAdaptor(root, prop);
            } catch (org.apache.hivemind.ApplicationRuntimeException e) {
                return lookup;
            }

            if (pa == null) {
                getLog().warn(root.getClass() + "." + prop + " property in path " + lookup
                        + " does not define a property ");
                root = null;
            } else if (!pa.isReadable()) {
                getLog().warn(root.getClass() + "." + prop + " property in path " + lookup
                        + " does not define a readable property ");
                root = null;
            } else {
                root = pa.read(root);
            }
        }
        if (root instanceof IFormComponent) {
            IFormComponent component = (IFormComponent) root;
            IBinding binding = component.getBinding("value");
            if (binding == null) {
                getLog().debug(lookup + ": component does not have a value binding");
            } else {
                root = binding.getObject();
            }
        }
    }
    return root;
}

From source file:org.esupportail.papercut.domain.PayBoxForm.java

public String getParamsAsString() {
    String paramsAsString = "";
    SortedMap<String, String> params = getOrderedParams();
    for (String key : params.keySet()) {
        paramsAsString = paramsAsString + key + "=" + params.get(key) + "&";
    }/*  w w w  .j av  a2s.  co m*/
    paramsAsString = paramsAsString.subSequence(0, paramsAsString.length() - 1).toString();
    try {
        // paramsAsString = URLEncoder.encode(paramsAsString, "utf8");
        System.out.println(paramsAsString);
        return paramsAsString;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.sufficientlysecure.keychain.ui.adapter.KeyListPublicAdapter.java

/**
 * Header IDs should be static, position=1 should always return the same Id that is.
 *///from  w  ww.  jav a 2 s  .com
@Override
public long getHeaderId(int position) {
    if (!mDataValid) {
        // no data available at this point
        Log.d(Constants.TAG, "getHeaderView: No data available at this point!");
        return -1;
    }

    if (!mCursor.moveToPosition(position)) {
        throw new IllegalStateException("couldn't move cursor to position " + position);
    }

    // return the first character of the name as ID because this is what
    // headers are based upon
    String userId = mCursor.getString(mSectionColumnIndex);
    if (userId != null && userId.length() > 0) {
        return userId.subSequence(0, 1).charAt(0);
    } else {
        return Long.MAX_VALUE;
    }
}

From source file:org.wso2.andes.server.security.access.FirewallPluginTest.java

public void testSingleHostWilcardRule() throws Exception {
    RuleInfo rule = new RuleInfo();
    rule.setAccess("allow");
    String hostname = new InetSocketAddress("127.0.0.1", 0).getHostName();
    rule.setHostname(".*" + hostname.subSequence(hostname.length() - 1, hostname.length()) + "*");
    Firewall plugin = initialisePlugin("deny", new RuleInfo[] { rule });

    // Set IP so that we're connected from the right address
    _address = new InetSocketAddress("127.0.0.1", 65535);
    assertEquals(Result.ALLOWED, plugin.access(ObjectType.VIRTUALHOST, _address));
}