1 16 17 package org.pentaho.plugin.core; 18 19 import java.io.OutputStream ; 20 import java.util.Set ; 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 TEMPLATE = "template"; 34 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 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")); return false; 56 } 57 Set outputs = getOutputNames(); 58 if ((outputs == null) || (outputs.size() == 0) || (outputs.size() > 1)) { 59 error(Messages.getString("Template.ERROR_0002_OUTPUT_COUNT_WRONG")); return false; 61 } 62 return true; 63 } 64 65 protected boolean validateSystemSettings() { 66 return true; 68 } 69 70 public void done() { 71 } 73 74 protected boolean executeAction() { 75 76 try { 77 String template = null; 78 79 if (isDefinedInput(TEMPLATE)) { 80 template = getInputStringValue(TEMPLATE); 81 } else if (isDefinedResource(TEMPLATE)) { 82 IActionResource resource = getResource("template"); template = getResourceAsString(resource); 84 } 85 86 String outputName = (String ) getOutputNames().iterator().next(); 87 IActionParameter outputParam = getOutputItem( outputName ); 88 89 if ( outputParam.getType().equals( IActionParameter.TYPE_CONTENT ) ) { 90 91 String mimeType = ""; String extension = ""; if (isDefinedInput("mime-type")) { mimeType = getInputStringValue("mime-type"); } 96 if (isDefinedInput("extension")) { extension = getInputStringValue("extension"); } 99 OutputStream 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 e) { 110 error(Messages.getString("Template.ERROR_0004_COULD_NOT_FORMAT_TEMPLATE"), e); return false; 112 } 113 114 } 115 116 public boolean init() { 117 return true; 119 } 120 121 } 122 | Popular Tags |