You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ak...@apache.org on 2006/03/07 17:50:27 UTC

svn commit: r383928 - in /incubator/felix/trunk: ./ org.apache.felix.examples.spellcheckservice/ org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/ org.apache.felix.examples.spellcheckservice/src/main...

Author: akarasulu
Date: Tue Mar  7 08:50:26 2006
New Revision: 383928

URL: http://svn.apache.org/viewcvs?rev=383928&view=rev
Log:
got the fifth example working

Modified:
    incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/pom.xml
    incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
    incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
    incubator/felix/trunk/pom.xml

Modified: incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/pom.xml
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/pom.xml?rev=383928&r1=383927&r2=383928&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/pom.xml (original)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/pom.xml Tue Mar  7 08:50:26 2006
@@ -15,6 +15,12 @@
       <version>${pom.version}</version>
       <scope>provided</scope>
     </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.apache.felix.examples.dictionaryservice</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
@@ -33,6 +39,9 @@
             <bundleActivator>
               org.apache.felix.examples.spellcheckservice.impl.Activator
             </bundleActivator>
+            <importPackage>
+              org.apache.felix.examples.dictionaryservice
+            </importPackage>
             <exportPackage>
               org.apache.felix.examples.spellcheckservice
             </exportPackage>

Modified: incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java?rev=383928&r1=383927&r2=383928&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java (original)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/SpellCheckService.java Tue Mar  7 08:50:26 2006
@@ -18,9 +18,23 @@
 
 
 /**
+ * A simple service interface that defines a spell check service. A spell check
+ * service checks the spelling of all words in a given passage. A passage is any
+ * number of words separated by a space character and the following punctuation
+ * marks: comma, period, exclamation mark, question mark, semi-colon, and colon.
  * 
  * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
  */
 public interface SpellCheckService
 {
-}
+    /**
+     * Checks a given passage for spelling errors. A passage is any number of
+     * words separated by a space and any of the following punctuation marks:
+     * comma (,), period (.), exclamation mark (!), question mark (?),
+     * semi-colon (;), and colon(:).
+     * 
+     * @param passage the passage to spell check.
+     * @return An array of misspelled words or null if no words are misspelled.
+     */
+    public String[] check( String passage );
+}
\ No newline at end of file

Modified: incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java?rev=383928&r1=383927&r2=383928&view=diff
==============================================================================
--- incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java (original)
+++ incubator/felix/trunk/org.apache.felix.examples.spellcheckservice/src/main/java/org/apache/felix/examples/spellcheckservice/impl/Activator.java Tue Mar  7 08:50:26 2006
@@ -17,23 +17,249 @@
 package org.apache.felix.examples.spellcheckservice.impl;
 
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.StringTokenizer;
+
+import org.apache.felix.examples.dictionaryservice.DictionaryService;
+import org.apache.felix.examples.spellcheckservice.SpellCheckService;
+
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 
 
 /**
- * 
+ * This class implements a bundle that implements a spell check service. The
+ * spell check service uses all available dictionary services to check for the
+ * existence of words in a given sentence. This bundle not only monitors the
+ * dynamic availability of dictionary services, but it manages the aggregation
+ * of all available dictionary services as they arrive and depart. The spell
+ * check service is only registered if there are dictionary services available,
+ * thus the spell check service will appear and disappear as dictionary services
+ * appear and disappear, respectively.
  * 
  * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
  */
-public class Activator implements BundleActivator
+public class Activator implements BundleActivator, ServiceListener
 {
-    public void start( BundleContext arg0 ) throws Exception
+    // Bundle's context.
+    private BundleContext m_context = null;
+
+    // List of available dictionary service references.
+    private ArrayList m_refList = new ArrayList();
+
+    // Maps service references to service objects.
+    private HashMap m_refToObjMap = new HashMap();
+
+    // The spell check service registration.
+    private ServiceRegistration m_reg = null;
+
+
+    /**
+     * Implements BundleActivator.start(). Adds itself as a service listener and
+     * queries for all currently available dictionary services. Any available
+     * dictionary services are added to the service reference list. If
+     * dictionary services are found, then the spell check service is
+     * registered.
+     * 
+     * @param context the framework context for the bundle.
+     */
+    public void start( BundleContext context ) throws Exception
+    {
+        m_context = context;
+
+        // Listen for events pertaining to dictionary services.
+        m_context.addServiceListener( this, "(&(objectClass=" + DictionaryService.class.getName() + ")"
+            + "(Language=*))" );
+
+        // Query for all dictionary services.
+        ServiceReference[] refs = m_context.getServiceReferences( DictionaryService.class.getName(), "(Language=*)" );
+
+        // Add any dictionaries to the service reference list.
+        if ( refs != null )
+        {
+            // Lock the list.
+            synchronized ( m_refList )
+            {
+                for ( int i = 0; i < refs.length; i++ )
+                {
+                    // Get the service object.
+                    Object service = m_context.getService( refs[i] );
+
+                    // Make that the service is not being duplicated.
+                    if ( ( service != null ) && ( m_refToObjMap.get( refs[i] ) == null ) )
+                    {
+                        // Add to the reference list.
+                        m_refList.add( refs[i] );
+                        // Map reference to service object for easy look up.
+                        m_refToObjMap.put( refs[i], service );
+                    }
+                }
+
+                // Register spell check service if there are any dictionary services.
+                if ( m_refList.size() > 0 )
+                {
+                    m_reg = m_context.registerService( SpellCheckService.class.getName(), 
+                        new SpellCheckServiceImpl(), null );
+                }
+            }
+        }
+    }
+
+
+    /**
+     * Implements BundleActivator.stop(). Does nothing since the framework will
+     * automatically unregister any registered services, release any used
+     * services, and remove any event listeners.
+     * 
+     * @param context
+     *            the framework context for the bundle.
+     */
+    public void stop( BundleContext context )
     {
+        // NOTE: The services automatically released.
     }
 
 
-    public void stop( BundleContext arg0 ) throws Exception
+    /**
+     * Implements ServiceListener.serviceChanged(). Monitors the arrival and
+     * departure of dictionary services, adding and removing them from the
+     * service reference list, respectively. In the case where no more
+     * dictionary services are available, the spell check service is registered.
+     * As soon as any dictionary spell check becomes available, the spell check
+     * service is reregistered.
+     * 
+     * @param event the fired service event.
+     */
+    public void serviceChanged( ServiceEvent event )
+    {
+        // Add the new dictionary service to the service list.
+        if ( event.getType() == ServiceEvent.REGISTERED )
+        {
+            synchronized ( m_refList )
+            {
+                // Get the service object.
+                Object service = m_context.getService( event.getServiceReference() );
+
+                // Make that the service is not being duplicated.
+                if ( ( service != null ) && ( m_refToObjMap.get( event.getServiceReference() ) == null ) )
+                {
+                    // Add to the reference list.
+                    m_refList.add( event.getServiceReference() );
+                    // Map reference to service object for easy look up.
+                    m_refToObjMap.put( event.getServiceReference(), service );
+
+                    // Register spell check service if necessary.
+                    if ( m_reg == null )
+                    {
+                        m_reg = m_context.registerService( SpellCheckService.class.getName(),
+                            new SpellCheckServiceImpl(), null );
+                    }
+                }
+                else if ( service != null )
+                {
+                    m_context.ungetService( event.getServiceReference() );
+                }
+            }
+        }
+        // Remove the departing service from the service list.
+        else if ( event.getType() == ServiceEvent.UNREGISTERING )
+        {
+            synchronized ( m_refList )
+            {
+                // Make sure the service is in the list.
+                if ( m_refToObjMap.get( event.getServiceReference() ) != null )
+                {
+                    // Unget the service object.
+                    m_context.ungetService( event.getServiceReference() );
+                    // Remove service reference.
+                    m_refList.remove( event.getServiceReference() );
+                    // Remove service reference from map.
+                    m_refToObjMap.remove( event.getServiceReference() );
+
+                    // If there are no more dictionary services,
+                    // then unregister spell check service.
+                    if ( m_refList.size() == 0 )
+                    {
+                        m_reg.unregister();
+                        m_reg = null;
+                    }
+                }
+            }
+        }
+    }
+
+    
+    /**
+     * A private inner class that implements a spell check service; see
+     * SpellCheckService for details of the service.
+     */
+    private class SpellCheckServiceImpl implements SpellCheckService
     {
+        /**
+         * Implements SpellCheckService.check(). Checks the given passage for
+         * misspelled words.
+         * 
+         * @param passage
+         *            the passage to spell check.
+         * @return An array of misspelled words or null if no words are
+         *         misspelled.
+         */
+        public String[] check( String passage )
+        {
+            // No misspelled words for an empty string.
+            if ( ( passage == null ) || ( passage.length() == 0 ) )
+            {
+                return null;
+            }
+
+            ArrayList errorList = new ArrayList();
+
+            // Tokenize the passage using spaces and punctionation.
+            StringTokenizer st = new StringTokenizer( passage, " ,.!?;:" );
+
+            // Lock the service list.
+            synchronized ( m_refList )
+            {
+                // Loop through each word in the passage.
+                while ( st.hasMoreTokens() )
+                {
+                    String word = st.nextToken();
+
+                    boolean correct = false;
+
+                    // Check each available dictionary for the current word.
+                    for ( int i = 0; ( !correct ) && ( i < m_refList.size() ); i++ )
+                    {
+                        DictionaryService dictionary = ( DictionaryService ) m_refToObjMap.get( m_refList.get( i ) );
+
+                        if ( dictionary.checkWord( word ) )
+                        {
+                            correct = true;
+                        }
+                    }
+
+                    // If the word is not correct, then add it
+                    // to the incorrect word list.
+                    if ( !correct )
+                    {
+                        errorList.add( word );
+                    }
+                }
+            }
+
+            // Return null if no words are incorrect.
+            if ( errorList.size() == 0 )
+            {
+                return null;
+            }
+
+            // Return the array of incorrect words.
+            return ( String[] ) errorList.toArray( new String[errorList.size()] );
+        }
     }
 }

Modified: incubator/felix/trunk/pom.xml
URL: http://svn.apache.org/viewcvs/incubator/felix/trunk/pom.xml?rev=383928&r1=383927&r2=383928&view=diff
==============================================================================
--- incubator/felix/trunk/pom.xml (original)
+++ incubator/felix/trunk/pom.xml Tue Mar  7 08:50:26 2006
@@ -19,6 +19,7 @@
     <module>org.apache.felix.examples.frenchdictionary</module>
     <module>org.apache.felix.examples.dictionaryclient</module>
     <module>org.apache.felix.examples.dictionaryclient2</module>
+    <module>org.apache.felix.examples.spellcheckservice</module>
   </modules>
 
   <repositories>