]> source.dussan.org Git - archiva.git/blob
58a71bd4de1acd6a0cd96129a525202fbbda61d1
[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 ArtifactRepositorySearcher implements RepositorySearcher {
51
52         private IndexSearcher searcher;
53         private ArtifactRepository repository;
54         private ArtifactFactory factory;
55         private static final String NAME = "name";
56         private static final String GROUPID = "groupId";
57         private static final String ARTIFACTID = "artifactId";
58         private static final String VERSION = "version";
59         private static final String JAR_TYPE = "jar";
60         private static final String XML_TYPE = "xml";
61         private static final String POM_TYPE = "pom";
62         
63         /**
64          * Constructor
65          * 
66          * @param indexPath
67          * @param repository
68          */
69         public ArtifactRepositorySearcher(String indexPath,
70                         ArtifactRepository repository) {
71
72                 this.repository = repository;
73                 factory = new DefaultArtifactFactory();
74
75                 try {
76                         searcher = new IndexSearcher(indexPath);
77                 } catch (IOException ie) {
78                         ie.printStackTrace();
79                 }
80         }
81         
82         protected Analyzer getAnalyzer()
83     {
84         //PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new SimpleAnalyzer());
85         //wrapper.addAnalyzer(VERSION, new StandardAnalyzer());
86         
87                 //return wrapper;
88         return new ArtifactRepositoryIndexAnalyzer(new SimpleAnalyzer());
89     }
90
91         /**
92          * Search the artifact that contains the query string in the specified
93          * search field.
94          * 
95          * @param queryString
96          * @param searchField
97          * @return
98          */
99         public List searchArtifact(String queryString, String searchField) {
100
101                 
102                 QueryParser parser = new QueryParser(searchField,
103                                 getAnalyzer());
104                 Query qry = null;
105                 List artifactList = new ArrayList();
106
107                 try {
108                         qry = parser.parse(queryString);
109                 } catch (ParseException pe) {
110                         pe.printStackTrace();
111                         return artifactList;
112                 }
113
114                 try {
115                         Hits hits = searcher.search(qry);
116                          //System.out.println("HITS SIZE --> " + hits.length());
117
118                         for (int i = 0; i < hits.length(); i++) {
119                                 Document doc = hits.doc(i);
120                                 // System.out.println("===========================");
121                                 // System.out.println("NAME :: " + (String) doc.get(NAME));
122                                 // System.out.println("GROUP ID :: " + (String)
123                                 // doc.get(GROUPID));
124                                 // System.out.println("ARTIFACT ID :: " + (String)
125                                 // doc.get(ARTIFACTID));
126                                 //System.out.println("VERSION :: " + (String)
127                                 // doc.get(VERSION));
128                                 // System.out.println("SHA! :: " + (String) doc.get(SHA1));
129                                 // System.out.println("MD5 :: " + (String) doc.get(MD5));
130                                 // System.out.println("CLASSES :: " + (String)
131                                 // doc.get(CLASSES));
132                                 // System.out.println("PACKAGES :: " + (String)
133                                 // doc.get(PACKAGES));
134                                 // System.out.println("FILES :: " + (String) doc.get(FILES));
135                                 // System.out.println("===========================");
136
137                                 String name = (String) doc.get(NAME);
138                                 String type = "";
139                                 if ((name.substring(name.length() - 3).toLowerCase())
140                                                 .equals(JAR_TYPE))
141                                         type = JAR_TYPE;
142                                 else if ((name.substring(name.length() - 3).toLowerCase())
143                                                 .equals(XML_TYPE)
144                                                 || (name.substring(name.length() - 3).toLowerCase())
145                                                                 .equals(POM_TYPE))
146                                         type = POM_TYPE;
147
148                                 if (!type.equals("") && type != null) {
149                                         ArtifactHandler handler = new DefaultArtifactHandler(type);
150                                         VersionRange version = VersionRange
151                                                         .createFromVersion((String) doc.get(VERSION));
152
153                                         Artifact artifact = new DefaultArtifact((String) doc
154                                                         .get(GROUPID), (String) doc.get(ARTIFACTID),
155                                                         version, "compile", type, "", handler);
156
157                                         /*
158                                          * Artifact artifact = factory.createArtifact((String)
159                                          * doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
160                                          * doc.get(VERSION), "", type);
161                                          */
162                                         artifact.setRepository(repository);
163                                         artifact.setFile(new File(repository.getBasedir() + "/"
164                                                         + (String) doc.get(NAME)));
165
166                                         artifactList.add(artifact);
167                                 }
168                         }
169
170                 } catch (IOException ie) {
171                         ie.printStackTrace();
172                         return artifactList;
173                 }
174
175                 return artifactList;
176         }
177 }