1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49:
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62:
63:
76: public class HTMLDocument extends DefaultStyledDocument
77: {
78:
81: public static final String AdditionalComments = "AdditionalComments";
82: URL baseURL = null;
83: boolean preservesUnknownTags = true;
84: int tokenThreshold = Integer.MAX_VALUE;
85: HTMLEditorKit.Parser parser;
86: StyleSheet styleSheet;
87: AbstractDocument.Content content;
88:
89:
93: public HTMLDocument()
94: {
95: this(null);
96: }
97:
98:
104: public HTMLDocument(StyleSheet styles)
105: {
106: this(new GapContent(BUFFER_SIZE_DEFAULT), styles);
107: }
108:
109:
116: public HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
117: {
118: this.content = c;
119: if (styles == null)
120: {
121: styles = new StyleSheet();
122: styles.importStyleSheet(getClass().getResource(HTMLEditorKit.
123: DEFAULT_CSS));
124: }
125: this.styleSheet = styles;
126: }
127:
128:
134: public StyleSheet getStyleSheet()
135: {
136: return styleSheet;
137: }
138:
139:
144: protected AbstractElement createDefaultRoot()
145: {
146: AbstractDocument.AttributeContext ctx = getAttributeContext();
147:
148:
149: AttributeSet atts = ctx.getEmptySet();
150: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.HTML);
151: BranchElement html = (BranchElement) createBranchElement(null, atts);
152:
153:
154: atts = ctx.getEmptySet();
155: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.BODY);
156: BranchElement body = (BranchElement) createBranchElement(html, atts);
157: html.replace(0, 0, new Element[] { body });
158:
159:
160: atts = ctx.getEmptySet();
161: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.P);
162: BranchElement p = (BranchElement) createBranchElement(body, atts);
163: body.replace(0, 0, new Element[] { p });
164:
165:
166: atts = ctx.getEmptySet();
167: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute,
168: HTML.Tag.CONTENT);
169: Element leaf = createLeafElement(p, atts, 0, 1);
170: p.replace(0, 0, new Element[]{ leaf });
171:
172: return html;
173: }
174:
175:
187: protected Element createLeafElement(Element parent, AttributeSet a, int p0,
188: int p1)
189: {
190: RunElement el = new RunElement(parent, a, p0, p1);
191: el.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
192: return new RunElement(parent, a, p0, p1);
193: }
194:
195:
204: protected Element createBranchElement(Element parent, AttributeSet a)
205: {
206: return new BlockElement(parent, a);
207: }
208:
209:
214: public HTMLEditorKit.Parser getParser()
215: {
216: return parser;
217: }
218:
219:
224: public void setParser (HTMLEditorKit.Parser p)
225: {
226: parser = p;
227: }
228:
234: public void setTokenThreshold (int n)
235: {
236: tokenThreshold = n;
237: }
238:
239:
245: public int getTokenThreshold ()
246: {
247: return tokenThreshold;
248: }
249:
250:
256: public URL getBase()
257: {
258: return baseURL;
259: }
260:
261:
265: public void setBase(URL u)
266: {
267: baseURL = u;
268: styleSheet.setBase(u);
269: }
270:
271:
275: public boolean getPreservesUnknownTags()
276: {
277: return preservesUnknownTags;
278: }
279:
280:
284: public void setPreservesUnknownTags(boolean preservesTags)
285: {
286: preservesUnknownTags = preservesTags;
287: }
288:
289:
292: class LeafIterator extends Iterator
293: {
294: HTML.Tag tag;
295: HTMLDocument doc;
296: ElementIterator it;
297:
298: public LeafIterator (HTML.Tag t, HTMLDocument d)
299: {
300: doc = d;
301: tag = t;
302: it = new ElementIterator(doc);
303: }
304:
305:
309: public AttributeSet getAttributes()
310: {
311: if (it.current() != null)
312: return it.current().getAttributes();
313: return null;
314: }
315:
316:
321: public int getEndOffset()
322: {
323: if (it.current() != null)
324: return it.current().getEndOffset();
325: return -1;
326: }
327:
328:
333:
334: public int getStartOffset()
335: {
336: if (it.current() != null)
337: return it.current().getStartOffset();
338: return -1;
339: }
340:
341:
344: public void next()
345: {
346: it.next();
347: while (it.current()!= null && !it.current().isLeaf())
348: it.next();
349: }
350:
351:
357: public boolean isValid()
358: {
359: return it.current() != null;
360: }
361:
362:
365: public Tag getTag()
366: {
367: return tag;
368: }
369:
370: }
371:
372: public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event)
373: throws NotImplementedException
374: {
375:
376: }
377:
378:
383: public HTMLDocument.Iterator getIterator (HTML.Tag t)
384: {
385: return new HTMLDocument.LeafIterator(t, this);
386: }
387:
388:
391: public abstract static class Iterator
392: {
393:
397: public abstract AttributeSet getAttributes();
398:
399:
404: public abstract int getEndOffset();
405:
406:
411: public abstract int getStartOffset();
412:
413:
416: public abstract void next();
417:
418:
424: public abstract boolean isValid();
425:
426:
430: public abstract HTML.Tag getTag();
431: }
432:
433: public class BlockElement extends AbstractDocument.BranchElement
434: {
435: public BlockElement (Element parent, AttributeSet a)
436: {
437: super(parent, a);
438: }
439:
440:
444: public AttributeSet getResolveParent()
445: {
446: return null;
447: }
448:
449:
454: public String getName()
455: {
456: Object tag = getAttribute(StyleConstants.NameAttribute);
457: String name = null;
458: if (tag != null)
459: name = tag.toString();
460: return name;
461: }
462: }
463:
464:
468: public class RunElement extends AbstractDocument.LeafElement
469: {
470:
471:
480: public RunElement(Element parent, AttributeSet a, int start, int end)
481: {
482: super(parent, a, start, end);
483: }
484:
485:
490: public String getName()
491: {
492: Object tag = getAttribute(StyleConstants.NameAttribute);
493: String name = null;
494: if (tag != null)
495: name = tag.toString();
496: return name;
497: }
498:
499:
505: public AttributeSet getResolveParent()
506: {
507: return null;
508: }
509: }
510:
511:
516: public class HTMLReader extends HTMLEditorKit.ParserCallback
517: {
518:
519: protected MutableAttributeSet charAttr = new SimpleAttributeSet();
520:
521: protected Vector parseBuffer = new Vector();
522:
523:
524: Stack charAttrStack = new Stack();
525:
526:
530: private Stack parseStack = new Stack();
531:
532:
533: HashMap tagToAction;
534:
535:
536: boolean endHTMLEncountered = false;
537:
538:
539: int popDepth, pushDepth, offset;
540: HTML.Tag insertTag;
541: boolean insertTagEncountered = false;
542:
543:
544: boolean debug = false;
545:
546: void print (String line)
547: {
548: if (debug)
549: System.out.println (line);
550: }
551:
552: public class TagAction
553: {
554:
558: public void start(HTML.Tag t, MutableAttributeSet a)
559: {
560:
561: }
562:
563:
567: public void end(HTML.Tag t)
568: {
569:
570: }
571: }
572:
573: public class BlockAction extends TagAction
574: {
575:
579: public void start(HTML.Tag t, MutableAttributeSet a)
580: {
581:
582: blockOpen(t, a);
583: }
584:
585:
589: public void end(HTML.Tag t)
590: {
591:
592: blockClose(t);
593: }
594: }
595:
596: public class CharacterAction extends TagAction
597: {
598:
602: public void start(HTML.Tag t, MutableAttributeSet a)
603: {
604:
605: pushCharacterStyle();
606:
607:
608: if(CharacterAttributeTranslator.translateTag(charAttr, t, a))
609: return;
610:
611:
612: if (a != null)
613: charAttr.addAttribute(t, a.copyAttributes());
614: }
615:
616:
620: public void end(HTML.Tag t)
621: {
622: popCharacterStyle();
623: }
624: }
625:
626: public class FormAction extends SpecialAction
627: {
628:
632: public void start(HTML.Tag t, MutableAttributeSet a)
633: throws NotImplementedException
634: {
635:
636: print ("FormAction.start not implemented");
637: }
638:
639:
643: public void end(HTML.Tag t)
644: throws NotImplementedException
645: {
646:
647: print ("FormAction.end not implemented");
648: }
649: }
650:
651:
660: public class HiddenAction
661: extends TagAction
662: {
663:
667: public void start(HTML.Tag t, MutableAttributeSet a)
668: {
669: blockOpen(t, a);
670: }
671:
672:
676: public void end(HTML.Tag t)
677: {
678: blockClose(t);
679: }
680: }
681:
682: public class IsindexAction extends TagAction
683: {
684:
688: public void start(HTML.Tag t, MutableAttributeSet a)
689: throws NotImplementedException
690: {
691:
692: print ("IsindexAction.start not implemented");
693: }
694: }
695:
696: public class ParagraphAction extends BlockAction
697: {
698:
702: public void start(HTML.Tag t, MutableAttributeSet a)
703: {
704: blockOpen(t, a);
705: }
706:
707:
711: public void end(HTML.Tag t)
712: {
713: blockClose(t);
714: }
715: }
716:
717: public class PreAction extends BlockAction
718: {
719:
723: public void start(HTML.Tag t, MutableAttributeSet a)
724: throws NotImplementedException
725: {
726:
727: print ("PreAction.start not implemented");
728: super.start(t, a);
729: }
730:
731:
735: public void end(HTML.Tag t)
736: throws NotImplementedException
737: {
738:
739: print ("PreAction.end not implemented");
740: super.end(t);
741: }
742: }
743:
744:
751: public class SpecialAction extends TagAction
752: {
753:
756: public void start(HTML.Tag t, MutableAttributeSet a)
757: {
758: addSpecialElement(t, a);
759: }
760: }
761:
762: class AreaAction extends TagAction
763: {
764:
768: public void start(HTML.Tag t, MutableAttributeSet a)
769: throws NotImplementedException
770: {
771:
772: print ("AreaAction.start not implemented");
773: }
774:
775:
779: public void end(HTML.Tag t)
780: throws NotImplementedException
781: {
782:
783: print ("AreaAction.end not implemented");
784: }
785: }
786:
787: class BaseAction extends TagAction
788: {
789:
793: public void start(HTML.Tag t, MutableAttributeSet a)
794: throws NotImplementedException
795: {
796:
797: print ("BaseAction.start not implemented");
798: }
799:
800:
804: public void end(HTML.Tag t)
805: throws NotImplementedException
806: {
807:
808: print ("BaseAction.end not implemented");
809: }
810: }
811:
812: class HeadAction extends BlockAction
813: {
814:
818: public void start(HTML.Tag t, MutableAttributeSet a)
819: throws NotImplementedException
820: {
821:
822: print ("HeadAction.start not implemented: "+t);
823: super.start(t, a);
824: }
825:
826:
830: public void end(HTML.Tag t)
831: throws NotImplementedException
832: {
833:
834: print ("HeadAction.end not implemented: "+t);
835: super.end(t);
836: }
837: }
838:
839: class LinkAction extends TagAction
840: {
841:
845: public void start(HTML.Tag t, MutableAttributeSet a)
846: throws NotImplementedException
847: {
848:
849: print ("LinkAction.start not implemented");
850: }
851:
852:
856: public void end(HTML.Tag t)
857: throws NotImplementedException
858: {
859:
860: print ("LinkAction.end not implemented");
861: }
862: }
863:
864: class MapAction extends TagAction
865: {
866:
870: public void start(HTML.Tag t, MutableAttributeSet a)
871: throws NotImplementedException
872: {
873:
874: print ("MapAction.start not implemented");
875: }
876:
877:
881: public void end(HTML.Tag t)
882: throws NotImplementedException
883: {
884:
885: print ("MapAction.end not implemented");
886: }
887: }
888:
889: class MetaAction extends TagAction
890: {
891:
895: public void start(HTML.Tag t, MutableAttributeSet a)
896: throws NotImplementedException
897: {
898:
899: print ("MetaAction.start not implemented");
900: }
901:
902:
906: public void end(HTML.Tag t)
907: throws NotImplementedException
908: {
909:
910: print ("MetaAction.end not implemented");
911: }
912: }
913:
914: class StyleAction extends TagAction
915: {
916:
920: public void start(HTML.Tag t, MutableAttributeSet a)
921: throws NotImplementedException
922: {
923:
924: print ("StyleAction.start not implemented");
925: }
926:
927:
931: public void end(HTML.Tag t)
932: throws NotImplementedException
933: {
934:
935: print ("StyleAction.end not implemented");
936: }
937: }
938:
939: class TitleAction extends TagAction
940: {
941:
945: public void start(HTML.Tag t, MutableAttributeSet a)
946: throws NotImplementedException
947: {
948:
949: print ("TitleAction.start not implemented");
950: }
951:
952:
956: public void end(HTML.Tag t)
957: throws NotImplementedException
958: {
959:
960: print ("TitleAction.end not implemented");
961: }
962: }
963:
964: public HTMLReader(int offset)
965: {
966: this (offset, 0, 0, null);
967: }
968:
969: public HTMLReader(int offset, int popDepth, int pushDepth,
970: HTML.Tag insertTag)
971: {
972: print ("HTMLReader created with pop: "+popDepth
973: + " push: "+pushDepth + " offset: "+offset
974: + " tag: "+insertTag);
975: this.insertTag = insertTag;
976: this.offset = offset;
977: this.popDepth = popDepth;
978: this.pushDepth = pushDepth;
979: initTags();
980: }
981:
982: void initTags()
983: {
984: tagToAction = new HashMap(72);
985: CharacterAction characterAction = new CharacterAction();
986: HiddenAction hiddenAction = new HiddenAction();
987: AreaAction areaAction = new AreaAction();
988: BaseAction baseAction = new BaseAction();
989: BlockAction blockAction = new BlockAction();
990: SpecialAction specialAction = new SpecialAction();
991: ParagraphAction paragraphAction = new ParagraphAction();
992: HeadAction headAction = new HeadAction();
993: FormAction formAction = new FormAction();
994: IsindexAction isindexAction = new IsindexAction();
995: LinkAction linkAction = new LinkAction();
996: MapAction mapAction = new MapAction();
997: PreAction preAction = new PreAction();
998: MetaAction metaAction = new MetaAction();
999: StyleAction styleAction = new StyleAction();
1000: TitleAction titleAction = new TitleAction();
1001:
1002:
1003: tagToAction.put(HTML.Tag.A, characterAction);
1004: tagToAction.put(HTML.Tag.ADDRESS, characterAction);
1005: tagToAction.put(HTML.Tag.APPLET, hiddenAction);
1006: tagToAction.put(HTML.Tag.AREA, areaAction);
1007: tagToAction.put(HTML.Tag.B, characterAction);
1008: tagToAction.put(HTML.Tag.BASE, baseAction);
1009: tagToAction.put(HTML.Tag.BASEFONT, characterAction);
1010: tagToAction.put(HTML.Tag.BIG, characterAction);
1011: tagToAction.put(HTML.Tag.BLOCKQUOTE, blockAction);
1012: tagToAction.put(HTML.Tag.BODY, blockAction);
1013: tagToAction.put(HTML.Tag.BR, specialAction);
1014: tagToAction.put(HTML.Tag.CAPTION, blockAction);
1015: tagToAction.put(HTML.Tag.CENTER, blockAction);
1016: tagToAction.put(HTML.Tag.CITE, characterAction);
1017: tagToAction.put(HTML.Tag.CODE, characterAction);
1018: tagToAction.put(HTML.Tag.DD, blockAction);
1019: tagToAction.put(HTML.Tag.DFN, characterAction);
1020: tagToAction.put(HTML.Tag.DIR, blockAction);
1021: tagToAction.put(HTML.Tag.DIV, blockAction);
1022: tagToAction.put(HTML.Tag.DL, blockAction);
1023: tagToAction.put(HTML.Tag.DT, paragraphAction);
1024: tagToAction.put(HTML.Tag.EM, characterAction);
1025: tagToAction.put(HTML.Tag.FONT, characterAction);
1026: tagToAction.put(HTML.Tag.FORM, blockAction);
1027: tagToAction.put(HTML.Tag.FRAME, specialAction);
1028: tagToAction.put(HTML.Tag.FRAMESET, blockAction);
1029: tagToAction.put(HTML.Tag.H1, paragraphAction);
1030: tagToAction.put(HTML.Tag.H2, paragraphAction);
1031: tagToAction.put(HTML.Tag.H3, paragraphAction);
1032: tagToAction.put(HTML.Tag.H4, paragraphAction);
1033: tagToAction.put(HTML.Tag.H5, paragraphAction);
1034: tagToAction.put(HTML.Tag.H6, paragraphAction);
1035: tagToAction.put(HTML.Tag.HEAD, headAction);
1036: tagToAction.put(HTML.Tag.HR, specialAction);
1037: tagToAction.put(HTML.Tag.HTML, blockAction);
1038: tagToAction.put(HTML.Tag.I, characterAction);
1039: tagToAction.put(HTML.Tag.IMG, specialAction);
1040: tagToAction.put(HTML.Tag.INPUT, formAction);
1041: tagToAction.put(HTML.Tag.ISINDEX, isindexAction);
1042: tagToAction.put(HTML.Tag.KBD, characterAction);
1043: tagToAction.put(HTML.Tag.LI, blockAction);
1044: tagToAction.put(HTML.Tag.LINK, linkAction);
1045: tagToAction.put(HTML.Tag.MAP, mapAction);
1046: tagToAction.put(HTML.Tag.MENU, blockAction);
1047: tagToAction.put(HTML.Tag.META, metaAction);
1048: tagToAction.put(HTML.Tag.NOFRAMES, blockAction);
1049: tagToAction.put(HTML.Tag.OBJECT, specialAction);
1050: tagToAction.put(HTML.Tag.OL, blockAction);
1051: tagToAction.put(HTML.Tag.OPTION, formAction);
1052: tagToAction.put(HTML.Tag.P, paragraphAction);
1053: tagToAction.put(HTML.Tag.PARAM, hiddenAction);
1054: tagToAction.put(HTML.Tag.PRE, preAction);
1055: tagToAction.put(HTML.Tag.SAMP, characterAction);
1056: tagToAction.put(HTML.Tag.SCRIPT, hiddenAction);
1057: tagToAction.put(HTML.Tag.SELECT, formAction);
1058: tagToAction.put(HTML.Tag.SMALL, characterAction);
1059: tagToAction.put(HTML.Tag.STRIKE, characterAction);
1060: tagToAction.put(HTML.Tag.S, characterAction);
1061: tagToAction.put(HTML.Tag.STRONG, characterAction);
1062: tagToAction.put(HTML.Tag.STYLE, styleAction);
1063: tagToAction.put(HTML.Tag.SUB, characterAction);
1064: tagToAction.put(HTML.Tag.SUP, characterAction);
1065: tagToAction.put(HTML.Tag.TABLE, blockAction);
1066: tagToAction.put(HTML.Tag.TD, blockAction);
1067: tagToAction.put(HTML.Tag.TEXTAREA, formAction);
1068: tagToAction.put(HTML.Tag.TH, blockAction);
1069: tagToAction.put(HTML.Tag.TITLE, titleAction);
1070: tagToAction.put(HTML.Tag.TR, blockAction);
1071: tagToAction.put(HTML.Tag.TT, characterAction);
1072: tagToAction.put(HTML.Tag.U, characterAction);
1073: tagToAction.put(HTML.Tag.UL, blockAction);
1074: tagToAction.put(HTML.Tag.VAR, characterAction);
1075: }
1076:
1077:
1081: protected void pushCharacterStyle()
1082: {
1083: charAttrStack.push(charAttr.copyAttributes());
1084: }
1085:
1086:
1091: protected void popCharacterStyle()
1092: {
1093: if (!charAttrStack.isEmpty())
1094: charAttr = (MutableAttributeSet) charAttrStack.pop();
1095: }
1096:
1097:
1105: protected void registerTag(HTML.Tag t, HTMLDocument.HTMLReader.TagAction a)
1106: {
1107: tagToAction.put (t, a);
1108: }
1109:
1110:
1114: public void flush() throws BadLocationException
1115: {
1116: DefaultStyledDocument.ElementSpec[] elements;
1117: elements = new DefaultStyledDocument.ElementSpec[parseBuffer.size()];
1118: parseBuffer.copyInto(elements);
1119: parseBuffer.removeAllElements();
1120: if (offset == 0)
1121: create(elements);
1122: else
1123: insert(offset, elements);
1124:
1125: offset += HTMLDocument.this.getLength() - offset;
1126: }
1127:
1128:
1135: public void handleText(char[] data, int pos)
1136: {
1137: if (data != null && data.length > 0)
1138: addContent(data, 0, data.length);
1139: }
1140:
1141:
1149: public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
1150: {
1151:
1152: if (endHTMLEncountered)
1153: return;
1154:
1155: TagAction action = (TagAction) tagToAction.get(t);
1156: if (action != null)
1157: action.start(t, a);
1158: }
1159:
1160:
1166: public void handleComment(char[] data, int pos)
1167: {
1168:
1169: if (endHTMLEncountered)
1170: return;
1171:
1172: TagAction action = (TagAction) tagToAction.get(HTML.Tag.COMMENT);
1173: if (action != null)
1174: {
1175: action.start(HTML.Tag.COMMENT, new SimpleAttributeSet());
1176: action.end (HTML.Tag.COMMENT);
1177: }
1178: }
1179:
1180:
1187: public void handleEndTag(HTML.Tag t, int pos)
1188: {
1189:
1190: if (endHTMLEncountered)
1191: return;
1192:
1193:
1194: if (t == HTML.Tag.HTML)
1195: endHTMLEncountered = true;
1196:
1197: TagAction action = (TagAction) tagToAction.get(t);
1198: if (action != null)
1199: action.end(t);
1200: }
1201:
1202:
1210: public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
1211: {
1212:
1213: if (endHTMLEncountered)
1214: return;
1215:
1216: TagAction action = (TagAction) tagToAction.get (t);
1217: if (action != null)
1218: {
1219: action.start(t, a);
1220: action.end(t);
1221: }
1222: }
1223:
1224:
1232: public void handleEndOfLineString(String eol)
1233: throws NotImplementedException
1234: {
1235:
1236: print ("HTMLReader.handleEndOfLineString not implemented yet");
1237: }
1238:
1239:
1245: protected void textAreaContent(char[] data)
1246: throws NotImplementedException
1247: {
1248:
1249: print ("HTMLReader.textAreaContent not implemented yet");
1250: }
1251:
1252:
1257: protected void preContent(char[] data)
1258: throws NotImplementedException
1259: {
1260:
1261: print ("HTMLReader.preContent not implemented yet");
1262: }
1263:
1264:
1271: protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
1272: {
1273: printBuffer();
1274: DefaultStyledDocument.ElementSpec element;
1275:
1276:
1277:
1278: if (parseStack.size() > 0 && parseStack.peek() == HTML.Tag.IMPLIED)
1279: {
1280: element = new DefaultStyledDocument.ElementSpec(null,
1281: DefaultStyledDocument.ElementSpec.EndTagType);
1282: parseBuffer.addElement(element);
1283: parseStack.pop();
1284: }
1285:
1286: parseStack.push(t);
1287: AbstractDocument.AttributeContext ctx = getAttributeContext();
1288: AttributeSet copy = attr.copyAttributes();
1289: copy = ctx.addAttribute(copy, StyleConstants.NameAttribute, t);
1290: element = new DefaultStyledDocument.ElementSpec(copy,
1291: DefaultStyledDocument.ElementSpec.StartTagType);
1292: parseBuffer.addElement(element);
1293: printBuffer();
1294: }
1295:
1296:
1302: protected void blockClose(HTML.Tag t)
1303: {
1304: printBuffer();
1305: DefaultStyledDocument.ElementSpec element;
1306:
1307:
1308:
1309: DefaultStyledDocument.ElementSpec prev;
1310: prev = (DefaultStyledDocument.ElementSpec)
1311: parseBuffer.get(parseBuffer.size() - 1);
1312: if (prev.getType() == DefaultStyledDocument.ElementSpec.StartTagType)
1313: {
1314: AbstractDocument.AttributeContext ctx = getAttributeContext();
1315: AttributeSet attributes = ctx.getEmptySet();
1316: attributes = ctx.addAttribute(attributes, StyleConstants.NameAttribute,
1317: HTML.Tag.CONTENT);
1318: element = new DefaultStyledDocument.ElementSpec(attributes,
1319: DefaultStyledDocument.ElementSpec.ContentType,
1320: new char[0], 0, 0);
1321: parseBuffer.add(element);
1322: }
1323:
1324:
1325: else if (parseStack.peek() == HTML.Tag.IMPLIED)
1326: {
1327: element = new DefaultStyledDocument.ElementSpec(null,
1328: DefaultStyledDocument.ElementSpec.EndTagType);
1329: parseBuffer.addElement(element);
1330: if (parseStack.size() > 0)
1331: parseStack.pop();
1332: }
1333:
1334: element = new DefaultStyledDocument.ElementSpec(null,
1335: DefaultStyledDocument.ElementSpec.EndTagType);
1336: parseBuffer.addElement(element);
1337: printBuffer();
1338: if (parseStack.size() > 0)
1339: parseStack.pop();
1340: }
1341:
1342:
1350: protected void addContent(char[] data, int offs, int length)
1351: {
1352: addContent(data, offs, length, true);
1353: }
1354:
1355:
1365: protected void addContent(char[] data, int offs, int length,
1366: boolean generateImpliedPIfNecessary)
1367: {
1368: AbstractDocument.AttributeContext ctx = getAttributeContext();
1369: DefaultStyledDocument.ElementSpec element;
1370: AttributeSet attributes = null;
1371:
1372:
1373:
1374:
1375: boolean createImpliedParagraph = false;
1376: HTML.Tag parent = (HTML.Tag) parseStack.peek();
1377: if (parent != HTML.Tag.P && parent != HTML.Tag.H1
1378: && parent != HTML.Tag.H2
1379: && parent != HTML.Tag.H3 && parent != HTML.Tag.H4
1380: && parent != HTML.Tag.H5 && parent != HTML.Tag.H6
1381: && parent != HTML.Tag.TD)
1382: {
1383: attributes = ctx.getEmptySet();
1384: attributes = ctx.addAttribute(attributes,
1385: StyleConstants.NameAttribute,
1386: HTML.Tag.IMPLIED);
1387: element = new DefaultStyledDocument.ElementSpec(attributes,
1388: DefaultStyledDocument.ElementSpec.StartTagType);
1389: parseBuffer.add(element);
1390: parseStack.push(HTML.Tag.IMPLIED);
1391: }
1392:
1393:
1394:
1395: if (charAttr != null)
1396: attributes = charAttr.copyAttributes();
1397: else
1398: attributes = ctx.getEmptySet();
1399: attributes = ctx.addAttribute(attributes, StyleConstants.NameAttribute,
1400: HTML.Tag.CONTENT);
1401: element = new DefaultStyledDocument.ElementSpec(attributes,
1402: DefaultStyledDocument.ElementSpec.ContentType,
1403: data, offs, length);
1404:
1405: printBuffer();
1406:
1407: parseBuffer.addElement(element);
1408: printBuffer();
1409:
1410: if (parseBuffer.size() > HTMLDocument.this.getTokenThreshold())
1411: {
1412: try
1413: {
1414: flush();
1415: }
1416: catch (BadLocationException ble)
1417: {
1418:
1419: }
1420: }
1421: }
1422:
1423:
1429: protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a)
1430: {
1431: a.addAttribute(StyleConstants.NameAttribute, t);
1432:
1433:
1434:
1435: AttributeSet copy = a.copyAttributes();
1436:
1437:
1438:
1439:
1440: DefaultStyledDocument.ElementSpec spec;
1441: spec = new DefaultStyledDocument.ElementSpec(copy,
1442: DefaultStyledDocument.ElementSpec.ContentType,
1443: new char[] {' '}, 0, 1 );
1444: parseBuffer.add(spec);
1445: }
1446:
1447: void printBuffer()
1448: {
1449: print ("\n*********BUFFER**********");
1450: for (int i = 0; i < parseBuffer.size(); i ++)
1451: print (" "+parseBuffer.get(i));
1452: print ("***************************");
1453: }
1454: }
1455:
1456:
1462: public HTMLEditorKit.ParserCallback getReader(int pos)
1463: {
1464: return new HTMLReader(pos);
1465: }
1466:
1467:
1478: public HTMLEditorKit.ParserCallback getReader(int pos,
1479: int popDepth,
1480: int pushDepth,
1481: HTML.Tag insertTag)
1482: {
1483: return new HTMLReader(pos, popDepth, pushDepth, insertTag);
1484: }
1485:
1486:
1496: public Element getElement(Element e, Object attribute, Object value)
1497: {
1498: if (e != null)
1499: {
1500: if (e.getAttributes().containsAttribute(attribute, value))
1501: return e;
1502:
1503: int count = e.getElementCount();
1504: for (int j = 0; j < count; j++)
1505: {
1506: Element child = e.getElement(j);
1507: if (child.getAttributes().containsAttribute(attribute, value))
1508: return child;
1509:
1510: Element grandChild = getElement(child, attribute, value);
1511: if (grandChild != null)
1512: return grandChild;
1513: }
1514: }
1515: return null;
1516: }
1517:
1518:
1526: public Element getElement(String attrId)
1527: {
1528: return getElement(getDefaultRootElement(), HTML.getAttributeKey(attrId),
1529: attrId);
1530: }
1531:
1532:
1544: public void setInnerHTML(Element elem, String htmlText)
1545: throws BadLocationException, IOException, NotImplementedException
1546: {
1547: if (elem.isLeaf())
1548: throw new IllegalArgumentException("Element is a leaf");
1549: if (parser == null)
1550: throw new IllegalStateException("Parser has not been set");
1551:
1552: System.out.println("setInnerHTML not implemented");
1553: }
1554:
1555:
1568: public void setOuterHTML(Element elem, String htmlText)
1569: throws BadLocationException, IOException, NotImplementedException
1570: {
1571: if (parser == null)
1572: throw new IllegalStateException("Parser has not been set");
1573:
1574: System.out.println("setOuterHTML not implemented");
1575: }
1576:
1577:
1587: public void insertBeforeStart(Element elem, String htmlText)
1588: throws BadLocationException, IOException, NotImplementedException
1589: {
1590: if (parser == null)
1591: throw new IllegalStateException("Parser has not been set");
1592:
1593: System.out.println("insertBeforeStart not implemented");
1594: }
1595:
1596:
1607: public void insertBeforeEnd(Element elem, String htmlText)
1608: throws BadLocationException, IOException, NotImplementedException
1609: {
1610: if (parser == null)
1611: throw new IllegalStateException("Parser has not been set");
1612:
1613: System.out.println("insertBeforeEnd not implemented");
1614: }
1615:
1616:
1626: public void insertAfterEnd(Element elem, String htmlText)
1627: throws BadLocationException, IOException, NotImplementedException
1628: {
1629: if (parser == null)
1630: throw new IllegalStateException("Parser has not been set");
1631:
1632: System.out.println("insertAfterEnd not implemented");
1633: }
1634:
1635:
1645: public void insertAfterStart(Element elem, String htmlText)
1646: throws BadLocationException, IOException, NotImplementedException
1647: {
1648: if (parser == null)
1649: throw new IllegalStateException("Parser has not been set");
1650:
1651: System.out.println("insertAfterStart not implemented");
1652: }
1653: }