sshoc-citationservice/src/main/java/eu/sshoc/citation/service/wfconfigurator/util/CSVHelper.java

116 lines
3.7 KiB
Java

/*******************************************************************************
* Copyright (c) 2021 VRE4EIC Consortium
*
* 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 eu.sshoc.citation.service.wfconfigurator.util;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URISyntaxException;
import java.net.URL;
public class CSVHelper {
static CSVHelper app = new CSVHelper();
public static void main(String[] args) {
try {
File repo=app.getFileFromResource("repoprofiles.csv");
try(
BufferedReader br = new BufferedReader(new FileReader(repo));
CSVParser parser = CSVFormat.DEFAULT.withDelimiter(';').withHeader().parse(br);
) {
System.out.println("header "+parser.getHeaderMap());
for(CSVRecord record : parser) {
System.out.println(record.get("Repository name") +" - "+record.get("Website") +" - "+record.get("dns")+" - "+record.get("viewer"));
}
} catch (Exception e) {
System.out.println(e);
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private File getFileFromResource(String fileName) throws URISyntaxException{
ClassLoader classLoader = getClass().getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file not found! " + fileName);
} else {
// failed if files have whitespaces or special characters
//return new File(resource.getFile());
return new File(resource.toURI());
}
}
public CSVParser getRepoProfiles() throws URISyntaxException{
File repo=app.getFileFromResource("repoprofiles.csv");
try(
BufferedReader br = new BufferedReader(new FileReader(repo));
CSVParser parser = CSVFormat.DEFAULT.withDelimiter(';').withHeader().parse(br);
) {
System.out.println("header "+parser.getHeaderMap());
return parser;
}
catch (Exception e) {
System.out.println(e);
}
return null;
}
public String getRepoApi(String repositoryurl) throws URISyntaxException{
File repo=app.getFileFromResource("repoprofiles.csv");
try(
BufferedReader br = new BufferedReader(new FileReader(repo));
CSVParser parser = CSVFormat.DEFAULT.withDelimiter(';').withHeader().parse(br);
) {
int slashpos= repositoryurl.indexOf('/', 8);
String repoid=repositoryurl.substring(0, slashpos);
System.out.println("repoid "+repoid);
//System.out.println("repositoryurl "+repositoryurl);
for(CSVRecord record : parser) {
String apiurl=record.get("api");
//apiurl=apiurl.replace("[", "");
//apiurl=apiurl.replace("]", "");
//System.out.println("dns "+record.get("dns").trim());
if(record.get("Website").trim().contains(repoid) || apiurl.contains(repoid) || record.get("dns").trim().contains(repoid)) {
System.out.println(record.get("Repository name") +" - "+record.get("api")+" - "+record.get("viewer"));
return record.get("viewer");
}
}
return "none";
}
catch (Exception e) {
System.out.println(e);
}
return null;
}
}