List of usage examples for java.lang Character isWhitespace
public static boolean isWhitespace(int codePoint)
From source file:Main.java
/** * Indent the supplied XML string by the number of spaces specified in the * 'indent' param./*from www . ja va 2s .com*/ * <p/> * The indents are only inserted after newlines, where the first non-whitespace character * is '<'. * * @param xml The XML to indent. * @param indent The number of spaces to insert as the indent. * @return The indented XML string. */ public static String indent(String xml, int indent) { StringBuffer indentedXml = new StringBuffer(); int xmlLen = xml.length(); char[] indentChars = new char[indent]; Arrays.fill(indentChars, ' '); int i = 0; while (i < xmlLen) { if (isStartOf(xml, i, COMMENT_START)) { int commentEnd = xml.indexOf(COMMENT_END, i); indentedXml.append(xml, i, commentEnd); i = commentEnd; } else if (isStartOf(xml, i, CDATA_START)) { int cdataEnd = xml.indexOf(CDATA_END, i); indentedXml.append(xml, i, cdataEnd); i = cdataEnd; } else { char nextChar = xml.charAt(i); indentedXml.append(nextChar); if (nextChar == '\n') { // We're at the start of a new line. Need to determine // if the next sequence of non-whitespace characters are the start/end of // an XML element. If it is... add an indent before.... while (i < xmlLen) { i++; char preXmlChar = xml.charAt(i); if (!Character.isWhitespace(preXmlChar)) { if (preXmlChar == '<') { if (!isStartOf(xml, i, COMMENT_START) && !isStartOf(xml, i, CDATA_START)) { indentedXml.append(indentChars); } } break; } else { indentedXml.append(preXmlChar); } } } else { i++; } } } return indentedXml.toString(); }
From source file:de.micromata.genome.gwiki.utils.CommaListParser.java
/** * return behind the last nonwhitespace character. * * @param input the input/*from w ww . ja va 2s. c o m*/ * @param position the position * @return the int */ private static int seekNonWsBack(String input, int position) { for (int i = position; i >= 0; --i) { if (Character.isWhitespace(input.charAt(i)) == false) { return i + 1; } } return 0; }
From source file:Utils.java
/** * Truncate text on a whitespace boundary (near a specified length). The * length of the resultant string will be in the range:<br> * <code> (requested-length * .25) ~ (requested-length * 1.5) </code> * //from ww w . jav a 2 s . com * @param text * Text to truncate * @param length * Target length * @return Truncated text */ public static String truncateAtWhitespace(String text, int length) { int desired, lowerBound, upperBound; /* * Make sure we have a reasonable length to work with */ if (length < MINIMUM_SUPPORTED_LENGTH) { throw new IllegalArgumentException( "Requested length too short (must be " + MINIMUM_SUPPORTED_LENGTH + " or greated)"); } /* * No need to truncate - the original string "fits" */ if (text.length() <= length) { return text; } /* * Try to find whitespace befor the requested maximum */ lowerBound = length / 4; upperBound = length + (length / 2); for (int i = length - 1; i > lowerBound; i--) { if (Character.isWhitespace(text.charAt(i))) { return text.substring(0, i); } } /* * No whitespace - look beyond the desired maximum */ for (int i = (length); i < upperBound; i++) { if (Character.isWhitespace(text.charAt(i))) { return text.substring(0, i); } } /* * No whitespace, just truncate the text at the requested length */ return text.substring(0, length); }
From source file:Main.java
/** * /*from ww w . jav a 2 s . c o m*/ * @param caracter * @param pos * @param cadena * @return */ private static boolean esElCaracterElPrimerSucesorQueNoSeaBlanco(char caracter, int pos, String cadena) { if (pos == cadena.length()) return false; while (true) { char sucesor = cadena.charAt(pos); if (sucesor == caracter && cadena.charAt(pos - 1) != '\\') //para saltar los caracteres con \ return true; if (!Character.isWhitespace(sucesor) && sucesor != caracter) return false; if (pos == cadena.length()) return false; pos = pos + 1; } }
From source file:Main.java
/** * Retrieve the name of the first tag in the XML document specified by the * given Reader, without parsing the full file/string. This function is * useful to identify the DocType of an XML document before parsing, * possibly to send the document off to different pieces of code. * For performance reasons, the function attempts to read as little of * the file or string as possible before making its decision about the * first tag. Leading comments are ignored. * @param xml a Reader containing an XML document. * @return the first tag name, as a String, or null if no first tag * can be found./*from ww w . j a va2s.co m*/ */ public static String getFirstTagName(Reader xml) { final int OUTSIDE = 0; // constant: identify outside state final int BRACKET = 1; // constant: bracket, contents unknown final int COMMENT = 2; // constant: identify a comment section final int IGNORE = 3; // constant: identify an ignored section final int TAG = 4; // constant: identify a tag section int state = OUTSIDE; String commentMatch = null; StringBuffer tagBuffer = null; boolean sawBang = false; try { int c = xml.read(); for (;;) { // No tag found if we hit EOF first. if (c == -1) return null; switch (state) { case OUTSIDE: // Start of any sort of tag if (c == '<') { state = BRACKET; commentMatch = "!--"; sawBang = false; c = xml.read(); } // Other non-whitespace characters outside of any tag else if (!Character.isWhitespace((char) c)) return null; // Whitespace characters are ignored else c = xml.read(); break; case BRACKET: // Check for the start of a comment. if (commentMatch != null) { if (c == commentMatch.charAt(0)) { // This match indicates a comment if (commentMatch.length() == 1) { c = xml.read(); commentMatch = "-->"; state = COMMENT; } else { // Remove the first character from commentMatch, // then process the character as usual. commentMatch = commentMatch.substring(1, commentMatch.length()); } } else // No longer eligible for comment. commentMatch = null; } // Hit whitespace; ignore the character. if (Character.isWhitespace((char) c)) { c = xml.read(); break; } switch (c) { case '?': c = xml.read(); state = IGNORE; break; case '!': // Enter an ignored section unless eligible for comment. c = xml.read(); sawBang = true; if (commentMatch == null) state = IGNORE; break; case '-': // Enter an ignored section unless eligible for comment. c = xml.read(); if (commentMatch == null) state = IGNORE; break; case '>': // Return to OUTSIDE state immediately c = xml.read(); state = OUTSIDE; break; default: // State depends on whether we saw a ! or not. if (sawBang) state = IGNORE; else state = TAG; tagBuffer = new StringBuffer(); } break; case COMMENT: // Did we match the next expected end-of-comment character? if (c == commentMatch.charAt(0)) { c = xml.read(); if (commentMatch.length() == 1) // Done with the comment state = OUTSIDE; else commentMatch = commentMatch.substring(1, commentMatch.length()); } // If not, restart our quest for the end-of-comment character. else { c = xml.read(); commentMatch = "-->"; } break; case IGNORE: // Drop out on a close >. Ignore all other characters. if (c == '>') { c = xml.read(); state = OUTSIDE; } else c = xml.read(); break; case TAG: // Store characters in the tag buffer until we hit whitespace. // When we hit whitespace or '>' or '/', return the name of the tag. if (Character.isWhitespace((char) c) || c == '>' || c == '/') return tagBuffer.toString(); else { tagBuffer.append((char) c); c = xml.read(); } break; } } } catch (IOException ex) { // On exception, we can't determine the first tag, so return null. return null; } }
From source file:Main.java
/** * Parses a whitespace separated series of name tokens. * @param stringValue the full string/* w w w. j av a 2 s . c o m*/ * @return a list of each constituent value, or null * if there are no tokens (that is, the string is empty or * all whitespace) */ static public List<String> parseNameTokensAsList(String stringValue) { if (stringValue == null) return null; ArrayList<String> list = new ArrayList<String>(5); int length = stringValue.length(); boolean inSpace = true; int start = 0; for (int i = 0; i < length; i++) { char ch = stringValue.charAt(i); // We're in whitespace; if we've just departed // a run of non-whitespace, append a string. // Now, why do we use the supposedly deprecated "Character.isSpace()" // function instead of "isWhitespace"? We're following XML rules // here for the meaning of whitespace, which specifically // EXCLUDES general Unicode spaces. if (Character.isWhitespace(ch)) { if (!inSpace) { list.add(stringValue.substring(start, i)); inSpace = true; } } // We're out of whitespace; if we've just departed // a run of whitespace, start keeping track of this string else { if (inSpace) { start = i; inSpace = false; } } } if (!inSpace) list.add(stringValue.substring(start)); if (list.isEmpty()) return null; return list; }
From source file:Main.java
private static boolean isSpace(final String s) { if (s == null) return true; for (int i = 0, len = s.length(); i < len; ++i) { if (!Character.isWhitespace(s.charAt(i))) { return false; }/*from ww w . jav a2 s . c om*/ } return true; }
From source file:org.apache.archiva.redback.policy.rules.WhitespacePasswordRule.java
public void testPassword(PasswordRuleViolations violations, User user) { if (user.getPassword() != null) { char[] password = user.getPassword().toCharArray(); for (int i = 0; i < password.length; i++) { if (Character.isWhitespace(password[i])) { violations.addViolation(NO_WHITE_SPACE_VIOLATION); return; }//from w ww. java 2 s.co m } } }
From source file:Main.java
/** * <p>Checks if a String is whitespace, empty ("") or null.</p> * * <pre>/*from w w w . j a va 2 s . c om*/ * StringUtils.isBlank(null) = true * StringUtils.isBlank("") = true * StringUtils.isBlank(" ") = true * StringUtils.isBlank("bob") = false * StringUtils.isBlank(" bob ") = false * </pre> * * @param str the String to check, may be null * @return <code>true</code> if the String is null, empty or whitespace * @since 2.0 */ public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) { return false; } } return true; }
From source file:importer.filters.PoemFilter.java
boolean isEmpty(String line) { if (line.length() > 0) { for (int i = 0; i < line.length(); i++) if (!Character.isWhitespace(line.charAt(i))) return false; }//from www . j a v a 2s.com return true; }