001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.sequencer.ddl; 017 018import org.modeshape.common.annotation.NotThreadSafe; 019 020/** 021 * Interface used by a parser to determine a score describing how well it handles the DDL content. 022 */ 023@NotThreadSafe 024public class DdlParserScorer { 025 026 private int score = 0; 027 028 /** 029 * Reset the score back to zero. 030 */ 031 public void reset() { 032 this.score = 0; 033 } 034 035 /** 036 * Increment the score because another statement was matched. 037 * 038 * @param count the number of statements 039 */ 040 public void scoreStatements( int count ) { 041 score += count; 042 } 043 044 /** 045 * Increment the score if the given text contains any of the supply keywords. 046 * 047 * @param text the text to evaluate; may be null 048 * @param factor the factor to use for each increment 049 * @param keywords the keywords to be found in the text 050 */ 051 public void scoreText( String text, 052 int factor, 053 String... keywords ) { 054 if (text != null && keywords != null) { 055 // Increment the score once for each keyword that is found within the text ... 056 String lowercaseText = text.toLowerCase(); 057 for (String keyword : keywords) { 058 if (keyword == null) continue; 059 String lowercaseKeyword = keyword.toLowerCase(); 060 int index = 0; 061 while (true) { 062 index = lowercaseText.indexOf(lowercaseKeyword, index); 063 if (index == -1) break; 064 score += factor; 065 ++index; 066 } 067 } 068 } 069 } 070 071 /** 072 * Increment the score if the given text contains any of the supply keywords. 073 * 074 * @param text the text to evaluate; may be null 075 * @param keywords the keywords to be found in the text 076 */ 077 public void scoreText( String text, 078 String... keywords ) { 079 scoreText(text, 1, keywords); 080 } 081 082 /** 083 * Get the score. 084 * 085 * @return the score 086 */ 087 public int getScore() { 088 return score; 089 } 090 091 /** 092 * {@inheritDoc} 093 * 094 * @see java.lang.Object#toString() 095 */ 096 @Override 097 public String toString() { 098 return Integer.toString(score); 099 } 100}