package dalclient; import edu.jhu.pha.ivoa.*; import java.io.*; import java.net.*; import java.util.*; /** * DAL Query. Provides methods to build up and execute a query against a * predefined service connection context. */ public class DALQuery { LinkedHashMap params = new LinkedHashMap(); DALConnection connection; String queryURL; DALQuery(DALConnection conn) { connection = conn; } /* Add a query parameter=value pair. */ public void addParameter(String name, String value) { params.put(name, value); } /* Execute query and write to stdout in CSV format. */ public void executeCSV() throws Exception { executeCSV(System.out); } /* Execute query and write to the named file in CSV format. */ public void executeCSV(String fname) throws Exception { OutputStream ostream = new FileOutputStream(fname); executeCSV(new PrintStream(ostream)); } /* Execute query and write to the given output stream in CSV format. * At this level we do not support queries to multiple services as * the tables returned may differ. */ public void executeCSV(PrintStream out) throws Exception { VOTWrap.VOTable vot = this.executeVOTable(); VOTWrap.Resource r = vot.getResource(0); VOTWrap.Table table = r.getTable(0); // Output the table header in CSV. for (int i=0; i < table.getFieldCount(); i++) { String colname; VOTWrap.Field field = table.getField(i); if ((colname = field.getUCD()) == null) colname = ""; if (i > 0) System.out.print(","); out.print(colname); } out.println(""); // Output the table data. int TRCount = table.getTableData().getTRCount(); for (int j=0; j < TRCount; j++) { VOTWrap.TR row = table.getTableData().getTR(j); for (int i=0; i < table.getFieldCount(); i++) { String s = row.getTD(i).getPCDATA(); if (i > 0) out.print(","); out.print(s.replace(',', ' ')); } out.println(""); } } /* Execute query and return a VOTable. */ public VOTWrap.VOTable executeVOTable() throws Exception { return (executeVOTable(0)); } /* Query the specified service. */ public VOTWrap.VOTable executeVOTable(int serviceIndex) throws Exception { return (VOTWrap.createVOTable(executeRaw(serviceIndex))); } /* Execute query and return a binary input stream to read raw results. * Note: multiple service connections are not supported at this level. * By default only the first service specified is queried. */ public InputStream executeRaw() throws Exception { return (executeRaw(0)); } /* Query the specified service. */ public InputStream executeRaw(int serviceIndex) throws Exception { String url = getQueryString(serviceIndex); URL http = new URL(queryURL = url); return (http.openStream()); } /* Get the current query URL as a string for the given service. */ public String getQueryString(int serviceIndex) { Set s = params.entrySet(); String url; // Get service baseURL. ArrayList services = connection.services; if (services.size() < serviceIndex) return (null); String baseURL = (String) services.get(serviceIndex); url = baseURL; // Try to ensure that the service URL is properly terminated. char lastch = url.charAt(url.length() - 1); if (lastch != '?' && lastch != '&') url += (url.indexOf('?') > 0) ? '&' : '?'; // Add query parameters. int arg = 0; for (Iterator i = s.iterator(); i.hasNext(); arg++) { Map.Entry me = (Map.Entry) i.next(); String name = (String) me.getKey(); String value = (String) me.getValue(); if (arg > 0) url += "&"; url += name + "=" + value; } return (url); } }