01: package nl.hippo.cms.javascript;
02:
03: import java.io.IOException;
04: import java.util.List;
05: import java.util.Map;
06:
07: import org.apache.avalon.framework.parameters.ParameterException;
08: import org.apache.avalon.framework.parameters.Parameters;
09: import org.apache.cocoon.ProcessingException;
10: import org.apache.cocoon.environment.ObjectModelHelper;
11: import org.apache.cocoon.environment.Request;
12: import org.apache.cocoon.environment.Session;
13: import org.apache.cocoon.environment.SourceResolver;
14: import org.apache.cocoon.generation.ServiceableGenerator;
15: import org.xml.sax.SAXException;
16: import nl.hippo.cocoon.generation.repository.navigation.ExtendedContentHandler;
17:
18: public class JavascriptPackageGenerator extends ServiceableGenerator {
19: private static final String JSPACKAGE_KEY = "jspackage-key";
20:
21: private Request request;
22: private Session session;
23: private String packageId;
24: private boolean lenient = false;
25: private JavascriptPackageHandler jph;
26:
27: public void setup(SourceResolver resolver, Map objectModel,
28: String src, Parameters params) throws ProcessingException,
29: SAXException, IOException {
30:
31: super .setup(resolver, objectModel, src, params);
32:
33: request = ObjectModelHelper.getRequest(objectModel);
34: session = request.getSession();
35:
36: if (request.getParameter(JSPACKAGE_KEY) != null) {
37: packageId = request.getParameter(JSPACKAGE_KEY);
38: } else {
39: try {
40: packageId = params.getParameter(JSPACKAGE_KEY);
41: } catch (ParameterException e) {
42: throw new ProcessingException("Parameter "
43: + JSPACKAGE_KEY + " is undefined");
44: }
45: }
46: jph = new JavascriptPackageHandler(lenient, getLogger()
47: .getChildLogger("JavascriptPackageHandler"));
48: }
49:
50: public void generate() throws IOException, SAXException,
51: ProcessingException {
52:
53: ExtendedContentHandler extended = new ExtendedContentHandler(
54: contentHandler, "");
55: extended.startDocument();
56:
57: extended.startElement("text");
58: Map packages = (Map) session.getAttribute(JSPACKAGE_KEY);
59: if (packages != null) {
60: jph.sourcesToCharacters((List) packages.get(packageId),
61: resolver, extended);
62: }
63: extended.endElement("text");
64: extended.endDocument();
65: }
66:
67: }
|