rulebasedbreakiterator.java
来自「java源代码 请看看啊 提点宝贵的意见」· Java 代码 · 共 1,470 行 · 第 1/5 页
JAVA
1,470 行
// set up a vector to contain the broken-up description (each entry in the // vector is a separate rule) and a stack for keeping track of opening // punctuation Vector tempRuleList = new Vector(); Stack parenStack = new Stack(); int p = 0; int ruleStart = 0; char c = '\u0000'; char lastC = '\u0000'; char lastOpen = '\u0000'; boolean haveEquals = false; boolean havePipe = false; boolean sawVarName = false; final String charsThatCantPrecedeAsterisk = "=/{(|}*;\u0000"; // if the description doesn't end with a semicolon, tack a semicolon onto the end if (description.length() != 0 && description.charAt(description.length() - 1) != ';') { description = description + ";"; } // for each character, do... while (p < description.length()) { c = description.charAt(p); switch (c) { // if the character is a backslash, skip the character that follows it // (it'll get treated as a literal character) case '\\': ++p; break; // if the character is opening punctuation, verify that no nesting // rules are broken, and push the character onto the stack case '{': case '<': case '[': case '(': if (lastOpen == '<') { error("Can't nest brackets inside <>", p, description); } if (lastOpen == '[' && c != '[') { error("Can't nest anything in [] but []", p, description); } // if we see < anywhere except on the left-hand side of =, // we must be seeing a variable name that was never defined if (c == '<' && (haveEquals || havePipe)) { error("Unknown variable name", p, description); } lastOpen = c; parenStack.push(new Character(c)); if (c == '<') { sawVarName = true; } break; // if the character is closing punctuation, verify that it matches the // last opening punctuation we saw, and that the brackets contain // something, then pop the stack case '}': case '>': case ']': case ')': char expectedClose = '\u0000'; switch (lastOpen) { case '{': expectedClose = '}'; break; case '[': expectedClose = ']'; break; case '(': expectedClose = ')'; break; case '<': expectedClose = '>'; break; } if (c != expectedClose) { error("Unbalanced parentheses", p, description); } if (lastC == lastOpen) { error("Parens don't contain anything", p, description); } parenStack.pop(); if (!parenStack.empty()) { lastOpen = ((Character)(parenStack.peek())).charValue(); } else { lastOpen = '\u0000'; } break; // if the character is an asterisk, make sure it occurs in a place // where an asterisk can legally go case '*': if (charsThatCantPrecedeAsterisk.indexOf(lastC) != -1) { error("Misplaced asterisk", p, description); } break; // if the character is a question mark, make sure it follows an asterisk case '?': if (lastC != '*') { error("Misplaced ?", p, description); } break; // if the character is an equals sign, make sure we haven't seen another // equals sign or a slash yet case '=': if (haveEquals || havePipe) { error("More than one = or / in rule", p, description); } haveEquals = true; break; // if the character is a slash, make sure we haven't seen another slash // or an equals sign yet case '/': if (haveEquals || havePipe) { error("More than one = or / in rule", p, description); } if (sawVarName) { error("Unknown variable name", p, description); } havePipe = true; break; // if the character is an exclamation point, make sure it occurs only // at the beginning of a rule case '!': if (lastC != ';' && lastC != '\u0000') { error("! can only occur at the beginning of a rule", p, description); } break; // we don't have to do anything special on a period case '.': break; // if the character is a syntax character that can only occur // inside [], make sure that it does in fact only occur inside []. case '^': case '-': case ':': if (lastOpen != '[' && lastOpen != '<') { error("Illegal character", p, description); } break; // if the character is a semicolon, do the following... case ';': // make sure the rule contains something and that there are no // unbalanced parentheses or brackets if (lastC == ';' || lastC == '\u0000') { error("Empty rule", p, description); } if (!parenStack.empty()) { error("Unbalanced parenheses", p, description); } if (parenStack.empty()) { // if the rule contained an = sign, call processSubstitution() // to replace the substitution name with the substitution text // wherever it appears in the description if (haveEquals) { description = processSubstitution(description.substring(ruleStart, p), description, p + 1); } else { // otherwise, check to make sure the rule doesn't reference // any undefined substitutions if (sawVarName) { error("Unknown variable name", p, description); } // then add it to tempRuleList tempRuleList.addElement(description.substring(ruleStart, p)); } // and reset everything to process the next rule ruleStart = p + 1; haveEquals = havePipe = sawVarName = false; } break; // if the character is a vertical bar, check to make sure that it // occurs inside a () expression and that the character that precedes // it isn't also a vertical bar case '|': if (lastC == '|') { error("Empty alternative", p, description); } if (parenStack.empty() || lastOpen != '(') { error("Misplaced |", p, description); } break; // if the character is anything else (escaped characters are // skipped and don't make it here), it's an error default: if (c >= ' ' && c < '\u007f' && !Character.isLetter(c) && !Character.isDigit(c)) { error("Illegal character", p, description); } break; } lastC = c; ++p; } if (tempRuleList.size() == 0) { error("No valid rules in description", p, description); } return tempRuleList; } /** * This function performs variable-name substitutions. First it does syntax * checking on the variable-name definition. If it's syntactically valid, it * then goes through the remainder of the description and does a simple * find-and-replace of the variable name with its text. (The variable text * must be enclosed in either [] or () for this to work.) */ protected String processSubstitution(String substitutionRule, String description, int startPos) { // isolate out the text on either side of the equals sign String replace; String replaceWith; int equalPos = substitutionRule.indexOf('='); replace = substitutionRule.substring(0, equalPos); replaceWith = substitutionRule.substring(equalPos + 1); // check to see whether the substitution name is something we've declared // to be "special". For RuleBasedBreakIterator itself, this is "<ignore>". // This function takes care of any extra processing that has to be done // with "special" substitution names. handleSpecialSubstitution(replace, replaceWith, startPos, description); // perform various other syntax checks on the rule if (replaceWith.length() == 0) { error("Nothing on right-hand side of =", startPos, description); } if (replace.length() == 0) { error("Nothing on left-hand side of =", startPos, description); } if (replace.length() == 2 && replace.charAt(0) != '\\') { error("Illegal left-hand side for =", startPos, description); } if (replace.length() >= 3 && replace.charAt(0) != '<' && replace.charAt(equalPos - 1) != '>') { error("Illegal left-hand side for =", startPos, description); } if (!(replaceWith.charAt(0) == '[' && replaceWith.charAt(replaceWith.length() - 1) == ']') && !(replaceWith.charAt(0) == '(' && replaceWith.charAt( replaceWith.length() - 1) == ')')) { error("Illegal right-hand side for =", startPos, description); } // now go through the rest of the description (which hasn't been broken up // into separate rules yet) and replace every occurrence of the // substitution name with the substitution body StringBuffer result = new StringBuffer(); result.append(description.substring(0, startPos)); int lastPos = startPos; int pos = description.indexOf(replace, startPos); while (pos != -1) { result.append(description.substring(lastPos, pos)); result.append(replaceWith); lastPos = pos + replace.length(); pos = description.indexOf(replace, lastPos); } result.append(description.substring(lastPos)); return result.toString(); } /** * This function defines a protocol for handling substitution names that * are "special," i.e., that have some property beyond just being * substitutions. At the RuleBasedBreakIterator level, we have one * special substitution name, "<ignore>". Subclasses can override this * function to add more. Any special processing that has to go on beyond * that which is done by the normal substitution-processing code is done * here. */ protected void handleSpecialSubstitution(String replace, String replaceWith, int startPos, String description) { // if we get a definition for a substitution called "ignore", it defines // the ignore characters for the iterator. Check to make sure the expression // is a [] expression, and if it is, parse it and store the characters off // to the side. if (replace.equals("<ignore>")) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?