1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44:
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66:
67:
80: public class HTMLDocument extends DefaultStyledDocument
81: {
82:
85: public static final String AdditionalComments = "AdditionalComments";
86: URL baseURL = null;
87: boolean preservesUnknownTags = true;
88: int tokenThreshold = Integer.MAX_VALUE;
89: HTMLEditorKit.Parser parser;
90: StyleSheet styleSheet;
91: AbstractDocument.Content content;
92:
93:
97: public HTMLDocument()
98: {
99: this(null);
100: }
101:
102:
108: public HTMLDocument(StyleSheet styles)
109: {
110: this(new GapContent(BUFFER_SIZE_DEFAULT), styles);
111: }
112:
113:
120: public HTMLDocument(AbstractDocument.Content c, StyleSheet styles)
121: {
122: this.content = c;
123: if (styles == null)
124: {
125: styles = new StyleSheet();
126: styles.importStyleSheet(getClass().getResource(HTMLEditorKit.
127: DEFAULT_CSS));
128: }
129: this.styleSheet = styles;
130: }
131:
132:
138: public StyleSheet getStyleSheet()
139: {
140: return styleSheet;
141: }
142:
143:
148: protected AbstractElement createDefaultRoot()
149: {
150: AbstractDocument.AttributeContext ctx = getAttributeContext();
151:
152:
153: AttributeSet atts = ctx.getEmptySet();
154: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.HTML);
155: BranchElement html = (BranchElement) createBranchElement(null, atts);
156:
157:
158: atts = ctx.getEmptySet();
159: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.BODY);
160: BranchElement body = (BranchElement) createBranchElement(html, atts);
161: html.replace(0, 0, new Element[] { body });
162:
163:
164: atts = ctx.getEmptySet();
165: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute, HTML.Tag.P);
166: BranchElement p = (BranchElement) createBranchElement(body, atts);
167: body.replace(0, 0, new Element[] { p });
168:
169:
170: atts = ctx.getEmptySet();
171: atts = ctx.addAttribute(atts, StyleConstants.NameAttribute,
172: HTML.Tag.CONTENT);
173: Element leaf = createLeafElement(p, atts, 0, 1);
174: p.replace(0, 0, new Element[]{ leaf });
175:
176: return html;
177: }
178:
179:
191: protected Element createLeafElement(Element parent, AttributeSet a, int p0,
192: int p1)
193: {
194: RunElement el = new RunElement(parent, a, p0, p1);
195: el.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
196: return new RunElement(parent, a, p0, p1);
197: }
198:
199:
208: protected Element createBranchElement(Element parent, AttributeSet a)
209: {
210: return new BlockElement(parent, a);
211: }
212:
213:
218: public HTMLEditorKit.Parser getParser()
219: {
220: return parser;
221: }
222:
223:
228: public void setParser (HTMLEditorKit.Parser p)
229: {
230: parser = p;
231: }
232:
238: public void setTokenThreshold (int n)
239: {
240: tokenThreshold = n;
241: }
242:
243:
249: public int getTokenThreshold ()
250: {
251: return tokenThreshold;
252: }
253:
254:
260: public URL getBase()
261: {
262: return baseURL;
263: }
264:
265:
269: public void setBase(URL u)
270: {
271: baseURL = u;
272: styleSheet.setBase(u);
273: }
274:
275:
279: public boolean getPreservesUnknownTags()
280: {
281: return preservesUnknownTags;
282: }
283:
284:
288: public void setPreservesUnknownTags(boolean preservesTags)
289: {
290: preservesUnknownTags = preservesTags;
291: }
292:
293:
296: class LeafIterator extends Iterator
297: {
298: HTML.Tag tag;
299: HTMLDocument doc;
300: ElementIterator it;
301:
302: public LeafIterator (HTML.Tag t, HTMLDocument d)
303: {
304: doc = d;
305: tag = t;
306: it = new ElementIterator(doc);
307: }
308:
309:
313: public AttributeSet getAttributes()
314: {
315: if (it.current() != null)
316: return it.current().getAttributes();
317: return null;
318: }
319:
320:
325: public int getEndOffset()
326: {
327: if (it.current() != null)
328: return it.current().getEndOffset();
329: return -1;
330: }
331:
332:
337:
338: public int getStartOffset()
339: {
340: if (it.current() != null)
341: return it.current().getStartOffset();
342: return -1;
343: }
344:
345:
348: public void next()
349: {
350: it.next();
351: while (it.current()!= null && !it.current().isLeaf())
352: it.next();
353: }
354:
355:
361: public boolean isValid()
362: {
363: return it.current() != null;
364: }
365:
366:
369: public Tag getTag()
370: {
371: return tag;
372: }
373:
374: }
375:
376: public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event)
377: throws NotImplementedException
378: {
379:
380: }
381:
382:
387: public HTMLDocument.Iterator getIterator (HTML.Tag t)
388: {
389: return new HTMLDocument.LeafIterator(t, this);
390: }
391:
392:
395: public abstract static class Iterator
396: {
397:
401: public abstract AttributeSet getAttributes();
402:
403:
408: public abstract int getEndOffset();
409:
410:
415: public abstract int getStartOffset();
416:
417:
420: public abstract void next();
421:
422:
428: public abstract boolean isValid();
429:
430:
434: public abstract HTML.Tag getTag();
435: }
436:
437: public class BlockElement extends AbstractDocument.BranchElement
438: {
439: public BlockElement (Element parent, AttributeSet a)
440: {
441: super(parent, a);
442: }
443:
444:
448: public AttributeSet getResolveParent()
449: {
450: return null;
451: }
452:
453:
458: public String getName()
459: {
460: Object tag = getAttribute(StyleConstants.NameAttribute);
461: String name = null;
462: if (tag != null)
463: name = tag.toString();
464: return name;
465: }
466: }
467:
468:
472: public class RunElement extends AbstractDocument.LeafElement
473: {
474:
475:
484: public RunElement(Element parent, AttributeSet a, int start, int end)
485: {
486: super(parent, a, start, end);
487: }
488:
489:
494: public String getName()
495: {
496: Object tag = getAttribute(StyleConstants.NameAttribute);
497: String name = null;
498: if (tag != null)
499: name = tag.toString();
500: return name;
501: }
502:
503:
509: public AttributeSet getResolveParent()
510: {
511: return null;
512: }
513: }
514:
515:
520: public class HTMLReader extends HTMLEditorKit.ParserCallback
521: {
522:
525: protected MutableAttributeSet charAttr = new SimpleAttributeSet();
526:
527: protected Vector parseBuffer = new Vector();
528:
529:
532: Stack charAttrStack = new Stack();
533:
534:
538: Stack parseStack = new Stack();
539:
540:
541: HashMap tagToAction;
542:
543:
544: boolean endHTMLEncountered = false;
545:
546:
549: int popDepth;
550:
551:
554: int pushDepth;
555:
556:
559: int offset;
560:
561:
564: HTML.Tag insertTag;
565:
566:
569: boolean insertTagEncountered;
570:
571:
572:
573: boolean debug = false;
574:
575: void print (String line)
576: {
577: if (debug)
578: System.out.println (line);
579: }
580:
581: public class TagAction
582: {
583:
587: public void start(HTML.Tag t, MutableAttributeSet a)
588: {
589:
590: }
591:
592:
596: public void end(HTML.Tag t)
597: {
598:
599: }
600: }
601:
602: public class BlockAction extends TagAction
603: {
604:
608: public void start(HTML.Tag t, MutableAttributeSet a)
609: {
610:
611: blockOpen(t, a);
612: }
613:
614:
618: public void end(HTML.Tag t)
619: {
620:
621: blockClose(t);
622: }
623: }
624:
625: public class CharacterAction extends TagAction
626: {
627:
631: public void start(HTML.Tag t, MutableAttributeSet a)
632: {
633:
634: pushCharacterStyle();
635:
636:
637: if(CharacterAttributeTranslator.translateTag(charAttr, t, a))
638: return;
639:
640:
641: if (a != null)
642: charAttr.addAttribute(t, a.copyAttributes());
643: }
644:
645:
649: public void end(HTML.Tag t)
650: {
651: popCharacterStyle();
652: }
653: }
654:
655: public class FormAction extends SpecialAction
656: {
657:
661: public void start(HTML.Tag t, MutableAttributeSet a)
662: throws NotImplementedException
663: {
664:
665: print ("FormAction.start not implemented");
666: }
667:
668:
672: public void end(HTML.Tag t)
673: throws NotImplementedException
674: {
675:
676: print ("FormAction.end not implemented");
677: }
678: }
679:
680:
689: public class HiddenAction
690: extends TagAction
691: {
692:
696: public void start(HTML.Tag t, MutableAttributeSet a)
697: {
698: blockOpen(t, a);
699: }
700:
701:
705: public void end(HTML.Tag t)
706: {
707: blockClose(t);
708: }
709: }
710:
711: public class IsindexAction extends TagAction
712: {
713:
717: public void start(HTML.Tag t, MutableAttributeSet a)
718: throws NotImplementedException
719: {
720:
721: print ("IsindexAction.start not implemented");
722: }
723: }
724:
725: public class ParagraphAction extends BlockAction
726: {
727:
731: public void start(HTML.Tag t, MutableAttributeSet a)
732: {
733: blockOpen(t, a);
734: }
735:
736:
740: public void end(HTML.Tag t)
741: {
742: blockClose(t);
743: }
744: }
745:
746: public class PreAction extends BlockAction
747: {
748:
752: public void start(HTML.Tag t, MutableAttributeSet a)
753: throws NotImplementedException
754: {
755:
756: print ("PreAction.start not implemented");
757: super.start(t, a);
758: }
759:
760:
764: public void end(HTML.Tag t)
765: throws NotImplementedException
766: {
767:
768: print ("PreAction.end not implemented");
769: super.end(t);
770: }
771: }
772:
773:
780: public class SpecialAction extends TagAction
781: {
782:
785: public void start(HTML.Tag t, MutableAttributeSet a)
786: {
787: addSpecialElement(t, a);
788: }
789: }
790:
791: class AreaAction extends TagAction
792: {
793:
797: public void start(HTML.Tag t, MutableAttributeSet a)
798: throws NotImplementedException
799: {
800:
801: print ("AreaAction.start not implemented");
802: }
803:
804:
808: public void end(HTML.Tag t)
809: throws NotImplementedException
810: {
811:
812: print ("AreaAction.end not implemented");
813: }
814: }
815:
816: class BaseAction extends TagAction
817: {
818:
822: public void start(HTML.Tag t, MutableAttributeSet a)
823: throws NotImplementedException
824: {
825:
826: print ("BaseAction.start not implemented");
827: }
828:
829:
833: public void end(HTML.Tag t)
834: throws NotImplementedException
835: {
836:
837: print ("BaseAction.end not implemented");
838: }
839: }
840:
841: class HeadAction extends BlockAction
842: {
843:
847: public void start(HTML.Tag t, MutableAttributeSet a)
848: throws NotImplementedException
849: {
850:
851: print ("HeadAction.start not implemented: "+t);
852: super.start(t, a);
853: }
854:
855:
859: public void end(HTML.Tag t)
860: throws NotImplementedException
861: {
862:
863: print ("HeadAction.end not implemented: "+t);
864: super.end(t);
865: }
866: }
867:
868: class LinkAction extends TagAction
869: {
870:
874: public void start(HTML.Tag t, MutableAttributeSet a)
875: throws NotImplementedException
876: {
877:
878: print ("LinkAction.start not implemented");
879: }
880:
881:
885: public void end(HTML.Tag t)
886: throws NotImplementedException
887: {
888:
889: print ("LinkAction.end not implemented");
890: }
891: }
892:
893: class MapAction extends TagAction
894: {
895:
899: public void start(HTML.Tag t, MutableAttributeSet a)
900: throws NotImplementedException
901: {
902:
903: print ("MapAction.start not implemented");
904: }
905:
906:
910: public void end(HTML.Tag t)
911: throws NotImplementedException
912: {
913:
914: print ("MapAction.end not implemented");
915: }
916: }
917:
918: class MetaAction extends TagAction
919: {
920:
924: public void start(HTML.Tag t, MutableAttributeSet a)
925: throws NotImplementedException
926: {
927:
928: print ("MetaAction.start not implemented");
929: }
930:
931:
935: public void end(HTML.Tag t)
936: throws NotImplementedException
937: {
938:
939: print ("MetaAction.end not implemented");
940: }
941: }
942:
943: class StyleAction extends TagAction
944: {
945:
949: public void start(HTML.Tag t, MutableAttributeSet a)
950: throws NotImplementedException
951: {
952:
953: print ("StyleAction.start not implemented");
954: }
955:
956:
960: public void end(HTML.Tag t)
961: throws NotImplementedException
962: {
963:
964: print ("StyleAction.end not implemented");
965: }
966: }
967:
968: class TitleAction extends TagAction
969: {
970:
974: public void start(HTML.Tag t, MutableAttributeSet a)
975: throws NotImplementedException
976: {
977:
978: print ("TitleAction.start not implemented");
979: }
980:
981:
985: public void end(HTML.Tag t)
986: throws NotImplementedException
987: {
988:
989: print ("TitleAction.end not implemented");
990: }
991: }
992:
993: public HTMLReader(int offset)
994: {
995: this (offset, 0, 0, null);
996: }
997:
998: public HTMLReader(int offset, int popDepth, int pushDepth,
999: HTML.Tag insertTag)
1000: {
1001: print ("HTMLReader created with pop: "+popDepth
1002: + " push: "+pushDepth + " offset: "+offset
1003: + " tag: "+insertTag);
1004: this.insertTag = insertTag;
1005: this.offset = offset;
1006: this.popDepth = popDepth;
1007: this.pushDepth = pushDepth;
1008: initTags();
1009: }
1010:
1011: void initTags()
1012: {
1013: tagToAction = new HashMap(72);
1014: CharacterAction characterAction = new CharacterAction();
1015: HiddenAction hiddenAction = new HiddenAction();
1016: AreaAction areaAction = new AreaAction();
1017: BaseAction baseAction = new BaseAction();
1018: BlockAction blockAction = new BlockAction();
1019: SpecialAction specialAction = new SpecialAction();
1020: ParagraphAction paragraphAction = new ParagraphAction();
1021: HeadAction headAction = new HeadAction();
1022: FormAction formAction = new FormAction();
1023: IsindexAction isindexAction = new IsindexAction();
1024: LinkAction linkAction = new LinkAction();
1025: MapAction mapAction = new MapAction();
1026: PreAction preAction = new PreAction();
1027: MetaAction metaAction = new MetaAction();
1028: StyleAction styleAction = new StyleAction();
1029: TitleAction titleAction = new TitleAction();
1030:
1031:
1032: tagToAction.put(HTML.Tag.A, characterAction);
1033: tagToAction.put(HTML.Tag.ADDRESS, characterAction);
1034: tagToAction.put(HTML.Tag.APPLET, hiddenAction);
1035: tagToAction.put(HTML.Tag.AREA, areaAction);
1036: tagToAction.put(HTML.Tag.B, characterAction);
1037: tagToAction.put(HTML.Tag.BASE, baseAction);
1038: tagToAction.put(HTML.Tag.BASEFONT, characterAction);
1039: tagToAction.put(HTML.Tag.BIG, characterAction);
1040: tagToAction.put(HTML.Tag.BLOCKQUOTE, blockAction);
1041: tagToAction.put(HTML.Tag.BODY, blockAction);
1042: tagToAction.put(HTML.Tag.BR, specialAction);
1043: tagToAction.put(HTML.Tag.CAPTION, blockAction);
1044: tagToAction.put(HTML.Tag.CENTER, blockAction);
1045: tagToAction.put(HTML.Tag.CITE, characterAction);
1046: tagToAction.put(HTML.Tag.CODE, characterAction);
1047: tagToAction.put(HTML.Tag.DD, blockAction);
1048: tagToAction.put(HTML.Tag.DFN, characterAction);
1049: tagToAction.put(HTML.Tag.DIR, blockAction);
1050: tagToAction.put(HTML.Tag.DIV, blockAction);
1051: tagToAction.put(HTML.Tag.DL, blockAction);
1052: tagToAction.put(HTML.Tag.DT, paragraphAction);
1053: tagToAction.put(HTML.Tag.EM, characterAction);
1054: tagToAction.put(HTML.Tag.FONT, characterAction);
1055: tagToAction.put(HTML.Tag.FORM, blockAction);
1056: tagToAction.put(HTML.Tag.FRAME, specialAction);
1057: tagToAction.put(HTML.Tag.FRAMESET, blockAction);
1058: tagToAction.put(HTML.Tag.H1, paragraphAction);
1059: tagToAction.put(HTML.Tag.H2, paragraphAction);
1060: tagToAction.put(HTML.Tag.H3, paragraphAction);
1061: tagToAction.put(HTML.Tag.H4, paragraphAction);
1062: tagToAction.put(HTML.Tag.H5, paragraphAction);
1063: tagToAction.put(HTML.Tag.H6, paragraphAction);
1064: tagToAction.put(HTML.Tag.HEAD, headAction);
1065: tagToAction.put(HTML.Tag.HR, specialAction);
1066: tagToAction.put(HTML.Tag.HTML, blockAction);
1067: tagToAction.put(HTML.Tag.I, characterAction);
1068: tagToAction.put(HTML.Tag.IMG, specialAction);
1069: tagToAction.put(HTML.Tag.INPUT, formAction);
1070: tagToAction.put(HTML.Tag.ISINDEX, isindexAction);
1071: tagToAction.put(HTML.Tag.KBD, characterAction);
1072: tagToAction.put(HTML.Tag.LI, blockAction);
1073: tagToAction.put(HTML.Tag.LINK, linkAction);
1074: tagToAction.put(HTML.Tag.MAP, mapAction);
1075: tagToAction.put(HTML.Tag.MENU, blockAction);
1076: tagToAction.put(HTML.Tag.META, metaAction);
1077: tagToAction.put(HTML.Tag.NOFRAMES, blockAction);
1078: tagToAction.put(HTML.Tag.OBJECT, specialAction);
1079: tagToAction.put(HTML.Tag.OL, blockAction);
1080: tagToAction.put(HTML.Tag.OPTION, formAction);
1081: tagToAction.put(HTML.Tag.P, paragraphAction);
1082: tagToAction.put(HTML.Tag.PARAM, hiddenAction);
1083: tagToAction.put(HTML.Tag.PRE, preAction);
1084: tagToAction.put(HTML.Tag.SAMP, characterAction);
1085: tagToAction.put(HTML.Tag.SCRIPT, hiddenAction);
1086: tagToAction.put(HTML.Tag.SELECT, formAction);
1087: tagToAction.put(HTML.Tag.SMALL, characterAction);
1088: tagToAction.put(HTML.Tag.STRIKE, characterAction);
1089: tagToAction.put(HTML.Tag.S, characterAction);
1090: tagToAction.put(HTML.Tag.STRONG, characterAction);
1091: tagToAction.put(HTML.Tag.STYLE, styleAction);
1092: tagToAction.put(HTML.Tag.SUB, characterAction);
1093: tagToAction.put(HTML.Tag.SUP, characterAction);
1094: tagToAction.put(HTML.Tag.TABLE, blockAction);
1095: tagToAction.put(HTML.Tag.TD, blockAction);
1096: tagToAction.put(HTML.Tag.TEXTAREA, formAction);
1097: tagToAction.put(HTML.Tag.TH, blockAction);
1098: tagToAction.put(HTML.Tag.TITLE, titleAction);
1099: tagToAction.put(HTML.Tag.TR, blockAction);
1100: tagToAction.put(HTML.Tag.TT, characterAction);
1101: tagToAction.put(HTML.Tag.U, characterAction);
1102: tagToAction.put(HTML.Tag.UL, blockAction);
1103: tagToAction.put(HTML.Tag.VAR, characterAction);
1104: }
1105:
1106:
1110: protected void pushCharacterStyle()
1111: {
1112: charAttrStack.push(charAttr.copyAttributes());
1113: }
1114:
1115:
1120: protected void popCharacterStyle()
1121: {
1122: if (!charAttrStack.isEmpty())
1123: charAttr = (MutableAttributeSet) charAttrStack.pop();
1124: }
1125:
1126:
1134: protected void registerTag(HTML.Tag t, HTMLDocument.HTMLReader.TagAction a)
1135: {
1136: tagToAction.put (t, a);
1137: }
1138:
1139:
1143: public void flush() throws BadLocationException
1144: {
1145: DefaultStyledDocument.ElementSpec[] elements;
1146: elements = new DefaultStyledDocument.ElementSpec[parseBuffer.size()];
1147: parseBuffer.copyInto(elements);
1148: parseBuffer.removeAllElements();
1149: if (offset == 0)
1150: create(elements);
1151: else
1152: insert(offset, elements);
1153:
1154: offset += HTMLDocument.this.getLength() - offset;
1155: }
1156:
1157:
1164: public void handleText(char[] data, int pos)
1165: {
1166: if (data != null && data.length > 0)
1167: addContent(data, 0, data.length);
1168: }
1169:
1170:
1177: private boolean shouldInsert()
1178: {
1179: return ! endHTMLEncountered
1180: && (insertTagEncountered || insertTag == null);
1181: }
1182:
1183:
1191: public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
1192: {
1193: if (t == insertTag)
1194: insertTagEncountered = true;
1195:
1196: if (shouldInsert())
1197: {
1198: TagAction action = (TagAction) tagToAction.get(t);
1199: if (action != null)
1200: action.start(t, a);
1201: }
1202: }
1203:
1204:
1210: public void handleComment(char[] data, int pos)
1211: {
1212: if (shouldInsert())
1213: {
1214: TagAction action = (TagAction) tagToAction.get(HTML.Tag.COMMENT);
1215: if (action != null)
1216: {
1217: action.start(HTML.Tag.COMMENT,
1218: htmlAttributeSet.EMPTY_HTML_ATTRIBUTE_SET);
1219: action.end(HTML.Tag.COMMENT);
1220: }
1221: }
1222: }
1223:
1224:
1231: public void handleEndTag(HTML.Tag t, int pos)
1232: {
1233: if (shouldInsert())
1234: {
1235:
1236: if (t == HTML.Tag.HTML)
1237: endHTMLEncountered = true;
1238:
1239: TagAction action = (TagAction) tagToAction.get(t);
1240: if (action != null)
1241: action.end(t);
1242: }
1243: }
1244:
1245:
1253: public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos)
1254: {
1255: if (t == insertTag)
1256: insertTagEncountered = true;
1257:
1258: if (shouldInsert())
1259: {
1260: TagAction action = (TagAction) tagToAction.get(t);
1261: if (action != null)
1262: {
1263: action.start(t, a);
1264: action.end(t);
1265: }
1266: }
1267: }
1268:
1269:
1277: public void handleEndOfLineString(String eol)
1278: {
1279:
1280: print ("HTMLReader.handleEndOfLineString not implemented yet");
1281: }
1282:
1283:
1289: protected void textAreaContent(char[] data)
1290: throws NotImplementedException
1291: {
1292:
1293: print ("HTMLReader.textAreaContent not implemented yet");
1294: }
1295:
1296:
1301: protected void preContent(char[] data)
1302: throws NotImplementedException
1303: {
1304:
1305: print ("HTMLReader.preContent not implemented yet");
1306: }
1307:
1308:
1315: protected void blockOpen(HTML.Tag t, MutableAttributeSet attr)
1316: {
1317: printBuffer();
1318: DefaultStyledDocument.ElementSpec element;
1319:
1320: parseStack.push(t);
1321: AbstractDocument.AttributeContext ctx = getAttributeContext();
1322: AttributeSet copy = attr.copyAttributes();
1323: copy = ctx.addAttribute(copy, StyleConstants.NameAttribute, t);
1324: element = new DefaultStyledDocument.ElementSpec(copy,
1325: DefaultStyledDocument.ElementSpec.StartTagType);
1326: parseBuffer.addElement(element);
1327: printBuffer();
1328: }
1329:
1330:
1336: protected void blockClose(HTML.Tag t)
1337: {
1338: printBuffer();
1339: DefaultStyledDocument.ElementSpec element;
1340:
1341:
1342:
1343: DefaultStyledDocument.ElementSpec prev;
1344: prev = (DefaultStyledDocument.ElementSpec)
1345: parseBuffer.get(parseBuffer.size() - 1);
1346: if (prev.getType() == DefaultStyledDocument.ElementSpec.StartTagType)
1347: {
1348: AbstractDocument.AttributeContext ctx = getAttributeContext();
1349: AttributeSet attributes = ctx.getEmptySet();
1350: attributes = ctx.addAttribute(attributes, StyleConstants.NameAttribute,
1351: HTML.Tag.CONTENT);
1352: element = new DefaultStyledDocument.ElementSpec(attributes,
1353: DefaultStyledDocument.ElementSpec.ContentType,
1354: new char[0], 0, 0);
1355: parseBuffer.add(element);
1356: }
1357:
1358: element = new DefaultStyledDocument.ElementSpec(null,
1359: DefaultStyledDocument.ElementSpec.EndTagType);
1360: parseBuffer.addElement(element);
1361: printBuffer();
1362: if (parseStack.size() > 0)
1363: parseStack.pop();
1364: }
1365:
1366:
1374: protected void addContent(char[] data, int offs, int length)
1375: {
1376: addContent(data, offs, length, true);
1377: }
1378:
1379:
1389: protected void addContent(char[] data, int offs, int length,
1390: boolean generateImpliedPIfNecessary)
1391: {
1392: AbstractDocument.AttributeContext ctx = getAttributeContext();
1393: DefaultStyledDocument.ElementSpec element;
1394: AttributeSet attributes = null;
1395:
1396:
1397:
1398: if (charAttr != null)
1399: attributes = charAttr.copyAttributes();
1400: else
1401: attributes = ctx.getEmptySet();
1402: attributes = ctx.addAttribute(attributes, StyleConstants.NameAttribute,
1403: HTML.Tag.CONTENT);
1404: element = new DefaultStyledDocument.ElementSpec(attributes,
1405: DefaultStyledDocument.ElementSpec.ContentType,
1406: data, offs, length);
1407:
1408: printBuffer();
1409:
1410: parseBuffer.addElement(element);
1411: printBuffer();
1412:
1413: if (parseBuffer.size() > HTMLDocument.this.getTokenThreshold())
1414: {
1415: try
1416: {
1417: flush();
1418: }
1419: catch (BadLocationException ble)
1420: {
1421:
1422: }
1423: }
1424: }
1425:
1426:
1432: protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a)
1433: {
1434: a.addAttribute(StyleConstants.NameAttribute, t);
1435:
1436:
1437:
1438: AttributeSet copy = a.copyAttributes();
1439:
1440:
1441:
1442:
1443: DefaultStyledDocument.ElementSpec spec =
1444: new DefaultStyledDocument.ElementSpec(copy,
1445: DefaultStyledDocument.ElementSpec.ContentType,
1446: new char[] {' ', ' '}, 0, 2 );
1447: parseBuffer.add(spec);
1448: }
1449:
1450: void printBuffer()
1451: {
1452: print ("\n*********BUFFER**********");
1453: for (int i = 0; i < parseBuffer.size(); i ++)
1454: print (" "+parseBuffer.get(i));
1455: print ("***************************");
1456: }
1457: }
1458:
1459:
1465: public HTMLEditorKit.ParserCallback getReader(int pos)
1466: {
1467: return new HTMLReader(pos);
1468: }
1469:
1470:
1481: public HTMLEditorKit.ParserCallback getReader(int pos,
1482: int popDepth,
1483: int pushDepth,
1484: HTML.Tag insertTag)
1485: {
1486: return new HTMLReader(pos, popDepth, pushDepth, insertTag);
1487: }
1488:
1489:
1506: public HTMLEditorKit.ParserCallback getInsertingReader(int pos, int popDepth,
1507: int pushDepth,
1508: HTML.Tag insertTag,
1509: final Element parent)
1510: throws IllegalStateException
1511: {
1512: if (parser == null)
1513: throw new IllegalStateException("Parser has not been set");
1514:
1515: HTMLReader reader = new HTMLReader(pos, popDepth, pushDepth, insertTag)
1516: {
1517:
1520: public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos)
1521: {
1522: if (t != HTML.Tag.BODY)
1523: super.handleStartTag(t, a, pos);
1524: }
1525:
1526:
1529: public void handleEndTag(HTML.Tag t, int pos)
1530: {
1531: if (t != HTML.Tag.BODY)
1532: super.handleEndTag(t, pos);
1533: }
1534: };
1535:
1536:
1537: reader.parseStack.push(parent.getAttributes().getAttribute(
1538: StyleConstants.NameAttribute));
1539:
1540: return reader;
1541: }
1542:
1543:
1553: public Element getElement(Element e, Object attribute, Object value)
1554: {
1555: if (e != null)
1556: {
1557: if (e.getAttributes().containsAttribute(attribute, value))
1558: return e;
1559:
1560: int count = e.getElementCount();
1561: for (int j = 0; j < count; j++)
1562: {
1563: Element child = e.getElement(j);
1564: if (child.getAttributes().containsAttribute(attribute, value))
1565: return child;
1566:
1567: Element grandChild = getElement(child, attribute, value);
1568: if (grandChild != null)
1569: return grandChild;
1570: }
1571: }
1572: return null;
1573: }
1574:
1575:
1584: public Element getElement(String attrId)
1585: {
1586: return getElement(getDefaultRootElement(), HTML.Attribute.ID,
1587: attrId);
1588: }
1589:
1590:
1602: public void setInnerHTML(Element elem, String htmlText)
1603: throws BadLocationException, IOException
1604: {
1605: if (elem.isLeaf())
1606: throw new IllegalArgumentException("Element is a leaf");
1607:
1608: int start = elem.getStartOffset();
1609: int end = elem.getEndOffset();
1610:
1611: HTMLEditorKit.ParserCallback reader = getInsertingReader(
1612: end, 0, 0, HTML.Tag.BODY, elem);
1613:
1614:
1615: getParser().parse(new StringReader(htmlText), reader, true);
1616:
1617:
1618: remove(start, end - start);
1619: }
1620:
1621:
1634: public void setOuterHTML(Element elem, String htmlText)
1635: throws BadLocationException, IOException
1636: {
1637:
1638: int start = elem.getStartOffset();
1639: int end = elem.getEndOffset();
1640:
1641: remove(start, end-start);
1642:
1643: HTMLEditorKit.ParserCallback reader = getInsertingReader(
1644: start, 0, 0, HTML.Tag.BODY, elem);
1645:
1646:
1647: getParser().parse(new StringReader(htmlText), reader, true);
1648: }
1649:
1650:
1660: public void insertBeforeStart(Element elem, String htmlText)
1661: throws BadLocationException, IOException
1662: {
1663: HTMLEditorKit.ParserCallback reader = getInsertingReader(
1664: elem.getStartOffset(), 0, 0, HTML.Tag.BODY, elem);
1665:
1666:
1667: getParser().parse(new StringReader(htmlText), reader, true);
1668: }
1669:
1670:
1681: public void insertBeforeEnd(Element elem, String htmlText)
1682: throws BadLocationException, IOException
1683: {
1684: HTMLEditorKit.ParserCallback reader = getInsertingReader(
1685: elem.getEndOffset(), 0, 0, HTML.Tag.BODY, elem);
1686:
1687:
1688: getParser().parse(new StringReader(htmlText), reader, true);
1689:
1690: }
1691:
1692:
1702: public void insertAfterEnd(Element elem, String htmlText)
1703: throws BadLocationException, IOException
1704: {
1705: HTMLEditorKit.ParserCallback reader = getInsertingReader(
1706: elem.getEndOffset(), 0, 0, HTML.Tag.BODY, elem);
1707:
1708:
1709: getParser().parse(new StringReader(htmlText), reader, true);
1710: }
1711:
1712:
1722: public void insertAfterStart(Element elem, String htmlText)
1723: throws BadLocationException, IOException
1724: {
1725: HTMLEditorKit.ParserCallback reader = getInsertingReader(
1726: elem.getStartOffset(), 0, 0, HTML.Tag.BODY, elem);
1727:
1728:
1729: getParser().parse(new StringReader(htmlText), reader, true);
1730: }
1731: }