Source Code Cross Referenced for FormFiller.java in  » Web-Crawler » JoBo » net » matuschek » html » 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 » JoBo » net.matuschek.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.matuschek.html;
002:
003:        /*************************************************
004:         Copyright (c) 2001/2002 by Daniel Matuschek
005:         **************************************************/
006:
007:        import java.net.MalformedURLException;
008:        import java.net.URL;
009:        import java.util.Vector;
010:
011:        import net.matuschek.http.ExtendedURL;
012:        import net.matuschek.http.HttpConstants;
013:
014:        import org.apache.log4j.Category;
015:
016:        import org.w3c.dom.Element;
017:        import org.w3c.dom.NodeList;
018:
019:        /**
020:         * This class fills out form fields by predefined values
021:         *
022:         * @author Daniel Matuschek 
023:         * @version $Id: FormFiller.java,v 1.9 2003/01/30 14:54:51 oliver_schmidt Exp $
024:         */
025:        public class FormFiller {
026:
027:            /** 
028:             * Form Handlers 
029:             * @link aggregation
030:             * @associates <{FormHandler}>
031:             */
032:            private Vector formHandlers = null;
033:
034:            /** Log4J category for logging purposes */
035:            private Category log;
036:
037:            /**
038:             * Basic initialization 
039:             */
040:            public FormFiller() {
041:                log = Category.getInstance(getClass().getName());
042:            }
043:
044:            /** 
045:             * Initializes the form filler with a given list of form handlers
046:             * 
047:             * @param formHandlers a Vector containing FormHandler objects
048:             */
049:            public FormFiller(Vector formHandler) {
050:                this ();
051:                this .formHandlers = formHandler;
052:            }
053:
054:            /**
055:             * Sets the list of form handlers
056:             * @param formHandlers a Vector containing FormHandler objects
057:             */
058:            public void setFormHandlers(Vector formHandlers) {
059:                this .formHandlers = formHandlers;
060:            }
061:
062:            /**
063:             * Gets the list of form handlers
064:             * @return a Vector containing FormHandler objects
065:             */
066:            public Vector getFormHandlers() {
067:                return this .formHandlers;
068:            }
069:
070:            /**
071:             * Tries to fill the given form with values
072:             *
073:             * @param baseURL the URL of the form itself. needed for relative adressing
074:             * @param form a element node containing a DOM description of a form 
075:             * (e.g. from a DOM parser or HTML Tidy)
076:             *
077:             * @return a form filled with values or null, if no form handler was found
078:             */
079:            public ExtendedURL fillForm(URL baseURL, Element form) {
080:                ExtendedURL eurl = new ExtendedURL();
081:                String formURL = form.getAttribute("action");
082:                String type = form.getAttribute("method");
083:                FormHandler handler;
084:                URL absoluteFormURL = null;
085:
086:                try {
087:                    absoluteFormURL = new URL(baseURL, formURL);
088:                } catch (MalformedURLException e) {
089:                    log.info("MalformedURLException in fillForm(): "
090:                            + e.getMessage());
091:                }
092:
093:                if (!form.getNodeName().equals("form")) {
094:                    log.error("not a form !");
095:                    return null;
096:                }
097:
098:                handler = getFormHandler(absoluteFormURL.toString());
099:                if (handler == null) {
100:                    log.debug("found no form handler for URL " + formURL);
101:                    return null;
102:                }
103:
104:                if (type.equalsIgnoreCase("get")) {
105:                    eurl.setRequestMethod(HttpConstants.GET);
106:                } else if (type.equalsIgnoreCase("post")) {
107:                    eurl.setRequestMethod(HttpConstants.POST);
108:                } else if (type.equals("")) {
109:                    // workaround for sites that have no "action" attribute
110:                    // in their forms, like Google :-(
111:                    eurl.setRequestMethod(HttpConstants.GET);
112:                } else {
113:                    log.debug("method " + type + " unknown");
114:                    return null;
115:                }
116:
117:                try {
118:                    eurl.setURL(absoluteFormURL);
119:                } catch (Exception e) {
120:                    log.debug("error calculating URL: " + e.getMessage());
121:                }
122:
123:                // clear the old data in this form handler
124:                handler.clearValues();
125:
126:                // okay, now fill the form fields ...
127:                collectInputFields(form, handler);
128:                eurl.setParams(handler.getParamString());
129:
130:                return eurl;
131:            }
132:
133:            /** 
134:             * Get 
135:             */
136:            private void collectInputFields(Element element, FormHandler fh) {
137:                // this should not happen !
138:                if (element == null) {
139:                    log.error("got a null element");
140:                    return;
141:                }
142:
143:                if (element.getNodeName().equals("input")) {
144:
145:                    String type = element.getAttribute("type").toLowerCase();
146:                    String name = element.getAttribute("name");
147:                    String value = element.getAttribute("value");
148:
149:                    // ignore reset tags
150:                    if (!type.equals("reset")) {
151:
152:                        // must have a name
153:                        if ((name != null) && (!name.equals(""))) {
154:
155:                            // must have a value
156:                            if ((value != null) && (!value.equals(""))) {
157:
158:                                // add this value to the form handler
159:                                fh.addValue(name, value);
160:
161:                            }
162:
163:                        }
164:
165:                    }
166:
167:                }
168:
169:                // recursive travel through all childs
170:                NodeList childs = element.getChildNodes();
171:
172:                for (int i = 0; i < childs.getLength(); i++) {
173:                    if (childs.item(i) instanceof  Element) {
174:                        collectInputFields((Element) childs.item(i), fh);
175:                    }
176:                }
177:
178:            }
179:
180:            /**
181:             * Gets a form handler for a given URL
182:             * @param u an URL
183:             * @return a FormHandler object, if there is a registered FormHandler
184:             * for this URL, null otherwise
185:             */
186:            protected FormHandler getFormHandler(String url) {
187:                if (url == null) {
188:                    return null;
189:                }
190:
191:                if (formHandlers == null) {
192:                    return null;
193:                }
194:
195:                for (int i = 0; i < formHandlers.size(); i++) {
196:                    FormHandler fh = (FormHandler) formHandlers.elementAt(i);
197:                    if (fh.getUrl().toString().equals(url)) {
198:                        return fh;
199:                    }
200:                }
201:
202:                return null;
203:            }
204:
205:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.