Permalink
Browse files

Fix issue #216 #217 and #218 by decoding parameters correctly and han…

…dling the empty string case
  • Loading branch information...
ndw committed Jul 30, 2015
1 parent 3600f8b commit b0507a0233ce370ebbb8d7eebe59a44f467b1fa8
Showing with 37 additions and 45 deletions.
  1. +37 −45 src/main/java/com/xmlcalabash/library/WWWFormURLDecode.java
@@ -30,6 +30,9 @@
import net.sf.saxon.s9api.QName;
import com.xmlcalabash.runtime.XAtomicStep;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
/**
*
* @author ndw
@@ -44,6 +47,7 @@
public static final QName _name = new QName("", "name");
public static final QName c_paramset = new QName("c",XProcConstants.NS_XPROC_STEP,"param-set");
public static final QName c_param = new QName("c", XProcConstants.NS_XPROC_STEP, "param");
private static final QName cx_encoding = new QName("cx", XProcConstants.NS_CALABASH_EX, "encoding");
private WritablePipe result = null;
/** Creates a new instance of FormURLDecode */
@@ -64,62 +68,50 @@ public void run() throws SaxonApiException {
String value = getOption(_value).getString();
String encoding = step.getExtensionAttribute(cx_encoding);
if (encoding == null) {
encoding = "UTF-8";
}
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
tree.addStartElement(c_paramset);
tree.startContent();
String[] params = value.split("&");
for (int idx = 0; idx < params.length; idx++) {
String p = params[idx];
int pos = p.indexOf("=");
if (pos > 0) {
String name = p.substring(0, pos);
String val = p.substring(pos+1);
try {
TypeUtils.checkType(runtime, name, XProcConstants.xs_NCName, null);
} catch (XProcException e) {
throw XProcException.stepError(61);
if (!"".equals(value)) {
String[] params = value.split("&");
for (String p : params) {
int pos = p.indexOf("=");
if (pos > 0) {
String name = p.substring(0, pos);
String val = p.substring(pos+1);
try {
name = URLDecoder.decode(name, encoding);
val = URLDecoder.decode(val, encoding);
} catch (UnsupportedEncodingException e) {
throw new XProcException(e);
}
try {
TypeUtils.checkType(runtime, name, XProcConstants.xs_NCName, null);
} catch (XProcException e) {
throw XProcException.stepError(61);
}
tree.addStartElement(c_param);
tree.addAttribute(_name, name);
tree.addAttribute(_value, val);
tree.startContent();
tree.addEndElement();
} else {
throw new XProcException(step.getNode(), "Badly formatted parameters");
}
tree.addStartElement(c_param);
tree.addAttribute(_name, name);
tree.addAttribute(_value, decode(val));
tree.startContent();
tree.addEndElement();
} else {
throw new XProcException(step.getNode(), "Badly formatted parameters");
}
}
tree.addEndElement();
tree.endDocument();
result.write(tree.getResult());
}
private String decode(String val) {
int pos;
String result = "";
while ((pos = val.indexOf("%")) >= 0) {
result += val.substring(0, pos);
try {
String digits = val.substring(pos+1,pos+3);
int dec = Integer.parseInt(digits, 16);
char ch = (char) dec;
result += ch;
} catch (StringIndexOutOfBoundsException ex) {
throw new XProcException("Badly formatted parameters", ex);
} catch (NumberFormatException ex) {
throw new XProcException("Badly formatted parameters", ex);
}
val = val.substring(pos+3);
}
result += val;
return result;
}
}

0 comments on commit b0507a0

Please sign in to comment.