1 package org.apache.maven.archiva.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.archiva.indexing.record.MinimalArtifactIndexRecord;
24 import org.apache.maven.archiva.indexing.record.MinimalIndexRecordFields;
25 import org.apache.maven.archiva.indexing.record.RepositoryIndexRecord;
26 import org.codehaus.plexus.util.StringUtils;
28 import java.text.ParseException;
29 import java.util.Arrays;
32 * Convert the minimal index record to a Lucene document.
34 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36 public class LuceneMinimalIndexRecordConverter
37 implements LuceneIndexRecordConverter
39 public Document convert( RepositoryIndexRecord record )
41 MinimalArtifactIndexRecord rec = (MinimalArtifactIndexRecord) record;
43 Document document = new Document();
44 addTokenizedField( document, MinimalIndexRecordFields.FILENAME, rec.getFilename() );
45 addUntokenizedField( document, MinimalIndexRecordFields.LAST_MODIFIED,
46 DateTools.timeToString( rec.getLastModified(), DateTools.Resolution.SECOND ) );
47 addUntokenizedField( document, MinimalIndexRecordFields.FILE_SIZE, NumberTools.longToString( rec.getSize() ) );
48 addUntokenizedField( document, MinimalIndexRecordFields.MD5, rec.getMd5Checksum() );
49 addTokenizedField( document, MinimalIndexRecordFields.CLASSES,
50 StringUtils.join( rec.getClasses().iterator(), "\n" ) );
55 public RepositoryIndexRecord convert( Document document )
58 MinimalArtifactIndexRecord record = new MinimalArtifactIndexRecord();
60 record.setFilename( document.get( MinimalIndexRecordFields.FILENAME ) );
61 record.setLastModified( DateTools.stringToTime( document.get( MinimalIndexRecordFields.LAST_MODIFIED ) ) );
62 record.setSize( NumberTools.stringToLong( document.get( MinimalIndexRecordFields.FILE_SIZE ) ) );
63 record.setMd5Checksum( document.get( MinimalIndexRecordFields.MD5 ) );
64 record.setClasses( Arrays.asList( document.get( MinimalIndexRecordFields.CLASSES ).split( "\n" ) ) );
69 private static void addUntokenizedField( Document document, String name, String value )
73 document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
77 private static void addTokenizedField( Document document, String name, String value )
81 document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );