OLD | NEW |
1 // Copyright 2009 Google Inc. All Rights Reserved. | 1 // Copyright 2009 Google Inc. All Rights Reserved. |
2 // Licensed under the Apache License, Version 2.0 (the "License"); | 2 // Licensed under the Apache License, Version 2.0 (the "License"); |
3 // you may not use this file except in compliance with the License. | 3 // you may not use this file except in compliance with the License. |
4 // You may obtain a copy of the License at | 4 // You may obtain a copy of the License at |
5 // | 5 // |
6 // http://www.apache.org/licenses/LICENSE-2.0 | 6 // http://www.apache.org/licenses/LICENSE-2.0 |
7 // | 7 // |
8 // Unless required by applicable law or agreed to in writing, software | 8 // Unless required by applicable law or agreed to in writing, software |
9 // distributed under the License is distributed on an "AS IS" BASIS, | 9 // distributed under the License is distributed on an "AS IS" BASIS, |
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
11 // See the License for the specific language governing permissions and | 11 // See the License for the specific language governing permissions and |
12 // limitations under the License. | 12 // limitations under the License. |
13 | 13 |
14 package com.google.caja.service; | 14 package com.google.caja.service; |
15 | 15 |
16 import com.google.caja.lexer.CharProducer; | 16 import com.google.caja.lexer.CharProducer; |
17 import com.google.caja.lexer.ExternalReference; | 17 import com.google.caja.lexer.ExternalReference; |
18 import com.google.caja.lexer.HtmlLexer; | 18 import com.google.caja.lexer.HtmlLexer; |
19 import com.google.caja.lexer.InputSource; | 19 import com.google.caja.lexer.InputSource; |
20 import com.google.caja.lexer.ParseException; | 20 import com.google.caja.lexer.ParseException; |
| 21 import com.google.caja.opensocial.UriCallback; |
| 22 import com.google.caja.opensocial.UriCallbackException; |
21 import com.google.caja.parser.AncestorChain; | 23 import com.google.caja.parser.AncestorChain; |
22 import com.google.caja.parser.ParseTreeNode; | 24 import com.google.caja.parser.ParseTreeNode; |
23 import com.google.caja.parser.html.DomParser; | 25 import com.google.caja.parser.html.DomParser; |
24 import com.google.caja.parser.quasiliteral.Rewriter; | 26 import com.google.caja.parser.html.Nodes; |
| 27 import com.google.caja.parser.js.CajoledModule; |
25 import com.google.caja.plugin.Dom; | 28 import com.google.caja.plugin.Dom; |
26 import com.google.caja.plugin.PluginCompiler; | 29 import com.google.caja.plugin.PluginCompiler; |
27 import com.google.caja.plugin.PluginEnvironment; | 30 import com.google.caja.plugin.PluginEnvironment; |
28 import com.google.caja.plugin.PluginMeta; | 31 import com.google.caja.plugin.PluginMeta; |
| 32 import com.google.caja.render.Concatenator; |
| 33 import com.google.caja.render.JsMinimalPrinter; |
29 import com.google.caja.reporting.BuildInfo; | 34 import com.google.caja.reporting.BuildInfo; |
30 import com.google.caja.reporting.Message; | 35 import com.google.caja.reporting.Message; |
31 import com.google.caja.reporting.MessageContext; | 36 import com.google.caja.reporting.MessageContext; |
32 import com.google.caja.reporting.MessageLevel; | 37 import com.google.caja.reporting.MessageLevel; |
33 import com.google.caja.reporting.MessageQueue; | 38 import com.google.caja.reporting.MessageQueue; |
| 39 import com.google.caja.reporting.RenderContext; |
34 import com.google.caja.reporting.SimpleMessageQueue; | 40 import com.google.caja.reporting.SimpleMessageQueue; |
35 import com.google.caja.util.Pair; | 41 import com.google.caja.util.Pair; |
36 | 42 |
37 import java.io.IOException; | 43 import java.io.IOException; |
38 import java.io.OutputStream; | 44 import java.io.OutputStream; |
39 import java.io.OutputStreamWriter; | 45 import java.io.OutputStreamWriter; |
40 import java.io.Reader; | 46 import java.io.Reader; |
41 import java.io.StringReader; | 47 import java.io.StringReader; |
42 import java.io.UnsupportedEncodingException; | 48 import java.io.UnsupportedEncodingException; |
43 import java.net.URI; | 49 import java.net.URI; |
44 import java.net.URLEncoder; | 50 import java.net.URLEncoder; |
45 | 51 |
| 52 import org.w3c.dom.Document; |
| 53 import org.w3c.dom.Element; |
| 54 import org.w3c.dom.Node; |
| 55 |
46 /** | 56 /** |
47 * Retrieves html files and cajoles them | 57 * Retrieves html files and cajoles them |
48 * | 58 * |
49 * @author jasvir@google.com (Jasvir Nagra) | 59 * @author jasvir@google.com (Jasvir Nagra) |
50 */ | 60 */ |
51 public class HtmlHandler implements ContentHandler { | 61 public class HtmlHandler implements ContentHandler { |
52 private final BuildInfo buildInfo; | 62 private final BuildInfo buildInfo; |
53 private PluginMeta meta; | 63 private final PluginMeta meta; |
54 private final static String DEFAULT_HOSTED_SERVICE = | 64 private final static String DEFAULT_HOSTED_SERVICE = |
55 "http://caja.appsport.com/cajoler"; | 65 "http://caja.appsport.com/cajoler"; |
56 | 66 |
57 public HtmlHandler(BuildInfo buildInfo) { | 67 public HtmlHandler(BuildInfo buildInfo) { |
58 this(buildInfo, DEFAULT_HOSTED_SERVICE); | 68 this(buildInfo, DEFAULT_HOSTED_SERVICE, null); |
59 } | 69 } |
60 | 70 |
61 | 71 public HtmlHandler( |
62 public HtmlHandler(BuildInfo buildInfo, final String hostedService) { | 72 BuildInfo buildInfo, final String hostedService, |
| 73 final UriCallback retriever) { |
63 this.buildInfo = buildInfo; | 74 this.buildInfo = buildInfo; |
64 this.meta = new PluginMeta(new PluginEnvironment() { | 75 this.meta = new PluginMeta(new PluginEnvironment() { |
65 public CharProducer loadExternalResource(ExternalReference ref, | 76 public CharProducer loadExternalResource( |
66 String mimeType) { | 77 ExternalReference ref, String mimeType) { |
67 return null; | 78 try { |
| 79 Reader in = retriever != null |
| 80 ? retriever.retrieve(ref, mimeType) : null; |
| 81 InputSource is = new InputSource(ref.getUri()); |
| 82 return in != null ? CharProducer.Factory.create(in, is) : null; |
| 83 } catch (UriCallbackException ex) { |
| 84 return null; |
| 85 } catch (IOException ex) { |
| 86 return null; |
| 87 } |
68 } | 88 } |
69 | 89 |
70 public String rewriteUri(ExternalReference uri, String mimeType) { | 90 public String rewriteUri(ExternalReference uri, String mimeType) { |
71 if (hostedService != null) { | 91 if (hostedService != null) { |
72 try { | 92 try { |
73 return hostedService | 93 return hostedService |
74 + "?url=" | 94 + "?url=" |
75 + URLEncoder.encode(uri.getUri().toString(), | 95 + URLEncoder.encode(uri.getUri().toString(), "UTF-8") |
76 "UTF-8") | 96 + "&mime-type=" + mimeType; |
77 + "&mime-type=" + mimeType; | |
78 } catch (UnsupportedEncodingException e) { | 97 } catch (UnsupportedEncodingException e) { |
79 return null; | 98 return null; |
80 } | 99 } |
81 } else { | 100 } else { |
82 return null; | 101 return null; |
83 } | 102 } |
84 } | 103 } |
85 | 104 |
86 }); | 105 }); |
87 // HtmlHandler only cajoles in valija mode | 106 // HtmlHandler only cajoles in valija mode |
(...skipping 26 matching lines...) Expand all Loading... |
114 MessageLevel level = m.getMessageLevel(); | 133 MessageLevel level = m.getMessageLevel(); |
115 out.append(level.name() + ": "); | 134 out.append(level.name() + ": "); |
116 m.format(mc, out); | 135 m.format(mc, out); |
117 out.append("\n"); | 136 out.append("\n"); |
118 } | 137 } |
119 } catch (IOException ex) { | 138 } catch (IOException ex) { |
120 ex.printStackTrace(); | 139 ex.printStackTrace(); |
121 } | 140 } |
122 } | 141 } |
123 | 142 |
124 private void cajoleHtml(final URI inputUri, Reader cajaInput, | 143 private void cajoleHtml(URI inputUri, Reader cajaInput, Appendable output) |
125 Appendable output) | |
126 throws IOException, UnsupportedContentTypeException { | 144 throws IOException, UnsupportedContentTypeException { |
127 InputSource is = new InputSource (inputUri); | 145 InputSource is = new InputSource (inputUri); |
128 CharProducer cp = CharProducer.Factory.create(cajaInput,is); | 146 CharProducer cp = CharProducer.Factory.create(cajaInput,is); |
129 MessageQueue mq = new SimpleMessageQueue(); | 147 MessageQueue mq = new SimpleMessageQueue(); |
130 boolean okToContinue = true; | 148 boolean okToContinue = true; |
131 try { | 149 try { |
132 DomParser p = new DomParser(new HtmlLexer(cp), is, mq); | 150 DomParser p = new DomParser(new HtmlLexer(cp), is, mq); |
133 if (p.getTokenQueue().isEmpty()) { okToContinue = false; } | 151 if (p.getTokenQueue().isEmpty()) { okToContinue = false; } |
134 | 152 |
135 ParseTreeNode html = new Dom(p.parseFragment( | 153 Document doc = DomParser.makeDocument(null, null); |
136 DomParser.makeDocument(null, null))); | 154 ParseTreeNode html = new Dom(p.parseFragment(doc)); |
137 p.getTokenQueue().expectEmpty(); | 155 p.getTokenQueue().expectEmpty(); |
138 | 156 |
139 PluginCompiler compiler = new PluginCompiler(buildInfo, meta, mq); | 157 PluginCompiler compiler = new PluginCompiler(buildInfo, meta, mq); |
140 | 158 |
141 compiler.addInput(AncestorChain.instance(html)); | 159 compiler.addInput(AncestorChain.instance(html)); |
142 if (okToContinue) { | 160 if (okToContinue) { |
143 okToContinue &= compiler.run(); | 161 okToContinue &= compiler.run(); |
144 } | 162 } |
145 if (okToContinue) { | 163 if (okToContinue) { |
146 output.append(Rewriter.render(compiler.getJavascript())); | 164 Node staticHtml = compiler.getStaticHtml(); |
| 165 if (staticHtml != null) { |
| 166 output.append(Nodes.render(staticHtml)); |
| 167 } |
| 168 CajoledModule js = compiler.getJavascript(); |
| 169 if (js != null) { |
| 170 StringBuilder jsOut = new StringBuilder(); |
| 171 RenderContext rc = new RenderContext( |
| 172 new JsMinimalPrinter(new Concatenator(jsOut))) |
| 173 .withEmbeddable(true); |
| 174 js.render(rc); |
| 175 rc.getOut().noMoreTokens(); |
| 176 |
| 177 Element script = doc.createElement("SCRIPT"); |
| 178 script.setAttribute("type", "text/javascript"); |
| 179 script.appendChild(doc.createTextNode(jsOut.toString())); |
| 180 output.append(Nodes.render(script)); |
| 181 } |
147 } else { | 182 } else { |
148 MessageContext mc = new MessageContext(); | 183 MessageContext mc = new MessageContext(); |
149 printMessages(mq, mc, System.err); | 184 printMessages(mq, mc, System.err); |
150 } | 185 } |
151 } catch (ParseException e) { | 186 } catch (ParseException e) { |
152 throw new UnsupportedContentTypeException(); | 187 throw new UnsupportedContentTypeException(); |
153 } catch (IllegalArgumentException e) { | 188 } catch (IllegalArgumentException e) { |
154 throw new UnsupportedContentTypeException(); | 189 throw new UnsupportedContentTypeException(); |
155 } catch (IOException e) { | 190 } catch (IOException e) { |
156 throw new UnsupportedContentTypeException(); | 191 throw new UnsupportedContentTypeException(); |
157 } | 192 } |
158 } | 193 } |
159 } | 194 } |
OLD | NEW |