]> source.dussan.org Git - archiva.git/blob
c552e0f790549dde51543063ce197d3712528561
[archiva.git] /
1 package org.apache.maven.repository.indexing;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
27 import org.apache.lucene.analysis.SimpleAnalyzer;
28 import org.apache.lucene.analysis.StopAnalyzer;
29 import org.apache.lucene.analysis.standard.StandardAnalyzer;
30 import org.apache.lucene.document.Document;
31 import org.apache.lucene.queryParser.ParseException;
32 import org.apache.lucene.queryParser.QueryParser;
33 import org.apache.lucene.search.Hits;
34 import org.apache.lucene.search.IndexSearcher;
35 import org.apache.lucene.search.Query;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.artifact.DefaultArtifact;
38 import org.apache.maven.artifact.factory.ArtifactFactory;
39 import org.apache.maven.artifact.factory.DefaultArtifactFactory;
40 import org.apache.maven.artifact.handler.ArtifactHandler;
41 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
42 import org.apache.maven.artifact.repository.ArtifactRepository;
43 import org.apache.maven.artifact.versioning.VersionRange;
44
45 /**
46  * This class searches the index for existing artifacts that contains the
47  * specified query string.
48  * 
49  */
50 public class ArtifactRepositoryIndexSearcher
51     implements RepositoryIndexSearcher
52 {
53         private static final String NAME = "name";
54         private static final String GROUPID = "groupId";
55         private static final String ARTIFACTID = "artifactId";
56         private static final String VERSION = "version";
57         private static final String JAR_TYPE = "jar";
58         private static final String XML_TYPE = "xml";
59         private static final String POM_TYPE = "pom";
60
61         private IndexSearcher searcher;
62         private ArtifactRepository repository;
63         private ArtifactFactory factory;
64         
65         /**
66          * Constructor
67          * 
68          * @param indexPath
69          * @param repository
70          */
71         public ArtifactRepositoryIndexSearcher( String indexPath, ArtifactRepository repository )
72     {
73                 this.repository = repository;
74
75                 try
76         {
77                         searcher = new IndexSearcher( indexPath );
78         }
79         catch ( IOException ie )
80         {
81                         ie.printStackTrace();
82                 }
83         }
84         
85         protected Analyzer getAnalyzer()
86     {
87         return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
88     }
89
90         /**
91          * Search the artifact that contains the query string in the specified
92          * search field.
93          * 
94          * @param queryString
95          * @param searchField
96          * @return
97          */
98         public List searchArtifact( String queryString, String searchField )
99     {
100                 List artifactList = new ArrayList();
101
102                 try {
103             QueryParser parser = new QueryParser( searchField, getAnalyzer() );
104             Query qry = parser.parse( queryString );
105             Hits hits = searcher.search( qry );
106                          //System.out.println("HITS SIZE --> " + hits.length());
107
108                         for ( int i = 0; i < hits.length(); i++ )
109             {
110                                 Document doc = hits.doc( i );
111                                 // System.out.println("===========================");
112                                 // System.out.println("NAME :: " + (String) doc.get(NAME));
113                                 // System.out.println("GROUP ID :: " + (String)
114                                 // doc.get(GROUPID));
115                                 // System.out.println("ARTIFACT ID :: " + (String)
116                                 // doc.get(ARTIFACTID));
117                                 //System.out.println("VERSION :: " + (String)
118                                 // doc.get(VERSION));
119                                 // System.out.println("SHA! :: " + (String) doc.get(SHA1));
120                                 // System.out.println("MD5 :: " + (String) doc.get(MD5));
121                                 // System.out.println("CLASSES :: " + (String)
122                                 // doc.get(CLASSES));
123                                 // System.out.println("PACKAGES :: " + (String)
124                                 // doc.get(PACKAGES));
125                                 // System.out.println("FILES :: " + (String) doc.get(FILES));
126                                 // System.out.println("===========================");
127
128                                 String name = (String) doc.get( NAME );
129                                 String type = "";
130                                 if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( JAR_TYPE ) )
131                 {
132                                         type = JAR_TYPE;
133                 }
134                                 else if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( XML_TYPE ) ||
135                           ( name.substring( name.length() - 3 ).toLowerCase() ).equals( POM_TYPE ) )
136                 {
137                                         type = POM_TYPE;
138                 }
139
140                                 if ( type != null && type.length() > 0 )
141                 {
142                                         ArtifactHandler handler = new DefaultArtifactHandler( type );
143                                         VersionRange version = VersionRange.createFromVersion( (String) doc.get( VERSION ) );
144
145                                         Artifact artifact = new DefaultArtifact((String) doc.get( GROUPID ), (String) doc.get( ARTIFACTID ),
146                                                         version, "compile", type, "", handler );
147
148                                         /*
149                                          * Artifact artifact = factory.createArtifact((String)
150                                          * doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
151                                          * doc.get(VERSION), "", type);
152                                          */
153                                         artifact.setRepository( repository );
154                                         artifact.setFile( new File( repository.getBasedir() + "/" + (String) doc.get( NAME ) ) );
155
156                                         artifactList.add( artifact );
157                                 }
158                         }
159                 }
160         catch ( Exception e )
161         {
162                         e.printStackTrace();
163                 }
164
165                 return artifactList;
166         }
167 }