1 37 package org.webharvest.runtime.processors; 38 39 import java.io.*; 40 41 import org.webharvest.definition.FileDef; 42 import org.webharvest.exception.FileException; 43 import org.webharvest.runtime.Scraper; 44 import org.webharvest.runtime.ScraperContext; 45 import org.webharvest.runtime.scripting.ScriptEngine; 46 import org.webharvest.runtime.templaters.BaseTemplater; 47 import org.webharvest.runtime.variables.*; 48 import org.webharvest.utils.CommonUtil; 49 50 53 public class FileProcessor extends BaseProcessor { 54 55 private FileDef fileDef; 56 57 public FileProcessor(FileDef fileDef) { 58 super(fileDef); 59 this.fileDef = fileDef; 60 } 61 62 public IVariable execute(Scraper scraper, ScraperContext context) { 63 String workingDir = scraper.getWorkingDir(); 64 65 ScriptEngine scriptEngine = scraper.getScriptEngine(); 66 String action = BaseTemplater.execute( fileDef.getAction(), scriptEngine); 67 String filePath = BaseTemplater.execute( fileDef.getPath(), scriptEngine); 68 String type = BaseTemplater.execute( fileDef.getType(), scriptEngine); 69 String charset = BaseTemplater.execute( fileDef.getCharset(), scriptEngine); 70 if (charset == null) { 71 charset = scraper.getConfiguration().getCharset(); 72 } 73 74 String fullPath = CommonUtil.getAbsoluteFilename(workingDir, filePath); 75 76 if ( "write".equalsIgnoreCase(action) ) { 78 return executeFileWrite(false, scraper, context, fullPath, type, charset); 79 } else if ( "append".equalsIgnoreCase(action) ) { 80 return executeFileWrite(true, scraper, context, fullPath, type, charset); 81 } else { 82 return executeFileRead(fullPath, type, charset); 83 } 84 } 85 86 90 private IVariable executeFileWrite(boolean append, Scraper scraper, ScraperContext context, String fullPath, String type, String charset) { 91 IVariable result; 92 93 try { 94 new File( CommonUtil.getDirectoryFromPath(fullPath) ).mkdirs(); 96 97 FileOutputStream out = new FileOutputStream(fullPath, append); 98 byte[] data; 99 100 if ( Types.TYPE_BINARY.equalsIgnoreCase(type) ) { 101 IVariable body = getBodyBinaryContent(fileDef, scraper, context); 102 data = body.toBinary(); 103 result = new NodeVariable(data); 104 } else { 105 IVariable body = getBodyTextContent(fileDef, scraper, context); 106 String content = body.toString(); 107 data = content.getBytes(charset); 108 result = new NodeVariable(content); 109 } 110 111 out.write(data); 112 out.flush(); 113 out.close(); 114 115 return result; 116 } catch (IOException e) { 117 throw new FileException("Error writing data to file: " + fullPath, e); 118 } 119 } 120 121 124 private IVariable executeFileRead(String fullPath, String type, String charset) { 125 if ( Types.TYPE_BINARY.equalsIgnoreCase(type) ) { 126 try { 127 byte[] data = CommonUtil.readBytesFromFile(new File(fullPath)); 128 log.info("Binary file read processor: " + data.length + " bytes read."); 129 return new NodeVariable(data); 130 } catch (IOException e) { 131 throw new FileException("Error reading file: " + fullPath, e); 132 } 133 } else { 134 try { 135 String content = CommonUtil.readStringFromFile(new File(fullPath), charset); 136 log.info( "Text file read processor: " + (content == null ? 0 : content.length()) + " characters read." ); 137 return new NodeVariable(content); 138 } catch (IOException e) { 139 throw new FileException("Error reading the file: " + fullPath, e); 140 } 141 } 142 } 143 144 } | Popular Tags |