List of usage examples for org.apache.commons.lang StringUtils splitPreserveAllTokens
public static String[] splitPreserveAllTokens(String str, String separatorChars)
Splits the provided text into an array, separators specified, preserving all tokens, including empty tokens created by adjacent separators.
From source file:org.openmrs.module.pihmalawi.sql.MysqlRunner.java
/** * Executes a Sql Script/*www. j a v a2 s . c o m*/ */ public static MysqlResult executeSql(String sql, Map<String, Object> parameterValues) { log.info("Executing SQL..."); File toExecute = null; try { // Writing SQL to temporary file for execution toExecute = File.createTempFile("mysqlrunner", ".sql"); StringBuilder sqlToWrite = new StringBuilder(); if (parameterValues != null) { for (String paramName : parameterValues.keySet()) { Object paramValue = parameterValues.get(paramName); sqlToWrite.append("set @").append(paramName); sqlToWrite.append("=").append(getParameterAssignmentString(paramValue)).append(";"); sqlToWrite.append(System.getProperty("line.separator")); } } sqlToWrite.append(sql); FileUtils.writeStringToFile(toExecute, sqlToWrite.toString()); log.debug("Wrote SQL file for execution: " + toExecute.getAbsolutePath()); log.debug("Contents:\n" + sqlToWrite); // Constructing command line elements to execute List<String> commands = new ArrayList<String>(); commands.add("mysql"); commands.add("-u" + Context.getRuntimeProperties().getProperty("connection.username")); commands.add("-p" + Context.getRuntimeProperties().getProperty("connection.password")); commands.add("-esource " + toExecute.getAbsolutePath()); commands.add(DatabaseUpdater.getConnection().getCatalog()); // Database Name log.debug("Constructed command to execute: \n" + OpenmrsUtil.join(commands, " ")); Process process = Runtime.getRuntime().exec(commands.toArray(new String[] {})); MysqlResult result = new MysqlResult(); LineIterator successIterator = null; try { successIterator = IOUtils.lineIterator(process.getInputStream(), "UTF-8"); while (successIterator.hasNext()) { String line = successIterator.nextLine(); String[] elements = StringUtils.splitPreserveAllTokens(line, '\t'); if (result.getColumns().isEmpty()) { result.setColumns(Arrays.asList(elements)); } else { Map<String, String> row = new LinkedHashMap<String, String>(); for (int i = 0; i < result.getColumns().size(); i++) { String value = elements[i].trim(); if ("NULL".equals(value)) { value = null; } row.put(result.getColumns().get(i), value); } result.getData().add(row); } } } finally { successIterator.close(); } LineIterator errorIterator = null; try { errorIterator = IOUtils.lineIterator(process.getErrorStream(), "UTF-8"); while (errorIterator.hasNext()) { String line = errorIterator.nextLine(); if (!line.toLowerCase().startsWith("warning")) { result.getErrors().add(line); } } } finally { errorIterator.close(); } return result; } catch (Exception e) { throw new RuntimeException("An error occurred while executing a SQL file", e); } finally { FileUtils.deleteQuietly(toExecute); } }
From source file:org.sakaiproject.component.section.sakai.CourseSectionImpl.java
private boolean getIndexedBooleanProperty(int index, String complexString) { String[] sa = StringUtils.splitPreserveAllTokens(complexString, CourseSectionImpl.SEP_CHARACTER); if (sa == null) { return false; }//from w w w .j a v a2 s. c o m if (index >= sa.length) { log.debug("Can not get " + index + " index from string " + complexString); return false; } return Boolean.parseBoolean(sa[index]); }
From source file:org.sakaiproject.component.section.sakai.CourseSectionImpl.java
private String getIndexedStringProperty(int index, String complexString) { if (complexString == null || "".equals(complexString.trim())) { return null; }/* ww w . j ava 2 s . co m*/ String[] sa = StringUtils.splitPreserveAllTokens(complexString, CourseSectionImpl.SEP_CHARACTER); if (index >= sa.length) { log.warn("Can not get " + index + " index from string " + complexString); return null; } return sa[index]; }
From source file:org.sakaiproject.content.metadata.model.DurationMetadataType.java
@Override public MetadataConverter<Duration> getConverter() { return new MetadataConverter<Duration>() { @Override// w w w . j ava 2 s . c o m public String toString(Duration metaValue) { if (metaValue != null) { StringBuilder builder = new StringBuilder(); builder.append(nullToEmpty(metaValue.getFirstCount())); builder.append("-"); builder.append(nullToEmpty(metaValue.getFirstUnit())); builder.append("-"); builder.append(nullToEmpty(metaValue.getSecondCount())); builder.append("-"); builder.append(nullToEmpty(metaValue.getSecondUnit())); return builder.toString(); } return null; } @Override public Duration fromString(String stringValue) { if (stringValue != null && !stringValue.isEmpty()) { String[] values = StringUtils.splitPreserveAllTokens(stringValue, "-"); if (values.length == 4) { Duration duration = new Duration(); duration.setFirstCount(NumberUtils.toInt(values[0])); duration.setFirstUnit(Duration.Unit.parse(values[1])); duration.setSecondCount(NumberUtils.toInt(values[2])); duration.setSecondUnit(Duration.Unit.parse(values[3])); return duration; } } return null; } public Map<String, ?> toProperties(Duration metadataValue) { String stringValue = toString(metadataValue); return Collections.singletonMap(getUniqueName(), (stringValue == null || metadataValue.isEmpty()) ? null : stringValue); } public Duration fromProperties(Map<String, ?> properties) { return fromString((String) properties.get(getUniqueName())); } public Duration fromHttpForm(Map<String, ?> parameters, String parameterSuffix) { Duration duration = new Duration(); duration.setFirstCount( toInteger(parameters.get(getUniqueName() + "_first_count" + parameterSuffix))); duration.setFirstUnit( Duration.Unit.parse(parameters.get(getUniqueName() + "_first_unit" + parameterSuffix))); duration.setSecondCount( toInteger(parameters.get(getUniqueName() + "_second_count" + parameterSuffix))); duration.setSecondUnit( Duration.Unit.parse(parameters.get(getUniqueName() + "_second_unit" + parameterSuffix))); return duration; } }; }
From source file:org.sonar.batch.issue.ignore.pattern.PatternDecoder.java
/** * Main method that decodes a line which defines a pattern *//*from w ww. j a v a 2s . c o m*/ public IssuePattern decodeLine(String line) { if (isBlankOrComment(line)) { return null; } String[] fields = StringUtils.splitPreserveAllTokens(line, ';'); if (fields.length > THREE_FIELDS_PER_LINE) { throw new SonarException(CONFIG_FORMAT_ERROR_PREFIX + "The following line has more than 3 fields separated by comma: " + line); } IssuePattern pattern; if (fields.length == THREE_FIELDS_PER_LINE) { checkRegularLineConstraints(line, fields); pattern = new IssuePattern(StringUtils.trim(fields[0]), StringUtils.trim(fields[1])); decodeRangeOfLines(pattern, fields[2]); } else if (fields.length == 2) { checkDoubleRegexpLineConstraints(line, fields); pattern = new IssuePattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]); } else { checkWholeFileRegexp(fields[0]); pattern = new IssuePattern().setAllFileRegexp(fields[0]); } return pattern; }
From source file:org.sonar.ide.eclipse.core.internal.remote.SimpleSourceCodeDiffEngine.java
public static String[] split(String text) { return StringUtils.splitPreserveAllTokens(text, '\n'); }
From source file:org.sonar.plugins.switchoffviolations.pattern.PatternDecoder.java
/** * Main method that decodes a line which defines a pattern *//*from www . j av a 2s . c om*/ public Pattern decodeLine(String line) { if (isBlankOrComment(line)) { return null; } String[] fields = StringUtils.splitPreserveAllTokens(line, ';'); if (fields.length > THREE_FIELDS_PER_LINE) { throw new SonarException( "Invalid format. The following line has more than 3 fields separated by comma: " + line); } Pattern pattern; if (fields.length == THREE_FIELDS_PER_LINE) { checkRegularLineConstraints(line, fields); pattern = new Pattern(StringUtils.trim(fields[0]), StringUtils.trim(fields[1])); decodeRangeOfLines(pattern, fields[2]); } else if (fields.length == 2) { checkDoubleRegexpLineConstraints(line, fields); pattern = new Pattern().setBeginBlockRegexp(fields[0]).setEndBlockRegexp(fields[1]); } else { pattern = new Pattern().setAllFileRegexp(fields[0]); } return pattern; }
From source file:org.sonar.xoo.scm.XooBlameCommand.java
@VisibleForTesting protected void processFile(InputFile inputFile, BlameOutput result) { File ioFile = inputFile.file(); File scmDataFile = new java.io.File(ioFile.getParentFile(), ioFile.getName() + SCM_EXTENSION); if (!scmDataFile.exists()) { return;/*from w ww .j a v a2 s . co m*/ } try { List<String> lines = FileUtils.readLines(scmDataFile, StandardCharsets.UTF_8); List<BlameLine> blame = new ArrayList<>(lines.size()); int lineNumber = 0; for (String line : lines) { lineNumber++; if (StringUtils.isNotBlank(line)) { // revision,author,dateTime String[] fields = StringUtils.splitPreserveAllTokens(line, ','); if (fields.length < 3) { throw new IllegalStateException("Not enough fields on line " + lineNumber); } String revision = StringUtils.trimToNull(fields[0]); String author = StringUtils.trimToNull(fields[1]); BlameLine blameLine = new BlameLine().revision(revision).author(author); String dateStr = StringUtils.trimToNull(fields[2]); if (dateStr != null) { Date dateTime = DateUtils.parseDateTimeQuietly(dateStr); if (dateTime != null) { blameLine.date(dateTime); } else { // Will throw an exception, when date is not in format "yyyy-MM-dd" blameLine.date(DateUtils.parseDate(dateStr)); } } blame.add(blameLine); } } result.blameResult(inputFile, blame); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:org.talend.dataquality.record.linkage.analyzer.StringsClusterAnalyzerTest.java
@Test public void testGetResult() throws IOException { analyser.init();//from w ww.j a v a2 s.c om String columnDelimiter = "|"; InputStream in = this.getClass().getResourceAsStream("incoming_customers_swoosh_fingerprintkey.txt"); //$NON-NLS-1$ BufferedReader bfr = new BufferedReader(new InputStreamReader(in)); List<String> listOfLines = IOUtils.readLines(bfr); for (String line : listOfLines) { String[] fields = StringUtils.splitPreserveAllTokens(line, columnDelimiter); analyser.analyze(fields[1]); } analyser.end(); List<StringClusters> results = analyser.getResult(); assertElementResult(results.get(0)); }
From source file:org.talend.dataquality.record.linkage.analyzer.StringsClusterAnalyzerTest.java
@Test public void testCluster10000() throws IOException { analyser.init();//from www .ja v a2s. c om String columnDelimiter = "|"; InputStream in = this.getClass().getResourceAsStream("cluster10000.txt"); //$NON-NLS-1$ BufferedReader bfr = new BufferedReader(new InputStreamReader(in)); List<String> listOfLines = IOUtils.readLines(bfr); for (String line : listOfLines) { String[] fields = StringUtils.splitPreserveAllTokens(line, columnDelimiter); analyser.analyze(fields[0]); } analyser.end(); List<StringClusters> results = analyser.getResult(); assertElementResult(results.get(0)); }