BaseXCdiscLibraryXQuery.java
/* * Copyright 2020 Jozef Aerts. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.basex.examples.api; import java.io.*; import org.basex.core.BaseXException; import org.basex.core.Context; import org.basex.core.cmd.XQuery; public class BaseXCDISCLibraryXQuery { // For running the code, you will need the BaseX Java library, e.g. // "BaseX911.jar" private Context context; private String apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // CDISC Library XQuery private static String cdiscLibraryXQueryString = "(: apikey is passed from external :)\r\n" + "declare variable $apikey external; \r\n" + "let $response := http:send-request(\r\n" + "<http:request method='get' href='https://api.library.cdisc.org/api/mdr/products'>\r\n" + " <http:header name=\"api-key\" value=\"{$apikey}\"/> \r\n" + "</http:request>)\r\n" + "return $response"; public BaseXCDISCLibraryXQuery() { context = new Context(); } public void runXQuery() { XQuery xq = new XQuery(cdiscLibraryXQueryString); xq.bind("apikey", apiKey); try { String response = xq.execute(context); // as XQuery is for XML, the response will be an XML representation of the // returned JSON System.out.println("response = \n" + response); } catch (BaseXException e) { e.printStackTrace(); } } public String readCdiscLibraryXQueryFormFile(String fileLocation) { String query = ""; BufferedReader reader; try { reader = new BufferedReader(new FileReader(fileLocation)); } catch (FileNotFoundException e1) { e1.printStackTrace(); return null; } StringBuilder stringBuilder = new StringBuilder(); String line = null; String ls = System.getProperty("line.separator"); try { while ((line = reader.readLine()) != null) { stringBuilder.append(line); stringBuilder.append(ls); } reader.close(); } catch (IOException e) { e.printStackTrace(); return null; } // return as a single string query = stringBuilder.toString(); System.out.println("CDISC Library XQuery was read from file = " + fileLocation); return query; } public static void main(String[] args) { BaseXCDISCLibraryXQuery b = new BaseXCDISCLibraryXQuery(); // uncomment end edit the following line for reading the XQuery from file // cdiscLibraryXQueryString = // b.readCdiscLibraryXQueryFormFile("C:\\baseX\\XQueries\\CDISC_Library_XQuery.xq"); b.runXQuery(); } }