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 ArtifactRepositoryIndexSearcher
51 implements RepositoryIndexSearcher
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";
61 private IndexSearcher searcher;
62 private ArtifactRepository repository;
63 private ArtifactFactory factory;
71 public ArtifactRepositoryIndexSearcher( String indexPath, ArtifactRepository repository )
73 this.repository = repository;
77 searcher = new IndexSearcher( indexPath );
79 catch ( IOException ie )
85 protected Analyzer getAnalyzer()
87 return new ArtifactRepositoryIndexAnalyzer( new SimpleAnalyzer() );
91 * Search the artifact that contains the query string in the specified
98 public List searchArtifact( String queryString, String searchField )
100 List artifactList = new ArrayList();
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());
108 for ( int i = 0; i < hits.length(); i++ )
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("===========================");
128 String name = (String) doc.get( NAME );
130 if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( JAR_TYPE ) )
134 else if ( ( name.substring( name.length() - 3 ).toLowerCase() ).equals( XML_TYPE ) ||
135 ( name.substring( name.length() - 3 ).toLowerCase() ).equals( POM_TYPE ) )
140 if ( type != null && type.length() > 0 )
142 ArtifactHandler handler = new DefaultArtifactHandler( type );
143 VersionRange version = VersionRange.createFromVersion( (String) doc.get( VERSION ) );
145 Artifact artifact = new DefaultArtifact((String) doc.get( GROUPID ), (String) doc.get( ARTIFACTID ),
146 version, "compile", type, "", handler );
149 * Artifact artifact = factory.createArtifact((String)
150 * doc.get(GROUPID), (String) doc.get(ARTIFACTID), (String)
151 * doc.get(VERSION), "", type);
153 artifact.setRepository( repository );
154 artifact.setFile( new File( repository.getBasedir() + "/" + (String) doc.get( NAME ) ) );
156 artifactList.add( artifact );
160 catch ( Exception e )