1 package org.apache.maven.archiva.indexer.lucene;
4 * Copyright 2005-2006 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
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.lucene.document.DateTools;
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.document.NumberTools;
23 import org.apache.maven.archiva.indexer.record.RepositoryIndexRecord;
24 import org.apache.maven.archiva.indexer.record.StandardArtifactIndexRecord;
25 import org.apache.maven.archiva.indexer.record.StandardIndexRecordFields;
26 import org.apache.commons.lang.StringUtils;
28 import java.text.ParseException;
29 import java.util.Arrays;
32 * Convert the standard index record to a Lucene document.
34 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36 public class LuceneStandardIndexRecordConverter
37 implements LuceneIndexRecordConverter
39 public Document convert( RepositoryIndexRecord record )
41 StandardArtifactIndexRecord rec = (StandardArtifactIndexRecord) record;
43 Document document = new Document();
44 addTokenizedField( document, StandardIndexRecordFields.FILENAME, rec.getFilename() );
45 addTokenizedField( document, StandardIndexRecordFields.GROUPID, rec.getGroupId() );
46 addExactField( document, StandardIndexRecordFields.GROUPID_EXACT, rec.getGroupId() );
47 addTokenizedField( document, StandardIndexRecordFields.ARTIFACTID, rec.getArtifactId() );
48 addExactField( document, StandardIndexRecordFields.ARTIFACTID_EXACT, rec.getArtifactId() );
49 addTokenizedField( document, StandardIndexRecordFields.VERSION, rec.getVersion() );
50 addExactField( document, StandardIndexRecordFields.VERSION_EXACT, rec.getVersion() );
51 addTokenizedField( document, StandardIndexRecordFields.BASE_VERSION, rec.getBaseVersion() );
52 addExactField( document, StandardIndexRecordFields.BASE_VERSION_EXACT, rec.getBaseVersion() );
53 addUntokenizedField( document, StandardIndexRecordFields.TYPE, rec.getType() );
54 addTokenizedField( document, StandardIndexRecordFields.CLASSIFIER, rec.getClassifier() );
55 addUntokenizedField( document, StandardIndexRecordFields.PACKAGING, rec.getPackaging() );
56 addUntokenizedField( document, StandardIndexRecordFields.REPOSITORY, rec.getRepository() );
57 addUntokenizedField( document, StandardIndexRecordFields.LAST_MODIFIED,
58 DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) );
59 addUntokenizedField( document, StandardIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) );
60 addUntokenizedField( document, StandardIndexRecordFields.MD5, rec.getMd5Checksum() );
61 addUntokenizedField( document, StandardIndexRecordFields.SHA1, rec.getSha1Checksum() );
62 if ( rec.getClasses() != null )
64 addTokenizedField( document, StandardIndexRecordFields.CLASSES,
65 StringUtils.join( rec.getClasses().iterator(), "\n" ) );
67 if ( rec.getFiles() != null )
69 addTokenizedField( document, StandardIndexRecordFields.FILES,
70 StringUtils.join( rec.getFiles().iterator(), "\n" ) );
72 addUntokenizedField( document, StandardIndexRecordFields.PLUGIN_PREFIX, rec.getPluginPrefix() );
73 addUntokenizedField( document, StandardIndexRecordFields.INCEPTION_YEAR, rec.getInceptionYear() );
74 addTokenizedField( document, StandardIndexRecordFields.PROJECT_NAME, rec.getProjectName() );
75 addTokenizedField( document, StandardIndexRecordFields.PROJECT_DESCRIPTION, rec.getProjectDescription() );
76 if ( rec.getDependencies() != null )
78 addTokenizedField( document, StandardIndexRecordFields.DEPENDENCIES,
79 StringUtils.join( rec.getDependencies().iterator(), "\n" ) );
81 if ( rec.getDevelopers() != null )
83 addTokenizedField( document, StandardIndexRecordFields.DEVELOPERS,
84 StringUtils.join( rec.getDevelopers().iterator(), "\n" ) );
87 document.add( Field.Keyword( StandardIndexRecordFields.FLD_LICENSE_URLS, "" ) );
88 document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_REPORT, "" ) );
89 document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_BUILD, "" ) );
95 public RepositoryIndexRecord convert( Document document )
98 StandardArtifactIndexRecord record = new StandardArtifactIndexRecord();
100 record.setFilename( document.get( StandardIndexRecordFields.FILENAME ) );
101 record.setGroupId( document.get( StandardIndexRecordFields.GROUPID ) );
102 record.setArtifactId( document.get( StandardIndexRecordFields.ARTIFACTID ) );
103 record.setVersion( document.get( StandardIndexRecordFields.VERSION ) );
104 record.setBaseVersion( document.get( StandardIndexRecordFields.BASE_VERSION ) );
105 record.setType( document.get( StandardIndexRecordFields.TYPE ) );
106 record.setClassifier( document.get( StandardIndexRecordFields.CLASSIFIER ) );
107 record.setPackaging( document.get( StandardIndexRecordFields.PACKAGING ) );
108 record.setRepository( document.get( StandardIndexRecordFields.REPOSITORY ) );
109 record.setLastModified( DateTools.stringToTime( document.get( StandardIndexRecordFields.LAST_MODIFIED ) ) );
110 record.setSize( NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
111 record.setMd5Checksum( document.get( StandardIndexRecordFields.MD5 ) );
112 record.setSha1Checksum( document.get( StandardIndexRecordFields.SHA1 ) );
113 String classes = document.get( StandardIndexRecordFields.CLASSES );
114 if ( classes != null )
116 record.setClasses( Arrays.asList( classes.split( "\n" ) ) );
118 String files = document.get( StandardIndexRecordFields.FILES );
121 record.setFiles( Arrays.asList( files.split( "\n" ) ) );
123 String dependencies = document.get( StandardIndexRecordFields.DEPENDENCIES );
124 if ( dependencies != null )
126 record.setDependencies( Arrays.asList( dependencies.split( "\n" ) ) );
128 String developers = document.get( StandardIndexRecordFields.DEVELOPERS );
129 if ( developers != null )
131 record.setDevelopers( Arrays.asList( developers.split( "\n" ) ) );
133 record.setPluginPrefix( document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
134 record.setInceptionYear( document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
135 record.setProjectName( document.get( StandardIndexRecordFields.PROJECT_NAME ) );
136 record.setProjectDescription( document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
141 private static void addUntokenizedField( Document document, String name, String value )
145 document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
149 private static void addExactField( Document document, String name, String value )
153 document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) );
157 private static void addTokenizedField( Document document, String name, String value )
161 document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );