⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tagnode.java

📁 html 解析处理代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        attribute = new Attribute (name, null, (char)0);        attributes = getAttributesEx ();        if (null == attributes)        {            attributes = new Vector ();            setAttributesEx (attributes);        }        if (0 == attributes.size ())            // nothing added yet            attributes.addElement (attribute);        else        {            zeroth = (Attribute)attributes.elementAt (0);            // check for attribute that looks like a name            if ((null == zeroth.getValue ()) && (0 == zeroth.getQuote ()))                attributes.setElementAt (attribute, 0);            else                attributes.insertElementAt (attribute, 0);        }    }    /**     * Return the text contained in this tag.     * @return The complete contents of the tag (within the angle brackets).     */    public String getText ()    {        String ret;                ret = toHtml ();        ret = ret.substring (1, ret.length () - 1);                return (ret);    }    /**     * Sets the attributes.     * NOTE: Values of the extended hashtable are two element arrays of String,     * with the first element being the original name (not uppercased),     * and the second element being the value.     * @param attribs The attribute collection to set.     */    public void setAttributesEx (Vector attribs)    {        mAttributes = attribs;    }    /**     * Sets the nodeBegin.     * @param tagBegin The nodeBegin to set     */    public void setTagBegin (int tagBegin)    {        nodeBegin = tagBegin;    }    /**     * Gets the nodeBegin.     * @return The nodeBegin value.     */    public int getTagBegin ()    {        return (nodeBegin);    }    /**     * Sets the nodeEnd.     * @param tagEnd The nodeEnd to set     */    public void setTagEnd (int tagEnd)    {        nodeEnd = tagEnd;    }    /**     * Gets the nodeEnd.     * @return The nodeEnd value.     */    public int getTagEnd ()    {        return (nodeEnd);    }    /**     * Parses the given text to create the tag contents.     * @param text A string of the form &lt;TAGNAME xx="yy"&gt;.     */    public void setText (String text)    {        Lexer lexer;        TagNode output;                lexer = new Lexer (text);        try        {            output = (TagNode)lexer.nextNode ();            mPage = output.getPage ();            nodeBegin = output.getStartPosition ();            nodeEnd = output.getEndPosition ();            mAttributes = output.getAttributesEx ();        }        catch (ParserException pe)        {            throw new IllegalArgumentException (pe.getMessage ());        }    }    /**     * Get the plain text from this node.     * @return An empty string (tag contents do not display in a browser).     * If you want this tags HTML equivalent, use {@link #toHtml toHtml()}.     */    public String toPlainTextString ()    {        return ("");    }    /**     * Render the tag as HTML.     * A call to a tag's <code>toHtml()</code> method will render it in HTML.     * @param verbatim If <code>true</code> return as close to the original     * page text as possible.     * @return The tag as an HTML fragment.     * @see org.htmlparser.Node#toHtml()     */    public String toHtml (boolean verbatim)    {        int length;        int size;        Vector attributes;        Attribute attribute;        StringBuffer ret;        length = 2;        attributes = getAttributesEx ();        size = attributes.size ();        for (int i = 0; i < size; i++)        {            attribute = (Attribute)attributes.elementAt (i);            length += attribute.getLength ();        }        ret = new StringBuffer (length);        ret.append ("<");        for (int i = 0; i < size; i++)        {            attribute = (Attribute)attributes.elementAt (i);            attribute.toString (ret);        }        ret.append (">");        return (ret.toString ());    }    /**     * Print the contents of the tag.     * @return An string describing the tag. For text that looks like HTML use #toHtml().     */    public String toString ()    {        String text;        String type;        Cursor start;        Cursor end;        StringBuffer ret;        text = getText ();        ret = new StringBuffer (20 + text.length ());        if (isEndTag ())            type = "End";        else            type = "Tag";        start = new Cursor (getPage (), getStartPosition ());        end = new Cursor (getPage (), getEndPosition ());        ret.append (type);        ret.append (" (");        ret.append (start);        ret.append (",");        ret.append (end);        ret.append ("): ");        if (80 < ret.length () + text.length ())        {            text = text.substring (0, 77 - ret.length ());            ret.append (text);            ret.append ("...");        }        else            ret.append (text);                return (ret.toString ());    }    /**     * Determines if the given tag breaks the flow of text.     * @return <code>true</code> if following text would start on a new line,     * <code>false</code> otherwise.     */    public boolean breaksFlow ()    {        return (breakTags.containsKey (getTagName ()));    }    /**     * Default tag visiting code.     * Based on <code>isEndTag()</code>, calls either <code>visitTag()</code> or     * <code>visitEndTag()</code>.     * @param visitor The visitor that is visiting this node.     */    public void accept (NodeVisitor visitor)    {        if (isEndTag ())            visitor.visitEndTag (this);        else            visitor.visitTag (this);    }    /**     * Is this an empty xml tag of the form &lt;tag/&gt;.     * @return true if the last character of the last attribute is a '/'.     */    public boolean isEmptyXmlTag ()    {        Vector attributes;        int size;        Attribute attribute;        String name;        int length;        boolean ret;        ret = false;        attributes = getAttributesEx ();        size = attributes.size ();        if (0 < size)        {            attribute = (Attribute)attributes.elementAt (size - 1);            name = attribute.getName ();            if (null != name)            {                length = name.length ();                ret = name.charAt (length - 1) == '/';            }        }        return (ret);    }    /**     * Set this tag to be an empty xml node, or not.     * Adds or removes an ending slash on the tag.     * @param emptyXmlTag If true, ensures there is an ending slash in the node,     * i.e. &lt;tag/&gt;, otherwise removes it.     */    public void setEmptyXmlTag (boolean emptyXmlTag)    {        Vector attributes;        int size;        Attribute attribute;        String name;        String value;        int length;                attributes = getAttributesEx ();        size = attributes.size ();        if (0 < size)        {            attribute = (Attribute)attributes.elementAt (size - 1);            name = attribute.getName ();            if (null != name)            {                length = name.length ();                value = attribute.getValue ();                if (null == value)                    if (name.charAt (length - 1) == '/')                    {                        // already exists, remove if requested                        if (!emptyXmlTag)                            if (1 == length)                                attributes.removeElementAt (size - 1);                            else                            {                                // this shouldn't happen, but covers the case                                // where no whitespace separates the slash                                // from the previous attribute                                name = name.substring (0, length - 1);                                attribute = new Attribute (name, null);                                attributes.removeElementAt (size - 1);                                attributes.addElement (attribute);                            }                    }                    else                    {                        // ends with attribute, add whitespace + slash if requested                        if (emptyXmlTag)                        {                            attribute = new Attribute (" ");                            attributes.addElement (attribute);                            attribute = new Attribute ("/", null);                            attributes.addElement (attribute);                        }                    }                else                {                    // some valued attribute, add whitespace + slash if requested                    if (emptyXmlTag)                    {                        attribute = new Attribute (" ");                        attributes.addElement (attribute);                        attribute = new Attribute ("/", null);                        attributes.addElement (attribute);                    }                }            }            else            {                // ends with whitespace, add if requested                if (emptyXmlTag)                {                    attribute = new Attribute ("/", null);                    attributes.addElement (attribute);                }            }        }        else            // nothing there, add if requested            if (emptyXmlTag)            {                attribute = new Attribute ("/", null);                attributes.addElement (attribute);            }    }    /**     * Predicate to determine if this tag is an end tag (i.e. &lt;/HTML&gt;).     * @return <code>true</code> if this tag is an end tag.     */    public boolean isEndTag ()    {        String raw;                raw = getRawTagName ();        return ((null == raw) ? false : ((0 != raw.length ()) && ('/' == raw.charAt (0))));    }    /**     * Get the line number where this tag starts.     * @return The (zero based) line number in the page where this tag starts.     */    public int getStartingLineNumber ()    {        return (getPage ().row (getStartPosition ()));    }    /**     * Get the line number where this tag ends.     * @return The (zero based) line number in the page where this tag ends.     */    public int getEndingLineNumber ()    {        return (getPage ().row (getEndPosition ()));    }    /**     * Return the set of names handled by this tag.     * Since this a a generic tag, it has no ids.     * @return The names to be matched that create tags of this type.     */    public String[] getIds ()    {        return (NONE);    }    /**     * Return the set of tag names that cause this tag to finish.     * These are the normal (non end tags) that if encountered while     * scanning (a composite tag) will cause the generation of a virtual     * tag.     * Since this a a non-composite tag, the default is no enders.     * @return The names of following tags that stop further scanning.     */    public String[] getEnders ()    {        return (NONE);    }    /**     * Return the set of end tag names that cause this tag to finish.     * These are the end tags that if encountered while     * scanning (a composite tag) will cause the generation of a virtual     * tag.     * Since this a a non-composite tag, it has no end tag enders.     * @return The names of following end tags that stop further scanning.     */    public String[] getEndTagEnders ()    {        return (NONE);    }    /**     * Return the scanner associated with this tag.     * @return The scanner associated with this tag.     */    public Scanner getThisScanner ()    {        return (mScanner);    }    /**     * Set the scanner associated with this tag.     * @param scanner The scanner for this tag.     */    public void setThisScanner (Scanner scanner)    {        mScanner = scanner;    }    /**     * Get the end tag for this (composite) tag.     * For a non-composite tag this always returns <code>null</code>.     * @return The tag that terminates this composite tag, i.e. &lt;/HTML&gt;.     */    public Tag getEndTag ()    {        return (null);    }    /**     * Set the end tag for this (composite) tag.     * For a non-composite tag this is a no-op.     * @param end The tag that terminates this composite tag, i.e. &lt;/HTML&gt;.     */    public void setEndTag (Tag end)    {    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -