📄 lucenepdfdocument.java
字号:
/**
* Copyright (c) 2003-2006, www.pdfbox.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of pdfbox; nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://www.pdfbox.org
*
*/
package org.pdfbox.searchengine.lucene;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Calendar;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.exceptions.CryptographyException;
import org.pdfbox.exceptions.InvalidPasswordException;
import org.pdfbox.util.PDFTextStripper;
/**
* This class is used to create a document for the lucene search engine.
* This should easily plug into the IndexHTML or IndexFiles that comes with
* the lucene project. This class will populate the following fields.
* <table>
* <tr>
* <th>Lucene Field Name</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>path</td>
* <td>File system path if loaded from a file</td>
* </tr>
* <tr>
* <td>url</td>
* <td>URL to PDF document</td>
* </tr>
* <tr>
* <td>contents</td>
* <td>Entire contents of PDF document, indexed but not stored</td>
* </tr>
* <tr>
* <td>summary</td>
* <td>First 500 characters of content</td>
* </tr>
* <tr>
* <td>modified</td>
* <td>The modified date/time according to the url or path</td>
* </tr>
* <tr>
* <td>uid</td>
* <td>A unique identifier for the Lucene document.</td>
* </tr>
* <tr>
* <td>CreationDate</td>
* <td>From PDF meta-data if available</td>
* </tr>
* <tr>
* <td>Creator</td>
* <td>From PDF meta-data if available</td>
* </tr>
* <tr>
* <td>Keywords</td>
* <td>From PDF meta-data if available</td>
* </tr>
* <tr>
* <td>ModificationDate</td>
* <td>From PDF meta-data if available</td>
* </tr>
* <tr>
* <td>Producer</td>
* <td>From PDF meta-data if available</td>
* </tr>
* <tr>
* <td>Subject</td>
* <td>From PDF meta-data if available</td>
* </tr>
* <tr>
* <td>Trapped</td>
* <td>From PDF meta-data if available</td>
* </tr>
* </table>
*
* @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
* @version $Revision: 1.22 $
*/
public final class LucenePDFDocument
{
private static final char FILE_SEPARATOR = System.getProperty("file.separator").charAt(0);
// given caveat of increased search times when using
//MICROSECOND, only use SECOND by default
private DateTools.Resolution dateTimeResolution = DateTools.Resolution.SECOND;
private PDFTextStripper stripper = null;
/**
* Constructor.
*/
public LucenePDFDocument()
{
}
/**
* Set the text stripper that will be used during extraction.
*
* @param aStripper The new pdf text stripper.
*/
public void setTextStripper( PDFTextStripper aStripper )
{
stripper = aStripper;
}
/**
* Get the Lucene data time resolution.
*
* @return current date/time resolution
*/
public DateTools.Resolution getDateTimeResolution()
{
return dateTimeResolution;
}
/**
* Set the Lucene data time resolution.
*
* @param resolution set new date/time resolution
*/
public void setDateTimeResolution( DateTools.Resolution resolution )
{
dateTimeResolution = resolution;
}
//
// compatibility methods for lucene-1.9+
//
private String timeToString( long time )
{
return DateTools.timeToString( time, dateTimeResolution );
}
private void addKeywordField( Document document, String name, String value )
{
if ( value != null )
{
document.add( new Field( name, value, Field.Store.YES, Field.Index.UN_TOKENIZED ) );
}
}
private void addTextField( Document document, String name, Reader value )
{
if ( value != null )
{
document.add( new Field( name, value ) );
}
}
private void addTextField( Document document, String name, String value )
{
if ( value != null )
{
document.add( new Field( name, value, Field.Store.YES, Field.Index.TOKENIZED ) );
}
}
private void addTextField( Document document, String name, Date value )
{
if ( value != null )
{
addTextField( document, name, DateTools.dateToString( value, dateTimeResolution ) );
}
}
private void addTextField( Document document, String name, Calendar value )
{
if ( value != null )
{
addTextField( document, name, value.getTime() );
}
}
private static void addUnindexedField( Document document, String name, String value )
{
if ( value != null )
{
document.add( new Field( name, value, Field.Store.YES, Field.Index.NO ) );
}
}
private void addUnstoredKeywordField( Document document, String name, String value )
{
if ( value != null )
{
document.add( new Field( name, value, Field.Store.NO, Field.Index.UN_TOKENIZED ) );
}
}
/**
* Convert the PDF stream to a lucene document.
*
* @param is The input stream.
* @return The input stream converted to a lucene document.
* @throws IOException If there is an error converting the PDF.
*/
public Document convertDocument( InputStream is ) throws IOException
{
Document document = new Document();
addContent( document, is, "<inputstream>" );
return document;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -