Source code: org/htmlparser/scanners/BgSoundScanner.java
1 // $Header: /home/cvs/jakarta-jmeter/src/htmlparser/org/htmlparser/scanners/BgSoundScanner.java,v 1.2 2005/07/12 20:50:30 mstover1 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 package org.htmlparser.scanners;
33
34 //////////////////
35 // Java Imports //
36 //////////////////
37 import java.util.Hashtable;
38
39 import org.htmlparser.tags.BgSoundTag;
40 import org.htmlparser.tags.Tag;
41 import org.htmlparser.tags.data.TagData;
42 import org.htmlparser.util.LinkProcessor;
43 import org.htmlparser.util.ParserException;
44 import org.htmlparser.util.ParserUtils;
45
46 /**
47 * Scans for the bgsound Tag. This is a subclass of TagScanner, and is called
48 * using a variant of the template method. If the evaluate() method returns
49 * true, that means the given string contains an bgsound tag. Extraction is done
50 * by the scan method thereafter by the user of this class.
51 */
52 public class BgSoundScanner extends TagScanner {
53 public static final String BGSOUND_SCANNER_ID = "BGSOUND";
54
55 private Hashtable table;
56
57 private LinkProcessor processor;
58
59 /**
60 * Overriding the default constructor
61 */
62 public BgSoundScanner() {
63 super();
64 processor = new LinkProcessor();
65 }
66
67 /**
68 * Overriding the constructor to accept the filter
69 */
70 public BgSoundScanner(String filter, LinkProcessor processor) {
71 super(filter);
72 this.processor = processor;
73 }
74
75 /**
76 * Extract the location of the bgsound, given the string to be parsed, and
77 * the url of the html page in which this tag exists.
78 *
79 * @param s
80 * String to be parsed
81 * @param url
82 * URL of web page being parsed
83 */
84 public String extractBgSoundLocn(Tag tag, String url) throws ParserException {
85 String relativeLink = null;
86 try {
87 table = tag.getAttributes();
88 relativeLink = (String) table.get("SRC");
89
90 if (relativeLink != null) {
91 relativeLink = ParserUtils.removeChars(relativeLink, '\n');
92 relativeLink = ParserUtils.removeChars(relativeLink, '\r');
93 }
94 if (relativeLink == null || relativeLink.length() == 0) {
95 // try fix
96 String tagText = tag.getText().toUpperCase();
97 int indexSrc = tagText.indexOf("SRC");
98 if (indexSrc != -1) {
99 // There is a missing equals.
100 tag.setText(tag.getText().substring(0, indexSrc + 3) + "="
101 + tag.getText().substring(indexSrc + 3, tag.getText().length()));
102 table = tag.redoParseAttributes();
103 relativeLink = (String) table.get("SRC");
104
105 }
106 }
107 if (relativeLink == null)
108 return "";
109 else
110 return processor.extract(relativeLink, url);
111 } catch (Exception e) {
112 throw new ParserException(
113 "HTMLbgsoundScanner.extractbgsoundLocn() : Error in extracting image location, relativeLink = "
114 + relativeLink + ", url = " + url, e);
115 }
116 }
117
118 public String[] getID() {
119 String[] ids = new String[1];
120 ids[0] = BGSOUND_SCANNER_ID;
121 return ids;
122 }
123
124 protected Tag createTag(TagData tagData, Tag tag, String url) throws ParserException {
125 String link = extractBgSoundLocn(tag, url);
126 return new BgSoundTag(tagData, link);
127 }
128
129 }