2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.clay.parser;
19  
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import junit.framework.TestCase;
24  
25  public class ParserTestCase extends TestCase {
26  
27      /***
28       * <p>
29       * Tests to see if we can parse a document fragment that has multiple root
30       * nodes
31       * </p>
32       */
33      public void testManyRootNodes() {
34          Parser p = new Parser();
35          StringBuffer doc1 = new StringBuffer();
36  
37          doc1.append("<p>").append(
38                  "<input type=text size=10 maxsize=10 id=username>").append(
39                  "<input type=text size=10 maxsize=10 id=password>").append(
40                  "</p>").append("<p>").append("This is a test.  Just a test")
41                  .append("</p>").append("<p></p>");
42  
43          List nodes1 = p.parse(doc1);
44          assertTrue("Has 3 root nodes", nodes1.size() == 3);
45  
46      }
47  
48      /***
49       * <p>
50       * Test a couple comment block scenarios
51       * </p>
52       */
53      public void testCommentBlocks() {
54          Parser p = new Parser();
55          StringBuffer doc1 = new StringBuffer();
56  
57          doc1.append("<p><!-- self contained comment -->").append(
58                  "<!--<input type=text size=10 maxsize=10 id=username>").append(
59                  "<input type=text size=10 maxsize=10 id=password>-->").append(
60                  "</p>").append("<!--This is a test.  Just a test-->").append(
61                  "<!--<p>Testing <b>123</b></p> -->");
62  
63          List nodes1 = p.parse(doc1);
64          assertTrue("Has 3 root nodes", nodes1.size() == 3);
65  
66          Node node = (Node) nodes1.get(0);
67          assertTrue("first paragraph has 2 node", node.getChildren().size() == 2);
68  
69          node = (Node) node.getChildren().get(1);
70          assertTrue("second comment block has 3 nodes",
71                  node.getChildren().size() == 3);
72  
73          node = (Node) nodes1.get(2);
74          assertTrue("third comment has 7 child nodes", node.getChildren()
75                  .size() == 7);
76  
77  
78          // truncate the buffer
79          doc1.setLength(0);
80          doc1
81                  .append("<p>")
82                  .append(
83                          "<!--</p><b><tr></table> we should have a single root with 5 children -->")
84                  .append("</p>");
85  
86          nodes1 = p.parse(doc1);
87          assertTrue("root has 1 child nodes", nodes1.size() == 1);
88  
89          node = (Node) nodes1.get(0);
90          assertTrue("paragraph has 1 child node", node.getChildren().size() == 1);
91  
92          node = (Node) node.getChildren().get(0);
93          assertTrue("comment has 5 child nodes", node.getChildren().size() == 5);
94  
95          // truncate the buffer
96          doc1.setLength(0);
97          doc1.append("<!--A Comment-->");
98          
99          nodes1 = p.parse(doc1);
100         assertTrue("one root node", nodes1.size() == 1);
101         
102         node = (Node) nodes1.get(0);
103         assertTrue("node is a valid comment", node.isComment());
104 
105         // truncate the buffer
106         doc1.setLength(0);
107         doc1.append("<!-->-->");
108         
109         nodes1 = p.parse(doc1);
110         assertTrue("one root node", nodes1.size() == 1);
111         
112         node = (Node) nodes1.get(0);
113         assertTrue("node is a valid comment", node.isComment());
114 
115         // truncate the buffer
116         doc1.setLength(0);
117         doc1.append("<!--<-->");
118         
119         nodes1 = p.parse(doc1);
120         assertTrue("one root node", nodes1.size() == 1);
121         
122         node = (Node) nodes1.get(0);
123         assertTrue("node is a valid comment", node.isComment());
124    
125         
126         // truncate the buffer
127         doc1.setLength(0);
128         doc1.append("<!-- <a attr=\"> -->");
129         
130         nodes1 = p.parse(doc1);
131         assertTrue("one root node", nodes1.size() == 1);
132         
133         node = (Node) nodes1.get(0);
134         assertTrue("node is a valid comment", node.isComment());
135         
136         // truncate the buffer
137         doc1.setLength(0);
138         doc1.append("<!-- <a attr=\" size=4 class=\"test\"> -->");
139         
140         nodes1 = p.parse(doc1);
141         assertTrue("one root node", nodes1.size() == 1);
142         
143         node = (Node) nodes1.get(0);
144         assertTrue("node is a valid comment", node.isComment());
145 
146 
147         // truncate the buffer
148         doc1.setLength(0);
149         doc1.append("<!-- <a attr=> -->");
150         
151         nodes1 = p.parse(doc1);
152         assertTrue("one root node", nodes1.size() == 1);
153         
154         node = (Node) nodes1.get(0);
155         assertTrue("node is a valid comment", node.isComment());
156 
157         
158         // truncate the buffer
159         doc1.setLength(0);
160         doc1.append("<!--- </ -->");
161         
162         nodes1 = p.parse(doc1);
163         assertTrue("one root node", nodes1.size() == 1);
164         
165         node = (Node) nodes1.get(0);
166         assertTrue("node is a valid comment", node.isComment());
167         
168     }
169 
170     /***
171      * <p>
172      * Tests case insensitivity in parsing the document.
173      * </p>
174      */
175     public void testCaseInsensitivity() {
176         Parser p = new Parser();
177         StringBuffer doc1 = new StringBuffer();
178 
179         doc1.append("<body>").append("<p>").append(
180                 "<input type=text size=10 maxsize=10 id=username>").append(
181                 "<input type=text size=10 maxsize=10 id=password>").append(
182                 "<textarea id=memo>testing 123</textarea>").append("</p>")
183                 .append("</body>");
184 
185         List nodes1 = p.parse(doc1);
186         assertTrue("Lower case HTML document", nodes1.size() == 1);
187 
188         StringBuffer doc2 = new StringBuffer();
189 
190         doc2.append("<BODY>").append("<p>").append(
191                 "<input TYPE=text size=10 maxSize=10 Id=username>").append(
192                 "<input typE=text size=10 MAXSIZE=10 id=password>").append(
193                 "<textarea id=memo>testing 123</TEXTAREA>").append("</P>")
194                 .append("</body>");
195 
196         List nodes2 = p.parse(doc2);
197         assertTrue("Mixed case HTML document", nodes2.size() == 1);
198 
199         compareTrees(nodes1, nodes2);
200 
201     }
202 
203     /***
204      * <p>
205      * Tests the parsing to make sure that self terminated nodes are handled the
206      * same as well-formed self terminating nodes.
207      * </p>
208      */
209     public void testSelfTerminating() {
210         Parser p = new Parser();
211         StringBuffer doc1 = new StringBuffer();
212 
213         doc1
214                 .append("<head>")
215                 .append("<title>Shale Rocks</title>")
216                 .append(
217                         "<style type=\"text/css\" media=\"all\"><!-- @import \"common.css\"; --></style>")
218                 .append(
219                         "<style type=\"text/css\" media=\"all\"><!-- @import \"content.css\"; --></style>")
220                 .append(
221                         "<script type=\"text/javascript\" src=\"common.js\"></script>")
222                 .append(
223                         "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">")
224                 .append("<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">")
225                 .append("<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\">")
226                 .append(
227                         "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">")
228                 .append("</head>");
229 
230         List nodes1 = p.parse(doc1);
231         assertTrue("Lazy HTML has 1 node", nodes1.size() == 1);
232 
233         StringBuffer doc2 = new StringBuffer();
234 
235         doc2
236                 .append("<head>")
237                 .append("<title>Shale Rocks</title>")
238                 .append(
239                         "<style type=\"text/css\" media=\"all\"><!-- @import \"common.css\"; --></style>")
240                 .append(
241                         "<style type=\"text/css\" media=\"all\"><!-- @import \"content.css\"; --></style>")
242                 .append(
243                         "<script type=\"text/javascript\" src=\"common.js\"></script>")
244                 .append(
245                         "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\"/>")
246                 .append("<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\"/>")
247                 .append("<META HTTP-EQUIV=\"Expires\" CONTENT=\"-1\"/>")
248                 .append(
249                         "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"/>")
250                 .append("</head>");
251 
252         List nodes2 = p.parse(doc2);
253         assertTrue("Well-formed HTML has 1 node", nodes2.size() == 1);
254 
255         compareTrees(nodes1, nodes2);
256 
257     }
258 
259     /***
260      * <p>
261      * Tests to make sure that the parser handles the HTML tags that can have
262      * optional ending tags the same that it would a document that was
263      * well-formed
264      * </p>
265      */
266     public void testOptionalEnding() {
267         Parser p = new Parser();
268 
269         StringBuffer doc1 = new StringBuffer();
270 
271         // lazy html
272         doc1
273                 .append("<table>")
274                 .append("<tr><th>Test<th>Test")
275                 .append("<tr><td>")
276                 .append("<ol><li>1<li>2<li>3</ol>")
277                 .append("<tr><td>")
278                 .append(
279                         "<select><option value=1>1<option value=1>2<option value=1>3</select>")
280                 .append("</table>");
281 
282         List nodes1 = p.parse(doc1);
283         assertTrue("Lazy HTML has 1 node", nodes1.size() == 1);
284 
285         // good html
286         StringBuffer doc2 = new StringBuffer();
287         doc2
288                 .append("<table>")
289                 .append("<tr><th>Test</th><th>Test</th></tr>")
290                 .append("<tr><td>")
291                 .append("<ol><li>1</li><li>2</li><li>3</li></ol>")
292                 .append("</td></tr>")
293                 .append("<tr><td>")
294                 .append(
295                         "<select><option value=1>1</option><option value=1>2</option><option value=1>3</option></select>")
296                 .append("</td></tr>").append("</table>");
297 
298         List nodes2 = p.parse(doc2);
299         assertTrue("Well-formed HTML has 1 node", nodes2.size() == 1);
300 
301         compareTrees(nodes1, nodes2);
302 
303     }
304 
305     /***
306      * <p>Check for an ending tag without a begining.</p>
307      *
308      */
309     public void testUnmatchedNonOptionalEnding() {
310 
311         Parser p = new Parser();
312         StringBuffer doc = new StringBuffer();
313         doc.append("<html>\n")   
314            .append("<head/>\n")
315            .append("<body>\n")
316            .append("</p>\n")
317            .append("</body>\n")
318            .append("</html>");
319         
320         try {
321            List nodes = p.parse(doc);
322            assertTrue("Failed unmatched non-optional ending tag test", false);
323         } catch (Exception e) {
324            assertTrue("Failed unmatched non-optional ending tag test", e.getMessage().startsWith("Unmatched ending non-optional token"));
325         }
326 
327     }
328 
329     /***
330      * <p>Check for a begining tag that was not terminated.</p>
331      *
332      */
333     public void testUnmatchedBegining() {
334 
335         Parser p = new Parser();
336         StringBuffer doc = new StringBuffer();
337         doc.append("<html>\n")   
338            .append("<head/>\n")
339            .append("<body>\n")
340            .append("<p>\n")
341            .append("</body>\n")
342            .append("</html>");
343         
344         try {
345            List nodes = p.parse(doc);
346            assertTrue("Failed unmatched begining", false);
347         } catch (Exception e) {
348            assertTrue("Failed unmatched begining test", e.getMessage().startsWith("Unmatched begining token"));
349         }
350 
351     }
352 
353     
354     /***
355      * Checks to see if nested tables parse correctly
356      */
357     public void testNestedTables() {
358         Parser p = new Parser();
359         StringBuffer doc = new StringBuffer();
360         doc
361                 .append("<table>")
362                 .append("<tr><td><table><tr><td></td></tr></table></td>")
363                 .append(
364                         "<td><span jsfid=\"tabs\"><ul id=\"menu\"><li id=\"nav\"><a href=\"#\">Tab 1</a></li><li id=\"nav-sel\"><a href=\"#\">Tab 2</a></li><li id=\"nav\"><a href=\"#\">Tab 3</a></li></ul></span>")
365                 .append("</td></tr>")
366                 .append("</table>")
367                 .append("<div id=\"contents\">")
368                 .append("<table border=\"0\">")
369                 .append("<tr><td rowspan=\"3\">")
370                 .append("<span jsfid=\"contactTable\">")
371                 .append("<table class=\"contacts\">")
372                 .append("<tr class=\"contactsHeader\">              ")
373                 .append("<td>")
374                 .append("Contacts")
375                 .append("</td>")
376                 .append("</tr>    ")
377                 .append("<tr class=\"contactsRow1\">")
378                 .append("<td>")
379                 .append("<a href=\"#\">ABC Company</a>")
380                 .append("</td>")
381                 .append("</tr>")
382                 .append("<tr class=\"contactsRow2\">")
383                 .append("<td>")
384                 .append("<a href=\"#\">XYZ Company</a>")
385                 .append("</td>")
386                 .append("</tr>")
387                 .append("</table>")
388                 .append("</span>")
389                 .append("</td></tr>")
390                 .append("<tr><td>")
391                 .append("<table>")
392                 .append(
393                         "<tr><td><label style=\"color:#99CC66\" jsfid=\"contactNameLabel\">Name:</label></td><td><input jsfid=\"contactName\" type=text size=40 maxlength=\"50\"/></td><td><span style=\"color:red\" jsfid=\"contactNameMessage\">Mock Error Name</span></td></tr></table></td></tr>")
394                 .append("</table>").append("</div>");
395 
396         List nodes = p.parse(doc);
397         assertTrue("Well-formed nested table has 1 root node",
398                 nodes.size() == 2);
399 
400     }
401 
402     /***
403      * <p>
404      * Aserts that two trees of parsed HTML have the same number children and
405      * the same attributes. Verifies that the structure is the same
406      * </p>
407      */
408     protected void compareTrees(List tree1, List tree2) {
409 
410         boolean isSame = (tree1 == null && tree2 == null)
411                 || (tree1.size() == tree2.size());
412 
413         assertTrue("Trees have same # children", isSame);
414         if (tree1 != null && tree2 != null) {
415             for (int i = 0; i < tree1.size(); i++) {
416                 Node node1 = (Node) tree1.get(i);
417                 Node node2 = (Node) tree2.get(i);
418 
419                 isSame = false;
420                 if (node1 != null && node2 != null) {
421                     if (node1.getName() == null && node2.getName() == null)
422                         isSame = true;
423                     else
424                         isSame = (node1.getName() != null
425                                 && node2.getName() != null && node1.getName()
426                                 .equalsIgnoreCase(node2.getName()));
427 
428                     assertTrue("Nodes names are equal", isSame);
429 
430                     isSame = (node1.getAttributes().size() == node2
431                             .getAttributes().size());
432                     assertTrue("Nodes have same # attributes", isSame);
433                     Iterator ki = node1.getAttributes().keySet().iterator();
434                     while (ki.hasNext()) {
435                         String key = (String) ki.next();
436                         String value1 = (String) node1.getAttributes().get(key);
437                         String value2 = (String) node2.getAttributes().get(key);
438                         isSame = value1.equalsIgnoreCase(value2);
439                         assertTrue("Nodes have same attribute value", isSame);
440 
441                     }
442                     compareTrees(node1.getChildren(), node2.getChildren());
443 
444                 }
445             }
446 
447         }
448     }
449     
450     /***
451      * <p>Test attribute parsing.</p>
452      */
453     public void testAttributes() {
454         Parser p = new Parser();
455         StringBuffer doc = new StringBuffer();
456         doc.append("<input type=text size=\"10\" name=date>");
457    
458         List nodes = p.parse(doc);
459         assertTrue("1 root node", nodes.size() == 1);
460 
461         Node node = (Node) nodes.get(0);
462 
463         String value = (String) node.getAttributes().get("type");
464         assertEquals("Attribute type", "text", value);
465         
466         value = (String) node.getAttributes().get("size");
467         assertEquals("Attribute size", "10", value);
468         
469         value = (String) node.getAttributes().get("name");
470         assertEquals("Attribute name", "date", value);
471 
472         //test linespan with bogus whitespace
473         doc.setLength(0);
474         doc.append("<input \n type=text \t\r \nsize=\"10\" \nname=date\r>");
475    
476         nodes = p.parse(doc);
477         assertTrue("1 root node", nodes.size() == 1);
478 
479         node = (Node) nodes.get(0);
480 
481         value = (String) node.getAttributes().get("type");
482         assertEquals("Attribute type", "text", value);
483         
484         value = (String) node.getAttributes().get("size");
485         assertEquals("Attribute size", "10", value);
486         
487         value = (String) node.getAttributes().get("name");
488         assertEquals("Attribute name", "date", value);
489 
490        
491         //test bogus attribute with missing delimiter
492         doc.setLength(0);
493         doc.append("<input type=\"text \" size=\"10 name=date>");
494 
495         nodes = p.parse(doc);
496         assertTrue("1 root node", nodes.size() == 1);
497         
498         node = (Node) nodes.get(0);
499 
500         value = (String) node.getAttributes().get("type");
501         assertEquals("Attribute type ", "text ", value);
502 
503         value = (String) node.getAttributes().get("size");
504         assertEquals("Attribute size", "10", value);
505 
506         value = (String) node.getAttributes().get("name");
507         assertEquals("Attribute name", "date", value);
508 
509       
510         //test bogus attribute with missing delimiter
511         doc.setLength(0);
512         doc.append("<input type=radio\" name=date checked>");
513 
514         nodes = p.parse(doc);
515         assertTrue("1 root node", nodes.size() == 1);
516         
517         node = (Node) nodes.get(0);
518 
519         value = (String) node.getAttributes().get("type");
520         assertEquals("Attribute type", "radio", value);
521 
522         value = (String) node.getAttributes().get("name");
523         assertEquals("Attribute name", "date", value);
524         
525         assertTrue("Attribute checked exists", node.getAttributes().containsKey("checked"));
526         value = (String) node.getAttributes().get("checked");
527         assertNull("Attribute checked", value);
528 
529 
530         doc.setLength(0);
531         doc.append("<option selected value=\"\"/>");
532 
533         nodes = p.parse(doc);
534         assertTrue("1 root node", nodes.size() == 1);
535         
536         node = (Node) nodes.get(0);
537 
538         assertTrue("Attribute selected exists", node.getAttributes().containsKey("selected"));
539         value = (String) node.getAttributes().get("selected");
540         assertNull("Attribute selected", value);
541         
542         assertTrue("Attribute selected exists", node.getAttributes().containsKey("value"));
543         value = (String) node.getAttributes().get("value");
544         assertNull("Attribute value", value);
545 
546         //test tabbed attributes
547         doc.setLength(0);
548         doc.append("<a\thref=\"http://www.acme.com\">\nAcme Company</a>");
549    
550         nodes = p.parse(doc);
551         assertTrue("1 root node", nodes.size() == 1);
552 
553         node = (Node) nodes.get(0);
554         assertTrue("Node is well-formed", node.isWellFormed());
555 
556         
557         value = (String) node.getAttributes().get("href");
558         assertEquals("Attribute href",  "http://www.acme.com", value);   
559             
560     }
561     
562     public void testTable() {
563         Parser p = new Parser();
564         StringBuffer doc = new StringBuffer();
565 
566         doc.append("<table>")
567            .append("<tr><th></th><th></tr>")
568            .append("<tr><td></td><td></tr>")
569            .append("</table>");
570         
571         List nodes = p.parse(doc);
572         assertEquals("table, 1 root node", nodes.size(), 1);   
573         
574         Node table = (Node) nodes.get(0);
575         assertNotNull("table", table);
576         
577         List trs = table.getChildren();
578         assertEquals("trs, 2 root nodes", 2, trs.size());   
579         
580         Node tr = (Node) trs.get(0);
581         assertNotNull("tr row 1", tr);
582         
583         List ths = tr.getChildren();
584         assertEquals("ths, 2 root nodes", 2, ths.size());           
585         
586         tr = (Node) trs.get(1);
587         assertNotNull("tr row 2", tr);
588         
589         List tds = tr.getChildren();
590         assertEquals("tds, 2 root nodes", 2, tds.size());           
591         
592         
593     }
594     
595     public void testJSPTag() {
596         Parser p = new Parser();
597         StringBuffer doc = new StringBuffer();
598         
599         doc.append("<%@ taglib uri=\"/tags/struts-html\" prefix=\"html\" %>")
600            .append("<%@ taglib uri=\"/tags/struts-bean\" prefix=\"bean\" %>")
601            .append("<%@ page language=\"java\" contentType=\"text/html; charset=ISO-8859-1\" session=\"true\" %>")
602            .append("<%@ page errorPage=\"../error.jsp\" %>")
603            .append("<table>")
604            .append("<tr><td>")
605            .append("<bean:message key=\"msg.username\"/><td><html:text property=\"userName\" size=\"10\" maxlength=\"10\"/>")
606            .append("<tr><td>")
607            .append("<bean:message key=\"msg.password\"/><td><html:text property=\"password\" size=\"10\" maxlength=10/><br>")
608            .append("</table>");
609         
610         List nodes = p.parse(doc);
611         assertTrue("Well-formed JSP, 5 root nodes", nodes.size() == 5);
612         
613         Node root = (Node) nodes.get(4);
614         assertNotNull("table node not null", root);
615 
616         Node tr = (Node) root.getChildren().get(0);
617         assertNotNull("tr node not null", tr);
618 
619         Node td = (Node) tr.getChildren().get(0);
620         assertNotNull("td node not null", td);
621 
622         Node message = (Node) td.getChildren().get(0);
623         assertNotNull("bean:message node not null", message);
624         
625         assertEquals("node.name", "message", message.getName());
626         assertEquals("node.qname", "bean", message.getQname());
627         assertEquals("node.attributes.key", "msg.username", message.getAttributes().get("key"));
628        
629         td = (Node) tr.getChildren().get(1);
630         assertNotNull("td node not null", td);
631 
632         Node text = (Node) td.getChildren().get(0);
633         assertNotNull("bean:message node not null", text);
634         
635         assertEquals("node.name", "text", text.getName());
636         assertEquals("node.qname", "html", text.getQname());
637         assertEquals("node.attributes.property", "userName", text.getAttributes().get("property"));
638         assertEquals("node.attributes.size", "10", text.getAttributes().get("size"));
639         assertEquals("node.attributes.maxlength", "10", text.getAttributes().get("maxlength"));  
640 
641     }
642     
643     //parse document ending in text
644     public void testPlainText() {
645    
646         Parser p = new Parser();
647         StringBuffer doc = new StringBuffer();
648         
649         doc.append("<br/>test");
650 
651         List nodes = p.parse(doc);
652         assertTrue("Plain text, 2 node", nodes.size() == 2);
653 
654         Node root = (Node) nodes.get(1);
655         assertNotNull("last node", root);
656 
657         assertEquals("node raw text", "test", root.getToken().getRawText());
658 
659 
660         doc.setLength(0);
661         doc.append("This is a test.");
662 
663         nodes = p.parse(doc);
664         assertTrue("Plain text, 1 node", nodes.size() == 1);
665 
666         root = (Node) nodes.get(0);
667         assertNotNull("first node", root);
668 
669         assertEquals("node raw text", doc.toString(), root.getToken().getRawText());
670 
671         
672     }
673     
674     
675     /***
676      * <p>Test parsing a DOCTYPE Tag.</p>
677      */
678     public void testDoctype() {
679         Parser p = new Parser();
680         StringBuffer doc = new StringBuffer();
681         doc.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" ")
682            .append("\"http://www.w3.org/TR/html4/strict.dtd\">")
683            .append("<HTML>")
684            .append("<HEAD>")
685            .append("<TITLE>My first HTML document</TITLE>")
686            .append("</HEAD>")
687            .append("<BODY>")
688            .append("<P>Hello world!</p>")
689            .append("</BODY>")
690            .append("</HTML>");
691 
692 
693         List nodes = p.parse(doc);
694         assertTrue("Well-formed DOCTYPE, 2 root nodes",
695                 nodes.size() == 2);
696 
697     }
698     
699     
700     public void testNameSpace() {
701         Parser p = new Parser();
702         StringBuffer doc = new StringBuffer();
703         doc.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
704            .append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" ") 
705            .append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">")
706            .append("<html xmlns=\"http://www.w3.org/1999/xhtml\" ")
707            .append("xmlns:clay=\"http://shale.apache.org/xml/clay\" ")
708            .append("xml:lang=\"en\" ")  
709            .append("lang=\"en\">")
710            .append("<head>")
711            .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>")
712            .append("<title>Test</title>")
713            .append("</head>")
714            .append("<body>")
715            .append("<clay:component jsfid=\"outputText\" value=\"testing\"/>")
716            .append("<div xmlns:clay=\"http://shale.apache.org/dtds/shale-clay\">")
717            .append("<clay:component jsfid=\"outputText\" value=\"testing\"/>")  
718            .append("</div>")
719            .append("</body>")
720            .append("</html>");
721         
722         
723         List roots = p.parse(doc);
724         assertEquals(3, roots.size());
725         Node root = (Node) roots.get(2);
726         assertNotNull(root);
727         
728         List nodes = root.getNodesByName("component");
729         assertEquals(2, nodes.size());
730         
731         Node clayComponent = (Node) nodes.get(0);
732         assertEquals("prefix", "clay", clayComponent.getQname());
733         
734         // clay namespace
735         String uri = clayComponent.getNamespaceURI(clayComponent.getQname());
736         assertEquals("uri", "http://shale.apache.org/xml/clay", uri);
737         
738         // default namespace
739         uri = clayComponent.getNamespaceURI(null);
740         assertEquals("uri", "http://www.w3.org/1999/xhtml", uri);
741 
742         
743         clayComponent = (Node) nodes.get(1);
744         assertEquals("prefix", "clay", clayComponent.getQname());
745         
746         // clay namespace
747         uri = clayComponent.getNamespaceURI(clayComponent.getQname());
748         assertEquals("uri", "http://shale.apache.org/dtds/shale-clay", uri);
749         
750         // default namespace
751         uri = clayComponent.getNamespaceURI(null);
752         assertEquals("uri", "http://www.w3.org/1999/xhtml", uri);
753         
754     }
755     
756     
757     public void testParseCDATA() {
758         Parser p = new Parser();
759         StringBuffer doc = new StringBuffer();
760         doc.append("<jsp:text\njsp:id=\"9\"\n><![CDATA[\n<html>\n]]>\n</jsp:text>");
761         doc.append("<jsp:text\njsp:id=\"10\"\n><![CDATA[\n<head>\n]]>\n</jsp:text>");
762         doc.append("<jsp:text\njsp:id=\"11\"\n><![CDATA[\n</title>\n]]>\n</jsp:text>");
763         doc.append("<jsp:text\njsp:id=\"12\"\n><![CDATA[\n<!-- </title> -->\n]]>\n</jsp:text>");
764         
765         List roots = p.parse(doc);
766         assertEquals(4, roots.size());
767         
768         for (int i = 0; i < roots.size(); i++) {
769            Node node = (Node) roots.get(i);  
770            assertEquals("jsp", node.getQname());
771            assertEquals("text", node.getName());
772            
773            String id = (String) node.getAttributes().get("jsp:id");
774            assertEquals(String.valueOf(i + 9), id);
775            
776            
777            
778         }
779                 
780     }
781     
782     
783     public void testRemoveBlock() {
784         Parser p = new Parser();
785         StringBuffer doc = new StringBuffer();
786         doc.append("<!-- ### clay:remove ### -->")
787            .append("<html>")
788            .append("<div class=\"content\">")
789            .append("<!-- ### /clay:remove ### -->")
790            .append("<!--")
791            .append("    this is the body content to be placed")
792            .append("    in the basic layout HTML template defined elsewhere")
793            .append("-->")
794            .append("<form>")
795            .append("<input type=\"text\" value=\"#{contentBean.inputText}\"/>")
796            .append("<input type=\"submit\" action=\"#{contentBean.doSubmit}\"/>")
797            .append("</form>")
798            .append("<!-- ### clay:remove ### -->")
799            .append("</div>")
800            .append("<!--   this is the basic layout to be removed when converting to an include -->")
801            .append("</html>")
802            .append("<!-- ### /clay:remove ### -->");     
803         
804         List roots = p.parse(doc);
805         assertNotNull(roots);
806         
807         assertEquals("root nodes", 2, roots.size());
808         
809         Node comment = (Node) roots.get(0);
810         
811         assertEquals("isComment", true, comment.isComment());
812         
813         Node form = (Node) roots.get(1);
814         
815         assertEquals("form well-formed", true, form.isWellFormed());
816         assertEquals("form name", "form", form.getName());
817         assertEquals("form children", 2, form.getChildren().size());
818         
819         
820     }
821 
822     
823     public void testRemoveCharset() {
824         Parser p = new Parser();
825         StringBuffer doc = new StringBuffer();
826         doc.append("<!-- ### clay:page charset=\"UTF-8\" ### -->")
827            .append("<html></html>");
828 
829         List roots = p.parse(doc);
830         assertNotNull(roots);
831         
832         //charset comment should be removed
833         assertEquals("root nodes", 1, roots.size());
834 
835         Node root = (Node) roots.get(0); 
836         assertEquals("root node", "html", root.getName());
837 
838     }
839     
840 
841 }