// 过滤整数字符,把所有非0~9的字符全部删除 private void filterInt(StringBuilder builder) { for (int i = builder.length() - 1; i >= 0; i--) { int cp = builder.codePointAt(i); if (cp > '9' || cp < '0') { builder.deleteCharAt(i); } } }
public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { StringBuilder builder = new StringBuilder(string); for (int i = builder.length() - 1; i >= 0; i--) { int cp = builder.codePointAt(i); if (!Character.isDigit(cp) && cp != '-') { builder.deleteCharAt(i); if (Character.isSupplementaryCodePoint(cp)) { i--; builder.deleteCharAt(i); } } } super.insertString(fb, offset, builder.toString(), attr); }
private StringBuilder sanitize(final StringBuilder aContent) { int i = 0; int lastBreak = 0; while (i < aContent.length()) { // Check valid unicode char if (!isValidXMLChar(aContent.codePointAt(i))) { aContent.setCharAt(i, ' '); i++; continue; } // Set up how many characters we want to skip int seek = i + 1; // Do we maybe have an entity? if (aContent.charAt(i) == '&') { // REC 2006-10-21 Some PDFs seem to have entities and others // don't // so we may encounter &'s that do not introduce an entity and // just ignore them. final int end = aContent.indexOf(";", i); if (end != -1) { final String cand = aContent.substring(i, end + 1); String r = null; try { if (cand.startsWith("&#x")) { final int cp = Integer.parseInt(cand.substring(2, cand.length() - 1), 16); r = isValidXMLChar(cp) ? String.valueOf(Character.toChars(cp)) : " "; } else if (cand.startsWith("&#")) { final int cp = Integer.parseInt(cand.substring(2, cand.length() - 1)); r = isValidXMLChar(cp) ? String.valueOf(Character.toChars(cp)) : " "; } else { // RE 2006-10-22 The chance that there is a & and a // ; // together in a string is quite big. Let's be // tolerant. } } catch (final NumberFormatException e) { log.warn("Invalid numeric entity in fragment [" + cand + "] - Dropping it."); } // Expand the entity and set proper skip (if found) if (r != null) { aContent.replace(i, i + cand.length(), r); seek = i + r.length(); } } } // Match against the Trie after numeric entity expansion is over if (substitutionTable != null) { final Trie<String>.Node match = substitutionTable.getNode(aContent, i); if (match != null) { aContent.replace(i, i + match.level, match.value); seek = i + match.value.length(); } } // Check line breaks while (i < seek) { if (aContent.charAt(i) == '\n') { lastBreak = i; } else if (Character.isWhitespace(aContent.codePointAt(i)) && (i > (lastBreak + 79))) { lastBreak = i; aContent.replace(i, i + 1, "\n"); } i++; } } return aContent; }