1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text.html; 26 27 import java.awt.font.TextAttribute; 28 import java.util.*; 29 import java.net.URL; 30 import java.net.MalformedURLException; 31 import java.io.*; 32 import javax.swing.*; 33 import javax.swing.event.*; 34 import javax.swing.text.*; 35 import javax.swing.undo.*; 36 import sun.swing.SwingUtilities2; 37 import static sun.swing.SwingUtilities2.IMPLIED_CR; 38 39 /** 40 * A document that models HTML. The purpose of this model is to 41 * support both browsing and editing. As a result, the structure 42 * described by an HTML document is not exactly replicated by default. 43 * The element structure that is modeled by default, is built by the 44 * class <code>HTMLDocument.HTMLReader</code>, which implements the 45 * <code>HTMLEditorKit.ParserCallback</code> protocol that the parser 46 * expects. To change the structure one can subclass 47 * <code>HTMLReader</code>, and reimplement the method {@link 48 * #getReader(int)} to return the new reader implementation. The 49 * documentation for <code>HTMLReader</code> should be consulted for 50 * the details of the default structure created. The intent is that 51 * the document be non-lossy (although reproducing the HTML format may 52 * result in a different format). 53 * 54 * <p>The document models only HTML, and makes no attempt to store 55 * view attributes in it. The elements are identified by the 56 * <code>StyleContext.NameAttribute</code> attribute, which should 57 * always have a value of type <code>HTML.Tag</code> that identifies 58 * the kind of element. Some of the elements (such as comments) are 59 * synthesized. The <code>HTMLFactory</code> uses this attribute to 60 * determine what kind of view to build.</p> 61 * 62 * <p>This document supports incremental loading. The 63 * <code>TokenThreshold</code> property controls how much of the parse 64 * is buffered before trying to update the element structure of the 65 * document. This property is set by the <code>EditorKit</code> so 66 * that subclasses can disable it.</p> 67 * 68 * <p>The <code>Base</code> property determines the URL against which 69 * relative URLs are resolved. By default, this will be the 70 * <code>Document.StreamDescriptionProperty</code> if the value of the 71 * property is a URL. If a <BASE> tag is encountered, the base 72 * will become the URL specified by that tag. Because the base URL is 73 * a property, it can of course be set directly.</p> 74 * 75 * <p>The default content storage mechanism for this document is a gap 76 * buffer (<code>GapContent</code>). Alternatives can be supplied by 77 * using the constructor that takes a <code>Content</code> 78 * implementation.</p> 79 * 80 * <h2>Modifying HTMLDocument</h2> 81 * 82 * <p>In addition to the methods provided by Document and 83 * StyledDocument for mutating an HTMLDocument, HTMLDocument provides 84 * a number of convenience methods. The following methods can be used 85 * to insert HTML content into an existing document.</p> 86 * 87 * <ul> 88 * <li>{@link #setInnerHTML(Element, String)}</li> 89 * <li>{@link #setOuterHTML(Element, String)}</li> 90 * <li>{@link #insertBeforeStart(Element, String)}</li> 91 * <li>{@link #insertAfterStart(Element, String)}</li> 92 * <li>{@link #insertBeforeEnd(Element, String)}</li> 93 * <li>{@link #insertAfterEnd(Element, String)}</li> 94 * </ul> 95 * 96 * <p>The following examples illustrate using these methods. Each 97 * example assumes the HTML document is initialized in the following 98 * way:</p> 99 * 100 * <pre> 101 * JEditorPane p = new JEditorPane(); 102 * p.setContentType("text/html"); 103 * p.setText("..."); // Document text is provided below. 104 * HTMLDocument d = (HTMLDocument) p.getDocument(); 105 * </pre> 106 * 107 * <p>With the following HTML content:</p> 108 * 109 * <pre> 110 * <html> 111 * <head> 112 * <title>An example HTMLDocument</title> 113 * <style type="text/css"> 114 * div { background-color: silver; } 115 * ul { color: red; } 116 * </style> 117 * </head> 118 * <body> 119 * <div id="BOX"> 120 * <p>Paragraph 1</p> 121 * <p>Paragraph 2</p> 122 * </div> 123 * </body> 124 * </html> 125 * </pre> 126 * 127 * <p>All the methods for modifying an HTML document require an {@link 128 * Element}. Elements can be obtained from an HTML document by using 129 * the method {@link #getElement(Element e, Object attribute, Object 130 * value)}. It returns the first descendant element that contains the 131 * specified attribute with the given value, in depth-first order. 132 * For example, <code>d.getElement(d.getDefaultRootElement(), 133 * StyleConstants.NameAttribute, HTML.Tag.P)</code> returns the first 134 * paragraph element.</p> 135 * 136 * <p>A convenient shortcut for locating elements is the method {@link 137 * #getElement(String)}; returns an element whose <code>ID</code> 138 * attribute matches the specified value. For example, 139 * <code>d.getElement("BOX")</code> returns the <code>DIV</code> 140 * element.</p> 141 * 142 * <p>The {@link #getIterator(HTML.Tag t)} method can also be used for 143 * finding all occurrences of the specified HTML tag in the 144 * document.</p> 145 * 146 * <h3>Inserting elements</h3> 147 * 148 * <p>Elements can be inserted before or after the existing children 149 * of any non-leaf element by using the methods 150 * <code>insertAfterStart</code> and <code>insertBeforeEnd</code>. 151 * For example, if <code>e</code> is the <code>DIV</code> element, 152 * <code>d.insertAfterStart(e, "<ul><li>List 153 * Item</li></ul>")</code> inserts the list before the first 154 * paragraph, and <code>d.insertBeforeEnd(e, "<ul><li>List 155 * Item</li></ul>")</code> inserts the list after the last 156 * paragraph. The <code>DIV</code> block becomes the parent of the 157 * newly inserted elements.</p> 158 * 159 * <p>Sibling elements can be inserted before or after any element by 160 * using the methods <code>insertBeforeStart</code> and 161 * <code>insertAfterEnd</code>. For example, if <code>e</code> is the 162 * <code>DIV</code> element, <code>d.insertBeforeStart(e, 163 * "<ul><li>List Item</li></ul>")</code> inserts the list 164 * before the <code>DIV</code> element, and <code>d.insertAfterEnd(e, 165 * "<ul><li>List Item</li></ul>")</code> inserts the list 166 * after the <code>DIV</code> element. The newly inserted elements 167 * become siblings of the <code>DIV</code> element.</p> 168 * 169 * <h3>Replacing elements</h3> 170 * 171 * <p>Elements and all their descendants can be replaced by using the 172 * methods <code>setInnerHTML</code> and <code>setOuterHTML</code>. 173 * For example, if <code>e</code> is the <code>DIV</code> element, 174 * <code>d.setInnerHTML(e, "<ul><li>List 175 * Item</li></ul>")</code> replaces all children paragraphs with 176 * the list, and <code>d.setOuterHTML(e, "<ul><li>List 177 * Item</li></ul>")</code> replaces the <code>DIV</code> element 178 * itself. In latter case the parent of the list is the 179 * <code>BODY</code> element. 180 * 181 * <h3>Summary</h3> 182 * 183 * <p>The following table shows the example document and the results 184 * of various methods described above.</p> 185 * 186 * <table border=1 cellspacing=0 summary="HTML Content of example above"> 187 * <tr> 188 * <th>Example</th> 189 * <th><code>insertAfterStart</code></th> 190 * <th><code>insertBeforeEnd</code></th> 191 * <th><code>insertBeforeStart</code></th> 192 * <th><code>insertAfterEnd</code></th> 193 * <th><code>setInnerHTML</code></th> 194 * <th><code>setOuterHTML</code></th> 195 * </tr> 196 * <tr valign="top"> 197 * <td style="white-space:nowrap"> 198 * <div style="background-color: silver;"> 199 * <p>Paragraph 1</p> 200 * <p>Paragraph 2</p> 201 * </div> 202 * </td> 203 * <!--insertAfterStart--> 204 * <td style="white-space:nowrap"> 205 * <div style="background-color: silver;"> 206 * <ul style="color: red;"> 207 * <li>List Item</li> 208 * </ul> 209 * <p>Paragraph 1</p> 210 * <p>Paragraph 2</p> 211 * </div> 212 * </td> 213 * <!--insertBeforeEnd--> 214 * <td style="white-space:nowrap"> 215 * <div style="background-color: silver;"> 216 * <p>Paragraph 1</p> 217 * <p>Paragraph 2</p> 218 * <ul style="color: red;"> 219 * <li>List Item</li> 220 * </ul> 221 * </div> 222 * </td> 223 * <!--insertBeforeStart--> 224 * <td style="white-space:nowrap"> 225 * <ul style="color: red;"> 226 * <li>List Item</li> 227 * </ul> 228 * <div style="background-color: silver;"> 229 * <p>Paragraph 1</p> 230 * <p>Paragraph 2</p> 231 * </div> 232 * </td> 233 * <!--insertAfterEnd--> 234 * <td style="white-space:nowrap"> 235 * <div style="background-color: silver;"> 236 * <p>Paragraph 1</p> 237 * <p>Paragraph 2</p> 238 * </div> 239 * <ul style="color: red;"> 240 * <li>List Item</li> 241 * </ul> 242 * </td> 243 * <!--setInnerHTML--> 244 * <td style="white-space:nowrap"> 245 * <div style="background-color: silver;"> 246 * <ul style="color: red;"> 247 * <li>List Item</li> 248 * </ul> 249 * </div> 250 * </td> 251 * <!--setOuterHTML--> 252 * <td style="white-space:nowrap"> 253 * <ul style="color: red;"> 254 * <li>List Item</li> 255 * </ul> 256 * </td> 257 * </tr> 258 * </table> 259 * 260 * <p><strong>Warning:</strong> Serialized objects of this class will 261 * not be compatible with future Swing releases. The current 262 * serialization support is appropriate for short term storage or RMI 263 * between applications running the same version of Swing. As of 1.4, 264 * support for long term storage of all JavaBeans™ 265 * has been added to the 266 * <code>java.beans</code> package. Please see {@link 267 * java.beans.XMLEncoder}.</p> 268 * 269 * @author Timothy Prinzing 270 * @author Scott Violet 271 * @author Sunita Mani 272 */ 273 public class HTMLDocument extends DefaultStyledDocument { 274 /** 275 * Constructs an HTML document using the default buffer size 276 * and a default <code>StyleSheet</code>. This is a convenience 277 * method for the constructor 278 * <code>HTMLDocument(Content, StyleSheet)</code>. 279 */ 280 public HTMLDocument() { 281 this(new GapContent(BUFFER_SIZE_DEFAULT), new StyleSheet()); 282 } 283 284 /** 285 * Constructs an HTML document with the default content 286 * storage implementation and the specified style/attribute 287 * storage mechanism. This is a convenience method for the 288 * constructor 289 * <code>HTMLDocument(Content, StyleSheet)</code>. 290 * 291 * @param styles the styles 292 */ 293 public HTMLDocument(StyleSheet styles) { 294 this(new GapContent(BUFFER_SIZE_DEFAULT), styles); 295 } 296 297 /** 298 * Constructs an HTML document with the given content 299 * storage implementation and the given style/attribute 300 * storage mechanism. 301 * 302 * @param c the container for the content 303 * @param styles the styles 304 */ 305 public HTMLDocument(Content c, StyleSheet styles) { 306 super(c, styles); 307 } 308 309 /** 310 * Fetches the reader for the parser to use when loading the document 311 * with HTML. This is implemented to return an instance of 312 * <code>HTMLDocument.HTMLReader</code>. 313 * Subclasses can reimplement this 314 * method to change how the document gets structured if desired. 315 * (For example, to handle custom tags, or structurally represent character 316 * style elements.) 317 * 318 * @param pos the starting position 319 * @return the reader used by the parser to load the document 320 */ 321 public HTMLEditorKit.ParserCallback getReader(int pos) { 322 Object desc = getProperty(Document.StreamDescriptionProperty); 323 if (desc instanceof URL) { 324 setBase((URL)desc); 325 } 326 HTMLReader reader = new HTMLReader(pos); 327 return reader; 328 } 329 330 /** 331 * Returns the reader for the parser to use to load the document 332 * with HTML. This is implemented to return an instance of 333 * <code>HTMLDocument.HTMLReader</code>. 334 * Subclasses can reimplement this 335 * method to change how the document gets structured if desired. 336 * (For example, to handle custom tags, or structurally represent character 337 * style elements.) 338 * <p>This is a convenience method for 339 * <code>getReader(int, int, int, HTML.Tag, TRUE)</code>. 340 * 341 * @param popDepth the number of <code>ElementSpec.EndTagTypes</code> 342 * to generate before inserting 343 * @param pushDepth the number of <code>ElementSpec.StartTagTypes</code> 344 * with a direction of <code>ElementSpec.JoinNextDirection</code> 345 * that should be generated before inserting, 346 * but after the end tags have been generated 347 * @param insertTag the first tag to start inserting into document 348 * @return the reader used by the parser to load the document 349 */ 350 public HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, 351 int pushDepth, 352 HTML.Tag insertTag) { 353 return getReader(pos, popDepth, pushDepth, insertTag, true); 354 } 355 356 /** 357 * Fetches the reader for the parser to use to load the document 358 * with HTML. This is implemented to return an instance of 359 * HTMLDocument.HTMLReader. Subclasses can reimplement this 360 * method to change how the document get structured if desired 361 * (e.g. to handle custom tags, structurally represent character 362 * style elements, etc.). 363 * 364 * @param popDepth the number of <code>ElementSpec.EndTagTypes</code> 365 * to generate before inserting 366 * @param pushDepth the number of <code>ElementSpec.StartTagTypes</code> 367 * with a direction of <code>ElementSpec.JoinNextDirection</code> 368 * that should be generated before inserting, 369 * but after the end tags have been generated 370 * @param insertTag the first tag to start inserting into document 371 * @param insertInsertTag false if all the Elements after insertTag should 372 * be inserted; otherwise insertTag will be inserted 373 * @return the reader used by the parser to load the document 374 */ 375 HTMLEditorKit.ParserCallback getReader(int pos, int popDepth, 376 int pushDepth, 377 HTML.Tag insertTag, 378 boolean insertInsertTag) { 379 Object desc = getProperty(Document.StreamDescriptionProperty); 380 if (desc instanceof URL) { 381 setBase((URL)desc); 382 } 383 HTMLReader reader = new HTMLReader(pos, popDepth, pushDepth, 384 insertTag, insertInsertTag, false, 385 true); 386 return reader; 387 } 388 389 /** 390 * Returns the location to resolve relative URLs against. By 391 * default this will be the document's URL if the document 392 * was loaded from a URL. If a base tag is found and 393 * can be parsed, it will be used as the base location. 394 * 395 * @return the base location 396 */ 397 public URL getBase() { 398 return base; 399 } 400 401 /** 402 * Sets the location to resolve relative URLs against. By 403 * default this will be the document's URL if the document 404 * was loaded from a URL. If a base tag is found and 405 * can be parsed, it will be used as the base location. 406 * <p>This also sets the base of the <code>StyleSheet</code> 407 * to be <code>u</code> as well as the base of the document. 408 * 409 * @param u the desired base URL 410 */ 411 public void setBase(URL u) { 412 base = u; 413 getStyleSheet().setBase(u); 414 } 415 416 /** 417 * Inserts new elements in bulk. This is how elements get created 418 * in the document. The parsing determines what structure is needed 419 * and creates the specification as a set of tokens that describe the 420 * edit while leaving the document free of a write-lock. This method 421 * can then be called in bursts by the reader to acquire a write-lock 422 * for a shorter duration (i.e. while the document is actually being 423 * altered). 424 * 425 * @param offset the starting offset 426 * @param data the element data 427 * @exception BadLocationException if the given position does not 428 * represent a valid location in the associated document. 429 */ 430 protected void insert(int offset, ElementSpec[] data) throws BadLocationException { 431 super.insert(offset, data); 432 } 433 434 /** 435 * Updates document structure as a result of text insertion. This 436 * will happen within a write lock. This implementation simply 437 * parses the inserted content for line breaks and builds up a set 438 * of instructions for the element buffer. 439 * 440 * @param chng a description of the document change 441 * @param attr the attributes 442 */ 443 protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) { 444 if(attr == null) { 445 attr = contentAttributeSet; 446 } 447 448 // If this is the composed text element, merge the content attribute to it 449 else if (attr.isDefined(StyleConstants.ComposedTextAttribute)) { 450 ((MutableAttributeSet)attr).addAttributes(contentAttributeSet); 451 } 452 453 if (attr.isDefined(IMPLIED_CR)) { 454 ((MutableAttributeSet)attr).removeAttribute(IMPLIED_CR); 455 } 456 457 super.insertUpdate(chng, attr); 458 } 459 460 /** 461 * Replaces the contents of the document with the given 462 * element specifications. This is called before insert if 463 * the loading is done in bursts. This is the only method called 464 * if loading the document entirely in one burst. 465 * 466 * @param data the new contents of the document 467 */ 468 protected void create(ElementSpec[] data) { 469 super.create(data); 470 } 471 472 /** 473 * Sets attributes for a paragraph. 474 * <p> 475 * This method is thread safe, although most Swing methods 476 * are not. Please see 477 * <A HREF="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html">Concurrency 478 * in Swing</A> for more information. 479 * 480 * @param offset the offset into the paragraph (must be at least 0) 481 * @param length the number of characters affected (must be at least 0) 482 * @param s the attributes 483 * @param replace whether to replace existing attributes, or merge them 484 */ 485 public void setParagraphAttributes(int offset, int length, AttributeSet s, 486 boolean replace) { 487 try { 488 writeLock(); 489 // Make sure we send out a change for the length of the paragraph. 490 int end = Math.min(offset + length, getLength()); 491 Element e = getParagraphElement(offset); 492 offset = e.getStartOffset(); 493 e = getParagraphElement(end); 494 length = Math.max(0, e.getEndOffset() - offset); 495 DefaultDocumentEvent changes = 496 new DefaultDocumentEvent(offset, length, 497 DocumentEvent.EventType.CHANGE); 498 AttributeSet sCopy = s.copyAttributes(); 499 int lastEnd = Integer.MAX_VALUE; 500 for (int pos = offset; pos <= end; pos = lastEnd) { 501 Element paragraph = getParagraphElement(pos); 502 if (lastEnd == paragraph.getEndOffset()) { 503 lastEnd++; 504 } 505 else { 506 lastEnd = paragraph.getEndOffset(); 507 } 508 MutableAttributeSet attr = 509 (MutableAttributeSet) paragraph.getAttributes(); 510 changes.addEdit(new AttributeUndoableEdit(paragraph, sCopy, replace)); 511 if (replace) { 512 attr.removeAttributes(attr); 513 } 514 attr.addAttributes(s); 515 } 516 changes.end(); 517 fireChangedUpdate(changes); 518 fireUndoableEditUpdate(new UndoableEditEvent(this, changes)); 519 } finally { 520 writeUnlock(); 521 } 522 } 523 524 /** 525 * Fetches the <code>StyleSheet</code> with the document-specific display 526 * rules (CSS) that were specified in the HTML document itself. 527 * 528 * @return the <code>StyleSheet</code> 529 */ 530 public StyleSheet getStyleSheet() { 531 return (StyleSheet) getAttributeContext(); 532 } 533 534 /** 535 * Fetches an iterator for the specified HTML tag. 536 * This can be used for things like iterating over the 537 * set of anchors contained, or iterating over the input 538 * elements. 539 * 540 * @param t the requested <code>HTML.Tag</code> 541 * @return the <code>Iterator</code> for the given HTML tag 542 * @see javax.swing.text.html.HTML.Tag 543 */ 544 public Iterator getIterator(HTML.Tag t) { 545 if (t.isBlock()) { 546 // TBD 547 return null; 548 } 549 return new LeafIterator(t, this); 550 } 551 552 /** 553 * Creates a document leaf element that directly represents 554 * text (doesn't have any children). This is implemented 555 * to return an element of type 556 * <code>HTMLDocument.RunElement</code>. 557 * 558 * @param parent the parent element 559 * @param a the attributes for the element 560 * @param p0 the beginning of the range (must be at least 0) 561 * @param p1 the end of the range (must be at least p0) 562 * @return the new element 563 */ 564 protected Element createLeafElement(Element parent, AttributeSet a, int p0, int p1) { 565 return new RunElement(parent, a, p0, p1); 566 } 567 568 /** 569 * Creates a document branch element, that can contain other elements. 570 * This is implemented to return an element of type 571 * <code>HTMLDocument.BlockElement</code>. 572 * 573 * @param parent the parent element 574 * @param a the attributes 575 * @return the element 576 */ 577 protected Element createBranchElement(Element parent, AttributeSet a) { 578 return new BlockElement(parent, a); 579 } 580 581 /** 582 * Creates the root element to be used to represent the 583 * default document structure. 584 * 585 * @return the element base 586 */ 587 protected AbstractElement createDefaultRoot() { 588 // grabs a write-lock for this initialization and 589 // abandon it during initialization so in normal 590 // operation we can detect an illegitimate attempt 591 // to mutate attributes. 592 writeLock(); 593 MutableAttributeSet a = new SimpleAttributeSet(); 594 a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.HTML); 595 BlockElement html = new BlockElement(null, a.copyAttributes()); 596 a.removeAttributes(a); 597 a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.BODY); 598 BlockElement body = new BlockElement(html, a.copyAttributes()); 599 a.removeAttributes(a); 600 a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.P); 601 getStyleSheet().addCSSAttributeFromHTML(a, CSS.Attribute.MARGIN_TOP, "0"); 602 BlockElement paragraph = new BlockElement(body, a.copyAttributes()); 603 a.removeAttributes(a); 604 a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); 605 RunElement brk = new RunElement(paragraph, a, 0, 1); 606 Element[] buff = new Element[1]; 607 buff[0] = brk; 608 paragraph.replace(0, 0, buff); 609 buff[0] = paragraph; 610 body.replace(0, 0, buff); 611 buff[0] = body; 612 html.replace(0, 0, buff); 613 writeUnlock(); 614 return html; 615 } 616 617 /** 618 * Sets the number of tokens to buffer before trying to update 619 * the documents element structure. 620 * 621 * @param n the number of tokens to buffer 622 */ 623 public void setTokenThreshold(int n) { 624 putProperty(TokenThreshold, new Integer(n)); 625 } 626 627 /** 628 * Gets the number of tokens to buffer before trying to update 629 * the documents element structure. The default value is 630 * <code>Integer.MAX_VALUE</code>. 631 * 632 * @return the number of tokens to buffer 633 */ 634 public int getTokenThreshold() { 635 Integer i = (Integer) getProperty(TokenThreshold); 636 if (i != null) { 637 return i.intValue(); 638 } 639 return Integer.MAX_VALUE; 640 } 641 642 /** 643 * Determines how unknown tags are handled by the parser. 644 * If set to true, unknown 645 * tags are put in the model, otherwise they are dropped. 646 * 647 * @param preservesTags true if unknown tags should be 648 * saved in the model, otherwise tags are dropped 649 * @see javax.swing.text.html.HTML.Tag 650 */ 651 public void setPreservesUnknownTags(boolean preservesTags) { 652 preservesUnknownTags = preservesTags; 653 } 654 655 /** 656 * Returns the behavior the parser observes when encountering 657 * unknown tags. 658 * 659 * @see javax.swing.text.html.HTML.Tag 660 * @return true if unknown tags are to be preserved when parsing 661 */ 662 public boolean getPreservesUnknownTags() { 663 return preservesUnknownTags; 664 } 665 666 /** 667 * Processes <code>HyperlinkEvents</code> that 668 * are generated by documents in an HTML frame. 669 * The <code>HyperlinkEvent</code> type, as the parameter suggests, 670 * is <code>HTMLFrameHyperlinkEvent</code>. 671 * In addition to the typical information contained in a 672 * <code>HyperlinkEvent</code>, 673 * this event contains the element that corresponds to the frame in 674 * which the click happened (the source element) and the 675 * target name. The target name has 4 possible values: 676 * <ul> 677 * <li> _self 678 * <li> _parent 679 * <li> _top 680 * <li> a named frame 681 * </ul> 682 * 683 * If target is _self, the action is to change the value of the 684 * <code>HTML.Attribute.SRC</code> attribute and fires a 685 * <code>ChangedUpdate</code> event. 686 *<p> 687 * If the target is _parent, then it deletes the parent element, 688 * which is a <FRAMESET> element, and inserts a new <FRAME> 689 * element, and sets its <code>HTML.Attribute.SRC</code> attribute 690 * to have a value equal to the destination URL and fire a 691 * <code>RemovedUpdate</code> and <code>InsertUpdate</code>. 692 *<p> 693 * If the target is _top, this method does nothing. In the implementation 694 * of the view for a frame, namely the <code>FrameView</code>, 695 * the processing of _top is handled. Given that _top implies 696 * replacing the entire document, it made sense to handle this outside 697 * of the document that it will replace. 698 *<p> 699 * If the target is a named frame, then the element hierarchy is searched 700 * for an element with a name equal to the target, its 701 * <code>HTML.Attribute.SRC</code> attribute is updated and a 702 * <code>ChangedUpdate</code> event is fired. 703 * 704 * @param e the event 705 */ 706 public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent e) { 707 String frameName = e.getTarget(); 708 Element element = e.getSourceElement(); 709 String urlStr = e.getURL().toString(); 710 711 if (frameName.equals("_self")) { 712 /* 713 The source and destination elements 714 are the same. 715 */ 716 updateFrame(element, urlStr); 717 } else if (frameName.equals("_parent")) { 718 /* 719 The destination is the parent of the frame. 720 */ 721 updateFrameSet(element.getParentElement(), urlStr); 722 } else { 723 /* 724 locate a named frame 725 */ 726 Element targetElement = findFrame(frameName); 727 if (targetElement != null) { 728 updateFrame(targetElement, urlStr); 729 } 730 } 731 } 732 733 734 /** 735 * Searches the element hierarchy for an FRAME element 736 * that has its name attribute equal to the <code>frameName</code>. 737 * 738 * @param frameName 739 * @return the element whose NAME attribute has a value of 740 * <code>frameName</code>; returns <code>null</code> 741 * if not found 742 */ 743 private Element findFrame(String frameName) { 744 ElementIterator it = new ElementIterator(this); 745 Element next; 746 747 while ((next = it.next()) != null) { 748 AttributeSet attr = next.getAttributes(); 749 if (matchNameAttribute(attr, HTML.Tag.FRAME)) { 750 String frameTarget = (String)attr.getAttribute(HTML.Attribute.NAME); 751 if (frameTarget != null && frameTarget.equals(frameName)) { 752 break; 753 } 754 } 755 } 756 return next; 757 } 758 759 /** 760 * Returns true if <code>StyleConstants.NameAttribute</code> is 761 * equal to the tag that is passed in as a parameter. 762 * 763 * @param attr the attributes to be matched 764 * @param tag the value to be matched 765 * @return true if there is a match, false otherwise 766 * @see javax.swing.text.html.HTML.Attribute 767 */ 768 static boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag) { 769 Object o = attr.getAttribute(StyleConstants.NameAttribute); 770 if (o instanceof HTML.Tag) { 771 HTML.Tag name = (HTML.Tag) o; 772 if (name == tag) { 773 return true; 774 } 775 } 776 return false; 777 } 778 779 /** 780 * Replaces a frameset branch Element with a frame leaf element. 781 * 782 * @param element the frameset element to remove 783 * @param url the value for the SRC attribute for the 784 * new frame that will replace the frameset 785 */ 786 private void updateFrameSet(Element element, String url) { 787 try { 788 int startOffset = element.getStartOffset(); 789 int endOffset = Math.min(getLength(), element.getEndOffset()); 790 String html = "<frame"; 791 if (url != null) { 792 html += " src=\"" + url + "\""; 793 } 794 html += ">"; 795 installParserIfNecessary(); 796 setOuterHTML(element, html); 797 } catch (BadLocationException e1) { 798 // Should handle this better 799 } catch (IOException ioe) { 800 // Should handle this better 801 } 802 } 803 804 805 /** 806 * Updates the Frame elements <code>HTML.Attribute.SRC attribute</code> 807 * and fires a <code>ChangedUpdate</code> event. 808 * 809 * @param element a FRAME element whose SRC attribute will be updated 810 * @param url a string specifying the new value for the SRC attribute 811 */ 812 private void updateFrame(Element element, String url) { 813 814 try { 815 writeLock(); 816 DefaultDocumentEvent changes = new DefaultDocumentEvent(element.getStartOffset(), 817 1, 818 DocumentEvent.EventType.CHANGE); 819 AttributeSet sCopy = element.getAttributes().copyAttributes(); 820 MutableAttributeSet attr = (MutableAttributeSet) element.getAttributes(); 821 changes.addEdit(new AttributeUndoableEdit(element, sCopy, false)); 822 attr.removeAttribute(HTML.Attribute.SRC); 823 attr.addAttribute(HTML.Attribute.SRC, url); 824 changes.end(); 825 fireChangedUpdate(changes); 826 fireUndoableEditUpdate(new UndoableEditEvent(this, changes)); 827 } finally { 828 writeUnlock(); 829 } 830 } 831 832 833 /** 834 * Returns true if the document will be viewed in a frame. 835 * @return true if document will be viewed in a frame, otherwise false 836 */ 837 boolean isFrameDocument() { 838 return frameDocument; 839 } 840 841 /** 842 * Sets a boolean state about whether the document will be 843 * viewed in a frame. 844 * @param frameDoc true if the document will be viewed in a frame, 845 * otherwise false 846 */ 847 void setFrameDocumentState(boolean frameDoc) { 848 this.frameDocument = frameDoc; 849 } 850 851 /** 852 * Adds the specified map, this will remove a Map that has been 853 * previously registered with the same name. 854 * 855 * @param map the <code>Map</code> to be registered 856 */ 857 void addMap(Map map) { 858 String name = map.getName(); 859 860 if (name != null) { 861 Object maps = getProperty(MAP_PROPERTY); 862 863 if (maps == null) { 864 maps = new Hashtable(11); 865 putProperty(MAP_PROPERTY, maps); 866 } 867 if (maps instanceof Hashtable) { 868 ((Hashtable)maps).put("#" + name, map); 869 } 870 } 871 } 872 873 /** 874 * Removes a previously registered map. 875 * @param map the <code>Map</code> to be removed 876 */ 877 void removeMap(Map map) { 878 String name = map.getName(); 879 880 if (name != null) { 881 Object maps = getProperty(MAP_PROPERTY); 882 883 if (maps instanceof Hashtable) { 884 ((Hashtable)maps).remove("#" + name); 885 } 886 } 887 } 888 889 /** 890 * Returns the Map associated with the given name. 891 * @param name the name of the desired <code>Map</code> 892 * @return the <code>Map</code> or <code>null</code> if it can't 893 * be found, or if <code>name</code> is <code>null</code> 894 */ 895 Map getMap(String name) { 896 if (name != null) { 897 Object maps = getProperty(MAP_PROPERTY); 898 899 if (maps != null && (maps instanceof Hashtable)) { 900 return (Map)((Hashtable)maps).get(name); 901 } 902 } 903 return null; 904 } 905 906 /** 907 * Returns an <code>Enumeration</code> of the possible Maps. 908 * @return the enumerated list of maps, or <code>null</code> 909 * if the maps are not an instance of <code>Hashtable</code> 910 */ 911 Enumeration getMaps() { 912 Object maps = getProperty(MAP_PROPERTY); 913 914 if (maps instanceof Hashtable) { 915 return ((Hashtable)maps).elements(); 916 } 917 return null; 918 } 919 920 /** 921 * Sets the content type language used for style sheets that do not 922 * explicitly specify the type. The default is text/css. 923 * @param contentType the content type language for the style sheets 924 */ 925 /* public */ 926 void setDefaultStyleSheetType(String contentType) { 927 putProperty(StyleType, contentType); 928 } 929 930 /** 931 * Returns the content type language used for style sheets. The default 932 * is text/css. 933 * @return the content type language used for the style sheets 934 */ 935 /* public */ 936 String getDefaultStyleSheetType() { 937 String retValue = (String)getProperty(StyleType); 938 if (retValue == null) { 939 return "text/css"; 940 } 941 return retValue; 942 } 943 944 /** 945 * Sets the parser that is used by the methods that insert html 946 * into the existing document, such as <code>setInnerHTML</code>, 947 * and <code>setOuterHTML</code>. 948 * <p> 949 * <code>HTMLEditorKit.createDefaultDocument</code> will set the parser 950 * for you. If you create an <code>HTMLDocument</code> by hand, 951 * be sure and set the parser accordingly. 952 * @param parser the parser to be used for text insertion 953 * 954 * @since 1.3 955 */ 956 public void setParser(HTMLEditorKit.Parser parser) { 957 this.parser = parser; 958 putProperty("__PARSER__", null); 959 } 960 961 /** 962 * Returns the parser that is used when inserting HTML into the existing 963 * document. 964 * @return the parser used for text insertion 965 * 966 * @since 1.3 967 */ 968 public HTMLEditorKit.Parser getParser() { 969 Object p = getProperty("__PARSER__"); 970 971 if (p instanceof HTMLEditorKit.Parser) { 972 return (HTMLEditorKit.Parser)p; 973 } 974 return parser; 975 } 976 977 /** 978 * Replaces the children of the given element with the contents 979 * specified as an HTML string. 980 * 981 * <p>This will be seen as at least two events, n inserts followed by 982 * a remove.</p> 983 * 984 * <p>Consider the following structure (the <code>elem</code> 985 * parameter is <b>in bold</b>).</p> 986 * 987 * <pre> 988 * <body> 989 * | 990 * <b><div></b> 991 * / \ 992 * <p> <p> 993 * </pre> 994 * 995 * <p>Invoking <code>setInnerHTML(elem, "<ul><li>")</code> 996 * results in the following structure (new elements are <font 997 * style="color: red;">in red</font>).</p> 998 * 999 * <pre> 1000 * <body> 1001 * | 1002 * <b><div></b> 1003 * \ 1004 * <font style="color: red;"><ul></font> 1005 * \ 1006 * <font style="color: red;"><li></font> 1007 * </pre> 1008 * 1009 * <p>Parameter <code>elem</code> must not be a leaf element, 1010 * otherwise an <code>IllegalArgumentException</code> is thrown. 1011 * If either <code>elem</code> or <code>htmlText</code> parameter 1012 * is <code>null</code>, no changes are made to the document.</p> 1013 * 1014 * <p>For this to work correctly, the document must have an 1015 * <code>HTMLEditorKit.Parser</code> set. This will be the case 1016 * if the document was created from an HTMLEditorKit via the 1017 * <code>createDefaultDocument</code> method.</p> 1018 * 1019 * @param elem the branch element whose children will be replaced 1020 * @param htmlText the string to be parsed and assigned to <code>elem</code> 1021 * @throws IllegalArgumentException if <code>elem</code> is a leaf 1022 * @throws IllegalStateException if an <code>HTMLEditorKit.Parser</code> 1023 * has not been defined 1024 * @since 1.3 1025 */ 1026 public void setInnerHTML(Element elem, String htmlText) throws 1027 BadLocationException, IOException { 1028 verifyParser(); 1029 if (elem != null && elem.isLeaf()) { 1030 throw new IllegalArgumentException 1031 ("Can not set inner HTML of a leaf"); 1032 } 1033 if (elem != null && htmlText != null) { 1034 int oldCount = elem.getElementCount(); 1035 int insertPosition = elem.getStartOffset(); 1036 insertHTML(elem, elem.getStartOffset(), htmlText, true); 1037 if (elem.getElementCount() > oldCount) { 1038 // Elements were inserted, do the cleanup. 1039 removeElements(elem, elem.getElementCount() - oldCount, 1040 oldCount); 1041 } 1042 } 1043 } 1044 1045 /** 1046 * Replaces the given element in the parent with the contents 1047 * specified as an HTML string. 1048 * 1049 * <p>This will be seen as at least two events, n inserts followed by 1050 * a remove.</p> 1051 * 1052 * <p>When replacing a leaf this will attempt to make sure there is 1053 * a newline present if one is needed. This may result in an additional 1054 * element being inserted. Consider, if you were to replace a character 1055 * element that contained a newline with <img> this would create 1056 * two elements, one for the image, and one for the newline.</p> 1057 * 1058 * <p>If you try to replace the element at length you will most 1059 * likely end up with two elements, eg 1060 * <code>setOuterHTML(getCharacterElement (getLength()), 1061 * "blah")</code> will result in two leaf elements at the end, one 1062 * representing 'blah', and the other representing the end 1063 * element.</p> 1064 * 1065 * <p>Consider the following structure (the <code>elem</code> 1066 * parameter is <b>in bold</b>).</p> 1067 * 1068 * <pre> 1069 * <body> 1070 * | 1071 * <b><div></b> 1072 * / \ 1073 * <p> <p> 1074 * </pre> 1075 * 1076 * <p>Invoking <code>setOuterHTML(elem, "<ul><li>")</code> 1077 * results in the following structure (new elements are <font 1078 * style="color: red;">in red</font>).</p> 1079 * 1080 * <pre> 1081 * <body> 1082 * | 1083 * <font style="color: red;"><ul></font> 1084 * \ 1085 * <font style="color: red;"><li></font> 1086 * </pre> 1087 * 1088 * <p>If either <code>elem</code> or <code>htmlText</code> 1089 * parameter is <code>null</code>, no changes are made to the 1090 * document.</p> 1091 * 1092 * <p>For this to work correctly, the document must have an 1093 * HTMLEditorKit.Parser set. This will be the case if the document 1094 * was created from an HTMLEditorKit via the 1095 * <code>createDefaultDocument</code> method.</p> 1096 * 1097 * @param elem the element to replace 1098 * @param htmlText the string to be parsed and inserted in place of <code>elem</code> 1099 * @throws IllegalStateException if an HTMLEditorKit.Parser has not 1100 * been set 1101 * @since 1.3 1102 */ 1103 public void setOuterHTML(Element elem, String htmlText) throws 1104 BadLocationException, IOException { 1105 verifyParser(); 1106 if (elem != null && elem.getParentElement() != null && 1107 htmlText != null) { 1108 int start = elem.getStartOffset(); 1109 int end = elem.getEndOffset(); 1110 int startLength = getLength(); 1111 // We don't want a newline if elem is a leaf, and doesn't contain 1112 // a newline. 1113 boolean wantsNewline = !elem.isLeaf(); 1114 if (!wantsNewline && (end > startLength || 1115 getText(end - 1, 1).charAt(0) == NEWLINE[0])){ 1116 wantsNewline = true; 1117 } 1118 Element parent = elem.getParentElement(); 1119 int oldCount = parent.getElementCount(); 1120 insertHTML(parent, start, htmlText, wantsNewline); 1121 // Remove old. 1122 int newLength = getLength(); 1123 if (oldCount != parent.getElementCount()) { 1124 int removeIndex = parent.getElementIndex(start + newLength - 1125 startLength); 1126 removeElements(parent, removeIndex, 1); 1127 } 1128 } 1129 } 1130 1131 /** 1132 * Inserts the HTML specified as a string at the start 1133 * of the element. 1134 * 1135 * <p>Consider the following structure (the <code>elem</code> 1136 * parameter is <b>in bold</b>).</p> 1137 * 1138 * <pre> 1139 * <body> 1140 * | 1141 * <b><div></b> 1142 * / \ 1143 * <p> <p> 1144 * </pre> 1145 * 1146 * <p>Invoking <code>insertAfterStart(elem, 1147 * "<ul><li>")</code> results in the following structure 1148 * (new elements are <font style="color: red;">in red</font>).</p> 1149 * 1150 * <pre> 1151 * <body> 1152 * | 1153 * <b><div></b> 1154 * / | \ 1155 * <font style="color: red;"><ul></font> <p> <p> 1156 * / 1157 * <font style="color: red;"><li></font> 1158 * </pre> 1159 * 1160 * <p>Unlike the <code>insertBeforeStart</code> method, new 1161 * elements become <em>children</em> of the specified element, 1162 * not siblings.</p> 1163 * 1164 * <p>Parameter <code>elem</code> must not be a leaf element, 1165 * otherwise an <code>IllegalArgumentException</code> is thrown. 1166 * If either <code>elem</code> or <code>htmlText</code> parameter 1167 * is <code>null</code>, no changes are made to the document.</p> 1168 * 1169 * <p>For this to work correctly, the document must have an 1170 * <code>HTMLEditorKit.Parser</code> set. This will be the case 1171 * if the document was created from an HTMLEditorKit via the 1172 * <code>createDefaultDocument</code> method.</p> 1173 * 1174 * @param elem the branch element to be the root for the new text 1175 * @param htmlText the string to be parsed and assigned to <code>elem</code> 1176 * @throws IllegalArgumentException if <code>elem</code> is a leaf 1177 * @throws IllegalStateException if an HTMLEditorKit.Parser has not 1178 * been set on the document 1179 * @since 1.3 1180 */ 1181 public void insertAfterStart(Element elem, String htmlText) throws 1182 BadLocationException, IOException { 1183 verifyParser(); 1184 1185 if (elem == null || htmlText == null) { 1186 return; 1187 } 1188 1189 if (elem.isLeaf()) { 1190 throw new IllegalArgumentException 1191 ("Can not insert HTML after start of a leaf"); 1192 } 1193 insertHTML(elem, elem.getStartOffset(), htmlText, false); 1194 } 1195 1196 /** 1197 * Inserts the HTML specified as a string at the end of 1198 * the element. 1199 * 1200 * <p> If <code>elem</code>'s children are leaves, and the 1201 * character at a <code>elem.getEndOffset() - 1</code> is a newline, 1202 * this will insert before the newline so that there isn't text after 1203 * the newline.</p> 1204 * 1205 * <p>Consider the following structure (the <code>elem</code> 1206 * parameter is <b>in bold</b>).</p> 1207 * 1208 * <pre> 1209 * <body> 1210 * | 1211 * <b><div></b> 1212 * / \ 1213 * <p> <p> 1214 * </pre> 1215 * 1216 * <p>Invoking <code>insertBeforeEnd(elem, "<ul><li>")</code> 1217 * results in the following structure (new elements are <font 1218 * style="color: red;">in red</font>).</p> 1219 * 1220 * <pre> 1221 * <body> 1222 * | 1223 * <b><div></b> 1224 * / | \ 1225 * <p> <p> <font style="color: red;"><ul></font> 1226 * \ 1227 * <font style="color: red;"><li></font> 1228 * </pre> 1229 * 1230 * <p>Unlike the <code>insertAfterEnd</code> method, new elements 1231 * become <em>children</em> of the specified element, not 1232 * siblings.</p> 1233 * 1234 * <p>Parameter <code>elem</code> must not be a leaf element, 1235 * otherwise an <code>IllegalArgumentException</code> is thrown. 1236 * If either <code>elem</code> or <code>htmlText</code> parameter 1237 * is <code>null</code>, no changes are made to the document.</p> 1238 * 1239 * <p>For this to work correctly, the document must have an 1240 * <code>HTMLEditorKit.Parser</code> set. This will be the case 1241 * if the document was created from an HTMLEditorKit via the 1242 * <code>createDefaultDocument</code> method.</p> 1243 * 1244 * @param elem the element to be the root for the new text 1245 * @param htmlText the string to be parsed and assigned to <code>elem</code> 1246 * @throws IllegalArgumentException if <code>elem</code> is a leaf 1247 * @throws IllegalStateException if an HTMLEditorKit.Parser has not 1248 * been set on the document 1249 * @since 1.3 1250 */ 1251 public void insertBeforeEnd(Element elem, String htmlText) throws 1252 BadLocationException, IOException { 1253 verifyParser(); 1254 if (elem != null && elem.isLeaf()) { 1255 throw new IllegalArgumentException 1256 ("Can not set inner HTML before end of leaf"); 1257 } 1258 if (elem != null) { 1259 int offset = elem.getEndOffset(); 1260 if (elem.getElement(elem.getElementIndex(offset - 1)).isLeaf() && 1261 getText(offset - 1, 1).charAt(0) == NEWLINE[0]) { 1262 offset--; 1263 } 1264 insertHTML(elem, offset, htmlText, false); 1265 } 1266 } 1267 1268 /** 1269 * Inserts the HTML specified as a string before the start of 1270 * the given element. 1271 * 1272 * <p>Consider the following structure (the <code>elem</code> 1273 * parameter is <b>in bold</b>).</p> 1274 * 1275 * <pre> 1276 * <body> 1277 * | 1278 * <b><div></b> 1279 * / \ 1280 * <p> <p> 1281 * </pre> 1282 * 1283 * <p>Invoking <code>insertBeforeStart(elem, 1284 * "<ul><li>")</code> results in the following structure 1285 * (new elements are <font style="color: red;">in red</font>).</p> 1286 * 1287 * <pre> 1288 * <body> 1289 * / \ 1290 * <font style="color: red;"><ul></font> <b><div></b> 1291 * / / \ 1292 * <font style="color: red;"><li></font> <p> <p> 1293 * </pre> 1294 * 1295 * <p>Unlike the <code>insertAfterStart</code> method, new 1296 * elements become <em>siblings</em> of the specified element, not 1297 * children.</p> 1298 * 1299 * <p>If either <code>elem</code> or <code>htmlText</code> 1300 * parameter is <code>null</code>, no changes are made to the 1301 * document.</p> 1302 * 1303 * <p>For this to work correctly, the document must have an 1304 * <code>HTMLEditorKit.Parser</code> set. This will be the case 1305 * if the document was created from an HTMLEditorKit via the 1306 * <code>createDefaultDocument</code> method.</p> 1307 * 1308 * @param elem the element the content is inserted before 1309 * @param htmlText the string to be parsed and inserted before <code>elem</code> 1310 * @throws IllegalStateException if an HTMLEditorKit.Parser has not 1311 * been set on the document 1312 * @since 1.3 1313 */ 1314 public void insertBeforeStart(Element elem, String htmlText) throws 1315 BadLocationException, IOException { 1316 verifyParser(); 1317 if (elem != null) { 1318 Element parent = elem.getParentElement(); 1319 1320 if (parent != null) { 1321 insertHTML(parent, elem.getStartOffset(), htmlText, false); 1322 } 1323 } 1324 } 1325 1326 /** 1327 * Inserts the HTML specified as a string after the the end of the 1328 * given element. 1329 * 1330 * <p>Consider the following structure (the <code>elem</code> 1331 * parameter is <b>in bold</b>).</p> 1332 * 1333 * <pre> 1334 * <body> 1335 * | 1336 * <b><div></b> 1337 * / \ 1338 * <p> <p> 1339 * </pre> 1340 * 1341 * <p>Invoking <code>insertAfterEnd(elem, "<ul><li>")</code> 1342 * results in the following structure (new elements are <font 1343 * style="color: red;">in red</font>).</p> 1344 * 1345 * <pre> 1346 * <body> 1347 * / \ 1348 * <b><div></b> <font style="color: red;"><ul></font> 1349 * / \ \ 1350 * <p> <p> <font style="color: red;"><li></font> 1351 * </pre> 1352 * 1353 * <p>Unlike the <code>insertBeforeEnd</code> method, new elements 1354 * become <em>siblings</em> of the specified element, not 1355 * children.</p> 1356 * 1357 * <p>If either <code>elem</code> or <code>htmlText</code> 1358 * parameter is <code>null</code>, no changes are made to the 1359 * document.</p> 1360 * 1361 * <p>For this to work correctly, the document must have an 1362 * <code>HTMLEditorKit.Parser</code> set. This will be the case 1363 * if the document was created from an HTMLEditorKit via the 1364 * <code>createDefaultDocument</code> method.</p> 1365 * 1366 * @param elem the element the content is inserted after 1367 * @param htmlText the string to be parsed and inserted after <code>elem</code> 1368 * @throws IllegalStateException if an HTMLEditorKit.Parser has not 1369 * been set on the document 1370 * @since 1.3 1371 */ 1372 public void insertAfterEnd(Element elem, String htmlText) throws 1373 BadLocationException, IOException { 1374 verifyParser(); 1375 if (elem != null) { 1376 Element parent = elem.getParentElement(); 1377 1378 if (parent != null) { 1379 int offset = elem.getEndOffset(); 1380 if (offset > getLength()) { 1381 offset--; 1382 } 1383 else if (elem.isLeaf() && getText(offset - 1, 1). 1384 charAt(0) == NEWLINE[0]) { 1385 offset--; 1386 } 1387 insertHTML(parent, offset, htmlText, false); 1388 } 1389 } 1390 } 1391 1392 /** 1393 * Returns the element that has the given id <code>Attribute</code>. 1394 * If the element can't be found, <code>null</code> is returned. 1395 * Note that this method works on an <code>Attribute</code>, 1396 * <i>not</i> a character tag. In the following HTML snippet: 1397 * <code><a id="HelloThere"></code> the attribute is 1398 * 'id' and the character tag is 'a'. 1399 * This is a convenience method for 1400 * <code>getElement(RootElement, HTML.Attribute.id, id)</code>. 1401 * This is not thread-safe. 1402 * 1403 * @param id the string representing the desired <code>Attribute</code> 1404 * @return the element with the specified <code>Attribute</code> 1405 * or <code>null</code> if it can't be found, 1406 * or <code>null</code> if <code>id</code> is <code>null</code> 1407 * @see javax.swing.text.html.HTML.Attribute 1408 * @since 1.3 1409 */ 1410 public Element getElement(String id) { 1411 if (id == null) { 1412 return null; 1413 } 1414 return getElement(getDefaultRootElement(), HTML.Attribute.ID, id, 1415 true); 1416 } 1417 1418 /** 1419 * Returns the child element of <code>e</code> that contains the 1420 * attribute, <code>attribute</code> with value <code>value</code>, or 1421 * <code>null</code> if one isn't found. This is not thread-safe. 1422 * 1423 * @param e the root element where the search begins 1424 * @param attribute the desired <code>Attribute</code> 1425 * @param value the values for the specified <code>Attribute</code> 1426 * @return the element with the specified <code>Attribute</code> 1427 * and the specified <code>value</code>, or <code>null</code> 1428 * if it can't be found 1429 * @see javax.swing.text.html.HTML.Attribute 1430 * @since 1.3 1431 */ 1432 public Element getElement(Element e, Object attribute, Object value) { 1433 return getElement(e, attribute, value, true); 1434 } 1435 1436 /** 1437 * Returns the child element of <code>e</code> that contains the 1438 * attribute, <code>attribute</code> with value <code>value</code>, or 1439 * <code>null</code> if one isn't found. This is not thread-safe. 1440 * <p> 1441 * If <code>searchLeafAttributes</code> is true, and <code>e</code> is 1442 * a leaf, any attributes that are instances of <code>HTML.Tag</code> 1443 * with a value that is an <code>AttributeSet</code> will also be checked. 1444 * 1445 * @param e the root element where the search begins 1446 * @param attribute the desired <code>Attribute</code> 1447 * @param value the values for the specified <code>Attribute</code> 1448 * @return the element with the specified <code>Attribute</code> 1449 * and the specified <code>value</code>, or <code>null</code> 1450 * if it can't be found 1451 * @see javax.swing.text.html.HTML.Attribute 1452 */ 1453 private Element getElement(Element e, Object attribute, Object value, 1454 boolean searchLeafAttributes) { 1455 AttributeSet attr = e.getAttributes(); 1456 1457 if (attr != null && attr.isDefined(attribute)) { 1458 if (value.equals(attr.getAttribute(attribute))) { 1459 return e; 1460 } 1461 } 1462 if (!e.isLeaf()) { 1463 for (int counter = 0, maxCounter = e.getElementCount(); 1464 counter < maxCounter; counter++) { 1465 Element retValue = getElement(e.getElement(counter), attribute, 1466 value, searchLeafAttributes); 1467 1468 if (retValue != null) { 1469 return retValue; 1470 } 1471 } 1472 } 1473 else if (searchLeafAttributes && attr != null) { 1474 // For some leaf elements we store the actual attributes inside 1475 // the AttributeSet of the Element (such as anchors). 1476 Enumeration names = attr.getAttributeNames(); 1477 if (names != null) { 1478 while (names.hasMoreElements()) { 1479 Object name = names.nextElement(); 1480 if ((name instanceof HTML.Tag) && 1481 (attr.getAttribute(name) instanceof AttributeSet)) { 1482 1483 AttributeSet check = (AttributeSet)attr. 1484 getAttribute(name); 1485 if (check.isDefined(attribute) && 1486 value.equals(check.getAttribute(attribute))) { 1487 return e; 1488 } 1489 } 1490 } 1491 } 1492 } 1493 return null; 1494 } 1495 1496 /** 1497 * Verifies the document has an <code>HTMLEditorKit.Parser</code> set. 1498 * If <code>getParser</code> returns <code>null</code>, this will throw an 1499 * IllegalStateException. 1500 * 1501 * @throws IllegalStateException if the document does not have a Parser 1502 */ 1503 private void verifyParser() { 1504 if (getParser() == null) { 1505 throw new IllegalStateException("No HTMLEditorKit.Parser"); 1506 } 1507 } 1508 1509 /** 1510 * Installs a default Parser if one has not been installed yet. 1511 */ 1512 private void installParserIfNecessary() { 1513 if (getParser() == null) { 1514 setParser(new HTMLEditorKit().getParser()); 1515 } 1516 } 1517 1518 /** 1519 * Inserts a string of HTML into the document at the given position. 1520 * <code>parent</code> is used to identify the location to insert the 1521 * <code>html</code>. If <code>parent</code> is a leaf this can have 1522 * unexpected results. 1523 */ 1524 private void insertHTML(Element parent, int offset, String html, 1525 boolean wantsTrailingNewline) 1526 throws BadLocationException, IOException { 1527 if (parent != null && html != null) { 1528 HTMLEditorKit.Parser parser = getParser(); 1529 if (parser != null) { 1530 int lastOffset = Math.max(0, offset - 1); 1531 Element charElement = getCharacterElement(lastOffset); 1532 Element commonParent = parent; 1533 int pop = 0; 1534 int push = 0; 1535 1536 if (parent.getStartOffset() > lastOffset) { 1537 while (commonParent != null && 1538 commonParent.getStartOffset() > lastOffset) { 1539 commonParent = commonParent.getParentElement(); 1540 push++; 1541 } 1542 if (commonParent == null) { 1543 throw new BadLocationException("No common parent", 1544 offset); 1545 } 1546 } 1547 while (charElement != null && charElement != commonParent) { 1548 pop++; 1549 charElement = charElement.getParentElement(); 1550 } 1551 if (charElement != null) { 1552 // Found it, do the insert. 1553 HTMLReader reader = new HTMLReader(offset, pop - 1, push, 1554 null, false, true, 1555 wantsTrailingNewline); 1556 1557 parser.parse(new StringReader(html), reader, true); 1558 reader.flush(); 1559 } 1560 } 1561 } 1562 } 1563 1564 /** 1565 * Removes child Elements of the passed in Element <code>e</code>. This 1566 * will do the necessary cleanup to ensure the element representing the 1567 * end character is correctly created. 1568 * <p>This is not a general purpose method, it assumes that <code>e</code> 1569 * will still have at least one child after the remove, and it assumes 1570 * the character at <code>e.getStartOffset() - 1</code> is a newline and 1571 * is of length 1. 1572 */ 1573 private void removeElements(Element e, int index, int count) throws BadLocationException { 1574 writeLock(); 1575 try { 1576 int start = e.getElement(index).getStartOffset(); 1577 int end = e.getElement(index + count - 1).getEndOffset(); 1578 if (end > getLength()) { 1579 removeElementsAtEnd(e, index, count, start, end); 1580 } 1581 else { 1582 removeElements(e, index, count, start, end); 1583 } 1584 } finally { 1585 writeUnlock(); 1586 } 1587 } 1588 1589 /** 1590 * Called to remove child elements of <code>e</code> when one of the 1591 * elements to remove is representing the end character. 1592 * <p>Since the Content will not allow a removal to the end character 1593 * this will do a remove from <code>start - 1</code> to <code>end</code>. 1594 * The end Element(s) will be removed, and the element representing 1595 * <code>start - 1</code> to <code>start</code> will be recreated. This 1596 * Element has to be recreated as after the content removal its offsets 1597 * become <code>start - 1</code> to <code>start - 1</code>. 1598 */ 1599 private void removeElementsAtEnd(Element e, int index, int count, 1600 int start, int end) throws BadLocationException { 1601 // index must be > 0 otherwise no insert would have happened. 1602 boolean isLeaf = (e.getElement(index - 1).isLeaf()); 1603 DefaultDocumentEvent dde = new DefaultDocumentEvent( 1604 start - 1, end - start + 1, DocumentEvent. 1605 EventType.REMOVE); 1606 1607 if (isLeaf) { 1608 Element endE = getCharacterElement(getLength()); 1609 // e.getElement(index - 1) should represent the newline. 1610 index--; 1611 if (endE.getParentElement() != e) { 1612 // The hiearchies don't match, we'll have to manually 1613 // recreate the leaf at e.getElement(index - 1) 1614 replace(dde, e, index, ++count, start, end, true, true); 1615 } 1616 else { 1617 // The hierarchies for the end Element and 1618 // e.getElement(index - 1), match, we can safely remove 1619 // the Elements and the end content will be aligned 1620 // appropriately. 1621 replace(dde, e, index, count, start, end, true, false); 1622 } 1623 } 1624 else { 1625 // Not a leaf, descend until we find the leaf representing 1626 // start - 1 and remove it. 1627 Element newLineE = e.getElement(index - 1); 1628 while (!newLineE.isLeaf()) { 1629 newLineE = newLineE.getElement(newLineE.getElementCount() - 1); 1630 } 1631 newLineE = newLineE.getParentElement(); 1632 replace(dde, e, index, count, start, end, false, false); 1633 replace(dde, newLineE, newLineE.getElementCount() - 1, 1, start, 1634 end, true, true); 1635 } 1636 postRemoveUpdate(dde); 1637 dde.end(); 1638 fireRemoveUpdate(dde); 1639 fireUndoableEditUpdate(new UndoableEditEvent(this, dde)); 1640 } 1641 1642 /** 1643 * This is used by <code>removeElementsAtEnd</code>, it removes 1644 * <code>count</code> elements starting at <code>start</code> from 1645 * <code>e</code>. If <code>remove</code> is true text of length 1646 * <code>start - 1</code> to <code>end - 1</code> is removed. If 1647 * <code>create</code> is true a new leaf is created of length 1. 1648 */ 1649 private void replace(DefaultDocumentEvent dde, Element e, int index, 1650 int count, int start, int end, boolean remove, 1651 boolean create) throws BadLocationException { 1652 Element[] added; 1653 AttributeSet attrs = e.getElement(index).getAttributes(); 1654 Element[] removed = new Element[count]; 1655 1656 for (int counter = 0; counter < count; counter++) { 1657 removed[counter] = e.getElement(counter + index); 1658 } 1659 if (remove) { 1660 UndoableEdit u = getContent().remove(start - 1, end - start); 1661 if (u != null) { 1662 dde.addEdit(u); 1663 } 1664 } 1665 if (create) { 1666 added = new Element[1]; 1667 added[0] = createLeafElement(e, attrs, start - 1, start); 1668 } 1669 else { 1670 added = new Element[0]; 1671 } 1672 dde.addEdit(new ElementEdit(e, index, removed, added)); 1673 ((AbstractDocument.BranchElement)e).replace( 1674 index, removed.length, added); 1675 } 1676 1677 /** 1678 * Called to remove child Elements when the end is not touched. 1679 */ 1680 private void removeElements(Element e, int index, int count, 1681 int start, int end) throws BadLocationException { 1682 Element[] removed = new Element[count]; 1683 Element[] added = new Element[0]; 1684 for (int counter = 0; counter < count; counter++) { 1685 removed[counter] = e.getElement(counter + index); 1686 } 1687 DefaultDocumentEvent dde = new DefaultDocumentEvent 1688 (start, end - start, DocumentEvent.EventType.REMOVE); 1689 ((AbstractDocument.BranchElement)e).replace(index, removed.length, 1690 added); 1691 dde.addEdit(new ElementEdit(e, index, removed, added)); 1692 UndoableEdit u = getContent().remove(start, end - start); 1693 if (u != null) { 1694 dde.addEdit(u); 1695 } 1696 postRemoveUpdate(dde); 1697 dde.end(); 1698 fireRemoveUpdate(dde); 1699 if (u != null) { 1700 fireUndoableEditUpdate(new UndoableEditEvent(this, dde)); 1701 } 1702 } 1703 1704 1705 // These two are provided for inner class access. The are named different 1706 // than the super class as the super class implementations are final. 1707 void obtainLock() { 1708 writeLock(); 1709 } 1710 1711 void releaseLock() { 1712 writeUnlock(); 1713 } 1714 1715 // 1716 // Provided for inner class access. 1717 // 1718 1719 /** 1720 * Notifies all listeners that have registered interest for 1721 * notification on this event type. The event instance 1722 * is lazily created using the parameters passed into 1723 * the fire method. 1724 * 1725 * @param e the event 1726 * @see EventListenerList 1727 */ 1728 protected void fireChangedUpdate(DocumentEvent e) { 1729 super.fireChangedUpdate(e); 1730 } 1731 1732 /** 1733 * Notifies all listeners that have registered interest for 1734 * notification on this event type. The event instance 1735 * is lazily created using the parameters passed into 1736 * the fire method. 1737 * 1738 * @param e the event 1739 * @see EventListenerList 1740 */ 1741 protected void fireUndoableEditUpdate(UndoableEditEvent e) { 1742 super.fireUndoableEditUpdate(e); 1743 } 1744 1745 boolean hasBaseTag() { 1746 return hasBaseTag; 1747 } 1748 1749 String getBaseTarget() { 1750 return baseTarget; 1751 } 1752 1753 /* 1754 * state defines whether the document is a frame document 1755 * or not. 1756 */ 1757 private boolean frameDocument = false; 1758 private boolean preservesUnknownTags = true; 1759 1760 /* 1761 * Used to store button groups for radio buttons in 1762 * a form. 1763 */ 1764 private HashMap<String, ButtonGroup> radioButtonGroupsMap; 1765 1766 /** 1767 * Document property for the number of tokens to buffer 1768 * before building an element subtree to represent them. 1769 */ 1770 static final String TokenThreshold = "token threshold"; 1771 1772 private static final int MaxThreshold = 10000; 1773 1774 private static final int StepThreshold = 5; 1775 1776 1777 /** 1778 * Document property key value. The value for the key will be a Vector 1779 * of Strings that are comments not found in the body. 1780 */ 1781 public static final String AdditionalComments = "AdditionalComments"; 1782 1783 /** 1784 * Document property key value. The value for the key will be a 1785 * String indicating the default type of stylesheet links. 1786 */ 1787 /* public */ static final String StyleType = "StyleType"; 1788 1789 /** 1790 * The location to resolve relative URLs against. By 1791 * default this will be the document's URL if the document 1792 * was loaded from a URL. If a base tag is found and 1793 * can be parsed, it will be used as the base location. 1794 */ 1795 URL base; 1796 1797 /** 1798 * does the document have base tag 1799 */ 1800 boolean hasBaseTag = false; 1801 1802 /** 1803 * BASE tag's TARGET attribute value 1804 */ 1805 private String baseTarget = null; 1806 1807 /** 1808 * The parser that is used when inserting html into the existing 1809 * document. 1810 */ 1811 private HTMLEditorKit.Parser parser; 1812 1813 /** 1814 * Used for inserts when a null AttributeSet is supplied. 1815 */ 1816 private static AttributeSet contentAttributeSet; 1817 1818 /** 1819 * Property Maps are registered under, will be a Hashtable. 1820 */ 1821 static String MAP_PROPERTY = "__MAP__"; 1822 1823 private static char[] NEWLINE; 1824 1825 /** 1826 * I18N property key. 1827 * 1828 * @see AbstractDocument#I18NProperty 1829 */ 1830 private static final String I18NProperty = "i18n"; 1831 1832 static { 1833 contentAttributeSet = new SimpleAttributeSet(); 1834 ((MutableAttributeSet)contentAttributeSet). 1835 addAttribute(StyleConstants.NameAttribute, 1836 HTML.Tag.CONTENT); 1837 NEWLINE = new char[1]; 1838 NEWLINE[0] = '\n'; 1839 } 1840 1841 1842 /** 1843 * An iterator to iterate over a particular type of 1844 * tag. The iterator is not thread safe. If reliable 1845 * access to the document is not already ensured by 1846 * the context under which the iterator is being used, 1847 * its use should be performed under the protection of 1848 * Document.render. 1849 */ 1850 public static abstract class Iterator { 1851 1852 /** 1853 * Return the attributes for this tag. 1854 * @return the <code>AttributeSet</code> for this tag, or 1855 * <code>null</code> if none can be found 1856 */ 1857 public abstract AttributeSet getAttributes(); 1858 1859 /** 1860 * Returns the start of the range for which the current occurrence of 1861 * the tag is defined and has the same attributes. 1862 * 1863 * @return the start of the range, or -1 if it can't be found 1864 */ 1865 public abstract int getStartOffset(); 1866 1867 /** 1868 * Returns the end of the range for which the current occurrence of 1869 * the tag is defined and has the same attributes. 1870 * 1871 * @return the end of the range 1872 */ 1873 public abstract int getEndOffset(); 1874 1875 /** 1876 * Move the iterator forward to the next occurrence 1877 * of the tag it represents. 1878 */ 1879 public abstract void next(); 1880 1881 /** 1882 * Indicates if the iterator is currently 1883 * representing an occurrence of a tag. If 1884 * false there are no more tags for this iterator. 1885 * @return true if the iterator is currently representing an 1886 * occurrence of a tag, otherwise returns false 1887 */ 1888 public abstract boolean isValid(); 1889 1890 /** 1891 * Type of tag this iterator represents. 1892 */ 1893 public abstract HTML.Tag getTag(); 1894 } 1895 1896 /** 1897 * An iterator to iterate over a particular type of tag. 1898 */ 1899 static class LeafIterator extends Iterator { 1900 1901 LeafIterator(HTML.Tag t, Document doc) { 1902 tag = t; 1903 pos = new ElementIterator(doc); 1904 endOffset = 0; 1905 next(); 1906 } 1907 1908 /** 1909 * Returns the attributes for this tag. 1910 * @return the <code>AttributeSet</code> for this tag, 1911 * or <code>null</code> if none can be found 1912 */ 1913 public AttributeSet getAttributes() { 1914 Element elem = pos.current(); 1915 if (elem != null) { 1916 AttributeSet a = (AttributeSet) 1917 elem.getAttributes().getAttribute(tag); 1918 if (a == null) { 1919 a = elem.getAttributes(); 1920 } 1921 return a; 1922 } 1923 return null; 1924 } 1925 1926 /** 1927 * Returns the start of the range for which the current occurrence of 1928 * the tag is defined and has the same attributes. 1929 * 1930 * @return the start of the range, or -1 if it can't be found 1931 */ 1932 public int getStartOffset() { 1933 Element elem = pos.current(); 1934 if (elem != null) { 1935 return elem.getStartOffset(); 1936 } 1937 return -1; 1938 } 1939 1940 /** 1941 * Returns the end of the range for which the current occurrence of 1942 * the tag is defined and has the same attributes. 1943 * 1944 * @return the end of the range 1945 */ 1946 public int getEndOffset() { 1947 return endOffset; 1948 } 1949 1950 /** 1951 * Moves the iterator forward to the next occurrence 1952 * of the tag it represents. 1953 */ 1954 public void next() { 1955 for (nextLeaf(pos); isValid(); nextLeaf(pos)) { 1956 Element elem = pos.current(); 1957 if (elem.getStartOffset() >= endOffset) { 1958 AttributeSet a = pos.current().getAttributes(); 1959 1960 if (a.isDefined(tag) || 1961 a.getAttribute(StyleConstants.NameAttribute) == tag) { 1962 1963 // we found the next one 1964 setEndOffset(); 1965 break; 1966 } 1967 } 1968 } 1969 } 1970 1971 /** 1972 * Returns the type of tag this iterator represents. 1973 * 1974 * @return the <code>HTML.Tag</code> that this iterator represents. 1975 * @see javax.swing.text.html.HTML.Tag 1976 */ 1977 public HTML.Tag getTag() { 1978 return tag; 1979 } 1980 1981 /** 1982 * Returns true if the current position is not <code>null</code>. 1983 * @return true if current position is not <code>null</code>, 1984 * otherwise returns false 1985 */ 1986 public boolean isValid() { 1987 return (pos.current() != null); 1988 } 1989 1990 /** 1991 * Moves the given iterator to the next leaf element. 1992 * @param iter the iterator to be scanned 1993 */ 1994 void nextLeaf(ElementIterator iter) { 1995 for (iter.next(); iter.current() != null; iter.next()) { 1996 Element e = iter.current(); 1997 if (e.isLeaf()) { 1998 break; 1999 } 2000 } 2001 } 2002 2003 /** 2004 * Marches a cloned iterator forward to locate the end 2005 * of the run. This sets the value of <code>endOffset</code>. 2006 */ 2007 void setEndOffset() { 2008 AttributeSet a0 = getAttributes(); 2009 endOffset = pos.current().getEndOffset(); 2010 ElementIterator fwd = (ElementIterator) pos.clone(); 2011 for (nextLeaf(fwd); fwd.current() != null; nextLeaf(fwd)) { 2012 Element e = fwd.current(); 2013 AttributeSet a1 = (AttributeSet) e.getAttributes().getAttribute(tag); 2014 if ((a1 == null) || (! a1.equals(a0))) { 2015 break; 2016 } 2017 endOffset = e.getEndOffset(); 2018 } 2019 } 2020 2021 private int endOffset; 2022 private HTML.Tag tag; 2023 private ElementIterator pos; 2024 2025 } 2026 2027 /** 2028 * An HTML reader to load an HTML document with an HTML 2029 * element structure. This is a set of callbacks from 2030 * the parser, implemented to create a set of elements 2031 * tagged with attributes. The parse builds up tokens 2032 * (ElementSpec) that describe the element subtree desired, 2033 * and burst it into the document under the protection of 2034 * a write lock using the insert method on the document 2035 * outer class. 2036 * <p> 2037 * The reader can be configured by registering actions 2038 * (of type <code>HTMLDocument.HTMLReader.TagAction</code>) 2039 * that describe how to handle the action. The idea behind 2040 * the actions provided is that the most natural text editing 2041 * operations can be provided if the element structure boils 2042 * down to paragraphs with runs of some kind of style 2043 * in them. Some things are more naturally specified 2044 * structurally, so arbitrary structure should be allowed 2045 * above the paragraphs, but will need to be edited with structural 2046 * actions. The implication of this is that some of the 2047 * HTML elements specified in the stream being parsed will 2048 * be collapsed into attributes, and in some cases paragraphs 2049 * will be synthesized. When HTML elements have been 2050 * converted to attributes, the attribute key will be of 2051 * type HTML.Tag, and the value will be of type AttributeSet 2052 * so that no information is lost. This enables many of the 2053 * existing actions to work so that the user can type input, 2054 * hit the return key, backspace, delete, etc and have a 2055 * reasonable result. Selections can be created, and attributes 2056 * applied or removed, etc. With this in mind, the work done 2057 * by the reader can be categorized into the following kinds 2058 * of tasks: 2059 * <dl> 2060 * <dt>Block 2061 * <dd>Build the structure like it's specified in the stream. 2062 * This produces elements that contain other elements. 2063 * <dt>Paragraph 2064 * <dd>Like block except that it's expected that the element 2065 * will be used with a paragraph view so a paragraph element 2066 * won't need to be synthesized. 2067 * <dt>Character 2068 * <dd>Contribute the element as an attribute that will start 2069 * and stop at arbitrary text locations. This will ultimately 2070 * be mixed into a run of text, with all of the currently 2071 * flattened HTML character elements. 2072 * <dt>Special 2073 * <dd>Produce an embedded graphical element. 2074 * <dt>Form 2075 * <dd>Produce an element that is like the embedded graphical 2076 * element, except that it also has a component model associated 2077 * with it. 2078 * <dt>Hidden 2079 * <dd>Create an element that is hidden from view when the 2080 * document is being viewed read-only, and visible when the 2081 * document is being edited. This is useful to keep the 2082 * model from losing information, and used to store things 2083 * like comments and unrecognized tags. 2084 * 2085 * </dl> 2086 * <p> 2087 * Currently, <APPLET>, <PARAM>, <MAP>, <AREA>, <LINK>, 2088 * <SCRIPT> and <STYLE> are unsupported. 2089 * 2090 * <p> 2091 * The assignment of the actions described is shown in the 2092 * following table for the tags defined in <code>HTML.Tag</code>. 2093 * <table border=1 summary="HTML tags and assigned actions"> 2094 * <tr><th>Tag</th><th>Action</th></tr> 2095 * <tr><td><code>HTML.Tag.A</code> <td>CharacterAction 2096 * <tr><td><code>HTML.Tag.ADDRESS</code> <td>CharacterAction 2097 * <tr><td><code>HTML.Tag.APPLET</code> <td>HiddenAction 2098 * <tr><td><code>HTML.Tag.AREA</code> <td>AreaAction 2099 * <tr><td><code>HTML.Tag.B</code> <td>CharacterAction 2100 * <tr><td><code>HTML.Tag.BASE</code> <td>BaseAction 2101 * <tr><td><code>HTML.Tag.BASEFONT</code> <td>CharacterAction 2102 * <tr><td><code>HTML.Tag.BIG</code> <td>CharacterAction 2103 * <tr><td><code>HTML.Tag.BLOCKQUOTE</code><td>BlockAction 2104 * <tr><td><code>HTML.Tag.BODY</code> <td>BlockAction 2105 * <tr><td><code>HTML.Tag.BR</code> <td>SpecialAction 2106 * <tr><td><code>HTML.Tag.CAPTION</code> <td>BlockAction 2107 * <tr><td><code>HTML.Tag.CENTER</code> <td>BlockAction 2108 * <tr><td><code>HTML.Tag.CITE</code> <td>CharacterAction 2109 * <tr><td><code>HTML.Tag.CODE</code> <td>CharacterAction 2110 * <tr><td><code>HTML.Tag.DD</code> <td>BlockAction 2111 * <tr><td><code>HTML.Tag.DFN</code> <td>CharacterAction 2112 * <tr><td><code>HTML.Tag.DIR</code> <td>BlockAction 2113 * <tr><td><code>HTML.Tag.DIV</code> <td>BlockAction 2114 * <tr><td><code>HTML.Tag.DL</code> <td>BlockAction 2115 * <tr><td><code>HTML.Tag.DT</code> <td>ParagraphAction 2116 * <tr><td><code>HTML.Tag.EM</code> <td>CharacterAction 2117 * <tr><td><code>HTML.Tag.FONT</code> <td>CharacterAction 2118 * <tr><td><code>HTML.Tag.FORM</code> <td>As of 1.4 a BlockAction 2119 * <tr><td><code>HTML.Tag.FRAME</code> <td>SpecialAction 2120 * <tr><td><code>HTML.Tag.FRAMESET</code> <td>BlockAction 2121 * <tr><td><code>HTML.Tag.H1</code> <td>ParagraphAction 2122 * <tr><td><code>HTML.Tag.H2</code> <td>ParagraphAction 2123 * <tr><td><code>HTML.Tag.H3</code> <td>ParagraphAction 2124 * <tr><td><code>HTML.Tag.H4</code> <td>ParagraphAction 2125 * <tr><td><code>HTML.Tag.H5</code> <td>ParagraphAction 2126 * <tr><td><code>HTML.Tag.H6</code> <td>ParagraphAction 2127 * <tr><td><code>HTML.Tag.HEAD</code> <td>HeadAction 2128 * <tr><td><code>HTML.Tag.HR</code> <td>SpecialAction 2129 * <tr><td><code>HTML.Tag.HTML</code> <td>BlockAction 2130 * <tr><td><code>HTML.Tag.I</code> <td>CharacterAction 2131 * <tr><td><code>HTML.Tag.IMG</code> <td>SpecialAction 2132 * <tr><td><code>HTML.Tag.INPUT</code> <td>FormAction 2133 * <tr><td><code>HTML.Tag.ISINDEX</code> <td>IsndexAction 2134 * <tr><td><code>HTML.Tag.KBD</code> <td>CharacterAction 2135 * <tr><td><code>HTML.Tag.LI</code> <td>BlockAction 2136 * <tr><td><code>HTML.Tag.LINK</code> <td>LinkAction 2137 * <tr><td><code>HTML.Tag.MAP</code> <td>MapAction 2138 * <tr><td><code>HTML.Tag.MENU</code> <td>BlockAction 2139 * <tr><td><code>HTML.Tag.META</code> <td>MetaAction 2140 * <tr><td><code>HTML.Tag.NOFRAMES</code> <td>BlockAction 2141 * <tr><td><code>HTML.Tag.OBJECT</code> <td>SpecialAction 2142 * <tr><td><code>HTML.Tag.OL</code> <td>BlockAction 2143 * <tr><td><code>HTML.Tag.OPTION</code> <td>FormAction 2144 * <tr><td><code>HTML.Tag.P</code> <td>ParagraphAction 2145 * <tr><td><code>HTML.Tag.PARAM</code> <td>HiddenAction 2146 * <tr><td><code>HTML.Tag.PRE</code> <td>PreAction 2147 * <tr><td><code>HTML.Tag.SAMP</code> <td>CharacterAction 2148 * <tr><td><code>HTML.Tag.SCRIPT</code> <td>HiddenAction 2149 * <tr><td><code>HTML.Tag.SELECT</code> <td>FormAction 2150 * <tr><td><code>HTML.Tag.SMALL</code> <td>CharacterAction 2151 * <tr><td><code>HTML.Tag.STRIKE</code> <td>CharacterAction 2152 * <tr><td><code>HTML.Tag.S</code> <td>CharacterAction 2153 * <tr><td><code>HTML.Tag.STRONG</code> <td>CharacterAction 2154 * <tr><td><code>HTML.Tag.STYLE</code> <td>StyleAction 2155 * <tr><td><code>HTML.Tag.SUB</code> <td>CharacterAction 2156 * <tr><td><code>HTML.Tag.SUP</code> <td>CharacterAction 2157 * <tr><td><code>HTML.Tag.TABLE</code> <td>BlockAction 2158 * <tr><td><code>HTML.Tag.TD</code> <td>BlockAction 2159 * <tr><td><code>HTML.Tag.TEXTAREA</code> <td>FormAction 2160 * <tr><td><code>HTML.Tag.TH</code> <td>BlockAction 2161 * <tr><td><code>HTML.Tag.TITLE</code> <td>TitleAction 2162 * <tr><td><code>HTML.Tag.TR</code> <td>BlockAction 2163 * <tr><td><code>HTML.Tag.TT</code> <td>CharacterAction 2164 * <tr><td><code>HTML.Tag.U</code> <td>CharacterAction 2165 * <tr><td><code>HTML.Tag.UL</code> <td>BlockAction 2166 * <tr><td><code>HTML.Tag.VAR</code> <td>CharacterAction 2167 * </table> 2168 * <p> 2169 * Once </html> is encountered, the Actions are no longer notified. 2170 */ 2171 public class HTMLReader extends HTMLEditorKit.ParserCallback { 2172 2173 public HTMLReader(int offset) { 2174 this(offset, 0, 0, null); 2175 } 2176 2177 public HTMLReader(int offset, int popDepth, int pushDepth, 2178 HTML.Tag insertTag) { 2179 this(offset, popDepth, pushDepth, insertTag, true, false, true); 2180 } 2181 2182 /** 2183 * Generates a RuntimeException (will eventually generate 2184 * a BadLocationException when API changes are alloced) if inserting 2185 * into non empty document, <code>insertTag</code> is 2186 * non-<code>null</code>, and <code>offset</code> is not in the body. 2187 */ 2188 // PENDING(sky): Add throws BadLocationException and remove 2189 // RuntimeException 2190 HTMLReader(int offset, int popDepth, int pushDepth, 2191 HTML.Tag insertTag, boolean insertInsertTag, 2192 boolean insertAfterImplied, boolean wantsTrailingNewline) { 2193 emptyDocument = (getLength() == 0); 2194 isStyleCSS = "text/css".equals(getDefaultStyleSheetType()); 2195 this.offset = offset; 2196 threshold = HTMLDocument.this.getTokenThreshold(); 2197 tagMap = new Hashtable<HTML.Tag, TagAction>(57); 2198 TagAction na = new TagAction(); 2199 TagAction ba = new BlockAction(); 2200 TagAction pa = new ParagraphAction(); 2201 TagAction ca = new CharacterAction(); 2202 TagAction sa = new SpecialAction(); 2203 TagAction fa = new FormAction(); 2204 TagAction ha = new HiddenAction(); 2205 TagAction conv = new ConvertAction(); 2206 2207 // register handlers for the well known tags 2208 tagMap.put(HTML.Tag.A, new AnchorAction()); 2209 tagMap.put(HTML.Tag.ADDRESS, ca); 2210 tagMap.put(HTML.Tag.APPLET, ha); 2211 tagMap.put(HTML.Tag.AREA, new AreaAction()); 2212 tagMap.put(HTML.Tag.B, conv); 2213 tagMap.put(HTML.Tag.BASE, new BaseAction()); 2214 tagMap.put(HTML.Tag.BASEFONT, ca); 2215 tagMap.put(HTML.Tag.BIG, ca); 2216 tagMap.put(HTML.Tag.BLOCKQUOTE, ba); 2217 tagMap.put(HTML.Tag.BODY, ba); 2218 tagMap.put(HTML.Tag.BR, sa); 2219 tagMap.put(HTML.Tag.CAPTION, ba); 2220 tagMap.put(HTML.Tag.CENTER, ba); 2221 tagMap.put(HTML.Tag.CITE, ca); 2222 tagMap.put(HTML.Tag.CODE, ca); 2223 tagMap.put(HTML.Tag.DD, ba); 2224 tagMap.put(HTML.Tag.DFN, ca); 2225 tagMap.put(HTML.Tag.DIR, ba); 2226 tagMap.put(HTML.Tag.DIV, ba); 2227 tagMap.put(HTML.Tag.DL, ba); 2228 tagMap.put(HTML.Tag.DT, pa); 2229 tagMap.put(HTML.Tag.EM, ca); 2230 tagMap.put(HTML.Tag.FONT, conv); 2231 tagMap.put(HTML.Tag.FORM, new FormTagAction()); 2232 tagMap.put(HTML.Tag.FRAME, sa); 2233 tagMap.put(HTML.Tag.FRAMESET, ba); 2234 tagMap.put(HTML.Tag.H1, pa); 2235 tagMap.put(HTML.Tag.H2, pa); 2236 tagMap.put(HTML.Tag.H3, pa); 2237 tagMap.put(HTML.Tag.H4, pa); 2238 tagMap.put(HTML.Tag.H5, pa); 2239 tagMap.put(HTML.Tag.H6, pa); 2240 tagMap.put(HTML.Tag.HEAD, new HeadAction()); 2241 tagMap.put(HTML.Tag.HR, sa); 2242 tagMap.put(HTML.Tag.HTML, ba); 2243 tagMap.put(HTML.Tag.I, conv); 2244 tagMap.put(HTML.Tag.IMG, sa); 2245 tagMap.put(HTML.Tag.INPUT, fa); 2246 tagMap.put(HTML.Tag.ISINDEX, new IsindexAction()); 2247 tagMap.put(HTML.Tag.KBD, ca); 2248 tagMap.put(HTML.Tag.LI, ba); 2249 tagMap.put(HTML.Tag.LINK, new LinkAction()); 2250 tagMap.put(HTML.Tag.MAP, new MapAction()); 2251 tagMap.put(HTML.Tag.MENU, ba); 2252 tagMap.put(HTML.Tag.META, new MetaAction()); 2253 tagMap.put(HTML.Tag.NOBR, ca); 2254 tagMap.put(HTML.Tag.NOFRAMES, ba); 2255 tagMap.put(HTML.Tag.OBJECT, sa); 2256 tagMap.put(HTML.Tag.OL, ba); 2257 tagMap.put(HTML.Tag.OPTION, fa); 2258 tagMap.put(HTML.Tag.P, pa); 2259 tagMap.put(HTML.Tag.PARAM, new ObjectAction()); 2260 tagMap.put(HTML.Tag.PRE, new PreAction()); 2261 tagMap.put(HTML.Tag.SAMP, ca); 2262 tagMap.put(HTML.Tag.SCRIPT, ha); 2263 tagMap.put(HTML.Tag.SELECT, fa); 2264 tagMap.put(HTML.Tag.SMALL, ca); 2265 tagMap.put(HTML.Tag.SPAN, ca); 2266 tagMap.put(HTML.Tag.STRIKE, conv); 2267 tagMap.put(HTML.Tag.S, ca); 2268 tagMap.put(HTML.Tag.STRONG, ca); 2269 tagMap.put(HTML.Tag.STYLE, new StyleAction()); 2270 tagMap.put(HTML.Tag.SUB, conv); 2271 tagMap.put(HTML.Tag.SUP, conv); 2272 tagMap.put(HTML.Tag.TABLE, ba); 2273 tagMap.put(HTML.Tag.TD, ba); 2274 tagMap.put(HTML.Tag.TEXTAREA, fa); 2275 tagMap.put(HTML.Tag.TH, ba); 2276 tagMap.put(HTML.Tag.TITLE, new TitleAction()); 2277 tagMap.put(HTML.Tag.TR, ba); 2278 tagMap.put(HTML.Tag.TT, ca); 2279 tagMap.put(HTML.Tag.U, conv); 2280 tagMap.put(HTML.Tag.UL, ba); 2281 tagMap.put(HTML.Tag.VAR, ca); 2282 2283 if (insertTag != null) { 2284 this.insertTag = insertTag; 2285 this.popDepth = popDepth; 2286 this.pushDepth = pushDepth; 2287 this.insertInsertTag = insertInsertTag; 2288 foundInsertTag = false; 2289 } 2290 else { 2291 foundInsertTag = true; 2292 } 2293 if (insertAfterImplied) { 2294 this.popDepth = popDepth; 2295 this.pushDepth = pushDepth; 2296 this.insertAfterImplied = true; 2297 foundInsertTag = false; 2298 midInsert = false; 2299 this.insertInsertTag = true; 2300 this.wantsTrailingNewline = wantsTrailingNewline; 2301 } 2302 else { 2303 midInsert = (!emptyDocument && insertTag == null); 2304 if (midInsert) { 2305 generateEndsSpecsForMidInsert(); 2306 } 2307 } 2308 2309 /** 2310 * This block initializes the <code>inParagraph</code> flag. 2311 * It is left in <code>false</code> value automatically 2312 * if the target document is empty or future inserts 2313 * were positioned into the 'body' tag. 2314 */ 2315 if (!emptyDocument && !midInsert) { 2316 int targetOffset = Math.max(this.offset - 1, 0); 2317 Element elem = 2318 HTMLDocument.this.getCharacterElement(targetOffset); 2319 /* Going up by the left document structure path */ 2320 for (int i = 0; i <= this.popDepth; i++) { 2321 elem = elem.getParentElement(); 2322 } 2323 /* Going down by the right document structure path */ 2324 for (int i = 0; i < this.pushDepth; i++) { 2325 int index = elem.getElementIndex(this.offset); 2326 elem = elem.getElement(index); 2327 } 2328 AttributeSet attrs = elem.getAttributes(); 2329 if (attrs != null) { 2330 HTML.Tag tagToInsertInto = 2331 (HTML.Tag) attrs.getAttribute(StyleConstants.NameAttribute); 2332 if (tagToInsertInto != null) { 2333 this.inParagraph = tagToInsertInto.isParagraph(); 2334 } 2335 } 2336 } 2337 } 2338 2339 /** 2340 * Generates an initial batch of end <code>ElementSpecs</code> 2341 * in parseBuffer to position future inserts into the body. 2342 */ 2343 private void generateEndsSpecsForMidInsert() { 2344 int count = heightToElementWithName(HTML.Tag.BODY, 2345 Math.max(0, offset - 1)); 2346 boolean joinNext = false; 2347 2348 if (count == -1 && offset > 0) { 2349 count = heightToElementWithName(HTML.Tag.BODY, offset); 2350 if (count != -1) { 2351 // Previous isn't in body, but current is. Have to 2352 // do some end specs, followed by join next. 2353 count = depthTo(offset - 1) - 1; 2354 joinNext = true; 2355 } 2356 } 2357 if (count == -1) { 2358 throw new RuntimeException("Must insert new content into body element-"); 2359 } 2360 if (count != -1) { 2361 // Insert a newline, if necessary. 2362 try { 2363 if (!joinNext && offset > 0 && 2364 !getText(offset - 1, 1).equals("\n")) { 2365 SimpleAttributeSet newAttrs = new SimpleAttributeSet(); 2366 newAttrs.addAttribute(StyleConstants.NameAttribute, 2367 HTML.Tag.CONTENT); 2368 ElementSpec spec = new ElementSpec(newAttrs, 2369 ElementSpec.ContentType, NEWLINE, 0, 1); 2370 parseBuffer.addElement(spec); 2371 } 2372 // Should never throw, but will catch anyway. 2373 } catch (BadLocationException ble) {} 2374 while (count-- > 0) { 2375 parseBuffer.addElement(new ElementSpec 2376 (null, ElementSpec.EndTagType)); 2377 } 2378 if (joinNext) { 2379 ElementSpec spec = new ElementSpec(null, ElementSpec. 2380 StartTagType); 2381 2382 spec.setDirection(ElementSpec.JoinNextDirection); 2383 parseBuffer.addElement(spec); 2384 } 2385 } 2386 // We should probably throw an exception if (count == -1) 2387 // Or look for the body and reset the offset. 2388 } 2389 2390 /** 2391 * @return number of parents to reach the child at offset. 2392 */ 2393 private int depthTo(int offset) { 2394 Element e = getDefaultRootElement(); 2395 int count = 0; 2396 2397 while (!e.isLeaf()) { 2398 count++; 2399 e = e.getElement(e.getElementIndex(offset)); 2400 } 2401 return count; 2402 } 2403 2404 /** 2405 * @return number of parents of the leaf at <code>offset</code> 2406 * until a parent with name, <code>name</code> has been 2407 * found. -1 indicates no matching parent with 2408 * <code>name</code>. 2409 */ 2410 private int heightToElementWithName(Object name, int offset) { 2411 Element e = getCharacterElement(offset).getParentElement(); 2412 int count = 0; 2413 2414 while (e != null && e.getAttributes().getAttribute 2415 (StyleConstants.NameAttribute) != name) { 2416 count++; 2417 e = e.getParentElement(); 2418 } 2419 return (e == null) ? -1 : count; 2420 } 2421 2422 /** 2423 * This will make sure there aren't two BODYs (the second is 2424 * typically created when you do a remove all, and then an insert). 2425 */ 2426 private void adjustEndElement() { 2427 int length = getLength(); 2428 if (length == 0) { 2429 return; 2430 } 2431 obtainLock(); 2432 try { 2433 Element[] pPath = getPathTo(length - 1); 2434 int pLength = pPath.length; 2435 if (pLength > 1 && pPath[1].getAttributes().getAttribute 2436 (StyleConstants.NameAttribute) == HTML.Tag.BODY && 2437 pPath[1].getEndOffset() == length) { 2438 String lastText = getText(length - 1, 1); 2439 DefaultDocumentEvent event; 2440 Element[] added; 2441 Element[] removed; 2442 int index; 2443 // Remove the fake second body. 2444 added = new Element[0]; 2445 removed = new Element[1]; 2446 index = pPath[0].getElementIndex(length); 2447 removed[0] = pPath[0].getElement(index); 2448 ((BranchElement)pPath[0]).replace(index, 1, added); 2449 ElementEdit firstEdit = new ElementEdit(pPath[0], index, 2450 removed, added); 2451 2452 // Insert a new element to represent the end that the 2453 // second body was representing. 2454 SimpleAttributeSet sas = new SimpleAttributeSet(); 2455 sas.addAttribute(StyleConstants.NameAttribute, 2456 HTML.Tag.CONTENT); 2457 sas.addAttribute(IMPLIED_CR, Boolean.TRUE); 2458 added = new Element[1]; 2459 added[0] = createLeafElement(pPath[pLength - 1], 2460 sas, length, length + 1); 2461 index = pPath[pLength - 1].getElementCount(); 2462 ((BranchElement)pPath[pLength - 1]).replace(index, 0, 2463 added); 2464 event = new DefaultDocumentEvent(length, 1, 2465 DocumentEvent.EventType.CHANGE); 2466 event.addEdit(new ElementEdit(pPath[pLength - 1], 2467 index, new Element[0], added)); 2468 event.addEdit(firstEdit); 2469 event.end(); 2470 fireChangedUpdate(event); 2471 fireUndoableEditUpdate(new UndoableEditEvent(this, event)); 2472 2473 if (lastText.equals("\n")) { 2474 // We now have two \n's, one part of the Document. 2475 // We need to remove one 2476 event = new DefaultDocumentEvent(length - 1, 1, 2477 DocumentEvent.EventType.REMOVE); 2478 removeUpdate(event); 2479 UndoableEdit u = getContent().remove(length - 1, 1); 2480 if (u != null) { 2481 event.addEdit(u); 2482 } 2483 postRemoveUpdate(event); 2484 // Mark the edit as done. 2485 event.end(); 2486 fireRemoveUpdate(event); 2487 fireUndoableEditUpdate(new UndoableEditEvent( 2488 this, event)); 2489 } 2490 } 2491 } 2492 catch (BadLocationException ble) { 2493 } 2494 finally { 2495 releaseLock(); 2496 } 2497 } 2498 2499 private Element[] getPathTo(int offset) { 2500 Stack<Element> elements = new Stack<Element>(); 2501 Element e = getDefaultRootElement(); 2502 int index; 2503 while (!e.isLeaf()) { 2504 elements.push(e); 2505 e = e.getElement(e.getElementIndex(offset)); 2506 } 2507 Element[] retValue = new Element[elements.size()]; 2508 elements.copyInto(retValue); 2509 return retValue; 2510 } 2511 2512 // -- HTMLEditorKit.ParserCallback methods -------------------- 2513 2514 /** 2515 * The last method called on the reader. It allows 2516 * any pending changes to be flushed into the document. 2517 * Since this is currently loading synchronously, the entire 2518 * set of changes are pushed in at this point. 2519 */ 2520 public void flush() throws BadLocationException { 2521 if (emptyDocument && !insertAfterImplied) { 2522 if (HTMLDocument.this.getLength() > 0 || 2523 parseBuffer.size() > 0) { 2524 flushBuffer(true); 2525 adjustEndElement(); 2526 } 2527 // We won't insert when 2528 } 2529 else { 2530 flushBuffer(true); 2531 } 2532 } 2533 2534 /** 2535 * Called by the parser to indicate a block of text was 2536 * encountered. 2537 */ 2538 public void handleText(char[] data, int pos) { 2539 if (receivedEndHTML || (midInsert && !inBody)) { 2540 return; 2541 } 2542 2543 // see if complex glyph layout support is needed 2544 if(HTMLDocument.this.getProperty(I18NProperty).equals( Boolean.FALSE ) ) { 2545 // if a default direction of right-to-left has been specified, 2546 // we want complex layout even if the text is all left to right. 2547 Object d = getProperty(TextAttribute.RUN_DIRECTION); 2548 if ((d != null) && (d.equals(TextAttribute.RUN_DIRECTION_RTL))) { 2549 HTMLDocument.this.putProperty( I18NProperty, Boolean.TRUE); 2550 } else { 2551 if (SwingUtilities2.isComplexLayout(data, 0, data.length)) { 2552 HTMLDocument.this.putProperty( I18NProperty, Boolean.TRUE); 2553 } 2554 } 2555 } 2556 2557 if (inTextArea) { 2558 textAreaContent(data); 2559 } else if (inPre) { 2560 preContent(data); 2561 } else if (inTitle) { 2562 putProperty(Document.TitleProperty, new String(data)); 2563 } else if (option != null) { 2564 option.setLabel(new String(data)); 2565 } else if (inStyle) { 2566 if (styles != null) { 2567 styles.addElement(new String(data)); 2568 } 2569 } else if (inBlock > 0) { 2570 if (!foundInsertTag && insertAfterImplied) { 2571 // Assume content should be added. 2572 foundInsertTag(false); 2573 foundInsertTag = true; 2574 inParagraph = impliedP = true; 2575 } 2576 if (data.length >= 1) { 2577 addContent(data, 0, data.length); 2578 } 2579 } 2580 } 2581 2582 /** 2583 * Callback from the parser. Route to the appropriate 2584 * handler for the tag. 2585 */ 2586 public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) { 2587 if (receivedEndHTML) { 2588 return; 2589 } 2590 if (midInsert && !inBody) { 2591 if (t == HTML.Tag.BODY) { 2592 inBody = true; 2593 // Increment inBlock since we know we are in the body, 2594 // this is needed incase an implied-p is needed. If 2595 // inBlock isn't incremented, and an implied-p is 2596 // encountered, addContent won't be called! 2597 inBlock++; 2598 } 2599 return; 2600 } 2601 if (!inBody && t == HTML.Tag.BODY) { 2602 inBody = true; 2603 } 2604 if (isStyleCSS && a.isDefined(HTML.Attribute.STYLE)) { 2605 // Map the style attributes. 2606 String decl = (String)a.getAttribute(HTML.Attribute.STYLE); 2607 a.removeAttribute(HTML.Attribute.STYLE); 2608 styleAttributes = getStyleSheet().getDeclaration(decl); 2609 a.addAttributes(styleAttributes); 2610 } 2611 else { 2612 styleAttributes = null; 2613 } 2614 TagAction action = tagMap.get(t); 2615 2616 if (action != null) { 2617 action.start(t, a); 2618 } 2619 } 2620 2621 public void handleComment(char[] data, int pos) { 2622 if (receivedEndHTML) { 2623 addExternalComment(new String(data)); 2624 return; 2625 } 2626 if (inStyle) { 2627 if (styles != null) { 2628 styles.addElement(new String(data)); 2629 } 2630 } 2631 else if (getPreservesUnknownTags()) { 2632 if (inBlock == 0 && (foundInsertTag || 2633 insertTag != HTML.Tag.COMMENT)) { 2634 // Comment outside of body, will not be able to show it, 2635 // but can add it as a property on the Document. 2636 addExternalComment(new String(data)); 2637 return; 2638 } 2639 SimpleAttributeSet sas = new SimpleAttributeSet(); 2640 sas.addAttribute(HTML.Attribute.COMMENT, new String(data)); 2641 addSpecialElement(HTML.Tag.COMMENT, sas); 2642 } 2643 2644 TagAction action = tagMap.get(HTML.Tag.COMMENT); 2645 if (action != null) { 2646 action.start(HTML.Tag.COMMENT, new SimpleAttributeSet()); 2647 action.end(HTML.Tag.COMMENT); 2648 } 2649 } 2650 2651 /** 2652 * Adds the comment <code>comment</code> to the set of comments 2653 * maintained outside of the scope of elements. 2654 */ 2655 private void addExternalComment(String comment) { 2656 Object comments = getProperty(AdditionalComments); 2657 if (comments != null && !(comments instanceof Vector)) { 2658 // No place to put comment. 2659 return; 2660 } 2661 if (comments == null) { 2662 comments = new Vector(); 2663 putProperty(AdditionalComments, comments); 2664 } 2665 ((Vector)comments).addElement(comment); 2666 } 2667 2668 /** 2669 * Callback from the parser. Route to the appropriate 2670 * handler for the tag. 2671 */ 2672 public void handleEndTag(HTML.Tag t, int pos) { 2673 if (receivedEndHTML || (midInsert && !inBody)) { 2674 return; 2675 } 2676 if (t == HTML.Tag.HTML) { 2677 receivedEndHTML = true; 2678 } 2679 if (t == HTML.Tag.BODY) { 2680 inBody = false; 2681 if (midInsert) { 2682 inBlock--; 2683 } 2684 } 2685 TagAction action = tagMap.get(t); 2686 if (action != null) { 2687 action.end(t); 2688 } 2689 } 2690 2691 /** 2692 * Callback from the parser. Route to the appropriate 2693 * handler for the tag. 2694 */ 2695 public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) { 2696 if (receivedEndHTML || (midInsert && !inBody)) { 2697 return; 2698 } 2699 2700 if (isStyleCSS && a.isDefined(HTML.Attribute.STYLE)) { 2701 // Map the style attributes. 2702 String decl = (String)a.getAttribute(HTML.Attribute.STYLE); 2703 a.removeAttribute(HTML.Attribute.STYLE); 2704 styleAttributes = getStyleSheet().getDeclaration(decl); 2705 a.addAttributes(styleAttributes); 2706 } 2707 else { 2708 styleAttributes = null; 2709 } 2710 2711 TagAction action = tagMap.get(t); 2712 if (action != null) { 2713 action.start(t, a); 2714 action.end(t); 2715 } 2716 else if (getPreservesUnknownTags()) { 2717 // unknown tag, only add if should preserve it. 2718 addSpecialElement(t, a); 2719 } 2720 } 2721 2722 /** 2723 * This is invoked after the stream has been parsed, but before 2724 * <code>flush</code>. <code>eol</code> will be one of \n, \r 2725 * or \r\n, which ever is encountered the most in parsing the 2726 * stream. 2727 * 2728 * @since 1.3 2729 */ 2730 public void handleEndOfLineString(String eol) { 2731 if (emptyDocument && eol != null) { 2732 putProperty(DefaultEditorKit.EndOfLineStringProperty, 2733 eol); 2734 } 2735 } 2736 2737 // ---- tag handling support ------------------------------ 2738 2739 /** 2740 * Registers a handler for the given tag. By default 2741 * all of the well-known tags will have been registered. 2742 * This can be used to change the handling of a particular 2743 * tag or to add support for custom tags. 2744 */ 2745 protected void registerTag(HTML.Tag t, TagAction a) { 2746 tagMap.put(t, a); 2747 } 2748 2749 /** 2750 * An action to be performed in response 2751 * to parsing a tag. This allows customization 2752 * of how each tag is handled and avoids a large 2753 * switch statement. 2754 */ 2755 public class TagAction { 2756 2757 /** 2758 * Called when a start tag is seen for the 2759 * type of tag this action was registered 2760 * to. The tag argument indicates the actual 2761 * tag for those actions that are shared across 2762 * many tags. By default this does nothing and 2763 * completely ignores the tag. 2764 */ 2765 public void start(HTML.Tag t, MutableAttributeSet a) { 2766 } 2767 2768 /** 2769 * Called when an end tag is seen for the 2770 * type of tag this action was registered 2771 * to. The tag argument indicates the actual 2772 * tag for those actions that are shared across 2773 * many tags. By default this does nothing and 2774 * completely ignores the tag. 2775 */ 2776 public void end(HTML.Tag t) { 2777 } 2778 2779 } 2780 2781 public class BlockAction extends TagAction { 2782 2783 public void start(HTML.Tag t, MutableAttributeSet attr) { 2784 blockOpen(t, attr); 2785 } 2786 2787 public void end(HTML.Tag t) { 2788 blockClose(t); 2789 } 2790 } 2791 2792 2793 /** 2794 * Action used for the actual element form tag. This is named such 2795 * as there was already a public class named FormAction. 2796 */ 2797 private class FormTagAction extends BlockAction { 2798 public void start(HTML.Tag t, MutableAttributeSet attr) { 2799 super.start(t, attr); 2800 // initialize a ButtonGroupsMap when 2801 // FORM tag is encountered. This will 2802 // be used for any radio buttons that 2803 // might be defined in the FORM. 2804 // for new group new ButtonGroup will be created (fix for 4529702) 2805 // group name is a key in radioButtonGroupsMap 2806 radioButtonGroupsMap = new HashMap<String, ButtonGroup>(); 2807 } 2808 2809 public void end(HTML.Tag t) { 2810 super.end(t); 2811 // reset the button group to null since 2812 // the form has ended. 2813 radioButtonGroupsMap = null; 2814 } 2815 } 2816 2817 2818 public class ParagraphAction extends BlockAction { 2819 2820 public void start(HTML.Tag t, MutableAttributeSet a) { 2821 super.start(t, a); 2822 inParagraph = true; 2823 } 2824 2825 public void end(HTML.Tag t) { 2826 super.end(t); 2827 inParagraph = false; 2828 } 2829 } 2830 2831 public class SpecialAction extends TagAction { 2832 2833 public void start(HTML.Tag t, MutableAttributeSet a) { 2834 addSpecialElement(t, a); 2835 } 2836 2837 } 2838 2839 public class IsindexAction extends TagAction { 2840 2841 public void start(HTML.Tag t, MutableAttributeSet a) { 2842 blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet()); 2843 addSpecialElement(t, a); 2844 blockClose(HTML.Tag.IMPLIED); 2845 } 2846 2847 } 2848 2849 2850 public class HiddenAction extends TagAction { 2851 2852 public void start(HTML.Tag t, MutableAttributeSet a) { 2853 addSpecialElement(t, a); 2854 } 2855 2856 public void end(HTML.Tag t) { 2857 if (!isEmpty(t)) { 2858 MutableAttributeSet a = new SimpleAttributeSet(); 2859 a.addAttribute(HTML.Attribute.ENDTAG, "true"); 2860 addSpecialElement(t, a); 2861 } 2862 } 2863 2864 boolean isEmpty(HTML.Tag t) { 2865 if (t == HTML.Tag.APPLET || 2866 t == HTML.Tag.SCRIPT) { 2867 return false; 2868 } 2869 return true; 2870 } 2871 } 2872 2873 2874 /** 2875 * Subclass of HiddenAction to set the content type for style sheets, 2876 * and to set the name of the default style sheet. 2877 */ 2878 class MetaAction extends HiddenAction { 2879 2880 public void start(HTML.Tag t, MutableAttributeSet a) { 2881 Object equiv = a.getAttribute(HTML.Attribute.HTTPEQUIV); 2882 if (equiv != null) { 2883 equiv = ((String)equiv).toLowerCase(); 2884 if (equiv.equals("content-style-type")) { 2885 String value = (String)a.getAttribute 2886 (HTML.Attribute.CONTENT); 2887 setDefaultStyleSheetType(value); 2888 isStyleCSS = "text/css".equals 2889 (getDefaultStyleSheetType()); 2890 } 2891 else if (equiv.equals("default-style")) { 2892 defaultStyle = (String)a.getAttribute 2893 (HTML.Attribute.CONTENT); 2894 } 2895 } 2896 super.start(t, a); 2897 } 2898 2899 boolean isEmpty(HTML.Tag t) { 2900 return true; 2901 } 2902 } 2903 2904 2905 /** 2906 * End if overridden to create the necessary stylesheets that 2907 * are referenced via the link tag. It is done in this manner 2908 * as the meta tag can be used to specify an alternate style sheet, 2909 * and is not guaranteed to come before the link tags. 2910 */ 2911 class HeadAction extends BlockAction { 2912 2913 public void start(HTML.Tag t, MutableAttributeSet a) { 2914 inHead = true; 2915 // This check of the insertTag is put in to avoid considering 2916 // the implied-p that is generated for the head. This allows 2917 // inserts for HR to work correctly. 2918 if ((insertTag == null && !insertAfterImplied) || 2919 (insertTag == HTML.Tag.HEAD) || 2920 (insertAfterImplied && 2921 (foundInsertTag || !a.isDefined(IMPLIED)))) { 2922 super.start(t, a); 2923 } 2924 } 2925 2926 public void end(HTML.Tag t) { 2927 inHead = inStyle = false; 2928 // See if there is a StyleSheet to link to. 2929 if (styles != null) { 2930 boolean isDefaultCSS = isStyleCSS; 2931 for (int counter = 0, maxCounter = styles.size(); 2932 counter < maxCounter;) { 2933 Object value = styles.elementAt(counter); 2934 if (value == HTML.Tag.LINK) { 2935 handleLink((AttributeSet)styles. 2936 elementAt(++counter)); 2937 counter++; 2938 } 2939 else { 2940 // Rule. 2941 // First element gives type. 2942 String type = (String)styles.elementAt(++counter); 2943 boolean isCSS = (type == null) ? isDefaultCSS : 2944 type.equals("text/css"); 2945 while (++counter < maxCounter && 2946 (styles.elementAt(counter) 2947 instanceof String)) { 2948 if (isCSS) { 2949 addCSSRules((String)styles.elementAt 2950 (counter)); 2951 } 2952 } 2953 } 2954 } 2955 } 2956 if ((insertTag == null && !insertAfterImplied) || 2957 insertTag == HTML.Tag.HEAD || 2958 (insertAfterImplied && foundInsertTag)) { 2959 super.end(t); 2960 } 2961 } 2962 2963 boolean isEmpty(HTML.Tag t) { 2964 return false; 2965 } 2966 2967 private void handleLink(AttributeSet attr) { 2968 // Link. 2969 String type = (String)attr.getAttribute(HTML.Attribute.TYPE); 2970 if (type == null) { 2971 type = getDefaultStyleSheetType(); 2972 } 2973 // Only choose if type==text/css 2974 // Select link if rel==stylesheet. 2975 // Otherwise if rel==alternate stylesheet and 2976 // title matches default style. 2977 if (type.equals("text/css")) { 2978 String rel = (String)attr.getAttribute(HTML.Attribute.REL); 2979 String title = (String)attr.getAttribute 2980 (HTML.Attribute.TITLE); 2981 String media = (String)attr.getAttribute 2982 (HTML.Attribute.MEDIA); 2983 if (media == null) { 2984 media = "all"; 2985 } 2986 else { 2987 media = media.toLowerCase(); 2988 } 2989 if (rel != null) { 2990 rel = rel.toLowerCase(); 2991 if ((media.indexOf("all") != -1 || 2992 media.indexOf("screen") != -1) && 2993 (rel.equals("stylesheet") || 2994 (rel.equals("alternate stylesheet") && 2995 title.equals(defaultStyle)))) { 2996 linkCSSStyleSheet((String)attr.getAttribute 2997 (HTML.Attribute.HREF)); 2998 } 2999 } 3000 } 3001 } 3002 } 3003 3004 3005 /** 3006 * A subclass to add the AttributeSet to styles if the 3007 * attributes contains an attribute for 'rel' with value 3008 * 'stylesheet' or 'alternate stylesheet'. 3009 */ 3010 class LinkAction extends HiddenAction { 3011 3012 public void start(HTML.Tag t, MutableAttributeSet a) { 3013 String rel = (String)a.getAttribute(HTML.Attribute.REL); 3014 if (rel != null) { 3015 rel = rel.toLowerCase(); 3016 if (rel.equals("stylesheet") || 3017 rel.equals("alternate stylesheet")) { 3018 if (styles == null) { 3019 styles = new Vector<Object>(3); 3020 } 3021 styles.addElement(t); 3022 styles.addElement(a.copyAttributes()); 3023 } 3024 } 3025 super.start(t, a); 3026 } 3027 } 3028 3029 class MapAction extends TagAction { 3030 3031 public void start(HTML.Tag t, MutableAttributeSet a) { 3032 lastMap = new Map((String)a.getAttribute(HTML.Attribute.NAME)); 3033 addMap(lastMap); 3034 } 3035 3036 public void end(HTML.Tag t) { 3037 } 3038 } 3039 3040 3041 class AreaAction extends TagAction { 3042 3043 public void start(HTML.Tag t, MutableAttributeSet a) { 3044 if (lastMap != null) { 3045 lastMap.addArea(a.copyAttributes()); 3046 } 3047 } 3048 3049 public void end(HTML.Tag t) { 3050 } 3051 } 3052 3053 3054 class StyleAction extends TagAction { 3055 3056 public void start(HTML.Tag t, MutableAttributeSet a) { 3057 if (inHead) { 3058 if (styles == null) { 3059 styles = new Vector<Object>(3); 3060 } 3061 styles.addElement(t); 3062 styles.addElement(a.getAttribute(HTML.Attribute.TYPE)); 3063 inStyle = true; 3064 } 3065 } 3066 3067 public void end(HTML.Tag t) { 3068 inStyle = false; 3069 } 3070 3071 boolean isEmpty(HTML.Tag t) { 3072 return false; 3073 } 3074 } 3075 3076 3077 public class PreAction extends BlockAction { 3078 3079 public void start(HTML.Tag t, MutableAttributeSet attr) { 3080 inPre = true; 3081 blockOpen(t, attr); 3082 attr.addAttribute(CSS.Attribute.WHITE_SPACE, "pre"); 3083 blockOpen(HTML.Tag.IMPLIED, attr); 3084 } 3085 3086 public void end(HTML.Tag t) { 3087 blockClose(HTML.Tag.IMPLIED); 3088 // set inPre to false after closing, so that if a newline 3089 // is added it won't generate a blockOpen. 3090 inPre = false; 3091 blockClose(t); 3092 } 3093 } 3094 3095 public class CharacterAction extends TagAction { 3096 3097 public void start(HTML.Tag t, MutableAttributeSet attr) { 3098 pushCharacterStyle(); 3099 if (!foundInsertTag) { 3100 // Note that the third argument should really be based off 3101 // inParagraph and impliedP. If we're wrong (that is 3102 // insertTagDepthDelta shouldn't be changed), we'll end up 3103 // removing an extra EndSpec, which won't matter anyway. 3104 boolean insert = canInsertTag(t, attr, false); 3105 if (foundInsertTag) { 3106 if (!inParagraph) { 3107 inParagraph = impliedP = true; 3108 } 3109 } 3110 if (!insert) { 3111 return; 3112 } 3113 } 3114 if (attr.isDefined(IMPLIED)) { 3115 attr.removeAttribute(IMPLIED); 3116 } 3117 charAttr.addAttribute(t, attr.copyAttributes()); 3118 if (styleAttributes != null) { 3119 charAttr.addAttributes(styleAttributes); 3120 } 3121 } 3122 3123 public void end(HTML.Tag t) { 3124 popCharacterStyle(); 3125 } 3126 } 3127 3128 /** 3129 * Provides conversion of HTML tag/attribute 3130 * mappings that have a corresponding StyleConstants 3131 * and CSS mapping. The conversion is to CSS attributes. 3132 */ 3133 class ConvertAction extends TagAction { 3134 3135 public void start(HTML.Tag t, MutableAttributeSet attr) { 3136 pushCharacterStyle(); 3137 if (!foundInsertTag) { 3138 // Note that the third argument should really be based off 3139 // inParagraph and impliedP. If we're wrong (that is 3140 // insertTagDepthDelta shouldn't be changed), we'll end up 3141 // removing an extra EndSpec, which won't matter anyway. 3142 boolean insert = canInsertTag(t, attr, false); 3143 if (foundInsertTag) { 3144 if (!inParagraph) { 3145 inParagraph = impliedP = true; 3146 } 3147 } 3148 if (!insert) { 3149 return; 3150 } 3151 } 3152 if (attr.isDefined(IMPLIED)) { 3153 attr.removeAttribute(IMPLIED); 3154 } 3155 if (styleAttributes != null) { 3156 charAttr.addAttributes(styleAttributes); 3157 } 3158 // We also need to add attr, otherwise we lose custom 3159 // attributes, including class/id for style lookups, and 3160 // further confuse style lookup (doesn't have tag). 3161 charAttr.addAttribute(t, attr.copyAttributes()); 3162 StyleSheet sheet = getStyleSheet(); 3163 if (t == HTML.Tag.B) { 3164 sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_WEIGHT, "bold"); 3165 } else if (t == HTML.Tag.I) { 3166 sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_STYLE, "italic"); 3167 } else if (t == HTML.Tag.U) { 3168 Object v = charAttr.getAttribute(CSS.Attribute.TEXT_DECORATION); 3169 String value = "underline"; 3170 value = (v != null) ? value + "," + v.toString() : value; 3171 sheet.addCSSAttribute(charAttr, CSS.Attribute.TEXT_DECORATION, value); 3172 } else if (t == HTML.Tag.STRIKE) { 3173 Object v = charAttr.getAttribute(CSS.Attribute.TEXT_DECORATION); 3174 String value = "line-through"; 3175 value = (v != null) ? value + "," + v.toString() : value; 3176 sheet.addCSSAttribute(charAttr, CSS.Attribute.TEXT_DECORATION, value); 3177 } else if (t == HTML.Tag.SUP) { 3178 Object v = charAttr.getAttribute(CSS.Attribute.VERTICAL_ALIGN); 3179 String value = "sup"; 3180 value = (v != null) ? value + "," + v.toString() : value; 3181 sheet.addCSSAttribute(charAttr, CSS.Attribute.VERTICAL_ALIGN, value); 3182 } else if (t == HTML.Tag.SUB) { 3183 Object v = charAttr.getAttribute(CSS.Attribute.VERTICAL_ALIGN); 3184 String value = "sub"; 3185 value = (v != null) ? value + "," + v.toString() : value; 3186 sheet.addCSSAttribute(charAttr, CSS.Attribute.VERTICAL_ALIGN, value); 3187 } else if (t == HTML.Tag.FONT) { 3188 String color = (String) attr.getAttribute(HTML.Attribute.COLOR); 3189 if (color != null) { 3190 sheet.addCSSAttribute(charAttr, CSS.Attribute.COLOR, color); 3191 } 3192 String face = (String) attr.getAttribute(HTML.Attribute.FACE); 3193 if (face != null) { 3194 sheet.addCSSAttribute(charAttr, CSS.Attribute.FONT_FAMILY, face); 3195 } 3196 String size = (String) attr.getAttribute(HTML.Attribute.SIZE); 3197 if (size != null) { 3198 sheet.addCSSAttributeFromHTML(charAttr, CSS.Attribute.FONT_SIZE, size); 3199 } 3200 } 3201 } 3202 3203 public void end(HTML.Tag t) { 3204 popCharacterStyle(); 3205 } 3206 3207 } 3208 3209 class AnchorAction extends CharacterAction { 3210 3211 public void start(HTML.Tag t, MutableAttributeSet attr) { 3212 // set flag to catch empty anchors 3213 emptyAnchor = true; 3214 super.start(t, attr); 3215 } 3216 3217 public void end(HTML.Tag t) { 3218 if (emptyAnchor) { 3219 // if the anchor was empty it was probably a 3220 // named anchor point and we don't want to throw 3221 // it away. 3222 char[] one = new char[1]; 3223 one[0] = '\n'; 3224 addContent(one, 0, 1); 3225 } 3226 super.end(t); 3227 } 3228 } 3229 3230 class TitleAction extends HiddenAction { 3231 3232 public void start(HTML.Tag t, MutableAttributeSet attr) { 3233 inTitle = true; 3234 super.start(t, attr); 3235 } 3236 3237 public void end(HTML.Tag t) { 3238 inTitle = false; 3239 super.end(t); 3240 } 3241 3242 boolean isEmpty(HTML.Tag t) { 3243 return false; 3244 } 3245 } 3246 3247 3248 class BaseAction extends TagAction { 3249 3250 public void start(HTML.Tag t, MutableAttributeSet attr) { 3251 String href = (String) attr.getAttribute(HTML.Attribute.HREF); 3252 if (href != null) { 3253 try { 3254 URL newBase = new URL(base, href); 3255 setBase(newBase); 3256 hasBaseTag = true; 3257 } catch (MalformedURLException ex) { 3258 } 3259 } 3260 baseTarget = (String) attr.getAttribute(HTML.Attribute.TARGET); 3261 } 3262 } 3263 3264 class ObjectAction extends SpecialAction { 3265 3266 public void start(HTML.Tag t, MutableAttributeSet a) { 3267 if (t == HTML.Tag.PARAM) { 3268 addParameter(a); 3269 } else { 3270 super.start(t, a); 3271 } 3272 } 3273 3274 public void end(HTML.Tag t) { 3275 if (t != HTML.Tag.PARAM) { 3276 super.end(t); 3277 } 3278 } 3279 3280 void addParameter(AttributeSet a) { 3281 String name = (String) a.getAttribute(HTML.Attribute.NAME); 3282 String value = (String) a.getAttribute(HTML.Attribute.VALUE); 3283 if ((name != null) && (value != null)) { 3284 ElementSpec objSpec = parseBuffer.lastElement(); 3285 MutableAttributeSet objAttr = (MutableAttributeSet) objSpec.getAttributes(); 3286 objAttr.addAttribute(name, value); 3287 } 3288 } 3289 } 3290 3291 /** 3292 * Action to support forms by building all of the elements 3293 * used to represent form controls. This will process 3294 * the <INPUT>, <TEXTAREA>, <SELECT>, 3295 * and <OPTION> tags. The element created by 3296 * this action is expected to have the attribute 3297 * <code>StyleConstants.ModelAttribute</code> set to 3298 * the model that holds the state for the form control. 3299 * This enables multiple views, and allows document to 3300 * be iterated over picking up the data of the form. 3301 * The following are the model assignments for the 3302 * various type of form elements. 3303 * <table summary="model assignments for the various types of form elements"> 3304 * <tr> 3305 * <th>Element Type 3306 * <th>Model Type 3307 * <tr> 3308 * <td>input, type button 3309 * <td>{@link DefaultButtonModel} 3310 * <tr> 3311 * <td>input, type checkbox 3312 * <td>{@link javax.swing.JToggleButton.ToggleButtonModel} 3313 * <tr> 3314 * <td>input, type image 3315 * <td>{@link DefaultButtonModel} 3316 * <tr> 3317 * <td>input, type password 3318 * <td>{@link PlainDocument} 3319 * <tr> 3320 * <td>input, type radio 3321 * <td>{@link javax.swing.JToggleButton.ToggleButtonModel} 3322 * <tr> 3323 * <td>input, type reset 3324 * <td>{@link DefaultButtonModel} 3325 * <tr> 3326 * <td>input, type submit 3327 * <td>{@link DefaultButtonModel} 3328 * <tr> 3329 * <td>input, type text or type is null. 3330 * <td>{@link PlainDocument} 3331 * <tr> 3332 * <td>select 3333 * <td>{@link DefaultComboBoxModel} or an {@link DefaultListModel}, with an item type of Option 3334 * <tr> 3335 * <td>textarea 3336 * <td>{@link PlainDocument} 3337 * </table> 3338 * 3339 */ 3340 public class FormAction extends SpecialAction { 3341 3342 public void start(HTML.Tag t, MutableAttributeSet attr) { 3343 if (t == HTML.Tag.INPUT) { 3344 String type = (String) 3345 attr.getAttribute(HTML.Attribute.TYPE); 3346 /* 3347 * if type is not defined the default is 3348 * assumed to be text. 3349 */ 3350 if (type == null) { 3351 type = "text"; 3352 attr.addAttribute(HTML.Attribute.TYPE, "text"); 3353 } 3354 setModel(type, attr); 3355 } else if (t == HTML.Tag.TEXTAREA) { 3356 inTextArea = true; 3357 textAreaDocument = new TextAreaDocument(); 3358 attr.addAttribute(StyleConstants.ModelAttribute, 3359 textAreaDocument); 3360 } else if (t == HTML.Tag.SELECT) { 3361 int size = HTML.getIntegerAttributeValue(attr, 3362 HTML.Attribute.SIZE, 3363 1); 3364 boolean multiple = attr.getAttribute(HTML.Attribute.MULTIPLE) != null; 3365 if ((size > 1) || multiple) { 3366 OptionListModel<Option> m = new OptionListModel<Option>(); 3367 if (multiple) { 3368 m.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 3369 } 3370 selectModel = m; 3371 } else { 3372 selectModel = new OptionComboBoxModel<Option>(); 3373 } 3374 attr.addAttribute(StyleConstants.ModelAttribute, 3375 selectModel); 3376 3377 } 3378 3379 // build the element, unless this is an option. 3380 if (t == HTML.Tag.OPTION) { 3381 option = new Option(attr); 3382 3383 if (selectModel instanceof OptionListModel) { 3384 OptionListModel<Option> m = (OptionListModel<Option>) selectModel; 3385 m.addElement(option); 3386 if (option.isSelected()) { 3387 m.addSelectionInterval(optionCount, optionCount); 3388 m.setInitialSelection(optionCount); 3389 } 3390 } else if (selectModel instanceof OptionComboBoxModel) { 3391 OptionComboBoxModel<Option> m = (OptionComboBoxModel<Option>) selectModel; 3392 m.addElement(option); 3393 if (option.isSelected()) { 3394 m.setSelectedItem(option); 3395 m.setInitialSelection(option); 3396 } 3397 } 3398 optionCount++; 3399 } else { 3400 super.start(t, attr); 3401 } 3402 } 3403 3404 public void end(HTML.Tag t) { 3405 if (t == HTML.Tag.OPTION) { 3406 option = null; 3407 } else { 3408 if (t == HTML.Tag.SELECT) { 3409 selectModel = null; 3410 optionCount = 0; 3411 } else if (t == HTML.Tag.TEXTAREA) { 3412 inTextArea = false; 3413 3414 /* Now that the textarea has ended, 3415 * store the entire initial text 3416 * of the text area. This will 3417 * enable us to restore the initial 3418 * state if a reset is requested. 3419 */ 3420 textAreaDocument.storeInitialText(); 3421 } 3422 super.end(t); 3423 } 3424 } 3425 3426 void setModel(String type, MutableAttributeSet attr) { 3427 if (type.equals("submit") || 3428 type.equals("reset") || 3429 type.equals("image")) { 3430 3431 // button model 3432 attr.addAttribute(StyleConstants.ModelAttribute, 3433 new DefaultButtonModel()); 3434 } else if (type.equals("text") || 3435 type.equals("password")) { 3436 // plain text model 3437 int maxLength = HTML.getIntegerAttributeValue( 3438 attr, HTML.Attribute.MAXLENGTH, -1); 3439 Document doc; 3440 3441 if (maxLength > 0) { 3442 doc = new FixedLengthDocument(maxLength); 3443 } 3444 else { 3445 doc = new PlainDocument(); 3446 } 3447 String value = (String) 3448 attr.getAttribute(HTML.Attribute.VALUE); 3449 try { 3450 doc.insertString(0, value, null); 3451 } catch (BadLocationException e) { 3452 } 3453 attr.addAttribute(StyleConstants.ModelAttribute, doc); 3454 } else if (type.equals("file")) { 3455 // plain text model 3456 attr.addAttribute(StyleConstants.ModelAttribute, 3457 new PlainDocument()); 3458 } else if (type.equals("checkbox") || 3459 type.equals("radio")) { 3460 JToggleButton.ToggleButtonModel model = new JToggleButton.ToggleButtonModel(); 3461 if (type.equals("radio")) { 3462 String name = (String) attr.getAttribute(HTML.Attribute.NAME); 3463 if ( radioButtonGroupsMap == null ) { //fix for 4772743 3464 radioButtonGroupsMap = new HashMap<String, ButtonGroup>(); 3465 } 3466 ButtonGroup radioButtonGroup = radioButtonGroupsMap.get(name); 3467 if (radioButtonGroup == null) { 3468 radioButtonGroup = new ButtonGroup(); 3469 radioButtonGroupsMap.put(name,radioButtonGroup); 3470 } 3471 model.setGroup(radioButtonGroup); 3472 } 3473 boolean checked = (attr.getAttribute(HTML.Attribute.CHECKED) != null); 3474 model.setSelected(checked); 3475 attr.addAttribute(StyleConstants.ModelAttribute, model); 3476 } 3477 } 3478 3479 /** 3480 * If a <SELECT> tag is being processed, this 3481 * model will be a reference to the model being filled 3482 * with the <OPTION> elements (which produce 3483 * objects of type <code>Option</code>. 3484 */ 3485 Object selectModel; 3486 int optionCount; 3487 } 3488 3489 3490 // --- utility methods used by the reader ------------------ 3491 3492 /** 3493 * Pushes the current character style on a stack in preparation 3494 * for forming a new nested character style. 3495 */ 3496 protected void pushCharacterStyle() { 3497 charAttrStack.push(charAttr.copyAttributes()); 3498 } 3499 3500 /** 3501 * Pops a previously pushed character style off the stack 3502 * to return to a previous style. 3503 */ 3504 protected void popCharacterStyle() { 3505 if (!charAttrStack.empty()) { 3506 charAttr = (MutableAttributeSet) charAttrStack.peek(); 3507 charAttrStack.pop(); 3508 } 3509 } 3510 3511 /** 3512 * Adds the given content to the textarea document. 3513 * This method gets called when we are in a textarea 3514 * context. Therefore all text that is seen belongs 3515 * to the text area and is hence added to the 3516 * TextAreaDocument associated with the text area. 3517 */ 3518 protected void textAreaContent(char[] data) { 3519 try { 3520 textAreaDocument.insertString(textAreaDocument.getLength(), new String(data), null); 3521 } catch (BadLocationException e) { 3522 // Should do something reasonable 3523 } 3524 } 3525 3526 /** 3527 * Adds the given content that was encountered in a 3528 * PRE element. This synthesizes lines to hold the 3529 * runs of text, and makes calls to addContent to 3530 * actually add the text. 3531 */ 3532 protected void preContent(char[] data) { 3533 int last = 0; 3534 for (int i = 0; i < data.length; i++) { 3535 if (data[i] == '\n') { 3536 addContent(data, last, i - last + 1); 3537 blockClose(HTML.Tag.IMPLIED); 3538 MutableAttributeSet a = new SimpleAttributeSet(); 3539 a.addAttribute(CSS.Attribute.WHITE_SPACE, "pre"); 3540 blockOpen(HTML.Tag.IMPLIED, a); 3541 last = i + 1; 3542 } 3543 } 3544 if (last < data.length) { 3545 addContent(data, last, data.length - last); 3546 } 3547 } 3548 3549 /** 3550 * Adds an instruction to the parse buffer to create a 3551 * block element with the given attributes. 3552 */ 3553 protected void blockOpen(HTML.Tag t, MutableAttributeSet attr) { 3554 if (impliedP) { 3555 blockClose(HTML.Tag.IMPLIED); 3556 } 3557 3558 inBlock++; 3559 3560 if (!canInsertTag(t, attr, true)) { 3561 return; 3562 } 3563 if (attr.isDefined(IMPLIED)) { 3564 attr.removeAttribute(IMPLIED); 3565 } 3566 lastWasNewline = false; 3567 attr.addAttribute(StyleConstants.NameAttribute, t); 3568 ElementSpec es = new ElementSpec( 3569 attr.copyAttributes(), ElementSpec.StartTagType); 3570 parseBuffer.addElement(es); 3571 } 3572 3573 /** 3574 * Adds an instruction to the parse buffer to close out 3575 * a block element of the given type. 3576 */ 3577 protected void blockClose(HTML.Tag t) { 3578 inBlock--; 3579 3580 if (!foundInsertTag) { 3581 return; 3582 } 3583 3584 // Add a new line, if the last character wasn't one. This is 3585 // needed for proper positioning of the cursor. addContent 3586 // with true will force an implied paragraph to be generated if 3587 // there isn't one. This may result in a rather bogus structure 3588 // (perhaps a table with a child pargraph), but the paragraph 3589 // is needed for proper positioning and display. 3590 if(!lastWasNewline) { 3591 pushCharacterStyle(); 3592 charAttr.addAttribute(IMPLIED_CR, Boolean.TRUE); 3593 addContent(NEWLINE, 0, 1, true); 3594 popCharacterStyle(); 3595 lastWasNewline = true; 3596 } 3597 3598 if (impliedP) { 3599 impliedP = false; 3600 inParagraph = false; 3601 if (t != HTML.Tag.IMPLIED) { 3602 blockClose(HTML.Tag.IMPLIED); 3603 } 3604 } 3605 // an open/close with no content will be removed, so we 3606 // add a space of content to keep the element being formed. 3607 ElementSpec prev = (parseBuffer.size() > 0) ? 3608 parseBuffer.lastElement() : null; 3609 if (prev != null && prev.getType() == ElementSpec.StartTagType) { 3610 char[] one = new char[1]; 3611 one[0] = ' '; 3612 addContent(one, 0, 1); 3613 } 3614 ElementSpec es = new ElementSpec( 3615 null, ElementSpec.EndTagType); 3616 parseBuffer.addElement(es); 3617 } 3618 3619 /** 3620 * Adds some text with the current character attributes. 3621 * 3622 * @param data the content to add 3623 * @param offs the initial offset 3624 * @param length the length 3625 */ 3626 protected void addContent(char[] data, int offs, int length) { 3627 addContent(data, offs, length, true); 3628 } 3629 3630 /** 3631 * Adds some text with the current character attributes. 3632 * 3633 * @param data the content to add 3634 * @param offs the initial offset 3635 * @param length the length 3636 * @param generateImpliedPIfNecessary whether to generate implied 3637 * paragraphs 3638 */ 3639 protected void addContent(char[] data, int offs, int length, 3640 boolean generateImpliedPIfNecessary) { 3641 if (!foundInsertTag) { 3642 return; 3643 } 3644 3645 if (generateImpliedPIfNecessary && (! inParagraph) && (! inPre)) { 3646 blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet()); 3647 inParagraph = true; 3648 impliedP = true; 3649 } 3650 emptyAnchor = false; 3651 charAttr.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); 3652 AttributeSet a = charAttr.copyAttributes(); 3653 ElementSpec es = new ElementSpec( 3654 a, ElementSpec.ContentType, data, offs, length); 3655 parseBuffer.addElement(es); 3656 3657 if (parseBuffer.size() > threshold) { 3658 if ( threshold <= MaxThreshold ) { 3659 threshold *= StepThreshold; 3660 } 3661 try { 3662 flushBuffer(false); 3663 } catch (BadLocationException ble) { 3664 } 3665 } 3666 if(length > 0) { 3667 lastWasNewline = (data[offs + length - 1] == '\n'); 3668 } 3669 } 3670 3671 /** 3672 * Adds content that is basically specified entirely 3673 * in the attribute set. 3674 */ 3675 protected void addSpecialElement(HTML.Tag t, MutableAttributeSet a) { 3676 if ((t != HTML.Tag.FRAME) && (! inParagraph) && (! inPre)) { 3677 nextTagAfterPImplied = t; 3678 blockOpen(HTML.Tag.IMPLIED, new SimpleAttributeSet()); 3679 nextTagAfterPImplied = null; 3680 inParagraph = true; 3681 impliedP = true; 3682 } 3683 if (!canInsertTag(t, a, t.isBlock())) { 3684 return; 3685 } 3686 if (a.isDefined(IMPLIED)) { 3687 a.removeAttribute(IMPLIED); 3688 } 3689 emptyAnchor = false; 3690 a.addAttributes(charAttr); 3691 a.addAttribute(StyleConstants.NameAttribute, t); 3692 char[] one = new char[1]; 3693 one[0] = ' '; 3694 ElementSpec es = new ElementSpec( 3695 a.copyAttributes(), ElementSpec.ContentType, one, 0, 1); 3696 parseBuffer.addElement(es); 3697 // Set this to avoid generating a newline for frames, frames 3698 // shouldn't have any content, and shouldn't need a newline. 3699 if (t == HTML.Tag.FRAME) { 3700 lastWasNewline = true; 3701 } 3702 } 3703 3704 /** 3705 * Flushes the current parse buffer into the document. 3706 * @param endOfStream true if there is no more content to parser 3707 */ 3708 void flushBuffer(boolean endOfStream) throws BadLocationException { 3709 int oldLength = HTMLDocument.this.getLength(); 3710 int size = parseBuffer.size(); 3711 if (endOfStream && (insertTag != null || insertAfterImplied) && 3712 size > 0) { 3713 adjustEndSpecsForPartialInsert(); 3714 size = parseBuffer.size(); 3715 } 3716 ElementSpec[] spec = new ElementSpec[size]; 3717 parseBuffer.copyInto(spec); 3718 3719 if (oldLength == 0 && (insertTag == null && !insertAfterImplied)) { 3720 create(spec); 3721 } else { 3722 insert(offset, spec); 3723 } 3724 parseBuffer.removeAllElements(); 3725 offset += HTMLDocument.this.getLength() - oldLength; 3726 flushCount++; 3727 } 3728 3729 /** 3730 * This will be invoked for the last flush, if <code>insertTag</code> 3731 * is non null. 3732 */ 3733 private void adjustEndSpecsForPartialInsert() { 3734 int size = parseBuffer.size(); 3735 if (insertTagDepthDelta < 0) { 3736 // When inserting via an insertTag, the depths (of the tree 3737 // being read in, and existing hierarchy) may not match up. 3738 // This attemps to clean it up. 3739 int removeCounter = insertTagDepthDelta; 3740 while (removeCounter < 0 && size >= 0 && 3741 parseBuffer.elementAt(size - 1). 3742 getType() == ElementSpec.EndTagType) { 3743 parseBuffer.removeElementAt(--size); 3744 removeCounter++; 3745 } 3746 } 3747 if (flushCount == 0 && (!insertAfterImplied || 3748 !wantsTrailingNewline)) { 3749 // If this starts with content (or popDepth > 0 && 3750 // pushDepth > 0) and ends with EndTagTypes, make sure 3751 // the last content isn't a \n, otherwise will end up with 3752 // an extra \n in the middle of content. 3753 int index = 0; 3754 if (pushDepth > 0) { 3755 if (parseBuffer.elementAt(0).getType() == 3756 ElementSpec.ContentType) { 3757 index++; 3758 } 3759 } 3760 index += (popDepth + pushDepth); 3761 int cCount = 0; 3762 int cStart = index; 3763 while (index < size && parseBuffer.elementAt 3764 (index).getType() == ElementSpec.ContentType) { 3765 index++; 3766 cCount++; 3767 } 3768 if (cCount > 1) { 3769 while (index < size && parseBuffer.elementAt 3770 (index).getType() == ElementSpec.EndTagType) { 3771 index++; 3772 } 3773 if (index == size) { 3774 char[] lastText = parseBuffer.elementAt 3775 (cStart + cCount - 1).getArray(); 3776 if (lastText.length == 1 && lastText[0] == NEWLINE[0]){ 3777 index = cStart + cCount - 1; 3778 while (size > index) { 3779 parseBuffer.removeElementAt(--size); 3780 } 3781 } 3782 } 3783 } 3784 } 3785 if (wantsTrailingNewline) { 3786 // Make sure there is in fact a newline 3787 for (int counter = parseBuffer.size() - 1; counter >= 0; 3788 counter--) { 3789 ElementSpec spec = parseBuffer.elementAt(counter); 3790 if (spec.getType() == ElementSpec.ContentType) { 3791 if (spec.getArray()[spec.getLength() - 1] != '\n') { 3792 SimpleAttributeSet attrs =new SimpleAttributeSet(); 3793 3794 attrs.addAttribute(StyleConstants.NameAttribute, 3795 HTML.Tag.CONTENT); 3796 parseBuffer.insertElementAt(new ElementSpec( 3797 attrs, 3798 ElementSpec.ContentType, NEWLINE, 0, 1), 3799 counter + 1); 3800 } 3801 break; 3802 } 3803 } 3804 } 3805 } 3806 3807 /** 3808 * Adds the CSS rules in <code>rules</code>. 3809 */ 3810 void addCSSRules(String rules) { 3811 StyleSheet ss = getStyleSheet(); 3812 ss.addRule(rules); 3813 } 3814 3815 /** 3816 * Adds the CSS stylesheet at <code>href</code> to the known list 3817 * of stylesheets. 3818 */ 3819 void linkCSSStyleSheet(String href) { 3820 URL url; 3821 try { 3822 url = new URL(base, href); 3823 } catch (MalformedURLException mfe) { 3824 try { 3825 url = new URL(href); 3826 } catch (MalformedURLException mfe2) { 3827 url = null; 3828 } 3829 } 3830 if (url != null) { 3831 getStyleSheet().importStyleSheet(url); 3832 } 3833 } 3834 3835 /** 3836 * Returns true if can insert starting at <code>t</code>. This 3837 * will return false if the insert tag is set, and hasn't been found 3838 * yet. 3839 */ 3840 private boolean canInsertTag(HTML.Tag t, AttributeSet attr, 3841 boolean isBlockTag) { 3842 if (!foundInsertTag) { 3843 boolean needPImplied = ((t == HTML.Tag.IMPLIED) 3844 && (!inParagraph) 3845 && (!inPre)); 3846 if (needPImplied && (nextTagAfterPImplied != null)) { 3847 3848 /* 3849 * If insertTag == null then just proceed to 3850 * foundInsertTag() call below and return true. 3851 */ 3852 if (insertTag != null) { 3853 boolean nextTagIsInsertTag = 3854 isInsertTag(nextTagAfterPImplied); 3855 if ( (! nextTagIsInsertTag) || (! insertInsertTag) ) { 3856 return false; 3857 } 3858 } 3859 /* 3860 * Proceed to foundInsertTag() call... 3861 */ 3862 } else if ((insertTag != null && !isInsertTag(t)) 3863 || (insertAfterImplied 3864 && (attr == null 3865 || attr.isDefined(IMPLIED) 3866 || t == HTML.Tag.IMPLIED 3867 ) 3868 ) 3869 ) { 3870 return false; 3871 } 3872 3873 // Allow the insert if t matches the insert tag, or 3874 // insertAfterImplied is true and the element is implied. 3875 foundInsertTag(isBlockTag); 3876 if (!insertInsertTag) { 3877 return false; 3878 } 3879 } 3880 return true; 3881 } 3882 3883 private boolean isInsertTag(HTML.Tag tag) { 3884 return (insertTag == tag); 3885 } 3886 3887 private void foundInsertTag(boolean isBlockTag) { 3888 foundInsertTag = true; 3889 if (!insertAfterImplied && (popDepth > 0 || pushDepth > 0)) { 3890 try { 3891 if (offset == 0 || !getText(offset - 1, 1).equals("\n")) { 3892 // Need to insert a newline. 3893 AttributeSet newAttrs = null; 3894 boolean joinP = true; 3895 3896 if (offset != 0) { 3897 // Determine if we can use JoinPrevious, we can't 3898 // if the Element has some attributes that are 3899 // not meant to be duplicated. 3900 Element charElement = getCharacterElement 3901 (offset - 1); 3902 AttributeSet attrs = charElement.getAttributes(); 3903 3904 if (attrs.isDefined(StyleConstants. 3905 ComposedTextAttribute)) { 3906 joinP = false; 3907 } 3908 else { 3909 Object name = attrs.getAttribute 3910 (StyleConstants.NameAttribute); 3911 if (name instanceof HTML.Tag) { 3912 HTML.Tag tag = (HTML.Tag)name; 3913 if (tag == HTML.Tag.IMG || 3914 tag == HTML.Tag.HR || 3915 tag == HTML.Tag.COMMENT || 3916 (tag instanceof HTML.UnknownTag)) { 3917 joinP = false; 3918 } 3919 } 3920 } 3921 } 3922 if (!joinP) { 3923 // If not joining with the previous element, be 3924 // sure and set the name (otherwise it will be 3925 // inherited). 3926 newAttrs = new SimpleAttributeSet(); 3927 ((SimpleAttributeSet)newAttrs).addAttribute 3928 (StyleConstants.NameAttribute, 3929 HTML.Tag.CONTENT); 3930 } 3931 ElementSpec es = new ElementSpec(newAttrs, 3932 ElementSpec.ContentType, NEWLINE, 0, 3933 NEWLINE.length); 3934 if (joinP) { 3935 es.setDirection(ElementSpec. 3936 JoinPreviousDirection); 3937 } 3938 parseBuffer.addElement(es); 3939 } 3940 } catch (BadLocationException ble) {} 3941 } 3942 // pops 3943 for (int counter = 0; counter < popDepth; counter++) { 3944 parseBuffer.addElement(new ElementSpec(null, ElementSpec. 3945 EndTagType)); 3946 } 3947 // pushes 3948 for (int counter = 0; counter < pushDepth; counter++) { 3949 ElementSpec es = new ElementSpec(null, ElementSpec. 3950 StartTagType); 3951 es.setDirection(ElementSpec.JoinNextDirection); 3952 parseBuffer.addElement(es); 3953 } 3954 insertTagDepthDelta = depthTo(Math.max(0, offset - 1)) - 3955 popDepth + pushDepth - inBlock; 3956 if (isBlockTag) { 3957 // A start spec will be added (for this tag), so we account 3958 // for it here. 3959 insertTagDepthDelta++; 3960 } 3961 else { 3962 // An implied paragraph close (end spec) is going to be added, 3963 // so we account for it here. 3964 insertTagDepthDelta--; 3965 inParagraph = true; 3966 lastWasNewline = false; 3967 } 3968 } 3969 3970 /** 3971 * This is set to true when and end is invoked for {@literal <html>}. 3972 */ 3973 private boolean receivedEndHTML; 3974 /** Number of times <code>flushBuffer</code> has been invoked. */ 3975 private int flushCount; 3976 /** If true, behavior is similar to insertTag, but instead of 3977 * waiting for insertTag will wait for first Element without 3978 * an 'implied' attribute and begin inserting then. */ 3979 private boolean insertAfterImplied; 3980 /** This is only used if insertAfterImplied is true. If false, only 3981 * inserting content, and there is a trailing newline it is removed. */ 3982 private boolean wantsTrailingNewline; 3983 int threshold; 3984 int offset; 3985 boolean inParagraph = false; 3986 boolean impliedP = false; 3987 boolean inPre = false; 3988 boolean inTextArea = false; 3989 TextAreaDocument textAreaDocument = null; 3990 boolean inTitle = false; 3991 boolean lastWasNewline = true; 3992 boolean emptyAnchor; 3993 /** True if (!emptyDocument && insertTag == null), this is used so 3994 * much it is cached. */ 3995 boolean midInsert; 3996 /** True when the body has been encountered. */ 3997 boolean inBody; 3998 /** If non null, gives parent Tag that insert is to happen at. */ 3999 HTML.Tag insertTag; 4000 /** If true, the insertTag is inserted, otherwise elements after 4001 * the insertTag is found are inserted. */ 4002 boolean insertInsertTag; 4003 /** Set to true when insertTag has been found. */ 4004 boolean foundInsertTag; 4005 /** When foundInsertTag is set to true, this will be updated to 4006 * reflect the delta between the two structures. That is, it 4007 * will be the depth the inserts are happening at minus the 4008 * depth of the tags being passed in. A value of 0 (the common 4009 * case) indicates the structures match, a value greater than 0 indicates 4010 * the insert is happening at a deeper depth than the stream is 4011 * parsing, and a value less than 0 indicates the insert is happening earlier 4012 * in the tree that the parser thinks and that we will need to remove 4013 * EndTagType specs in the flushBuffer method. 4014 */ 4015 int insertTagDepthDelta; 4016 /** How many parents to ascend before insert new elements. */ 4017 int popDepth; 4018 /** How many parents to descend (relative to popDepth) before 4019 * inserting. */ 4020 int pushDepth; 4021 /** Last Map that was encountered. */ 4022 Map lastMap; 4023 /** Set to true when a style element is encountered. */ 4024 boolean inStyle = false; 4025 /** Name of style to use. Obtained from Meta tag. */ 4026 String defaultStyle; 4027 /** Vector describing styles that should be include. Will consist 4028 * of a bunch of HTML.Tags, which will either be: 4029 * <p>LINK: in which case it is followed by an AttributeSet 4030 * <p>STYLE: in which case the following element is a String 4031 * indicating the type (may be null), and the elements following 4032 * it until the next HTML.Tag are the rules as Strings. 4033 */ 4034 Vector<Object> styles; 4035 /** True if inside the head tag. */ 4036 boolean inHead = false; 4037 /** Set to true if the style language is text/css. Since this is 4038 * used alot, it is cached. */ 4039 boolean isStyleCSS; 4040 /** True if inserting into an empty document. */ 4041 boolean emptyDocument; 4042 /** Attributes from a style Attribute. */ 4043 AttributeSet styleAttributes; 4044 4045 /** 4046 * Current option, if in an option element (needed to 4047 * load the label. 4048 */ 4049 Option option; 4050 4051 protected Vector<ElementSpec> parseBuffer = new Vector<ElementSpec>(); 4052 protected MutableAttributeSet charAttr = new TaggedAttributeSet(); 4053 Stack<AttributeSet> charAttrStack = new Stack<AttributeSet>(); 4054 Hashtable<HTML.Tag, TagAction> tagMap; 4055 int inBlock = 0; 4056 4057 /** 4058 * This attribute is sometimes used to refer to next tag 4059 * to be handled after p-implied when the latter is 4060 * the current tag which is being handled. 4061 */ 4062 private HTML.Tag nextTagAfterPImplied = null; 4063 } 4064 4065 4066 /** 4067 * Used by StyleSheet to determine when to avoid removing HTML.Tags 4068 * matching StyleConstants. 4069 */ 4070 static class TaggedAttributeSet extends SimpleAttributeSet { 4071 TaggedAttributeSet() { 4072 super(); 4073 } 4074 } 4075 4076 4077 /** 4078 * An element that represents a chunk of text that has 4079 * a set of HTML character level attributes assigned to 4080 * it. 4081 */ 4082 public class RunElement extends LeafElement { 4083 4084 /** 4085 * Constructs an element that represents content within the 4086 * document (has no children). 4087 * 4088 * @param parent the parent element 4089 * @param a the element attributes 4090 * @param offs0 the start offset (must be at least 0) 4091 * @param offs1 the end offset (must be at least offs0) 4092 * @since 1.4 4093 */ 4094 public RunElement(Element parent, AttributeSet a, int offs0, int offs1) { 4095 super(parent, a, offs0, offs1); 4096 } 4097 4098 /** 4099 * Gets the name of the element. 4100 * 4101 * @return the name, null if none 4102 */ 4103 public String getName() { 4104 Object o = getAttribute(StyleConstants.NameAttribute); 4105 if (o != null) { 4106 return o.toString(); 4107 } 4108 return super.getName(); 4109 } 4110 4111 /** 4112 * Gets the resolving parent. HTML attributes are not inherited 4113 * at the model level so we override this to return null. 4114 * 4115 * @return null, there are none 4116 * @see AttributeSet#getResolveParent 4117 */ 4118 public AttributeSet getResolveParent() { 4119 return null; 4120 } 4121 } 4122 4123 /** 4124 * An element that represents a structural <em>block</em> of 4125 * HTML. 4126 */ 4127 public class BlockElement extends BranchElement { 4128 4129 /** 4130 * Constructs a composite element that initially contains 4131 * no children. 4132 * 4133 * @param parent the parent element 4134 * @param a the attributes for the element 4135 * @since 1.4 4136 */ 4137 public BlockElement(Element parent, AttributeSet a) { 4138 super(parent, a); 4139 } 4140 4141 /** 4142 * Gets the name of the element. 4143 * 4144 * @return the name, null if none 4145 */ 4146 public String getName() { 4147 Object o = getAttribute(StyleConstants.NameAttribute); 4148 if (o != null) { 4149 return o.toString(); 4150 } 4151 return super.getName(); 4152 } 4153 4154 /** 4155 * Gets the resolving parent. HTML attributes are not inherited 4156 * at the model level so we override this to return null. 4157 * 4158 * @return null, there are none 4159 * @see AttributeSet#getResolveParent 4160 */ 4161 public AttributeSet getResolveParent() { 4162 return null; 4163 } 4164 4165 } 4166 4167 4168 /** 4169 * Document that allows you to set the maximum length of the text. 4170 */ 4171 private static class FixedLengthDocument extends PlainDocument { 4172 private int maxLength; 4173 4174 public FixedLengthDocument(int maxLength) { 4175 this.maxLength = maxLength; 4176 } 4177 4178 public void insertString(int offset, String str, AttributeSet a) 4179 throws BadLocationException { 4180 if (str != null && str.length() + getLength() <= maxLength) { 4181 super.insertString(offset, str, a); 4182 } 4183 } 4184 } 4185 }