Source Code Cross Referenced for UrlUtils.java in  » Testing » htmlunit » com » gargoylesoftware » htmlunit » util » 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 » htmlunit » com.gargoylesoftware.htmlunit.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003:         *
004:         * Redistribution and use in source and binary forms, with or without
005:         * modification, are permitted provided that the following conditions are met:
006:         *
007:         * 1. Redistributions of source code must retain the above copyright notice,
008:         *    this list of conditions and the following disclaimer.
009:         * 2. Redistributions in binary form must reproduce the above copyright notice,
010:         *    this list of conditions and the following disclaimer in the documentation
011:         *    and/or other materials provided with the distribution.
012:         * 3. The end-user documentation included with the redistribution, if any, must
013:         *    include the following acknowledgment:
014:         *
015:         *       "This product includes software developed by Gargoyle Software Inc.
016:         *        (http://www.GargoyleSoftware.com/)."
017:         *
018:         *    Alternately, this acknowledgment may appear in the software itself, if
019:         *    and wherever such third-party acknowledgments normally appear.
020:         * 4. The name "Gargoyle Software" must not be used to endorse or promote
021:         *    products derived from this software without prior written permission.
022:         *    For written permission, please contact info@GargoyleSoftware.com.
023:         * 5. Products derived from this software may not be called "HtmlUnit", nor may
024:         *    "HtmlUnit" appear in their name, without prior written permission of
025:         *    Gargoyle Software Inc.
026:         *
027:         * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028:         * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029:         * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030:         * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033:         * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034:         * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035:         * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036:         * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037:         */
038:        package com.gargoylesoftware.htmlunit.util;
039:
040:        import java.net.MalformedURLException;
041:        import java.net.URL;
042:
043:        /**
044:         * URL utilities class that makes it easy to create new URLs based off of old URLs
045:         * without having to assemble or parse them yourself.
046:         *
047:         * @version $Revision: 2132 $
048:         * @author Daniel Gredler
049:         */
050:        public final class UrlUtils {
051:
052:            /**
053:             * Disallow instantiation of this class.
054:             */
055:            private UrlUtils() {
056:                // Empty.
057:            }
058:
059:            /**
060:             * Creates and returns a new URL identical to the specified URL, except using the specified protocol.
061:             * @param u The URL on which to base the returned URL.
062:             * @param newProtocol The new protocol to use in the returned URL.
063:             * @return A new URL identical to the specified URL, except using the specified protocol.
064:             * @throws MalformedURLException If there is a problem creating the new URL.
065:             */
066:            public static URL getUrlWithNewProtocol(final URL u,
067:                    final String newProtocol) throws MalformedURLException {
068:                return createNewUrl(newProtocol, u.getHost(), u.getPort(), u
069:                        .getPath(), u.getRef(), u.getQuery());
070:            }
071:
072:            /**
073:             * Creates and returns a new URL identical to the specified URL, except using the specified host.
074:             * @param u The URL on which to base the returned URL.
075:             * @param newHost The new host to use in the returned URL.
076:             * @return A new URL identical to the specified URL, except using the specified host.
077:             * @throws MalformedURLException If there is a problem creating the new URL.
078:             */
079:            public static URL getUrlWithNewHost(final URL u,
080:                    final String newHost) throws MalformedURLException {
081:                return createNewUrl(u.getProtocol(), newHost, u.getPort(), u
082:                        .getPath(), u.getRef(), u.getQuery());
083:            }
084:
085:            /**
086:             * Creates and returns a new URL identical to the specified URL, except using the specified port.
087:             * @param u The URL on which to base the returned URL.
088:             * @param newPort The new port to use in the returned URL.
089:             * @return A new URL identical to the specified URL, except using the specified port.
090:             * @throws MalformedURLException If there is a problem creating the new URL.
091:             */
092:            public static URL getUrlWithNewPort(final URL u, final int newPort)
093:                    throws MalformedURLException {
094:                return createNewUrl(u.getProtocol(), u.getHost(), newPort, u
095:                        .getPath(), u.getRef(), u.getQuery());
096:            }
097:
098:            /**
099:             * Creates and returns a new URL identical to the specified URL, except using the specified path.
100:             * @param u The URL on which to base the returned URL.
101:             * @param newPath The new path to use in the returned URL.
102:             * @return A new URL identical to the specified URL, except using the specified path.
103:             * @throws MalformedURLException If there is a problem creating the new URL.
104:             */
105:            public static URL getUrlWithNewPath(final URL u,
106:                    final String newPath) throws MalformedURLException {
107:                return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(),
108:                        newPath, u.getRef(), u.getQuery());
109:            }
110:
111:            /**
112:             * Creates and returns a new URL identical to the specified URL, except using the specified reference.
113:             * @param u The URL on which to base the returned URL.
114:             * @param newRef The new reference to use in the returned URL.
115:             * @return A new URL identical to the specified URL, except using the specified reference.
116:             * @throws MalformedURLException If there is a problem creating the new URL.
117:             */
118:            public static URL getUrlWithNewRef(final URL u, final String newRef)
119:                    throws MalformedURLException {
120:                return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(),
121:                        u.getPath(), newRef, u.getQuery());
122:            }
123:
124:            /**
125:             * Creates and returns a new URL identical to the specified URL, except using the specified query string.
126:             * @param u The URL on which to base the returned URL.
127:             * @param newQuery The new query string to use in the returned URL.
128:             * @return A new URL identical to the specified URL, except using the specified query string.
129:             * @throws MalformedURLException If there is a problem creating the new URL.
130:             */
131:            public static URL getUrlWithNewQuery(final URL u,
132:                    final String newQuery) throws MalformedURLException {
133:                return createNewUrl(u.getProtocol(), u.getHost(), u.getPort(),
134:                        u.getPath(), u.getRef(), newQuery);
135:            }
136:
137:            /**
138:             * Creates a new URL based on the specified fragments.
139:             * @param protocol The protocol to use. May not be <tt>null</tt>.
140:             * @param host The host to use. May not be <tt>null</tt>.
141:             * @param port The port to use. May be <tt>-1</tt> if no port is specified.
142:             * @param path The path to use. May be <tt>null</tt> and may omit the initial <tt>'/'</tt>.
143:             * @param ref The reference to use. May be <tt>null</tt>. Must not include the <tt>'#'</tt>.
144:             * @param query The query to use. May be <tt>null</tt>. Must not include the <tt>'?'</tt>.
145:             * @return A new URL based on the specified fragments.
146:             * @throws MalformedURLException If there is a problem creating the new URL.
147:             */
148:            private static URL createNewUrl(final String protocol,
149:                    final String host, final int port, final String path,
150:                    final String ref, final String query)
151:                    throws MalformedURLException {
152:                final StringBuffer s = new StringBuffer();
153:                s.append(protocol);
154:                s.append("://");
155:                s.append(host);
156:                if (port != -1) {
157:                    s.append(":").append(port);
158:                }
159:                if (path != null && path.length() > 0) {
160:                    if (!path.startsWith("/")) {
161:                        s.append("/");
162:                    }
163:                    s.append(path);
164:                }
165:                if (query != null) {
166:                    s.append("?").append(query);
167:                }
168:                if (ref != null) {
169:                    s.append("#").append(ref);
170:                }
171:                final URL url = new URL(s.toString());
172:                return url;
173:            }
174:
175:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.