KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > core > TemplateComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Aug 17, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin.core;
18
19 import java.io.OutputStream JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.pentaho.core.runtime.IActionParameter;
25 import org.pentaho.core.solution.IActionResource;
26 import org.pentaho.messages.Messages;
27 import org.pentaho.messages.util.LocaleHelper;
28 import org.pentaho.plugin.ComponentBase;
29
30 public class TemplateComponent extends ComponentBase {
31
32     private final static String JavaDoc TEMPLATE = "template"; //$NON-NLS-1$
33

34     /**
35      *
36      */

37     private static final long serialVersionUID = 4383466190328580251L;
38
39     public Log getLogger() {
40         return LogFactory.getLog(TemplateComponent.class);
41     }
42
43     protected boolean validateAction() {
44
45         // see if we have a template defined
46

47         boolean templateOk = false;
48         if (isDefinedInput(TEMPLATE)) {
49             templateOk = true;
50         } else if (isDefinedResource(TEMPLATE)) {
51             templateOk = true;
52         }
53         if (!templateOk) {
54             error(Messages.getString("Template.ERROR_0001_TEMPLATE_NOT_DEFINED")); //$NON-NLS-1$
55
return false;
56         }
57         Set JavaDoc outputs = getOutputNames();
58         if ((outputs == null) || (outputs.size() == 0) || (outputs.size() > 1)) {
59             error(Messages.getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG")); //$NON-NLS-1$
60
return false;
61         }
62         return true;
63     }
64
65     protected boolean validateSystemSettings() {
66         // nothing to do here
67
return true;
68     }
69
70     public void done() {
71         // nothing to do here
72
}
73
74     protected boolean executeAction() {
75
76         try {
77             String JavaDoc template = null;
78
79             if (isDefinedInput(TEMPLATE)) {
80                 template = getInputStringValue(TEMPLATE);
81             } else if (isDefinedResource(TEMPLATE)) {
82                 IActionResource resource = getResource("template"); //$NON-NLS-1$
83
template = getResourceAsString(resource);
84             }
85
86             String JavaDoc outputName = (String JavaDoc) getOutputNames().iterator().next();
87             IActionParameter outputParam = getOutputItem( outputName );
88
89             if ( outputParam.getType().equals( IActionParameter.TYPE_CONTENT ) ) {
90
91                 String JavaDoc mimeType = ""; //$NON-NLS-1$
92
String JavaDoc extension = ""; //$NON-NLS-1$
93
if (isDefinedInput("mime-type")) { //$NON-NLS-1$
94
mimeType = getInputStringValue("mime-type"); //$NON-NLS-1$
95
}
96                 if (isDefinedInput("extension")) { //$NON-NLS-1$
97
extension = getInputStringValue("extension"); //$NON-NLS-1$
98
}
99                 OutputStream JavaDoc outputStream = getOutputStream(outputName, mimeType, extension);
100                
101                 outputStream.write(applyInputsToFormat(template).getBytes(LocaleHelper.getSystemEncoding()));
102                 return true;
103             }
104             else {
105                 setOutputValue(outputName, applyInputsToFormat(template));
106             }
107
108             return true;
109         } catch (Exception JavaDoc e) {
110             error(Messages.getString("Template.ERROR_0004_COULD_NOT_FORMAT_TEMPLATE"), e); //$NON-NLS-1$
111
return false;
112         }
113
114     }
115
116     public boolean init() {
117         // nothing to do here
118
return true;
119     }
120
121 }
122
Popular Tags