001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.language.programming.javascript;
018:
019: import org.apache.cocoon.components.language.LanguageException;
020: import org.apache.cocoon.components.language.markup.xsp.XSLTExtension;
021: import org.apache.cocoon.components.language.programming.AbstractProgrammingLanguage;
022: import org.apache.cocoon.components.language.programming.Program;
023: import org.apache.cocoon.util.ClassUtils;
024: import org.apache.cocoon.util.IOUtils;
025:
026: import java.io.BufferedReader;
027: import java.io.File;
028: import java.io.FileReader;
029: import java.io.IOException;
030: import java.util.ArrayList;
031:
032: /**
033: * The interpreted Javascript programming language.
034: * Program in Javascript must have comment line as first line of file:
035: * <pre>
036: * // $Cocoon extends: org.apache.cocoon.components.language.xsp.JSGenerator$
037: * </pre>
038: * The class specified will be used as a Java wrapper interpreting javascript program.
039: *
040: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
041: * @version $Id: JavascriptLanguage.java 433543 2006-08-22 06:22:54Z crossley $
042: */
043: public class JavascriptLanguage extends AbstractProgrammingLanguage {
044:
045: public Program preload(String filename, File baseDirectory,
046: String encoding) throws LanguageException {
047: return load(filename, baseDirectory, encoding);
048: }
049:
050: public Program load(String filename, File baseDirectory,
051: String encoding) throws LanguageException {
052: // Does source file exist?
053: File sourceFile = new File(baseDirectory, filename + "."
054: + this .getSourceExtension());
055: if (!sourceFile.exists()) {
056: throw new LanguageException(
057: "Can't load program - File doesn't exist: "
058: + IOUtils.getFullFilename(sourceFile));
059: }
060: if (!sourceFile.isFile()) {
061: throw new LanguageException(
062: "Can't load program - File is not a normal file: "
063: + IOUtils.getFullFilename(sourceFile));
064: }
065: if (!sourceFile.canRead()) {
066: throw new LanguageException(
067: "Can't load program - File cannot be read: "
068: + IOUtils.getFullFilename(sourceFile));
069: }
070:
071: Class clazz = null;
072: ArrayList dependecies = new ArrayList();
073:
074: String className = null;
075: BufferedReader r = null;
076: try {
077: r = new BufferedReader(new FileReader(sourceFile));
078: className = getMeta(r.readLine(), "extends");
079: if (className == null) {
080: throw new LanguageException(
081: "Can't load program - Signature is not found: "
082: + IOUtils.getFullFilename(sourceFile));
083: }
084:
085: clazz = ClassUtils.loadClass(className);
086:
087: String line;
088: while ((line = getMeta(r.readLine(), "depends")) != null) {
089: dependecies.add(line);
090: }
091: } catch (IOException e) {
092: throw new LanguageException(
093: "Can't load program - Signature is not found: "
094: + IOUtils.getFullFilename(sourceFile));
095: } catch (ClassNotFoundException e) {
096: throw new LanguageException(
097: "Can't load program - Base class " + className
098: + " is not found: "
099: + IOUtils.getFullFilename(sourceFile));
100: } finally {
101: if (r != null)
102: try {
103: r.close();
104: } catch (IOException ignored) {
105: }
106: }
107:
108: return new JavascriptProgram(sourceFile, clazz, dependecies);
109: }
110:
111: private String getMeta(String line, String meta) {
112: if (line == null) {
113: return null;
114: }
115:
116: meta = "$Cocoon " + meta + ": ";
117: int i = line.indexOf(meta);
118: if (i != -1) {
119: int j = line.indexOf("$", i + 1);
120: if (j != -1) {
121: line = line.substring(i + meta.length(), j);
122: } else {
123: line = null;
124: }
125: } else {
126: line = null;
127: }
128: return line;
129: }
130:
131: protected void doUnload(Object program, String filename,
132: File baseDir) throws LanguageException {
133: // Do nothing. Source is already deleted by the AbstractProgrammingLanguage.
134: }
135:
136: public String quoteString(String constant) {
137: return XSLTExtension.escapeJavaString(constant);
138: }
139:
140: /**
141: * Return the language's canonical source file extension.
142: *
143: * @return The source file extension
144: */
145: public String getSourceExtension() {
146: return "js";
147: }
148: }
|