1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: import ;
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:
70: public class HTMLDocument extends DefaultStyledDocument
71: {
72:
75: public static final String AdditionalComments = "AdditionalComments";
76: URL baseURL = null;
77: boolean preservesUnknownTags = true;
78: int tokenThreshold = Integer.MAX_VALUE;
79: HTMLEditorKit.Parser parser;
80: StyleSheet styleSheet;
81: AbstractDocument.Content content;
82:
83:
87: public HTMLDocument()
88: {
89: this(null);
90: }
91:
92:
98: public HTMLDocument(StyleSheet styles)
99: {
100: this(new GapContent(BUFFER_SIZE_DEFAULT), styles);
101: }
102:
103:
110: public HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
111: {
112: this.content = c;
113: if (styles == null)
114: {
115: styles = new StyleSheet();
116: styles.importStyleSheet(getClass().getResource(HTMLEditorKit.
117: DEFAULT_CSS));
118: }
119: this.styleSheet = styles;
120: }
121:
122:
128: public StyleSheet getStyleSheet()
129: {
130: return styleSheet;
131: }
132:
133:
141: protected void create(DefaultStyledDocument.ElementSpec[] data)
142: {
143:
144:
145: super.create(data);
146: }
147:
148:
153: protected AbstractElement createDefaultRoot()
154: {
155: AbstractDocument.AttributeContext ctx = getAttributeContext();
156:
157:
158: AttributeSet atts = ctx.getEmptySet();
159: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.HTML);
160: BranchElement html = (BranchElement) createBranchElement(null, atts);
161:
162:
163: atts = ctx.getEmptySet();
164: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.BODY);
165: BranchElement body = (BranchElement) createBranchElement(html, atts);
166: html.replace(0, 0, new Element[] { body });
167:
168:
169: atts = ctx.getEmptySet();
170: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.P);
171: BranchElement p = (BranchElement) createBranchElement(body, atts);
172: body.replace(0, 0, new Element[] { p });
173:
174:
175: atts = ctx.getEmptySet();
176: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute,
177: HTML.Tag.CONTENT);
178: Element leaf = createLeafElement(p, atts, 0, 1);
179: p.replace(0, 0, new Element[]{ leaf });
180:
181: return html;
182: }
183:
184:
196: protected Element createLeafElement(Element parent, AttributeSet a, int p0,
197: int p1)
198: {
199: RunElement el = new RunElement(parent, a, p0, p1);
200: el.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
201: return new RunElement(parent, a, p0, p1);
202: }
203:
204:
213: protected Element createBranchElement(Element parent, AttributeSet a)
214: {
215: return new BlockElement(parent, a);
216: }
217:
218:
231: protected void insert(int offset, DefaultStyledDocument.ElementSpec[] data)
232: throws BadLocationException
233: {
234: super.insert(offset, data);
235: }
236:
237:
245: protected void insertUpdate(AbstractDocument.DefaultDocumentEvent chng,
246: AttributeSet attr)
247: {
248:
249: System.out.println("insertUpdate not implemented");
250: super.insertUpdate(chng, attr);
251: }
252:
253:
258: public HTMLEditorKit.Parser getParser()
259: {
260: return parser;
261: }
262:
263:
268: public void setParser (HTMLEditorKit.Parser p)
269: {
270: parser = p;
271: }
272:
278: public void setTokenThreshold (int n)
279: {
280: tokenThreshold = n;
281: }
282:
283:
289: public int getTokenThreshold ()
290: {
291: return tokenThreshold;
292: }
293:
294:
300: public URL getBase()
301: {
302: return baseURL;
303: }
304:
305:
309: public void setBase(URL u)
310: {
311: baseURL = u;
312: styleSheet.setBase(u);
313: }
314:
315:
319: public boolean getPreservesUnknownTags()
320: {
321: return preservesUnknownTags;
322: }
323:
324:
328: public void setPreservesUnknownTags(boolean preservesTags)
329: {
330: preservesUnknownTags = preservesTags;
331: }
332:
333:
336: class LeafIterator extends Iterator
337: {
338: HTML.Tag tag;
339: HTMLDocument doc;
340: ElementIterator it;
341:
342: public LeafIterator (HTML.Tag t, HTMLDocument d)
343: {
344: doc = d;
345: tag = t;
346: it = new ElementIterator(doc);
347: }
348:
349:
353: public AttributeSet getAttributes()
354: {
355: if (it.current() != null)
356: return it.current().getAttributes();
357: return null;
358: }
359:
360:
365: public int getEndOffset()
366: {
367: if (it.current() != null)
368: return it.current().getEndOffset();
369: return -1;
370: }
371:
372:
377:
378: public int getStartOffset()
379: {
380: if (it.current() != null)
381: return it.current().getStartOffset();
382: return -1;
383: }
384:
385:
388: public void next()
389: {
390: it.next();
391: while (it.current()!= null && !it.current().isLeaf())
392: it.next();
393: }
394:
395:
401: public boolean isValid()
402: {
403: return it.current() != null;
404: }
405:
406:
409: public Tag getTag()
410: {
411: return tag;
412: }
413:
414: }
415:
416: public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event)
417: {
418:
419: }
420:
421:
426: public HTMLDocument.Iterator getIterator (HTML.Tag t)
427: {
428: return new HTMLDocument.LeafIterator(t, this);
429: }
430:
431:
434: public abstract static class Iterator
435: {
436:
440: public abstract AttributeSet getAttributes();
441:
442:
447: public abstract int getEndOffset();
448:
449:
454: public abstract int getStartOffset();
455:
456:
459: public abstract void next();
460:
461:
467: public abstract boolean isValid();
468:
469:
473: public abstract HTML.Tag getTag();
474: }
475:
476: public class BlockElement extends AbstractDocument.BranchElement
477: {
478: public BlockElement (Element parent, AttributeSet a)
479: {
480: super(parent, a);
481: }
482:
483:
487: public AttributeSet getResolveParent()
488: {
489: return null;
490: }
491:
492:
497: public String getName()
498: {
499: Object tag = getAttribute(StyleConstants.NameAttribute);
500: String name = null;
501: if (tag != null)
502: name = tag.toString();
503: return name;
504: }
505: }
506:
507:
511: public class RunElement extends AbstractDocument.LeafElement
512: {
513:
514:
523: public RunElement(Element parent, AttributeSet a, int start, int end)
524: {
525: super(parent, a, start, end);
526: }
527:
528:
533: public String getName()
534: {
535: Object tag = getAttribute(StyleConstants.NameAttribute);
536: String name = null;
537: if (tag != null)
538: name = tag.toString();
539: return name;
540: }
541:
542:
548: public AttributeSet getResolveParent()
549: {
550: return null;
551: }
552: }
553:
554:
559: public class HTMLReader extends HTMLEditorKit.ParserCallback
560: {
561:
562: protected MutableAttributeSet charAttr = new SimpleAttributeSet();
563:
564: protected Vector parseBuffer = new Vector();
565:
566:
567: Stack charAttrStack = new Stack();
568:
569:
573: private Stack parseStack = new Stack();
574:
575:
576: HashMap tagToAction;
577:
578:
579: boolean endHTMLEncountered = false;
580:
581:
582: int popDepth, pushDepth, offset;
583: HTML.Tag insertTag;
584: boolean insertTagEncountered = false;
585:
586:
587: boolean debug = false;
588:
589: void print (String line)
590: {
591: if (debug)
592: System.out.println (line);
593: }
594:
595: public class TagAction
596: {
597:
601: public void start(HTML.Tag t, MutableAttributeSet a)
602: {
603:
604: }
605:
606:
610: public void end(HTML.Tag t)
611: {
612:
613: }
614: }
615:
616: public class BlockAction extends TagAction
617: {
618:
622: public void start(HTML.Tag t, MutableAttributeSet a)
623: {
624:
625: blockOpen(t, a);
626: }
627:
628:
632: public void end(HTML.Tag t)
633: {
634:
635: blockClose(t);
636: }
637: }
638:
639: public class CharacterAction extends TagAction
640: {
641:
645: public void start(HTML.Tag t, MutableAttributeSet a)
646: {
647:
648: pushCharacterStyle();
649:
650:
651: if (a != null)
652: charAttr.addAttribute(t, a.copyAttributes());
653: }
654:
655:
659: public void end(HTML.Tag t)
660: {
661: popCharacterStyle();
662: }
663: }
664:
665: public class FormAction extends SpecialAction
666: {
667:
671: public void start(HTML.Tag t, MutableAttributeSet a)
672: throws NotImplementedException
673: {
674:
675: print ("FormAction.start not implemented");
676: }
677:
678:
682: public void end(HTML.Tag t)
683: throws NotImplementedException
684: {
685:
686: print ("FormAction.end not implemented");
687: }
688: }
689:
690: public class HiddenAction extends TagAction
691: {
692:
696: public void start(HTML.Tag t, MutableAttributeSet a)
697: throws NotImplementedException
698: {
699:
700: print ("HiddenAction.start not implemented");
701: }
702:
703:
707: public void end(HTML.Tag t)
708: throws NotImplementedException
709: {
710:
711: print ("HiddenAction.end not implemented");
712: }
713: }
714:
715: public class IsindexAction extends TagAction
716: {
717:
721: public void start(HTML.Tag t, MutableAttributeSet a)
722: throws NotImplementedException
723: {
724:
725: print ("IsindexAction.start not implemented");
726: }
727:
728:
732: public void end(HTML.Tag t)
733: throws NotImplementedException
734: {
735:
736: print ("IsindexAction.end not implemented");
737: }
738: }
739:
740: public class ParagraphAction extends BlockAction
741: {
742:
746: public void start(HTML.Tag t, MutableAttributeSet a)
747: {
748:
749: blockOpen(t, a);
750: }
751:
752:
756: public void end(HTML.Tag t)
757: {
758:
759: blockClose(t);
760: }
761: }
762:
763: public class PreAction extends BlockAction
764: {
765:
769: public void start(HTML.Tag t, MutableAttributeSet a)
770: throws NotImplementedException
771: {
772:
773: print ("PreAction.start not implemented");
774: }
775:
776:
780: public void end(HTML.Tag t)
781: throws NotImplementedException
782: {
783:
784: print ("PreAction.end not implemented");
785: }
786: }
787:
788: public class SpecialAction extends TagAction
789: {
790:
794: public void start(HTML.Tag t, MutableAttributeSet a)
795: throws NotImplementedException
796: {
797:
798: print ("SpecialAction.start not implemented");
799: }
800:
801:
805: public void end(HTML.Tag t)
806: throws NotImplementedException
807: {
808:
809: print ("SpecialAction.end not implemented");
810: }
811: }
812:
813: class AreaAction extends TagAction
814: {
815:
819: public void start(HTML.Tag t, MutableAttributeSet a)
820: throws NotImplementedException
821: {
822:
823: print ("AreaAction.start not implemented");
824: }
825:
826:
830: public void end(HTML.Tag t)
831: throws NotImplementedException
832: {
833:
834: print ("AreaAction.end not implemented");
835: }
836: }
837:
838: class BaseAction extends TagAction
839: {
840:
844: public void start(HTML.Tag t, MutableAttributeSet a)
845: throws NotImplementedException
846: {
847:
848: print ("BaseAction.start not implemented");
849: }
850:
851:
855: public void end(HTML.Tag t)
856: throws NotImplementedException
857: {
858:
859: print ("BaseAction.end not implemented");
860: }
861: }
862:
863: class HeadAction extends BlockAction
864: {
865:
869: public void start(HTML.Tag t, MutableAttributeSet a)
870: throws NotImplementedException
871: {
872:
873: print ("HeadAction.start not implemented: "+t);
874: super.start(t, a);
875: }
876:
877:
881: public void end(HTML.Tag t)
882: throws NotImplementedException
883: {
884:
885: print ("HeadAction.end not implemented: "+t);
886: super.end(t);
887: }
888: }
889:
890: class LinkAction extends TagAction
891: {
892:
896: public void start(HTML.Tag t, MutableAttributeSet a)
897: throws NotImplementedException
898: {
899:
900: print ("LinkAction.start not implemented");
901: }
902:
903:
907: public void end(HTML.Tag t)
908: throws NotImplementedException
909: {
910:
911: print ("LinkAction.end not implemented");
912: }
913: }
914:
915: class MapAction extends TagAction
916: {
917:
921: public void start(HTML.Tag t, MutableAttributeSet a)
922: throws NotImplementedException
923: {
924:
925: print ("MapAction.start not implemented");
926: }
927:
928:
932: public void end(HTML.Tag t)
933: throws NotImplementedException
934: {
935:
936: print ("MapAction.end not implemented");
937: }
938: }
939:
940: class MetaAction extends TagAction
941: {
942:
946: public void start(HTML.Tag t, MutableAttributeSet a)
947: throws NotImplementedException
948: {
949:
950: print ("MetaAction.start not implemented");
951: }
952:
953:
957: public void end(HTML.Tag t)
958: throws NotImplementedException
959: {
960:
961: print ("MetaAction.end not implemented");
962: }
963: }
964:
965: class StyleAction extends TagAction
966: {
967:
971: public void start(HTML.Tag t, MutableAttributeSet a)
972: throws NotImplementedException
973: {
974:
975: print ("StyleAction.start not implemented");
976: }
977:
978:
982: public void end(HTML.Tag t)
983: throws NotImplementedException
984: {
985:
986: print ("StyleAction.end not implemented");
987: }
988: }
989:
990: class TitleAction extends TagAction
991: {
992:
996: public void start(HTML.Tag t, MutableAttributeSet a)
997: throws NotImplementedException
998: {
999:
1000: print ("TitleAction.start not implemented");
1001: }
1002:
1003:
1007: public void end(HTML.Tag t)
1008: throws NotImplementedException
1009: {
1010:
1011: print ("TitleAction.end not implemented");
1012: }
1013: }
1014:
1015: public HTMLReader(int offset)
1016: {
1017: this (offset, 0, 0, null);
1018: }
1019:
1020: public HTMLReader(int offset, int popDepth, int pushDepth,
1021: HTML.Tag insertTag)
1022: {
1023: print ("HTMLReader created with pop: "+popDepth
1024: + " push: "+pushDepth + " offset: "+offset
1025: + " tag: "+insertTag);
1026: this.insertTag = insertTag;
1027: this.offset = offset;
1028: this.popDepth = popDepth;
1029: this.pushDepth = pushDepth;
1030: initTags();
1031: }
1032:
1033: void initTags()
1034: {
1035: tagToAction = new HashMap(72);
1036: CharacterAction characterAction = new CharacterAction();
1037: HiddenAction hiddenAction = new HiddenAction();
1038: AreaAction areaAction = new AreaAction();
1039: BaseAction baseAction = new BaseAction();
1040: BlockAction blockAction = new BlockAction();
1041: SpecialAction specialAction = new SpecialAction();
1042: ParagraphAction paragraphAction = new ParagraphAction();
1043: HeadAction headAction = new HeadAction();
1044: FormAction formAction = new FormAction();
1045: IsindexAction isindexAction = new IsindexAction();
1046: LinkAction linkAction = new LinkAction();
1047: MapAction mapAction = new MapAction();
1048: PreAction preAction = new PreAction();
1049: MetaAction metaAction = new MetaAction();
1050: StyleAction styleAction = new StyleAction();
1051: TitleAction titleAction = new TitleAction();
1052:
1053:
1054: tagToAction.put(HTML.Tag.A, characterAction);
1055: tagToAction.put(HTML.Tag.ADDRESS, characterAction);
1056: tagToAction.put(HTML.Tag.APPLET, hiddenAction);
1057: tagToAction.put(HTML.Tag.AREA, areaAction);
1058: tagToAction.put(HTML.Tag.B, characterAction);
1059: tagToAction.put(HTML.Tag.BASE, baseAction);
1060: tagToAction.put(HTML.Tag.BASEFONT, characterAction);
1061: tagToAction.put(HTML.Tag.BIG, characterAction);
1062: tagToAction.put(HTML.Tag.BLOCKQUOTE, blockAction);
1063: tagToAction.put(HTML.Tag.BODY, blockAction);
1064: tagToAction.put(HTML.Tag.BR, specialAction);
1065: tagToAction.put(HTML.Tag.CAPTION, blockAction);
1066: tagToAction.put(HTML.Tag.CENTER, blockAction);
1067: tagToAction.put(HTML.Tag.CITE, characterAction);
1068: tagToAction.put(HTML.Tag.CODE, characterAction);
1069: tagToAction.put(HTML.Tag.DD, blockAction);
1070: tagToAction.put(HTML.Tag.DFN, characterAction);
1071: tagToAction.put(HTML.Tag.DIR, blockAction);
1072: tagToAction.put(HTML.Tag.DIV, blockAction);
1073: tagToAction.put(HTML.Tag.DL, blockAction);
1074: tagToAction.put(HTML.Tag.DT, paragraphAction);
1075: tagToAction.put(HTML.Tag.EM, characterAction);
1076: tagToAction.put(HTML.Tag.FONT, characterAction);
1077: tagToAction.put(HTML.Tag.FORM, blockAction);
1078: tagToAction.put(HTML.Tag.FRAME, specialAction);
1079: tagToAction.put(HTML.Tag.FRAMESET, blockAction);
1080: tagToAction.put(HTML.Tag.H1, paragraphAction);
1081: tagToAction.put(HTML.Tag.H2, paragraphAction);
1082: tagToAction.put(HTML.Tag.H3, paragraphAction);
1083: tagToAction.put(HTML.Tag.H4, paragraphAction);
1084: tagToAction.put(HTML.Tag.H5, paragraphAction);
1085: tagToAction.put(HTML.Tag.H6, paragraphAction);
1086: tagToAction.put(HTML.Tag.HEAD, headAction);
1087: tagToAction.put(HTML.Tag.HR, specialAction);
1088: tagToAction.put(HTML.Tag.HTML, blockAction);
1089: tagToAction.put(HTML.Tag.I, characterAction);
1090: tagToAction.put(HTML.Tag.IMG, specialAction);
1091: tagToAction.put(HTML.Tag.INPUT, formAction);
1092: tagToAction.put(HTML.Tag.ISINDEX, isindexAction);
1093: tagToAction.put(HTML.Tag.KBD, characterAction);
1094: tagToAction.put(HTML.Tag.LI, blockAction);
1095: tagToAction.put(HTML.Tag.LINK, linkAction);
1096: tagToAction.put(HTML.Tag.MAP, mapAction);
1097: tagToAction.put(HTML.Tag.MENU, blockAction);
1098: tagToAction.put(HTML.Tag.META, metaAction);
1099: tagToAction.put(HTML.Tag.NOFRAMES, blockAction);
1100: tagToAction.put(HTML.Tag.OBJECT, specialAction);
1101: tagToAction.put(HTML.Tag.OL, blockAction);
1102: tagToAction.put(HTML.Tag.OPTION, formAction);
1103: tagToAction.put(HTML.Tag.P, paragraphAction);
1104: tagToAction.put(HTML.Tag.PARAM, hiddenAction);
1105: tagToAction.put(HTML.Tag.PRE, preAction);
1106: tagToAction.put(HTML.Tag.SAMP, characterAction);
1107: tagToAction.put(HTML.Tag.SCRIPT, hiddenAction);
1108: tagToAction.put(HTML.Tag.SELECT, formAction);
1109: tagToAction.put(HTML.Tag.SMALL, characterAction);
1110: tagToAction.put(HTML.Tag.STRIKE, characterAction);
1111: tagToAction.put(HTML.Tag.S, characterAction);
1112: tagToAction.put(HTML.Tag.STRONG, characterAction);
1113: tagToAction.put(HTML.Tag.STYLE, styleAction);
1114: tagToAction.put(HTML.Tag.SUB, characterAction);
1115: tagToAction.put(HTML.Tag.SUP, characterAction);
1116: tagToAction.put(HTML.Tag.TABLE, blockAction);
1117: tagToAction.put(HTML.Tag.TD, blockAction);
1118: tagToAction.put(HTML.Tag.TEXTAREA, formAction);
1119: tagToAction.put(HTML.Tag.TH, blockAction);
1120: tagToAction.put(HTML.Tag.TITLE, titleAction);
1121: tagToAction.put(HTML.Tag.TR, blockAction);
1122: tagToAction.put(HTML.Tag.TT, characterAction);
1123: tagToAction.put(HTML.Tag.U, characterAction);
1124: tagToAction.put(HTML.Tag.UL, blockAction);
1125: tagToAction.put(HTML.Tag.VAR, characterAction);
1126: }
1127:
1128:
1132: protected void pushCharacterStyle()
1133: {
1134: charAttrStack.push(charAttr);
1135: }
1136:
1137:
1142: protected void popCharacterStyle()
1143: {
1144: if (!charAttrStack.isEmpty())
1145: charAttr = (MutableAttributeSet) charAttrStack.pop();
1146: }
1147:
1148:
1156: protected void registerTag(HTML.Tag t, HTMLDocument.HTMLReader.TagAction a)
1157: {
1158: tagToAction.put (t, a);
1159: }
1160:
1161:
1165: public void flush() throws BadLocationException
1166: {
1167: DefaultStyledDocument.ElementSpec[] elements;
1168: elements = new DefaultStyledDocument.ElementSpec[parseBuffer.size()];
1169: parseBuffer.copyInto(elements);
1170: parseBuffer.removeAllElements();
1171: if (offset == 0)
1172: create(elements);
1173: else
1174: insert(offset, elements);
1175:
1176: offset += HTMLDocument.this.getLength() - offset;
1177: }
1178:
1179:
1186: public void handleText(char[] data, int pos)
1187: {
1188: if (data != null && data.length > 0)
1189: addContent(data, 0, data.length);
1190: }
1191:
1192:
1200: public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
1201: {
1202:
1203: if (endHTMLEncountered)
1204: return;
1205:
1206: TagAction action = (TagAction) tagToAction.get(t);
1207: if (action != null)
1208: action.start(t, a);
1209: }
1210:
1211:
1217: public void handleComment(char[] data, int pos)
1218: {
1219:
1220: if (endHTMLEncountered)
1221: return;
1222:
1223: TagAction action = (TagAction) tagToAction.get(HTML.Tag.COMMENT);
1224: if (action != null)
1225: {
1226: action.start(HTML.Tag.COMMENT, new SimpleAttributeSet());
1227: action.end (HTML.Tag.COMMENT);
1228: }
1229: }
1230:
1231:
1238: public void handleEndTag(HTML.Tag t, int pos)
1239: {
1240:
1241: if (endHTMLEncountered)
1242: return;
1243:
1244:
1245: if (t == HTML.Tag.HTML)
1246: endHTMLEncountered = true;
1247:
1248: TagAction action = (TagAction) tagToAction.get(t);
1249: if (action != null)
1250: action.end(t);
1251: }
1252:
1253:
1261: public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
1262: {
1263:
1264: if (endHTMLEncountered)
1265: return;
1266:
1267: TagAction action = (TagAction) tagToAction.get (t);
1268: if (action != null)
1269: {
1270: action.start(t, a);
1271: action.end(t);
1272: }
1273: }
1274:
1275:
1283: public void handleEndOfLineString(String eol)
1284: throws NotImplementedException
1285: {
1286:
1287: print ("HTMLReader.handleEndOfLineString not implemented yet");
1288: }
1289:
1290:
1296: protected void textAreaContent(char[] data)
1297: throws NotImplementedException
1298: {
1299:
1300: print ("HTMLReader.textAreaContent not implemented yet");
1301: }
1302:
1303:
1308: protected void preContent(char[] data)
1309: throws NotImplementedException
1310: {
1311:
1312: print ("HTMLReader.preContent not implemented yet");
1313: }
1314:
1315:
1322: protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
1323: {
1324: printBuffer();
1325: DefaultStyledDocument.ElementSpec element;
1326:
1327:
1328:
1329: if (parseStack.size() > 0 && parseStack.peek() == HTML.Tag.IMPLIED)
1330: {
1331: element = new DefaultStyledDocument.ElementSpec(null,
1332: DefaultStyledDocument.ElementSpec.EndTagType);
1333: parseBuffer.addElement(element);
1334: parseStack.pop();
1335: }
1336:
1337: parseStack.push(t);
1338: AbstractDocument.AttributeContext ctx = getAttributeContext();
1339: AttributeSet copy = attr.copyAttributes();
1340: copy = ctx.addAttribute(copy, StyleConstants.NameAttribute, t);
1341: element = new DefaultStyledDocument.ElementSpec(copy,
1342: DefaultStyledDocument.ElementSpec.StartTagType);
1343: parseBuffer.addElement(element);
1344: printBuffer();
1345: }
1346:
1347:
1353: protected void blockClose(HTML.Tag t)
1354: {
1355: printBuffer();
1356: DefaultStyledDocument.ElementSpec element;
1357:
1358:
1359:
1360: DefaultStyledDocument.ElementSpec prev;
1361: prev = (DefaultStyledDocument.ElementSpec)
1362: parseBuffer.get(parseBuffer.size() - 1);
1363: if (prev.getType() == DefaultStyledDocument.ElementSpec.StartTagType)
1364: {
1365: AbstractDocument.AttributeContext ctx = getAttributeContext();
1366: AttributeSet attributes = ctx.getEmptySet();
1367: attributes = ctx.addAttribute(attributes, StyleConstants.NameAttribute,
1368: HTML.Tag.CONTENT);
1369: element = new DefaultStyledDocument.ElementSpec(attributes,
1370: DefaultStyledDocument.ElementSpec.ContentType,
1371: new char[0], 0, 0);
1372: parseBuffer.add(element);
1373: }
1374:
1375:
1376: else if (parseStack.peek() == HTML.Tag.IMPLIED)
1377: {
1378: element = new DefaultStyledDocument.ElementSpec(null,
1379: DefaultStyledDocument.ElementSpec.EndTagType);
1380: parseBuffer.addElement(element);
1381: if (parseStack.size() > 0)
1382: parseStack.pop();
1383: }
1384:
1385: element = new DefaultStyledDocument.ElementSpec(null,
1386: DefaultStyledDocument.ElementSpec.EndTagType);
1387: parseBuffer.addElement(element);
1388: printBuffer();
1389: if (parseStack.size() > 0)
1390: parseStack.pop();
1391: }
1392:
1393:
1401: protected void addContent(char[] data, int offs, int length)
1402: {
1403: addContent(data, offs, length, true);
1404: }
1405:
1406:
1416: protected void addContent(char[] data, int offs, int length,
1417: boolean generateImpliedPIfNecessary)
1418: {
1419: AbstractDocument.AttributeContext ctx = getAttributeContext();
1420: DefaultStyledDocument.ElementSpec element;
1421: AttributeSet attributes = null;
1422:
1423:
1424:
1425:
1426: boolean createImpliedParagraph = false;
1427: HTML.Tag parent = (HTML.Tag) parseStack.peek();
1428: if (parent != HTML.Tag.P && parent != HTML.Tag.H1
1429: && parent != HTML.Tag.H2
1430: && parent != HTML.Tag.H3 && parent != HTML.Tag.H4
1431: && parent != HTML.Tag.H5 && parent != HTML.Tag.H6
1432: && parent != HTML.Tag.TD)
1433: {
1434: attributes = ctx.getEmptySet();
1435: attributes = ctx.addAttribute(attributes,
1436: StyleConstants.NameAttribute,
1437: HTML.Tag.IMPLIED);
1438: element = new DefaultStyledDocument.ElementSpec(attributes,
1439: DefaultStyledDocument.ElementSpec.StartTagType);
1440: parseBuffer.add(element);
1441: parseStack.push(HTML.Tag.IMPLIED);
1442: }
1443:
1444:
1445:
1446: if (charAttr != null)
1447: attributes = charAttr.copyAttributes();
1448: else
1449: attributes = ctx.getEmptySet();
1450: attributes = ctx.addAttribute(attributes, StyleConstants.NameAttribute,
1451: HTML.Tag.CONTENT);
1452: element = new DefaultStyledDocument.ElementSpec(attributes,
1453: DefaultStyledDocument.ElementSpec.ContentType,
1454: data, offs, length);
1455:
1456: printBuffer();
1457:
1458: parseBuffer.addElement(element);
1459: printBuffer();
1460:
1461: if (parseBuffer.size() > HTMLDocument.this.getTokenThreshold())
1462: {
1463: try
1464: {
1465: flush();
1466: }
1467: catch (BadLocationException ble)
1468: {
1469:
1470: }
1471: }
1472: }
1473:
1474:
1480: protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a)
1481: throws NotImplementedException
1482: {
1483:
1484: print ("HTMLReader.addSpecialElement not implemented yet");
1485: }
1486:
1487: void printBuffer()
1488: {
1489: print ("\n*********BUFFER**********");
1490: for (int i = 0; i < parseBuffer.size(); i ++)
1491: print (" "+parseBuffer.get(i));
1492: print ("***************************");
1493: }
1494: }
1495:
1496:
1502: public HTMLEditorKit.ParserCallback getReader(int pos)
1503: {
1504: return new HTMLReader(pos);
1505: }
1506:
1507:
1518: public HTMLEditorKit.ParserCallback getReader(int pos,
1519: int popDepth,
1520: int pushDepth,
1521: HTML.Tag insertTag)
1522: {
1523: return new HTMLReader(pos, popDepth, pushDepth, insertTag);
1524: }
1525:
1526:
1536: public Element getElement(Element e, Object attribute, Object value)
1537: {
1538: if (e != null)
1539: {
1540: if (e.getAttributes().containsAttribute(attribute, value))
1541: return e;
1542:
1543: int count = e.getElementCount();
1544: for (int j = 0; j < count; j++)
1545: {
1546: Element child = e.getElement(j);
1547: if (child.getAttributes().containsAttribute(attribute, value))
1548: return child;
1549:
1550: Element grandChild = getElement(child, attribute, value);
1551: if (grandChild != null)
1552: return grandChild;
1553: }
1554: }
1555: return null;
1556: }
1557:
1558:
1566: public Element getElement(String attrId)
1567: {
1568: Element root = getDefaultRootElement();
1569: return getElement(root, HTML.getAttributeKey(attrId) , attrId);
1570: }
1571:
1572:
1584: public void setInnerHTML(Element elem, String htmlText)
1585: throws BadLocationException, IOException, NotImplementedException
1586: {
1587: if (elem.isLeaf())
1588: throw new IllegalArgumentException("Element is a leaf");
1589: if (parser == null)
1590: throw new IllegalStateException("Parser has not been set");
1591:
1592: System.out.println("setInnerHTML not implemented");
1593: }
1594:
1595:
1608: public void setOuterHTML(Element elem, String htmlText)
1609: throws BadLocationException, IOException, NotImplementedException
1610: {
1611: if (parser == null)
1612: throw new IllegalStateException("Parser has not been set");
1613:
1614: System.out.println("setOuterHTML not implemented");
1615: }
1616:
1617:
1627: public void insertBeforeStart(Element elem, String htmlText)
1628: throws BadLocationException, IOException, NotImplementedException
1629: {
1630: if (parser == null)
1631: throw new IllegalStateException("Parser has not been set");
1632:
1633: System.out.println("insertBeforeStart not implemented");
1634: }
1635:
1636:
1647: public void insertBeforeEnd(Element elem, String htmlText)
1648: throws BadLocationException, IOException, NotImplementedException
1649: {
1650: if (parser == null)
1651: throw new IllegalStateException("Parser has not been set");
1652:
1653: System.out.println("insertBeforeEnd not implemented");
1654: }
1655:
1656:
1666: public void insertAfterEnd(Element elem, String htmlText)
1667: throws BadLocationException, IOException, NotImplementedException
1668: {
1669: if (parser == null)
1670: throw new IllegalStateException("Parser has not been set");
1671:
1672: System.out.println("insertAfterEnd not implemented");
1673: }
1674:
1675:
1685: public void insertAfterStart(Element elem, String htmlText)
1686: throws BadLocationException, IOException, NotImplementedException
1687: {
1688: if (parser == null)
1689: throw new IllegalStateException("Parser has not been set");
1690:
1691: System.out.println("insertAfterStart not implemented");
1692: }
1693:
1694:
1709: public void setParagraphAttributes(int offset, int length, AttributeSet s,
1710: boolean replace)
1711: throws NotImplementedException
1712: {
1713:
1714: System.out.println("setParagraphAttributes not implemented");
1715: super.setParagraphAttributes(offset, length, s, replace);
1716: }
1717:
1718:
1723: protected void fireChangedUpdate(DocumentEvent e)
1724: throws NotImplementedException
1725: {
1726:
1727: System.out.println("fireChangedUpdate not implemented");
1728: super.fireChangedUpdate(e);
1729: }
1730:
1731:
1738: protected void fireUndoableEditUpdate(UndoableEditEvent e)
1739: {
1740: super.fireUndoableEditUpdate(e);
1741: }
1742: }