⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 extensionfilehandler.java

📁 LuceneInAction配套源码,LuceneInAction是对lucene api的详细讲解及具体应用.此源码即应用例子
💻 JAVA
字号:
package lia.handlingtypes.framework;import org.apache.lucene.document.Document;import java.io.File;import java.io.IOException;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.util.Properties;/** * A FileHandler implementation that delegates responsibility to * appropriate DocumentHandler implementation, based on a file * extension. */public class ExtensionFileHandler implements FileHandler {  private Properties handlerProps;  public ExtensionFileHandler(Properties props) {    handlerProps = props;  }  public Document getDocument(File file)    throws FileHandlerException {    String name = file.getName();    int dotIndex = name.indexOf(".");    if ((dotIndex > 0) && (dotIndex < name.length())) {      String ext = name.substring(dotIndex + 1, name.length());      String handlerClassName = handlerProps.getProperty(ext);      if (handlerClassName == null)        return null;      try {        Class handlerClass = Class.forName(handlerClassName);        DocumentHandler handler =          (DocumentHandler) handlerClass.newInstance();        return handler.getDocument(new FileInputStream(file));      }      catch (ClassNotFoundException e) {        throw new FileHandlerException(          "Cannot create instance of : "          + handlerClassName, e);      }      catch (InstantiationException e) {        throw new FileHandlerException(          "Cannot create instance of : "          + handlerClassName, e);      }      catch (IllegalAccessException e) {        throw new FileHandlerException(          "Cannot create instance of : "          + handlerClassName, e);      }      catch (FileNotFoundException e) {        throw new FileHandlerException(          "File not found: "          + file.getAbsolutePath(), e);      }      catch (DocumentHandlerException e) {        throw new FileHandlerException(          "Document cannot be handler: "          + file.getAbsolutePath(), e);      }    }    return null;  }  public static void main(String[] args) throws Exception {    if (args.length < 2) {      usage();      System.exit(0);    }    Properties props = new Properties();    props.load(new FileInputStream(args[0]));    ExtensionFileHandler fileHandler =      new ExtensionFileHandler(props);    Document doc = fileHandler.getDocument(new File(args[1]));    System.out.println(doc);  }  private static void usage() {    System.err.println("USAGE: java "      + ExtensionFileHandler.class.getName()      + " /path/to/properties /path/to/document");  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -