package org.xins.common.ant;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Property;
import org.xins.common.text.URLEncoding;
import org.xins.common.xml.Element;
public class CallXINSTask extends Task {
private String _function;
private String _apiLocation;
private String _prefix;
private Vector _params = new Vector();
public CallXINSTask() {
}
public void setFunction(String function) {
_function = function;
}
public void setApiLocation(String apiLocation) {
_apiLocation = apiLocation;
}
public void setPrefix(String prefix) {
_prefix = prefix;
}
public Property createParam() {
Property property = new Property();
_params.add(property);
return property;
}
public void execute() throws BuildException {
checkAttributes();
try {
StringBuffer sbRequestURL = new StringBuffer(_apiLocation + "?_convention=_xins-std&_function=" + _function);
for (int i = 0; i < _params.size(); i++) {
Property nextParam = (Property) _params.elementAt(i);
String paramName = nextParam.getName();
String paramValue = nextParam.getValue();
sbRequestURL.append("&" + URLEncoding.encode(paramName) + "=" + URLEncoding.encode(paramValue));
}
String requestURL = sbRequestURL.toString();
Element resultXML = CreateExampleTask.getResultAsXML(requestURL);
log("result: " + resultXML.toString(), Project.MSG_VERBOSE);
List outputParams = resultXML.getChildElements("param");
Iterator itOutputParams = outputParams.iterator();
while (itOutputParams.hasNext()) {
Element nextParam = (Element) itOutputParams.next();
String paramName = nextParam.getAttribute("name");
String paramValue = nextParam.getText();
if (_prefix != null && _prefix.length() > 0) {
paramName = _prefix + "." + paramName;
}
getProject().setNewProperty(paramName, paramValue);
}
if (resultXML.getChildElements("data").size() > 0) {
Element dataSection = resultXML.getUniqueChildElement("data");
elementToProperties(dataSection, _prefix);
}
} catch (Exception ex) {
throw new BuildException(ex);
}
}
private void elementToProperties(Element element, String prefix) {
String localName = element.getLocalName();
String elementPrefix;
if (prefix != null && prefix.length() > 0) {
elementPrefix = prefix + "." + localName;
} else {
elementPrefix = localName;
}
Map attributes = element.getAttributeMap();
Iterator itAttributes = attributes.entrySet().iterator();
while (itAttributes.hasNext()) {
Map.Entry nextAttr = (Map.Entry) itAttributes.next();
String attributeName = ((Element.QualifiedName) nextAttr.getKey()).getLocalName();
String attributeValue = (String) nextAttr.getValue();
String propertyName = elementPrefix + "." + attributeName;
getProject().setNewProperty(propertyName, attributeValue);
}
String pcdata = element.getText();
if (pcdata != null && pcdata.length() > 0) {
getProject().setNewProperty(elementPrefix, pcdata);
}
Iterator children = element.getChildElements().iterator();
while (children.hasNext()) {
Element nextChild = (Element) children.next();
elementToProperties(nextChild, elementPrefix);
}
}
private void checkAttributes() throws BuildException {
if (_function == null) {
throw new BuildException("The \"function\" attribute needs to be specified.");
}
if (_apiLocation == null) {
throw new BuildException("An \"exampleProperty\" attribute needs to be specified.");
}
}
}