KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > FilePopulator


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.cms.applications.workflowtool.function;
24
25 import java.io.File JavaDoc;
26
27 import org.apache.log4j.Logger;
28 import org.infoglue.cms.io.FileHelper;
29 import org.infoglue.cms.util.CmsPropertyHandler;
30
31 import com.opensymphony.workflow.WorkflowException;
32
33 /**
34  * This function is used for populating the propertyset with the contents of a file (stored relative the <code>contextRootPath</code>).
35  * <p>The variables (that is <code>${...}</code>) in the content of the file will be translated.</p>
36  *
37  * <h1 class="workflow">Context in</h1>
38  * <table class="workflow">
39  * <thead class="workflow">
40  * <tr class="workflow"><th class="workflow">Name</th><th class="workflow">Type</th><th class="workflow">Class</th><th class="workflow">Required</th><th class="workflow">Default</th><th class="workflow">Comments</th></tr>
41  * </thead>
42  * <tbody class="workflow">
43  * <tr class="workflow"><td class="workflow">path</td><td class="workflow">argument</td><td class="workflow">String</td><td class="workflow">true</td><td class="workflow">-</td><td class="workflow_comment">The path of the file (relative the <code>contextRootPath</code>).</td></tr>
44  * <tr class="workflow"><td class="workflow">key</td><td class="workflow">argument</td><td class="workflow">String</td><td class="workflow">true</td><td class="workflow">-</td><td class="workflow_comment">The key to use when storing the result in the propertyset.</td></tr>
45  * </tbody>
46  * </table>
47  * <h1 class="workflow">Context out</h1>
48  * <table class="workflow">
49  * <thead class="workflow">
50  * <tr class="workflow"><th class="workflow">Name</th><th class="workflow">Type</th><th class="workflow">Class</th><th class="workflow">Comments</th></tr>
51  * </thead>
52  * <tbody class="workflow">
53  * <tr class="workflow"><td class="workflow">&lt;key&gt;</td><td class="workflow">propertyset</td><td class="workflow">DataString</td><td class="workflow_comment">The translated file contents.</td></tr>
54  * </tbody>
55  * </table>
56  */

57 public class FilePopulator extends InfoglueFunction
58 {
59     private final static Logger logger = Logger.getLogger(FilePopulator.class.getName());
60
61     /**
62      * The name of the path argument.
63      */

64     private static final String JavaDoc PATH_ARGUMENT = "path";
65     
66     /**
67      * The name of the key argument.
68      */

69     private static final String JavaDoc PROPERTYSET_KEY_ARGUMENT = "key";
70     
71     /**
72      * The path of the file (relative the <code>contextRootPath</code>).
73      */

74     private String JavaDoc path;
75     
76     /**
77      * The key to use when storing the result in the propertyset.
78      */

79     private String JavaDoc key;
80     
81     /**
82      * Default constructor.
83      */

84     public FilePopulator()
85     {
86         super();
87     }
88
89     /**
90      * Loads the file, translates the content and stores the result in the propertyset.
91      *
92      * @throws WorkflowException if an error occurs during the execution.
93      */

94     protected void execute() throws WorkflowException
95     {
96         try
97         {
98             final String JavaDoc fullPath = getFullPath();
99             final String JavaDoc unparsed = FileHelper.getFileAsString(new File JavaDoc(fullPath));
100             final String JavaDoc parsed = translate(unparsed);
101             
102             logger.debug("path=[" + fullPath + "],unparsed=[" + unparsed + "],parsed=[" + parsed + "]");
103             setPropertySetDataString(key, parsed);
104         }
105         catch(Exception JavaDoc e)
106         {
107             throwException(e);
108         }
109     }
110     
111     /**
112      * Returns the full path of the file.
113      *
114      * @return the full path of the file.
115      */

116     private String JavaDoc getFullPath()
117     {
118         return CmsPropertyHandler.getContextRootPath() + path;
119     }
120
121     /**
122      * Method used for initializing the function; will be called before <code>execute</code> is called.
123      * <p><strong>Note</strong>! You must call <code>super.initialize()</code> first.</p>
124      *
125      * @throws WorkflowException if an error occurs during the initialization.
126      */

127     protected void initialize() throws WorkflowException
128     {
129         super.initialize();
130         this.path = getArgument(PATH_ARGUMENT);
131         this.key = getArgument(PROPERTYSET_KEY_ARGUMENT);
132     }
133 }
134
Popular Tags