Project

General

Profile

1
package eu.dnetlib.enabling.is.registry;
2

    
3
import java.io.IOException;
4

    
5
import javax.xml.parsers.DocumentBuilderFactory;
6
import javax.xml.parsers.ParserConfigurationException;
7
import javax.xml.xpath.XPath;
8
import javax.xml.xpath.XPathExpressionException;
9
import javax.xml.xpath.XPathFactory;
10

    
11
import org.w3c.dom.Document;
12
import org.xml.sax.SAXException;
13

    
14
/**
15
 * Resolves resource kinds (pending and normal from the application profile xml file).
16
 * 
17
 * @author marko
18
 * 
19
 */
20
public class ApplicationProfileResourceKindResolver implements ResourceKindResolver {
21

    
22
	/**
23
	 * information space application profile. Contains, among others, mappings for the.. TODO: move to a DAO.
24
	 * 
25
	 */
26
	private Document appProfile;
27

    
28
	public ApplicationProfileResourceKindResolver() {
29
		try {
30
			appProfile = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
31
					getClass().getResourceAsStream("DRIVERInformationSpaceApplicationProfile.xml"));
32
		} catch (SAXException e) {
33
			throw new IllegalStateException("cannot parse information space application profile", e);
34
		} catch (IOException e) {
35
			throw new IllegalStateException("cannot parse information space application profile", e);
36
		} catch (ParserConfigurationException e) {
37
			throw new IllegalStateException("cannot parse information space application profile", e);
38
		}
39
	}
40

    
41
	/**
42
	 * get the default resource kind associated with a given resource type.
43
	 * 
44
	 * @param resourceType
45
	 *            resource type
46
	 * @return resourceType
47
	 * @throws XPathExpressionException
48
	 *             happens?
49
	 */
50
	@Override
51
	public String getNormalKindForType(final String resourceType) throws XPathExpressionException {
52
		final XPath xpath = XPathFactory.newInstance().newXPath();
53
		final String res = xpath.evaluate("//RESOURCE_TYPE[text() = '" + resourceType + "']/../../RESOURCE_KIND", appProfile);
54
		if (res.isEmpty())
55
			return null;
56
		return res;
57
	}
58

    
59
	/**
60
	 * find the associated pending typology for a given resource type.
61
	 * 
62
	 * @param resourceType
63
	 *            resource type
64
	 * @return pending resource kind
65
	 * @throws XPathExpressionException
66
	 *             shouldn't happen
67
	 */
68
	@Override
69
	public String getPendingKindForType(final String resourceType) throws XPathExpressionException {
70
		final XPath xpath = XPathFactory.newInstance().newXPath();
71
		final String res = xpath.evaluate("//RESOURCE_TYPE[text() = '" + resourceType + "']/../../PENDING_TYPOLOGY", appProfile);
72
		if (res.isEmpty())
73
			throw new NoSuchPendingCategoryException("no pending category for " + resourceType);
74
		return res;
75
	}
76

    
77
}
(1-1/5)