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 Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.io.StringReader;
023: import java.util.ArrayList;
024: import java.util.Enumeration;
025:
026: import javax.swing.BasicSwingTestCase;
027: import javax.swing.event.DocumentEvent;
028: import javax.swing.event.DocumentListener;
029: import javax.swing.text.AttributeSet;
030: import javax.swing.text.BadLocationException;
031: import javax.swing.text.DefStyledDoc_Helpers;
032: import javax.swing.text.GapContent;
033: import javax.swing.text.StyleConstants;
034: import javax.swing.text.DefaultStyledDocument.ElementSpec;
035: import javax.swing.text.html.HTMLEditorKit.ParserCallback;
036: import javax.swing.text.html.parser.ParserDelegator;
037:
038: public abstract class HTMLDocumentTestCase extends BasicSwingTestCase {
039:
040: public static class PublicHTMLDocument extends HTMLDocument {
041: private final Marker insertMarker = new Marker();
042: private final Marker createMarker = new Marker();
043:
044: private boolean editable = true;
045:
046: public PublicHTMLDocument(final StyleSheet sheet) {
047: super (sheet);
048: }
049:
050: public PublicHTMLDocument() {
051: super ();
052: }
053:
054: public PublicHTMLDocument(final GapContent content,
055: final StyleSheet styles) {
056: super (content, styles);
057: }
058:
059: public void lockWrite() {
060: writeLock();
061: }
062:
063: public void unlockWrite() {
064: writeUnlock();
065: }
066:
067: public void setEditable(final boolean editable) {
068: this .editable = editable;
069: }
070:
071: public Content getContentPublicly() {
072: return super .getContent();
073: }
074:
075: public AttributeContext getAttributeContextPublicly() {
076: return super .getAttributeContext();
077: }
078:
079: protected void insert(final int offset,
080: final ElementSpec[] specs) throws BadLocationException {
081: ArrayList info = insertMarker.getAuxiliary() != null ? (ArrayList) insertMarker
082: .getAuxiliary()
083: : new ArrayList();
084: info.add(specs);
085: info.add(new Integer(offset));
086: insertMarker.setAuxiliary(info);
087: insertMarker.setOccurred();
088: if (editable) {
089: super .insert(offset, specs);
090: }
091: }
092:
093: public Marker getInsertMarker() {
094: return insertMarker;
095: }
096:
097: protected void create(final ElementSpec[] specs) {
098: ArrayList info = createMarker.getAuxiliary() != null ? (ArrayList) createMarker
099: .getAuxiliary()
100: : new ArrayList();
101: info.add(specs);
102: createMarker.setAuxiliary(info);
103: createMarker.setOccurred();
104: if (editable) {
105: super .create(specs);
106: }
107: }
108:
109: public Marker getCreateMarker() {
110: return createMarker;
111: }
112: }
113:
114: public static class DocumentController extends EventsController
115: implements DocumentListener {
116: public void insertUpdate(final DocumentEvent e) {
117: processEvent(e);
118: }
119:
120: public void removeUpdate(final DocumentEvent e) {
121: processEvent(e);
122: }
123:
124: public void changedUpdate(final DocumentEvent e) {
125: processEvent(e);
126: }
127:
128: protected void processEvent(final DocumentEvent e) {
129: addEvent(Integer.toString(getNumEvents()), e);
130: if (isVerbose()) {
131: System.err.println(e);
132: }
133: }
134:
135: public DocumentEvent getEvent(final int index) {
136: return (DocumentEvent) super .getEvent(Integer
137: .toString(index));
138: }
139: }
140:
141: public static void assertSpec(final ElementSpec spec,
142: final short type, final short direction, final int offset,
143: final char[] array) {
144: int length = array != null ? array.length : 0;
145: assertSpec(spec, type, direction, offset, length, array);
146: }
147:
148: public static void assertSpec(final ElementSpec spec,
149: final short type, final short direction, final int offset,
150: final int length, final char[] array) {
151: DefStyledDoc_Helpers.assertSpec(spec, type, direction, offset,
152: length, length == 0);
153: if (array != null) {
154: assertEquals("text", new String(array), new String(spec
155: .getArray()));
156: }
157: }
158:
159: public static void checkOpenImpliedSpec(final ElementSpec spec) {
160: AttributeSet specAttr = spec.getAttributes();
161: assertEquals("number of attributes", 1, specAttr
162: .getAttributeCount());
163: checkAttributes(specAttr, StyleConstants.NameAttribute,
164: HTML.Tag.IMPLIED);
165: assertSpec(spec, ElementSpec.StartTagType,
166: ElementSpec.OriginateDirection, 0, null);
167: }
168:
169: public static void checkAttributes(final AttributeSet attr,
170: final Object key, final Object value) {
171: final Enumeration attributeNames = attr.getAttributeNames();
172: while (attributeNames.hasMoreElements()) {
173: Object name = (Object) attributeNames.nextElement();
174: if (name.equals(key)) {
175: assertEquals("attribute value", value.toString(), attr
176: .getAttribute(key).toString());
177: return;
178: }
179: }
180: fail("attribute is not found");
181: }
182:
183: public static void checkEndTagSpec(final ElementSpec spec) {
184: assertSpec(spec, ElementSpec.EndTagType,
185: ElementSpec.OriginateDirection, 0, null);
186: assertNull(spec.getAttributes());
187: }
188:
189: public static void checkStartJNTagSpec(final ElementSpec spec) {
190: assertSpec(spec, ElementSpec.StartTagType,
191: ElementSpec.JoinNextDirection, 0, null);
192: assertNull(spec.getAttributes());
193: }
194:
195: public static void checkImplicitContentSpec(ElementSpec spec) {
196: assertSpec(spec, ElementSpec.ContentType,
197: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
198: }
199:
200: public static void loadDocument(final HTMLDocument doc,
201: final String content) throws Exception {
202: final ParserCallback reader = doc.getReader(0);
203: new ParserDelegator().parse(new StringReader(content), reader,
204: true);
205: reader.flush();
206: }
207: }
|