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 org.antlr.stringtemplate.StringTemplate;
9
import org.apache.commons.io.IOUtils;
10
import org.apache.commons.lang.StringEscapeUtils;
11
import org.apache.commons.lang.StringUtils;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.core.io.ClassPathResource;
15
import org.springframework.core.io.Resource;
16

    
17
import com.google.common.base.Splitter;
18
import com.google.common.collect.Lists;
19
import com.google.common.collect.Maps;
20

    
21
public class RepositoryXsltFunctions {
22

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

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

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

    
29
	private enum Params {
30
		FORMAT, SET
31
	};
32

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

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

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

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

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

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

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

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

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

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

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

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

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

    
131
}
    (1-1/1)