c3ba38a3c0a351a7c4a7498066c509d00e7e7085
[gcc.git] /
1 /* Parser_Test.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37
38
39
40 package test.gnu.javax.swing.text.html.parser;
41
42 import java.io.StringReader;
43
44 import java.util.Enumeration;
45 import java.util.Iterator;
46 import java.util.TreeSet;
47
48 import javax.swing.text.AttributeSet;
49 import javax.swing.text.MutableAttributeSet;
50 import javax.swing.text.SimpleAttributeSet;
51 import javax.swing.text.html.HTML;
52 import javax.swing.text.html.HTMLEditorKit;
53 import javax.swing.text.html.HTMLEditorKit.ParserCallback;
54 import javax.swing.text.html.parser.ParserDelegator;
55 import javax.swing.text.html.parser.TagElement;
56
57 /**
58 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
59 */
60 public class Parser_Test
61 extends HTMLEditorKit.ParserCallback
62 {
63 public boolean hideImplied = true;
64 protected StringBuffer out = new StringBuffer();
65 AttributeSet atts = new SimpleAttributeSet();
66
67 public void generate(String x, String comment)
68 throws Exception
69 {
70 String prolog = "<html><head></head><body>";
71 String epilog = "</body></html>";
72 String html = x; // prolog+x+epilog;
73 System.out.println("// Test " + comment + ".");
74 System.out.println("v.verify(\"" + html + "\",\n \"" + verify(html, null) +
75 "\");"
76 );
77 }
78
79 public void handleComment(char[] parm1, int position)
80 {
81 out.append("{" + new String(parm1) + "}");
82 }
83
84 public void handleEndTag(HTML.Tag tag, int position)
85 {
86 out.append("</" + tag + ">");
87 }
88
89 public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attributes,
90 int position
91 )
92 {
93 if (tag.toString().equals("#pcdata"))
94 return;
95 out.append("<" + tag);
96 dumpAttributes(attributes);
97 out.append("/>");
98 }
99
100 public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes,
101 int position
102 )
103 {
104 out.append("<" + tag);
105 dumpAttributes(attributes);
106 out.append('>');
107 }
108
109 public void handleText(char[] chars, int position)
110 {
111 out.append("'" + new String(chars) + "'");
112 }
113
114 public String verify(String html, String trace)
115 throws Exception
116 {
117 out.setLength(0);
118
119 HTMLEditorKit.ParserCallback callback = this;
120 ParserDelegator delegator = new ParserDelegator();
121 delegator.parse(new StringReader(html), callback, true);
122
123 String ou = out.toString();
124 if (trace != null)
125 {
126 if (!ou.equals(trace))
127 {
128 System.err.println("Unable to parse '" + html + "':");
129 System.err.println(" expected: '" + trace + "',");
130 System.out.println(" returned: '" + ou + "'.");
131 throw new Exception("'" + html + "' -> '" + ou + "' expected '" +
132 trace + "'"
133 );
134 }
135 }
136 return ou;
137 }
138
139 protected void dumpAttributes(AttributeSet atts)
140 {
141 Enumeration enum = atts.getAttributeNames();
142
143 // Sort them to ensure the same order every time:
144 TreeSet t = new TreeSet();
145 while (enum.hasMoreElements())
146 t.add(enum.nextElement().toString());
147
148 Iterator iter = t.iterator();
149
150 while (iter.hasNext())
151 {
152 String a = iter.next().toString();
153
154 if (hideImplied)
155 if (a.equals("_implied_"))
156 continue;
157
158 String v = atts.getAttribute(a).toString();
159 out.append(" " + a + "='" + v + "'");
160 }
161 }
162 }