001 /* sillyview : a free model-view-controller system for Java 002 * Copyright (C) 2004 T.J. Willis 003 * 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License 006 * as published by the Free Software Foundation; either version 2 007 * of the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. 017 * 018 * $Header: /cvsroot/sillyview/sillyview/src/net/sourceforge/sillyview/URLParser.java,v 1.2 2004/05/15 01:58:51 tj_willis Exp $ 019 */ 020 package net.sourceforge.sillyview; 021 022 import java.net.*; 023 import java.util.*; 024 import java.lang.reflect.Array; 025 import org.apache.log4j.*; 026 027 /** 028 * Provides some static methods for dealing with URLs passed from a View. 029 * 030 * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a> 031 * @version 1.0 032 */ 033 public class URLParser { 034 static String preToken = ""; 035 static String postToken = ""; 036 private static Category cat 037 = Category.getInstance(URLParser.class.getName()); 038 039 040 public static void setPreToken (String s) { 041 preToken = s; 042 } 043 044 public static void setPostToken (String s) { 045 postToken = s; 046 } 047 048 /** 049 * Takes the name/value pairs of the URL's query and returns them 050 * as a Hashtable. 051 * 052 * @param u an <code>URL</code> value 053 * @return a <code>Hashtable</code> value 054 */ 055 public static Hashtable parseVariables (URL u) { 056 Hashtable < String, String > h = new Hashtable < String, String > (); 057 if (u==null) 058 return null; 059 String x = u.getQuery (); 060 return parseVariables (x); 061 } 062 063 /** 064 * Parses a set of name/value pairs in the form of "name=value&foo=bar" 065 * into a Hashtable and returns it. 066 * 067 * @param u an <code>String</code> value 068 * @return a <code>Hashtable</code> value 069 */ 070 public static Hashtable<String,String> parseVariables (String x) { 071 Hashtable < String, String > h = new Hashtable < String, String > (); 072 if (x==null) 073 return null; 074 String arr[] = x.split ("&"); 075 if (arr==null) 076 return null; 077 int size = Array.getLength (arr); 078 if (size < 1) 079 return null; 080 for (String y:arr) { 081 082 String z[] = y.split ("="); 083 if (z == null) 084 continue; 085 int sz = Array.getLength (z); 086 //System.out.println("sz = " + sz); 087 if (sz != 2) 088 continue; 089 try { 090 String nm = 091 preToken + URLDecoder.decode (z[0], "UTF-8") + postToken; 092 String va = URLDecoder.decode (z[1], "UTF-8"); 093 h.put (nm, va); 094 } catch (java.io.UnsupportedEncodingException ex) { 095 cat.error("Encoding " + y,ex); 096 } 097 } 098 099 return h; 100 101 } 102 103 /** 104 * Strips the last element of the URL's path and returns it. 105 * Given, for example, the URL: 106 * "http://foo.bar.com/some/dir/lala.jpg?foo=bar" 107 * will return "lala.jpg". 108 * 109 * @param u an <code>URL</code> value 110 * @return a <code>String</code> value 111 */ 112 public static String getFilenameWithoutPath (URL u) { 113 if (u == null) 114 return null; 115 return getFilenameWithoutPath (u.getPath ()); 116 } 117 118 /** 119 * Given, for example, the string 120 * "http://foo.bar.com/some/dir/lala.jpg?foo=bar" 121 * will return "lala.jpg". 122 * 123 * @param x a <code>String</code> value 124 * @return a <code>String</code> value 125 */ 126 public static String getFilenameWithoutPath (String x) { 127 if (x == null) 128 return null; 129 String y[] = x.split ("/"); 130 int sz = Array.getLength (y); 131 if (sz < 1) 132 return null; 133 return y[sz - 1]; 134 } 135 136 }