1 package org.apache.maven.archiva.indexer.lucene;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
26 import java.util.List;
29 * LuceneDocumentMaker - a utility class for making lucene documents.
33 public class LuceneDocumentMaker
35 public static final String PRIMARY_KEY = "pk";
37 public static final String REPOSITORY_ID = "repoId";
39 private Document document;
42 * Construct a LuceneDocumentMaker based on the record provider.
44 * @param record the record.
45 * @throws IllegalArgumentException if the primary key is invalid.
47 public LuceneDocumentMaker( LuceneRepositoryContentRecord record ) throws IllegalArgumentException
51 throw new IllegalArgumentException( "Not allowed to have a null record provider." );
54 String primaryKey = record.getPrimaryKey();
56 if ( StringUtils.isBlank( primaryKey ) )
58 throw new IllegalArgumentException( "Not allowed to have a blank primary key." );
61 String repositoryId = record.getRepositoryId();
63 if ( StringUtils.isBlank( repositoryId ) )
65 throw new IllegalArgumentException( "Not allowed to have a blank repository id." );
68 document = new Document();
70 document.add( new Field( PRIMARY_KEY, primaryKey, Field.Store.NO, Field.Index.UN_TOKENIZED ) );
71 document.add( new Field( REPOSITORY_ID, repositoryId, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
74 public LuceneDocumentMaker addFieldTokenized( String key, String value )
78 document.add( new Field( key, value, Field.Store.YES, Field.Index.TOKENIZED ) );
84 public LuceneDocumentMaker addFieldTokenized( String key, List list )
86 if ( ( list != null ) && ( !list.isEmpty() ) )
88 return addFieldTokenized( key, StringUtils.join( list.iterator(), "\n" ) );
94 public LuceneDocumentMaker addFieldUntokenized( String name, String value )
98 document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
104 public LuceneDocumentMaker addFieldExact( String name, String value )
108 document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) );
114 public Document getDocument()
116 return this.document;