You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2020/01/28 20:34:31 UTC

[jspwiki] 30/32: apply format and fixes suggested by intellij to classes under the org.apache.wiki.search package

This is an automated email from the ASF dual-hosted git repository.

juanpablo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jspwiki.git

commit d94e958d666b6cd91e5dc67b8dc20e5d74e8a0e0
Author: juanpablo <ju...@apache.org>
AuthorDate: Tue Jan 28 21:17:05 2020 +0100

    apply format and fixes suggested by intellij to classes under the org.apache.wiki.search package
---
 .../apache/wiki/search/BasicSearchProvider.java    | 143 +++++++++------------
 .../java/org/apache/wiki/search/QueryItem.java     |   3 +-
 .../java/org/apache/wiki/search/SearchMatcher.java |  49 +++----
 .../org/apache/wiki/search/SearchProvider.java     |  17 +--
 .../java/org/apache/wiki/search/SearchResult.java  |   3 +-
 .../apache/wiki/search/SearchResultComparator.java |   7 +-
 6 files changed, 93 insertions(+), 129 deletions(-)

diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/BasicSearchProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/search/BasicSearchProvider.java
index 3f2d58b..12e2887 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/search/BasicSearchProvider.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/search/BasicSearchProvider.java
@@ -42,30 +42,28 @@ import java.util.TreeSet;
  *
  *  @since 2.2.21.
  */
