From 3e0635a03f3114b7340cfbf05c2fcb628c41e6e0 Mon Sep 17 00:00:00 2001 From: Baptiste Toulemonde <toulemonde@cines.fr> Date: Tue, 2 Nov 2021 11:27:40 +0100 Subject: [PATCH] init mapping --- .../java/com/smartharvester/model/Path.java | 21 +++ .../service/MappingService.java | 127 ++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 src/main/java/com/smartharvester/model/Path.java create mode 100644 src/main/java/com/smartharvester/service/MappingService.java diff --git a/src/main/java/com/smartharvester/model/Path.java b/src/main/java/com/smartharvester/model/Path.java new file mode 100644 index 0000000..cad03b4 --- /dev/null +++ b/src/main/java/com/smartharvester/model/Path.java @@ -0,0 +1,21 @@ +package com.smartharvester.model; + +public class Path { + + private String property; + private String path; + + + public String getProperty() { + return property; + } + public void setProperty(String property) { + this.property = property; + } + public String getPath() { + return path; + } + public void setPath(String path) { + this.path = path; + } +} diff --git a/src/main/java/com/smartharvester/service/MappingService.java b/src/main/java/com/smartharvester/service/MappingService.java new file mode 100644 index 0000000..a39c3db --- /dev/null +++ b/src/main/java/com/smartharvester/service/MappingService.java @@ -0,0 +1,127 @@ +package com.smartharvester.service; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Iterator; +import java.util.List; + +import javax.net.ssl.HttpsURLConnection; +import javax.xml.transform.stream.StreamSource; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.simple.parser.JSONParser; + +import com.google.gson.stream.JsonReader; +import com.smartharvester.model.Path; + +import io.swagger.util.Json; + +public class MappingService { + + public static JSONObject getJson(String urlRepo) { + URL url = null; + HttpURLConnection urlConnection = null; + String result = null; + + try { + url = new URL(urlRepo); + urlConnection = (HttpsURLConnection) url.openConnection(); + InputStream input = new BufferedInputStream(urlConnection.getInputStream()); + + result = readStream(input); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (urlConnection != null) + urlConnection.disconnect(); + } + + JSONObject json = null; + try { + json = new JSONObject(result); + } catch (JSONException e) { + e.printStackTrace(); + } + + return json; + } + + public static JSONObject getValues(String urlRepo) throws IOException { + + JSONObject json = getJson(urlRepo); + return json; + } + + public String createRDF(Path[] paths, String urlRepo) { + String properties = null; + JSONObject json = null; + try { + json = getValues(urlRepo); + for (Path path : paths) { + String[] array = path.getPath().split(" : "); + if (array.length == 0) { + if (json.optJSONArray(array[0]) != null) { + JSONArray jsonArray = json.getJSONArray(array[0]); + for (int i = 0; i < jsonArray.length(); i++) { + properties += path.getProperty() + " \"" + jsonArray.get(i) + "\" ;\n"; + } + + } else { + properties += path.getProperty() + " \"" + json.getString(array[0]) + "\" ;\n"; + } + } else { + JSONObject jsonT = json; + for (int i = 0; i < array.length -1 ; i++) { + if (jsonT.get(array[i]) instanceof JSONObject) { + jsonT = jsonT.getJSONObject(array[i]); + } else if (jsonT.get(array[i]) instanceof JSONArray) { + jsonT = jsonT.getJSONArray(array[i]).toJSONObject(null); + } else if (jsonT.get(array[i]) instanceof String) { + properties += path.getProperty() + " \"" + json.getString(array[0]) + "\" ;\n"; + } + } + if (jsonT.optJSONArray(array[array.length - 1]) != null) { + JSONArray jsonArray = json.getJSONArray(array[array.length - 1]); + for (int i = 0; i < jsonArray.length(); i++) { + properties += path.getProperty() + " \"" + jsonArray.get(i) + "\" ;\n"; + } + + } else if (jsonT.get(array[array.length - 1]) instanceof String ){ + properties += path.getProperty() + " \"" + jsonT.getString(array[array.length - 1]) + "\" ;\n"; + } + } + } + } catch (IOException | JSONException e) { + e.printStackTrace(); + } + + return properties; + } + + private static String readStream(InputStream input) throws IOException { + StringBuilder sb = new StringBuilder(); + BufferedReader reader = new BufferedReader(new InputStreamReader(input), 1000); + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + sb.append(line); + } + input.close(); + return sb.toString(); + } + + public static void main(String[] args) throws IOException { + System.out.println(getValues( + "https://sextant.ifremer.fr/geonetwork/srv/fre/qi?buildSummary=false&_content_type=json&fast=index&_uuid=7691f8b8-1193-4aef-8e7e-0d7a9c88c057")); + } + +} -- GitLab