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
41 public Document convert( LuceneRepositoryContentRecord record )
43 if ( !( record instanceof FileContentRecord ) )
45 throw new ClassCastException( "Unable to convert type " + record.getClass().getName() + " to "
46 + FileContentRecord.class.getName() + "." );
49 FileContentRecord filecontent = (FileContentRecord) record;
51 LuceneDocumentMaker doc = new LuceneDocumentMaker( filecontent );
53 if( filecontent.getArtifact() != null )
56 doc.addFieldTokenized( ArtifactKeys.GROUPID, filecontent.getArtifact().getGroupId() );
57 doc.addFieldExact( ArtifactKeys.GROUPID_EXACT, filecontent.getArtifact().getGroupId() );
58 doc.addFieldTokenized( ArtifactKeys.ARTIFACTID, filecontent.getArtifact().getArtifactId() );
59 doc.addFieldExact( ArtifactKeys.ARTIFACTID_EXACT, filecontent.getArtifact().getArtifactId() );
60 doc.addFieldTokenized( ArtifactKeys.VERSION, filecontent.getArtifact().getVersion() );
61 doc.addFieldExact( ArtifactKeys.VERSION_EXACT, filecontent.getArtifact().getVersion() );
62 doc.addFieldTokenized( ArtifactKeys.TYPE, filecontent.getArtifact().getType() );
63 doc.addFieldUntokenized( ArtifactKeys.CLASSIFIER, filecontent.getArtifact().getClassifier() );
66 doc.addFieldTokenized( FileContentKeys.FILENAME, filecontent.getFilename() );
67 doc.addFieldTokenized( FileContentKeys.CONTENT, filecontent.getContents() );
69 return doc.getDocument();
72 public LuceneRepositoryContentRecord convert( Document document )
75 FileContentRecord record = new FileContentRecord();
77 record.setRepositoryId( document.get( LuceneDocumentMaker.REPOSITORY_ID ) );
80 String groupId = document.get( ArtifactKeys.GROUPID );
81 String artifactId = document.get( ArtifactKeys.ARTIFACTID );
82 String version = document.get( ArtifactKeys.VERSION );
83 String classifier = document.get( ArtifactKeys.CLASSIFIER );
84 String type = document.get( ArtifactKeys.TYPE );
86 if( StringUtils.isNotBlank( groupId ) && StringUtils.isNotBlank( artifactId ) )
88 ArchivaArtifact artifact = new ArchivaArtifact( groupId, artifactId, version, classifier, type );
89 record.setArtifact( artifact );
92 // Filecontent Specifics
93 record.setFilename( document.get( FileContentKeys.FILENAME ) );
94 record.setContents( document.get( FileContentKeys.CONTENT ) );