Project

General

Profile

« Previous | Next » 

Revision 57486

Added by Enrico Ottonello over 4 years ago

removed mapping from profiles section, mapping is taken only from url; x3m transformation node moved to dnet-ariadneplus module

View differences:

ApplyX3Mapping.java
19 19
public class ApplyX3Mapping implements Function<String, String> {
20 20

  
21 21
	private static final Log log = LogFactory.getLog(ApplyX3Mapping.class);
22
	private String[] mappings;
23 22
	private String generatorPolicy;
24 23
	private boolean verboseLogging;
25 24
	private URL mappingUrl;
26

  
27
	public ApplyX3Mapping(final String[] mappings, final String generatorPolicy, final boolean verboseLogging) {
28
		this.mappings = mappings;
29
		this.generatorPolicy = generatorPolicy;
30
		this.verboseLogging = verboseLogging;
31
	}
32 25
	
33 26
	public ApplyX3Mapping(final URL mappingUrl, final String generatorPolicy, final boolean verboseLogging) {
34 27
		this.mappingUrl = mappingUrl;
......
38 31

  
39 32
	@Override
40 33
	public String apply(final String metadata) {
41
		InputStream[] mappingStreams = null;
42
		if (mappingUrl==null) {
43
			mappingStreams = new InputStream[mappings.length];
44
			try {
45
				for (int i = 0; i < mappings.length; i++) {
46
					mappingStreams[i] = getStream(mappings[i]);
47
				}
48
			}catch(IOException e){
49
				log.error("Can't create mappingStreams for mappings");
50
				closeStreams(mappingStreams);
51
				throw new RuntimeException(e);
52
			}
53
		}
54 34
		try (   InputStream policyStream = getStream(generatorPolicy);
55 35
				InputStream metadataStream = getStream(metadata);
56 36
				final ByteArrayOutputStream os = new ByteArrayOutputStream()){
57
			X3MLEngineFactory x3mEngineFactory  = null;
58
			if (mappingUrl==null) {
59
				x3mEngineFactory = getConfiguredX3MEngineFactory(mappingStreams, policyStream);
60
			}
61
			else {
62
				x3mEngineFactory = getConfiguredX3MEngineFactory(mappingUrl, policyStream);
63
			}
37
			X3MLEngineFactory x3mEngineFactory = getConfiguredX3MEngineFactory(mappingUrl, policyStream);
64 38
			x3mEngineFactory.withInput(metadataStream).withOutput(os, OutputFormat.RDF_XML_PLAIN);
65 39
			x3mEngineFactory.execute();
66 40
			return new String(os.toByteArray());
......
68 42
			log.error("Error transforming record: "+e.getMessage()+"\n"+metadata);
69 43
			throw new RuntimeException(e);
70 44
		}
71
		finally {
72
			if (mappingStreams!=null) {
73
				closeStreams(mappingStreams);
74
			}
75
		}
76 45
	}
77 46

  
78
	private void closeStreams(InputStream[] streams){
79
		for(InputStream s: streams){
80
			try {
81
				if(s != null) s.close();
82
			} catch (IOException e) {
83
				log.error("Can't close stream"+e.getMessage());
84
			}
85
		}
86
	}
87

  
88
	private X3MLEngineFactory getConfiguredX3MEngineFactory(InputStream[] mappings, InputStream policy) {
89
		X3MLEngineFactory x3mEngineFactory = X3MLEngineFactory.create().withMappings(mappings);
90
		if (policy != null) {
91
			x3mEngineFactory.withGeneratorPolicy(policy);
92
		}
93
		if (verboseLogging)
94
			x3mEngineFactory.withVerboseLogging();
95
		//to enable real UUID
96
		x3mEngineFactory.withUuidSize(0);
97
		return x3mEngineFactory;
98
	}
99

  
100 47
	private InputStream getStream(String s) throws IOException {
101 48
		if (StringUtils.isNotBlank(s)) {
102 49
			return IOUtils.toInputStream(s, "UTF-8");

Also available in: Unified diff