01: package nl.hippo.cms.javascript;
02:
03: import java.io.IOException;
04: import java.util.Iterator;
05: import java.util.List;
06:
07: import org.apache.avalon.framework.logger.AbstractLogEnabled;
08: import org.apache.avalon.framework.logger.Logger;
09: import org.apache.cocoon.ProcessingException;
10: import org.apache.cocoon.components.source.SourceUtil;
11: import org.apache.cocoon.environment.SourceResolver;
12: import org.apache.excalibur.source.Source;
13: import org.xml.sax.ContentHandler;
14: import org.xml.sax.SAXException;
15:
16: public class JavascriptPackageHandler extends AbstractLogEnabled {
17:
18: private boolean lenient;
19:
20: public JavascriptPackageHandler(boolean lenient, Logger logger) {
21: this .lenient = lenient;
22: this .enableLogging(logger);
23: }
24:
25: public void sourcesToCharacters(List list, SourceResolver resolver,
26: ContentHandler contentHandler) throws SAXException {
27: if (list == null)
28: return;
29:
30: Source source = null;
31:
32: Iterator it = list.iterator();
33: while (it.hasNext()) {
34: String scriptSource = (String) it.next();
35: if (scriptSource.startsWith("/")) {
36: scriptSource = "cocoon:/" + scriptSource;
37: }
38:
39: if (getLogger().isDebugEnabled()) {
40: getLogger().debug("Loading " + scriptSource);
41: }
42:
43: try {
44: source = resolver.resolveURI(scriptSource);
45: SourceUtil
46: .toCharacters(source, "utf-8", contentHandler);
47:
48: if (getLogger().isDebugEnabled()) {
49: getLogger().debug("Loaded " + scriptSource);
50: }
51: } catch (SAXException e) {
52: if (getLogger().isDebugEnabled()) {
53: getLogger().debug("Failed to load " + scriptSource,
54: e);
55: }
56:
57: throw e;
58:
59: } catch (ProcessingException e) {
60: if (getLogger().isDebugEnabled()) {
61: getLogger().debug("Failed to load " + scriptSource,
62: e);
63: }
64:
65: throw new SAXException(e);
66:
67: } catch (IOException e) {
68: if (getLogger().isDebugEnabled()) {
69: getLogger().debug("Failed to load " + scriptSource,
70: e);
71: }
72: if (!lenient) {
73: throw new SAXException(e);
74: }
75:
76: } finally {
77: if (source != null) {
78: resolver.release(source);
79: }
80: }
81: }
82: }
83: }
|