Alex Karasulu | 5ae51c5 | 2006-03-07 21:17:07 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 The Apache Software Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | package org.apache.felix.examples.spellcheckbinder; |
| 18 | |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.StringTokenizer; |
| 22 | |
| 23 | import org.apache.felix.examples.dictionaryservice.DictionaryService; |
| 24 | import org.apache.felix.examples.spellcheckservice.SpellCheckService; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * This class re-implements the spell check service of Example 5. This service |
| 29 | * implementation behaves exactly like the one in Example 5, specifically, it |
| 30 | * aggregates all available dictionary services, monitors their dynamic |
| 31 | * availability, and only offers the spell check service if there are dictionary |
| 32 | * services available. The service implementation is greatly simplified, though, |
| 33 | * by using the Service Binder. Notice that there is no OSGi references in the |
| 34 | * application code; intead, the metadata.xml file describes the service |
| 35 | * dependencies to the Service Binder, which automatically manages them and it |
| 36 | * also automatically registers the spell check services as appropriate. |
| 37 | * |
| 38 | * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a> |
| 39 | */ |
| 40 | public class SpellCheckServiceImpl implements SpellCheckService |
| 41 | { |
| 42 | // List of service objects. |
| 43 | private ArrayList m_svcObjList = new ArrayList(); |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * This method is used by the Service Binder to add new dictionaries to the |
| 48 | * spell check service. |
| 49 | * |
| 50 | * @param dictionary |
| 51 | * the dictionary to add to the spell check service. |
| 52 | */ |
| 53 | public void addDictionary( DictionaryService dictionary ) |
| 54 | { |
| 55 | // Lock list and add service object. |
| 56 | synchronized ( m_svcObjList ) |
| 57 | { |
| 58 | m_svcObjList.add( dictionary ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * This method is used by the Service Binder to remove dictionaries from the |
| 65 | * spell check service. |
| 66 | * |
| 67 | * @param dictionary |
| 68 | * the dictionary to remove from the spell check service. |
| 69 | */ |
| 70 | public void removeDictionary( DictionaryService dictionary ) |
| 71 | { |
| 72 | // Lock list and remove service object. |
| 73 | synchronized ( m_svcObjList ) |
| 74 | { |
| 75 | m_svcObjList.remove( dictionary ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Checks a given passage for spelling errors. A passage is any number of |
| 82 | * words separated by a space and any of the following punctuation marks: |
| 83 | * comma (,), period (.), exclamation mark (!), question mark (?), |
| 84 | * semi-colon (;), and colon(:). |
| 85 | * |
| 86 | * @param passage |
| 87 | * the passage to spell check. |
| 88 | * @return An array of misspelled words or null if no words are misspelled. |
| 89 | */ |
| 90 | public String[] check( String passage ) |
| 91 | { |
| 92 | // No misspelled words for an empty string. |
| 93 | if ( ( passage == null ) || ( passage.length() == 0 ) ) |
| 94 | { |
| 95 | return null; |
| 96 | } |
| 97 | |
| 98 | ArrayList errorList = new ArrayList(); |
| 99 | |
| 100 | // Tokenize the passage using spaces and punctionation. |
| 101 | StringTokenizer st = new StringTokenizer( passage, " ,.!?;:" ); |
| 102 | |
| 103 | // Lock the service list. |
| 104 | synchronized ( m_svcObjList ) |
| 105 | { |
| 106 | // Loop through each word in the passage. |
| 107 | while ( st.hasMoreTokens() ) |
| 108 | { |
| 109 | String word = st.nextToken(); |
| 110 | boolean correct = false; |
| 111 | |
| 112 | // Check each available dictionary for the current word. |
| 113 | for ( int i = 0; ( !correct ) && ( i < m_svcObjList.size() ); i++ ) |
| 114 | { |
| 115 | DictionaryService dictionary = ( DictionaryService ) m_svcObjList.get( i ); |
| 116 | |
| 117 | if ( dictionary.checkWord( word ) ) |
| 118 | { |
| 119 | correct = true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // If the word is not correct, then add it |
| 124 | // to the incorrect word list. |
| 125 | if ( !correct ) |
| 126 | { |
| 127 | errorList.add( word ); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Return null if no words are incorrect. |
| 133 | if ( errorList.size() == 0 ) |
| 134 | { |
| 135 | return null; |
| 136 | } |
| 137 | |
| 138 | // Return the array of incorrect words. |
| 139 | return ( String[] ) errorList.toArray( new String[errorList.size()] ); |
| 140 | } |
| 141 | } |