ckan2zenodo-library/ckan2zenodo-library/src/main/java/org/gcube/data/publishing/ckan2zenodo/TransformerManager.java

95 lines
3.3 KiB
Java
Raw Normal View History

2019-11-22 16:31:50 +01:00
package org.gcube.data.publishing.ckan2zenodo;
2019-12-03 12:50:36 +01:00
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.gcube.common.resources.gcore.GenericResource;
import org.gcube.data.publishing.ckan2zenodo.commons.IS;
import org.gcube.data.publishing.ckan2zenodo.model.Mapping;
2019-12-03 17:20:12 +01:00
import org.gcube.data.publishing.ckan2zenodo.model.Mapping.Regexp;
2019-12-03 12:50:36 +01:00
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import lombok.Synchronized;
2019-11-26 14:55:20 +01:00
public class TransformerManager {
2019-11-22 16:31:50 +01:00
2019-12-03 12:50:36 +01:00
private static DocumentBuilder builder=null;
@Synchronized
private static DocumentBuilder getBuilder() throws ParserConfigurationException {
if(builder==null) {
DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
}
return builder;
}
public Transformer getByProfile(String profile) throws Exception {
for(GenericResource r: IS.queryForGenericResources("Ckan-Zenodo-Mappings")){
if (r.profile().name().equals(profile))
return new Transformer(readMappings(r));
}
throw new Exception("No transformer found for profile "+profile);
}
private static ArrayList<Mapping> readMappings(GenericResource res){
2019-12-03 17:20:12 +01:00
// ByteArrayInputStream input = new ByteArrayInputStream(
// res.profile()..toString().getBytes("UTF-8"));
// Document doc = builder.parse(input);
// XPath xPath = XPathFactory.newInstance().newXPath();
// String expression = "/class/student";
// NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
// doc, XPathConstants.NODESET);
2019-12-03 12:50:36 +01:00
ArrayList<Mapping> toReturn=new ArrayList<Mapping>();
2019-12-03 17:20:12 +01:00
2019-12-03 12:50:36 +01:00
Element root=res.profile().body();
NodeList mappings=root.getElementsByTagName("mapping");
2019-12-03 17:20:12 +01:00
2019-12-03 12:50:36 +01:00
for(int i = 0; i<mappings.getLength();i++) {
Element mapping=(Element) mappings.item(i);
String source=mapping.getElementsByTagName("source").item(0).getTextContent();
String target=mapping.getElementsByTagName("target").item(0).getTextContent();
2019-12-03 17:20:12 +01:00
2019-12-03 12:50:36 +01:00
HashMap<String,String> values=new HashMap<>();
NodeList valueMappings=mapping.getElementsByTagName("valueMapping");
for(int j = 0; i<valueMappings.getLength();j++) {
2019-12-03 17:20:12 +01:00
Element codelistMapping=(Element) valueMappings.item(j);
String sourceValue=codelistMapping.getElementsByTagName("sourceValue").item(0).getTextContent();
String targetValue=codelistMapping.getElementsByTagName("targetValue").item(0).getTextContent();
2019-12-03 12:50:36 +01:00
values.put(sourceValue, targetValue);
}
2019-12-03 17:20:12 +01:00
ArrayList<Regexp> regularExpressions=new ArrayList<>();
NodeList regexpDeclarations=mapping.getElementsByTagName("regexp");
for(int j = 0; i<regexpDeclarations.getLength();j++) {
Element regexpElement=(Element) regexpDeclarations.item(j);
String regexpTarget=regexpElement.getElementsByTagName("target").item(0).getTextContent();
String typeName=regexpElement.getAttribute("type");
Regexp regexp=new Regexp(Regexp.Type.valueOf(typeName),regexpTarget);
if(regexp.getType().equals(Regexp.Type.replace))
regexp.setReplacement(regexpElement.getElementsByTagName("replacement").item(0).getTextContent());
regularExpressions.add(regexp);
}
toReturn.add(new Mapping(source,target,values,regularExpressions));
2019-12-03 12:50:36 +01:00
}
2019-12-03 17:20:12 +01:00
2019-12-03 12:50:36 +01:00
return toReturn;
2019-12-03 17:20:12 +01:00
2019-11-27 18:21:01 +01:00
}
2019-11-22 16:31:50 +01:00
}