KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > scanners > FrameScanner


1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/scanners/FrameScanner.java,v 1.2 2004/02/10 13:41:09 woolfel Exp $
2
/*
3  * ====================================================================
4  * Copyright 2002-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */

19
20 // The developers of JMeter and Apache are greatful to the developers
21
// of HTMLParser for giving Apache Software Foundation a non-exclusive
22
// license. The performance benefits of HTMLParser are clear and the
23
// users of JMeter will benefit from the hard work the HTMLParser
24
// team. For detailed information about HTMLParser, the project is
25
// hosted on sourceforge at http://htmlparser.sourceforge.net/.
26
//
27
// HTMLParser was originally created by Somik Raha in 2000. Since then
28
// a healthy community of users has formed and helped refine the
29
// design so that it is able to tackle the difficult task of parsing
30
// dirty HTML. Derrick Oswald is the current lead developer and was kind
31
// enough to assist JMeter.
32

33 package org.htmlparser.scanners;
34
35
36 //////////////////
37
// Java Imports //
38
//////////////////
39
import java.util.Hashtable JavaDoc;
40
41 import org.htmlparser.tags.FrameTag;
42 import org.htmlparser.tags.Tag;
43 import org.htmlparser.tags.data.TagData;
44 import org.htmlparser.util.LinkProcessor;
45 import org.htmlparser.util.ParserException;
46
47 /**
48  * Scans for the Frame Tag. This is a subclass of TagScanner, and is called using a
49  * variant of the template method. If the evaluate() method returns true, that means the
50  * given string contains an image tag. Extraction is done by the scan method thereafter
51  * by the user of this class.
52  */

53 public class FrameScanner extends TagScanner
54 {
55     /**
56      * Overriding the default constructor
57      */

58     public FrameScanner()
59     {
60         super();
61     }
62     /**
63      * Overriding the constructor to accept the filter
64      */

65     public FrameScanner(String JavaDoc filter)
66     {
67         super(filter);
68     }
69     /**
70      * Extract the location of the image, given the string to be parsed, and the url
71      * of the html page in which this tag exists.
72      * @param s String to be parsed
73      * @param url URL of web page being parsed
74      */

75     public String JavaDoc extractFrameLocn(Tag tag, String JavaDoc url) throws ParserException
76     {
77         try
78         {
79             Hashtable JavaDoc table = tag.getAttributes();
80             String JavaDoc relativeFrame = (String JavaDoc) table.get("SRC");
81             if (relativeFrame == null)
82                 return "";
83             else
84                 return (new LinkProcessor()).extract(relativeFrame, url);
85         }
86         catch (Exception JavaDoc e)
87         {
88             String JavaDoc msg;
89             if (tag != null)
90                 msg = tag.getText();
91             else
92                 msg = "null";
93             throw new ParserException(
94                 "HTMLFrameScanner.extractFrameLocn() : Error in extracting frame location from tag "
95                     + msg,
96                 e);
97         }
98     }
99
100     public String JavaDoc extractFrameName(Tag tag, String JavaDoc url)
101     {
102         return tag.getAttribute("NAME");
103     }
104
105     /**
106      * @see org.htmlparser.scanners.TagScanner#getID()
107      */

108     public String JavaDoc[] getID()
109     {
110         String JavaDoc[] ids = new String JavaDoc[1];
111         ids[0] = "FRAME";
112         return ids;
113     }
114
115     protected Tag createTag(TagData tagData, Tag tag, String JavaDoc url)
116         throws ParserException
117     {
118         String JavaDoc frameUrl = extractFrameLocn(tag, url);
119         String JavaDoc frameName = extractFrameName(tag, url);
120
121         return new FrameTag(tagData, frameUrl, frameName);
122     }
123
124 }
125
Popular Tags