/ org.htmlparser / src / org / htmlparser / scanners / StyleScanner.java
StyleScanner.java
  1  // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML
  2  // http://sourceforge.org/projects/htmlparser
  3  // Copyright (C) 2004 Derrick Oswald
  4  //
  5  // Revision Control Information
  6  //
  7  // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/scanners/StyleScanner.java,v $
  8  // $Author: derrickoswald $
  9  // $Date: 2005/03/07 02:18:46 $
 10  // $Revision: 1.39 $
 11  //
 12  // This library is free software; you can redistribute it and/or
 13  // modify it under the terms of the GNU Lesser General Public
 14  // License as published by the Free Software Foundation; either
 15  // version 2.1 of the License, or (at your option) any later version.
 16  //
 17  // This library is distributed in the hope that it will be useful,
 18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
 19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 20  // Lesser General Public License for more details.
 21  //
 22  // You should have received a copy of the GNU Lesser General Public
 23  // License along with this library; if not, write to the Free Software
 24  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 25  //
 26  
 27  package org.htmlparser.scanners;
 28  
 29  import java.util.Vector;
 30  
 31  import org.htmlparser.Attribute;
 32  import org.htmlparser.Node;
 33  import org.htmlparser.NodeFactory;
 34  import org.htmlparser.PrototypicalNodeFactory;
 35  import org.htmlparser.Remark;
 36  import org.htmlparser.Text;
 37  import org.htmlparser.lexer.Lexer;
 38  import org.htmlparser.Tag;
 39  import org.htmlparser.util.NodeList;
 40  import org.htmlparser.util.ParserException;
 41  
 42  /**
 43   * The StyleScanner handles style elements.
 44   * It gathers all interior nodes into one undifferentiated string node.
 45   */
 46  public class StyleScanner extends CompositeTagScanner
 47  {
 48      /**
 49       * Create a style scanner.
 50       */
 51      public StyleScanner ()
 52      {
 53      }
 54  
 55      /**
 56       * Scan for style definitions.
 57       * Accumulates text from the page, until </[a-zA-Z] is encountered.
 58       * @param tag The tag this scanner is responsible for.
 59       * @param lexer The source of CDATA.
 60       * @param stack The parse stack, <em>not used</em>.
 61       */
 62      public Tag scan (Tag tag, Lexer lexer, NodeList stack)
 63          throws ParserException
 64      {
 65          Node content;
 66          int position;
 67          Node node;
 68          Attribute attribute;
 69          Vector vector;
 70  
 71          content = lexer.parseCDATA ();
 72          position = lexer.getPosition ();
 73          node = lexer.nextNode (false);
 74          if (null != node)
 75              if (!(node instanceof Tag) || !(   ((Tag)node).isEndTag ()
 76                  && ((Tag)node).getTagName ().equals (tag.getIds ()[0])))
 77              {
 78                  lexer.setPosition (position);
 79                  node = null;
 80              }
 81  
 82          // build new end tag if required
 83          if (null == node)
 84          {
 85              attribute = new Attribute ("/style", null);
 86              vector = new Vector ();
 87              vector.addElement (attribute);
 88              node = lexer.getNodeFactory ().createTagNode (
 89                  lexer.getPage (), position, position, vector);
 90          }
 91          tag.setEndTag ((Tag)node);
 92          if (null != content)
 93          {
 94              tag.setChildren (new NodeList (content));
 95              content.setParent (tag);
 96          }
 97          node.setParent (tag);
 98          tag.doSemanticAction ();
 99  
100          return (tag);
101      }
102  }