1 37 package org.webharvest.runtime.processors; 38 39 import org.webharvest.definition.IncludeDef; 40 import org.webharvest.definition.ScraperConfiguration; 41 import org.webharvest.runtime.Scraper; 42 import org.webharvest.runtime.ScraperContext; 43 import org.webharvest.runtime.templaters.BaseTemplater; 44 import org.webharvest.runtime.variables.EmptyVariable; 45 import org.webharvest.runtime.variables.IVariable; 46 import org.webharvest.utils.CommonUtil; 47 import org.webharvest.exception.FileException; 48 49 import java.io.*; 50 import java.net.URL ; 51 import java.net.MalformedURLException ; 52 53 56 public class IncludeProcessor extends BaseProcessor { 57 58 private IncludeDef includeDef; 59 60 public IncludeProcessor(IncludeDef includeDef) { 61 super(includeDef); 62 this.includeDef = includeDef; 63 } 64 65 public IVariable execute(Scraper scraper, ScraperContext context) { 66 boolean isUrl = false; 67 68 String path = BaseTemplater.execute( includeDef.getPath(), scraper.getScriptEngine() ); 69 path = CommonUtil.adaptFilename(path); 70 String fullPath = path; 71 72 ScraperConfiguration configuration = scraper.getConfiguration(); 73 File originalFile = configuration.getSourceFile(); 74 String originalUrl = configuration.getUrl(); 75 if (originalFile != null) { 76 String originalPath = CommonUtil.adaptFilename( originalFile.getAbsolutePath() ); 77 int index = originalPath.lastIndexOf('/'); 78 if (index > 0) { 79 String workingPath = originalPath.substring(0, index); 80 fullPath = CommonUtil.getAbsoluteFilename(workingPath, path); 81 } 82 } else if (originalUrl != null) { 83 fullPath = CommonUtil.fullUrl(originalUrl, path); 84 isUrl = true; 85 } 86 87 ScraperConfiguration includedConfig; 88 try { 89 includedConfig = isUrl ? new ScraperConfiguration(new URL (fullPath)) : new ScraperConfiguration(fullPath); 90 scraper.execute(includedConfig.getOperations()); 91 return new EmptyVariable(); 92 } catch (FileNotFoundException e) { 93 throw new FileException("Cannot include configuration file " + fullPath, e); 94 } catch (MalformedURLException e) { 95 throw new FileException("Cannot include configuration file " + fullPath, e); 96 } catch (IOException e) { 97 throw new FileException("Cannot include configuration file " + fullPath, e); 98 } 99 } 100 101 } | Popular Tags |