Source Code Cross Referenced for RSSParser.java in  » RSS-RDF » RSSLib4J » org » gnu » stealthp » rsslib » 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 » RSS RDF » RSSLib4J » org.gnu.stealthp.rsslib 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //$Id: RSSParser.java,v 1.9 2004/03/28 13:07:16 taganaka Exp $
002:        package org.gnu.stealthp.rsslib;
003:
004:        import org.xml.sax.*;
005:        import javax.xml.parsers.*;
006:        import org.xml.sax.helpers.*;
007:        import java.io.*;
008:        import java.net.*;
009:
010:        /**
011:         * RSS Parser.
012:         *
013:         * <blockquote>
014:         * <em>This module, both source code and documentation, is in the
015:         * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
016:         * </blockquote>
017:         *
018:         * @since RSSLIB4J 0.1
019:         * @author Francesco aka 'Stealthp' stealthp[@]stealthp.org
020:         * @version 0.2
021:         */
022:
023:        public class RSSParser {
024:
025:            private SAXParserFactory factory = RSSFactory.getInstance();
026:            private DefaultHandler hnd;
027:            private File f;
028:            private URL u;
029:            private InputStream in;
030:            private boolean validate;
031:
032:            public RSSParser() {
033:                validate = false;
034:            }
035:
036:            /**
037:             * Set the event handler
038:             * @param h the DefaultHandler
039:             *
040:             */
041:            public void setHandler(DefaultHandler h) {
042:                hnd = h;
043:            }
044:
045:            /**
046:             * Set rss resource by local file name
047:             * @param file_name loca file name
048:             * @throws RSSException
049:             */
050:            public void setXmlResource(String file_name) throws RSSException {
051:                f = new File(file_name);
052:                try {
053:                    in = new FileInputStream(f);
054:                } catch (Exception e) {
055:                    throw new RSSException("RSSParser::setXmlResource fails: "
056:                            + e.getMessage());
057:                }
058:
059:            }
060:
061:            /**
062:             * Set rss resource by URL
063:             * @param ur the remote url
064:             * @throws RSSException
065:             */
066:            public void setXmlResource(URL ur) throws RSSException {
067:                u = ur;
068:                File ft = null;
069:                try {
070:                    URLConnection con = u.openConnection();
071:                    in = u.openStream();
072:                    if (con.getContentLength() == -1) {
073:                        this .fixZeroLength();
074:                    }
075:
076:                } catch (IOException e) {
077:                    throw new RSSException("RSSParser::setXmlResource fails: "
078:                            + e.getMessage());
079:                }
080:            }
081:
082:            /**
083:             * set true if parse have to validate the document
084:             * defoult is false
085:             * @param b true or false
086:             */
087:            public void setValidate(boolean b) {
088:                validate = b;
089:            }
090:
091:            /**
092:             * Parse rss file
093:             * @param filename local file name
094:             * @param handler the handler
095:             * @param validating validate document??
096:             * @throws RSSException
097:             */
098:            public static void parseXmlFile(String filename,
099:                    DefaultHandler handler, boolean validating)
100:                    throws RSSException {
101:                RSSParser p = new RSSParser();
102:                p.setXmlResource(filename);
103:                p.setHandler(handler);
104:                p.setValidate(validating);
105:                p.parse();
106:            }
107:
108:            /**
109:             * Parse rss file from a url
110:             * @param remote_url remote rss file
111:             * @param handler the handler
112:             * @param validating validate document??
113:             * @throws RSSException
114:             */
115:            public static void parseXmlFile(URL remote_url,
116:                    DefaultHandler handler, boolean validating)
117:                    throws RSSException {
118:                RSSParser p = new RSSParser();
119:                p.setXmlResource(remote_url);
120:                p.setHandler(handler);
121:                p.setValidate(validating);
122:                p.parse();
123:            }
124:
125:            /**
126:             * Try to fix null length bug
127:             * @throws IOException
128:             * @throws RSSException
129:             */
130:            private void fixZeroLength() throws IOException, RSSException {
131:
132:                File ft = File.createTempFile(".rsslib4jbugfix", ".tmp");
133:                ft.deleteOnExit();
134:                FileWriter fw = new FileWriter(ft);
135:                BufferedReader reader = new BufferedReader(
136:                        new InputStreamReader(in));
137:                BufferedWriter out = new BufferedWriter(fw);
138:                String line = "";
139:                while ((line = reader.readLine()) != null) {
140:                    out.write(line + "\n");
141:                }
142:                out.flush();
143:                out.close();
144:                reader.close();
145:                fw.close();
146:                setXmlResource(ft.getAbsolutePath());
147:
148:            }
149:
150:            /**
151:             * Call it at the end of the work to preserve memory
152:             */
153:            public void free() {
154:                this .factory = null;
155:                this .f = null;
156:                this .in = null;
157:                this .hnd = null;
158:                System.gc();
159:            }
160:
161:            /**
162:             * Parse the documen
163:             * @throws RSSException
164:             */
165:            public void parse() throws RSSException {
166:                try {
167:                    factory.setValidating(validate);
168:                    // Create the builder and parse the file
169:                    factory.newSAXParser().parse(in, hnd);
170:                } catch (SAXException e) {
171:                    throw new RSSException("RSSParser::parse fails: "
172:                            + e.getMessage());
173:                } catch (ParserConfigurationException e) {
174:                    throw new RSSException("RSSParser::parse fails: "
175:                            + e.getMessage());
176:                } catch (IOException e) {
177:                    throw new RSSException("RSSParser::parse fails: "
178:                            + e.getMessage());
179:                }
180:
181:            }
182:
183:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.