Revision 61907
Added by Michele Artini over 2 years ago
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/PersonVocabulary.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
5 |
|
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import prototype.Person; |
|
9 |
|
|
10 |
/** |
|
11 |
* @author jochen |
|
12 |
* |
|
13 |
*/ |
|
14 |
public class PersonVocabulary extends Vocabulary{ |
|
15 |
|
|
16 |
@Override |
|
17 |
public String encoding(List<String> keys)throws ProcessingException{ |
|
18 |
Person p; |
|
19 |
String result = ""; |
|
20 |
for (String input: keys){ |
|
21 |
p = new Person(input); |
|
22 |
result = p.getNormalisedFullname(); |
|
23 |
} |
|
24 |
return result; |
|
25 |
} |
|
26 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/Extract.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
5 |
|
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import eu.dnetlib.data.collective.transformation.TransformationException; |
|
9 |
|
|
10 |
/** |
|
11 |
* @author jochen |
|
12 |
* |
|
13 |
*/ |
|
14 |
public class Extract extends AbstractTransformationFunction { |
|
15 |
|
|
16 |
public static final String paramNameFeature = "feature"; |
|
17 |
private IFeatureExtraction featureExtraction; |
|
18 |
|
|
19 |
/* (non-Javadoc) |
|
20 |
* @see eu.dnetlib.data.collective.transformation.engine.functions.AbstractTransformationFunction#execute() |
|
21 |
*/ |
|
22 |
@Override |
|
23 |
String execute() throws ProcessingException { |
|
24 |
// TODO Auto-generated method stub |
|
25 |
return null; |
|
26 |
} |
|
27 |
|
|
28 |
public List<String> execute(List<String> aObjectRecords, String aFeature) throws ProcessingException{ |
|
29 |
try { |
|
30 |
return featureExtraction.execute(aObjectRecords, aFeature); |
|
31 |
} catch (TransformationException e) { |
|
32 |
throw new ProcessingException(e); |
|
33 |
} |
|
34 |
} |
|
35 |
|
|
36 |
/** |
|
37 |
* @param featureExtraction the featureExtraction to set |
|
38 |
*/ |
|
39 |
public void setFeatureExtraction(IFeatureExtraction featureExtraction) { |
|
40 |
this.featureExtraction = featureExtraction; |
|
41 |
} |
|
42 |
|
|
43 |
/** |
|
44 |
* @return the featureExtraction |
|
45 |
*/ |
|
46 |
public IFeatureExtraction getFeatureExtraction() { |
|
47 |
return featureExtraction; |
|
48 |
} |
|
49 |
|
|
50 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/RegularExpression.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
5 |
|
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
import org.apache.oro.text.perl.MalformedPerl5PatternException; |
|
9 |
import org.apache.oro.text.perl.Perl5Util; |
|
10 |
|
|
11 |
/** |
|
12 |
* @author jochen |
|
13 |
* |
|
14 |
*/ |
|
15 |
public class RegularExpression extends AbstractTransformationFunction { |
|
16 |
|
|
17 |
public static final Log log = LogFactory.getLog(RegularExpression.class); |
|
18 |
public static final String paramRegularExpr = "regularExpression"; |
|
19 |
public static final String paramExpr1 = "expr1"; |
|
20 |
public static final String paramExpr2 = "expr2"; |
|
21 |
|
|
22 |
private Perl5Util util = new Perl5Util(); |
|
23 |
|
|
24 |
/* (non-Javadoc) |
|
25 |
* @see eu.dnetlib.data.collective.transformation.engine.functions.AbstractTransformationFunction#execute() |
|
26 |
*/ |
|
27 |
@Override |
|
28 |
String execute() throws ProcessingException { |
|
29 |
// TODO Auto-generated method stub |
|
30 |
return null; |
|
31 |
} |
|
32 |
|
|
33 |
public String executeSingleValue(String aRegularExpression, String aExpr1, String aExpr2) throws ProcessingException{ |
|
34 |
String result = ""; |
|
35 |
if (aRegularExpression.startsWith("m/")){ |
|
36 |
if (util.match(aRegularExpression, aExpr1)) |
|
37 |
result = util.group(1); |
|
38 |
}else if (!aRegularExpression.startsWith("s/")){ |
|
39 |
// assume match and extract |
|
40 |
// throw new ProcessingException("unsupported or invalid regular expression: " + aRegularExpression); |
|
41 |
if (util.match(aRegularExpression, aExpr1)){ |
|
42 |
String funder = util.group(1).toLowerCase(); |
|
43 |
String projectId = util.group(3); |
|
44 |
result = funder + "_" + projectId; |
|
45 |
} |
|
46 |
}else{ |
|
47 |
try{ |
|
48 |
result = util.substitute(aRegularExpression, aExpr1); |
|
49 |
}catch(MalformedPerl5PatternException patternExc){ |
|
50 |
log.fatal("aRegularExpression: " + aRegularExpression); |
|
51 |
log.fatal("aExpr1: " + aExpr1); |
|
52 |
log.fatal(patternExc.getMessage()); |
|
53 |
throw new ProcessingException(patternExc); |
|
54 |
} |
|
55 |
} |
|
56 |
return result; |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/AbstractTransformationFunction.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
public abstract class AbstractTransformationFunction implements |
|
6 |
ITransformationFunction { |
|
7 |
|
|
8 |
List<String> objectRecords; |
|
9 |
List<String> resultRecords; |
|
10 |
|
|
11 |
abstract String execute() throws ProcessingException; |
|
12 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/Lookup.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
5 |
|
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
|
|
9 |
/** |
|
10 |
* @author jochen |
|
11 |
* |
|
12 |
*/ |
|
13 |
public class Lookup extends AbstractTransformationFunction { |
|
14 |
public static final Log log = LogFactory.getLog(Lookup.class); |
|
15 |
public static final String paramExprIdentifier = "exprIdentifier"; |
|
16 |
public static final String paramExprProperty = "exprProperty"; |
|
17 |
|
|
18 |
/** |
|
19 |
* |
|
20 |
*/ |
|
21 |
public Lookup() { |
|
22 |
// TODO Auto-generated constructor stub |
|
23 |
} |
|
24 |
|
|
25 |
/* (non-Javadoc) |
|
26 |
* @see eu.dnetlib.data.collective.transformation.engine.functions.AbstractTransformationFunction#execute() |
|
27 |
*/ |
|
28 |
@Override |
|
29 |
String execute() throws ProcessingException { |
|
30 |
// TODO Auto-generated method stub |
|
31 |
return null; |
|
32 |
} |
|
33 |
|
|
34 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/IVocabulary.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
5 |
|
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
/** |
|
9 |
* @author jochen |
|
10 |
* |
|
11 |
*/ |
|
12 |
public interface IVocabulary { |
|
13 |
|
|
14 |
/** |
|
15 |
* return the encoding for a given list of values |
|
16 |
* @param keys |
|
17 |
* @return the encoding as string |
|
18 |
* @throws ProcessingException |
|
19 |
*/ |
|
20 |
public String encoding(List<String> keys) throws ProcessingException; |
|
21 |
|
|
22 |
/** |
|
23 |
* return the encoding for a given list of values using a default pattern and applying a filter function |
|
24 |
* @param aKeys |
|
25 |
* @param aDefaultPattern |
|
26 |
* @param aFilterFunction |
|
27 |
* @return the list of encoded values |
|
28 |
* @throws ProcessingException |
|
29 |
*/ |
|
30 |
public List<String> encoding(List<String> aKeys, String aDefaultPattern, String aFilterFunction) throws ProcessingException; |
|
31 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/ProcessingException.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.engine.functions; |
|
5 |
|
|
6 |
/** |
|
7 |
* @author jochen |
|
8 |
* |
|
9 |
*/ |
|
10 |
public class ProcessingException extends Exception { |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
*/ |
|
15 |
private static final long serialVersionUID = -8648116731979859467L; |
|
16 |
|
|
17 |
/** |
|
18 |
* |
|
19 |
*/ |
|
20 |
public ProcessingException() { |
|
21 |
super(); |
|
22 |
} |
|
23 |
|
|
24 |
/** |
|
25 |
* @param arg0 |
|
26 |
*/ |
|
27 |
public ProcessingException(String arg0) { |
|
28 |
super(arg0); |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* @param arg0 |
|
33 |
*/ |
|
34 |
public ProcessingException(Throwable arg0) { |
|
35 |
super(arg0); |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* @param arg0 |
|
40 |
* @param arg1 |
|
41 |
*/ |
|
42 |
public ProcessingException(String arg0, Throwable arg1) { |
|
43 |
super(arg0, arg1); |
|
44 |
} |
|
45 |
|
|
46 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/SimpleTransformationEngine.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.engine; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.util.HashMap; |
|
5 |
import java.util.Iterator; |
|
6 |
import java.util.LinkedHashMap; |
|
7 |
import java.util.LinkedList; |
|
8 |
import java.util.List; |
|
9 |
import java.util.Map; |
|
10 |
|
|
11 |
import javax.xml.xpath.XPath; |
|
12 |
import javax.xml.xpath.XPathConstants; |
|
13 |
import javax.xml.xpath.XPathExpressionException; |
|
14 |
import javax.xml.xpath.XPathFactory; |
|
15 |
|
|
16 |
import net.sf.saxon.instruct.TerminationException; |
|
17 |
|
|
18 |
import org.apache.commons.logging.Log; |
|
19 |
import org.apache.commons.logging.LogFactory; |
|
20 |
import org.springframework.core.io.Resource; |
|
21 |
import org.w3c.dom.Node; |
|
22 |
import org.xml.sax.InputSource; |
|
23 |
|
|
24 |
import eu.dnetlib.common.profile.ResourceDao; |
|
25 |
import eu.dnetlib.data.collective.transformation.IDatabaseConnector; |
|
26 |
import eu.dnetlib.data.collective.transformation.TransformationException; |
|
27 |
import eu.dnetlib.data.collective.transformation.VocabularyRegistry; |
|
28 |
import eu.dnetlib.data.collective.transformation.core.xsl.ext.TransformationFunctionProxy; |
|
29 |
import eu.dnetlib.data.collective.transformation.engine.core.ITransformation; |
|
30 |
import eu.dnetlib.data.collective.transformation.engine.functions.Convert; |
|
31 |
// import eu.dnetlib.data.collective.transformation.engine.functions.Dblookup; |
|
32 |
import eu.dnetlib.data.collective.transformation.engine.functions.Extract; |
|
33 |
import eu.dnetlib.data.collective.transformation.engine.functions.IFeatureExtraction; |
|
34 |
import eu.dnetlib.data.collective.transformation.engine.functions.ProcessingException; |
|
35 |
import eu.dnetlib.data.collective.transformation.engine.functions.RegularExpression; |
|
36 |
import eu.dnetlib.data.collective.transformation.engine.functions.RetrieveValue; |
|
37 |
import eu.dnetlib.data.collective.transformation.engine.functions.RetrieveValue.FUNCTION; |
|
38 |
import eu.dnetlib.data.collective.transformation.rulelanguage.Argument; |
|
39 |
import eu.dnetlib.data.collective.transformation.rulelanguage.Argument.Type; |
|
40 |
import eu.dnetlib.data.collective.transformation.rulelanguage.util.FunctionCall; |
|
41 |
import eu.dnetlib.data.collective.transformation.utils.BlacklistConsumer; |
|
42 |
|
|
43 |
/** |
|
44 |
* @author jochen |
|
45 |
* |
|
46 |
*/ |
|
47 |
public class SimpleTransformationEngine { |
|
48 |
|
|
49 |
private static Log log = LogFactory.getLog(SimpleTransformationEngine.class); |
|
50 |
private ITransformation transformation; |
|
51 |
private VocabularyRegistry vocabularyRegistry; |
|
52 |
private IDatabaseConnector databaseConnector; |
|
53 |
private ResourceDao resourceDao; |
|
54 |
private IFeatureExtraction featureExtraction; |
|
55 |
private final List<String> mdRecords = new LinkedList<String>(); |
|
56 |
private long totalTransformedRecords = 0; |
|
57 |
private long totalIgnoredRecords = 0; |
|
58 |
private String mappingFile; |
|
59 |
private boolean stylesheetParamsCalculated = false; |
|
60 |
private boolean preprocessingDone = false; |
|
61 |
private Map<String, String> stylesheetParams = new LinkedHashMap<String, String>(); |
|
62 |
private Resource blacklistApi; |
|
63 |
private List<String> blacklistedRecords = new LinkedList<String>(); |
|
64 |
|
|
65 |
|
|
66 |
/** |
|
67 |
* execute any preprocessings declared in the transformation script prior starting the transformation of records |
|
68 |
*/ |
|
69 |
public void preprocess(String dataSourceId) { |
|
70 |
for (Map<String, String> preprocMap : this.transformation.getRuleLanguageParser().getPreprocessings()) { |
|
71 |
Iterator<String> it = preprocMap.keySet().iterator(); |
|
72 |
while (it.hasNext()) { |
|
73 |
String function = it.next(); |
|
74 |
// if (function.equals("dblookup")) { |
|
75 |
// Dblookup fun = new Dblookup(); |
|
76 |
// fun.setDbConnector(databaseConnector); |
|
77 |
// try { |
|
78 |
// log.debug("preprocessingMap value: " + preprocMap.get(function)); |
|
79 |
// TransformationFunctionProxy.getInstance().setLookupRecord(fun.getResults(preprocMap.get(function))); |
|
80 |
// } catch (Exception e) { |
|
81 |
// log.debug(e.getMessage()); |
|
82 |
// throw new IllegalStateException(e); |
|
83 |
// } |
|
84 |
// } |
|
85 |
if (function.equals("blacklist")) { |
|
86 |
BlacklistConsumer bc = new BlacklistConsumer(); |
|
87 |
try{ |
|
88 |
blacklistedRecords = bc.getBlackList(blacklistApi.getURL() + dataSourceId); |
|
89 |
}catch(Exception e){ |
|
90 |
throw new IllegalStateException("error in preprocess: " + e.getMessage()); |
|
91 |
} |
|
92 |
} |
|
93 |
} |
|
94 |
} |
|
95 |
log.debug("preprocessing done."); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* check if blacklistedRecords exist and if so check if the current record is blacklisted by its objIdentifier |
|
100 |
* @param aRecord |
|
101 |
* @return |
|
102 |
* @throws XPathExpressionException |
|
103 |
* @throws ProcessingException |
|
104 |
*/ |
|
105 |
private boolean isBlacklistRecord(String aRecord){ |
|
106 |
if (blacklistedRecords.size() == 0) return false; |
|
107 |
XPath xpath = XPathFactory.newInstance().newXPath(); |
|
108 |
try{ |
|
109 |
Node root = (Node) xpath.evaluate("/", new InputSource(new StringReader(aRecord)), XPathConstants.NODE); |
|
110 |
String objId = xpath.evaluate("//*[local-name()='objIdentifier']", root); |
|
111 |
if (blacklistedRecords.contains(objId)) return true; |
|
112 |
}catch(Exception e){ |
|
113 |
throw new IllegalStateException("error in isBlacklistRecord: " + e.getMessage()); |
|
114 |
} |
|
115 |
return false; |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
119 |
* transforms a source record |
|
120 |
* |
|
121 |
* @param sourceRecord |
|
122 |
* the record to transform |
|
123 |
* @return transformed record |
|
124 |
*/ |
|
125 |
public String transform(final String sourceRecord) { |
|
126 |
List<String> objectRecords = new LinkedList<String>(); |
|
127 |
objectRecords.add(sourceRecord); |
|
128 |
int index = 0; |
|
129 |
mdRecords.clear(); |
|
130 |
initTransformationFunction(); |
|
131 |
|
|
132 |
if (!stylesheetParamsCalculated) { |
|
133 |
try{ |
|
134 |
calculateStylesheetParams(sourceRecord); |
|
135 |
}catch(Exception e){ |
|
136 |
throw new IllegalStateException("error in calculateStylesheetParams" + e.getMessage()); |
|
137 |
} |
|
138 |
} |
|
139 |
|
|
140 |
if (!preprocessingDone){ |
|
141 |
// xpath sourceRecord dataSourceid |
|
142 |
preprocess(stylesheetParams.get("varBlacklistDataSourceId")); |
|
143 |
preprocessingDone = true; |
|
144 |
} |
|
145 |
|
|
146 |
if (isBlacklistRecord(sourceRecord)){ |
|
147 |
try{ |
|
148 |
mdRecords.add(transformation.transformRecord(sourceRecord, ITransformation.XSLSyntaxcheckfailed)); |
|
149 |
}catch(Exception e){ |
|
150 |
log.fatal(sourceRecord); |
|
151 |
throw new IllegalStateException(e); |
|
152 |
} |
|
153 |
}else if (!transformation.getRuleLanguageParser().isXslStylesheet()) { |
|
154 |
// iterate over all rules which are functionCalls |
|
155 |
log.debug("functionCalls size: " + transformation.getRuleLanguageParser().getFunctionCalls().size()); |
|
156 |
for (FunctionCall functionCall : transformation.getRuleLanguageParser().getFunctionCalls()) { |
|
157 |
preprocess(objectRecords, functionCall); |
|
158 |
} |
|
159 |
for (String record : objectRecords) { |
|
160 |
// log.debug(record); |
|
161 |
try { |
|
162 |
log.debug("now run transformation for record with index: " + index); |
|
163 |
try{ |
|
164 |
String transformedRecord = transformation.transformRecord(record, index); |
|
165 |
mdRecords.add(transformedRecord); |
|
166 |
} catch (TerminationException e){ |
|
167 |
log.debug("record transformation terminated."); |
|
168 |
String failedRecord = transformation.transformRecord(record, ITransformation.XSLSyntaxcheckfailed); |
|
169 |
log.debug(failedRecord); |
|
170 |
totalIgnoredRecords++; |
|
171 |
mdRecords.add(failedRecord); |
|
172 |
} |
|
173 |
} catch (TransformationException e) { |
|
174 |
log.error(sourceRecord); |
|
175 |
throw new IllegalStateException(e); |
|
176 |
} |
|
177 |
index++; |
|
178 |
} |
|
179 |
} else { |
|
180 |
for (String record : objectRecords) { |
|
181 |
// test for init params and assign values |
|
182 |
try { |
|
183 |
log.debug("now run transformation for record with index: " + index); |
|
184 |
try{ |
|
185 |
String transformedRecord = transformation.transformRecord(record, stylesheetParams); |
|
186 |
mdRecords.add(transformedRecord); |
|
187 |
}catch(TerminationException e){ |
|
188 |
String failedRecord = transformation.transformRecord(record, ITransformation.XSLSyntaxcheckfailed); |
|
189 |
totalIgnoredRecords++; |
|
190 |
log.debug(failedRecord); |
|
191 |
mdRecords.add(failedRecord); |
|
192 |
} |
|
193 |
} catch (TransformationException e) { |
|
194 |
log.error(sourceRecord); |
|
195 |
throw new IllegalStateException(e); |
|
196 |
} |
|
197 |
index++; |
|
198 |
} |
|
199 |
} |
|
200 |
|
|
201 |
totalTransformedRecords = totalTransformedRecords + mdRecords.size(); |
|
202 |
log.debug("objRecordSize: " + objectRecords.size() + ", mdRecordSize: " + mdRecords.size() + ", ignoredRecordSize: " + totalIgnoredRecords); |
|
203 |
return mdRecords.get(0); |
|
204 |
} |
|
205 |
|
|
206 |
private void calculateStylesheetParams(final String aRecord) throws XPathExpressionException, ProcessingException { |
|
207 |
stylesheetParamsCalculated = true; |
|
208 |
XPath xpath = XPathFactory.newInstance().newXPath(); |
|
209 |
Node root = (Node) xpath.evaluate("/", new InputSource(new StringReader(aRecord)), XPathConstants.NODE); |
|
210 |
String datasourcePrefix = xpath.evaluate("//*[local-name()='datasourceprefix']", root); |
|
211 |
String profileXquery = "collection('/db/DRIVER/RepositoryServiceResources')//RESOURCE_PROFILE[.//EXTRA_FIELDS/FIELD[key=\"NamespacePrefix\"][value=\"" + datasourcePrefix + "\"]]"; |
|
212 |
//String repositoryId = xpath.evaluate("//*[local-name()='repositoryId']", root); |
|
213 |
log.debug("profileXquery: " + profileXquery); |
|
214 |
// static $varDatasourceid = getValue(PROFILEFIELD, [xpath:"//dri:repositoryId", |
|
215 |
// xpath:"//EXTRA_FIELDS/FIELD[key='OpenAireDataSourceId']/value"]); |
|
216 |
RetrieveValue retrieveValue = new RetrieveValue(); |
|
217 |
retrieveValue.setResourceDao(resourceDao); |
|
218 |
List<Argument> argList = new LinkedList<Argument>(); |
|
219 |
argList.add(new Argument(Type.VALUE, profileXquery)); |
|
220 |
Argument argXpath = new Argument(Type.INPUTFIELD, "//OFFICIAL_NAME"); |
|
221 |
argList.add(argXpath); |
|
222 |
String varOfficialName = retrieveValue.executeSingleValue(FUNCTION.PROFILEFIELD.toString(), argList, null, new HashMap<String, String>()); |
|
223 |
stylesheetParams.put("varOfficialName", varOfficialName); |
|
224 |
argList.remove(argXpath); |
|
225 |
argXpath = new Argument(Type.INPUTFIELD, "//EXTRA_FIELDS/FIELD[key='OpenAireDataSourceId']/value"); |
|
226 |
argList.add(argXpath); |
|
227 |
String varDataSourceId = retrieveValue.executeSingleValue(FUNCTION.PROFILEFIELD.toString(), argList, null, new HashMap<String, String>()); |
|
228 |
stylesheetParams.put("varDataSourceId", varDataSourceId); |
|
229 |
argList.remove(argXpath); |
|
230 |
argXpath = new Argument(Type.INPUTFIELD, "//CONFIGURATION/DATASOURCE_TYPE"); |
|
231 |
argList.add(argXpath); |
|
232 |
String varDsType = retrieveValue.executeSingleValue(FUNCTION.PROFILEFIELD.toString(), argList, null, new HashMap<String, String>()); |
|
233 |
stylesheetParams.put("varDsType", varDsType); |
|
234 |
argList.remove(argXpath); |
|
235 |
|
|
236 |
// if blacklist |
|
237 |
for (Map<String, String> preprocMap : this.transformation.getRuleLanguageParser().getPreprocessings()) { |
|
238 |
Iterator<String> it = preprocMap.keySet().iterator(); |
|
239 |
while (it.hasNext()) { |
|
240 |
String function = it.next(); |
|
241 |
if (function.equals("blacklist")) { |
|
242 |
argXpath = new Argument(Type.INPUTFIELD, preprocMap.get(function)); // blacklistDataSourceIdXpath |
|
243 |
argList.add(argXpath); |
|
244 |
String varBlacklistDataSourceId = retrieveValue.executeSingleValue(FUNCTION.PROFILEFIELD.toString(), argList, null, new HashMap<String, String>()); |
|
245 |
stylesheetParams.put("varBlacklistDataSourceId", varBlacklistDataSourceId); |
|
246 |
argList.remove(argXpath); |
|
247 |
} |
|
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
|
|
252 |
private void initTransformationFunction() { |
|
253 |
if (this.vocabularyRegistry == null) { throw new IllegalStateException("vocabularyReg is null"); } |
|
254 |
Convert convertFunction = new Convert(); |
|
255 |
convertFunction.setVocabularyRegistry(this.vocabularyRegistry); |
|
256 |
TransformationFunctionProxy.getInstance().setConvertFunction(convertFunction); |
|
257 |
|
|
258 |
} |
|
259 |
|
|
260 |
/** |
|
261 |
* preprocesses function if function is configured resp. |
|
262 |
* |
|
263 |
* @param records |
|
264 |
* list of object records |
|
265 |
* @param aFunctionCall |
|
266 |
*/ |
|
267 |
private void preprocess(final List<String> records, final FunctionCall aFunctionCall) { |
|
268 |
try { |
|
269 |
log.debug("preprocess"); |
|
270 |
if (transformation.getRuleLanguageParser() == null) { throw new IllegalStateException("rulelanguageparser not initialised"); } |
|
271 |
if (transformation.getRuleLanguageParser().getNamespaceDeclarations() == null) { throw new IllegalStateException("nsDecl is null"); } |
|
272 |
PreProcessor preProc = new PreProcessor(); |
|
273 |
preProc.setConvertFunction(TransformationFunctionProxy.getInstance().getConvertFunction()); |
|
274 |
RetrieveValue retrieveValue = new RetrieveValue(); |
|
275 |
retrieveValue.setResourceDao(resourceDao); |
|
276 |
preProc.setRetrieveFunction(retrieveValue); |
|
277 |
RegularExpression regExpr = new RegularExpression(); |
|
278 |
preProc.setRegExprFunction(regExpr); |
|
279 |
TransformationFunctionProxy functionProxy = TransformationFunctionProxy.getInstance(); |
|
280 |
preProc.setFunctionProxy(functionProxy); |
|
281 |
Extract extractFunction = new Extract(); |
|
282 |
extractFunction.setFeatureExtraction(featureExtraction); |
|
283 |
preProc.setExtractFunction(extractFunction); |
|
284 |
if (aFunctionCall.doPreprocess() || aFunctionCall.isStatic()) { |
|
285 |
// log.debug("now call preprocess with: " + aFunctionCall.getExternalFunctionName() + " " + aFunctionCall.getUuid()); |
|
286 |
preProc.preprocess( |
|
287 |
aFunctionCall, |
|
288 |
records, |
|
289 |
transformation.getRuleLanguageParser().getNamespaceDeclarations(), |
|
290 |
transformation.getStaticTransformationResults(), |
|
291 |
transformation.getJobProperties(), |
|
292 |
transformation.getRuleLanguageParser().getVariableMappingRules()); |
|
293 |
// log.debug("preprocess end"); |
|
294 |
} else { |
|
295 |
log.debug("skip preprocessing for function: " + aFunctionCall.getExternalFunctionName()); |
|
296 |
} |
|
297 |
|
|
298 |
} catch (Exception e) { |
|
299 |
throw new IllegalStateException(e); |
|
300 |
} |
|
301 |
|
|
302 |
} |
|
303 |
|
|
304 |
/** |
|
305 |
* @param transformation |
|
306 |
* the transformation to set |
|
307 |
*/ |
|
308 |
public void setTransformation(final ITransformation transformation) { |
|
309 |
this.transformation = transformation; |
|
310 |
} |
|
311 |
|
|
312 |
/** |
|
313 |
* @return the transformation |
|
314 |
*/ |
|
315 |
public ITransformation getTransformation() { |
|
316 |
return transformation; |
|
317 |
} |
|
318 |
|
|
319 |
/** |
|
320 |
* @param vocabularyRegistry |
|
321 |
* the vocabularyRegistry to set |
|
322 |
*/ |
|
323 |
public void setVocabularyRegistry(final VocabularyRegistry vocabularyRegistry) { |
|
324 |
this.vocabularyRegistry = vocabularyRegistry; |
|
325 |
} |
|
326 |
|
|
327 |
/** |
|
328 |
* @return the vocabularyRegistry |
|
329 |
*/ |
|
330 |
public VocabularyRegistry getVocabularyRegistry() { |
|
331 |
return vocabularyRegistry; |
|
332 |
} |
|
333 |
|
|
334 |
/** |
|
335 |
* @return the resourceDao |
|
336 |
*/ |
|
337 |
public ResourceDao getResourceDao() { |
|
338 |
return resourceDao; |
|
339 |
} |
|
340 |
|
|
341 |
/** |
|
342 |
* @param resourceDao |
|
343 |
* the resourceDao to set |
|
344 |
*/ |
|
345 |
public void setResourceDao(final ResourceDao resourceDao) { |
|
346 |
this.resourceDao = resourceDao; |
|
347 |
} |
|
348 |
|
|
349 |
/** |
|
350 |
* @param featureExtraction |
|
351 |
* the featureExtraction to set |
|
352 |
*/ |
|
353 |
public void setFeatureExtraction(final IFeatureExtraction featureExtraction) { |
|
354 |
this.featureExtraction = featureExtraction; |
|
355 |
} |
|
356 |
|
|
357 |
/** |
|
358 |
* @return the featureExtraction |
|
359 |
*/ |
|
360 |
public IFeatureExtraction getFeatureExtraction() { |
|
361 |
return featureExtraction; |
|
362 |
} |
|
363 |
|
|
364 |
/** |
|
365 |
* @return the databaseConnector |
|
366 |
*/ |
|
367 |
public IDatabaseConnector getDatabaseConnector() { |
|
368 |
return databaseConnector; |
|
369 |
} |
|
370 |
|
|
371 |
/** |
|
372 |
* @param databaseConnector |
|
373 |
* the databaseConnector to set |
|
374 |
*/ |
|
375 |
public void setDatabaseConnector(final IDatabaseConnector databaseConnector) { |
|
376 |
this.databaseConnector = databaseConnector; |
|
377 |
} |
|
378 |
|
|
379 |
public long getTotalTransformedRecords() { |
|
380 |
return this.totalTransformedRecords; |
|
381 |
} |
|
382 |
|
|
383 |
public long getTotalIgnoredRecords() { |
|
384 |
return this.totalIgnoredRecords; |
|
385 |
} |
|
386 |
|
|
387 |
/** |
|
388 |
* @return the mappingFile |
|
389 |
*/ |
|
390 |
public String getMappingFile() { |
|
391 |
return mappingFile; |
|
392 |
} |
|
393 |
|
|
394 |
/** |
|
395 |
* @param mappingFile |
|
396 |
* the mappingFile to set |
|
397 |
*/ |
|
398 |
public void setMappingFile(final String mappingFile) { |
|
399 |
this.mappingFile = mappingFile; |
|
400 |
} |
|
401 |
|
|
402 |
public Resource getBlacklistApi() { |
|
403 |
return blacklistApi; |
|
404 |
} |
|
405 |
|
|
406 |
public void setBlacklistApi(Resource blacklistApi) { |
|
407 |
this.blacklistApi = blacklistApi; |
|
408 |
} |
|
409 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/engine/FunctionResults.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.engine; |
|
2 |
|
|
3 |
import java.util.Collection; |
|
4 |
import java.util.LinkedHashMap; |
|
5 |
import java.util.LinkedList; |
|
6 |
import java.util.List; |
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
/** |
|
10 |
* @author js |
|
11 |
* |
|
12 |
*/ |
|
13 |
public class FunctionResults { |
|
14 |
|
|
15 |
private Map<String, List<String>> resultMap = new LinkedHashMap<String, List<String>>(); |
|
16 |
|
|
17 |
|
|
18 |
/** |
|
19 |
* get the first single result from the result list at the given index |
|
20 |
* @param aIndex |
|
21 |
* @return a result |
|
22 |
*/ |
|
23 |
public String get(int aIndex){ |
|
24 |
return resultMap.get(aIndex + "").get(0); |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* get the single result for the node at the given position from the list at the given index |
|
29 |
* @param aIndex |
|
30 |
* @param aPosition |
|
31 |
* @return a result |
|
32 |
*/ |
|
33 |
public String get(int aIndex, int aPosition){ |
|
34 |
if (aPosition <= 0){ |
|
35 |
throw new IllegalArgumentException("position is " + aPosition + ", must be greater 0"); |
|
36 |
} |
|
37 |
return resultMap.get(aIndex + "").get(aPosition - 1); |
|
38 |
} |
|
39 |
|
|
40 |
/** |
|
41 |
* add a collection containing the results for each record |
|
42 |
* @param aCollection |
|
43 |
*/ |
|
44 |
public void addAll(Collection<? extends String> aCollection){ |
|
45 |
for (String result : aCollection){ |
|
46 |
add(result); |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
/** |
|
51 |
* add a single result calculated for a record node |
|
52 |
* @param aResult |
|
53 |
*/ |
|
54 |
public void add(String aResult){ |
|
55 |
List<String> resultList = new LinkedList<String>(); |
|
56 |
resultList.add(aResult); |
|
57 |
resultMap.put(resultMap.size() + "", resultList); |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* add a list of results calculated for all resp. record nodes |
|
62 |
* @param aResults |
|
63 |
*/ |
|
64 |
public void add(List<String> aResults){ |
|
65 |
resultMap.put(resultMap.size() + "", aResults); |
|
66 |
} |
|
67 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/utils/NamespaceContextImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.utils; |
|
2 |
|
|
3 |
import java.util.HashMap; |
|
4 |
import java.util.Iterator; |
|
5 |
import java.util.Map; |
|
6 |
|
|
7 |
import javax.xml.XMLConstants; |
|
8 |
import javax.xml.namespace.NamespaceContext; |
|
9 |
|
|
10 |
public class NamespaceContextImpl implements NamespaceContext { |
|
11 |
|
|
12 |
private Map<String, String> nsMap = new HashMap<String, String>(); |
|
13 |
|
|
14 |
public void addNamespace(String aPrefix, String aURI){ |
|
15 |
nsMap.put(aPrefix, aURI); |
|
16 |
} |
|
17 |
|
|
18 |
@Override |
|
19 |
public String getNamespaceURI(String aPrefix) { |
|
20 |
return nsMap.get(aPrefix); |
|
21 |
} |
|
22 |
|
|
23 |
@Override |
|
24 |
public String getPrefix(String aNamespaceURI) { |
|
25 |
if (aNamespaceURI == null){ |
|
26 |
throw new IllegalStateException(); |
|
27 |
} |
|
28 |
if (aNamespaceURI.equals(XMLConstants.XML_NS_URI)){ |
|
29 |
return XMLConstants.XML_NS_PREFIX; |
|
30 |
}else if (aNamespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)){ |
|
31 |
return XMLConstants.XMLNS_ATTRIBUTE; |
|
32 |
}else if (nsMap.values().contains(aNamespaceURI)){ |
|
33 |
for (String prefix: nsMap.keySet()){ |
|
34 |
if (nsMap.get(prefix).equals(aNamespaceURI)){ |
|
35 |
return prefix; |
|
36 |
} |
|
37 |
} |
|
38 |
} |
|
39 |
return null; |
|
40 |
} |
|
41 |
|
|
42 |
@Override |
|
43 |
public Iterator getPrefixes(String arg0) { |
|
44 |
// TODO Auto-generated method stub |
|
45 |
return null; |
|
46 |
} |
|
47 |
|
|
48 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/utils/TransformationRulesImportTool.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.utils; |
|
2 |
|
|
3 |
import java.io.StringReader; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
import org.apache.commons.text.StringEscapeUtils; |
|
7 |
|
|
8 |
import eu.dnetlib.common.profile.ProfileNotFoundException; |
|
9 |
import eu.dnetlib.data.collective.transformation.rulelanguage.RuleLanguageParser; |
|
10 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException; |
|
11 |
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService; |
|
12 |
import eu.dnetlib.enabling.tools.ServiceLocator; |
|
13 |
|
|
14 |
/** |
|
15 |
* |
|
16 |
* @author jochen |
|
17 |
* @since 1.2 |
|
18 |
*/ |
|
19 |
public class TransformationRulesImportTool { |
|
20 |
private ServiceLocator<ISLookUpService> lookupServiceLocator; |
|
21 |
|
|
22 |
/** |
|
23 |
* retrieves the transformation rule script of a transformation rule profile identified by a profile id |
|
24 |
* @param aProfileId |
|
25 |
* @return list of the transformation rule script and optionally profile id's of subscripts |
|
26 |
* @throws ProfileNotFoundException |
|
27 |
*/ |
|
28 |
protected List<String> getScript(String aProfileId) throws ProfileNotFoundException{ |
|
29 |
String xquery = "collection('/db/DRIVER/TransformationRuleDSResources')//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value ='" + |
|
30 |
aProfileId + "']//CODE/child::node(), " + |
|
31 |
"for $id in (collection('/db/DRIVER/TransformationRuleDSResources')//RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value ='" + |
|
32 |
aProfileId + "']//IMPORTED/SCRIPT_REFERENCE/@id) return string($id)"; |
|
33 |
List<String> queryResult; |
|
34 |
try { |
|
35 |
queryResult = lookupServiceLocator.getService().quickSearchProfile(xquery); |
|
36 |
if (!queryResult.isEmpty()){ |
|
37 |
return queryResult; |
|
38 |
}else{ |
|
39 |
throw new ProfileNotFoundException("no script found in profile for profileId: " + aProfileId); |
|
40 |
} |
|
41 |
} catch (ISLookUpException e) { |
|
42 |
throw new ProfileNotFoundException(e); |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
protected void importRules(RuleLanguageParser aParser, String aProfileId) throws ProfileNotFoundException{ |
|
47 |
List<String> profileQueryResult = getScript(aProfileId); |
|
48 |
String script = StringEscapeUtils.unescapeXml(profileQueryResult.get(0)); // the first entry contains the script |
|
49 |
if (script.trim().startsWith("<xsl:stylesheet")){ |
|
50 |
aParser.setXslStylesheet(script.trim()); |
|
51 |
}else{ |
|
52 |
StringReader reader = new StringReader(script); |
|
53 |
aParser.parse(reader); |
|
54 |
if (aParser.getImportedScripts().size() != (profileQueryResult.size() - 1) ) throw new IllegalStateException( |
|
55 |
"invalid number of scripts to import: " + aParser.getImportedScripts().size() + " != " + profileQueryResult.size()); |
|
56 |
// recursiv |
|
57 |
if (!aParser.getImportedScripts().isEmpty()){ |
|
58 |
for (String importScriptProfileId: profileQueryResult.subList(1, profileQueryResult.size())){ |
|
59 |
RuleLanguageParser childParser = new RuleLanguageParser(); |
|
60 |
importRules(childParser, importScriptProfileId); |
|
61 |
aParser.addRulesFromParser(childParser); |
|
62 |
} |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* gets the rule language parser, which creates the mapping of rules which is defined in a transformation rule script identified by the transformation rule profile id |
|
69 |
* @param aProfileId - id of the transformation rules profile |
|
70 |
* @return the rule language parser |
|
71 |
* @throws ProfileNotFoundException |
|
72 |
*/ |
|
73 |
public RuleLanguageParser getRuleLanguageParser(String aProfileId) throws ProfileNotFoundException{ |
|
74 |
RuleLanguageParser parser = new RuleLanguageParser(); |
|
75 |
importRules(parser, aProfileId); |
|
76 |
return parser; |
|
77 |
} |
|
78 |
|
|
79 |
public ServiceLocator<ISLookUpService> getLookupServiceLocator() { |
|
80 |
return lookupServiceLocator; |
|
81 |
} |
|
82 |
|
|
83 |
public void setLookupServiceLocator( |
|
84 |
ServiceLocator<ISLookUpService> lookupServiceLocator) { |
|
85 |
this.lookupServiceLocator = lookupServiceLocator; |
|
86 |
} |
|
87 |
|
|
88 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/schema/visitor/XSContentTypeVisitorImpl.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.core.schema.visitor; |
|
2 |
|
|
3 |
import java.util.Collection; |
|
4 |
import java.util.Iterator; |
|
5 |
|
|
6 |
import org.apache.commons.lang3.NotImplementedException; |
|
7 |
|
|
8 |
import com.sun.xml.xsom.XSAttributeUse; |
|
9 |
import com.sun.xml.xsom.XSContentType; |
|
10 |
import com.sun.xml.xsom.XSElementDecl; |
|
11 |
import com.sun.xml.xsom.XSParticle; |
|
12 |
import com.sun.xml.xsom.XSSimpleType; |
|
13 |
import com.sun.xml.xsom.XSType; |
|
14 |
import com.sun.xml.xsom.visitor.XSContentTypeVisitor; |
|
15 |
|
|
16 |
import eu.dnetlib.data.collective.transformation.core.schema.SchemaAttribute; |
|
17 |
import eu.dnetlib.data.collective.transformation.core.schema.SchemaElement; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author jochen |
|
21 |
* |
|
22 |
*/ |
|
23 |
public class XSContentTypeVisitorImpl implements XSContentTypeVisitor { |
|
24 |
|
|
25 |
private Visitor visitor; |
|
26 |
|
|
27 |
@Override |
|
28 |
public void empty(XSContentType arg0) { |
|
29 |
throw new NotImplementedException("TODO empty"); |
|
30 |
} |
|
31 |
|
|
32 |
@Override |
|
33 |
public void particle(XSParticle aParticle) { |
|
34 |
XSTermVisitorImpl termVisitor = new XSTermVisitorImpl(); |
|
35 |
termVisitor.setVisitor(this.visitor); |
|
36 |
aParticle.getTerm().visit(termVisitor); |
|
37 |
if (aParticle.getTerm().isElementDecl()){ |
|
38 |
XSElementDecl elem = aParticle.getTerm().asElementDecl(); |
|
39 |
SchemaElement element = new SchemaElement(); |
|
40 |
|
|
41 |
XSType type = elem.getType(); |
|
42 |
if (type.isComplexType()){ |
|
43 |
Collection<? extends XSAttributeUse> attrColls = |
|
44 |
type.asComplexType().getDeclaredAttributeUses(); |
|
45 |
Iterator<? extends XSAttributeUse> attrIter = attrColls.iterator(); |
|
46 |
while (attrIter.hasNext()){ |
|
47 |
XSAttributeUse attrUse = attrIter.next(); |
|
48 |
SchemaAttribute attribute = new SchemaAttribute(); |
|
49 |
attribute.setName(attrUse.getDecl().getName()); |
|
50 |
attribute.setRequired(attrUse.isRequired()); |
|
51 |
element.addAttribute(attribute); |
|
52 |
} |
|
53 |
} |
|
54 |
element.setName(elem.getName()); |
|
55 |
element.setTargetNamespace(elem.getTargetNamespace()); |
|
56 |
element.setMinOccurs(aParticle.getMinOccurs().intValue()); |
|
57 |
element.setMaxOccurs(aParticle.getMaxOccurs().intValue()); |
|
58 |
element.setRepeatable(aParticle.isRepeated()); |
|
59 |
|
|
60 |
if (elem.getType().isComplexType()){ |
|
61 |
if (elem.getType().asComplexType().getContentType().asSimpleType() != null){ |
|
62 |
element.setContainsSimpleType(true); |
|
63 |
} |
|
64 |
} |
|
65 |
this.visitor.getCurrentElement().getChildList().add(element); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
@Override |
|
70 |
public void simpleType(XSSimpleType arg0) { |
|
71 |
throw new NotImplementedException("TODO simpleType"); |
|
72 |
} |
|
73 |
|
|
74 |
public void setVisitor(Visitor visitor) { |
|
75 |
this.visitor = visitor; |
|
76 |
} |
|
77 |
|
|
78 |
public Visitor getVisitor() { |
|
79 |
return visitor; |
|
80 |
} |
|
81 |
|
|
82 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/schema/visitor/Visitor.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.core.schema.visitor; |
|
2 |
|
|
3 |
|
|
4 |
import java.util.LinkedList; |
|
5 |
import java.util.List; |
|
6 |
|
|
7 |
import org.apache.commons.lang3.NotImplementedException; |
|
8 |
//import org.apache.commons.logging.Log; |
|
9 |
//import org.apache.commons.logging.LogFactory; |
|
10 |
|
|
11 |
import com.sun.xml.xsom.XSAnnotation; |
|
12 |
import com.sun.xml.xsom.XSAttGroupDecl; |
|
13 |
import com.sun.xml.xsom.XSAttributeDecl; |
|
14 |
import com.sun.xml.xsom.XSAttributeUse; |
|
15 |
import com.sun.xml.xsom.XSComplexType; |
|
16 |
import com.sun.xml.xsom.XSContentType; |
|
17 |
import com.sun.xml.xsom.XSElementDecl; |
|
18 |
import com.sun.xml.xsom.XSFacet; |
|
19 |
import com.sun.xml.xsom.XSIdentityConstraint; |
|
20 |
import com.sun.xml.xsom.XSModelGroup; |
|
21 |
import com.sun.xml.xsom.XSModelGroupDecl; |
|
22 |
import com.sun.xml.xsom.XSNotation; |
|
23 |
import com.sun.xml.xsom.XSParticle; |
|
24 |
import com.sun.xml.xsom.XSSchema; |
|
25 |
import com.sun.xml.xsom.XSSimpleType; |
|
26 |
import com.sun.xml.xsom.XSType; |
|
27 |
import com.sun.xml.xsom.XSWildcard; |
|
28 |
import com.sun.xml.xsom.XSXPath; |
|
29 |
import com.sun.xml.xsom.visitor.XSVisitor; |
|
30 |
|
|
31 |
import eu.dnetlib.data.collective.transformation.core.schema.SchemaAttribute; |
|
32 |
import eu.dnetlib.data.collective.transformation.core.schema.SchemaElement; |
|
33 |
|
|
34 |
/** |
|
35 |
* @author jochen |
|
36 |
* |
|
37 |
*/ |
|
38 |
public class Visitor implements XSVisitor { |
|
39 |
|
|
40 |
//private static Log log = LogFactory.getLog(Visitor.class); |
|
41 |
List<SchemaElement> schemaElements = new LinkedList<SchemaElement>(); |
|
42 |
SchemaElement currentElement; |
|
43 |
SchemaAttribute currentAttribute; |
|
44 |
|
|
45 |
@Override |
|
46 |
public void annotation(XSAnnotation arg0) { |
|
47 |
throw new NotImplementedException("TODO: annotation"); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public void attGroupDecl(XSAttGroupDecl arg0) { |
|
52 |
throw new NotImplementedException("TODO attGroupDecl"); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void attributeDecl(XSAttributeDecl aAttributeDecl) { |
|
57 |
currentAttribute.setName(aAttributeDecl.getName()); |
|
58 |
//log.debug("visit attribute name: " + aAttributeDecl.getName()); |
|
59 |
//log.debug("visit attribute type: " + aAttributeDecl.getType()); |
|
60 |
throw new NotImplementedException("TODO attributeDecl"); |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public void attributeUse(XSAttributeUse aAttributeUse) { |
|
65 |
throw new NotImplementedException("TODO attributeUse"); |
|
66 |
} |
|
67 |
|
|
68 |
@Override |
|
69 |
public void complexType(XSComplexType aType) { |
|
70 |
if (aType.getDerivationMethod()== XSType.RESTRICTION){ |
|
71 |
XSContentTypeVisitorImpl contentTypeVisitor = new XSContentTypeVisitorImpl(); |
|
72 |
contentTypeVisitor.setVisitor(this); |
|
73 |
aType.getContentType().visit(contentTypeVisitor); |
|
74 |
}else{ |
|
75 |
// aType.getExplicitContent().visit(this); |
|
76 |
throw new NotImplementedException("visiting types other then 'RESTRICTION are not implemented'"); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public void facet(XSFacet arg0) { |
|
82 |
throw new NotImplementedException("TODO facet"); |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public void identityConstraint(XSIdentityConstraint arg0) { |
|
87 |
throw new NotImplementedException("TODO identityConstraint"); |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public void notation(XSNotation arg0) { |
|
92 |
throw new NotImplementedException("TODO notation"); |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public void schema(XSSchema arg0) { |
|
97 |
throw new NotImplementedException("TODO schema"); |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public void xpath(XSXPath arg0) { |
|
102 |
throw new NotImplementedException("TODO xpath"); |
|
103 |
} |
|
104 |
|
|
105 |
@Override |
|
106 |
public void elementDecl(XSElementDecl aElementDecl) { |
|
107 |
XSType type = aElementDecl.getType(); |
|
108 |
if (type.isLocal()){ |
|
109 |
// complete infos about the current element |
|
110 |
// log.debug("visitor element name: " + aElementDecl.getName()); |
|
111 |
currentElement.setName(aElementDecl.getName()); |
|
112 |
currentElement.setTargetNamespace(aElementDecl.getTargetNamespace()); |
|
113 |
type.visit(this); |
|
114 |
} |
|
115 |
} |
|
116 |
|
|
117 |
@Override |
|
118 |
public void modelGroup(XSModelGroup aGroup) { |
|
119 |
// a group of elements as childs of the root element |
|
120 |
for (XSParticle p: aGroup.getChildren()){ |
|
121 |
particle(p); |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public void modelGroupDecl(XSModelGroupDecl arg0) { |
|
127 |
throw new NotImplementedException("TODO modelGroupDecl"); |
|
128 |
} |
|
129 |
|
|
130 |
@Override |
|
131 |
public void wildcard(XSWildcard arg0) { |
|
132 |
throw new NotImplementedException("TODO wildcard"); |
|
133 |
} |
|
134 |
|
|
135 |
@Override |
|
136 |
public void empty(XSContentType arg0) { |
|
137 |
throw new NotImplementedException("TODO empty"); |
|
138 |
} |
|
139 |
|
|
140 |
@Override |
|
141 |
public void particle(XSParticle aParticle) { |
|
142 |
// create a new schema element, add to the list of schema elements, set this element as current element |
|
143 |
SchemaElement element = new SchemaElement(); |
|
144 |
element.setMinOccurs(aParticle.getMinOccurs().intValue()); |
|
145 |
element.setMaxOccurs(aParticle.getMaxOccurs().intValue()); |
|
146 |
element.setRepeatable(aParticle.isRepeated()); |
|
147 |
schemaElements.add(element); |
|
148 |
currentElement = element; |
|
149 |
XSTermVisitorImpl termVisitor = new XSTermVisitorImpl(); |
|
150 |
termVisitor.setVisitor(this); |
|
151 |
aParticle.getTerm().visit(termVisitor); |
|
152 |
} |
|
153 |
|
|
154 |
|
|
155 |
@Override |
|
156 |
public void simpleType(XSSimpleType arg0) { |
|
157 |
throw new NotImplementedException("TODO simpleType"); |
|
158 |
} |
|
159 |
|
|
160 |
public List<SchemaElement> getElements(){ |
|
161 |
return this.schemaElements; |
|
162 |
} |
|
163 |
|
|
164 |
protected SchemaElement getCurrentElement(){ |
|
165 |
return currentElement; |
|
166 |
} |
|
167 |
|
|
168 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/schema/Namespace.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.core.schema; |
|
5 |
|
|
6 |
/** |
|
7 |
* @author jochen |
|
8 |
* |
|
9 |
*/ |
|
10 |
public class Namespace { |
|
11 |
|
|
12 |
String prefix; |
|
13 |
String uri; |
|
14 |
|
|
15 |
public Namespace(String aPrefix, String aUri) { |
|
16 |
this.prefix = aPrefix; |
|
17 |
this.uri = aUri; |
|
18 |
} |
|
19 |
|
|
20 |
/** |
|
21 |
* @return the prefix |
|
22 |
*/ |
|
23 |
public String getPrefix() { |
|
24 |
return prefix; |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* @param prefix the prefix to set |
|
29 |
*/ |
|
30 |
public void setPrefix(String prefix) { |
|
31 |
this.prefix = prefix; |
|
32 |
} |
|
33 |
|
|
34 |
/** |
|
35 |
* @return the uri |
|
36 |
*/ |
|
37 |
public String getUri() { |
|
38 |
return uri; |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* @param uri the uri to set |
|
43 |
*/ |
|
44 |
public void setUri(String uri) { |
|
45 |
this.uri = uri; |
|
46 |
} |
|
47 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/xsl/AbstractXslElement.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.core.xsl; |
|
2 |
|
|
3 |
import java.util.LinkedList; |
|
4 |
import java.util.List; |
|
5 |
|
|
6 |
/** |
|
7 |
* @author jochen |
|
8 |
* |
|
9 |
*/ |
|
10 |
public abstract class AbstractXslElement { |
|
11 |
|
|
12 |
private String functionName; |
|
13 |
protected List<String> attrList = new LinkedList<String>(); |
|
14 |
protected StringBuilder enclosedElements = new StringBuilder(); |
|
15 |
protected List<String> nsList = new LinkedList<String>(); |
|
16 |
|
|
17 |
|
|
18 |
public AbstractXslElement(String aFunctioName) { |
|
19 |
this.functionName = aFunctioName; |
|
20 |
} |
|
21 |
|
|
22 |
public String asXml(boolean isEmpty){ |
|
23 |
StringBuilder builder = new StringBuilder(); |
|
24 |
builder.append("<"); |
|
25 |
builder.append(functionName + " "); |
|
26 |
for (String ns: nsList){ |
|
27 |
builder.append(ns + " "); |
|
28 |
} |
|
29 |
|
|
30 |
for (String attr: attrList){ |
|
31 |
builder.append(attr); |
|
32 |
} |
|
33 |
if (isEmpty){ |
|
34 |
builder.append("/>"); |
|
35 |
}else{ |
|
36 |
builder.append(">"); |
|
37 |
builder.append(enclosedElements.toString()); |
|
38 |
builder.append("</"); |
|
39 |
builder.append(functionName + ">"); |
|
40 |
} |
|
41 |
return builder.toString(); |
|
42 |
} |
|
43 |
|
|
44 |
public String asXml() { |
|
45 |
return asXml(false); |
|
46 |
} |
|
47 |
|
|
48 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/schema/SchemaAttribute.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package eu.dnetlib.data.collective.transformation.core.schema; |
|
5 |
|
|
6 |
/** |
|
7 |
* @author jochen |
|
8 |
* |
|
9 |
*/ |
|
10 |
public class SchemaAttribute { |
|
11 |
|
|
12 |
private String name; |
|
13 |
private boolean required; |
|
14 |
|
|
15 |
/** |
|
16 |
* @return the name |
|
17 |
*/ |
|
18 |
public String getName() { |
|
19 |
return name; |
|
20 |
} |
|
21 |
|
|
22 |
/** |
|
23 |
* @param name the name to set |
|
24 |
*/ |
|
25 |
public void setName(String name) { |
|
26 |
this.name = name; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* @return the required |
|
31 |
*/ |
|
32 |
public boolean isRequired() { |
|
33 |
return required; |
|
34 |
} |
|
35 |
|
|
36 |
/** |
|
37 |
* @param required the required to set |
|
38 |
*/ |
|
39 |
public void setRequired(boolean required) { |
|
40 |
this.required = required; |
|
41 |
} |
|
42 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" ?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
|
3 |
<parent> |
|
4 |
<groupId>eu.dnetlib</groupId> |
|
5 |
<artifactId>dnet45-parent</artifactId> |
|
6 |
<version>1.0.0</version> |
|
7 |
</parent> |
|
8 |
<modelVersion>4.0.0</modelVersion> |
|
9 |
<groupId>eu.dnetlib</groupId> |
|
10 |
<artifactId>unibi-data-collective-transformation-common</artifactId> |
|
11 |
<packaging>jar</packaging> |
|
12 |
<version>2.2.13-SNAPSHOT</version> |
|
13 |
<scm> |
|
14 |
<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/unibi-data-collective-transformation-common/trunk</developerConnection> |
|
15 |
</scm> |
|
16 |
<dependencies> |
|
17 |
<dependency> |
|
18 |
<groupId>commons-beanutils</groupId> |
|
19 |
<artifactId>commons-beanutils</artifactId> |
|
20 |
<version>1.8.3</version> |
|
21 |
</dependency> |
|
22 |
<dependency> |
|
23 |
<groupId>org.apache.commons</groupId> |
|
24 |
<artifactId>commons-text</artifactId> |
|
25 |
<version>1.3</version> |
|
26 |
</dependency> |
|
27 |
<dependency> |
|
28 |
<groupId>junit</groupId> |
|
29 |
<artifactId>junit</artifactId> |
|
30 |
<version>${junit.version}</version> |
|
31 |
<scope>test</scope> |
|
32 |
</dependency> |
|
33 |
<dependency> |
|
34 |
<groupId>org.mockito</groupId> |
|
35 |
<artifactId>mockito-core</artifactId> |
|
36 |
<version>${mockito.version}</version> |
|
37 |
<scope>test</scope> |
|
38 |
</dependency> |
|
39 |
<dependency> |
|
40 |
<groupId>org.svenson</groupId> |
|
41 |
<artifactId>svenson-json</artifactId> |
|
42 |
<version>[1.4.0,1.5.0)</version> |
|
43 |
</dependency> |
|
44 |
<dependency> |
|
45 |
<groupId>org.slf4j</groupId> |
|
46 |
<artifactId>slf4j-api</artifactId> |
|
47 |
<version>1.6.4</version> |
|
48 |
<scope>compile</scope> |
|
49 |
</dependency> |
|
50 |
<dependency> |
|
51 |
<groupId>org.slf4j</groupId> |
|
52 |
<artifactId>slf4j-log4j12</artifactId> |
|
53 |
<version>1.6.4</version> |
|
54 |
<scope>runtime</scope> |
|
55 |
</dependency> |
|
56 |
<dependency> |
|
57 |
<groupId>com.google.guava</groupId> |
|
58 |
<artifactId>guava</artifactId> |
|
59 |
<version>${google.guava.version}</version> |
|
60 |
</dependency> |
|
61 |
<dependency> |
|
62 |
<groupId>eu.dnetlib</groupId> |
|
63 |
<artifactId>cnr-service-common</artifactId> |
|
64 |
<version>[2.1.0,3.0.0)</version> |
|
65 |
</dependency> |
|
66 |
<dependency> |
|
67 |
<groupId>com.sun.xsom</groupId> |
|
68 |
<artifactId>xsom</artifactId> |
|
69 |
<version>20110809</version> |
|
70 |
</dependency> |
|
71 |
</dependencies> |
|
72 |
</project> |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/xsl/XsltConstants.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.core.xsl; |
|
2 |
|
|
3 |
/** |
|
4 |
* |
|
5 |
* @author js |
|
6 |
* |
|
7 |
*/ |
|
8 |
public class XsltConstants { |
|
9 |
|
|
10 |
public static final String applyTemplates = "xsl:apply-templates"; |
|
11 |
public static final String attribute = "xsl:attribute"; |
|
12 |
public static final String callTemplate = "xsl:call-template"; |
|
13 |
public static final String choose = "xsl:choose"; |
|
14 |
public static final String copy = "xsl:copy"; |
|
15 |
public static final String copyOf = "xsl:copy-of"; |
|
16 |
public static final String element = "xsl:element"; |
|
17 |
public static final String forEach = "xsl:for-each"; |
|
18 |
public static final String ifCondition = "xsl:if"; |
|
19 |
public static final String message = "xsl:message"; |
|
20 |
public static final String otherwise = "xsl:otherwise"; |
|
21 |
public static final String param = "xsl:param"; |
|
22 |
public static final String template = "xsl:template"; |
|
23 |
public static final String text = "xsl:text"; |
|
24 |
public static final String valueOf = "xsl:value-of"; |
|
25 |
public static final String variable = "xsl:variable"; |
|
26 |
public static final String when = "xsl:when"; |
|
27 |
public static final String withParam = "xsl:with-param"; |
|
28 |
|
|
29 |
public static final String nsXsl = "xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\""; |
|
30 |
public static final String extFuncNS = "TransformationFunction"; |
|
31 |
|
|
32 |
} |
modules/unibi-data-collective-transformation-common/branches/dismiss_repo_profiles/src/main/java/eu/dnetlib/data/collective/transformation/core/schema/SchemaInspector.java | ||
---|---|---|
1 |
package eu.dnetlib.data.collective.transformation.core.schema; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.net.URL; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import org.xml.sax.SAXException; |
|
9 |
|
|
10 |
import com.sun.xml.xsom.XSContentType; |
|
11 |
import com.sun.xml.xsom.XSElementDecl; |
|
12 |
import com.sun.xml.xsom.XSParticle; |
|
13 |
import com.sun.xml.xsom.XSSchemaSet; |
|
14 |
import com.sun.xml.xsom.XSTerm; |
|
15 |
import com.sun.xml.xsom.parser.XSOMParser; |
|
16 |
|
|
17 |
import eu.dnetlib.data.collective.transformation.core.schema.visitor.Visitor; |
|
18 |
|
|
19 |
/** |
|
20 |
* @author jochen |
|
21 |
* |
|
22 |
*/ |
|
23 |
public class SchemaInspector { |
|
24 |
|
|
25 |
private List<SchemaElement> elementList = new java.util.LinkedList<SchemaElement>(); |
|
26 |
private boolean inspected = false; |
|
27 |
private String rootElement; |
|
28 |
|
|
29 |
public void inspect(File aSchema, String aRootElement) throws SAXException, IOException{ |
|
30 |
XSOMParser parser = new XSOMParser(); |
|
31 |
parser.parse(aSchema); |
|
32 |
doInspect(parser, aRootElement); |
|
33 |
} |
|
34 |
|
|
35 |
public void inspect(URL aSchema, String aRootElement)throws SAXException{ |
|
36 |
XSOMParser parser = new XSOMParser(); |
|
37 |
parser.parse(aSchema); |
|
38 |
doInspect(parser, aRootElement); |
|
39 |
} |
|
40 |
|
|
41 |
/** |
|
42 |
* inspects the schema and creates a new list of schema elements. |
|
43 |
* @param parser |
Also available in: Unified diff
new branch