package ie.dcu.apps.ist.labelling; import ie.dcu.apps.ist.AppWindow; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.xml.bind.DatatypeConverter; import javax.xml.parsers.*; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.json.JSONArray; import org.json.JSONObject; public class Labels { /** * Function for getting the Labels through web service * * @param * The user entered text in the html element * @return * ArrayList containing each Label item * */ public ArrayList getOntologyTerms(String content) { ArrayList terms = new ArrayList(); // temporary variable for storing each list element in the loop try { String encodedContent = URLEncoder.encode(content.toString(),"UTF-8"); String webServiceURL = AppWindow.props.getProperty("POWebService.TermSearch.URL"); webServiceURL = webServiceURL.replace("", encodedContent); URL url = new URL(webServiceURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // if the response from Web service is 'OK' if (connection.getResponseCode() == 200) { String inputLine; JSONObject object = null; while ((inputLine = in.readLine()) != null) { object = new JSONObject(inputLine); } JSONArray array = new JSONArray(object.getString("PO_term_search_response")); for(int i=0; i getSpeciesTerms(String content) { ArrayList speciesTerms = new ArrayList(); String namBankId,nameBankString; try { String xmlContent = ""; String encodedContent = URLEncoder.encode(content.toString(),"UTF-8"); String webServiceURL = AppWindow.props.getProperty("UbioWebService.SpeciesTermSearch.URL"); webServiceURL = webServiceURL.replace("", encodedContent); URL url = new URL(webServiceURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); // if the response from Web service is 'OK' if (connection.getResponseCode() == 200) { String inputLine; while ((inputLine = in.readLine()) != null) { xmlContent += inputLine; } Document doc = loadXMLFromString(xmlContent); Element docEle = doc.getDocumentElement(); NodeList nlist = docEle.getElementsByTagName("value"); if(nlist != null && nlist.getLength() > 0) { for(int i = 0 ; i < nlist.getLength();i++) { //get the employee element Element el = (Element)nlist.item(i); namBankId = getTagValue("namebankID", el); nameBankString = new String(DatatypeConverter.parseBase64Binary(getTagValue("nameString", el))); SpeciesTerm species = new SpeciesTerm(nameBankString,namBankId); speciesTerms.add(species); } } in.close(); } else { System.out.println("Response error"); } } catch(Exception ex) { } return speciesTerms; } public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); } private static String getTagValue(String sTag, Element eElement) { NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes(); Node nValue = (Node) nlList.item(0); return nValue.getNodeValue(); } }