Source Code Cross Referenced for RegexpHTMLParser.java in  » Testing » jakarta-jmeter » org » apache » jmeter » protocol » http » parser » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta jmeter » org.apache.jmeter.protocol.http.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *   http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         * 
017:         */
018:
019:        package org.apache.jmeter.protocol.http.parser;
020:
021:        import java.net.MalformedURLException;
022:        import java.net.URL;
023:        import java.util.Iterator;
024:
025:        import org.apache.jmeter.util.JMeterUtils;
026:        import org.apache.jorphan.logging.LoggingManager;
027:        import org.apache.log.Logger;
028:
029:        // NOTE: Also looked at using Java 1.4 regexp instead of ORO. The change was
030:        // trivial. Performance did not improve -- at least not significantly.
031:        // Finally decided for ORO following advise from Stefan Bodewig (message
032:        // to jmeter-dev dated 25 Nov 2003 8:52 CET) [Jordi]
033:        import org.apache.oro.text.regex.MatchResult;
034:        import org.apache.oro.text.regex.Pattern;
035:        import org.apache.oro.text.regex.PatternMatcherInput;
036:        import org.apache.oro.text.regex.Perl5Compiler;
037:        import org.apache.oro.text.regex.Perl5Matcher;
038:
039:        /**
040:         * HtmlParser implementation using regular expressions.
041:         * <p>
042:         * This class will find RLs specified in the following ways (where <b>url</b>
043:         * represents the RL being found:
044:         * <ul>
045:         * <li>&lt;img src=<b>url</b> ... &gt;
046:         * <li>&lt;script src=<b>url</b> ... &gt;
047:         * <li>&lt;applet code=<b>url</b> ... &gt;
048:         * <li>&lt;input type=image src=<b>url</b> ... &gt;
049:         * <li>&lt;body background=<b>url</b> ... &gt;
050:         * <li>&lt;table background=<b>url</b> ... &gt;
051:         * <li>&lt;td background=<b>url</b> ... &gt;
052:         * <li>&lt;tr background=<b>url</b> ... &gt;
053:         * <li>&lt;applet ... codebase=<b>url</b> ... &gt;
054:         * <li>&lt;embed src=<b>url</b> ... &gt;
055:         * <li>&lt;embed codebase=<b>url</b> ... &gt;
056:         * <li>&lt;object codebase=<b>url</b> ... &gt;
057:         * <li>&lt;link rel=stylesheet href=<b>url</b>... gt;
058:         * <li>&lt;bgsound src=<b>url</b> ... &gt;
059:         * <li>&lt;frame src=<b>url</b> ... &gt;
060:         * </ul>
061:         * 
062:         * <p>
063:         * This class will take into account the following construct:
064:         * <ul>
065:         * <li>&lt;base href=<b>url</b>&gt;
066:         * </ul>
067:         * 
068:         * <p>
069:         * But not the following:
070:         * <ul>
071:         * <li>&lt; ... codebase=<b>url</b> ... &gt;
072:         * </ul>
073:         * 
074:         * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
075:         */
076:        class RegexpHTMLParser extends HTMLParser {
077:            private static final Logger log = LoggingManager
078:                    .getLoggerForClass();
079:
080:            /**
081:             * Regexp fragment matching a tag attribute's value (including the equals
082:             * sign and any spaces before it). Note it matches unquoted values, which to
083:             * my understanding, are not conformant to any of the HTML specifications,
084:             * but are still quite common in the web and all browsers seem to understand
085:             * them.
086:             */
087:            private static final String VALUE = "\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\"'\\s>\\\\][^\\s>]*)(?=[\\s>]))";
088:
089:            // Note there's 3 capturing groups per value
090:
091:            /**
092:             * Regexp fragment matching the separation between two tag attributes.
093:             */
094:            private static final String SEP = "\\s(?:[^>]*\\s)?";
095:
096:            /**
097:             * Regular expression used against the HTML code to find the URIs of images,
098:             * etc.:
099:             */
100:            private static final String REGEXP = "<(?:" + "!--.*?-->" + "|BASE"
101:                    + SEP
102:                    + "HREF"
103:                    + VALUE
104:                    + "|(?:IMG|SCRIPT|FRAME|IFRAME|BGSOUND|FRAME)"
105:                    + SEP
106:                    + "SRC"
107:                    + VALUE
108:                    + "|APPLET"
109:                    + SEP
110:                    + "CODE(?:BASE)?"
111:                    + VALUE
112:                    + "|(?:EMBED|OBJECT)"
113:                    + SEP
114:                    + "(?:SRC|CODEBASE)"
115:                    + VALUE
116:                    + "|(?:BODY|TABLE|TR|TD)"
117:                    + SEP
118:                    + "BACKGROUND"
119:                    + VALUE
120:                    + "|[^<]+?STYLE\\s*=['\"].*?URL\\(\\s*['\"](.+?)['\"]\\s*\\)"
121:                    + "|INPUT(?:"
122:                    + SEP
123:                    + "(?:SRC"
124:                    + VALUE
125:                    + "|TYPE\\s*=\\s*(?:\"image\"|'image'|image(?=[\\s>])))){2,}"
126:                    + "|LINK(?:"
127:                    + SEP
128:                    + "(?:HREF"
129:                    + VALUE
130:                    + "|REL\\s*=\\s*(?:\"stylesheet\"|'stylesheet'|stylesheet(?=[\\s>])))){2,}"
131:                    + ")";
132:
133:            // Number of capturing groups possibly containing Base HREFs:
134:            private static final int NUM_BASE_GROUPS = 3;
135:
136:            /**
137:             * Thread-local input:
138:             */
139:            private static ThreadLocal localInput = new ThreadLocal() {
140:                protected Object initialValue() {
141:                    return new PatternMatcherInput(new char[0]);
142:                }
143:            };
144:
145:            protected boolean isReusable() {
146:                return true;
147:            }
148:
149:            /**
150:             * Make sure to compile the regular expression upon instantiation:
151:             */
152:            protected RegexpHTMLParser() {
153:                super ();
154:            }
155:
156:            /*
157:             * (non-Javadoc)
158:             * 
159:             * @see org.apache.jmeter.protocol.http.parser.HtmlParser#getEmbeddedResourceURLs(byte[],
160:             *      java.net.URL)
161:             */
162:            public Iterator getEmbeddedResourceURLs(byte[] html, URL baseUrl,
163:                    URLCollection urls) {
164:
165:                Perl5Matcher matcher = JMeterUtils.getMatcher();
166:                PatternMatcherInput input = (PatternMatcherInput) localInput
167:                        .get();
168:                // TODO: find a way to avoid the cost of creating a String here --
169:                // probably a new PatternMatcherInput working on a byte[] would do
170:                // better.
171:                input.setInput(new String(html));
172:                Pattern pattern = JMeterUtils.getPatternCache().getPattern(
173:                        REGEXP,
174:                        Perl5Compiler.CASE_INSENSITIVE_MASK
175:                                | Perl5Compiler.SINGLELINE_MASK
176:                                | Perl5Compiler.READ_ONLY_MASK);
177:
178:                while (matcher.contains(input, pattern)) {
179:                    MatchResult match = matcher.getMatch();
180:                    String s;
181:                    if (log.isDebugEnabled())
182:                        log.debug("match groups " + match.groups() + " "
183:                                + match.toString());
184:                    // Check for a BASE HREF:
185:                    for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) {
186:                        s = match.group(g);
187:                        if (s != null) {
188:                            if (log.isDebugEnabled()) {
189:                                log.debug("new baseUrl: " + s + " - "
190:                                        + baseUrl.toString());
191:                            }
192:                            try {
193:                                baseUrl = new URL(baseUrl, s);
194:                            } catch (MalformedURLException e) {
195:                                // Doesn't even look like a URL?
196:                                // Maybe it isn't: Ignore the exception.
197:                                if (log.isDebugEnabled()) {
198:                                    log.debug("Can't build base URL from RL "
199:                                            + s + " in page " + baseUrl, e);
200:                                }
201:                            }
202:                        }
203:                    }
204:                    for (int g = NUM_BASE_GROUPS + 1; g <= match.groups(); g++) {
205:                        s = match.group(g);
206:                        if (s != null) {
207:                            if (log.isDebugEnabled()) {
208:                                log
209:                                        .debug("group " + g + " - "
210:                                                + match.group(g));
211:                            }
212:                            urls.addURL(s, baseUrl);
213:                        }
214:                    }
215:                }
216:                return urls.iterator();
217:            }
218:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.