Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.profiles;
2

    
3
import java.io.IOException;
4
import java.io.StringWriter;
5
import java.util.List;
6
import java.util.Map;
7

    
8
import com.google.common.base.Splitter;
9
import com.google.common.collect.Lists;
10
import com.google.common.collect.Maps;
11
import org.antlr.stringtemplate.StringTemplate;
12
import org.apache.commons.io.IOUtils;
13
import org.apache.commons.lang.StringEscapeUtils;
14
import org.apache.commons.lang.StringUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.springframework.core.io.ClassPathResource;
18
import org.springframework.core.io.Resource;
19

    
20
public class RepositoryXsltFunctions {
21

    
22
	private static final Log log = LogFactory.getLog(RepositoryXsltFunctions.class); // NOPMD by marko on 11/24/08 5:02 PM
23

    
24
	private static Resource ifaceTemplate = new ClassPathResource("/eu/dnetlib/msro/openaireplus/workflows/profiles/repo_interface.st");
25

    
26
	private static Resource paramTemplate = new ClassPathResource("/eu/dnetlib/msro/openaireplus/workflows/profiles/repo_param.st");
27

    
28
	private enum Params {
29
		FORMAT, SET
30
	}
31

    
32
	;
33

    
34
	public static String buildInterface(final String infopackage) {
35

    
36
		final List<String> formats = Lists.newArrayList();
37
		final List<String> sets = Lists.newArrayList();
38
		final Map<String, String> otherParams = Maps.newHashMap();
39

    
40
		for (String param : parseParams(getValueBetween(infopackage, "===6===", null))) {
41
			StringTemplate p = getTemplate(paramTemplate);
42
			String paramName = StringUtils.substringBefore(param, "=").trim();
43
			String paramValue = StringUtils.substringAfter(param, "=").trim();
44

    
45
			p.setAttribute("param", paramName.toUpperCase());
46
			p.setAttribute("value", paramValue);
47

    
48
			try {
49
				switch (Params.valueOf(paramName.toUpperCase())) {
50
				case FORMAT:
51
					formats.add(p.toString());
52
					break;
53
				case SET:
54
					sets.add(p.toString());
55
					break;
56
				}
57
			} catch (Exception e) {
58
				if ((paramName != null) && !paramName.isEmpty()) {
59
					otherParams.put(paramName, StringEscapeUtils.escapeXml(paramValue));
60
				}
61
			}
62
		}
63

    
64
		final StringTemplate i = getTemplate(ifaceTemplate);
65
		i.setAttribute("id", getValueBetween(infopackage, null, "===1==="));
66
		i.setAttribute("fileMode", getValueBetween(infopackage, "===1===", "===2==="));
67
		i.setAttribute("fileDesc", getValueBetween(infopackage, "===2===", "===3==="));
68
		i.setAttribute("iisWf", getValueBetween(infopackage, "===3===", "===4==="));
69
		i.setAttribute("baseUrl", getBaseUrl(getValueBetween(infopackage, "===4===", "===5===")));
70
		i.setAttribute("accessProtocol", getAccessProtocol(getValueBetween(infopackage, "===5===", "===6==="), otherParams));
71
		i.setAttribute("formats", ensureMinOccurs(formats, "<FORMAT/>"));
72
		i.setAttribute("sets", ensureMinOccurs(sets, "<SET/>"));
73
		if (!otherParams.isEmpty()) {
74
			i.setAttribute("otherParams", otherParams);
75
		}
76

    
77
		final String iface = i.toString();
78
		return iface;
79
	}
80

    
81
	private static String getValueBetween(final String s, final String pre, final String post) {
82
		if ((pre == null) && (post == null)) {
83
			return s;
84
		} else if (pre == null) {
85
			return StringUtils.substringBefore(s, post);
86
		} else if (post == null) {
87
			return StringUtils.substringAfter(s, pre);
88
		} else {
89
			return StringUtils.substringBetween(s, pre, post);
90
		}
91
	}
92

    
93
	private static List<String> ensureMinOccurs(final List<String> list, final String empty) {
94
		if (list.isEmpty()) {
95
			list.add(empty);
96
		}
97
		return list;
98
	}
99

    
100
	private static Iterable<String> parseParams(final String s) {
101
		return Splitter.on("***").omitEmptyStrings().trimResults().split(s);
102
	}
103

    
104
	private static String getBaseUrl(final String url) {
105
		final StringTemplate b = getTemplate(paramTemplate);
106
		b.setAttribute("param", "BASE_URL");
107
		b.setAttribute("value", url);
108
		return b.toString();
109
	}
110

    
111
	private static String getAccessProtocol(final String protocol, final Map<String, String> otherParams) {
112
		final StringTemplate a = getTemplate(paramTemplate);
113
		a.setAttribute("param", "ACCESS_PROTOCOL");
114
		a.setAttribute("value", protocol);
115
		if ((otherParams != null) && !otherParams.isEmpty()) {
116
			a.setAttribute("attrs", otherParams);
117
		}
118
		return a.toString();
119
	}
120

    
121
	private static StringTemplate getTemplate(final Resource res) {
122
		final StringWriter body = new StringWriter();
123
		try {
124
			IOUtils.copy(res.getInputStream(), body);
125
			return new StringTemplate(body.toString());
126
		} catch (IOException e) {
127
			log.error("unable to get template", e);
128
			throw new RuntimeException(e);
129
		}
130
	}
131

    
132
}
    (1-1/1)