001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.text.html.parser;
021:
022: import java.util.BitSet;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.Vector;
026:
027: import javax.swing.SwingTestCase;
028:
029: import junit.framework.TestCase;
030:
031: public class DTD401Test extends TestCase {
032: DTD dtd;
033: Vector elements;
034:
035: protected void setUp() throws Exception {
036: dtd = Utils.getFilledDTD();
037: super .setUp();
038: }
039:
040: private void checkBitSet(final BitSet bs, final String descr) {
041: boolean isEmptyBs = (bs == null || bs.cardinality() == 0);
042: boolean isEmptyDescr = descr == null || descr == "";
043: assertFalse(isEmptyBs ^ isEmptyDescr);
044: if (!isEmptyBs) {
045: String[] names = descr.split("\\|");
046: for (int i = 0; i < names.length; i++) {
047: int index = ((Element) dtd.elementHash.get(names[i]))
048: .getIndex();
049: assertTrue(bs.get(index));
050: }
051: for (int i = bs.nextSetBit(0); i >= 0; i = bs
052: .nextSetBit(i + 1)) {
053: String name = ((Element) dtd.elements.get(i)).getName();
054: boolean result = false;
055: for (int j = 0; j < names.length; j++) {
056: if (names[j].equals(name)) {
057: result = true;
058: break;
059: }
060: }
061: assertTrue(result);
062: }
063: }
064: }
065:
066: private String tagsToString(final boolean oStart, final boolean oEnd) {
067: return (oStart ? "O" : "-") + " " + (oEnd ? "O" : "-");
068: }
069:
070: public void testEntities() {
071: if (!SwingTestCase.isHarmony()) {
072: Utils401.check32Entities(dtd.entityHash);
073: } else {
074: Utils401.check401Entities(dtd.entityHash);
075: }
076: }
077:
078: public void testElements() {
079: if (!SwingTestCase.isHarmony()) {
080: return;
081: }
082: ElementDescrHash hash = new ElementDescrHash();
083: ElementsHandler.initAttributes();
084: ElementsHandler.initElementsCreation(hash);
085: assertEquals(hash.size() + 2, dtd.elementHash.size());
086: assertEquals(hash.size() + 2, dtd.elements.size());
087: Iterator iter = hash.keySet().iterator();
088: for (int i = 0; i < dtd.elements.size(); i++) {
089: String name = ((Element) dtd.elements.get(i)).getName();
090: if (!hash.containsKey(name)) {
091: assertTrue("UNKNOWN".equals(name)
092: || "#PCDATA".equals(name));
093: }
094: }
095:
096: while (iter.hasNext()) {
097: String name = (String) iter.next();
098: String descr = (String) hash.get(name);
099: String atts = (String) ElementsHandler.atts.get(name);
100: checkElement(name, descr, atts);
101: }
102: }
103:
104: public void testFields() {
105: if (!SwingTestCase.isHarmony()) {
106: return;
107: }
108: ElementDescrHash hash = new ElementDescrHash();
109: ElementsHandler.initAttributes();
110: ElementsHandler.initElementsCreation(hash);
111:
112: Element[] elements = new Element[] { dtd.html, dtd.meta,
113: dtd.base, dtd.isindex, dtd.head, dtd.body, dtd.applet,
114: dtd.param, dtd.p, dtd.title };
115: for (int i = 0; i < elements.length; i++) {
116: Element element = elements[i];
117: String name = element.getName();
118: String descr = (String) hash.get(name);
119: String atts = (String) ElementsHandler.atts.get(name);
120: checkElement(element, descr, atts);
121: }
122:
123: }
124:
125: private void checkElement(final Element elem, final String descr,
126: final String atts) {
127: assertEquals(dtd.elements.get(elem.getIndex()), elem);
128: assertEquals(dtd.elementHash.get(elem.name), elem);
129: String[] splDescr = DTDUtilities.splitElementDescr(descr);
130: checkTags(elem.oStart, elem.oEnd, splDescr[0]);
131: checkBitSet(elem.exclusions, splDescr[2]);
132: checkBitSet(elem.inclusions, splDescr[3]);
133: checkContentModel(elem.name, elem.content, splDescr[1]);
134: checkType(elem, splDescr[1]);
135: checkAttributes(elem, atts);
136: }
137:
138: private void checkElement(final String name, final String descr,
139: final String atts) {
140: Element elem = (Element) dtd.elementHash.get(name);
141: checkElement(elem, descr, atts);
142: }
143:
144: private void checkType(final Element elem, final String descr) {
145: int type = elem.type;
146: if (descr.equals("EMPTY")) {
147: assertEquals(DTDConstants.EMPTY, type);
148: } else if (descr.equals("CDATA")) {
149: assertEquals(DTDConstants.CDATA, type);
150: } else {
151: assertEquals(DTDConstants.MODEL, type);
152: }
153: }
154:
155: private void checkContentModel(final String name,
156: final ContentModel contentModel, final String descr) {
157: if (contentModel == null) {
158: assertTrue(descr.equals("EMPTY") || descr.equals("CDATA"));
159: } else if (descr.equals("EMPTY")) {
160: assertNull(contentModel);
161: } else if (!descr.equals("CDATA")) {
162: String content = contentModel.toString();
163: if (!checExtraordinaryCase(name, content, descr)) {
164: assertEquals(descr, content);
165: }
166: }
167: }
168:
169: private boolean checExtraordinaryCase(final String name,
170: final String content, final String descr) {
171: if (name.equals("COLGROUP") || name.equals("TFOOT")
172: || name.equals("DIR") || name.equals("MENU")
173: || name.equals("OPTGROUP") || name.equals("OL")
174: || name.equals("UL") || name.equals("TBODY")
175: || name.equals("THEAD")) {
176: String s1 = "("
177: + content.substring(0, content.length() - 1) + ")"
178: + content.charAt(content.length() - 1);
179: assertEquals(descr, s1);
180: return true;
181: } else if (name.equals("TABLE") || name.equals("HTML")
182: || name.equals("#PCDATA") || name.equals("TEXTAREA")
183: || name.equals("FIELDSET") || name.equals("TITLE")
184: || name.equals("HEAD") || name.equals("OPTION")
185: || name.equals("FRAMESET")) {
186: String s1 = "(" + content + ")";
187: assertEquals(descr, s1);
188: return true;
189: } else if (name.equals("ADDRESS")) {
190: String s1 = "("
191: + content.substring(0, content.length() - 4)
192: + ")"
193: + content.substring(content.length() - 4, content
194: .length());
195: assertEquals(descr, s1);
196: return true;
197: } else if (name.equals("MAP")) {
198: String s1 = "("
199: + content.substring(0, content.length() - 7)
200: + ")"
201: + content.substring(content.length() - 7, content
202: .length());
203: assertEquals(descr, s1);
204: return true;
205: }
206: return false;
207: }
208:
209: private void checkTags(final boolean oStart, final boolean oEnd,
210: final String desc) {
211: assertEquals(desc, tagsToString(oStart, oEnd));
212: }
213:
214: private void checkAttributes(final Element elem, final String atts) {
215: String name = elem.getName();
216: if (name.equals("INPUT") || name.equals("FORM")
217: || name.equals("FRAMESET")) {
218: assertEquals(atts.replaceAll("\\(|\\)", "").replaceAll(
219: "( )+", "").replaceAll("CDATAs;", "CDATA"),
220: elem.atts.paramString().replaceAll(",", " ")
221: .replaceAll("( )+", ""));
222:
223: } else {
224: assertEquals(atts.replaceAll("\\(|\\)", "").replaceAll(
225: "( )+", ""), elem.atts.paramString().replaceAll(
226: ",", " ").replaceAll("( )+", ""));
227: }
228: }
229:
230: static class ElementDescrHash extends Hashtable {
231:
232: public synchronized Object put(final Object key,
233: final Object value) {
234: String name = (String) key;
235: if (name.indexOf("|") >= 0) {
236: String[] names = DTDUtilities.replaceAndSplit(name);
237: for (int i = 0; i < names.length; i++) {
238: put(names[i], value);
239: }
240: return null;
241: } else {
242: return super .put(key, value);
243: }
244: }
245: }
246:
247: // public void testSer() {
248: // DTDUtilities.createBinaryDTD("C:/jenja.bd");
249: // }
250: }
|