List of usage examples for org.apache.commons.lang3 CharUtils isAscii
public static boolean isAscii(final char ch)
Checks whether the character is ASCII 7 bit.
CharUtils.isAscii('a') = true CharUtils.isAscii('A') = true CharUtils.isAscii('3') = true CharUtils.isAscii('-') = true CharUtils.isAscii('\n') = true CharUtils.isAscii('©') = false From source file:com.igormaznitsa.jcp.expression.functions.FunctionSTR2WEB.java
@Override @Nonnull/*from www .j a v a2 s. c o m*/ public Value executeStr(@Nonnull final PreprocessorContext context, @Nonnull final Value value) { final String escaped = StringEscapeUtils.escapeHtml3(value.asString()); final StringBuilder result = new StringBuilder(escaped.length() * 2); for (int i = 0; i < escaped.length(); i++) { final char ch = escaped.charAt(i); if (CharUtils.isAscii(ch)) { result.append(ch); } else { result.append("&#").append(Integer.toString(Character.codePointAt(escaped, i))).append(';'); } } return Value.valueOf(result.toString()); }
From source file:jproxy.HttpRequestHdr.java
/** * Parses a http header from a stream./* w w w .ja v a2 s . c o m*/ * * @param in * the stream to parse. * @return array of bytes from client. * @throws IOException when reading the input stream fails */ public byte[] parse(InputStream in) throws IOException { boolean inHeaders = true; int readLength = 0; int dataLength = 0; boolean firstLine = true; ByteArrayOutputStream clientRequest = new ByteArrayOutputStream(); ByteArrayOutputStream line = new ByteArrayOutputStream(); int x; while ((inHeaders || readLength < dataLength) && ((x = in.read()) != -1)) { line.write(x); clientRequest.write(x); if (firstLine && !CharUtils.isAscii((char) x)) {// includes \n throw new IllegalArgumentException("Only ASCII supported in headers (perhaps SSL was used?)"); } if (inHeaders && (byte) x == (byte) '\n') { // $NON-NLS-1$ if (line.size() < 3) { inHeaders = false; firstLine = false; // cannot be first line either } final String reqLine = line.toString(); if (firstLine) { parseFirstLine(reqLine); firstLine = false; } else { // parse other header lines, looking for Content-Length final int contentLen = parseLine(reqLine); if (contentLen > 0) { dataLength = contentLen; // Save the last valid content length one } } if (log.isDebugEnabled()) { log.debug("Client Request Line: '" + reqLine.replaceFirst("\r\n$", "<CRLF>") + "'"); } line.reset(); } else if (!inHeaders) { readLength++; } } // Keep the raw post data rawPostData = line.toByteArray(); if (log.isDebugEnabled()) { log.debug("rawPostData in default JRE encoding: " + new String(rawPostData)); // TODO - charset? log.debug("Request: '" + clientRequest.toString().replaceAll("\r\n", "<CRLF>") + "'"); } rawRequestData = clientRequest.toByteArray(); return clientRequest.toByteArray(); }
From source file:com.screenslicer.common.HtmlCoder.java
public static String encode(String string) { try {// w w w . j ava2s . co m StringBuilder builder = new StringBuilder(); for (int i = 0; i < string.length();) { int codePoint = string.codePointAt(i); String symbol = codePointToSymbol(codePoint); char[] chars = symbol.toCharArray(); if (chars.length == 1 && CharUtils.isAscii(chars[0])) { builder.append(StringEscapeUtils.escapeHtml4(Character.toString(chars[0]))); } else { builder.append(symbol); } i += chars.length; } return builder.toString(); } catch (Throwable t) { Log.exception(t); return string; } }
From source file:org.apache.jmeter.protocol.amf.proxy.AmfRequestHdr.java
/** * Parses a http header from a stream./*from www.j a v a 2s .co m*/ * * @param in * the stream to parse. * @return array of bytes from client. */ public byte[] parse(InputStream in) throws IOException { boolean inHeaders = true; int readLength = 0; int dataLength = 0; boolean firstLine = true; ByteArrayOutputStream clientRequest = new ByteArrayOutputStream(); ByteArrayOutputStream line = new ByteArrayOutputStream(); int x; while ((inHeaders || readLength < dataLength) && ((x = in.read()) != -1)) { line.write(x); clientRequest.write(x); if (firstLine && !CharUtils.isAscii((char) x)) {// includes \n throw new IllegalArgumentException("Only ASCII supported in headers (perhaps SSL was used?)"); } if (inHeaders && (byte) x == (byte) '\n') { // $NON-NLS-1$ if (line.size() < 3) { inHeaders = false; firstLine = false; // cannot be first line either } if (firstLine) { parseFirstLine(line.toString()); firstLine = false; } else { // parse other header lines, looking for Content-Length final int contentLen = parseLine(line.toString()); if (contentLen > 0) { dataLength = contentLen; // Save the last valid content length one } } if (log.isDebugEnabled()) { log.debug("Client Request Line: " + line.toString()); } line.reset(); } else if (!inHeaders) { readLength++; } } // Keep the raw post data rawPostData = line.toByteArray(); if (log.isDebugEnabled()) { log.debug("rawPostData in default JRE encoding: " + new String(rawPostData)); // TODO - charset? log.debug("Request: " + clientRequest.toString()); } return clientRequest.toByteArray(); }
From source file:org.apache.jmeter.protocol.http.proxy.HttpRequestHdr.java
/** * Parses a http header from a stream./*from w w w . jav a 2s . co m*/ * * @param in * the stream to parse. * @return array of bytes from client. * @throws IOException when reading the input stream fails */ public byte[] parse(InputStream in) throws IOException { boolean inHeaders = true; int readLength = 0; int dataLength = 0; boolean firstLine = true; ByteArrayOutputStream clientRequest = new ByteArrayOutputStream(); ByteArrayOutputStream line = new ByteArrayOutputStream(); int x; while ((inHeaders || readLength < dataLength) && ((x = in.read()) != -1)) { line.write(x); clientRequest.write(x); if (firstLine && !CharUtils.isAscii((char) x)) {// includes \n throw new IllegalArgumentException("Only ASCII supported in headers (perhaps SSL was used?)"); } if (inHeaders && (byte) x == (byte) '\n') { // $NON-NLS-1$ if (line.size() < 3) { inHeaders = false; firstLine = false; // cannot be first line either } final String reqLine = line.toString(); if (firstLine) { parseFirstLine(reqLine); firstLine = false; } else { // parse other header lines, looking for Content-Length final int contentLen = parseLine(reqLine); if (contentLen > 0) { dataLength = contentLen; // Save the last valid content length one } } if (log.isDebugEnabled()) { log.debug("Client Request Line: '" + reqLine.replaceFirst("\r\n$", "<CRLF>") + "'"); } line.reset(); } else if (!inHeaders) { readLength++; } } // Keep the raw post data rawPostData = line.toByteArray(); if (log.isDebugEnabled()) { log.debug("rawPostData in default JRE encoding: " + new String(rawPostData)); // TODO - charset? log.debug("Request: '" + clientRequest.toString().replaceAll("\r\n", "<CRLF>") + "'"); } return clientRequest.toByteArray(); }
From source file:org.moe.natjgen.XcodeFullDocumentation.java
private String prettyString(StringBuilder capture) { if (capture.length() == 0) return null; // Remove heading new-lines while (capture.length() == 0 && (capture.charAt(0) == '\n' || capture.charAt(0) == '\r')) { capture.deleteCharAt(0);/*from w w w.j av a2 s.c o m*/ } // Remove trailing new-lines while (capture.length() == 0 && (capture.charAt(capture.length() - 1) == '\n' || capture.charAt(capture.length() - 1) == '\r')) { capture.deleteCharAt(capture.length() - 1); } String htmlencoded = StringEscapeUtils.escapeHtml4(capture.toString()); String[] words = htmlencoded.split("\\s+"); // Try to create lines with the maximum length of MAX_LINE_WIDTH capture.setLength(0); int lineLength = 0; for (String word : words) { if (lineLength > 0) { if (lineLength + word.length() > MAX_LINE_WIDTH) { capture.append("\n"); lineLength = 0; } else { capture.append(" "); } } capture.append(word); lineLength += word.length(); } for (HTMLTagReplacement replacement : replacements) { replaceAll(capture, replacement.openPlaceholder, replacement.openHtml); replaceAll(capture, replacement.closePlaceholder, replacement.closeHtml); } replaceAll(capture, "/*", "/*"); replaceAll(capture, "*/", "*/"); for (int i = 0; i < capture.length(); ++i) { char c = capture.charAt(i); if (!CharUtils.isAscii(c)) { capture.replace(i, i + 1, "&#x;"); capture.insert(i + 3, String.format("%04X", new Integer(c))); i += 7; } } return capture.toString(); }
From source file:org.xwiki.store.internal.FileSystemStoreUtils.java
/** * Return a safe version of the passed name for any filesystem. * <p>/*from ww w . j a v a 2 s .c om*/ * In practice it means the following: * <ul> * <li>any forbidden character is encoded with URL escaping format</li> * <li>in case of case sensitivity upper case characters are encoded with URL escaping format</li> * </ul> * * @param name the name to escape * @param caseInsensitive true if case insensitive filesystems should be supported * @return a safe version of the name */ public static String encode(String name, boolean caseInsensitive) { StringBuilder builder = new StringBuilder(name.length() * 3); for (int i = 0; i < name.length(); ++i) { char c = name.charAt(i); boolean encode = false; switch (c) { // % is used for encoding // + is used for encoding // Characters reserved on Windows and Unix // (https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247.aspx#naming_conventions) case '%': case '+': case '<': case '>': case ':': case '"': case '/': case '\\': case '|': case '?': case '*': encode = true; break; case ' ': // White space at the beginning and the end of a file is forbidden on Windows if (i == 0 || i == name.length() - 1) { encode = true; } break; case '.': // Dot at the beginning of a file means hidden file on Unix systems // Dot at the end of a file is forbidden on Windows if (i == 0 || i == name.length() - 1) { encode = true; } break; default: // Encode any non ASCII character to avoid surprises // For case insensitive filesystem encode upper case characters if (!CharUtils.isAscii(c) || (caseInsensitive && Character.isUpperCase(c))) { encode = true; } break; } if (encode) { encode(c, builder); } else { builder.append(c); } } return builder.toString(); }