Skip to content

Commit e82634f

Browse files
committed
- Updated logic used to encode url query string parameters in the URL class.
- Fixed bug setting url port in the html parser MapPath() method. - Updated documentation in the File and Generator classes. git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1035 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent 60c5a1f commit e82634f

File tree

4 files changed

+81
-43
lines changed

4 files changed

+81
-43
lines changed

src/javaxt/html/Parser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ public static String MapPath(String relPath, java.net.URL url){
350350
String[] arrRelPath = relPath.split("/");
351351
try{
352352
String urlBase = url.getProtocol() + "://" + url.getHost();
353-
if (url.getPort()!=80) urlBase+= ":" + url.getPort();
353+
int port = url.getPort();
354+
if (port>0 && port!=80) urlBase+= ":" + url.getPort();
354355

355356

356357

src/javaxt/io/File.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,20 @@ private String getHash(String algorithm){
865865
//**************************************************************************
866866
//** write
867867
//**************************************************************************
868+
/** Creates a new file using the given ByteArrayOutputStream. Note that this
869+
* method will convert the ByteArrayOutputStream to a byte array using the
870+
* toByteArray() method which may result in memory issues.
871+
*/
868872
public void write(ByteArrayOutputStream bas){
869873
write(bas.toByteArray());
870874
}
871875

872-
876+
877+
//**************************************************************************
878+
//** write
879+
//**************************************************************************
880+
/** Creates a new file using the given byte array.
881+
*/
873882
public void write(byte[] bytes){
874883
java.io.File File = getFile();
875884
attr = null;
@@ -887,8 +896,14 @@ public void write(byte[] bytes){
887896
}
888897
}
889898
}
890-
891-
899+
900+
901+
//**************************************************************************
902+
//** write
903+
//**************************************************************************
904+
/** Creates a new file using the given InputStream. Note that the caller is
905+
* responsible for closing the input stream after the method is complete.
906+
*/
892907
public void write(InputStream input){
893908
java.io.File File = getFile();
894909
attr = null;
@@ -973,7 +988,9 @@ public void write(org.w3c.dom.Document xml){
973988
//**************************************************************************
974989
//** Write Text
975990
//**************************************************************************
976-
991+
/** Used to write text to a file using an array of strings. A new line is
992+
* created for each entry in the array.
993+
*/
977994
public void write(String[] Content){
978995
java.io.File File = getFile();
979996
attr = null;
@@ -1116,7 +1133,6 @@ public FileOutputStream getOutputStream() throws IOException{
11161133
/** Returns the full file path (including the file name) */
11171134

11181135
public String toString(){
1119-
//if (file!=null) return file.toString();
11201136
return path + name;
11211137
}
11221138

src/javaxt/utils/Generator.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@
1111
* one entry at a time, this iterator can help avoid out of memory exceptions.
1212
*
1313
* Subclasses must define a method called {@link #run()} and may call
14-
* {@link yield(T)} to return values one at a time.
14+
* {@link yield(T)} to return values one at a time. Example:
15+
<pre>
16+
Generator&lt;String&gt; generator = new Generator&lt;String&gt;() {
17+
18+
@Override
19+
public void run() {
20+
21+
BufferedReader br = file.getBufferedReader("UTF-8");
22+
23+
String row;
24+
while ((row = br.readLine()) != null){
25+
yield(row);
26+
}
27+
28+
br.close();
29+
}
30+
};
31+
</pre>
1532
*
1633
* @author Adrian Kuhn &lt;akuhn(at)iam.unibe.ch&gt;
1734
*

src/javaxt/utils/URL.java

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,7 @@ public static HashMap<String, List<String>> parseQueryString(String query){
121121

122122

123123
//Decode the querystring. Note that the urlDecoder doesn't decode everything (e.g. "&amp;")
124-
try{
125-
query = java.net.URLDecoder.decode(query, "UTF-8");
126-
}
127-
catch(Exception e){
128-
//This should never happen. Try to decode the string manually?
129-
String find[] = new String[]{"%2C","%2F","%3A"};
130-
String replace[] = new String[]{",","/",":"};
131-
for (int i=0; i<find.length; i++){
132-
query = query.replace(find[i],replace[i]);
133-
}
134-
}
124+
query = decode(query);
135125

136126
//Special case for query strings with "&amp;" instead of "&" delimiters
137127
boolean amp = query.contains("&amp;");
@@ -180,16 +170,42 @@ public static HashMap<String, List<String>> parseQueryString(String query){
180170

181171
return parameters;
182172
}
183-
173+
174+
private static String decode(String str){
175+
try{
176+
return java.net.URLDecoder.decode(str, "UTF-8");
177+
}
178+
catch(Exception e){
179+
//This should never happen. Try to decode the string manually?
180+
String find[] = new String[]{"%2C","%2F","%3A"};
181+
String replace[] = new String[]{",","/",":"};
182+
for (int i=0; i<find.length; i++){
183+
str = str.replace(find[i],replace[i]);
184+
}
185+
return str;
186+
}
187+
}
188+
189+
private static String encode(String str){
190+
try{
191+
return java.net.URLEncoder.encode(str, "UTF-8").replaceAll("\\+", "%20");
192+
}
193+
catch(Exception e){
194+
return str;
195+
}
196+
}
197+
184198

185199
//**************************************************************************
186200
//** setParameter
187201
//**************************************************************************
188-
/** Used to set or update a value for a given parameter. If append is true,
189-
* the value will be added to other values for this key.
202+
/** Used to set or update a value for a given parameter in the query string.
203+
* If append is true, the value will be added to other values for this key.
190204
*/
191205
public void setParameter(String key, String value, boolean append){
192206

207+
if (value!=null) value = decode(value);
208+
193209
key = key.toLowerCase();
194210
if (append){
195211
List<String> values = parameters.get(key);
@@ -393,14 +409,18 @@ public String getQueryString(){
393409
value = null;
394410
}
395411
}
396-
str.append(key);
412+
413+
414+
str.append(encode(key));
397415
if (value!=null){
398-
str.append("=" + value);
416+
str.append("=");
417+
str.append(encode(value));
399418
}
419+
420+
400421
if (it.hasNext()) str.append("&");
401422
}
402423
return str.toString();
403-
404424
}
405425

406426

@@ -614,25 +634,9 @@ public java.net.URL toURL(){
614634

615635

616636
//Encode and append QueryString as needed
617-
java.util.HashSet<String> keys = getKeys();
618-
java.util.Iterator<String> it = keys.iterator();
619-
if (it.hasNext()){
620-
StringBuffer str = new StringBuffer();
621-
while (it.hasNext()){
622-
String key = it.next();
623-
String value = this.getParameter(key);
624-
if (value.length()==0){
625-
if (parameters.get(key.toLowerCase())==null){
626-
value = null;
627-
}
628-
}
629-
str.append(java.net.URLEncoder.encode(key, "UTF-8"));
630-
if (value!=null){
631-
str.append("=" + java.net.URLEncoder.encode(value, "UTF-8"));
632-
}
633-
if (it.hasNext()) str.append("&");
634-
}
635-
url = new java.net.URL(url.toString() + "?" + str.toString());
637+
String query = getQueryString();
638+
if (query.length()>0){
639+
url = new java.net.URL(url.toString() + "?" + query);
636640
}
637641

638642
}

0 commit comments

Comments
 (0)