package org.xins.common.ant;
import java.util.List;
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.w3c.dom.Attr;
import org.xins.common.text.URLEncoding;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.xins.common.xml.ElementList;
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);
ElementList outputParams = new ElementList(resultXML, "param");
for (int i = 0; i < outputParams.size(); i++) {
Element nextParam = (Element) outputParams.get(i);
String paramName = nextParam.getAttribute("name");
String paramValue = nextParam.getTextContent();
if (_prefix != null && _prefix.length() > 0) {
paramName = _prefix + "." + paramName;
}
getProject().setNewProperty(paramName, paramValue);
}
ElementList outputData = new ElementList(resultXML, "data");
if (!outputData.isEmpty()) {
Element dataSection = outputData.get(0);
elementToProperties(dataSection, _prefix);
}
} catch (Exception ex) {
throw new BuildException(ex);
}
}
private void elementToProperties(Element element, String prefix) {
String localName = element.getTagName();
String elementPrefix;
if (prefix != null && prefix.length() > 0) {
elementPrefix = prefix + "." + localName;
} else {
elementPrefix = localName;
}
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Attr attribute = (Attr) attributes.item(i);
String attributeName = attribute.getName();
String attributeValue = attribute.getValue();
String propertyName = elementPrefix + "." + attributeName;
getProject().setNewProperty(propertyName, attributeValue);
}
String pcdata = element.getTextContent();
if (pcdata != null && pcdata.length() > 0) {
getProject().setNewProperty(elementPrefix, pcdata);
}
for (Element nextChild : new ElementList(element)) {
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.");
}
}
}