KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webharvest > runtime > processors > FileProcessor


1 /* Copyright (c) 2006-2007, Vladimir Nikic
2     All rights reserved.
3
4     Redistribution and use of this software in source and binary forms,
5     with or without modification, are permitted provided that the following
6     conditions are met:
7
8     * Redistributions of source code must retain the above
9       copyright notice, this list of conditions and the
10       following disclaimer.
11
12     * Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the
14       following disclaimer in the documentation and/or other
15       materials provided with the distribution.
16
17     * The name of Web-Harvest may not be used to endorse or promote
18       products derived from this software without specific prior
19       written permission.
20
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31     POSSIBILITY OF SUCH DAMAGE.
32
33     You can contact Vladimir Nikic by sending e-mail to
34     nikic_vladimir@yahoo.com. Please include the word "Web-Harvest" in the
35     subject line.
36 */

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 /**
51  * File processor.
52  */

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 JavaDoc workingDir = scraper.getWorkingDir();
64
65         ScriptEngine scriptEngine = scraper.getScriptEngine();
66         String JavaDoc action = BaseTemplater.execute( fileDef.getAction(), scriptEngine);
67         String JavaDoc filePath = BaseTemplater.execute( fileDef.getPath(), scriptEngine);
68         String JavaDoc type = BaseTemplater.execute( fileDef.getType(), scriptEngine);
69         String JavaDoc charset = BaseTemplater.execute( fileDef.getCharset(), scriptEngine);
70         if (charset == null) {
71             charset = scraper.getConfiguration().getCharset();
72         }
73
74         String JavaDoc fullPath = CommonUtil.getAbsoluteFilename(workingDir, filePath);
75
76         // depending on file acton calls appropriate method
77
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     /**
87      * Writing content to the specified file.
88      * If parameter "append" is true, then append content, otherwise write
89      */

90     private IVariable executeFileWrite(boolean append, Scraper scraper, ScraperContext context, String JavaDoc fullPath, String JavaDoc type, String JavaDoc charset) {
91         IVariable result;
92         
93         try {
94             // ensure that target directory exists
95
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 JavaDoc 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     /**
122      * Reading the specified file.
123      */

124     private IVariable executeFileRead(String JavaDoc fullPath, String JavaDoc type, String JavaDoc 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 JavaDoc 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