1 16 package org.apache.cocoon.components.language.programming; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.avalon.framework.logger.LogEnabled; 20 import org.apache.avalon.framework.parameters.Parameterizable; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.avalon.framework.parameters.ParameterException; 23 24 import org.apache.cocoon.components.language.LanguageException; 25 import org.apache.cocoon.components.language.generator.CompiledComponent; 26 import org.apache.cocoon.util.ClassUtils; 27 28 import java.io.File ; 29 30 39 public abstract class AbstractProgrammingLanguage extends AbstractLogEnabled 40 implements ProgrammingLanguage, Parameterizable { 41 42 43 protected Class codeFormatter; 44 45 protected String languageName; 46 47 54 public void parameterize(Parameters params) throws ParameterException { 55 String className = params.getParameter("code-formatter", null); 56 57 try { 58 if (className != null) { 59 this.codeFormatter = ClassUtils.loadClass(className); 60 } 61 } catch (Exception e) { 62 getLogger().error("Error with \"code-formatter\" parameter", e); 63 throw new ParameterException("Unable to load code formatter: " + className, e); 64 } 65 } 66 67 73 public CodeFormatter getCodeFormatter() { 74 if (this.codeFormatter != null) { 75 try { 76 CodeFormatter formatter = (CodeFormatter) this.codeFormatter.newInstance(); 77 if (formatter instanceof LogEnabled) { 78 ((LogEnabled) formatter).enableLogging(this.getLogger()); 79 } 80 return formatter; 81 } catch (Exception e) { 82 getLogger().error("Error instantiating CodeFormatter", e); 83 } 84 } 85 86 return null; 87 } 88 89 95 protected abstract void doUnload(Object program, String filename, 96 File baseDirectory) 97 throws LanguageException; 98 99 public final void unload(Object program, String filename, File baseDirectory) 100 throws LanguageException { 101 102 File file = new File (baseDirectory, 103 filename + "." + getSourceExtension()); 104 file.delete(); 105 this.doUnload(program, filename, baseDirectory); 106 } 107 108 public final void setLanguageName(String name) { 109 this.languageName = name; 110 } 111 112 public final String getLanguageName() { 113 return this.languageName; 114 } 115 116 123 public CompiledComponent instantiate(Program program) throws LanguageException { 124 try { 125 return program.newInstance(); 126 } catch (Exception e) { 127 getLogger().warn("Could not instantiate program instance", e); 128 throw new LanguageException("Could not instantiate program instance due to a " + e.getClass().getName() + ": " + e.getMessage()); 129 } 130 } 131 } 132 | Popular Tags |