List of usage examples for java.lang String subSequence
public CharSequence subSequence(int beginIndex, int endIndex)
From source file:com.github.helenusdriver.driver.tools.Tool.java
/** * Sets the root and helenus loggers log level. * * @author paouelle/*w w w. j av a 2 s .c om*/ * * @param level the level to which the root logger should be at */ private static void setRootLogLevel(Level level) { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); final String pkg = Tool.class.getPackage().getName(); for (final String name : new String[] { LogManager.ROOT_LOGGER_NAME, pkg.subSequence(0, pkg.lastIndexOf('.', pkg.lastIndexOf('.') - 1)).toString() }) { final LoggerConfig loggerConfig = config.getLoggerConfig(name); loggerConfig.setLevel(level); } // This causes all Loggers to re-fetch information from their LoggerConfig. ctx.updateLoggers(); }
From source file:org.kalypso.model.wspm.tuhh.core.profile.export.knauf.printer.AbstractKnaufPrinter.java
@Override public CharSequence println() { final StringBuilder builder = new StringBuilder(); builder.append(String.format("%2d", m_bean.getSatzart())); //$NON-NLS-1$ builder.append(getContent());// w w w . j a v a 2 s .com final String output = builder.toString(); if (StringUtils.length(output) > getMaxRowSize()) { final CharSequence cutted = output.subSequence(0, getMaxRowSize()); System.out.println("Knauf Export - too long output string has been detected - cutted:"); //$NON-NLS-1$ System.out.println(output); System.out.println(" -> "); //$NON-NLS-1$ System.out.println(cutted); return cutted; } return String.format("%s\r\n", output); //$NON-NLS-1$ }
From source file:de.javakaffee.web.msm.serializer.javolution.XMLBinding.java
static String replace(final String text, final char c, final String chars) { int lastPos = 0; StringBuilder sb = null;/* ww w . java 2 s . com*/ for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == c) { if (sb == null) { sb = new StringBuilder(); } sb.append(text.subSequence(lastPos, i)).append(chars); lastPos = i + 1; } } if (sb != null) { sb.append(text.subSequence(lastPos, text.length())); } return sb != null ? sb.toString() : text; }
From source file:br.com.repository.QueryDslSupportJpaImpl.java
@Override public String property(Path path) { String temp = path.toString(); int index = temp.indexOf("."); temp = temp.subSequence(index + 1, temp.length()).toString(); return temp;/*from w w w .ja v a2 s .c o m*/ }
From source file:org.hobbit.storage.queries.SparqlQueries.java
/** * Extends a SPARQL query by inserting specified extension string every time a * target string is found./* w w w. ja va 2s .c o m*/ * * @param query * The original query. * @param target * Target string to find. * @param extension * Extension string to insert. * @return the modified query or <code>null</code> if the query is invalid. */ private static final String extendQuery(String query, String target, String extension) { StringBuilder queryBuilder = new StringBuilder(); int pos = query.indexOf("WHERE"); if (pos < 0) { return null; } // Add everything before the WHERE queryBuilder.append(query.subSequence(0, pos)); int oldpos = pos; // For every selection triple, insert the extension in front of it pos = query.indexOf(target, oldpos); while (pos > 0) { queryBuilder.append(query.substring(oldpos, pos)); queryBuilder.append(extension); oldpos = pos; pos = query.indexOf(target, oldpos + target.length()); } queryBuilder.append(query.substring(oldpos)); return queryBuilder.toString(); }
From source file:tests.unit.vcl.file.reader.FileReaderTest.java
/** * Test <code>FileReader</code> with a txt file * /* w ww. ja v a 2 s. co m*/ * @see it.greenvulcano.gvesb.virtual.file.reader.FileReader * * @throws Exception * if any error occurs */ @SuppressWarnings("deprecation") public void testReadFile() throws Exception { String content = "12345678"; FileUtils.write(new File(TEST_FILE_DEST_RESOURCES + File.separator + TEST_FILE_READER), content.subSequence(0, content.length())); Node node = XMLConfig.getNode("GVSystems.xml", "//filereader-call[@name='test_read_file']"); FileReader fr = new FileReader(); fr.init(node); GVBuffer gvBuffer = new GVBuffer("TEST", "FILEREADER-CALL"); fr.perform(gvBuffer); byte[] data = (byte[]) gvBuffer.getObject(); assertEquals(content, new String(data)); assertTrue("Resource " + TEST_FILE_READER + " not found in " + TEST_FILE_DEST_RESOURCES, new File(TEST_FILE_DEST_RESOURCES + File.separator + TEST_FILE_READER).exists()); }
From source file:tests.unit.vcl.file.reader.FileReaderTest.java
/** * Test <code>FileReader</code> with a xml file. * /*from w ww. ja va 2s .c o m*/ * @see it.greenvulcano.gvesb.virtual.file.reader.FileReader * * @throws Exception * if any error occurs */ @SuppressWarnings("deprecation") public void testReadFileXML() throws Exception { String content = "<tag>ciao</tag>"; FileUtils.write(new File(TEST_FILE_DEST_RESOURCES + File.separator + TEST_FILE_READER_XML), content.subSequence(0, content.length())); Node node = XMLConfig.getNode("GVSystems.xml", "//filereader-call[@name='test_read_file_xml']"); FileReader fr = new FileReader(); fr.init(node); GVBuffer gvBuffer = new GVBuffer("TEST", "FILEREADER-CALL"); fr.perform(gvBuffer); assertEquals("ciao", Document.class.cast(gvBuffer.getObject()).getDocumentElement().getTextContent()); assertTrue("Resource " + TEST_FILE_READER_XML + " not found in " + TEST_FILE_DEST_RESOURCES, new File(TEST_FILE_DEST_RESOURCES + File.separator + TEST_FILE_READER_XML).exists()); }
From source file:com.cloud.network.nicira.NiciraRestClient.java
private String responseToErrorMessage(final CloseableHttpResponse response) { String errorMessage = response.getStatusLine().toString(); if (response.containsHeader(CONTENT_TYPE) && TEXT_HTML_CONTENT_TYPE.equals(response.getFirstHeader(CONTENT_TYPE).getValue())) { try {/*from ww w . j a v a2s . c o m*/ final HttpEntity entity = response.getEntity(); final String respobnseBody = EntityUtils.toString(entity); errorMessage = respobnseBody.subSequence(0, maxResponseErrorMesageLength).toString(); } catch (final IOException e) { s_logger.debug("Could not read repsonse body. Response: " + response, e); } } return errorMessage; }
From source file:consultor.CSVLoader.java
/** * Parse CSV file using OpenCSV library and load in * given database table. //from w w w.j a v a2 s . c o m * @param csvFile Input CSV file * @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; Date date = null; while ((nextLine = csvReader.readNext()) != null) { if (null != nextLine) { int index = 1; for (String string : nextLine) { date = DateUtil.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:quiz.CsvLoader.java
/** * Parse CSV file using OpenCSV library and load in * given database table. /*w ww. j a v a 2 s . c om*/ * @param csvFile Input CSV file * @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; Date date = null; while ((nextLine = csvReader.readNext()) != null) { if (null != nextLine) { int index = 1; for (String string : nextLine) { date = DateUtils.stringToDate(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(); } }