DiskWriterPlugin.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:4k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.mod.plugin.diskwriter;
  2. import net.javacoding.jspider.api.event.JSpiderEvent;
  3. import net.javacoding.jspider.api.event.resource.ResourceFetchedEvent;
  4. import net.javacoding.jspider.core.logging.Log;
  5. import net.javacoding.jspider.core.logging.LogFactory;
  6. import net.javacoding.jspider.core.util.config.*;
  7. import net.javacoding.jspider.spi.Plugin;
  8. import java.io.*;
  9. import java.net.URL;
  10. import java.util.StringTokenizer;
  11. /**
  12.  * $Id: DiskWriterPlugin.java,v 1.13 2003/04/09 17:08:14 vanrogu Exp $
  13.  *
  14.  * $Id: DiskWriterPlugin.java,v 1.13 2003/04/09 17:08:14 vanrogu Exp $
  15.  */
  16. public class DiskWriterPlugin implements Plugin {
  17.     public static final String MODULE_NAME = "DiskWriter JSpider plugin";
  18.     public static final String MODULE_VERSION = "v1.0";
  19.     public static final String MODULE_DESCRIPTION = "A JSpider plugin that allows the mirroring of web resources";
  20.     public static final String MODULE_VENDOR = "http://www.javacoding.net";
  21.     public static final String OUTPUT_ABSOLUTE = "output.absolute";
  22.     public static final String OUTPUT_FOLDER = "output.folder";
  23.     public static final String DEFAULT_OUTPUT_FOLDER = ".";
  24.     protected Log log;
  25.     protected File outputFolder;
  26.     protected File baseFolder;
  27.     protected PrintWriter pw;
  28.     public DiskWriterPlugin(PropertySet config) {
  29.         log = LogFactory.getLog(DiskWriterPlugin.class);
  30.         JSpiderConfiguration jspiderConfig = ConfigurationFactory.getConfiguration();
  31.         File defaultOutputFolder = jspiderConfig.getDefaultOutputFolder();
  32.         if (config.getBoolean(OUTPUT_ABSOLUTE, false)) {
  33.             outputFolder = new File(config.getString(OUTPUT_FOLDER, DEFAULT_OUTPUT_FOLDER));
  34.             baseFolder = new File("/");
  35.         } else {
  36.             outputFolder = new File(defaultOutputFolder, config.getString(OUTPUT_FOLDER, DEFAULT_OUTPUT_FOLDER));
  37.             baseFolder = new File(".");
  38.         }
  39.         log.info("Writing to output folder: " + outputFolder);
  40.     }
  41.     public void initialize() {
  42.     }
  43.     public void shutdown() {
  44.     }
  45.     public String getName() {
  46.         return MODULE_NAME;
  47.     }
  48.     public String getVersion() {
  49.         return MODULE_VERSION;
  50.     }
  51.     public String getDescription() {
  52.         return MODULE_DESCRIPTION;
  53.     }
  54.     public String getVendor() {
  55.         return MODULE_VENDOR;
  56.     }
  57.     public void notify(JSpiderEvent event) {
  58.         if (event instanceof ResourceFetchedEvent) {
  59.             ResourceFetchedEvent rfe = (ResourceFetchedEvent) event;
  60.             URL url = rfe.getURL();
  61.             String path = url.getHost() + url.getPath();
  62.             if (includesFile(url)) {
  63.                 path = url.getHost() + url.getFile();
  64.             } else {
  65.                 path = url.getHost() + url.getPath() + "/index.html";
  66.             }
  67.             File outputFile = new File(outputFolder, path);
  68.             ensureFolders(outputFile);
  69.             log.debug("Writing " + outputFile);
  70.             writeFile(outputFile, rfe.getResource().getInputStream());
  71.             log.info("Wrote " + outputFile);
  72.         }
  73.     }
  74.     protected void writeFile(File outputFile, InputStream is) {
  75.         try {
  76.             FileOutputStream fos = new FileOutputStream(outputFile);
  77.             int read = is.read();
  78.             while (read != -1) {
  79.                 fos.write(read);
  80.                 read = is.read();
  81.             }
  82.             fos.close();
  83.         } catch (IOException e) {
  84.             log.error("i/o error writing file", e);
  85.        }
  86.     }
  87.     protected void ensureFolders( File folder) {
  88.         String path = folder.getPath();
  89.         StringTokenizer st = new StringTokenizer(path, File.separator);
  90.         ensureFolder(baseFolder, st);
  91.     }
  92.     protected void ensureFolder(File current, StringTokenizer st) {
  93.         // last part will be a file, we don't want a folder created for that one!
  94.         String thisToken = st.nextToken();
  95.         if (st.hasMoreTokens()) {
  96.             File thisFolder = new File(current, thisToken);
  97.             if (!thisFolder.exists()) {
  98.                 thisFolder.mkdir();
  99.             }
  100.             ensureFolder(thisFolder, st);
  101.         }
  102.     }
  103.     protected boolean includesFile(URL url) {
  104.         String urlString = url.getPath();
  105.         return (urlString.lastIndexOf(".") > urlString.lastIndexOf('/'));
  106.     }
  107. }