0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017: /**
0018: * @author Alexander T. Simbirtsev
0019: * @version $Revision$
0020: */package javax.swing.text.html;
0021:
0022: import java.io.IOException;
0023: import java.io.StringReader;
0024: import java.util.ArrayList;
0025: import java.util.Vector;
0026:
0027: import javax.swing.text.AttributeSet;
0028: import javax.swing.text.BadLocationException;
0029: import javax.swing.text.DefaultEditorKit;
0030: import javax.swing.text.DefaultStyledDocument;
0031: import javax.swing.text.MutableAttributeSet;
0032: import javax.swing.text.PlainDocument;
0033: import javax.swing.text.SimpleAttributeSet;
0034: import javax.swing.text.StyleConstants;
0035: import javax.swing.text.DefaultStyledDocument.ElementSpec;
0036: import javax.swing.text.html.HTML.Tag;
0037: import javax.swing.text.html.HTMLDocument.HTMLReader;
0038: import javax.swing.text.html.HTMLDocument.HTMLReader.TagAction;
0039: import javax.swing.text.html.HTMLEditorKit.ParserCallback;
0040: import javax.swing.text.html.parser.ParserDelegator;
0041:
0042: public class HTMLDocument_ReaderTest extends HTMLDocumentTestCase {
0043:
0044: protected HTMLReader reader;
0045: protected HTMLDocument doc;
0046: private Marker createMarker;
0047: private Marker insertMarker;
0048: private boolean editable;
0049:
0050: protected void tearDown() throws Exception {
0051: doc = null;
0052: reader = null;
0053: super .tearDown();
0054: }
0055:
0056: protected void setUp() throws Exception {
0057: super .setUp();
0058: setIgnoreNotImplemented(true);
0059: init();
0060: }
0061:
0062: public void testFlush_Create() throws Exception {
0063: final String text = "tag";
0064: SimpleAttributeSet attr = new SimpleAttributeSet();
0065: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0066: .toString());
0067: reader.parseBuffer.add(new ElementSpec(attr,
0068: ElementSpec.StartTagType));
0069: reader.parseBuffer.add(new ElementSpec(
0070: new SimpleAttributeSet(), ElementSpec.ContentType, text
0071: .toCharArray(), 0, 3));
0072: reader.parseBuffer.add(new ElementSpec(attr,
0073: ElementSpec.EndTagType));
0074: assertEquals(3, reader.parseBuffer.size());
0075: reader.flush();
0076: assertEquals(0, reader.parseBuffer.size());
0077: assertTrue(createMarker.isOccurred());
0078: assertFalse(insertMarker.isOccurred());
0079: assertEquals(3,
0080: ((ElementSpec[]) createMarker.getAuxiliary()).length);
0081: }
0082:
0083: public void testFlush_NoCreate_IfEmptyBuffer() throws Exception {
0084: final String text = "tag";
0085: SimpleAttributeSet attr = new SimpleAttributeSet();
0086: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0087: .toString());
0088: assertEquals(0, reader.parseBuffer.size());
0089: reader.flush();
0090: assertEquals(0, reader.parseBuffer.size());
0091: assertFalse(createMarker.isOccurred());
0092: assertFalse(insertMarker.isOccurred());
0093: }
0094:
0095: public void testFlush_Create_PushPopNull() throws Exception {
0096: final String text = "tag";
0097: SimpleAttributeSet attr = new SimpleAttributeSet();
0098: attr.addAttribute(StyleConstants.NameAttribute, Tag.B);
0099: reader = (HTMLReader) doc.getReader(0, 5, 5, null);
0100: reader.parseBuffer.add(new ElementSpec(attr,
0101: ElementSpec.StartTagType));
0102: reader.parseBuffer.add(new ElementSpec(
0103: new SimpleAttributeSet(), ElementSpec.ContentType, text
0104: .toCharArray(), 0, 3));
0105: reader.parseBuffer.add(new ElementSpec(attr,
0106: ElementSpec.EndTagType));
0107: assertEquals(3, reader.parseBuffer.size());
0108: reader.flush();
0109: assertEquals(0, reader.parseBuffer.size());
0110: assertTrue(createMarker.isOccurred());
0111: assertFalse(insertMarker.isOccurred());
0112: assertEquals(3,
0113: ((ElementSpec[]) createMarker.getAuxiliary()).length);
0114: }
0115:
0116: public void testFlush_Insert_Tag() throws Exception {
0117: final String text = "tag";
0118: SimpleAttributeSet attr = new SimpleAttributeSet();
0119: attr.addAttribute(StyleConstants.NameAttribute, Tag.B);
0120: reader = (HTMLReader) doc.getReader(0, 0, 0, Tag.B);
0121: reader.parseBuffer.add(new ElementSpec(attr,
0122: ElementSpec.StartTagType));
0123: reader.parseBuffer.add(new ElementSpec(
0124: new SimpleAttributeSet(), ElementSpec.ContentType, text
0125: .toCharArray(), 0, 3));
0126: reader.parseBuffer.add(new ElementSpec(attr,
0127: ElementSpec.EndTagType));
0128: assertEquals(3, reader.parseBuffer.size());
0129: reader.flush();
0130: assertEquals(0, reader.parseBuffer.size());
0131: assertFalse(createMarker.isOccurred());
0132: assertTrue(insertMarker.isOccurred());
0133: assertEquals(3,
0134: ((ElementSpec[]) insertMarker.getAuxiliary()).length);
0135: assertEquals(text, doc.getText(0, doc.getLength()));
0136: }
0137:
0138: public void testFlush_Insert_PushPopTag() throws Exception {
0139: final String text = "tag";
0140: editable = false;
0141: SimpleAttributeSet attr = new SimpleAttributeSet();
0142: attr.addAttribute(StyleConstants.NameAttribute, Tag.B);
0143: reader = (HTMLReader) doc.getReader(0, 15, 33, Tag.B);
0144: reader.parseBuffer.add(new ElementSpec(attr,
0145: ElementSpec.StartTagType));
0146: reader.parseBuffer.add(new ElementSpec(
0147: new SimpleAttributeSet(), ElementSpec.ContentType, text
0148: .toCharArray(), 0, 3));
0149: reader.parseBuffer.add(new ElementSpec(attr,
0150: ElementSpec.EndTagType));
0151: assertEquals(3, reader.parseBuffer.size());
0152: reader.flush();
0153: assertEquals(0, reader.parseBuffer.size());
0154: assertFalse(createMarker.isOccurred());
0155: assertTrue(insertMarker.isOccurred());
0156: assertEquals(3,
0157: ((ElementSpec[]) insertMarker.getAuxiliary()).length);
0158: }
0159:
0160: public void testFlush_Insert_PushPopTag_Wierd() throws Exception {
0161: final String text = "tag";
0162: editable = false;
0163: SimpleAttributeSet attr = new SimpleAttributeSet();
0164: attr.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT);
0165: reader = (HTMLReader) doc.getReader(1000, -15, 330, Tag.HTML);
0166: reader.parseBuffer.add(new ElementSpec(attr,
0167: ElementSpec.EndTagType));
0168: reader.parseBuffer.add(new ElementSpec(attr,
0169: ElementSpec.StartTagType));
0170: reader.parseBuffer.add(new ElementSpec(
0171: new SimpleAttributeSet(), ElementSpec.ContentType, text
0172: .toCharArray(), 0, 3));
0173: reader.parseBuffer.add(new ElementSpec(attr,
0174: ElementSpec.EndTagType));
0175: reader.parseBuffer.add(new ElementSpec(attr,
0176: ElementSpec.EndTagType));
0177: reader.parseBuffer.add(new ElementSpec(attr,
0178: ElementSpec.StartTagType));
0179: assertEquals(6, reader.parseBuffer.size());
0180: reader.flush();
0181: assertEquals(0, reader.parseBuffer.size());
0182: assertFalse(createMarker.isOccurred());
0183: assertTrue(insertMarker.isOccurred());
0184: assertEquals(6,
0185: ((ElementSpec[]) insertMarker.getAuxiliary()).length);
0186: }
0187:
0188: public void testFlush_Insert_PushPopTag_ApplicationMoment1()
0189: throws Exception {
0190: SimpleAttributeSet attr = new SimpleAttributeSet();
0191: reader = (HTMLReader) doc.getReader(1, 10, 100, Tag.I);
0192: reader.handleStartTag(Tag.I, new SimpleAttributeSet(), 0);
0193: assertEquals(110, reader.parseBuffer.size());
0194: }
0195:
0196: public void testFlush_Insert_PushPopTag_ApplicationMoment2()
0197: throws Exception {
0198: SimpleAttributeSet attr = new SimpleAttributeSet();
0199: reader = (HTMLReader) doc.getReader(-100, 10, 100, Tag.I);
0200: reader.registerTag(Tag.I, reader.new TagAction());
0201: reader.handleStartTag(Tag.I, new SimpleAttributeSet(), 1000);
0202: reader.handleSimpleTag(Tag.I, new SimpleAttributeSet(), 1000);
0203: reader.handleSimpleTag(Tag.BR, new SimpleAttributeSet(), 1000);
0204: assertEquals(0, reader.parseBuffer.size());
0205: }
0206:
0207: public void testFlush_Insert_PushPopTag_ApplicationMoment3()
0208: throws Exception {
0209: SimpleAttributeSet attr = new SimpleAttributeSet();
0210: reader = (HTMLReader) doc.getReader(-100, 10, 100, Tag.I);
0211: reader.registerTag(Tag.I, reader.new BlockAction());
0212: reader.handleStartTag(Tag.I, new SimpleAttributeSet(), 1000);
0213: assertEquals(111, reader.parseBuffer.size());
0214: }
0215:
0216: public void testFlush_Insert_PushPopTag_ApplicationMoment4()
0217: throws Exception {
0218: SimpleAttributeSet attr = new SimpleAttributeSet();
0219: reader = (HTMLReader) doc.getReader(-100, 10, 100, Tag.I);
0220: reader.new BlockAction().start(Tag.I, new SimpleAttributeSet());
0221: assertEquals(111, reader.parseBuffer.size());
0222: }
0223:
0224: public void testFlush_Insert_PushPopTag_ApplicationMoment5()
0225: throws Exception {
0226: SimpleAttributeSet attr = new SimpleAttributeSet();
0227: reader = (HTMLReader) doc.getReader(-100, 0, 0, Tag.I);
0228: reader.registerTag(Tag.I, reader.new CharacterAction());
0229: reader.handleStartTag(Tag.I, new SimpleAttributeSet(), 1000);
0230: assertEquals(0, reader.parseBuffer.size());
0231: reader.handleSimpleTag(Tag.IMG, new SimpleAttributeSet(), 100);
0232: assertEquals(1, reader.parseBuffer.size());
0233: }
0234:
0235: public void testFlush_Insert_PushPopTag_SpecsOrder()
0236: throws Exception {
0237: SimpleAttributeSet attr = new SimpleAttributeSet();
0238: reader = (HTMLReader) doc.getReader(-100, 2, 3, Tag.I);
0239: reader.handleStartTag(Tag.I, new SimpleAttributeSet(), 1000);
0240: assertEquals(5, reader.parseBuffer.size());
0241: for (int i = 0; i < 5; i++) {
0242: checkSpecType("eoeosnsnsn", 0,
0243: (ElementSpec) reader.parseBuffer.get(0));
0244: }
0245: }
0246:
0247: public void testFlush_Insert() throws Exception {
0248: final String text = "tag";
0249: final String initialText = "text";
0250: doc.insertString(0, initialText, SimpleAttributeSet.EMPTY);
0251: SimpleAttributeSet attr = new SimpleAttributeSet();
0252: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0253: .toString());
0254: reader.parseBuffer.add(new ElementSpec(attr,
0255: ElementSpec.StartTagType));
0256: reader.parseBuffer.add(new ElementSpec(
0257: SimpleAttributeSet.EMPTY, ElementSpec.ContentType, text
0258: .toCharArray(), 0, 3));
0259: reader.parseBuffer.add(new ElementSpec(attr,
0260: ElementSpec.EndTagType));
0261: assertEquals(3, reader.parseBuffer.size());
0262: reader.flush();
0263: assertEquals(0, reader.parseBuffer.size());
0264: assertFalse(createMarker.isOccurred());
0265: assertTrue(insertMarker.isOccurred());
0266: assertEquals(3,
0267: ((ElementSpec[]) insertMarker.getAuxiliary()).length);
0268: assertEquals(text + initialText, doc
0269: .getText(0, doc.getLength()));
0270: }
0271:
0272: public void testFlush_NoInsert_IfEmptyBuffer() throws Exception {
0273: final String text = "tag";
0274: final String initialText = "text";
0275: doc.insertString(0, initialText, SimpleAttributeSet.EMPTY);
0276: SimpleAttributeSet attr = new SimpleAttributeSet();
0277: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0278: .toString());
0279: assertEquals(0, reader.parseBuffer.size());
0280: reader.flush();
0281: assertEquals(0, reader.parseBuffer.size());
0282: assertFalse(createMarker.isOccurred());
0283: if (!isHarmony()) {
0284: assertTrue(insertMarker.isOccurred());
0285: assertEquals(0, ((ElementSpec[]) insertMarker
0286: .getAuxiliary()).length);
0287: } else {
0288: assertFalse(insertMarker.isOccurred());
0289: }
0290: }
0291:
0292: public void testFlush_Insert_Offset() throws Exception {
0293: final String text = "tag";
0294: final String initialText = "text";
0295: reader = (HTMLReader) doc.getReader(initialText.length());
0296: doc.insertString(0, initialText, new SimpleAttributeSet());
0297: SimpleAttributeSet attr = new SimpleAttributeSet();
0298: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0299: .toString());
0300: reader.parseBuffer.add(new ElementSpec(attr,
0301: ElementSpec.StartTagType));
0302: reader.parseBuffer.add(new ElementSpec(
0303: new SimpleAttributeSet(), ElementSpec.ContentType, text
0304: .toCharArray(), 0, 3));
0305: reader.parseBuffer.add(new ElementSpec(attr,
0306: ElementSpec.EndTagType));
0307: assertEquals(3, reader.parseBuffer.size());
0308: reader.flush();
0309: assertEquals(0, reader.parseBuffer.size());
0310: assertFalse(createMarker.isOccurred());
0311: assertTrue(insertMarker.isOccurred());
0312: assertEquals(3,
0313: ((ElementSpec[]) insertMarker.getAuxiliary()).length);
0314: assertEquals(initialText + text, doc
0315: .getText(0, doc.getLength()));
0316: }
0317:
0318: public void testHandleComment() {
0319: String text1 = "data";
0320: String text2 = "datadata";
0321: reader.handleComment(text1.toCharArray(), 0);
0322: assertEquals(0, reader.charAttr.getAttributeCount());
0323: assertEquals(0, reader.parseBuffer.size());
0324: assertFalse(createMarker.isOccurred());
0325: assertFalse(insertMarker.isOccurred());
0326: Vector comments = (Vector) doc
0327: .getProperty(HTMLDocument.AdditionalComments);
0328: assertEquals(1, comments.size());
0329: assertEquals(text1, comments.get(0));
0330:
0331: reader.handleStartTag(Tag.P, new SimpleAttributeSet(), 0);
0332: assertEquals(0, reader.charAttr.getAttributeCount());
0333: assertEquals(1, reader.parseBuffer.size());
0334: reader.handleComment(text1.toCharArray(), 100);
0335: assertEquals(0, reader.charAttr.getAttributeCount());
0336: assertEquals(2, reader.parseBuffer.size());
0337: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
0338: assertSpec(spec, ElementSpec.ContentType,
0339: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
0340: checkAttributes(spec.getAttributes(),
0341: StyleConstants.NameAttribute, Tag.COMMENT);
0342: checkAttributes(spec.getAttributes(), HTML.Attribute.COMMENT,
0343: text1);
0344: assertFalse(createMarker.isOccurred());
0345: assertFalse(insertMarker.isOccurred());
0346: reader.handleEndTag(Tag.P, 0);
0347: assertEquals(1, comments.size());
0348:
0349: reader.handleComment(text2.toCharArray(), 0);
0350: assertEquals(2, comments.size());
0351: assertEquals(text2, comments.get(1));
0352: }
0353:
0354: public void testHandleEndOfLineString() throws Exception {
0355: String text1 = "text1";
0356: String text2 = "text2";
0357:
0358: reader.handleEndOfLineString(text1);
0359: assertEquals(text1, doc
0360: .getProperty(DefaultEditorKit.EndOfLineStringProperty));
0361:
0362: reader.handleEndOfLineString(text2);
0363: assertEquals(text2, doc
0364: .getProperty(DefaultEditorKit.EndOfLineStringProperty));
0365: }
0366:
0367: public void testHandleSimpleTag() {
0368: SimpleAttributeSet attr = new SimpleAttributeSet();
0369: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0370: .toString());
0371: final Marker endMarker = new Marker();
0372: final Marker startMarker = new Marker();
0373: Tag tag = new Tag("mytag");
0374: reader.registerTag(tag, reader.new TagAction() {
0375: public void end(final Tag tag) {
0376: endMarker.setAuxiliary(tag);
0377: endMarker.setOccurred();
0378: }
0379:
0380: public void start(final Tag tag,
0381: final MutableAttributeSet attr) {
0382: startMarker.setOccurred();
0383: startMarker.setAuxiliary(attr);
0384: }
0385: });
0386: reader.handleSimpleTag(tag, attr, 1);
0387: assertEquals(0, reader.parseBuffer.size());
0388: assertFalse(createMarker.isOccurred());
0389: assertFalse(insertMarker.isOccurred());
0390: assertEquals(0, reader.charAttr.getAttributeCount());
0391: assertTrue(endMarker.isOccurred());
0392: assertEquals(tag, endMarker.getAuxiliary());
0393: assertTrue(startMarker.isOccurred());
0394: assertSame(attr, startMarker.getAuxiliary());
0395: }
0396:
0397: public void testHandleEndTag() {
0398: SimpleAttributeSet attr = new SimpleAttributeSet();
0399: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0400: .toString());
0401: final Marker endMarker = new Marker();
0402: final Marker startMarker = new Marker();
0403: Tag tag = new Tag("mytag");
0404: reader.registerTag(tag, reader.new TagAction() {
0405: public void end(final Tag tag) {
0406: endMarker.setAuxiliary(tag);
0407: endMarker.setOccurred();
0408: }
0409:
0410: public void start(final Tag tag,
0411: final MutableAttributeSet attr) {
0412: startMarker.setOccurred();
0413: startMarker.setAuxiliary(attr);
0414: }
0415: });
0416: reader.handleEndTag(tag, 0);
0417: assertEquals(0, reader.parseBuffer.size());
0418: assertFalse(createMarker.isOccurred());
0419: assertEquals(0, reader.charAttr.getAttributeCount());
0420: assertTrue(endMarker.isOccurred());
0421: assertEquals(tag, endMarker.getAuxiliary());
0422: assertFalse(startMarker.isOccurred());
0423: }
0424:
0425: public void testHandleStartTag() {
0426: SimpleAttributeSet attr = new SimpleAttributeSet();
0427: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
0428: .toString());
0429: final Marker endMarker = new Marker();
0430: final Marker startMarker = new Marker();
0431: Tag tag = new Tag("mytag");
0432: reader.registerTag(tag, reader.new TagAction() {
0433: public void end(final Tag tag) {
0434: endMarker.setAuxiliary(tag);
0435: endMarker.setOccurred();
0436: }
0437:
0438: public void start(final Tag tag,
0439: final MutableAttributeSet attr) {
0440: startMarker.setOccurred();
0441: startMarker.setAuxiliary(attr);
0442: }
0443: });
0444: reader.handleStartTag(tag, attr, 0);
0445: assertEquals(0, reader.parseBuffer.size());
0446: assertFalse(createMarker.isOccurred());
0447: assertEquals(0, reader.charAttr.getAttributeCount());
0448: assertTrue(startMarker.isOccurred());
0449: assertSame(attr, startMarker.getAuxiliary());
0450: assertFalse(endMarker.isOccurred());
0451: }
0452:
0453: public void testHandleStartTag_StyleAttr() {
0454: SimpleAttributeSet attr = new SimpleAttributeSet();
0455: attr.addAttribute(HTML.Attribute.STYLE, "color: red");
0456: reader.handleStartTag(Tag.P, attr, 0);
0457: assertEquals(0, reader.charAttr.getAttributeCount());
0458: assertEquals(1, reader.parseBuffer.size());
0459: final ElementSpec spec = (ElementSpec) reader.parseBuffer
0460: .get(0);
0461: final AttributeSet specAttr = spec.getAttributes();
0462: assertNotSame(attr, specAttr);
0463: assertNull(specAttr.getAttribute(HTML.Attribute.STYLE));
0464: checkAttributes(specAttr, CSS.Attribute.COLOR, "red");
0465: }
0466:
0467: public void testHandleSimpleTag_StyleAttr() {
0468: SimpleAttributeSet attr = new SimpleAttributeSet();
0469: attr.addAttribute(HTML.Attribute.STYLE, "color: red");
0470: reader.handleSimpleTag(Tag.HR, attr, 0);
0471: assertEquals(0, reader.charAttr.getAttributeCount());
0472: assertEquals(2, reader.parseBuffer.size());
0473: final ElementSpec spec = (ElementSpec) reader.parseBuffer
0474: .get(1);
0475: final AttributeSet specAttr = spec.getAttributes();
0476: assertNotSame(attr, specAttr);
0477: assertNull(specAttr.getAttribute(HTML.Attribute.STYLE));
0478: checkAttributes(specAttr, CSS.Attribute.COLOR, "red");
0479: }
0480:
0481: public void testHandleText() throws Exception {
0482: String text = "data";
0483: reader.handleText(text.toCharArray(), 0);
0484: assertEquals(0, reader.charAttr.getAttributeCount());
0485: assertEquals(0, reader.parseBuffer.size());
0486: assertFalse(createMarker.isOccurred());
0487: assertFalse(insertMarker.isOccurred());
0488:
0489: reader.handleStartTag(Tag.P, new SimpleAttributeSet(), 0);
0490: assertEquals(0, reader.charAttr.getAttributeCount());
0491: assertEquals(1, reader.parseBuffer.size());
0492: reader.handleText(text.toCharArray(), 0);
0493: assertEquals(1, reader.charAttr.getAttributeCount());
0494: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
0495: Tag.CONTENT);
0496: assertEquals(2, reader.parseBuffer.size());
0497: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
0498: assertSpec(spec, ElementSpec.ContentType,
0499: ElementSpec.OriginateDirection, 0, text.toCharArray());
0500: final AttributeSet specAttr = spec.getAttributes();
0501: assertEquals(1, specAttr.getAttributeCount());
0502: checkAttributes(specAttr, StyleConstants.NameAttribute,
0503: Tag.CONTENT);
0504: assertFalse(createMarker.isOccurred());
0505: assertFalse(insertMarker.isOccurred());
0506: }
0507:
0508: public void testHandleText_ContentMethodsCalls_Body()
0509: throws Exception {
0510: final Marker addContentMarker = new Marker();
0511: final Marker preContentMarker = new Marker();
0512: final Marker textAreaMarker = new Marker();
0513: createContentMarkersInstrumentedReader(addContentMarker,
0514: preContentMarker, textAreaMarker);
0515: String text = "data";
0516:
0517: reader.handleText(text.toCharArray(), 0);
0518: assertFalse(addContentMarker.isOccurred());
0519: assertFalse(preContentMarker.isOccurred());
0520: assertFalse(textAreaMarker.isOccurred());
0521:
0522: reader.handleStartTag(Tag.BODY, new SimpleAttributeSet(), 0);
0523: reader.handleText(text.toCharArray(), 0);
0524: assertTrue(addContentMarker.isOccurred());
0525: assertFalse(preContentMarker.isOccurred());
0526: assertFalse(textAreaMarker.isOccurred());
0527: addContentMarker.reset();
0528:
0529: reader.handleEndTag(Tag.BODY, 0);
0530: reader.handleText(text.toCharArray(), 0);
0531: assertTrue(addContentMarker.isOccurred());
0532: assertFalse(preContentMarker.isOccurred());
0533: assertFalse(textAreaMarker.isOccurred());
0534: addContentMarker.reset();
0535: }
0536:
0537: public void testHandleText_ContentMethodsCalls_P() throws Exception {
0538: final Marker addContentMarker = new Marker();
0539: final Marker preContentMarker = new Marker();
0540: final Marker textAreaMarker = new Marker();
0541: createContentMarkersInstrumentedReader(addContentMarker,
0542: preContentMarker, textAreaMarker);
0543: String text = "data";
0544:
0545: reader.handleText(text.toCharArray(), 0);
0546: assertFalse(textAreaMarker.isOccurred());
0547: assertFalse(addContentMarker.isOccurred());
0548: assertFalse(preContentMarker.isOccurred());
0549:
0550: reader.handleStartTag(Tag.P, new SimpleAttributeSet(), 0);
0551: reader.handleText(text.toCharArray(), 0);
0552: assertTrue(addContentMarker.isOccurred());
0553: assertFalse(preContentMarker.isOccurred());
0554: assertFalse(textAreaMarker.isOccurred());
0555: addContentMarker.reset();
0556:
0557: reader.handleEndTag(Tag.P, 0);
0558: reader.handleText(text.toCharArray(), 0);
0559: assertTrue(addContentMarker.isOccurred());
0560: assertFalse(preContentMarker.isOccurred());
0561: assertFalse(textAreaMarker.isOccurred());
0562: addContentMarker.reset();
0563: }
0564:
0565: public void testHandleText_ContentMethodsCalls_Implied()
0566: throws Exception {
0567: final Marker addContentMarker = new Marker();
0568: final Marker preContentMarker = new Marker();
0569: final Marker textAreaMarker = new Marker();
0570: createContentMarkersInstrumentedReader(addContentMarker,
0571: preContentMarker, textAreaMarker);
0572: String text = "data";
0573:
0574: reader.handleText(text.toCharArray(), 0);
0575: assertFalse(textAreaMarker.isOccurred());
0576: assertFalse(addContentMarker.isOccurred());
0577: assertFalse(preContentMarker.isOccurred());
0578:
0579: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0580: reader.handleText(text.toCharArray(), 0);
0581: assertTrue(addContentMarker.isOccurred());
0582: assertFalse(preContentMarker.isOccurred());
0583: assertFalse(textAreaMarker.isOccurred());
0584: addContentMarker.reset();
0585:
0586: reader.blockClose(Tag.IMPLIED);
0587: reader.blockClose(Tag.IMPLIED);
0588: reader.handleText(text.toCharArray(), 0);
0589: assertTrue(addContentMarker.isOccurred());
0590: assertFalse(preContentMarker.isOccurred());
0591: assertFalse(textAreaMarker.isOccurred());
0592: addContentMarker.reset();
0593: }
0594:
0595: public void testHandleText_ContentMethodsCalls_Pre()
0596: throws Exception {
0597: final Marker addContentMarker = new Marker();
0598: final Marker preContentMarker = new Marker();
0599: final Marker textAreaMarker = new Marker();
0600: createContentMarkersInstrumentedReader(addContentMarker,
0601: preContentMarker, textAreaMarker);
0602: String text = "data";
0603:
0604: reader.handleText(text.toCharArray(), 0);
0605: assertFalse(textAreaMarker.isOccurred());
0606: assertFalse(addContentMarker.isOccurred());
0607: assertFalse(preContentMarker.isOccurred());
0608:
0609: reader.handleStartTag(Tag.PRE, new SimpleAttributeSet(), 0);
0610: reader.handleText(text.toCharArray(), 0);
0611: assertFalse(textAreaMarker.isOccurred());
0612: assertTrue(addContentMarker.isOccurred());
0613: assertTrue(preContentMarker.isOccurred());
0614: addContentMarker.reset();
0615: preContentMarker.reset();
0616:
0617: reader.handleEndTag(Tag.PRE, 0);
0618: reader.handleText(text.toCharArray(), 0);
0619: if (!isHarmony()) {
0620: assertTrue(addContentMarker.isOccurred());
0621: }
0622: assertFalse(preContentMarker.isOccurred());
0623: }
0624:
0625: public void testHandleText_ContentMethodsCalls_Title()
0626: throws Exception {
0627: final Marker addContentMarker = new Marker();
0628: final Marker preContentMarker = new Marker();
0629: final Marker textAreaMarker = new Marker();
0630: createContentMarkersInstrumentedReader(addContentMarker,
0631: preContentMarker, textAreaMarker);
0632: String text = "data";
0633:
0634: reader.handleText(text.toCharArray(), 0);
0635: assertFalse(textAreaMarker.isOccurred());
0636: assertFalse(addContentMarker.isOccurred());
0637: assertFalse(preContentMarker.isOccurred());
0638:
0639: reader.handleStartTag(Tag.TITLE, new SimpleAttributeSet(), 0);
0640: reader.handleText(text.toCharArray(), 0);
0641: assertFalse(addContentMarker.isOccurred());
0642: assertFalse(preContentMarker.isOccurred());
0643: assertFalse(textAreaMarker.isOccurred());
0644:
0645: reader.handleEndTag(Tag.TITLE, 0);
0646: reader.handleText(text.toCharArray(), 0);
0647: assertTrue(addContentMarker.isOccurred());
0648: assertFalse(preContentMarker.isOccurred());
0649: assertFalse(textAreaMarker.isOccurred());
0650: }
0651:
0652: public void testHandleText_ContentMethodsCalls_Option()
0653: throws Exception {
0654: final Marker addContentMarker = new Marker();
0655: final Marker preContentMarker = new Marker();
0656: final Marker textAreaMarker = new Marker();
0657: createContentMarkersInstrumentedReader(addContentMarker,
0658: preContentMarker, textAreaMarker);
0659: String text = "data";
0660:
0661: reader.handleText(text.toCharArray(), 0);
0662: assertFalse(textAreaMarker.isOccurred());
0663: assertFalse(addContentMarker.isOccurred());
0664: assertFalse(preContentMarker.isOccurred());
0665:
0666: reader.handleStartTag(Tag.OPTION, new SimpleAttributeSet(), 0);
0667: reader.handleText(text.toCharArray(), 0);
0668: assertFalse(textAreaMarker.isOccurred());
0669: assertFalse(addContentMarker.isOccurred());
0670: assertFalse(preContentMarker.isOccurred());
0671:
0672: reader.handleEndTag(Tag.OPTION, 0);
0673: reader.handleText(text.toCharArray(), 0);
0674: assertFalse(textAreaMarker.isOccurred());
0675: assertFalse(addContentMarker.isOccurred());
0676: assertFalse(preContentMarker.isOccurred());
0677:
0678: reader.handleStartTag(Tag.P, new SimpleAttributeSet(), 0);
0679: reader.handleText(text.toCharArray(), 0);
0680: assertFalse(textAreaMarker.isOccurred());
0681: assertTrue(addContentMarker.isOccurred());
0682: assertFalse(preContentMarker.isOccurred());
0683: addContentMarker.reset();
0684:
0685: reader.handleStartTag(Tag.OPTION, new SimpleAttributeSet(), 0);
0686: reader.handleText(text.toCharArray(), 0);
0687: assertFalse(textAreaMarker.isOccurred());
0688: if (!isHarmony()) {
0689: assertFalse(addContentMarker.isOccurred());
0690: } else {
0691: assertTrue(addContentMarker.isOccurred());
0692: }
0693: assertFalse(preContentMarker.isOccurred());
0694:
0695: reader.handleEndTag(Tag.OPTION, 0);
0696: reader.handleText(text.toCharArray(), 0);
0697: assertFalse(textAreaMarker.isOccurred());
0698: assertTrue(addContentMarker.isOccurred());
0699: assertFalse(preContentMarker.isOccurred());
0700: addContentMarker.reset();
0701: }
0702:
0703: public void testHandleText_ContentMethodsCalls_TextArea()
0704: throws Exception {
0705: final Marker addContentMarker = new Marker();
0706: final Marker preContentMarker = new Marker();
0707: final Marker textAreaMarker = new Marker();
0708: createContentMarkersInstrumentedReader(addContentMarker,
0709: preContentMarker, textAreaMarker);
0710: String text = "data";
0711:
0712: reader.handleText(text.toCharArray(), 0);
0713: assertFalse(textAreaMarker.isOccurred());
0714: assertFalse(addContentMarker.isOccurred());
0715: assertFalse(preContentMarker.isOccurred());
0716:
0717: reader
0718: .handleStartTag(Tag.TEXTAREA, new SimpleAttributeSet(),
0719: 0);
0720: reader.handleText(text.toCharArray(), 0);
0721: assertTrue(textAreaMarker.isOccurred());
0722: assertFalse(addContentMarker.isOccurred());
0723: assertFalse(preContentMarker.isOccurred());
0724: textAreaMarker.reset();
0725:
0726: reader.handleEndTag(Tag.TEXTAREA, 0);
0727: reader.handleText(text.toCharArray(), 0);
0728: assertFalse(textAreaMarker.isOccurred());
0729: assertTrue(addContentMarker.isOccurred());
0730: assertFalse(preContentMarker.isOccurred());
0731: addContentMarker.reset();
0732:
0733: reader
0734: .handleStartTag(Tag.TEXTAREA, new SimpleAttributeSet(),
0735: 0);
0736: reader.handleStartTag(Tag.SELECT, new SimpleAttributeSet(), 0);
0737: reader.handleStartTag(Tag.OPTION, new SimpleAttributeSet(), 0);
0738: reader.handleText(text.toCharArray(), 0);
0739: assertTrue(textAreaMarker.isOccurred());
0740: assertFalse(addContentMarker.isOccurred());
0741: assertFalse(preContentMarker.isOccurred());
0742: textAreaMarker.reset();
0743:
0744: reader.handleStartTag(Tag.PRE, new SimpleAttributeSet(), 0);
0745: reader.handleText(text.toCharArray(), 0);
0746: assertTrue(textAreaMarker.isOccurred());
0747: // this is caused by blockOpen(IMPLIED) call
0748: assertTrue(addContentMarker.isOccurred());
0749: assertFalse(preContentMarker.isOccurred());
0750: textAreaMarker.reset();
0751: addContentMarker.reset();
0752:
0753: reader.handleEndTag(Tag.TEXTAREA, 0);
0754: reader.handleText(text.toCharArray(), 0);
0755: assertFalse(textAreaMarker.isOccurred());
0756: assertTrue(addContentMarker.isOccurred());
0757: assertTrue(preContentMarker.isOccurred());
0758: preContentMarker.reset();
0759: addContentMarker.reset();
0760:
0761: reader.handleEndTag(Tag.PRE, 0);
0762: reader.handleText(text.toCharArray(), 0);
0763: assertFalse(textAreaMarker.isOccurred());
0764: assertTrue(addContentMarker.isOccurred());
0765: assertFalse(preContentMarker.isOccurred());
0766: addContentMarker.reset();
0767: }
0768:
0769: public void testHandleText_Implied() throws Exception {
0770: String text = "data";
0771: final SimpleAttributeSet attr = new SimpleAttributeSet();
0772: reader.handleStartTag(Tag.BODY, attr, 0);
0773: final Tag tag = Tag.SUB;
0774: reader.handleStartTag(tag, attr, 0);
0775: assertEquals(2, reader.charAttr.getAttributeCount());
0776: assertEquals(1, reader.parseBuffer.size());
0777: assertEquals(attr, reader.charAttr.getAttribute(tag));
0778:
0779: reader.handleText(text.toCharArray(), 0);
0780: assertEquals(3, reader.charAttr.getAttributeCount());
0781: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
0782: Tag.CONTENT);
0783: assertEquals(3, reader.parseBuffer.size());
0784: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(2);
0785: assertSpec(spec, ElementSpec.ContentType,
0786: ElementSpec.OriginateDirection, 0, text.toCharArray());
0787: AttributeSet specAttr = spec.getAttributes();
0788: assertEquals(3, specAttr.getAttributeCount());
0789: checkAttributes(specAttr, StyleConstants.NameAttribute,
0790: Tag.CONTENT);
0791: checkAttributes(specAttr, tag, attr);
0792:
0793: spec = (ElementSpec) reader.parseBuffer.get(1);
0794: checkOpenImpliedSpec(spec);
0795:
0796: assertFalse(createMarker.isOccurred());
0797: assertFalse(insertMarker.isOccurred());
0798: }
0799:
0800: public void testHandleText_BlockOpenClose_P1() {
0801: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0802: reader.blockClose(Tag.H1);
0803: checkTextInNotInserted();
0804: }
0805:
0806: public void testHandleText_BlockOpenClose_P2() {
0807: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0808: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0809: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0810: reader.blockClose(Tag.H1);
0811: checkTextIsInserted();
0812: }
0813:
0814: public void testHandleText_BlockOpenClose_P3() {
0815: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0816: reader.blockOpen(Tag.H2, new SimpleAttributeSet());
0817: reader.blockOpen(Tag.H3, new SimpleAttributeSet());
0818: reader.blockClose(Tag.H2);
0819: reader.blockClose(Tag.H3);
0820: reader.blockClose(Tag.P);
0821: checkTextInNotInserted();
0822: }
0823:
0824: public void testHandleText_BlockOpenClose_Implied1() {
0825: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0826: reader.blockClose(Tag.IMPLIED);
0827: checkTextIsInserted();
0828: }
0829:
0830: public void testHandleText_BlockOpenClose_Implied2() {
0831: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0832: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0833: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0834: reader.blockClose(Tag.IMPLIED);
0835: reader.blockClose(Tag.IMPLIED);
0836: reader.blockClose(Tag.IMPLIED);
0837: reader.blockClose(Tag.IMPLIED);
0838: checkTextInNotInserted();
0839: }
0840:
0841: public void testHandleText_BlockOpenClose_Implied3() {
0842: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0843: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0844: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0845: reader.blockClose(Tag.IMPLIED);
0846: reader.blockClose(Tag.IMPLIED);
0847: reader.blockClose(Tag.IMPLIED);
0848: checkTextIsInserted();
0849: }
0850:
0851: public void testHandleText_BlockOpenClose_P_Implied1() {
0852: reader.blockOpen(Tag.P, new SimpleAttributeSet());
0853: reader.blockOpen(Tag.P, new SimpleAttributeSet());
0854: reader.blockOpen(Tag.P, new SimpleAttributeSet());
0855: reader.blockClose(Tag.IMPLIED);
0856: reader.blockClose(Tag.IMPLIED);
0857: reader.blockClose(Tag.IMPLIED);
0858: checkTextIsInserted();
0859: }
0860:
0861: public void testHandleText_BlockOpenClose_P_Implied2() {
0862: reader.blockOpen(Tag.P, new SimpleAttributeSet());
0863: reader.blockOpen(Tag.P, new SimpleAttributeSet());
0864: reader.blockOpen(Tag.P, new SimpleAttributeSet());
0865: reader.blockClose(Tag.IMPLIED);
0866: reader.blockClose(Tag.IMPLIED);
0867: reader.blockClose(Tag.IMPLIED);
0868: reader.blockClose(Tag.IMPLIED);
0869: checkTextInNotInserted();
0870: }
0871:
0872: public void testHandleText_BlockOpenClose_P_Implied3() {
0873: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0874: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0875: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0876: reader.blockClose(Tag.P);
0877: reader.blockClose(Tag.P);
0878: reader.blockClose(Tag.P);
0879: checkTextInNotInserted();
0880: }
0881:
0882: public void testHandleText_BlockOpenClose_P_Implied4() {
0883: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0884: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0885: reader.blockOpen(Tag.IMPLIED, new SimpleAttributeSet());
0886: reader.blockClose(Tag.P);
0887: reader.blockClose(Tag.P);
0888: checkTextIsInserted();
0889: }
0890:
0891: public void testAddContent_BlockOpenClose_P1() {
0892: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0893: reader.blockClose(Tag.H1);
0894: int numSpecs = reader.parseBuffer.size();
0895: reader.addContent("text".toCharArray(), 0, 4, true);
0896: assertEquals(numSpecs + 2, reader.parseBuffer.size());
0897: }
0898:
0899: public void testAddContent_BlockOpenClose_P2() {
0900: reader.blockOpen(Tag.H1, new SimpleAttributeSet());
0901: reader.blockClose(Tag.H1);
0902: int numSpecs = reader.parseBuffer.size();
0903: reader.addContent("text".toCharArray(), 0, 4, false);
0904: assertEquals(numSpecs + 1, reader.parseBuffer.size());
0905: }
0906:
0907: public void testAddContent_BlockOpenClose_P_Implied5() {
0908: TagAction action = reader.new ParagraphAction();
0909: reader.handleStartTag(Tag.BODY, new SimpleAttributeSet(), 0);
0910: assertEquals(1, reader.parseBuffer.size());
0911: reader.handleText("0000".toCharArray(), 0);
0912: assertEquals(3, reader.parseBuffer.size());
0913: action.start(Tag.P, new SimpleAttributeSet());
0914: assertEquals(6, reader.parseBuffer.size());
0915: reader.handleText("1111".toCharArray(), 0);
0916: assertEquals(7, reader.parseBuffer.size());
0917: action.end(Tag.P);
0918: assertEquals(9, reader.parseBuffer.size());
0919: }
0920:
0921: public void testHTMLReaderIntIntIntTag() throws Exception {
0922: reader = (HTMLReader) doc.getReader(10, 10, 20, Tag.B);
0923: assertNotNull(reader.parseBuffer);
0924: assertEquals(0, reader.parseBuffer.size());
0925:
0926: assertNotNull(reader.charAttr);
0927: assertEquals(0, reader.charAttr.getAttributeCount());
0928: }
0929:
0930: public void testHTMLReaderIntIntIntTag_TagParameter()
0931: throws Exception {
0932: checkConstructorTagParameter(Tag.BR,
0933: "<a>link</a><b>asdasd</b>", 0);
0934: checkConstructorTagParameter(Tag.BR,
0935: "<a>link</a><b>asdasd</b><br>", 1);
0936: checkConstructorTagParameter(Tag.BR,
0937: "<a>link</a><b><br>asdasd</b>", 2);
0938: checkConstructorTagParameter(Tag.BR,
0939: "<a>link</a><br><b>asdasd</b>", 2);
0940: checkConstructorTagParameter(Tag.BR,
0941: "<a>link<br></a><b>asdasd</b>", 2);
0942: checkConstructorTagParameter(Tag.BR,
0943: "<a><br>link</a><b>asdasd</b>", 3);
0944: checkConstructorTagParameter(Tag.BR,
0945: "<br><a>link</a><b>asdasd</b>", 3);
0946: checkConstructorTagParameter(null,
0947: "<br><a>link</a><b>asdasd</b>", 15);
0948: }
0949:
0950: public void testHTMLReaderIntIntIntTag_TagParameter_Closed()
0951: throws Exception {
0952: final String text = "tag";
0953: editable = false;
0954: reader = (HTMLReader) doc.getReader(0, 0, 0, Tag.FORM);
0955: SimpleAttributeSet attr = new SimpleAttributeSet();
0956: attr.addAttribute(StyleConstants.NameAttribute, Tag.FORM
0957: .toString());
0958: reader.handleStartTag(Tag.HTML, attr, 0);
0959: reader.handleStartTag(Tag.HEAD, attr, 0);
0960: reader.handleEndTag(Tag.HEAD, 0);
0961: reader.handleStartTag(Tag.BODY, attr, 0);
0962: reader.handleEndTag(Tag.FORM, 0);
0963: reader.handleText(text.toCharArray(), 0);
0964: reader.handleEndTag(Tag.BODY, 0);
0965: reader.handleEndTag(Tag.HTML, 0);
0966: reader.flush();
0967: assertEquals(0, reader.parseBuffer.size());
0968: assertFalse(insertMarker.isOccurred());
0969: assertFalse(createMarker.isOccurred());
0970: }
0971:
0972: public void testHTMLReaderIntIntIntTag_OffsetParameter()
0973: throws Exception {
0974: final String str = "<a>0000</a><b>1111</b>";
0975: checkConstructorOffsetParameter(Tag.A, str, 0);
0976: checkConstructorOffsetParameter(Tag.A, str, 1);
0977: checkConstructorOffsetParameter(Tag.A, str, 2);
0978: checkConstructorOffsetParameter(Tag.A, str, 3);
0979: checkConstructorOffsetParameter(Tag.A, str, 4);
0980: checkConstructorOffsetParameter(Tag.A, str, 5);
0981: }
0982:
0983: public void testHTMLReaderIntIntIntTag_PopParameter()
0984: throws Exception {
0985: final String str = "<a>link</a><b>asdasd</b>";
0986: checkConstructorPopParameter(Tag.A, str, 0, "coco");
0987: if (isHarmony()) {
0988: checkConstructorPopParameter(Tag.A, str, 1, "cpeocococo");
0989: }
0990: checkConstructorPopParameter(Tag.A, str, 2, "cpeoeocococo");
0991: checkConstructorPopParameter(Tag.A, str, 3, "cpeoeoeocococo");
0992: checkConstructorPopParameter(Tag.A, str, 4, "cpeoeoeoeocococo");
0993: checkConstructorPopParameter(Tag.A, str, 5,
0994: "cpeoeoeoeoeocococo");
0995: checkConstructorPopParameter(Tag.A, str, 6,
0996: "cpeoeoeoeoeoeocococo");
0997: }
0998:
0999: public void testHTMLReaderIntIntIntTag_PushParameter()
1000: throws Exception {
1001: final String str = "<a>link</a><b>asdasd</b>";
1002: checkConstructorPushParameter(Tag.A, str, 0, "coco");
1003: checkConstructorPushParameter(Tag.A, str, 1, "cpsncoco");
1004: checkConstructorPushParameter(Tag.A, str, 2, "cpsnsncoco");
1005: checkConstructorPushParameter(Tag.A, str, 3, "cpsnsnsncoco");
1006: checkConstructorPushParameter(Tag.A, str, 4, "cpsnsnsnsncoco");
1007: checkConstructorPushParameter(Tag.A, str, 5, "cpsnsnsnsnsncoco");
1008: checkConstructorPushParameter(Tag.A, str, 6,
1009: "cpsnsnsnsnsnsncoco");
1010: }
1011:
1012: public void testHTMLReaderIntIntIntTag_PushPopParameter()
1013: throws Exception {
1014: final String str = "<a>link</a><b>asdasd</b>";
1015: checkConstructorPopPushParameter(Tag.A, str, 0, 0, "coco");
1016: checkConstructorPopPushParameter(Tag.A, str, 1, 1, "cpeosncoco");
1017: checkConstructorPopPushParameter(Tag.A, str, 2, 1,
1018: "cpeoeosncoco");
1019: checkConstructorPopPushParameter(Tag.A, str, 1, 2,
1020: "cpeosnsncoco");
1021: checkConstructorPopPushParameter(Tag.A, str, 2, 2,
1022: "cpeoeosnsncoco");
1023: checkConstructorPopPushParameter(Tag.A, str, 3, 1,
1024: "cpeoeoeosncoco");
1025: checkConstructorPopPushParameter(Tag.A, str, 3, 2,
1026: "cpeoeoeosnsncoco");
1027: checkConstructorPopPushParameter(Tag.A, str, 1, 3,
1028: "cpeosnsnsncoco");
1029: checkConstructorPopPushParameter(Tag.A, str, 2, 3,
1030: "cpeoeosnsnsncoco");
1031: checkConstructorPopPushParameter(Tag.A, str, 3, 3,
1032: "cpeoeoeosnsnsncoco");
1033: }
1034:
1035: public void testHTMLReaderIntIntIntTag_PushPopParameter_JoinPrevSpec1()
1036: throws Exception {
1037: editable = false;
1038: HTMLReader reader = (HTMLReader) doc.getReader(0, 0, 1, Tag.A);
1039: reader.new CharacterAction().start(Tag.A,
1040: new SimpleAttributeSet());
1041: assertEquals(2, reader.parseBuffer.size());
1042: checkJoinPrevSpec((ElementSpec) reader.parseBuffer.get(0));
1043: }
1044:
1045: public void testHTMLReaderIntIntIntTag_PushPopParameter_JoinPrevSpec2()
1046: throws Exception {
1047: editable = false;
1048: HTMLReader reader = (HTMLReader) doc.getReader(1, 1, 0, Tag.A);
1049: reader.new CharacterAction().start(Tag.A,
1050: new SimpleAttributeSet());
1051: assertEquals(1, reader.parseBuffer.size());
1052: assertTrue(ElementSpec.JoinPreviousDirection != ((ElementSpec) reader.parseBuffer
1053: .get(0)).getDirection());
1054: }
1055:
1056: public void testHTMLReaderIntIntIntTag_PushPopParameter_JoinPrevSpec3()
1057: throws Exception {
1058: editable = false;
1059: HTMLReader reader = (HTMLReader) doc.getReader(0, 1, 0, Tag.A);
1060: reader.new CharacterAction().start(Tag.A,
1061: new SimpleAttributeSet());
1062: assertEquals(2, reader.parseBuffer.size());
1063: checkJoinPrevSpec((ElementSpec) reader.parseBuffer.get(0));
1064: }
1065:
1066: public void testHTMLReaderIntIntIntTag_PushPopParameter_JoinPrevSpec4()
1067: throws Exception {
1068: editable = false;
1069: HTMLReader reader = (HTMLReader) doc.getReader(0, 1, 0, Tag.P);
1070: reader.new ParagraphAction().start(Tag.P,
1071: new SimpleAttributeSet());
1072: assertEquals(3, reader.parseBuffer.size());
1073: checkJoinPrevSpec((ElementSpec) reader.parseBuffer.get(0));
1074: }
1075:
1076: public void testHTMLReaderIntIntIntTag_PushPopParameter_JoinPrevSpec5()
1077: throws Exception {
1078: final Tag[] tags = HTML.getAllTags();
1079: Tag[] oddTags;
1080: if (!isHarmony()) {
1081: oddTags = new Tag[] { Tag.AREA, Tag.BASE, Tag.MAP,
1082: Tag.OPTION, Tag.PARAM, Tag.STYLE };
1083: } else {
1084: oddTags = new Tag[] { Tag.AREA, Tag.APPLET, Tag.IFRAME,
1085: Tag.LEGEND, Tag.COL, Tag.COLGROUP, Tag.SCRIPT,
1086: Tag.BASE, Tag.MAP, Tag.OPTION, Tag.OPTGROUP,
1087: Tag.PARAM, Tag.STYLE };
1088: }
1089:
1090: for (int i = 0; i < tags.length; i++) {
1091: final Tag tag = tags[i];
1092: if (foundInArray(oddTags, tag)) {
1093: continue;
1094: }
1095: init();
1096: editable = false;
1097: HTMLReader reader = (HTMLReader) doc
1098: .getReader(0, 1, 0, tag);
1099: reader.handleStartTag(tag, new SimpleAttributeSet(), 0);
1100: assertTrue(tag.toString(), reader.parseBuffer.size() > 0);
1101: ElementSpec firstSpec = (ElementSpec) reader.parseBuffer
1102: .get(0);
1103: assertEquals(tag.toString(), ElementSpec.ContentType,
1104: firstSpec.getType());
1105: assertEquals(tag.toString(),
1106: ElementSpec.JoinPreviousDirection, firstSpec
1107: .getDirection());
1108: assertEquals(tag.toString(), '\n', firstSpec.getArray()[0]);
1109: }
1110: }
1111:
1112: public void testHTMLReaderIntIntIntTag_PushPopParameter_BlockParagraph()
1113: throws Exception {
1114: // trailing specs cutting for block and paragraph tags
1115: if (!isHarmony()) {
1116: return;
1117: }
1118: final String str = "<P>link</P>";
1119: init();
1120: editable = false;
1121: ParserCallback reader = doc.getReader(0, 2, 3, Tag.P);
1122: SimpleAttributeSet attr = new SimpleAttributeSet();
1123: attr.addAttribute(StyleConstants.NameAttribute, Tag.S
1124: .toString());
1125: doc.insertString(0, "0000", attr);
1126: assertFalse(insertMarker.isOccurred());
1127: parse(str, reader);
1128: reader.flush();
1129: assertTrue(insertMarker.isOccurred());
1130: ElementSpec[] specs = (ElementSpec[]) insertMarker
1131: .getAuxiliary();
1132: String specsDescr = "cpeoeosnsnsnsoco";
1133: assertEquals(specsDescr.length() / 2, specs.length);
1134: insertMarker.reset();
1135: for (int i = 0; i < specs.length; i++) {
1136: checkSpecType(specsDescr, i, (ElementSpec) specs[i]);
1137: }
1138: }
1139:
1140: public void testHTMLReaderIntIntIntTag_OffsetPushPopParameter()
1141: throws Exception {
1142: final String str = "<a>link</a><b>asdasd</b>";
1143: checkConstructorOffsetPopPushParameter(Tag.A, str, 0, 0, 0,
1144: "coco");
1145: checkConstructorOffsetPopPushParameter(Tag.A, str, 20, 1, 1,
1146: "eosncoco");
1147: checkConstructorOffsetPopPushParameter(Tag.A, str, 0, 2, 1,
1148: "cpeoeosncoco");
1149: checkConstructorOffsetPopPushParameter(Tag.A, str, 0, 1, 2,
1150: "cpeosnsncoco");
1151: checkConstructorOffsetPopPushParameter(Tag.A, str, 200, 2, 2,
1152: "eoeosnsncoco");
1153: checkConstructorOffsetPopPushParameter(Tag.A, str, 0, 3, 1,
1154: "cpeoeoeosncoco");
1155: checkConstructorOffsetPopPushParameter(Tag.A, str, 20, 3, 2,
1156: "eoeoeosnsncoco");
1157: checkConstructorOffsetPopPushParameter(Tag.A, str, 10, 1, 3,
1158: "eosnsnsncoco");
1159: checkConstructorOffsetPopPushParameter(Tag.A, str, 0, 2, 3,
1160: "cpeoeosnsnsncoco");
1161: checkConstructorOffsetPopPushParameter(Tag.A, str, 20, 3, 3,
1162: "eoeoeosnsnsncoco");
1163: }
1164:
1165: public void testHTMLReaderInt() {
1166: assertNotNull(reader.parseBuffer);
1167: assertEquals(0, reader.parseBuffer.size());
1168:
1169: assertNotNull(reader.charAttr);
1170: assertEquals(0, reader.charAttr.getAttributeCount());
1171: }
1172:
1173: public void testPushPopCharacterStyle() {
1174: reader.charAttr.addAttribute("initial", "true");
1175: final MutableAttributeSet attr1 = reader.charAttr;
1176: final SimpleAttributeSet attr2 = new SimpleAttributeSet();
1177: attr2.addAttribute("bbbb", "aaaa");
1178: final SimpleAttributeSet attr3 = new SimpleAttributeSet();
1179: attr3.addAttribute("aaaa", "bbbb");
1180:
1181: reader.pushCharacterStyle();
1182: reader.charAttr = attr2;
1183: reader.pushCharacterStyle();
1184: reader.charAttr = attr3;
1185: reader.pushCharacterStyle();
1186: reader.charAttr = null;
1187: reader.popCharacterStyle();
1188: assertEquals(attr3, reader.charAttr);
1189: reader.popCharacterStyle();
1190: assertEquals(attr2, reader.charAttr);
1191: reader.popCharacterStyle();
1192: assertEquals(attr1, reader.charAttr);
1193: reader.popCharacterStyle();
1194: assertEquals(attr1, reader.charAttr);
1195: reader.charAttr = null;
1196: reader.popCharacterStyle();
1197: assertNull(reader.charAttr);
1198:
1199: reader.charAttr = null;
1200: try {
1201: reader.pushCharacterStyle();
1202: fail("no exception has been thrown");
1203: } catch (Exception e) {
1204: }
1205:
1206: reader.charAttr = attr2;
1207: reader.pushCharacterStyle();
1208: reader.charAttr = attr3;
1209: reader.pushCharacterStyle();
1210: reader.pushCharacterStyle();
1211: reader.charAttr = null;
1212: reader.popCharacterStyle();
1213: assertEquals(attr3, reader.charAttr);
1214: reader.popCharacterStyle();
1215: assertEquals(attr3, reader.charAttr);
1216: reader.popCharacterStyle();
1217: assertEquals(attr2, reader.charAttr);
1218: }
1219:
1220: public void testAddContentCharArrayIntIntBoolean_Implying_Necess() {
1221: String text = "data";
1222: assertEquals(0, reader.parseBuffer.size());
1223: reader.addContent(text.toCharArray(), 1, 3, true);
1224: assertEquals(1, reader.charAttr.getAttributeCount());
1225: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1226: Tag.CONTENT);
1227: assertEquals(2, reader.parseBuffer.size());
1228: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1229: checkOpenImpliedSpec(spec);
1230: spec = (ElementSpec) reader.parseBuffer.get(1);
1231: assertSpec(spec, ElementSpec.ContentType,
1232: ElementSpec.OriginateDirection, 1, 3, text
1233: .toCharArray());
1234: assertEquals(1, spec.getAttributes().getAttributeCount());
1235: checkAttributes(spec.getAttributes(),
1236: StyleConstants.NameAttribute, Tag.CONTENT);
1237: assertEquals(text, new String(spec.getArray()));
1238: assertFalse(createMarker.isOccurred());
1239: assertFalse(insertMarker.isOccurred());
1240: }
1241:
1242: public void testAddContentCharArrayIntIntBoolean_Implying_Unnecess() {
1243: String text = "data";
1244: reader.handleStartTag(Tag.H1, new SimpleAttributeSet(), 0);
1245: assertEquals(0, reader.charAttr.getAttributeCount());
1246: assertEquals(1, reader.parseBuffer.size());
1247: reader.addContent(text.toCharArray(), 1, 3, true);
1248: assertEquals(1, reader.charAttr.getAttributeCount());
1249: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1250: Tag.CONTENT);
1251: assertEquals(2, reader.parseBuffer.size());
1252: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1253: assertSpec(spec, ElementSpec.StartTagType,
1254: ElementSpec.OriginateDirection, 0, null);
1255: assertEquals(1, spec.getAttributes().getAttributeCount());
1256: checkAttributes(spec.getAttributes(),
1257: StyleConstants.NameAttribute, Tag.H1);
1258: spec = (ElementSpec) reader.parseBuffer.get(1);
1259: assertEquals(1, spec.getAttributes().getAttributeCount());
1260: checkAttributes(spec.getAttributes(),
1261: StyleConstants.NameAttribute, Tag.CONTENT);
1262: assertSpec(spec, ElementSpec.ContentType,
1263: ElementSpec.OriginateDirection, 1, 3, text
1264: .toCharArray());
1265: assertFalse(createMarker.isOccurred());
1266: assertFalse(insertMarker.isOccurred());
1267: }
1268:
1269: public void testAddContentCharArrayIntIntBoolean_NotImplying() {
1270: String text = "data";
1271: assertEquals(0, reader.parseBuffer.size());
1272: reader.addContent(text.toCharArray(), 1, 3, false);
1273: assertEquals(1, reader.charAttr.getAttributeCount());
1274: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1275: Tag.CONTENT);
1276: assertEquals(1, reader.parseBuffer.size());
1277: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1278: assertSpec(spec, ElementSpec.ContentType,
1279: ElementSpec.OriginateDirection, 1, 3, text
1280: .toCharArray());
1281: assertEquals(1, spec.getAttributes().getAttributeCount());
1282: checkAttributes(spec.getAttributes(),
1283: StyleConstants.NameAttribute, Tag.CONTENT);
1284: assertFalse(createMarker.isOccurred());
1285: assertFalse(insertMarker.isOccurred());
1286: }
1287:
1288: public void testAddContentCharArrayIntInt_FullLength() {
1289: assertEquals(0, reader.parseBuffer.size());
1290: String text = "da\na";
1291: reader.charAttr.addAttribute("aaaa", "bbbb");
1292: reader.addContent(text.toCharArray(), 0, 4);
1293: assertEquals(2, reader.charAttr.getAttributeCount());
1294: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1295: Tag.CONTENT);
1296: checkAttributes(reader.charAttr, "aaaa", "bbbb");
1297: assertEquals(2, reader.parseBuffer.size());
1298: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1299: assertSpec(spec, ElementSpec.StartTagType,
1300: ElementSpec.OriginateDirection, 0, null);
1301: spec = (ElementSpec) reader.parseBuffer.get(1);
1302: assertSpec(spec, ElementSpec.ContentType,
1303: ElementSpec.OriginateDirection, 0, text.toCharArray());
1304: assertFalse(createMarker.isOccurred());
1305: assertFalse(insertMarker.isOccurred());
1306: }
1307:
1308: public void testAddContentCharArrayIntInt_PartLength() {
1309: assertEquals(0, reader.parseBuffer.size());
1310: final SimpleAttributeSet initialSet = new SimpleAttributeSet();
1311: reader.charAttr = initialSet;
1312: String text = "data";
1313: reader.addContent(text.toCharArray(), 1, 3);
1314: assertEquals(1, reader.charAttr.getAttributeCount());
1315: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1316: Tag.CONTENT);
1317: assertEquals(2, reader.parseBuffer.size());
1318: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1319: assertSpec(spec, ElementSpec.StartTagType,
1320: ElementSpec.OriginateDirection, 0, null);
1321: spec = (ElementSpec) reader.parseBuffer.get(1);
1322: assertSpec(spec, ElementSpec.ContentType,
1323: ElementSpec.OriginateDirection, 1, 3, text
1324: .toCharArray());
1325: assertFalse(createMarker.isOccurred());
1326: assertFalse(insertMarker.isOccurred());
1327: }
1328:
1329: public void testAddContent_CharAttr() {
1330: String text = "data";
1331: assertEquals(0, reader.parseBuffer.size());
1332: final SimpleAttributeSet initialSet = new SimpleAttributeSet();
1333: initialSet.addAttribute("aaaa", "bbbb");
1334: reader.charAttr = initialSet;
1335: reader.addContent(text.toCharArray(), 1, 3);
1336: assertEquals(2, reader.parseBuffer.size());
1337: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
1338: final AttributeSet specAttr = spec.getAttributes();
1339: assertEquals(2, specAttr.getAttributeCount());
1340: checkAttributes(specAttr, StyleConstants.NameAttribute,
1341: Tag.CONTENT);
1342: checkAttributes(specAttr, "aaaa", "bbbb");
1343:
1344: assertEquals(2, reader.charAttr.getAttributeCount());
1345: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1346: Tag.CONTENT);
1347: checkAttributes(reader.charAttr, "aaaa", "bbbb");
1348: reader.charAttr = null;
1349: reader.popCharacterStyle();
1350: assertNull(reader.charAttr);
1351: }
1352:
1353: public void testAddSpecialElement() {
1354: SimpleAttributeSet attr = new SimpleAttributeSet();
1355: attr.addAttribute("aaaa", "bbbb");
1356: reader.handleStartTag(Tag.P, attr, 0);
1357: assertEquals(1, reader.parseBuffer.size());
1358: assertEquals(0, reader.charAttr.getAttributeCount());
1359: assertFalse(createMarker.isOccurred());
1360: assertFalse(insertMarker.isOccurred());
1361:
1362: reader.addSpecialElement(Tag.HTML, attr);
1363: assertEquals(2, reader.parseBuffer.size());
1364: assertFalse(createMarker.isOccurred());
1365: assertFalse(insertMarker.isOccurred());
1366: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
1367: assertSpec(spec, ElementSpec.ContentType,
1368: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
1369: AttributeSet specAttr = spec.getAttributes();
1370: assertEquals(2, specAttr.getAttributeCount());
1371: checkAttributes(specAttr, StyleConstants.NameAttribute,
1372: Tag.HTML);
1373: checkAttributes(specAttr, "aaaa", "bbbb");
1374:
1375: assertNotSame(specAttr, attr);
1376: assertEquals(2, attr.getAttributeCount());
1377: checkAttributes(attr, StyleConstants.NameAttribute, Tag.HTML);
1378: checkAttributes(attr, "aaaa", "bbbb");
1379: }
1380:
1381: public void testAddSpecialElement_AfterBlockOpen1() {
1382: checkAddSpecialAfterBlockOpen(Tag.HTML, Tag.IMPLIED);
1383: }
1384:
1385: public void testAddSpecialElement_AfterBlockOpen2() {
1386: checkAddSpecialAfterBlockOpen(Tag.HTML, Tag.I);
1387: }
1388:
1389: public void testAddSpecialElement_AfterBlockOpen3() {
1390: checkAddSpecialAfterBlockOpen(Tag.IMG, Tag.P);
1391: }
1392:
1393: public void testAddSpecialElement_AllTagsImpliedBlockOpenCheck() {
1394: final Tag[] allTags = HTML.getAllTags();
1395: for (int i = 0; i < allTags.length; i++) {
1396: final Tag tag = allTags[i];
1397: if (Tag.FRAME.equals(tag)) {
1398: continue;
1399: }
1400: init();
1401: SimpleAttributeSet attr = new SimpleAttributeSet();
1402: attr.addAttribute("aaaa", "bbbb");
1403: reader.addSpecialElement(tag, attr);
1404: assertEquals(2, reader.parseBuffer.size());
1405: reader.blockClose(Tag.TABLE);
1406: assertEquals(5, reader.parseBuffer.size());
1407: }
1408: }
1409:
1410: public void testAddSpecialElement_FrameImpliedBlockOpenCheck() {
1411: Tag specialTag = Tag.FRAME;
1412: SimpleAttributeSet attr = new SimpleAttributeSet();
1413: attr.addAttribute("aaaa", "bbbb");
1414: reader.addSpecialElement(specialTag, attr);
1415: assertEquals(0, reader.charAttr.getAttributeCount());
1416: assertEquals(1, reader.parseBuffer.size());
1417: assertFalse(createMarker.isOccurred());
1418: assertFalse(insertMarker.isOccurred());
1419:
1420: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1421: assertSpec(spec, ElementSpec.ContentType,
1422: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
1423: AttributeSet specAttr = spec.getAttributes();
1424: assertEquals(2, specAttr.getAttributeCount());
1425: checkAttributes(specAttr, StyleConstants.NameAttribute,
1426: specialTag);
1427: checkAttributes(specAttr, "aaaa", "bbbb");
1428: }
1429:
1430: public void testAddSpecialElement_FrameImpliedBlockCloseCheck() {
1431: Tag specialTag = Tag.FRAME;
1432: SimpleAttributeSet attr = new SimpleAttributeSet();
1433: attr.addAttribute("aaaa", "bbbb");
1434: reader.blockOpen(Tag.FRAMESET, attr);
1435: reader.addSpecialElement(specialTag, attr);
1436: assertEquals(0, reader.charAttr.getAttributeCount());
1437: assertEquals(2, reader.parseBuffer.size());
1438: assertFalse(createMarker.isOccurred());
1439: assertFalse(insertMarker.isOccurred());
1440:
1441: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
1442: assertSpec(spec, ElementSpec.ContentType,
1443: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
1444: AttributeSet specAttr = spec.getAttributes();
1445: assertEquals(2, specAttr.getAttributeCount());
1446: checkAttributes(specAttr, StyleConstants.NameAttribute,
1447: specialTag);
1448: checkAttributes(specAttr, "aaaa", "bbbb");
1449:
1450: reader.blockClose(Tag.FRAMESET);
1451: assertEquals(3, reader.parseBuffer.size());
1452: spec = (ElementSpec) reader.parseBuffer.get(2);
1453: checkEndTagSpec(spec);
1454: specAttr = spec.getAttributes();
1455: }
1456:
1457: private void checkAddSpecialAfterBlockOpen(final Tag specialTag,
1458: final Tag blockTag) {
1459: SimpleAttributeSet attr = new SimpleAttributeSet();
1460: attr.addAttribute("aaaa", "bbbb");
1461: reader.blockOpen(blockTag, attr);
1462: assertEquals(1, reader.parseBuffer.size());
1463:
1464: reader.addSpecialElement(specialTag, attr);
1465: assertEquals(0, reader.charAttr.getAttributeCount());
1466: assertEquals(3, reader.parseBuffer.size());
1467: assertFalse(createMarker.isOccurred());
1468: assertFalse(insertMarker.isOccurred());
1469:
1470: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
1471: checkOpenImpliedSpec(spec);
1472:
1473: spec = (ElementSpec) reader.parseBuffer.get(2);
1474: assertSpec(spec, ElementSpec.ContentType,
1475: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
1476: AttributeSet specAttr = spec.getAttributes();
1477: assertEquals(2, specAttr.getAttributeCount());
1478: checkAttributes(specAttr, StyleConstants.NameAttribute,
1479: specialTag);
1480: checkAttributes(specAttr, "aaaa", "bbbb");
1481: }
1482:
1483: public void testBlockOpen() {
1484: SimpleAttributeSet attr = new SimpleAttributeSet();
1485: attr.addAttribute("aaaa", "bbbb");
1486: reader.blockOpen(Tag.B, attr);
1487: assertEquals(0, reader.charAttr.getAttributeCount());
1488: assertEquals(1, reader.parseBuffer.size());
1489: final ElementSpec spec = (ElementSpec) reader.parseBuffer
1490: .get(0);
1491: final AttributeSet specAttr = spec.getAttributes();
1492: assertEquals(2, specAttr.getAttributeCount());
1493: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1494: checkAttributes(specAttr, "aaaa", "bbbb");
1495: assertSpec(spec, ElementSpec.StartTagType,
1496: ElementSpec.OriginateDirection, 0, null);
1497:
1498: assertNotSame(attr, specAttr);
1499: assertEquals(2, attr.getAttributeCount());
1500: checkAttributes(attr, StyleConstants.NameAttribute, Tag.B);
1501: }
1502:
1503: public void testBlockOpen_ImpliedAttribute() {
1504: SimpleAttributeSet attr = new SimpleAttributeSet();
1505: attr.addAttribute("aaaa", "bbbb");
1506: attr.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED,
1507: Boolean.TRUE);
1508: reader.blockOpen(Tag.B, attr);
1509: assertEquals(0, reader.charAttr.getAttributeCount());
1510: assertEquals(1, reader.parseBuffer.size());
1511: final ElementSpec spec = (ElementSpec) reader.parseBuffer
1512: .get(0);
1513: final AttributeSet specAttr = spec.getAttributes();
1514: assertEquals(2, specAttr.getAttributeCount());
1515: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1516: checkAttributes(specAttr, "aaaa", "bbbb");
1517: assertSpec(spec, ElementSpec.StartTagType,
1518: ElementSpec.OriginateDirection, 0, null);
1519:
1520: assertNotSame(attr, specAttr);
1521: assertEquals(2, attr.getAttributeCount());
1522: checkAttributes(attr, StyleConstants.NameAttribute, Tag.B);
1523: checkAttributes(attr, "aaaa", "bbbb");
1524: }
1525:
1526: public void testBlockClose() {
1527: reader.blockClose(Tag.B);
1528: assertEquals(0, reader.charAttr.getAttributeCount());
1529: assertEquals(1, reader.parseBuffer.size());
1530: final ElementSpec spec = (ElementSpec) reader.parseBuffer
1531: .get(0);
1532: checkEndTagSpec(spec);
1533: }
1534:
1535: public void testBlockOpenClose() {
1536: SimpleAttributeSet attr = new SimpleAttributeSet();
1537: attr.addAttribute("aaaa", "bbbb");
1538:
1539: reader.blockOpen(Tag.B, attr);
1540: assertEquals(1, reader.parseBuffer.size());
1541: assertEquals(0, reader.charAttr.getAttributeCount());
1542:
1543: reader.blockClose(Tag.B);
1544: assertEquals(5, reader.parseBuffer.size());
1545: assertEquals(0, reader.charAttr.getAttributeCount());
1546:
1547: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1548: assertSpec(spec, ElementSpec.StartTagType,
1549: ElementSpec.OriginateDirection, 0, null);
1550: assertEquals(2, spec.getAttributes().getAttributeCount());
1551: checkAttributes(spec.getAttributes(),
1552: StyleConstants.NameAttribute, Tag.B);
1553:
1554: spec = (ElementSpec) reader.parseBuffer.get(1);
1555: checkOpenImpliedSpec(spec);
1556:
1557: spec = (ElementSpec) reader.parseBuffer.get(2);
1558: assertSpec(spec, ElementSpec.ContentType,
1559: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1560: assertEquals(2, spec.getAttributes().getAttributeCount());
1561: checkAttributes(spec.getAttributes(),
1562: StyleConstants.NameAttribute, Tag.CONTENT);
1563: checkAttributes(spec.getAttributes(), "CR", Boolean.TRUE);
1564:
1565: spec = (ElementSpec) reader.parseBuffer.get(3);
1566: checkEndTagSpec(spec);
1567:
1568: spec = (ElementSpec) reader.parseBuffer.get(4);
1569: checkEndTagSpec(spec);
1570: }
1571:
1572: public void testBlockOpenOpenCloseClose() {
1573: SimpleAttributeSet attr = new SimpleAttributeSet();
1574: attr.addAttribute("aaaa", "bbbb");
1575:
1576: reader.blockOpen(Tag.B, attr);
1577: assertEquals(1, reader.parseBuffer.size());
1578: assertEquals(0, reader.charAttr.getAttributeCount());
1579:
1580: reader.blockOpen(Tag.I, attr);
1581: assertEquals(2, reader.parseBuffer.size());
1582: assertEquals(0, reader.charAttr.getAttributeCount());
1583:
1584: reader.blockClose(Tag.I);
1585: assertEquals(6, reader.parseBuffer.size());
1586: assertEquals(0, reader.charAttr.getAttributeCount());
1587:
1588: reader.blockClose(Tag.B);
1589: assertEquals(7, reader.parseBuffer.size());
1590: assertEquals(0, reader.charAttr.getAttributeCount());
1591:
1592: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1593: assertSpec(spec, ElementSpec.StartTagType,
1594: ElementSpec.OriginateDirection, 0, null);
1595: AttributeSet specAttr = spec.getAttributes();
1596: assertEquals(2, specAttr.getAttributeCount());
1597: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1598: checkAttributes(specAttr, "aaaa", "bbbb");
1599:
1600: spec = (ElementSpec) reader.parseBuffer.get(1);
1601: specAttr = spec.getAttributes();
1602: assertSpec(spec, ElementSpec.StartTagType,
1603: ElementSpec.OriginateDirection, 0, null);
1604: assertEquals(2, specAttr.getAttributeCount());
1605: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.I);
1606: checkAttributes(specAttr, "aaaa", "bbbb");
1607:
1608: spec = (ElementSpec) reader.parseBuffer.get(2);
1609: checkOpenImpliedSpec(spec);
1610:
1611: spec = (ElementSpec) reader.parseBuffer.get(3);
1612: specAttr = spec.getAttributes();
1613: assertSpec(spec, ElementSpec.ContentType,
1614: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1615: assertEquals(2, specAttr.getAttributeCount());
1616: checkAttributes(specAttr, StyleConstants.NameAttribute,
1617: Tag.CONTENT);
1618: checkAttributes(specAttr, "CR", Boolean.TRUE);
1619:
1620: spec = (ElementSpec) reader.parseBuffer.get(4);
1621: checkEndTagSpec(spec);
1622:
1623: spec = (ElementSpec) reader.parseBuffer.get(5);
1624: checkEndTagSpec(spec);
1625:
1626: spec = (ElementSpec) reader.parseBuffer.get(6);
1627: checkEndTagSpec(spec);
1628: }
1629:
1630: public void testBlockOpenOpenCloseClose_Implied() {
1631: SimpleAttributeSet attr = new SimpleAttributeSet();
1632: attr.addAttribute("aaaa", "bbbb");
1633:
1634: reader.blockOpen(Tag.B, attr);
1635: assertEquals(1, reader.parseBuffer.size());
1636: assertEquals(0, reader.charAttr.getAttributeCount());
1637:
1638: reader.blockOpen(Tag.IMPLIED, attr);
1639: assertEquals(2, reader.parseBuffer.size());
1640: assertEquals(0, reader.charAttr.getAttributeCount());
1641:
1642: reader.blockClose(Tag.IMPLIED);
1643: assertEquals(5, reader.parseBuffer.size());
1644: assertEquals(0, reader.charAttr.getAttributeCount());
1645:
1646: reader.blockClose(Tag.B);
1647: assertEquals(6, reader.parseBuffer.size());
1648: assertEquals(0, reader.charAttr.getAttributeCount());
1649:
1650: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1651: assertSpec(spec, ElementSpec.StartTagType,
1652: ElementSpec.OriginateDirection, 0, null);
1653: AttributeSet specAttr = spec.getAttributes();
1654: assertEquals(2, specAttr.getAttributeCount());
1655: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1656:
1657: spec = (ElementSpec) reader.parseBuffer.get(1);
1658: specAttr = spec.getAttributes();
1659: assertSpec(spec, ElementSpec.StartTagType,
1660: ElementSpec.OriginateDirection, 0, null);
1661: assertEquals(2, specAttr.getAttributeCount());
1662: checkAttributes(specAttr, StyleConstants.NameAttribute,
1663: Tag.IMPLIED);
1664: checkAttributes(specAttr, "aaaa", "bbbb");
1665:
1666: spec = (ElementSpec) reader.parseBuffer.get(2);
1667: checkOpenImpliedSpec(spec);
1668:
1669: spec = (ElementSpec) reader.parseBuffer.get(3);
1670: specAttr = spec.getAttributes();
1671: assertSpec(spec, ElementSpec.ContentType,
1672: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1673: assertEquals(2, specAttr.getAttributeCount());
1674: checkAttributes(specAttr, StyleConstants.NameAttribute,
1675: Tag.CONTENT);
1676: checkAttributes(specAttr, "CR", Boolean.TRUE);
1677:
1678: spec = (ElementSpec) reader.parseBuffer.get(4);
1679: checkEndTagSpec(spec);
1680:
1681: spec = (ElementSpec) reader.parseBuffer.get(5);
1682: checkEndTagSpec(spec);
1683: }
1684:
1685: public void testBlockOpenOpenCloseClose_ImpliedImplied() {
1686: SimpleAttributeSet attr = new SimpleAttributeSet();
1687: attr.addAttribute("aaaa", "bbbb");
1688:
1689: reader.blockOpen(Tag.TITLE, attr);
1690: assertEquals(1, reader.parseBuffer.size());
1691: assertEquals(0, reader.charAttr.getAttributeCount());
1692:
1693: reader.blockOpen(Tag.IMPLIED, attr);
1694: assertEquals(2, reader.parseBuffer.size());
1695: assertEquals(0, reader.charAttr.getAttributeCount());
1696:
1697: reader.blockClose(Tag.IMPLIED);
1698: assertEquals(5, reader.parseBuffer.size());
1699: assertEquals(0, reader.charAttr.getAttributeCount());
1700:
1701: reader.blockClose(Tag.IMPLIED);
1702: assertEquals(6, reader.parseBuffer.size());
1703: assertEquals(0, reader.charAttr.getAttributeCount());
1704:
1705: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1706: assertSpec(spec, ElementSpec.StartTagType,
1707: ElementSpec.OriginateDirection, 0, null);
1708: AttributeSet specAttr = spec.getAttributes();
1709: assertEquals(2, specAttr.getAttributeCount());
1710: checkAttributes(specAttr, StyleConstants.NameAttribute,
1711: Tag.TITLE);
1712:
1713: spec = (ElementSpec) reader.parseBuffer.get(1);
1714: specAttr = spec.getAttributes();
1715: assertSpec(spec, ElementSpec.StartTagType,
1716: ElementSpec.OriginateDirection, 0, null);
1717: assertEquals(2, specAttr.getAttributeCount());
1718: checkAttributes(specAttr, StyleConstants.NameAttribute,
1719: Tag.IMPLIED);
1720: checkAttributes(specAttr, "aaaa", "bbbb");
1721:
1722: spec = (ElementSpec) reader.parseBuffer.get(2);
1723: checkOpenImpliedSpec(spec);
1724:
1725: spec = (ElementSpec) reader.parseBuffer.get(3);
1726: specAttr = spec.getAttributes();
1727: assertSpec(spec, ElementSpec.ContentType,
1728: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1729: assertEquals(2, specAttr.getAttributeCount());
1730: checkAttributes(specAttr, StyleConstants.NameAttribute,
1731: Tag.CONTENT);
1732: checkAttributes(specAttr, "CR", Boolean.TRUE);
1733:
1734: spec = (ElementSpec) reader.parseBuffer.get(4);
1735: checkEndTagSpec(spec);
1736:
1737: spec = (ElementSpec) reader.parseBuffer.get(5);
1738: checkEndTagSpec(spec);
1739: }
1740:
1741: public void testBlockOpenContentClose_B() {
1742: SimpleAttributeSet attr = new SimpleAttributeSet();
1743: attr.addAttribute("aaaa", "bbbb");
1744:
1745: reader.blockOpen(Tag.B, attr);
1746: assertEquals(1, reader.parseBuffer.size());
1747: assertEquals(0, reader.charAttr.getAttributeCount());
1748:
1749: final String text = "text";
1750: reader.addContent(text.toCharArray(), 0, 4);
1751: assertEquals(3, reader.parseBuffer.size());
1752: assertEquals(1, reader.charAttr.getAttributeCount());
1753:
1754: reader.blockClose(Tag.B);
1755: assertEquals(6, reader.parseBuffer.size());
1756: assertEquals(1, reader.charAttr.getAttributeCount());
1757:
1758: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1759: assertSpec(spec, ElementSpec.StartTagType,
1760: ElementSpec.OriginateDirection, 0, null);
1761: AttributeSet specAttr = spec.getAttributes();
1762: assertEquals(2, specAttr.getAttributeCount());
1763: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1764: checkAttributes(specAttr, "aaaa", "bbbb");
1765:
1766: spec = (ElementSpec) reader.parseBuffer.get(1);
1767: checkOpenImpliedSpec(spec);
1768:
1769: spec = (ElementSpec) reader.parseBuffer.get(2);
1770: assertSpec(spec, ElementSpec.ContentType,
1771: ElementSpec.OriginateDirection, 0, text.toCharArray());
1772: assertEquals(2, specAttr.getAttributeCount());
1773: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1774: checkAttributes(specAttr, "aaaa", "bbbb");
1775:
1776: spec = (ElementSpec) reader.parseBuffer.get(3);
1777: assertSpec(spec, ElementSpec.ContentType,
1778: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1779: assertEquals(2, specAttr.getAttributeCount());
1780: checkAttributes(specAttr, StyleConstants.NameAttribute, Tag.B);
1781: checkAttributes(specAttr, "aaaa", "bbbb");
1782:
1783: spec = (ElementSpec) reader.parseBuffer.get(4);
1784: checkEndTagSpec(spec);
1785:
1786: spec = (ElementSpec) reader.parseBuffer.get(5);
1787: checkEndTagSpec(spec);
1788: }
1789:
1790: public void testBlockOpenContentClose_Implied() {
1791: SimpleAttributeSet attr = new SimpleAttributeSet();
1792: attr.addAttribute("aaaa", "bbbb");
1793:
1794: reader.blockOpen(Tag.IMPLIED, attr);
1795: assertEquals(1, reader.parseBuffer.size());
1796: assertEquals(0, reader.charAttr.getAttributeCount());
1797:
1798: final String text = "text";
1799: reader.addContent(text.toCharArray(), 0, 4);
1800: assertEquals(3, reader.parseBuffer.size());
1801: assertEquals(1, reader.charAttr.getAttributeCount());
1802:
1803: reader.blockClose(Tag.IMPLIED);
1804: assertEquals(5, reader.parseBuffer.size());
1805: assertEquals(1, reader.charAttr.getAttributeCount());
1806:
1807: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1808: assertSpec(spec, ElementSpec.StartTagType,
1809: ElementSpec.OriginateDirection, 0, null);
1810: AttributeSet specAttr = spec.getAttributes();
1811: assertEquals(2, specAttr.getAttributeCount());
1812: checkAttributes(specAttr, StyleConstants.NameAttribute,
1813: Tag.IMPLIED);
1814: checkAttributes(specAttr, "aaaa", "bbbb");
1815:
1816: spec = (ElementSpec) reader.parseBuffer.get(1);
1817: checkOpenImpliedSpec(spec);
1818:
1819: spec = (ElementSpec) reader.parseBuffer.get(2);
1820: assertSpec(spec, ElementSpec.ContentType,
1821: ElementSpec.OriginateDirection, 0, text.toCharArray());
1822: specAttr = spec.getAttributes();
1823: assertEquals(1, specAttr.getAttributeCount());
1824: checkAttributes(specAttr, StyleConstants.NameAttribute,
1825: Tag.CONTENT);
1826:
1827: spec = (ElementSpec) reader.parseBuffer.get(3);
1828: assertSpec(spec, ElementSpec.ContentType,
1829: ElementSpec.OriginateDirection, 0, new char[] { '\n' });
1830: specAttr = spec.getAttributes();
1831: assertEquals(2, specAttr.getAttributeCount());
1832: checkAttributes(specAttr, StyleConstants.NameAttribute,
1833: Tag.CONTENT);
1834: checkAttributes(specAttr, "CR", Boolean.TRUE);
1835:
1836: spec = (ElementSpec) reader.parseBuffer.get(4);
1837: checkEndTagSpec(spec);
1838: }
1839:
1840: public void testRegisterTag() throws Exception {
1841: SimpleAttributeSet attr = new SimpleAttributeSet();
1842: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
1843: .toString());
1844: final Marker endMarker = new Marker();
1845: final Marker startMarker = new Marker();
1846: reader.registerTag(Tag.B, reader.new TagAction() {
1847: public void end(final Tag tag) {
1848: endMarker.setAuxiliary(tag);
1849: endMarker.setOccurred();
1850: }
1851:
1852: public void start(final Tag tag,
1853: final MutableAttributeSet attr) {
1854: startMarker.setOccurred();
1855: startMarker.setAuxiliary(attr);
1856: }
1857: });
1858: reader.handleSimpleTag(Tag.B, attr, 0);
1859: assertEquals(0, reader.parseBuffer.size());
1860: assertEquals(0, reader.charAttr.getAttributeCount());
1861: assertFalse(createMarker.isOccurred());
1862: assertFalse(insertMarker.isOccurred());
1863: assertTrue(startMarker.isOccurred());
1864: assertTrue(endMarker.isOccurred());
1865: assertSame(attr, startMarker.getAuxiliary());
1866: assertEquals(Tag.B, endMarker.getAuxiliary());
1867: }
1868:
1869: public void testPreContent() {
1870: String text = "data";
1871: assertEquals(0, reader.parseBuffer.size());
1872: reader.charAttr.addAttribute("aaaa", "bbbb");
1873: reader.preContent(text.toCharArray());
1874: assertEquals(2, reader.charAttr.getAttributeCount());
1875: checkAttributes(reader.charAttr, StyleConstants.NameAttribute,
1876: Tag.CONTENT);
1877: assertEquals(2, reader.parseBuffer.size());
1878:
1879: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(0);
1880: checkOpenImpliedSpec(spec);
1881:
1882: spec = (ElementSpec) reader.parseBuffer.get(1);
1883: assertSpec(spec, ElementSpec.ContentType,
1884: ElementSpec.OriginateDirection, 0, text.toCharArray());
1885: assertEquals(2, spec.getAttributes().getAttributeCount());
1886: checkAttributes(spec.getAttributes(), "aaaa", "bbbb");
1887: checkAttributes(spec.getAttributes(),
1888: StyleConstants.NameAttribute, Tag.CONTENT);
1889: assertFalse(createMarker.isOccurred());
1890: assertFalse(insertMarker.isOccurred());
1891: }
1892:
1893: public void testPreContent_Calls() {
1894: final Marker contentMarker = new Marker();
1895: doc = new HTMLDocument() {
1896: public ParserCallback getReader(int pos) {
1897: return new HTMLReader(0) {
1898: protected void addContent(char[] data, int offs,
1899: int length) {
1900: contentMarker.setOccurred();
1901: ArrayList callInfo = new ArrayList();
1902: callInfo.add(data);
1903: callInfo.add(new Integer(offs));
1904: callInfo.add(new Integer(length));
1905: contentMarker.setAuxiliary(callInfo);
1906: };
1907: };
1908: }
1909: };
1910: reader = (HTMLReader) doc.getReader(0);
1911: String text = "precontent";
1912: reader.preContent(text.toCharArray());
1913: assertTrue(contentMarker.isOccurred());
1914: ArrayList callInfo = (ArrayList) contentMarker.getAuxiliary();
1915: assertEquals(text, new String((char[]) callInfo.get(0)));
1916: assertEquals(new Integer(0), callInfo.get(1));
1917: assertEquals(text.length(), ((Integer) callInfo.get(2))
1918: .intValue());
1919: }
1920:
1921: public void testTextAreaContent() throws Exception {
1922: SimpleAttributeSet attr = new SimpleAttributeSet();
1923: String text1 = "data";
1924: String text2 = "atada";
1925: reader.handleStartTag(Tag.TEXTAREA, attr, 0);
1926: assertEquals(0, reader.charAttr.getAttributeCount());
1927: assertEquals(2, reader.parseBuffer.size());
1928: ElementSpec spec = (ElementSpec) reader.parseBuffer.get(1);
1929: assertSpec(spec, ElementSpec.ContentType,
1930: ElementSpec.OriginateDirection, 0, new char[] { ' ' });
1931: assertEquals(2, spec.getAttributes().getAttributeCount());
1932: checkAttributes(spec.getAttributes(),
1933: StyleConstants.NameAttribute, Tag.TEXTAREA);
1934: Object contentModel = spec.getAttributes().getAttribute(
1935: StyleConstants.ModelAttribute);
1936: assertNotNull(contentModel);
1937: assertTrue(contentModel instanceof PlainDocument);
1938: final PlainDocument plainDocument = (PlainDocument) contentModel;
1939: assertEquals("", plainDocument.getText(0, plainDocument
1940: .getLength()));
1941: reader.parseBuffer.add(new ElementSpec(null,
1942: ElementSpec.EndTagType));
1943: assertEquals(3, reader.parseBuffer.size());
1944:
1945: reader.textAreaContent(text1.toCharArray());
1946: assertEquals(0, reader.charAttr.getAttributeCount());
1947: assertEquals(3, reader.parseBuffer.size());
1948: assertEquals(4, plainDocument.getLength());
1949: assertEquals(text1, plainDocument.getText(0, plainDocument
1950: .getLength()));
1951:
1952: reader.textAreaContent(text2.toCharArray());
1953: assertEquals(0, reader.charAttr.getAttributeCount());
1954: assertEquals(3, reader.parseBuffer.size());
1955: assertEquals(9, plainDocument.getLength());
1956: assertEquals(text1 + text2, plainDocument.getText(0,
1957: plainDocument.getLength()));
1958:
1959: assertFalse(createMarker.isOccurred());
1960: assertFalse(insertMarker.isOccurred());
1961: }
1962:
1963: public void testHandleSimpleTag_Unknown() {
1964: SimpleAttributeSet attr = new SimpleAttributeSet();
1965: attr.addAttribute("aaaa", "bbbb");
1966:
1967: doc.setPreservesUnknownTags(true);
1968: reader.handleStartTag(Tag.HTML, attr, 0);
1969: reader.handleStartTag(Tag.BODY, attr, 0);
1970: reader.handleStartTag(Tag.P, attr, 0);
1971: assertEquals(3, reader.parseBuffer.size());
1972: reader.handleSimpleTag(new Tag("fake"), attr, 0);
1973: assertEquals(4, reader.parseBuffer.size());
1974:
1975: init();
1976: doc.setPreservesUnknownTags(false);
1977: reader.handleStartTag(Tag.HTML, attr, 0);
1978: reader.handleStartTag(Tag.BODY, attr, 0);
1979: reader.handleStartTag(Tag.P, attr, 0);
1980: assertEquals(3, reader.parseBuffer.size());
1981: reader.handleSimpleTag(new Tag("fake"), attr, 0);
1982: assertEquals(3, reader.parseBuffer.size());
1983: }
1984:
1985: public void testHandleStartEndTag_Unknown() {
1986: SimpleAttributeSet attr = new SimpleAttributeSet();
1987: attr.addAttribute("aaaa", "bbbb");
1988:
1989: doc.setPreservesUnknownTags(true);
1990: reader.handleStartTag(Tag.HTML, attr, 0);
1991: reader.handleStartTag(Tag.BODY, attr, 0);
1992: reader.handleStartTag(Tag.P, attr, 0);
1993: assertEquals(3, reader.parseBuffer.size());
1994: reader.handleStartTag(new Tag("fake"), attr, 0);
1995: reader.handleEndTag(new Tag("fake"), 0);
1996: assertEquals(3, reader.parseBuffer.size());
1997: }
1998:
1999: private void init() {
2000: createMarker = new Marker();
2001: insertMarker = new Marker();
2002: doc = new HTMLDocument() {
2003: protected void create(ElementSpec[] data) {
2004: createMarker.setOccurred();
2005: createMarker.setAuxiliary(data);
2006: if (editable) {
2007: super .create(data);
2008: }
2009: }
2010:
2011: protected void insert(int offset, ElementSpec[] data)
2012: throws BadLocationException {
2013: insertMarker.setOccurred();
2014: insertMarker.setAuxiliary(data);
2015: if (editable) {
2016: super .insert(offset, data);
2017: }
2018: }
2019: };
2020: reader = (HTMLReader) doc.getReader(0);
2021: editable = true;
2022: }
2023:
2024: private void checkConstructorTagParameter(final Tag tag,
2025: final String str, final int numSpecs) throws Exception {
2026: init();
2027: editable = false;
2028: ParserCallback reader = doc.getReader(0, 0, 0, tag);
2029: SimpleAttributeSet attr = new SimpleAttributeSet();
2030: attr.addAttribute(StyleConstants.NameAttribute, Tag.B
2031: .toString());
2032: doc.insertString(0, "0000", attr);
2033: assertFalse("no inserts", insertMarker.isOccurred());
2034: parse(str, reader);
2035: reader.flush();
2036: if (numSpecs == 0 && isHarmony()) {
2037: assertFalse("inserted", insertMarker.isOccurred());
2038: } else {
2039: assertTrue("inserted", insertMarker.isOccurred());
2040: ElementSpec[] specs = (ElementSpec[]) insertMarker
2041: .getAuxiliary();
2042: assertEquals("number of specs inserted", numSpecs,
2043: specs.length);
2044: insertMarker.reset();
2045: }
2046: }
2047:
2048: private void checkConstructorOffsetParameter(final Tag tag,
2049: final String str, final int offset) throws Exception {
2050: insertMarker.reset();
2051: doc = new HTMLDocument() {
2052: protected void insert(int offset, ElementSpec[] data)
2053: throws BadLocationException {
2054: insertMarker.setOccurred();
2055: insertMarker.setAuxiliary(new Integer(offset));
2056: }
2057: };
2058: ParserCallback reader = doc.getReader(offset, 0, 0, tag);
2059: parse(str, reader);
2060: reader.flush();
2061: assertTrue(insertMarker.isOccurred());
2062: assertEquals(new Integer(offset), insertMarker.getAuxiliary());
2063: }
2064:
2065: private void checkConstructorPopPushParameter(final Tag tag,
2066: final String str, final int pop, final int push,
2067: final String specsDescr) throws Exception {
2068: init();
2069: editable = false;
2070: ParserCallback reader = doc.getReader(0, pop, push, tag);
2071: SimpleAttributeSet attr = new SimpleAttributeSet();
2072: attr.addAttribute(StyleConstants.NameAttribute, Tag.S
2073: .toString());
2074: doc.insertString(0, "0000", attr);
2075: assertFalse(insertMarker.isOccurred());
2076: parse(str, reader);
2077: reader.flush();
2078: assertTrue(insertMarker.isOccurred());
2079: ElementSpec[] specs = (ElementSpec[]) insertMarker
2080: .getAuxiliary();
2081: assertEquals(specsDescr.length() / 2, specs.length);
2082: insertMarker.reset();
2083: for (int i = 0; i < specs.length; i++) {
2084: checkSpecType(specsDescr, i, (ElementSpec) specs[i]);
2085: }
2086: }
2087:
2088: private void checkConstructorOffsetPopPushParameter(final Tag tag,
2089: final String str, final int offset, final int pop,
2090: final int push, final String specsDescr) throws Exception {
2091: init();
2092: ParserCallback reader = doc.getReader(offset, pop, push, tag);
2093: SimpleAttributeSet attr = new SimpleAttributeSet();
2094: attr.addAttribute(StyleConstants.NameAttribute, Tag.S
2095: .toString());
2096: editable = false;
2097: insertMarker.reset();
2098: parse(str, reader);
2099: reader.flush();
2100: assertTrue(insertMarker.isOccurred());
2101: ElementSpec[] specs = (ElementSpec[]) insertMarker
2102: .getAuxiliary();
2103: assertEquals(specsDescr.length() / 2, specs.length);
2104: insertMarker.reset();
2105: for (int i = 0; i < specs.length; i++) {
2106: checkSpecType(specsDescr, i, (ElementSpec) specs[i]);
2107: }
2108: }
2109:
2110: private void checkTextIsInserted() {
2111: int numSpecs = reader.parseBuffer.size();
2112: reader.handleText("text".toCharArray(), 0);
2113: assertEquals(numSpecs + 2, reader.parseBuffer.size());
2114: }
2115:
2116: private void checkTextInNotInserted() {
2117: int numSpecs = reader.parseBuffer.size();
2118: reader.handleText("text".toCharArray(), 0);
2119: assertEquals(numSpecs, reader.parseBuffer.size());
2120: }
2121:
2122: private void checkConstructorPopParameter(final Tag tag,
2123: final String str, final int pop, final String specsDescr)
2124: throws Exception {
2125: checkConstructorPopPushParameter(tag, str, pop, 0, specsDescr);
2126: }
2127:
2128: private void checkConstructorPushParameter(final Tag tag,
2129: final String str, final int push, final String specsDescr)
2130: throws Exception {
2131: checkConstructorPopPushParameter(tag, str, 0, push, specsDescr);
2132: }
2133:
2134: private void parse(final String str, ParserCallback reader)
2135: throws IOException {
2136: new ParserDelegator()
2137: .parse(new StringReader(str), reader, true);
2138: }
2139:
2140: private void checkSpecType(final String specsDescr, int i,
2141: final DefaultStyledDocument.ElementSpec spec) {
2142: final char typeChar = specsDescr.charAt(i * 2);
2143: short specType = 0;
2144: if (typeChar == 's') {
2145: specType = ElementSpec.StartTagType;
2146: } else if (typeChar == 'c') {
2147: specType = ElementSpec.ContentType;
2148: } else if (typeChar == 'e') {
2149: specType = ElementSpec.EndTagType;
2150: }
2151: final char dirChar = specsDescr.charAt(i * 2 + 1);
2152: short specDir = 0;
2153: if (dirChar == 'n') {
2154: specDir = ElementSpec.JoinNextDirection;
2155: } else if (dirChar == 'p') {
2156: specDir = ElementSpec.JoinPreviousDirection;
2157: } else if (dirChar == 'o') {
2158: specDir = ElementSpec.OriginateDirection;
2159: }
2160: assertEquals("spec direction", specDir, spec.getDirection());
2161: assertEquals("spec type", specType, spec.getType());
2162: }
2163:
2164: private void createContentMarkersInstrumentedReader(
2165: final Marker addContentMarker,
2166: final Marker preContentMarker, final Marker textAreaMarker) {
2167: doc = new HTMLDocument() {
2168: public ParserCallback getReader(int pos) {
2169: return new HTMLReader(pos) {
2170: protected void addContent(char[] data, int offset,
2171: int length,
2172: boolean createImpliedPIfNecessary) {
2173: addContentMarker.setOccurred();
2174: super .addContent(data, offset, length,
2175: createImpliedPIfNecessary);
2176: };
2177:
2178: protected void preContent(char[] data) {
2179: preContentMarker.setOccurred();
2180: super .preContent(data);
2181: }
2182:
2183: protected void textAreaContent(char[] data) {
2184: textAreaMarker.setOccurred();
2185: super .textAreaContent(data);
2186: }
2187: };
2188: }
2189: };
2190: reader = (HTMLReader) doc.getReader(0);
2191: }
2192:
2193: private void checkJoinPrevSpec(final ElementSpec firstSpec) {
2194: assertEquals(ElementSpec.ContentType, firstSpec.getType());
2195: assertEquals(ElementSpec.JoinPreviousDirection, firstSpec
2196: .getDirection());
2197: assertEquals('\n', firstSpec.getArray()[0]);
2198: }
2199:
2200: private boolean foundInArray(final Tag[] array, final Tag tag) {
2201: for (int i = 0; i < array.length; i++) {
2202: if (tag.equals(array[i])) {
2203: return true;
2204: }
2205: }
2206: return false;
2207: }
2208:
2209: }
|