001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.editor.highlighting.support;
043:
044: import java.util.Enumeration;
045: import javax.swing.text.AttributeSet;
046: import javax.swing.text.DefaultStyledDocument;
047: import javax.swing.text.Document;
048: import javax.swing.text.Position;
049: import javax.swing.text.SimpleAttributeSet;
050: import junit.framework.AssertionFailedError;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.modules.editor.lib2.highlighting.OffsetGapList;
053: import org.netbeans.spi.editor.highlighting.*;
054:
055: /**
056: *
057: * @author vita
058: */
059: public class MergingOffsetsBagTest extends NbTestCase {
060:
061: private static final AttributeSet EMPTY = SimpleAttributeSet.EMPTY;
062:
063: private Document doc = new DefaultStyledDocument();
064:
065: /** Creates a new instance of HighlightSequenceTest */
066: public MergingOffsetsBagTest(String name) {
067: super (name);
068: }
069:
070: public void testSimple() {
071: OffsetsBag hs = new OffsetsBag(doc, true);
072: assertEquals("Sequence should be empty", 0, hs.getMarks()
073: .size());
074:
075: hs.addHighlight(10, 20, EMPTY);
076: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
077: assertEquals("Sequence should not be empty", 2, marks.size());
078: assertEquals("Wrong highlight's start offset", 10, marks.get(0)
079: .getOffset());
080: assertEquals("Wrong highlight's end offset", 20, marks.get(1)
081: .getOffset());
082: assertNull("Wrong highlight's end", marks.get(1)
083: .getAttributes());
084:
085: hs.clear();
086: assertEquals("Sequence was not cleared", 0, hs.getMarks()
087: .size());
088: }
089:
090: public void testAddLeftOverlap() {
091: OffsetsBag hs = new OffsetsBag(doc, true);
092: SimpleAttributeSet attribsA = new SimpleAttributeSet();
093: SimpleAttributeSet attribsB = new SimpleAttributeSet();
094:
095: attribsA.addAttribute("set-A", "attribsA");
096: attribsB.addAttribute("set-B", "attribsB");
097:
098: hs.addHighlight(10, 20, attribsA);
099: hs.addHighlight(5, 15, attribsB);
100: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
101:
102: assertEquals("Wrong number of highlights", 4, marks.size());
103: assertEquals("1. highlight - wrong start offset", 5, marks.get(
104: 0).getOffset());
105: assertEquals("1. highlight - wrong end offset", 10, marks
106: .get(1).getOffset());
107: assertAttribs("1. highlight - wrong attribs", marks.get(0)
108: .getAttributes(), "set-B");
109:
110: assertEquals("2. highlight - wrong start offset", 10, marks
111: .get(1).getOffset());
112: assertEquals("2. highlight - wrong end offset", 15, marks
113: .get(2).getOffset());
114: assertAttribs("2. highlight - wrong attribs", marks.get(1)
115: .getAttributes(), "set-A", "set-B");
116:
117: assertEquals("3. highlight - wrong start offset", 15, marks
118: .get(2).getOffset());
119: assertEquals("3. highlight - wrong end offset", 20, marks
120: .get(3).getOffset());
121: assertAttribs("3. highlight - wrong attribs", marks.get(2)
122: .getAttributes(), "set-A");
123: assertNull("3. highlight - wrong end", marks.get(3)
124: .getAttributes());
125: }
126:
127: public void testAddRightOverlap() {
128: OffsetsBag hs = new OffsetsBag(doc, true);
129: SimpleAttributeSet attribsA = new SimpleAttributeSet();
130: SimpleAttributeSet attribsB = new SimpleAttributeSet();
131:
132: attribsA.addAttribute("set-A", "attribsA");
133: attribsB.addAttribute("set-B", "attribsB");
134:
135: hs.addHighlight(10, 20, attribsA);
136: hs.addHighlight(15, 25, attribsB);
137: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
138:
139: assertEquals("Wrong number of highlights", 4, marks.size());
140: assertEquals("1. highlight - wrong start offset", 10, marks
141: .get(0).getOffset());
142: assertEquals("1. highlight - wrong end offset", 15, marks
143: .get(1).getOffset());
144: assertAttribs("1. highlight - wrong attribs", marks.get(0)
145: .getAttributes(), "set-A");
146:
147: assertEquals("2. highlight - wrong start offset", 15, marks
148: .get(1).getOffset());
149: assertEquals("2. highlight - wrong end offset", 20, marks
150: .get(2).getOffset());
151: assertAttribs("2. highlight - wrong attribs", marks.get(1)
152: .getAttributes(), "set-A", "set-B");
153:
154: assertEquals("3. highlight - wrong start offset", 20, marks
155: .get(2).getOffset());
156: assertEquals("3. highlight - wrong end offset", 25, marks
157: .get(3).getOffset());
158: assertAttribs("3. highlight - wrong attribs", marks.get(2)
159: .getAttributes(), "set-B");
160: assertNull("3. highlight - wrong end", marks.get(3)
161: .getAttributes());
162: }
163:
164: public void testAddLeftMatchBiggerOverlap() {
165: OffsetsBag hs = new OffsetsBag(doc, true);
166: SimpleAttributeSet attribsA = new SimpleAttributeSet();
167: SimpleAttributeSet attribsB = new SimpleAttributeSet();
168:
169: attribsA.addAttribute("set-A", "attribsA");
170: attribsB.addAttribute("set-B", "attribsB");
171:
172: hs.addHighlight(10, 20, attribsA);
173: hs.addHighlight(10, 15, attribsB);
174: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
175:
176: assertEquals("Wrong number of highlights", 3, marks.size());
177: assertEquals("1. highlight - wrong start offset", 10, marks
178: .get(0).getOffset());
179: assertEquals("1. highlight - wrong end offset", 15, marks
180: .get(1).getOffset());
181: assertAttribs("1. highlight - wrong attribs", marks.get(0)
182: .getAttributes(), "set-A", "set-B");
183:
184: assertEquals("2. highlight - wrong start offset", 15, marks
185: .get(1).getOffset());
186: assertEquals("2. highlight - wrong end offset", 20, marks
187: .get(2).getOffset());
188: assertAttribs("2. highlight - wrong attribs", marks.get(1)
189: .getAttributes(), "set-A");
190: assertNull("2. highlight - wrong end", marks.get(2)
191: .getAttributes());
192: }
193:
194: public void testAddRightMatchBiggerOverlap() {
195: OffsetsBag hs = new OffsetsBag(doc, true);
196: SimpleAttributeSet attribsA = new SimpleAttributeSet();
197: SimpleAttributeSet attribsB = new SimpleAttributeSet();
198:
199: attribsA.addAttribute("set-A", "attribsA");
200: attribsB.addAttribute("set-B", "attribsB");
201:
202: hs.addHighlight(10, 20, attribsA);
203: hs.addHighlight(15, 20, attribsB);
204: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
205:
206: assertEquals("Wrong number of highlights", 3, marks.size());
207: assertEquals("1. highlight - wrong start offset", 10, marks
208: .get(0).getOffset());
209: assertEquals("1. highlight - wrong end offset", 15, marks
210: .get(1).getOffset());
211: assertAttribs("1. highlight - wrong attribs", marks.get(0)
212: .getAttributes(), "set-A");
213:
214: assertEquals("2. highlight - wrong start offset", 15, marks
215: .get(1).getOffset());
216: assertEquals("2. highlight - wrong end offset", 20, marks
217: .get(2).getOffset());
218: assertAttribs("2. highlight - wrong attribs", marks.get(1)
219: .getAttributes(), "set-A", "set-B");
220: assertNull("2. highlight - wrong end", marks.get(2)
221: .getAttributes());
222: }
223:
224: public void testAddCompleteMatchOverlap() {
225: OffsetsBag hs = new OffsetsBag(doc, true);
226: SimpleAttributeSet attribsA = new SimpleAttributeSet();
227: SimpleAttributeSet attribsB = new SimpleAttributeSet();
228:
229: attribsA.addAttribute("set-A", "attribsA");
230: attribsB.addAttribute("set-B", "attribsB");
231:
232: hs.addHighlight(10, 20, attribsA);
233: hs.addHighlight(10, 20, attribsB);
234: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
235:
236: assertEquals("Wrong number of highlights", 2, marks.size());
237: assertEquals("1. highlight - wrong start offset", 10, marks
238: .get(0).getOffset());
239: assertEquals("1. highlight - wrong end offset", 20, marks
240: .get(1).getOffset());
241: assertAttribs("1. highlight - wrong attribs", marks.get(0)
242: .getAttributes(), "set-A", "set-B");
243: assertNull("1. highlight - wrong end", marks.get(1)
244: .getAttributes());
245: }
246:
247: public void testAddBiggerOverlap() {
248: OffsetsBag hs = new OffsetsBag(doc, true);
249: SimpleAttributeSet attribsA = new SimpleAttributeSet();
250: SimpleAttributeSet attribsB = new SimpleAttributeSet();
251:
252: attribsA.addAttribute("set-A", "attribsA");
253: attribsB.addAttribute("set-B", "attribsB");
254:
255: hs.addHighlight(10, 20, attribsA);
256: hs.addHighlight(5, 25, attribsB);
257: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
258:
259: assertEquals("Wrong number of highlights", 4, marks.size());
260: assertEquals("1. highlight - wrong start offset", 5, marks.get(
261: 0).getOffset());
262: assertEquals("1. highlight - wrong end offset", 10, marks
263: .get(1).getOffset());
264: assertAttribs("1. highlight - wrong attribs", marks.get(0)
265: .getAttributes(), "set-B");
266:
267: assertEquals("2. highlight - wrong start offset", 10, marks
268: .get(1).getOffset());
269: assertEquals("2. highlight - wrong end offset", 20, marks
270: .get(2).getOffset());
271: assertAttribs("2. highlight - wrong attribs", marks.get(1)
272: .getAttributes(), "set-A", "set-B");
273:
274: assertEquals("3. highlight - wrong start offset", 20, marks
275: .get(2).getOffset());
276: assertEquals("3. highlight - wrong end offset", 25, marks
277: .get(3).getOffset());
278: assertAttribs("3. highlight - wrong attribs", marks.get(2)
279: .getAttributes(), "set-B");
280: assertNull("3. highlight - wrong end", marks.get(3)
281: .getAttributes());
282: }
283:
284: public void testAddLeftMatchSmallerOverlap() {
285: OffsetsBag hs = new OffsetsBag(doc, true);
286: SimpleAttributeSet attribsA = new SimpleAttributeSet();
287: SimpleAttributeSet attribsB = new SimpleAttributeSet();
288:
289: attribsA.addAttribute("set-A", "attribsA");
290: attribsB.addAttribute("set-B", "attribsB");
291:
292: hs.addHighlight(10, 20, attribsA);
293: hs.addHighlight(10, 15, attribsB);
294: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
295:
296: assertEquals("Wrong number of highlights", 3, marks.size());
297: assertEquals("1. highlight - wrong start offset", 10, marks
298: .get(0).getOffset());
299: assertEquals("1. highlight - wrong end offset", 15, marks
300: .get(1).getOffset());
301: assertAttribs("1. highlight - wrong attribs", marks.get(0)
302: .getAttributes(), "set-A", "set-B");
303:
304: assertEquals("2. highlight - wrong start offset", 15, marks
305: .get(1).getOffset());
306: assertEquals("2. highlight - wrong end offset", 20, marks
307: .get(2).getOffset());
308: assertAttribs("2. highlight - wrong attribs", marks.get(1)
309: .getAttributes(), "set-A");
310: assertNull("2. highlight - wrong end", marks.get(2)
311: .getAttributes());
312: }
313:
314: public void testAddRightMatchSmallerOverlap() {
315: OffsetsBag hs = new OffsetsBag(doc, true);
316: SimpleAttributeSet attribsA = new SimpleAttributeSet();
317: SimpleAttributeSet attribsB = new SimpleAttributeSet();
318:
319: attribsA.addAttribute("set-A", "attribsA");
320: attribsB.addAttribute("set-B", "attribsB");
321:
322: hs.addHighlight(10, 20, attribsA);
323: hs.addHighlight(15, 20, attribsB);
324: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
325:
326: assertEquals("Wrong number of highlights", 3, marks.size());
327: assertEquals("1. highlight - wrong start offset", 10, marks
328: .get(0).getOffset());
329: assertEquals("1. highlight - wrong end offset", 15, marks
330: .get(1).getOffset());
331: assertAttribs("1. highlight - wrong attribs", marks.get(0)
332: .getAttributes(), "set-A");
333:
334: assertEquals("2. highlight - wrong start offset", 15, marks
335: .get(1).getOffset());
336: assertEquals("2. highlight - wrong end offset", 20, marks
337: .get(2).getOffset());
338: assertAttribs("2. highlight - wrong attribs", marks.get(1)
339: .getAttributes(), "set-A", "set-B");
340: assertNull("2. highlight - wrong end", marks.get(2)
341: .getAttributes());
342: }
343:
344: public void testAddSmallerOverlap() {
345: OffsetsBag hs = new OffsetsBag(doc, true);
346: SimpleAttributeSet attribsA = new SimpleAttributeSet();
347: SimpleAttributeSet attribsB = new SimpleAttributeSet();
348:
349: attribsA.addAttribute("set-A", "attribsA");
350: attribsB.addAttribute("set-B", "attribsB");
351:
352: hs.addHighlight(5, 25, attribsA);
353: hs.addHighlight(10, 20, attribsB);
354: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
355:
356: assertEquals("Wrong number of highlights", 4, marks.size());
357: assertEquals("1. highlight - wrong start offset", 5, marks.get(
358: 0).getOffset());
359: assertEquals("1. highlight - wrong end offset", 10, marks
360: .get(1).getOffset());
361: assertAttribs("1. highlight - wrong attribs", marks.get(0)
362: .getAttributes(), "set-A");
363:
364: assertEquals("2. highlight - wrong start offset", 10, marks
365: .get(1).getOffset());
366: assertEquals("2. highlight - wrong end offset", 20, marks
367: .get(2).getOffset());
368: assertAttribs("2. highlight - wrong attribs", marks.get(1)
369: .getAttributes(), "set-A", "set-B");
370:
371: assertEquals("3. highlight - wrong start offset", 20, marks
372: .get(2).getOffset());
373: assertEquals("3. highlight - wrong end offset", 25, marks
374: .get(3).getOffset());
375: assertAttribs("3. highlight - wrong attribs", marks.get(0)
376: .getAttributes(), "set-A");
377: assertNull("3. highlight - wrong end", marks.get(3)
378: .getAttributes());
379: }
380:
381: public void testOrdering() {
382: OffsetsBag hs = new OffsetsBag(doc, true);
383: SimpleAttributeSet attribsA = new SimpleAttributeSet();
384: SimpleAttributeSet attribsB = new SimpleAttributeSet();
385:
386: attribsA.addAttribute("attribute", "value-A");
387: attribsB.addAttribute("attribute", "value-B");
388:
389: hs.addHighlight(5, 15, attribsA);
390: hs.addHighlight(10, 20, attribsB);
391: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
392:
393: assertEquals("Wrong number of highlights", 4, marks.size());
394: assertEquals("1. highlight - wrong attribs", "value-A", marks
395: .get(0).getAttributes().getAttribute("attribute"));
396: assertEquals("2. highlight - wrong attribs", "value-B", marks
397: .get(1).getAttributes().getAttribute("attribute"));
398: assertEquals("3. highlight - wrong attribs", "value-B", marks
399: .get(2).getAttributes().getAttribute("attribute"));
400: assertNull("3. highlight - wrong end", marks.get(3)
401: .getAttributes());
402: }
403:
404: public void testAddMultipleOverlaps() {
405: Object[] src = new Object[] { 5, 10, new String[] { "set-A" },
406: 15, 20, new String[] { "set-B" }, 25, 30,
407: new String[] { "set-C" }, 0, 40,
408: new String[] { "set-D" }, };
409: Object[] trg = new Object[] { 0, 5, new String[] { "set-D" },
410: 5, 10, new String[] { "set-A", "set-D" }, 10, 15,
411: new String[] { "set-D" }, 15, 20,
412: new String[] { "set-B", "set-D" }, 20, 25,
413: new String[] { "set-D" }, 25, 30,
414: new String[] { "set-C", "set-D" }, 30, 40,
415: new String[] { "set-D" }, };
416:
417: checkMerging(src, trg);
418: }
419:
420: public void testAddMultipleOverlaps2() {
421: Object[] src = new Object[] { 2, 47, new String[] { "set-A" },
422: 49, 74, new String[] { "set-B" }, 74, 100,
423: new String[] { "set-C" }, 9, 48,
424: new String[] { "set-D" }, 49, 98,
425: new String[] { "set-E" }, 0, 44,
426: new String[] { "set-F" }, 46, 74,
427: new String[] { "set-G" }, 74, 100,
428: new String[] { "set-H" }, };
429: Object[] trg = new Object[] { 0, 2, new String[] { "set-F" },
430: 2, 9, new String[] { "set-A", "set-F" }, 9, 44,
431: new String[] { "set-A", "set-D", "set-F" }, 44, 46,
432: new String[] { "set-A", "set-D" }, 46, 47,
433: new String[] { "set-A", "set-D", "set-G" }, 47, 48,
434: new String[] { "set-D", "set-G" }, 48, 49,
435: new String[] { "set-G" }, 49, 74,
436: new String[] { "set-B", "set-E", "set-G" }, 74, 98,
437: new String[] { "set-C", "set-E", "set-H" }, 98, 100,
438: new String[] { "set-C", "set-H" }, };
439:
440: checkMerging(src, trg);
441: }
442:
443: public void testAddMultipleOverlaps3() {
444: Object[] src = new Object[] { 5, 13, new String[] { "set-A" },
445: 20, 49, new String[] { "set-B" }, 50, 53,
446: new String[] { "set-C" }, 62, 100,
447: new String[] { "set-D" }, 9, 54,
448: new String[] { "set-E" }, 57, 100,
449: new String[] { "set-F" }, 1, 41,
450: new String[] { "set-G" }, 41, 46,
451: new String[] { "set-H" }, 54, 83,
452: new String[] { "set-I" }, 88, 100,
453: new String[] { "set-J" }, };
454: Object[] trg = new Object[] { 1, 5, new String[] { "set-G" },
455: 5, 9, new String[] { "set-A", "set-G" }, 9, 13,
456: new String[] { "set-A", "set-E", "set-G" }, 13, 20,
457: new String[] { "set-E", "set-G" }, 20, 41,
458: new String[] { "set-B", "set-G", "set-E" }, 41, 46,
459: new String[] { "set-B", "set-E", "set-H" }, 46, 49,
460: new String[] { "set-B", "set-E" }, 49, 50,
461: new String[] { "set-E" }, 50, 53,
462: new String[] { "set-C", "set-E" }, 53, 54,
463: new String[] { "set-E" }, 54, 57,
464: new String[] { "set-I" }, 57, 62,
465: new String[] { "set-F", "set-I" }, 62, 83,
466: new String[] { "set-D", "set-F", "set-I" }, 83, 88,
467: new String[] { "set-D", "set-F" }, 88, 100,
468: new String[] { "set-D", "set-F", "set-J" }, };
469:
470: checkMerging(src, trg);
471: }
472:
473: public void checkMerging(Object[] src, Object[] trg) {
474: OffsetsBag hs = new OffsetsBag(doc, true);
475:
476: for (int i = 0; i < src.length / 3; i++) {
477: SimpleAttributeSet as = new SimpleAttributeSet();
478: String[] keys = (String[]) src[3 * i + 2];
479:
480: for (int j = 0; j < keys.length; j++) {
481: as.addAttribute(keys[j], Boolean.TRUE);
482: }
483:
484: hs.addHighlight(((Integer) src[3 * i + 0]).intValue(),
485: ((Integer) src[3 * i + 1]).intValue(), as);
486: }
487:
488: int lastOffset = Integer.MIN_VALUE;
489: int differentOffsets = 0;
490:
491: for (int i = 0; i < trg.length / 3; i++) {
492: if (lastOffset != ((Integer) trg[3 * i + 0]).intValue()) {
493: differentOffsets++;
494: lastOffset = ((Integer) trg[3 * i + 0]).intValue();
495: }
496: if (lastOffset != ((Integer) trg[3 * i + 1]).intValue()) {
497: differentOffsets++;
498: lastOffset = ((Integer) trg[3 * i + 1]).intValue();
499: }
500: }
501:
502: OffsetGapList<OffsetsBag.Mark> marks = hs.getMarks();
503:
504: try {
505: assertEquals("Wrong number of highlights",
506: differentOffsets, marks.size());
507:
508: int trgIdx = 0;
509: for (int idx = 0; idx < marks.size(); idx++) {
510: if (marks.get(idx).getAttributes() == null) {
511: assertTrue("Mark at index 0 must have attributes",
512: idx > 0);
513: continue;
514: }
515:
516: assertTrue("Too few marks", idx + 1 < marks.size());
517: assertTrue("Too many marks", trgIdx < trg.length);
518:
519: // Compare one pair
520: assertEquals(trgIdx
521: + ". highlight - wrong start offset",
522: ((Integer) trg[3 * trgIdx + 0]).intValue(),
523: marks.get(idx).getOffset());
524: assertEquals(trgIdx + ". highlight - wrong end offset",
525: ((Integer) trg[3 * trgIdx + 1]).intValue(),
526: marks.get(idx + 1).getOffset());
527: assertAttribs(trgIdx + ". highlight - wrong attribs",
528: marks.get(idx).getAttributes(),
529: (String[]) trg[3 * trgIdx + 2]);
530:
531: trgIdx++;
532: }
533:
534: assertTrue("Wrong number of marks: marks.size() = "
535: + marks.size() + ", trg.length = " + trg.length,
536: 3 * trgIdx == trg.length);
537:
538: } catch (AssertionFailedError afe) {
539: dumpMarks(marks);
540:
541: System.out.println("Dump through getHighlights {");
542: HighlightsSequence sequence = hs.getHighlights(
543: Integer.MIN_VALUE, Integer.MAX_VALUE);
544: for (; sequence.moveNext();) {
545: System.out.println(" <" + sequence.getStartOffset()
546: + ", " + sequence.getEndOffset() + ">");
547: }
548: System.out
549: .println("} ---- End of Dump through getHighlights ------------------");
550:
551: throw afe;
552: }
553: }
554:
555: private void assertAttribs(String msg, AttributeSet as,
556: String... keys) {
557: assertEquals(msg, keys.length, as.getAttributeCount());
558: for (String key : keys) {
559: if (null == as.getAttribute(key)) {
560: fail(msg + " attribute key: " + key);
561: }
562: }
563: }
564:
565: private String dumpHighlight(Position start, Position end,
566: AttributeSet attribs) {
567: StringBuilder sb = new StringBuilder();
568:
569: sb.append("<");
570: sb.append(start == null ? " " : start.getOffset());
571: sb.append(",");
572: sb.append(end == null ? " " : end.getOffset());
573: sb.append(",");
574: dumpAttributes(sb, attribs);
575: sb.append(">");
576:
577: return sb.toString();
578: }
579:
580: private String dumpAttributes(StringBuilder sb, AttributeSet attribs) {
581: if (sb == null) {
582: sb = new StringBuilder();
583: }
584:
585: if (attribs == null) {
586: sb.append(" ");
587: } else {
588: Enumeration en = attribs.getAttributeNames();
589: while (en.hasMoreElements()) {
590: Object attrName = en.nextElement();
591: Object attrValue = attribs.getAttribute(attrName);
592:
593: sb.append("'");
594: sb.append(attrName.toString());
595: sb.append("' = '");
596: sb.append(attrValue == null ? "null" : attrValue
597: .toString());
598: sb.append("'");
599: if (en.hasMoreElements()) {
600: sb.append(", ");
601: }
602: }
603: }
604:
605: return sb.toString();
606: }
607:
608: private void dumpMarks(OffsetGapList<OffsetsBag.Mark> marks) {
609: String signature = marks.getClass() + "@"
610: + Integer.toHexString(System.identityHashCode(marks));
611: System.out.println("Dumping marks from " + signature + " {");
612: for (OffsetsBag.Mark mark : marks) {
613: System.out
614: .println("<"
615: + mark.getOffset()
616: + ", ["
617: + dumpAttributes(null, mark.getAttributes())
618: + "]>");
619: }
620: System.out.println("} ---- End of Dumping marks from "
621: + signature + " --------");
622: }
623: }
|