1 package org.apache.maven.archiva.indexer.filecontent;
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.maven.archiva.indexer.ArtifactKeys;
25 import org.apache.maven.archiva.indexer.lucene.LuceneDocumentMaker;
26 import org.apache.maven.archiva.indexer.lucene.LuceneEntryConverter;
27 import org.apache.maven.archiva.indexer.lucene.LuceneRepositoryContentRecord;
28 import org.apache.maven.archiva.model.ArchivaArtifact;
30 import java.text.ParseException;
33 * FileContentConverter
37 public class FileContentConverter
38 implements LuceneEntryConverter
40 public Document convert( LuceneRepositoryContentRecord record )
42 if ( !( record instanceof FileContentRecord ) )
44 throw new ClassCastException( "Unable to convert type " + record.getClass().getName() + " to "
45 + FileContentRecord.class.getName() + "." );
48 FileContentRecord filecontent = (FileContentRecord) record;
50 LuceneDocumentMaker doc = new LuceneDocumentMaker( filecontent );
52 if( filecontent.getArtifact() != null )
55 doc.addFieldTokenized( ArtifactKeys.GROUPID, filecontent.getArtifact().getGroupId() );
56 doc.addFieldExact( ArtifactKeys.GROUPID_EXACT, filecontent.getArtifact().getGroupId() );
57 doc.addFieldTokenized( ArtifactKeys.ARTIFACTID, filecontent.getArtifact().getArtifactId() );
58 doc.addFieldExact( ArtifactKeys.ARTIFACTID_EXACT, filecontent.getArtifact().getArtifactId() );
59 doc.addFieldTokenized( ArtifactKeys.VERSION, filecontent.getArtifact().getVersion() );
60 doc.addFieldExact( ArtifactKeys.VERSION_EXACT, filecontent.getArtifact().getVersion() );
61 doc.addFieldTokenized( ArtifactKeys.TYPE, filecontent.getArtifact().getType() );
62 doc.addFieldUntokenized( ArtifactKeys.CLASSIFIER, filecontent.getArtifact().getClassifier() );
65 doc.addFieldTokenized( FileContentKeys.FILENAME, filecontent.getFilename() );
67 return doc.getDocument();
70 public LuceneRepositoryContentRecord convert( Document document )
73 FileContentRecord record = new FileContentRecord();
75 record.setRepositoryId( document.get( LuceneDocumentMaker.REPOSITORY_ID ) );
78 String groupId = document.get( ArtifactKeys.GROUPID );
79 String artifactId = document.get( ArtifactKeys.ARTIFACTID );
80 String version = document.get( ArtifactKeys.VERSION );
81 String classifier = document.get( ArtifactKeys.CLASSIFIER );
82 String type = document.get( ArtifactKeys.TYPE );
84 if( StringUtils.isNotBlank( groupId ) && StringUtils.isNotBlank( artifactId ) )
86 ArchivaArtifact artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type );
87 record.setArtifact( artifact );
90 // Filecontent Specifics
91 record.setFilename( document.get( FileContentKeys.FILENAME ) );