📄 htmldocument.java
字号:
* @return the <code>AttributeSet</code> for this tag, or * <code>null</code> if none can be found */ public abstract AttributeSet getAttributes(); /** * Returns the start of the range for which the current occurrence of * the tag is defined and has the same attributes. * * @return the start of the range, or -1 if it can't be found */ public abstract int getStartOffset(); /** * Returns the end of the range for which the current occurrence of * the tag is defined and has the same attributes. * * @return the end of the range */ public abstract int getEndOffset(); /** * Move the iterator forward to the next occurrence * of the tag it represents. */ public abstract void next(); /** * Indicates if the iterator is currently * representing an occurrence of a tag. If * false there are no more tags for this iterator. * @return true if the iterator is currently representing an * occurrence of a tag, otherwise returns false */ public abstract boolean isValid(); /** * Type of tag this iterator represents. */ public abstract HTML.Tag getTag(); } /** * An iterator to iterate over a particular type of tag. */ static class LeafIterator extends Iterator { LeafIterator(HTML.Tag t, Document doc) { tag = t; pos = new ElementIterator(doc); endOffset = 0; next(); } /** * Returns the attributes for this tag. * @return the <code>AttributeSet</code> for this tag, * or <code>null</code> if none can be found */ public AttributeSet getAttributes() { Element elem = pos.current(); if (elem != null) { AttributeSet a = (AttributeSet) elem.getAttributes().getAttribute(tag); if (a == null) { a = (AttributeSet)elem.getAttributes(); } return a; } return null; } /** * Returns the start of the range for which the current occurrence of * the tag is defined and has the same attributes. * * @return the start of the range, or -1 if it can't be found */ public int getStartOffset() { Element elem = pos.current(); if (elem != null) { return elem.getStartOffset(); } return -1; } /** * Returns the end of the range for which the current occurrence of * the tag is defined and has the same attributes. * * @return the end of the range */ public int getEndOffset() { return endOffset; } /** * Moves the iterator forward to the next occurrence * of the tag it represents. */ public void next() { for (nextLeaf(pos); isValid(); nextLeaf(pos)) { Element elem = pos.current(); if (elem.getStartOffset() >= endOffset) { AttributeSet a = pos.current().getAttributes(); if (a.isDefined(tag) || a.getAttribute(StyleConstants.NameAttribute) == tag) { // we found the next one setEndOffset(); break; } } } } /** * Returns the type of tag this iterator represents. * * @return the <code>HTML.Tag</code> that this iterator represents. * @see javax.swing.text.html.HTML.Tag */ public HTML.Tag getTag() { return tag; } /** * Returns true if the current position is not <code>null</code>. * @return true if current position is not <code>null</code>, * otherwise returns false */ public boolean isValid() { return (pos.current() != null); } /** * Moves the given iterator to the next leaf element. * @param iter the iterator to be scanned */ void nextLeaf(ElementIterator iter) { for (iter.next(); iter.current() != null; iter.next()) { Element e = iter.current(); if (e.isLeaf()) { break; } } } /** * Marches a cloned iterator forward to locate the end * of the run. This sets the value of <code>endOffset</code>. */ void setEndOffset() { AttributeSet a0 = getAttributes(); endOffset = pos.current().getEndOffset(); ElementIterator fwd = (ElementIterator) pos.clone(); for (nextLeaf(fwd); fwd.current() != null; nextLeaf(fwd)) { Element e = fwd.current(); AttributeSet a1 = (AttributeSet) e.getAttributes().getAttribute(tag); if ((a1 == null) || (! a1.equals(a0))) { break; } endOffset = e.getEndOffset(); } } private int endOffset; private HTML.Tag tag; private ElementIterator pos; } /** * An HTML reader to load an HTML document with an HTML * element structure. This is a set of callbacks from * the parser, implemented to create a set of elements * tagged with attributes. The parse builds up tokens * (ElementSpec) that describe the element subtree desired, * and burst it into the document under the protection of * a write lock using the insert method on the document * outer class. * <p> * The reader can be configured by registering actions * (of type <code>HTMLDocument.HTMLReader.TagAction</code>) * that describe how to handle the action. The idea behind * the actions provided is that the most natural text editing * operations can be provided if the element structure boils * down to paragraphs with runs of some kind of style * in them. Some things are more naturally specified * structurally, so arbitrary structure should be allowed * above the paragraphs, but will need to be edited with structural * actions. The implication of this is that some of the * HTML elements specified in the stream being parsed will * be collapsed into attributes, and in some cases paragraphs * will be synthesized. When HTML elements have been * converted to attributes, the attribute key will be of * type HTML.Tag, and the value will be of type AttributeSet * so that no information is lost. This enables many of the * existing actions to work so that the user can type input, * hit the return key, backspace, delete, etc and have a * reasonable result. Selections can be created, and attributes * applied or removed, etc. With this in mind, the work done * by the reader can be categorized into the following kinds * of tasks: * <dl> * <dt>Block * <dd>Build the structure like it's specified in the stream. * This produces elements that contain other elements. * <dt>Paragraph * <dd>Like block except that it's expected that the element * will be used with a paragraph view so a paragraph element * won't need to be synthesized. * <dt>Character * <dd>Contribute the element as an attribute that will start * and stop at arbitrary text locations. This will ultimately * be mixed into a run of text, with all of the currently * flattened HTML character elements. * <dt>Special * <dd>Produce an embedded graphical element. * <dt>Form * <dd>Produce an element that is like the embedded graphical * element, except that it also has a component model associated * with it. * <dt>Hidden * <dd>Create an element that is hidden from view when the * document is being viewed read-only, and visible when the * document is being edited. This is useful to keep the * model from losing information, and used to store things * like comments and unrecognized tags. * * </dl> * <p> * Currently, <APPLET>, <PARAM>, <MAP>, <AREA>, <LINK>, * <SCRIPT> and <STYLE> are unsupported. * * <p> * The assignment of the actions described is shown in the * following table for the tags defined in <code>HTML.Tag</code>.<P> * <table border=1 summary="HTML tags and assigned actions"> * <tr><th>Tag</th><th>Action</th></tr> * <tr><td><code>HTML.Tag.A</code> <td>CharacterAction * <tr><td><code>HTML.Tag.ADDRESS</code> <td>CharacterAction * <tr><td><code>HTML.Tag.APPLET</code> <td>HiddenAction * <tr><td><code>HTML.Tag.AREA</code> <td>AreaAction * <tr><td><code>HTML.Tag.B</code> <td>CharacterAction * <tr><td><code>HTML.Tag.BASE</code> <td>BaseAction * <tr><td><code>HTML.Tag.BASEFONT</code> <td>CharacterAction * <tr><td><code>HTML.Tag.BIG</code> <td>CharacterAction * <tr><td><code>HTML.Tag.BLOCKQUOTE</code><td>BlockAction * <tr><td><code>HTML.Tag.BODY</code> <td>BlockAction * <tr><td><code>HTML.Tag.BR</code> <td>SpecialAction * <tr><td><code>HTML.Tag.CAPTION</code> <td>BlockAction * <tr><td><code>HTML.Tag.CENTER</code> <td>BlockAction * <tr><td><code>HTML.Tag.CITE</code> <td>CharacterAction * <tr><td><code>HTML.Tag.CODE</code> <td>CharacterAction * <tr><td><code>HTML.Tag.DD</code> <td>BlockAction * <tr><td><code>HTML.Tag.DFN</code> <td>CharacterAction * <tr><td><code>HTML.Tag.DIR</code> <td>BlockAction * <tr><td><code>HTML.Tag.DIV</code> <td>BlockAction * <tr><td><code>HTML.Tag.DL</code> <td>BlockAction * <tr><td><code>HTML.Tag.DT</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.EM</code> <td>CharacterAction * <tr><td><code>HTML.Tag.FONT</code> <td>CharacterAction * <tr><td><code>HTML.Tag.FORM</code> <td>As of 1.4 a BlockAction * <tr><td><code>HTML.Tag.FRAME</code> <td>SpecialAction * <tr><td><code>HTML.Tag.FRAMESET</code> <td>BlockAction * <tr><td><code>HTML.Tag.H1</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.H2</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.H3</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.H4</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.H5</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.H6</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.HEAD</code> <td>HeadAction * <tr><td><code>HTML.Tag.HR</code> <td>SpecialAction * <tr><td><code>HTML.Tag.HTML</code> <td>BlockAction * <tr><td><code>HTML.Tag.I</code> <td>CharacterAction * <tr><td><code>HTML.Tag.IMG</code> <td>SpecialAction * <tr><td><code>HTML.Tag.INPUT</code> <td>FormAction * <tr><td><code>HTML.Tag.ISINDEX</code> <td>IsndexAction * <tr><td><code>HTML.Tag.KBD</code> <td>CharacterAction * <tr><td><code>HTML.Tag.LI</code> <td>BlockAction * <tr><td><code>HTML.Tag.LINK</code> <td>LinkAction * <tr><td><code>HTML.Tag.MAP</code> <td>MapAction * <tr><td><code>HTML.Tag.MENU</code> <td>BlockAction * <tr><td><code>HTML.Tag.META</code> <td>MetaAction * <tr><td><code>HTML.Tag.NOFRAMES</code> <td>BlockAction * <tr><td><code>HTML.Tag.OBJECT</code> <td>SpecialAction * <tr><td><code>HTML.Tag.OL</code> <td>BlockAction * <tr><td><code>HTML.Tag.OPTION</code> <td>FormAction * <tr><td><code>HTML.Tag.P</code> <td>ParagraphAction * <tr><td><code>HTML.Tag.PARAM</code> <td>HiddenAction * <tr><td><code>HTML.Tag.PRE</code> <td>PreAction * <tr><td><code>HTML.Tag.SAMP</code> <td>CharacterAction * <tr><td><code>HTML.Tag.SCRIPT</code> <td>HiddenAction * <tr><td><code>HTML.Tag.SELECT</code> <td>FormAction * <tr><td><code>HTML.Tag.SMALL</code> <td>CharacterAction * <tr><td><code>HTML.Tag.STRIKE</code> <td>CharacterAction * <tr><td><code>HTML.Tag.S</code> <td>CharacterAction * <tr><td><code>HTML.Tag.STRONG</code> <td>CharacterAction * <tr><td><code>HTML.Tag.STYLE</code> <td>StyleAction * <tr><td><code>HTML.Tag.SUB</code> <td>CharacterAction * <tr><td><code>HTML.Tag.SUP</code> <td>CharacterAction * <tr><td><code>HTML.Tag.TABLE</code> <td>BlockAction * <tr><td><code>HTML.Tag.TD</code> <td>BlockAction * <tr><td><code>HTML.Tag.TEXTAREA</code> <td>FormAction * <tr><td><code>HTML.Tag.TH</code> <td>BlockAction * <tr><td><code>HTML.Tag.TITLE</code> <td>TitleAction * <tr><td><code>HTML.Tag.TR</code> <td>BlockAction * <tr><td><code>HTML.Tag.TT</code> <td>CharacterAction * <tr><td><code>HTML.Tag.U</code> <td>CharacterAction * <tr><td><code>HTML.Tag.UL</code> <td>BlockAction * <tr><td><code>HTML.Tag.VAR</code> <td>CharacterAction * </table> * <p> * Once </html> is encountered, the Actions are no longer notified. */ public class HTMLReader extends HTMLEditorKit.ParserCallback { public HTMLReader(int offset) { this(offset, 0, 0, null); } public HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag) { this(offset, popDepth, pushDepth, insertTag, true, false, true); } /** * Generates a RuntimeException (will eventually generate * a BadLocationException when API changes are alloced) if inserting * into non empty document, <code>insertTag</code> is * non-<code>null</code>, and <code>offset</code> is not in the body. */ // PENDING(sky): Add throws BadLocationException and remove // RuntimeException HTMLReader(int offset, int popDepth, int pushDepth, HTML.Tag insertTag, boolean insertInsertTag, boolean insertAfterImplied, boolean wantsTrailingNewline) { emptyDocument = (getLength() == 0); isStyleCSS = "text/css".equals(getDefaultStyleSheetType()); this.offset = offset; threshold = HTMLDocument.this.getTokenThreshold(); tagMap = new Hashtable(57); TagAction na = new TagAction(); TagAction ba = new BlockAction(); TagAction pa = new ParagraphAction(); TagAction ca = new CharacterAction(); TagAction sa = new SpecialAction(); TagAction fa = new FormAction(); TagAction ha = new HiddenAction(); TagAction conv = new ConvertAction(); // register handlers for the well known tags tagMap.put(HTML.Tag.A, new AnchorAction()); tagMap.put(HTML.Tag.ADDRESS, ca); tagMap.put(HTML.Tag.APPLET, ha); tagMap.put(HTML.Tag.AREA, new AreaAction()); tagMap.put(HTML.Tag.B, conv); tagMap.put(HTML.Tag.BASE, new BaseAction()); tagMap.put(HTML.Tag.BASEFONT, ca);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -