/** * Return a string based on value that is safe to place into a javascript / html identifier: * anything not alphanumeric change to 'x'. If the first character is not alphabetic, a letter 'i' * is prepended. * * @param value The string to escape. * @return value fully escaped using javascript / html identifier rules. */ public static String escapeJavascript(String value) { if (value == null || "".equals(value)) return ""; try { StringBuilder buf = new StringBuilder(); // prepend 'i' if first character is not a letter if (!java.lang.Character.isLetter(value.charAt(0))) { buf.append("i"); } // change non-alphanumeric characters to 'x' for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); if (!java.lang.Character.isLetterOrDigit(c)) { buf.append("x"); } else { buf.append(c); } } String rv = buf.toString(); return rv; } catch (Exception e) { M_log.warn("Validator.escapeJavascript: ", e); return ""; } } // escapeJavascript
/* return string representation of fault code */ public static String GetFaultString(long fault) { if (fault > 0L) { StringBuffer sb = new StringBuffer(); if ((fault & TYPE_MASK) == TYPE_J1708) { // SID: "128/s123/1" // PID: "128/123/1" boolean active = DTOBDFault.DecodeActive(fault); int mid = DTOBDFault.DecodeSystem(fault); int fmi = DTOBDFault.DecodeFMI(fault); if (!active) { sb.append("["); } sb.append(mid); // MID sb.append("/"); if (DTOBDFault.IsJ1708_SID(fault)) { int sid = DTOBDFault.DecodePidSid(fault); sb.append("s").append(sid); // SID "128/s123/1" } else { int pid = DTOBDFault.DecodePidSid(fault); sb.append(pid); // PID "128/123/1" } sb.append("/"); sb.append(fmi); // FMI if (!active) { sb.append("]"); } return sb.toString(); } else if ((fault & TYPE_MASK) == TYPE_J1939) { // SPN: "128/1" boolean active = DTOBDFault.DecodeActive(fault); int spn = DTOBDFault.DecodeSystem(fault); int fmi = DTOBDFault.DecodeFMI(fault); sb.append(spn); // SPN sb.append("/"); sb.append(fmi); // FMI return sb.toString(); } else if ((fault & TYPE_MASK) == TYPE_OBDII) { // DTC: "P0071" [was "024C"] boolean active = DTOBDFault.DecodeActive(fault); int sysChar = DTOBDFault.DecodeSystem(fault); // System: powertrain int subSys = DTOBDFault.DecodeSPID(fault); // Mfg/Subsystem/Problem if (Character.isLetter((char) sysChar)) { sb.append((char) sysChar); } else { sb.append("U"); } if ((subSys & 0x8000) != 0) { sb.append("1"); } else { sb.append("0"); } String subSysStr = String.valueOf(1000 + ((subSys & 0xFFF) % 1000)); sb.append(subSysStr.substring(1)); return sb.toString(); } else { // unrecognized } } return ""; }
private boolean isConstantName(String completion) { for (char c : completion.toCharArray()) { if (Character.isLetter(c) && Character.isLowerCase(c)) { return false; } } return true; }
private static boolean isFirstQuestionId(String str, char c) { boolean isFirst = false; int length = str.length(); // '1' is only digital in string for backward direction, and '0' for forward direction if ((str.charAt(length - 1) == c) && Character.isLetter(str.charAt(length - 2))) isFirst = true; return isFirst; }
public List<int[]> findSeqeuences() { if (isConstantName(completion)) { rewriteCompletion(); } int[] start = new int[0]; curSequences.add(start); for (pToken = 0; pToken < token.length(); pToken++) { char t = token.charAt(pToken); for (int[] activeSequence : curSequences) { boolean mustmatch = false; int startIndex = activeSequence.length == 0 ? 0 : activeSequence[activeSequence.length - 1] + 1; for (pCompletion = startIndex; pCompletion < completion.length(); pCompletion++) { char c = completion.charAt(pCompletion); if (!Character.isLetter(c)) { if (c == t) { addNewSubsequenceForNext(activeSequence); continue; } mustmatch = true; continue; } else if (Character.isUpperCase(c)) { mustmatch = true; } if (mustmatch && !isSameIgnoreCase(c, t)) { jumpToEndOfWord(); } else if (isSameIgnoreCase(c, t)) { addNewSubsequenceForNext(activeSequence); } } } curSequences = nextSequences; nextSequences = Lists.newLinkedList(); } // filter for (Iterator<int[]> it = curSequences.iterator(); it.hasNext(); ) { int[] candidate = it.next(); if (candidate.length < token.length()) { it.remove(); continue; } } return curSequences; }
public Field(String s) { String f[] = StringTools.parseString(s, FIELD_VALUE_SEPARATOR); if ((f.length > 0) && (f[0].length() > 0) && Character.isLetter(f[0].charAt(0))) { this.isHiRes = (f.length > 0) ? f[0].equalsIgnoreCase("H") : false; this.type = (f.length > 1) ? StringTools.parseInt(f[1], -1) : -1; } else { this.type = (f.length > 0) ? StringTools.parseInt(f[0], -1) : -1; this.isHiRes = (f.length > 1) ? f[1].equalsIgnoreCase("H") : false; } this.index = (f.length > 2) ? StringTools.parseInt(f[2], 0) : 0; this.length = (f.length > 3) ? StringTools.parseInt(f[3], 0) : 0; this.isValid = (f.length == 4) && (this.type >= 0) && (this.index >= 0) && (this.length > 0); }
public boolean validString(String string) { if (string == null) return false; if (string.equals("") || string.length() > NAME_CAP || string.toLowerCase().equalsIgnoreCase("god") || string.toLowerCase().equalsIgnoreCase("default") || string.toLowerCase().startsWith("admin") || string.toLowerCase().startsWith("moderator") || string.toLowerCase().equalsIgnoreCase("dusk")) return false; String strValid = "0123456789][_'#"; char[] letters = string.toCharArray(); char[] validChars = strValid.toCharArray(); boolean blnValid = true; for (int n = 0; n < letters.length; n++) { if (!java.lang.Character.isLetter(letters[n])) { blnValid = false; for (int i = 0; i < validChars.length; i++) { if (letters[n] == validChars[i]) { blnValid = true; } } } } if (!blnValid) { return false; } if (string.indexOf('\\') != -1) return false; if (string.indexOf('\'') != -1) return false; if (string.indexOf('"') != -1) return false; if (string.indexOf('%') != -1) return false; if (string.indexOf(';') != -1) return false; if (string.indexOf(':') != -1) return false; if (string.indexOf('#') != -1) return false; return true; }
public void write(char ch) throws IOException { boolean ok; switch (state) { case S_text: if (ch == '\n' || ch == '\r') { break; // unadorned newlines are ignored } else if (ch == '{') { if (currentCharacters.length() > 0) { handleText(currentCharacters.toString()); currentCharacters = new StringBuffer(); } level++; begingroup(); } else if (ch == '}') { if (currentCharacters.length() > 0) { handleText(currentCharacters.toString()); currentCharacters = new StringBuffer(); } if (level == 0) throw new IOException("Too many close-groups in RTF text"); endgroup(); level--; } else if (ch == '\\') { if (currentCharacters.length() > 0) { handleText(currentCharacters.toString()); currentCharacters = new StringBuffer(); } state = S_backslashed; } else { currentCharacters.append(ch); } break; case S_backslashed: if (ch == '\'') { state = S_aftertick; break; } if (!Character.isLetter(ch)) { char newstring[] = new char[1]; newstring[0] = ch; if (!handleKeyword(new String(newstring))) { warning("Unknown keyword: " + newstring + " (" + (byte) ch + ")"); } state = S_text; pendingKeyword = null; /* currentCharacters is already an empty stringBuffer */ break; } state = S_token; /* FALL THROUGH */ case S_token: if (Character.isLetter(ch)) { currentCharacters.append(ch); } else { pendingKeyword = currentCharacters.toString(); currentCharacters = new StringBuffer(); // Parameter following? if (Character.isDigit(ch) || (ch == '-')) { state = S_parameter; currentCharacters.append(ch); } else { ok = handleKeyword(pendingKeyword); if (!ok) warning("Unknown keyword: " + pendingKeyword); pendingKeyword = null; state = S_text; // Non-space delimiters get included in the text if (!Character.isWhitespace(ch)) write(ch); } } break; case S_parameter: if (Character.isDigit(ch)) { currentCharacters.append(ch); } else { /* TODO: Test correct behavior of \bin keyword */ if (pendingKeyword.equals("bin")) { /* magic layer-breaking kwd */ long parameter = Long.parseLong(currentCharacters.toString()); pendingKeyword = null; state = S_inblob; binaryBytesLeft = parameter; if (binaryBytesLeft > Integer.MAX_VALUE) binaryBuf = new ByteArrayOutputStream(Integer.MAX_VALUE); else binaryBuf = new ByteArrayOutputStream((int) binaryBytesLeft); savedSpecials = specialsTable; specialsTable = allSpecialsTable; break; } int parameter = Integer.parseInt(currentCharacters.toString()); ok = handleKeyword(pendingKeyword, parameter); if (!ok) warning("Unknown keyword: " + pendingKeyword + " (param " + currentCharacters + ")"); pendingKeyword = null; currentCharacters = new StringBuffer(); state = S_text; // Delimiters here are interpreted as text too if (!Character.isWhitespace(ch)) write(ch); } break; case S_aftertick: if (Character.digit(ch, 16) == -1) state = S_text; else { pendingCharacter = Character.digit(ch, 16); state = S_aftertickc; } break; case S_aftertickc: state = S_text; if (Character.digit(ch, 16) != -1) { pendingCharacter = pendingCharacter * 16 + Character.digit(ch, 16); ch = translationTable[pendingCharacter]; if (ch != 0) handleText(ch); } break; case S_inblob: binaryBuf.write(ch); binaryBytesLeft--; if (binaryBytesLeft == 0) { state = S_text; specialsTable = savedSpecials; savedSpecials = null; handleBinaryBlob(binaryBuf.toByteArray()); binaryBuf = null; } } }