1 package org.apache.maven.repository.indexing.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.repository.indexing.record.RepositoryIndexRecord;
24 import org.apache.maven.repository.indexing.record.StandardArtifactIndexRecord;
25 import org.apache.maven.repository.indexing.record.StandardIndexRecordFields;
26 import org.codehaus.plexus.util.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() );
77 document.add( Field.Keyword( StandardIndexRecordFields.FLD_LICENSE_URLS, "" ) );
78 document.add( Field.Keyword( StandardIndexRecordFields.FLD_DEPENDENCIES, "" ) );
79 document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_REPORT, "" ) );
80 document.add( Field.Keyword( StandardIndexRecordFields.FLD_PLUGINS_BUILD, "" ) );
86 public RepositoryIndexRecord convert( Document document )
89 StandardArtifactIndexRecord record = new StandardArtifactIndexRecord();
91 record.setFilename( document.get( StandardIndexRecordFields.FILENAME ) );
92 record.setGroupId( document.get( StandardIndexRecordFields.GROUPID ) );
93 record.setArtifactId( document.get( StandardIndexRecordFields.ARTIFACTID ) );
94 record.setVersion( document.get( StandardIndexRecordFields.VERSION ) );
95 record.setBaseVersion( document.get( StandardIndexRecordFields.BASE_VERSION ) );
96 record.setType( document.get( StandardIndexRecordFields.TYPE ) );
97 record.setClassifier( document.get( StandardIndexRecordFields.CLASSIFIER ) );
98 record.setPackaging( document.get( StandardIndexRecordFields.PACKAGING ) );
99 record.setRepository( document.get( StandardIndexRecordFields.REPOSITORY ) );
100 record.setLastModified( DateTools.stringToTime( document.get( StandardIndexRecordFields.LAST_MODIFIED ) ) );
101 record.setSize( NumberTools.stringToLong( document.get( StandardIndexRecordFields.FILE_SIZE ) ) );
102 record.setMd5Checksum( document.get( StandardIndexRecordFields.MD5 ) );
103 record.setSha1Checksum( document.get( StandardIndexRecordFields.SHA1 ) );
104 String classes = document.get( StandardIndexRecordFields.CLASSES );
105 if ( classes != null )
107 record.setClasses( Arrays.asList( classes.split( "\n" ) ) );
109 String files = document.get( StandardIndexRecordFields.FILES );
112 record.setFiles( Arrays.asList( files.split( "\n" ) ) );
114 record.setPluginPrefix( document.get( StandardIndexRecordFields.PLUGIN_PREFIX ) );
115 record.setInceptionYear( document.get( StandardIndexRecordFields.INCEPTION_YEAR ) );
116 record.setProjectName( document.get( StandardIndexRecordFields.PROJECT_NAME ) );
117 record.setProjectDescription( document.get( StandardIndexRecordFields.PROJECT_DESCRIPTION ) );
122 private static void addUntokenizedField( Document document, String name, String value )
126 document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
130 private static void addExactField( Document document, String name, String value )
134 document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) );
138 private static void addTokenizedField( Document document, String name, String value )
142 document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );