List of usage examples for java.text StringCharacterIterator next
public char next()
From source file:org.talend.dataprep.transformation.actions.math.ExtractNumber.java
/** * @param value the value to parse.//w w w . j ava2 s . c o m * @param defaultValue the value to return when no number can be extracted * @return the number extracted out of the given value. */ protected static String extractNumber(String value, String defaultValue) { // easy case if (StringUtils.isEmpty(value)) { return defaultValue; } // Test if the input value is a valid number before removing any characters: if (NumericHelper.isBigDecimal(value)) { // If yes (no exception thrown), return the value as it, no change required: return String.valueOf(BigDecimalParser.toBigDecimal(value)); } StringCharacterIterator iter = new StringCharacterIterator(value); MetricPrefix metricPrefixBefore = null, metricPrefixAfter = null; boolean numberFound = false; // we build a new value including only number or separator as , or . StringBuilder reducedValue = new StringBuilder(value.length()); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { // we remove all non numeric characters but keep separators if (NumberUtils.isNumber(String.valueOf(c)) || SEPARATORS.contains(c)) { reducedValue.append(c); numberFound = true; } else { // we take the first metric prefix found before and after a number found if (metricPrefixBefore == null) { MetricPrefix found = METRICPREFIXES.get(String.valueOf(c)); if (found != null && !numberFound) { metricPrefixBefore = found; } } if (metricPrefixAfter == null) { MetricPrefix found = METRICPREFIXES.get(String.valueOf(c)); if (found != null && numberFound) { metricPrefixAfter = found; } } } } if (!NumericHelper.isBigDecimal(reducedValue.toString())) { return defaultValue; } BigDecimal bigDecimal = BigDecimalParser.toBigDecimal(reducedValue.toString()); if (metricPrefixBefore != null || metricPrefixAfter != null) { // the metrix found after use first MetricPrefix metricPrefix = metricPrefixAfter != null ? metricPrefixAfter : metricPrefixBefore; bigDecimal = bigDecimal.multiply(metricPrefix.getMultiply()); } DecimalFormat decimalFormat = new DecimalFormat("0.#"); decimalFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS_DISPLAY); return decimalFormat.format(bigDecimal.stripTrailingZeros()); }
From source file:org.wso2.carbon.bpel.ui.BpelUIUtil.java
public static String encodeHTML(String aText) { final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(aText); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else if (character == '&') { result.append("&"); // } else if (character == '\"') { // result.append("""); // } else if (character == '\t') { // addCharEntity(9, result); // } else if (character == '!') { // addCharEntity(33, result); // } else if (character == '#') { // addCharEntity(35, result); // } else if (character == '$') { // addCharEntity(36, result); // } else if (character == '%') { // addCharEntity(37, result); // } else if (character == '\'') { // addCharEntity(39, result); // } else if (character == '(') { // addCharEntity(40, result); // } else if (character == ')') { // addCharEntity(41, result); // } else if (character == '*') { // addCharEntity(42, result); // } else if (character == '+') { // addCharEntity(43, result); // } else if (character == ',') { // addCharEntity(44, result); // } else if (character == '-') { // addCharEntity(45, result); // } else if (character == '.') { // addCharEntity(46, result); // } else if (character == '/') { // addCharEntity(47, result); // } else if (character == ':') { // addCharEntity(58, result); // } else if (character == ';') { // addCharEntity(59, result); // } else if (character == '=') { // addCharEntity(61, result); // } else if (character == '?') { // addCharEntity(63, result); // } else if (character == '@') { // addCharEntity(64, result); // } else if (character == '[') { // addCharEntity(91, result); // } else if (character == '\\') { // addCharEntity(92, result); // } else if (character == ']') { // addCharEntity(93, result); // } else if (character == '^') { // addCharEntity(94, result); // } else if (character == '_') { // addCharEntity(95, result); // } else if (character == '`') { // addCharEntity(96, result); // } else if (character == '{') { // addCharEntity(123, result); // } else if (character == '|') { // addCharEntity(124, result); // } else if (character == '}') { // addCharEntity(125, result); // } else if (character == '~') { // addCharEntity(126, result); } else {/*from w w w . java 2 s . c o m*/ //the char is not a special one //add it to the result as is result.append(character); } character = iterator.next(); } return result.toString(); }
From source file:org.wso2.carbon.ui.taglibs.Breadcrumb.java
/** * replaces backslash with forward slash * @param str/*from w w w . j ava 2 s . c o m*/ * @return */ private static String replaceBacklash(String str) { StringBuilder result = new StringBuilder(); StringCharacterIterator iterator = new StringCharacterIterator(str); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '\\') { result.append("/"); } else { result.append(character); } character = iterator.next(); } return result.toString(); }
From source file:stg.pr.engine.startstop.CStartEngine.java
/** * Command builder./* w w w .j av a 2 s . com*/ * * @param extraCommands * if any * @param args * Program arguments for PRE. * @return Command */ public String[] buildCommand(ArrayList<String> extraCommands, String[] args) { long lCurrentTimestamp = System.currentTimeMillis(); ArrayList<String> commandBuilder = new ArrayList<String>(); if (logger_.isEnabledFor(LogLevel.FINER)) { logger_.log(LogLevel.FINER, "Building command."); } commandBuilder.add(System.getProperty("java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "java"); if (CSettings.get("pr.javaruntimevmargs", null) != null) { // following code is to handle the space delimiter within double // quotes. Example value for this property // can be =-Xms128M -Xmx128M -D.pre.home="/u0201/apps/stg pre" StringCharacterIterator sci = new java.text.StringCharacterIterator( CSettings.get("pr.javaruntimevmargs")); boolean bEscapeCharacter = false; boolean bQuoted = false; StringBuffer cmdBuffer = new StringBuffer(); for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) { switch (c) { case '\\': if (bEscapeCharacter) { cmdBuffer.append(c + "" + c); bEscapeCharacter = false; } else { bEscapeCharacter = true; } break; case ' ': if (!bQuoted) { commandBuilder.add(cmdBuffer.toString()); cmdBuffer.delete(0, cmdBuffer.length()); } else { cmdBuffer.append(c); } bEscapeCharacter = false; break; case '"': if (!bEscapeCharacter) { if (!bQuoted) { bQuoted = true; } else { bQuoted = false; } } bEscapeCharacter = false; break; default: cmdBuffer.append(c); break; } // end of switch case. } // end for string character iterator if (cmdBuffer.length() > 0) { commandBuilder.add(cmdBuffer.toString()); } } // pr.javaruntimevmarg != null if (extraCommands != null) { if (logger_.isEnabledFor(LogLevel.FINEST)) { logger_.log(LogLevel.FINEST, "Adding extra commands if any."); } commandBuilder.addAll(extraCommands); } if (logger_.isEnabledFor(LogLevel.FINER)) { logger_.log(LogLevel.FINER, "Adding constants."); } commandBuilder.add("-classpath"); String classpath = ""; // if (CSettings.get("pr.reportService","OFF").equalsIgnoreCase("ON")) { // File directory = new File(CSettings.get("pr.birt.home") + "/lib"); // if (!directory.exists()) { // throw new IllegalArgumentException("Directory is non-existant. Check property birt.home " + CSettings.getInstance().getSource("pr").getConfigFile().getAbsolutePath()); // } // ArrayList<String> list = new ArrayList<String>(); // list.add(".jar"); // list.add(".zip"); // classpath = getExtraClasspath(directory, list); // System.out.println(classpath); // } if (CSettings.get("pr.javaextraclasspath", null) != null) { commandBuilder.add(System.getProperty("java.class.path") + File.pathSeparatorChar + CSettings.get("pr.javaextraclasspath") + File.pathSeparatorChar + classpath); } else { commandBuilder.add(System.getProperty("java.class.path") + File.pathSeparatorChar + classpath); } commandBuilder.add("stg.pr.engine.CProcessRequestEngine"); commandBuilder.add(args[0]); commandBuilder.add(args[1]); String[] cmd = new String[commandBuilder.size()]; commandBuilder.toArray(cmd); if (logger_.isEnabledFor(LogLevel.FINEST)) { logger_.log(LogLevel.FINEST, "Command " + commandBuilder); logger_.log(LogLevel.FINEST, "Elapsed Time taken to build command " + (System.currentTimeMillis() - lCurrentTimestamp) + " ms."); } return cmd; }
From source file:stg.utils.StringUtils.java
/** * Joins the given tokens and forms the string by appropriately adding the sepeartorChar and the escapeChar wherever necessary. * //from w w w . j av a 2s .co m * Returns null if array is null or empty string if the array is empty. * * @param array Tokens * @param separatorChar delimiter character * @param escapeChar escape character * @return String */ public static String join(String[] array, char separatorChar, char escapeChar) { if (array == null) { return null; } StringBuilder sb = new StringBuilder(); for (String str : array) { StringCharacterIterator iter = new StringCharacterIterator(str); char c = iter.first(); while (c != CharacterIterator.DONE) { if (c == separatorChar) { if (escapeChar != CharacterIterator.DONE) { sb.append(escapeChar); } } sb.append(c); c = iter.next(); } sb.append(separatorChar); } if (sb.length() > 0) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
From source file:stg.utils.StringUtils.java
/** * Private method that performs split and returns the tokens in the form of List. * /*from ww w . j ava 2 s. c o m*/ * @param text String to be tokenized. * @param separatorChar used as a delimiter. * @param escapeChar used to escape delimiter. * @return List. */ private static List<String> psplit(String text, char separatorChar, char escapeChar) { if (text == null) { return new ArrayList<String>(); } if (separatorChar == escapeChar) { throw new IllegalArgumentException("Separator Char and Escape Char must be different."); } StringCharacterIterator iter = new StringCharacterIterator(text); char c = iter.first(); ArrayList<String> list = new ArrayList<String>(); StringBuilder sb = new StringBuilder(); while (c != CharacterIterator.DONE) { if (c == escapeChar) { c = iter.next(); if (c != CharacterIterator.DONE) { sb.append(c); } else { throw new IllegalArgumentException( "Escape Character found without the character to be ignored."); } } else if (c == separatorChar) { list.add(sb.toString()); sb.delete(0, sb.length()); } else { sb.append(c); } c = iter.next(); } if (sb.length() > -1) { list.add(sb.toString()); } return list; }
From source file:stg.utils.StringUtils.java
/** * Private method to extract the tokens as list. * @param text//from w w w .j a va 2s. co m * @param separatorCharStart * @param separatorCharEnd * @param escapeChar * @param includeSeparators * @return list of strings */ private static List<String> pExtract(String text, final char separatorCharStart, final char separatorCharEnd, final char escapeChar, final boolean includeSeparators) { if (text == null) { return new ArrayList<String>(); } if (separatorCharStart == separatorCharEnd) { return psplit(text, separatorCharStart, escapeChar); } boolean escapeAndSeparatorCharAreSame = (separatorCharStart == escapeChar); StringCharacterIterator iter = new StringCharacterIterator(text); char c = iter.first(); ArrayList<String> list = new ArrayList<String>(); StringBuilder sb = new StringBuilder(); boolean toBeAppended = false; while (c != CharacterIterator.DONE) { if (c == escapeChar && !escapeAndSeparatorCharAreSame) { c = iter.next(); if (c == CharacterIterator.DONE) { throw new IllegalArgumentException( "Escape Character found without the character to be ignored."); } } else if (c == separatorCharEnd) { if (toBeAppended) { if (includeSeparators) { sb.append(c); } list.add(sb.toString()); sb.delete(0, sb.length()); toBeAppended = false; } } else if (c == separatorCharStart) { c = iter.next(); if (c == CharacterIterator.DONE) { throw new IllegalArgumentException( "Separator Character Start found without any enclosing text."); } toBeAppended = true; if (c == separatorCharStart && escapeAndSeparatorCharAreSame) { toBeAppended = false; c = iter.next(); } if (toBeAppended && includeSeparators) { sb.append(separatorCharStart); } } if (toBeAppended) { sb.append(c); } c = iter.next(); } return list; }
From source file:uk.ac.ebi.intact.dataexchange.psimi.xml.converter.util.PsiConverterUtils.java
public static String forXML(String aText) { if (aText == null) { return null; }/*from ww w . j a va 2 s .c o m*/ final StringBuilder result = new StringBuilder(); final StringCharacterIterator iterator = new StringCharacterIterator(aText); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else if (character == '\"') { result.append("""); } else if (character == '\'') { result.append("'"); } else if (character == '&') { result.append("&"); } else if (!XMLChar.isInvalid(character)) { //the char is not a special one //add it to the result as is result.append(character); } character = iterator.next(); } return result.toString(); }