Source Code Cross Referenced for WebPageXtractor.java in  » Web-Crawler » Arachnid » bplatt » spider » 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 » Web Crawler » Arachnid » bplatt.spider 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package bplatt.spider;
002:
003:        /**
004:         * WebPageXtractor - extracts information from a WebPage
005:         * passed as an input stream.  Makes use of SimpleHTMLParser
006:         * object.  Used to use HTMLEditorKit and HTMLEditorKit.Parser. 
007:         * This turned out to be too buggy for this application.
008:         * Cannot use XML parser as HTML does not follow stricter XML
009:         * syntax rules.  In fact many Web pages are a "tag salad" that
010:         * don't even follow proper HTML syntax.  WebPageXtractor parses
011:         * a page and extracts links, images, and title(s).
012:         * 
013:         * Copyright 2002, Robert L. Platt, All rights reserved
014:         * @author Robert L. Platt 
015:         * 
016:         * This program is free software; you can redistribute it and/or modify
017:         * it under the terms of the GNU General Public License as published by
018:         * the Free Software Foundation; either version 2 of the License, or
019:         * (at your option) any later version.
020:         *
021:         * This program is distributed in the hope that it will be useful,
022:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
023:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
024:         * GNU General Public License for more details.
025:         *
026:         * You should have received a copy of the GNU General Public License
027:         * along with this program; if not, write to the Free Software
028:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029:         */
030:
031:        import java.io.*;
032:        import java.util.*;
033:
034:        public class WebPageXtractor extends SimpleHTMLParser {
035:            private ArrayList links;
036:            private ArrayList images;
037:            private ArrayList title;
038:            private boolean inTitle;
039:
040:            /** Constructor */
041:            public WebPageXtractor() {
042:                super ();
043:                links = new ArrayList();
044:                images = new ArrayList();
045:                title = new ArrayList();
046:            }
047:
048:            /**
049:             * If we're within TITLE tags - save the title
050:             * @see SimpleHTMLParser#processContent(SimpleHTMLToken)
051:             */
052:            public void processContent(SimpleHTMLToken token) {
053:                String s = token.getContent().trim();
054:                if (s != null && s.length() != 0) {
055:                    if (inTitle)
056:                        title.add(s);
057:                }
058:            }
059:
060:            /**
061:             * Look for </title> tags
062:             * @see SimpleHTMLParser#processEndTag(SimpleHTMLToken)
063:             */
064:            public void processEndTag(SimpleHTMLToken token) throws IOException {
065:                String tag = SimpleHTMLParser.getTagType(token, true);
066:                if (tag == null)
067:                    throw new IOException("HTML parsing error");
068:                else if (tag.equals("title"))
069:                    inTitle = false;
070:            }
071:
072:            /**
073:             * Handle Anchor, Image, Frame, and Title tags
074:             * @see SimpleHTMLParser#processTag(SimpleHTMLToken)
075:             */
076:            public void processTag(SimpleHTMLToken token) throws IOException {
077:                String tag = SimpleHTMLParser.getTagType(token, true);
078:                if (tag == null)
079:                    throw new IOException("HTML parsing error");
080:                else if (tag.equals("a")) {
081:                    String link = extractHref(token.getContent());
082:                    if (link != null)
083:                        links.add(link);
084:                } else if (tag.equals("img")) {
085:                    String image = extractSrc(token.getContent());
086:                    if (image != null)
087:                        images.add(image);
088:                } else if (tag.equals("frame")) {
089:                    String link = extractSrc(token.getContent());
090:                    if (link != null)
091:                        links.add(link);
092:                } else if (tag.equals("title"))
093:                    inTitle = true;
094:            }
095:
096:            // Utility method for extracting href attribute
097:            private String extractHref(String tag) {
098:                String delims = "\t\r\f\n \'\"=";
099:                StringTokenizer tt = new StringTokenizer(tag, delims);
100:                while (tt.hasMoreElements()) {
101:                    String s = tt.nextToken();
102:                    if (s.equalsIgnoreCase("href")) {
103:                        if (!tt.hasMoreElements())
104:                            return (null);
105:                        else
106:                            return (tt.nextToken());
107:                    }
108:                }
109:                return (null);
110:            }
111:
112:            // Utility method for extracting src attribute
113:            private String extractSrc(String tag) {
114:                String delims = "\t\r\f\n \'\"=";
115:                StringTokenizer tt = new StringTokenizer(tag, delims);
116:                while (tt.hasMoreElements()) {
117:                    String s = tt.nextToken();
118:                    if (s.equalsIgnoreCase("src")) {
119:                        if (!tt.hasMoreElements())
120:                            return (null);
121:                        else
122:                            return (tt.nextToken());
123:                    }
124:                }
125:                return (null);
126:            }
127:
128:            /**
129:             * Returns the images.
130:             * @return ArrayList
131:             */
132:            public ArrayList getImages() {
133:                return images;
134:            }
135:
136:            /**
137:             * Returns the links.
138:             * @return ArrayList
139:             */
140:            public ArrayList getLinks() {
141:                return links;
142:            }
143:
144:            /**
145:             * Returns the title.
146:             * @return ArrayList
147:             */
148:            public ArrayList getTitle() {
149:                return title;
150:            }
151:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.