-public class BasicSearchProvider implements SearchProvider
-{
-    private static final Logger log = Logger.getLogger(BasicSearchProvider.class);
+public class BasicSearchProvider implements SearchProvider {
+
+    private static final Logger log = Logger.getLogger( BasicSearchProvider.class );
 
     private WikiEngine m_engine;
 
     /**
      *  {@inheritDoc}
      */
-    public void initialize(WikiEngine engine, Properties props)
-            throws NoRequiredPropertyException, IOException
-    {
+    public void initialize( final WikiEngine engine, final Properties props ) throws NoRequiredPropertyException, IOException {
         m_engine = engine;
     }
 
     /**
      *  {@inheritDoc}
      */
-    public void pageRemoved(WikiPage page) {}
+    public void pageRemoved( final WikiPage page ) {}
 
     /**
      *  {@inheritDoc}
      */
-    public void reindexPage(WikiPage page) {}
+    public void reindexPage( final WikiPage page ) {}
 
     /**
      *  Parses a query into something that we can use.
@@ -73,74 +71,61 @@ public class BasicSearchProvider implements SearchProvider
      *  @param query A query string.
      *  @return A parsed array.
      */
-    public  QueryItem[] parseQuery(String query)
-    {
-        StringTokenizer st = new StringTokenizer( query, " \t," );
-
-        QueryItem[] items = new QueryItem[st.countTokens()];
+    public QueryItem[] parseQuery( final String query) {
+        final StringTokenizer st = new StringTokenizer( query, " \t," );
+        final QueryItem[] items = new QueryItem[st.countTokens()];
         int word = 0;
 
         log.debug("Expecting "+items.length+" items");
 
-        //
         //  Parse incoming search string
-        //
-
-        while( st.hasMoreTokens() )
-        {
-            log.debug("Item "+word);
+        while( st.hasMoreTokens() ) {
+            log.debug( "Item " + word );
             String token = st.nextToken().toLowerCase();
 
-            items[word] = new QueryItem();
+            items[ word ] = new QueryItem();
 
-            switch( token.charAt(0) )
-            {
-              case '+':
-                items[word].type = QueryItem.REQUIRED;
-                token = token.substring(1);
-                log.debug("Required word: "+token);
+            switch( token.charAt( 0 ) ) {
+            case '+':
+                items[ word ].type = QueryItem.REQUIRED;
+                token = token.substring( 1 );
+                log.debug( "Required word: " + token );
                 break;
 
-              case '-':
-                items[word].type = QueryItem.FORBIDDEN;
-                token = token.substring(1);
-                log.debug("Forbidden word: "+token);
+            case '-':
+                items[ word ].type = QueryItem.FORBIDDEN;
+                token = token.substring( 1 );
+                log.debug( "Forbidden word: " + token );
                 break;
 
-              default:
-                items[word].type = QueryItem.REQUESTED;
-                log.debug("Requested word: "+token);
+            default:
+                items[ word ].type = QueryItem.REQUESTED;
+                log.debug( "Requested word: " + token );
                 break;
             }
 
-            items[word++].word = token;
+            items[ word++ ].word = token;
         }
 
         return items;
     }
 
-    private String attachmentNames(WikiPage page, String separator)
-    {
-        if(m_engine.getAttachmentManager().hasAttachments(page))
-        {
-            List< Attachment > attachments;
-            try
-            {
-                attachments = m_engine.getAttachmentManager().listAttachments(page);
-            }
-            catch (ProviderException e)
-            {
-                log.error("Unable to get attachments for page", e);
+    private String attachmentNames( final WikiPage page ) {
+        if( m_engine.getAttachmentManager().hasAttachments( page ) ) {
+            final List< Attachment > attachments;
+            try {
+                attachments = m_engine.getAttachmentManager().listAttachments( page );
+            } catch( final ProviderException e ) {
+                log.error( "Unable to get attachments for page", e );
                 return "";
             }
 
-            StringBuilder attachmentNames = new StringBuilder();
-            for( Iterator< Attachment > it = attachments.iterator(); it.hasNext(); )
-            {
-                Attachment att = it.next();
-                attachmentNames.append(att.getName());
-                if(it.hasNext()) {
-                    attachmentNames.append(separator);
+            final StringBuilder attachmentNames = new StringBuilder();
+            for( final Iterator< Attachment > it = attachments.iterator(); it.hasNext(); ) {
+                final Attachment att = it.next();
+                attachmentNames.append( att.getName() );
+                if( it.hasNext() ) {
+                    attachmentNames.append( " " );
                 }
             }
             return attachmentNames.toString();
@@ -149,42 +134,36 @@ public class BasicSearchProvider implements SearchProvider
         return "";
     }
 
-    private Collection< SearchResult > findPages( QueryItem[] query, WikiContext wikiContext )
-    {
-        TreeSet<SearchResult> res = new TreeSet<>( new SearchResultComparator() );
-        SearchMatcher matcher = new SearchMatcher( m_engine, query );
-
-        Collection< WikiPage > allPages = null;
+    private Collection< SearchResult > findPages( final QueryItem[] query, final WikiContext wikiContext ) {
+        final TreeSet< SearchResult > res = new TreeSet<>( new SearchResultComparator() );
+        final SearchMatcher matcher = new SearchMatcher( m_engine, query );
+        final Collection< WikiPage > allPages;
         try {
             allPages = m_engine.getPageManager().getAllPages();
-        } catch( ProviderException pe ) {
+        } catch( final ProviderException pe ) {
             log.error( "Unable to retrieve page list", pe );
             return null;
         }
 
-        AuthorizationManager mgr = m_engine.getAuthorizationManager();
+        final AuthorizationManager mgr = m_engine.getAuthorizationManager();
 
-        Iterator< WikiPage > it = allPages.iterator();
-        while( it.hasNext() ) {
+        for( final WikiPage page : allPages ) {
             try {
-                WikiPage page = it.next();
-                if (page != null) {
-                	
-                    PagePermission pp = new PagePermission( page, PagePermission.VIEW_ACTION );
-                    if( wikiContext==null || mgr.checkPermission( wikiContext.getWikiSession(), pp ) ) {
-	                    String pageName = page.getName();
-	                    String pageContent = m_engine.getPageManager().getPageText(pageName, WikiPageProvider.LATEST_VERSION) +
-	                                         attachmentNames(page, " ");
-	                    SearchResult comparison = matcher.matchPageContent( pageName, pageContent );
-	
-	                    if( comparison != null ) {
-	                        res.add( comparison );
-	                    }
-	                }
-	            }
-            } catch( ProviderException pe ) {
+                if( page != null ) {
+                    final PagePermission pp = new PagePermission( page, PagePermission.VIEW_ACTION );
+                    if( wikiContext == null || mgr.checkPermission( wikiContext.getWikiSession(), pp ) ) {
+                        final String pageName = page.getName();
+                        final String pageContent =
+                                m_engine.getPageManager().getPageText( pageName, WikiPageProvider.LATEST_VERSION ) + attachmentNames( page );
+                        final SearchResult comparison = matcher.matchPageContent( pageName, pageContent );
+                        if( comparison != null ) {
+                            res.add( comparison );
+                        }
+                    }
+                }
+            } catch( final ProviderException pe ) {
                 log.error( "Unable to retrieve page from cache", pe );
-            } catch( IOException ioe ) {
+            } catch( final IOException ioe ) {
                 log.error( "Failed to search page", ioe );
             }
         }
@@ -195,8 +174,8 @@ public class BasicSearchProvider implements SearchProvider
     /**
      *  {@inheritDoc}
      */
-    public Collection< SearchResult > findPages(String query, WikiContext wikiContext) {
-        return findPages(parseQuery(query), wikiContext);
+    public Collection< SearchResult > findPages( final String query, final WikiContext wikiContext ) {
+        return findPages( parseQuery( query ), wikiContext );
     }
 
     /**
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/QueryItem.java b/jspwiki-main/src/main/java/org/apache/wiki/search/QueryItem.java
index ad18b27..acbb74a 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/search/QueryItem.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/search/QueryItem.java
@@ -20,8 +20,7 @@ package org.apache.wiki.search;
 
 
 /**
- * This simple class just fulfils the role of a container for searches.  It tells the word and whether it is 
- * requested or not.
+ * This simple class just fulfils the role of a container for searches.  It tells the word and whether it is requested or not.
  */
 public class QueryItem {
 	
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchMatcher.java b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchMatcher.java
index 71c67ca..c741ac7 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchMatcher.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchMatcher.java
@@ -18,19 +18,18 @@
  */
 package org.apache.wiki.search;
 
-import java.io.IOException;
-import java.io.BufferedReader;
-import java.io.StringReader;
-
 import org.apache.wiki.WikiEngine;
 import org.apache.wiki.WikiPage;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+
 
 /**
- * SearchMatcher performs the task of matching a search query to a page's
- * contents. This utility class is isolated to simplify WikiPageProvider
- * implementations and to offer an easy target for upgrades. The upcoming(?)
- * TranslatorReader rewrite will presumably invalidate this, among other things.
+ * SearchMatcher performs the task of matching a search query to a page's contents. This utility class is isolated to simplify
+ * WikiPageProvider implementations and to offer an easy target for upgrades. The upcoming(?) TranslatorReader rewrite will
+ * presumably invalidate this, among other things.
  *
  * @since 2.1.5
  */
@@ -45,15 +44,14 @@ public class SearchMatcher {
      *  @param engine The WikiEngine
      *  @param queries A list of queries
      */
-    public SearchMatcher( WikiEngine engine, QueryItem[] queries ) {
+    public SearchMatcher( final WikiEngine engine, final QueryItem[] queries ) {
         m_engine = engine;
         m_queries = queries != null ? queries.clone() : null;
     }
 
     /**
-     * Compares the page content, available through the given stream, to the
-     * query items of this matcher. Returns a search result object describing
-     * the quality of the match.
+     * Compares the page content, available through the given stream, to the query items of this matcher. Returns a search result
+     * object describing the quality of the match.
      *
      * <p>This method would benefit of regexps (1.4) and streaming. FIXME!
      * 
@@ -62,14 +60,14 @@ public class SearchMatcher {
      * @return A SearchResult item, or null, there are no queries
      * @throws IOException If reading page content fails
      */
-    public SearchResult matchPageContent( String wikiname, String pageText ) throws IOException {
+    public SearchResult matchPageContent( final String wikiname, final String pageText ) throws IOException {
         if( m_queries == null ) {
             return null;
         }
 
-        int[] scores = new int[ m_queries.length ];
-        BufferedReader in = new BufferedReader( new StringReader( pageText ) );
-        String line = null;
+        final int[] scores = new int[ m_queries.length ];
+        final BufferedReader in = new BufferedReader( new StringReader( pageText ) );
+        String line;
 
         while( (line = in.readLine() ) != null ) {
             line = line.toLowerCase();
@@ -88,29 +86,21 @@ public class SearchMatcher {
             }
         }
 
-        //
         //  Check that we have all required words.
-        //
         int totalscore = 0;
 
         for( int j = 0; j < scores.length; j++ ) {
-            // Give five points for each occurrence
-            // of the word in the wiki name.
-
-            if( wikiname.toLowerCase().indexOf( m_queries[j].word ) != -1 && m_queries[j].type != QueryItem.FORBIDDEN ) {
+            // Give five points for each occurrence of the word in the wiki name.
+            if( wikiname.toLowerCase().contains( m_queries[ j ].word ) && m_queries[j].type != QueryItem.FORBIDDEN ) {
                 scores[j] += 5;
             }
 
-            //  Filter out pages if the search word is marked 'required'
-            //  but they have no score.
-
+            //  Filter out pages if the search word is marked 'required' but they have no score.
             if( m_queries[j].type == QueryItem.REQUIRED && scores[j] == 0 ) {
                 return null;
             }
 
-            //
             //  Count the total score for this page.
-            //
             totalscore += scores[j];
         }
 
@@ -135,7 +125,7 @@ public class SearchMatcher {
          *  @param name Page Name
          *  @param score A score from 0+
          */
-        public SearchResultImpl( String name, int score ) {
+        public SearchResultImpl( final String name, final int score ) {
             m_page  = new WikiPage( m_engine, name );
             m_score = score;
         }
@@ -158,8 +148,7 @@ public class SearchMatcher {
         }
 
         /**
-         *  Returns an empty array, since BasicSearchProvider does not support
-         *  context matching.
+         *  Returns an empty array, since BasicSearchProvider does not support context matching.
          *  
          *  @return an empty array
          */
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchProvider.java b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchProvider.java
index c72c488..1c2fc3c 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchProvider.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchProvider.java
@@ -18,26 +18,26 @@
  */
 package org.apache.wiki.search;
 
-import java.io.IOException;
-import java.util.Collection;
-
 import org.apache.wiki.WikiContext;
 import org.apache.wiki.WikiPage;
 import org.apache.wiki.WikiProvider;
 import org.apache.wiki.api.exceptions.ProviderException;
 
+import java.io.IOException;
+import java.util.Collection;
+
 /**
  *  Interface for the search providers that handle searching the Wiki
  *
  *  @since 2.2.21.
  */
-public interface SearchProvider extends WikiProvider
-{
+public interface SearchProvider extends WikiProvider {
+
     /**
      * Delete a page from the search index
      * @param page Page to remove from search index
      */
-    void pageRemoved(WikiPage page);
+    void pageRemoved( WikiPage page );
 
     /**
      *  Adds a WikiPage for indexing queue. This is called a queue, since
@@ -46,7 +46,7 @@ public interface SearchProvider extends WikiProvider
      *
      *  @param page The WikiPage to be indexed.
      */
-    void reindexPage(WikiPage page);
+    void reindexPage( WikiPage page );
 
     /**
      * Search for pages matching a search query
@@ -56,5 +56,6 @@ public interface SearchProvider extends WikiProvider
      * @throws ProviderException if the search provider failed.
      * @throws IOException if for some reason the query could not be executed.
      */
-    Collection< SearchResult > findPages(String query, WikiContext wikiContext) throws ProviderException, IOException;
+    Collection< SearchResult > findPages( String query, WikiContext wikiContext ) throws ProviderException, IOException;
+
 }
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResult.java b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResult.java
index 161c6bf..34b0d88 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResult.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResult.java
@@ -39,8 +39,7 @@ public interface SearchResult {
      *  @return A positive score value.  Note that there is no upper limit for the score.
      */
     int getScore();
-    
-    
+
     /**
      * Collection of XHTML fragments representing some contexts in which the match was made (a.k.a., "snippets").
      *
diff --git a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResultComparator.java b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResultComparator.java
index dd3a180..e727c06 100644
--- a/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResultComparator.java
+++ b/jspwiki-main/src/main/java/org/apache/wiki/search/SearchResultComparator.java
@@ -30,16 +30,13 @@ public class SearchResultComparator implements Comparator< SearchResult >, Seria
     private static final long serialVersionUID = 1L;
 
     /**
-     *  Compares two SearchResult objects, returning
-     *  the one that scored higher.
+     *  Compares two SearchResult objects, returning the one that scored higher.
      *  
      *  {@inheritDoc}
      */
-    public int compare( SearchResult s1, SearchResult s2 ) {
+    public int compare( final SearchResult s1, final SearchResult s2 ) {
         // Bigger scores are first.
-
         int res = s2.getScore() - s1.getScore();
-
         if( res == 0 ) {
             res = s1.getPage().getName().compareTo( s2.getPage().getName() );
         }