2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
40 package test.gnu.javax.swing.text.html.parser;
42 import java.io.StringReader;
44 import java.util.Enumeration;
45 import java.util.Iterator;
46 import java.util.TreeSet;
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;
58 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
60 public class Parser_Test
61 extends HTMLEditorKit.ParserCallback
63 public boolean hideImplied = true;
64 protected StringBuffer out = new StringBuffer();
65 AttributeSet atts = new SimpleAttributeSet();
67 public void generate(String x, String comment)
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) +
79 public void handleComment(char[] parm1, int position)
81 out.append("{" + new String(parm1) + "}");
84 public void handleEndTag(HTML.Tag tag, int position)
86 out.append("</" + tag + ">");
89 public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attributes,
93 if (tag.toString().equals("#pcdata"))
95 out.append("<" + tag);
96 dumpAttributes(attributes);
100 public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes,
104 out.append("<" + tag);
105 dumpAttributes(attributes);
109 public void handleText(char[] chars, int position)
111 out.append("'" + new String(chars) + "'");
114 public String verify(String html, String trace)
119 HTMLEditorKit.ParserCallback callback = this;
120 ParserDelegator delegator = new ParserDelegator();
121 delegator.parse(new StringReader(html), callback, true);
123 String ou = out.toString();
126 if (!ou.equals(trace))
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 '" +
139 protected void dumpAttributes(AttributeSet atts)
141 Enumeration enum = atts.getAttributeNames();
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());
148 Iterator iter = t.iterator();
150 while (iter.hasNext())
152 String a = iter.next().toString();
155 if (a.equals("_implied_"))
158 String v = atts.getAttribute(a).toString();
159 out.append(" " + a + "='" + v + "'");