1 package org.apache.maven.repository.indexing;
4 * Copyright 2001-2005 The Apache Software Foundation.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
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;
46 * This class searches the index for existing artifacts that contains the
47 * specified query string.
50 public class ArtifactRepositorySearcher implements RepositorySearcher {
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";
69 public ArtifactRepositorySearcher(String indexPath,
70 ArtifactRepository repository) {
72 this.repository = repository;
73 factory = new DefaultArtifactFactory();
76 searcher = new IndexSearcher(indexPath);
77 } catch (IOException ie) {
82 protected Analyzer getAnalyzer()
84 //PerFieldAnalyzerWrapper wrapper = new PerFieldAnalyzerWrapper(new SimpleAnalyzer());
85 //wrapper.addAnalyzer(VERSION, new StandardAnalyzer());
88 return new ArtifactRepositoryIndexAnalyzer(new SimpleAnalyzer());
92 * Search the artifact that contains the query string in the specified
99 public List searchArtifact(String queryString, String searchField) {
102 QueryParser parser = new QueryParser(searchField,
105 List artifactList = new ArrayList();
108 qry = parser.parse(queryString);
109 } catch (ParseException pe) {
110 pe.printStackTrace();
115 Hits hits = searcher.search(qry);
116 //System.out.println("HITS SIZE --> " + hits.length());
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("===========================");
137 String name = (String) doc.get(NAME);
139 if ((name.substring(name.length() - 3).toLowerCase())
142 else if ((name.substring(name.length() - 3).toLowerCase())
144 || (name.substring(name.length() - 3).toLowerCase())
148 if (!type.equals("") && type != null) {
149 ArtifactHandler handler = new DefaultArtifactHandler(type);
150 VersionRange version = VersionRange
151 .createFromVersion((String) doc.get(VERSION));
153 Artifact artifact = new DefaultArtifact((String) doc
154 .get(GROUPID), (String) doc.get(ARTIFACTID),
155 version, "compile", type, "", handler);
158 * Artifact artifact = factory.createArtifact((String)
159 * doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
160 * doc.get(VERSION), "", type);
162 artifact.setRepository(repository);
163 artifact.setFile(new File(repository.getBasedir() + "/"
164 + (String) doc.get(NAME)));
166 artifactList.add(artifact);
170 } catch (IOException ie) {
171 ie.printStackTrace();