- pattern = newPattern;
- enum State{ startState, keywordState, pastKeywordState, phraseState};
-
- //Initialization
- UnicodeString keyword ;
- UnicodeString phrase ;
- UnicodeString* ptrPhrase ;
- int32_t braceCount = 0;
-
- if (parsedValuesHash == NULL) {
- initHashTable(status);
- if (U_FAILURE(status)) {
- return;
- }
- }
- parsedValuesHash->removeAll();
- parsedValuesHash->setValueDeleter(uhash_deleteUnicodeString);
-
- //Process the state machine
- State state = startState;
- for (int32_t i = 0; i < pattern.length(); ++i) {
- //Get the character and check its type
- UChar ch = pattern.charAt(i);
- CharacterClass type = classifyCharacter(ch);
-
- //Allow any character in phrase but nowhere else
- if ( type == tOther ) {
- if ( state == phraseState ){
- phrase += ch;
- continue;
- }else {
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
- }
- }
-
- //Process the state machine
- switch (state) {
- //At the start of pattern
- case startState:
- switch (type) {
- case tSpace:
- break;
- case tStartKeyword:
- state = keywordState;
- keyword += ch;
- break;
- //If anything else is encountered, it's a syntax error
- default:
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
- }//end of switch(type)
- break;
-
- //Handle the keyword state
- case keywordState:
- switch (type) {
- case tSpace:
- state = pastKeywordState;
- break;
- case tStartKeyword:
- case tContinueKeyword:
- keyword += ch;
- break;
- case tLeftBrace:
- state = phraseState;
- break;
- //If anything else is encountered, it's a syntax error
- default:
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
- }//end of switch(type)
- break;
-
- //Handle the pastkeyword state
- case pastKeywordState:
- switch (type) {
- case tSpace:
- break;
- case tLeftBrace:
- state = phraseState;
- break;
- //If anything else is encountered, it's a syntax error
- default:
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
- }//end of switch(type)
- break;
-
- //Handle the phrase state
- case phraseState:
- switch (type) {
- case tLeftBrace:
- braceCount++;
- phrase += ch;
- break;
- case tRightBrace:
- //Matching keyword, phrase pair found
- if (braceCount == 0){
- //Check validity of keyword
- if (parsedValuesHash->get(keyword) != NULL) {
- status = U_DUPLICATE_KEYWORD;
- cleanHashTable();
- return;
- }
- if (keyword.length() == 0) {
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
- }
-
- //Store the keyword, phrase pair in hashTable
- ptrPhrase = new UnicodeString(phrase);
- parsedValuesHash->put( keyword, ptrPhrase, status);
-
- //Reinitialize
- keyword.remove();
- phrase.remove();
- ptrPhrase = NULL;
- state = startState;
- }
-
- if (braceCount > 0){
- braceCount-- ;
- phrase += ch;
- }
- break;
- default:
- phrase += ch;
- }//end of switch(type)
- break;
-
- //Handle the default case of switch(state)
- default:
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
-
- }//end of switch(state)
- }
-
- //Check if the state machine is back to startState
- if ( state != startState){
- status = U_PATTERN_SYNTAX_ERROR;
- cleanHashTable();
- return;
- }
-
- //Check if "other" keyword is present
- if ( !checkSufficientDefinition() ) {
- status = U_DEFAULT_KEYWORD_MISSING;
- cleanHashTable();