List of usage examples for java.text StringCharacterIterator StringCharacterIterator
public StringCharacterIterator(String text)
From source file:com.entertailion.android.slideshow.utils.Utils.java
/** * Escape XML entities// w w w.j av a 2 s .c om * * @param aText * @return */ public static final String escapeXML(String aText) { if (null == aText) { return ""; } 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 { // 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:com.woonoz.proxy.servlet.UrlRewriterImpl.java
private static String removeLeadingSlashes(final String text) { if (text.isEmpty()) { return text; }/*from ww w . j a v a 2 s. c om*/ final CharacterIterator it = new StringCharacterIterator(text); Character c = it.first(); while (c.equals('/')) { c = it.next(); } return text.substring(it.getIndex()); }
From source file:com.jwebmp.core.utilities.EscapeChars.java
/** * Replace characters having special meaning in regular expressions with their escaped equivalents, preceded by a '\' character. * * @param aRegexFragment/*from www . ja va2s . co m*/ * * @return */ public static String forRegex(String aRegexFragment) { StringBuilder result = new StringBuilder(); StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (EscapeChars.EscapeRegexCharset.contains(character)) { result.append('\\' + character); } else { result.append(character); } character = iterator.next(); } return result.toString(); }
From source file:org.talend.dataprep.transformation.actions.math.ExtractNumber.java
/** * @param value the value to parse.//from www . j av a 2s . 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:EscapeHTML.java
/** * Return <tt>aText</tt> with all start-of-tag and end-of-tag characters * replaced by their escaped equivalents. * * <P>If user input may contain tags which must be disabled, then call * this method, not {@link #forHTMLTag}. This method is used for text appearing * <em>outside</em> of a tag, while {@link #forHTMLTag} is used for text appearing * <em>inside</em> an HTML tag. * * <P>It is not uncommon to see text on a web page presented erroneously, because * <em>all</em> special characters are escaped (as in {@link #forHTMLTag}), instead of * just the start-of-tag and end-of-tag characters. In * particular, the ampersand character is often escaped not once but <em>twice</em> : * once when the original input occurs, and then a second time when the same item is * retrieved from the database. This occurs because the ampersand is the only escaped * character which appears in a character entity. */// ww w . j a v a2 s . c om public String escapeDisableTags(String aText) { final StringBuffer result = new StringBuffer(); final StringCharacterIterator iterator = new StringCharacterIterator(aText); char character = iterator.current(); while (character != CharacterIterator.DONE) { if (character == '<') { result.append("<"); } else if (character == '>') { result.append(">"); } else { //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:UrlEncoder.java
/** * {@inheritDoc}//from w ww.j a v a 2s . co m */ public String decode(String encodedText) { if (encodedText == null) return null; if (encodedText.length() == 0) return encodedText; final StringBuilder result = new StringBuilder(); final CharacterIterator iter = new StringCharacterIterator(encodedText); for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) { if (c == ESCAPE_CHARACTER) { boolean foundEscapedCharacter = false; // Found the first character in a potential escape sequence, so grab the next two characters ... char hexChar1 = iter.next(); char hexChar2 = hexChar1 != CharacterIterator.DONE ? iter.next() : CharacterIterator.DONE; if (hexChar2 != CharacterIterator.DONE) { // We found two more characters, but ensure they form a valid hexadecimal number ... int hexNum1 = Character.digit(hexChar1, 16); int hexNum2 = Character.digit(hexChar2, 16); if (hexNum1 > -1 && hexNum2 > -1) { foundEscapedCharacter = true; result.append((char) (hexNum1 * 16 + hexNum2)); } } if (!foundEscapedCharacter) { result.append(c); if (hexChar1 != CharacterIterator.DONE) result.append(hexChar1); if (hexChar2 != CharacterIterator.DONE) result.append(hexChar2); } } else { result.append(c); } } return result.toString(); }
From source file:org.apache.maven.plugin.cxx.utils.svn.SvnService.java
public static SvnExternalsEntries loadSvnExternals(File basedir, Credential cred, String uri, Log log) throws MojoExecutionException { SvnExternalsEntries properties = new SvnExternalsEntries(); ByteArrayOutputStream out = new ByteArrayOutputStream(); SvnService.execSvnCommand(basedir, cred, new String[] { "propget", "svn:externals", uri }, out, log); BufferedReader in = new BufferedReader(new StringReader(out.toString())); String line = null;// ww w. j a v a 2s . c o m try { /* old svn 1.4 : third-party/sounds http://svn.example.com/repos/sounds third-party/skins -r148 http://svn.example.com/skinproj third-party/skins/toolkit -r21 http://svn.example.com/skin-maker */ /* svn 1.5 and above (operative rev): http://svn.example.com/repos/sounds third-party/sounds -r148 http://svn.example.com/skinproj third-party/skins -r21 http://svn.example.com/skin-maker third-party/skins/toolkit svn 1.5 and above (PEG rev): http://svn.example.com/repos/sounds third-party/sounds http://svn.example.com/skinproj@148 third-party/skins http://svn.example.com/skin-maker@21 third-party/skins/toolkit */ // this parser only support svn:externals syntaxe 1.5 and above // [revision] origin target_dir while (null != (line = in.readLine())) { StringCharacterIterator iter = new StringCharacterIterator(line); SvnExternalsTokenizer m = new SvnExternalsTokenizer(iter); SvnExternalEntry external = new SvnExternalEntry(); SvnExternalsTokenizer.Token tok = m.nextToken(); if (SvnExternalsTokenizer.TokenType.revision == tok.tokenType) { external.revision = tok.value.toString(); tok = m.nextToken(); external.origin = SvnExternalsTokenizer.TokenType.libelle == tok.tokenType ? tok.value.toString() : null; tok = m.nextToken(); external.targetDir = SvnExternalsTokenizer.TokenType.libelle == tok.tokenType ? tok.value.toString() : null; } else if (SvnExternalsTokenizer.TokenType.libelle == tok.tokenType) { external.origin = tok.value.toString(); tok = m.nextToken(); external.targetDir = SvnExternalsTokenizer.TokenType.libelle == tok.tokenType ? tok.value.toString() : null; } else if (SvnExternalsTokenizer.TokenType.comment == tok.tokenType) { external.comment = tok.value.toString(); } else if (SvnExternalsTokenizer.TokenType.empty == tok.tokenType) { // ignore empty lines continue; } if (external.isValide()) { properties.put(external); } else { log.warn("unrecognized svn:externals entry of " + uri + " line content : '" + line + "'"); } } } catch (Exception e) { log.warn("Failed to parse svn:externals of " + uri + " : " + e); } return properties; }
From source file:org.gradle.api.internal.plugins.StartScriptTemplateBindingFactory.java
private String escapeWindowsJvmOpt(String jvmOpts) { boolean wasOnBackslash = false; StringBuilder escapedJvmOpt = new StringBuilder(); CharacterIterator it = new StringCharacterIterator(jvmOpts); //argument quoting: // - " must be encoded as \" // - % must be encoded as %% // - pathological case: \" must be encoded as \\\", but other than that, \ MUST NOT be quoted // - other characters (including ') will not be quoted // - use a state machine rather than regexps for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) { String repl = Character.toString(ch); if (ch == '%') { repl = "%%"; } else if (ch == '"') { repl = (wasOnBackslash ? '\\' : "") + "\\\""; }/* ww w.java 2 s. c o m*/ wasOnBackslash = ch == '\\'; escapedJvmOpt.append(repl); } return escapedJvmOpt.toString(); }
From source file:Characters.java
/** * Removes all characters that are not considered <a href="http://www.w3.org/TR/2000/REC-xml-20001006#charsets">XML * characters </a> from the input. * /*w w w . jav a 2 s.c o m*/ * @param input the input to filter * @return the input with all non-XML characters removed */ public static String filterXML(String input) { // Null inputs return an empty string. if (input == null) { return ""; } // trim() removes all whitespace, not only spaces. String _input = input.trim(); // Empty inputs return an empty string. if (_input.equals((""))) { return ""; } // This StringBuilder will hold the result. StringBuilder result = new StringBuilder(_input.length()); // This StringCharacterIterator will iterate over the input. StringCharacterIterator iterator = new StringCharacterIterator(_input); // Iterate over the input. for (char aChar = iterator.first(); aChar != CharacterIterator.DONE; aChar = iterator.next()) { // Determine if this is a valid XML Character. if ((aChar == '\u0009') || (aChar == '\n') || (aChar == '\r') || (('\u0020' <= aChar) && (aChar <= '\uD7FF')) || (('\uE000' <= aChar) && (aChar <= '\uFFFD'))) { result.append(aChar); } } if (result.length() > _input.length()) { return result.toString(); } // (otherwise...) return _input; }
From source file:net.issarlk.androbunny.inkbunny.API.java
public static String extractTree(Pattern pattern, String data) { Matcher matches = pattern.matcher(data); if (matches.matches()) { data = matches.group(1);/* www .j a v a 2 s . c o m*/ StringBuilder result = new StringBuilder(); //Extract int taglevel = 0; CharacterIterator it = new StringCharacterIterator(data); int mode = 0; int inparamname = 0; int inparamvalue = 0; int outsidetags = 0; int intagname = 0; char paramvaluechar = '"'; HashMap<String, String> params = new HashMap<String, String>(); StringBuilder tagname = new StringBuilder(); StringBuilder paramname = new StringBuilder(); StringBuilder paramvalue = new StringBuilder(); StringBuilder text = new StringBuilder(); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { if (c == '<') { //start of tag c = it.next(); params.clear(); outsidetags = 0; //Output text //Log.d(TAG, repeat(' ', taglevel) + text.toString()); result.append(text); if (c == '/') { //Start of closing tag mode = 3; intagname = 1; c = it.next(); } else { //Start of openning tag mode = 2; intagname = 1; } tagname = new StringBuilder(); } if (intagname == 1) { if (c < 'a' || c > 'z') { intagname = 0; } else { tagname.append(c); } } if ((mode == 2 || mode == 3) && intagname == 0 && inparamname == 0 && inparamvalue == 0 && c >= 'a' && c <= 'z') { inparamname = 1; paramname = new StringBuilder(); } if (inparamname == 1) { if (c == '=') { inparamname = 0; inparamvalue = 1; paramvaluechar = it.next(); paramvalue = new StringBuilder(); continue; } else if (c != ' ') { paramname.append(c); } } if (inparamvalue == 1) { if (c == paramvaluechar) { //End of value inparamvalue = 0; params.put(paramname.toString(), paramvalue.toString()); } else { paramvalue.append(c); } } if (mode == 2 && inparamvalue == 0 && c == '/') { c = it.next(); if (c == '>') { //tag closed at its end mode = 4; if (tagname.toString().equals("br")) { result.append("<br/>"); } } else { c = it.previous(); } } if (c == '>') { if (mode == 2) { //openning taglevel += 1; outsidetags = 1; text = new StringBuilder(); String paramstr = ""; for (String key : params.keySet()) { paramstr += " " + key + "=\"" + params.get(key) + "\""; } //Log.d(TAG, repeat(' ', taglevel - 1) + "<" + tagname.toString() + paramstr + ">"); result.append("<" + tagname.toString() + paramstr + ">"); mode = 0; continue; } else if (mode == 3) { //Closing taglevel -= 1; outsidetags = 1; text = new StringBuilder(); //Log.d(TAG, repeat(' ', taglevel) + "</" + tagname.toString() + ">"); result.append("</" + tagname.toString() + ">\n"); mode = 0; if (taglevel <= 0) { break; } continue; } else if (mode == 4) { //closed at end outsidetags = 1; text = new StringBuilder(); //Log.d(TAG, repeat(' ', taglevel) + "<" + tagname.toString() + "/>"); result.append("<" + tagname.toString() + "/>\n"); mode = 0; continue; } } if (outsidetags == 1) { //Append char text.append(c); } } String res = result.toString(); return res; } return null; }