OLD | NEW |
1 // Copyright (C) 2008 Google Inc. | 1 // Copyright (C) 2008 Google Inc. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 package com.google.caja.util; | 15 package com.google.caja.util; |
16 | 16 |
17 import com.google.caja.lexer.CharProducer; | 17 import com.google.caja.lexer.CharProducer; |
18 import com.google.caja.lexer.CssTokenType; | 18 import com.google.caja.lexer.CssTokenType; |
| 19 import com.google.caja.lexer.FilePosition; |
19 import com.google.caja.lexer.HtmlLexer; | 20 import com.google.caja.lexer.HtmlLexer; |
20 import com.google.caja.lexer.HtmlTokenType; | 21 import com.google.caja.lexer.HtmlTokenType; |
21 import com.google.caja.lexer.InputSource; | 22 import com.google.caja.lexer.InputSource; |
22 import com.google.caja.lexer.JsLexer; | 23 import com.google.caja.lexer.JsLexer; |
23 import com.google.caja.lexer.JsTokenQueue; | 24 import com.google.caja.lexer.JsTokenQueue; |
24 import com.google.caja.lexer.JsTokenType; | 25 import com.google.caja.lexer.JsTokenType; |
25 import com.google.caja.lexer.ParseException; | 26 import com.google.caja.lexer.ParseException; |
26 import com.google.caja.lexer.Token; | 27 import com.google.caja.lexer.Token; |
27 import com.google.caja.lexer.TokenConsumer; | 28 import com.google.caja.lexer.TokenConsumer; |
28 import com.google.caja.lexer.TokenQueue; | 29 import com.google.caja.lexer.TokenQueue; |
(...skipping 10 matching lines...) Expand all Loading... |
39 import com.google.caja.reporting.Message; | 40 import com.google.caja.reporting.Message; |
40 import com.google.caja.reporting.MessageContext; | 41 import com.google.caja.reporting.MessageContext; |
41 import com.google.caja.reporting.MessageLevel; | 42 import com.google.caja.reporting.MessageLevel; |
42 import com.google.caja.reporting.MessagePart; | 43 import com.google.caja.reporting.MessagePart; |
43 import com.google.caja.reporting.MessageQueue; | 44 import com.google.caja.reporting.MessageQueue; |
44 import com.google.caja.reporting.MessageTypeInt; | 45 import com.google.caja.reporting.MessageTypeInt; |
45 import com.google.caja.reporting.RenderContext; | 46 import com.google.caja.reporting.RenderContext; |
46 | 47 |
47 import java.io.FileNotFoundException; | 48 import java.io.FileNotFoundException; |
48 import java.io.IOException; | 49 import java.io.IOException; |
| 50 import java.io.InputStream; |
| 51 import java.io.InputStreamReader; |
49 import java.io.StringReader; | 52 import java.io.StringReader; |
50 import java.net.URI; | 53 import java.net.URI; |
51 | 54 |
52 import org.w3c.dom.DocumentFragment; | 55 import org.w3c.dom.DocumentFragment; |
53 import org.w3c.dom.Element; | 56 import org.w3c.dom.Element; |
54 import org.w3c.dom.Node; | 57 import org.w3c.dom.Node; |
55 | 58 |
56 import junit.framework.TestCase; | 59 import junit.framework.TestCase; |
57 | 60 |
58 public abstract class CajaTestCase extends TestCase { | 61 public abstract class CajaTestCase extends TestCase { |
(...skipping 18 matching lines...) Expand all Loading... |
77 | 80 |
78 protected CharProducer fromString(String content) { | 81 protected CharProducer fromString(String content) { |
79 return fromString(content, is); | 82 return fromString(content, is); |
80 } | 83 } |
81 | 84 |
82 protected CharProducer fromString(String content, InputSource is) { | 85 protected CharProducer fromString(String content, InputSource is) { |
83 this.mc.addInputSource(is); | 86 this.mc.addInputSource(is); |
84 return CharProducer.Factory.create(new StringReader(content), is); | 87 return CharProducer.Factory.create(new StringReader(content), is); |
85 } | 88 } |
86 | 89 |
| 90 protected CharProducer fromString(String content, FilePosition pos) { |
| 91 this.mc.addInputSource(is); |
| 92 return CharProducer.Factory.create(new StringReader(content), pos); |
| 93 } |
| 94 |
87 protected CharProducer fromResource(String resourcePath) throws IOException { | 95 protected CharProducer fromResource(String resourcePath) throws IOException { |
88 URI uri = TestUtil.getResource(getClass(), resourcePath); | 96 URI resource = TestUtil.getResource(getClass(), resourcePath); |
89 if (uri == null) { | 97 if (resource == null) { throw new FileNotFoundException(resourcePath); } |
90 throw new FileNotFoundException(resourcePath); | 98 return fromResource(resourcePath, new InputSource(resource)); |
91 } | 99 } |
92 InputSource is = new InputSource(uri); | 100 |
93 CharProducer cp = TestUtil.getResourceAsProducer(getClass(), resourcePath); | 101 protected CharProducer fromResource(String resourcePath, InputSource is) |
| 102 throws IOException { |
| 103 InputStream in = TestUtil.getResourceAsStream(getClass(), resourcePath); |
| 104 CharProducer cp = CharProducer.Factory.create( |
| 105 new InputStreamReader(in, "UTF-8"), is); |
94 mc.addInputSource(is); | 106 mc.addInputSource(is); |
95 return cp; | 107 return cp; |
96 } | 108 } |
97 | 109 |
98 protected Block js(CharProducer cp) throws ParseException { | 110 protected Block js(CharProducer cp) throws ParseException { |
99 return js(cp, false); | 111 return js(cp, false); |
100 } | 112 } |
101 | 113 |
102 protected Expression jsExpr(CharProducer cp) throws ParseException { | 114 protected Expression jsExpr(CharProducer cp) throws ParseException { |
103 return jsExpr(cp, false); | 115 return jsExpr(cp, false); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 tq, mq, MessageLevel.FATAL_ERROR).parseDeclarationGroup(); | 213 tq, mq, MessageLevel.FATAL_ERROR).parseDeclarationGroup(); |
202 tq.expectEmpty(); | 214 tq.expectEmpty(); |
203 return dg; | 215 return dg; |
204 } | 216 } |
205 | 217 |
206 private TokenQueue<CssTokenType> cssTokenQueue( | 218 private TokenQueue<CssTokenType> cssTokenQueue( |
207 CharProducer cp, boolean substs) { | 219 CharProducer cp, boolean substs) { |
208 return CssParser.makeTokenQueue(cp, mq, substs); | 220 return CssParser.makeTokenQueue(cp, mq, substs); |
209 } | 221 } |
210 | 222 |
211 protected String render(ParseTreeNode node) { | 223 public static String render(ParseTreeNode node) { |
212 StringBuilder sb = new StringBuilder(); | 224 StringBuilder sb = new StringBuilder(); |
213 TokenConsumer tc = node.makeRenderer(sb, null); | 225 TokenConsumer tc = node.makeRenderer(sb, null); |
214 node.render(new RenderContext(tc)); | 226 node.render(new RenderContext(tc)); |
215 tc.noMoreTokens(); | 227 tc.noMoreTokens(); |
216 return sb.toString(); | 228 return sb.toString(); |
217 } | 229 } |
218 | 230 |
219 protected String minify(ParseTreeNode node) { | 231 protected String minify(ParseTreeNode node) { |
220 // Make sure it's a JS node. | 232 // Make sure it's a JS node. |
221 StringBuilder sb = new StringBuilder(); | 233 StringBuilder sb = new StringBuilder(); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 } | 301 } |
290 ++missing; | 302 ++missing; |
291 } | 303 } |
292 return missing; | 304 return missing; |
293 } | 305 } |
294 | 306 |
295 private InputSource sourceOf(CharProducer cp) { | 307 private InputSource sourceOf(CharProducer cp) { |
296 return cp.getSourceBreaks(0).source(); | 308 return cp.getSourceBreaks(0).source(); |
297 } | 309 } |
298 } | 310 } |
OLD | NEW |