Skip to main content
summaryrefslogtreecommitdiffstats
blob: 46ef33f281a1d7a8a3ef3fa36d777a5815f7c30c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Incorporated - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.rpmstubby.model;

import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Gives easy access to data from the maven pom.xml file.
 *
 */
public class PomModel {

	private Document docroot;
	private XPath xpath;

	/**
	 * Instantiates the model with the given XML document and preparing the
	 * XPath evaluation environment.
	 *
	 * @param docroot
	 *            The document to query.
	 */
	public PomModel(Document docroot) {
		this.docroot = docroot;
		xpath = XPathFactory.newInstance().newXPath();
	}

	/**
	 * Returns the proposed package name.
	 *
	 * @return The proposed package name.
	 */
	public String getPackageName() {
		// TODO make it return names suitable for Fedora's package naming
		// guidelines.
		return xpathEval("/project/artifactId");
	}

	/**
	 * Returns the artifact id xpath:/project/artifactId .
	 *
	 * @return The artifact id.
	 */
	public String getArtifactId() {
		return xpathEval("/project/artifactId");
	}

	/**
	 * Returns the group id (xpath:/project/groupId) or the groupId of the
	 * parent (xpath:/project/parent/groupId) if groupId is not present.
	 *
	 * @return The group id.
	 */
	public String getGroupId() {
		String groupId = xpathEval("/project/groupId");
		if (groupId.equals("")) {
			groupId = xpathEval("/project/parent/groupId");
		}
		return groupId;
	}

	/**
	 * Returns the summary (xpath:/project/name). Maven project name is verbose
	 * and it corresponds to the RPM specfile Summary tag.
	 *
	 * @return The summary.
	 */
	public String getSummary() {
		return xpathEval("/project/name");
	}

	/**
	 * Returns the project version (xpath:/project/version) or the parent version if
	 * version is not present.
	 *
	 * @return The version.
	 */
	public String getVersion() {
		String version = xpathEval("/project/version");
		if (version.equals("")) {
			version = xpathEval("/project/parent/version");
		}
		return version;
	}

	/**
	 * Returns the license (xpath:/project/licenses/license/name).
	 *
	 * @return The license name.
	 */
	public String getLicense() {
		// TODO make it return suitable license names.
		return xpathEval("/project/licenses/license/name");
	}

	/**
	 * Returns the URL (xpath:/project/url) or (xpath:/project/organization/url).
	 *
	 * @return The project url.
	 */
	public String getURL() {
		String url = xpathEval("/project/url");
		if (url.equals("")) {
			url = xpathEval("/project/organization/url");
		}
		return url;
	}

	/**
	 * Returns the project description (xpath:/project/description).
	 *
	 * @return The project description.
	 */
	public String getDescription() {
		getDependencies();
		return xpathEval("/project/description");
	}

	/**
	 * Returns the dependencies.
	 * @return All the dependencies.
	 */
	public List<String> getDependencies() {
		List<String> dependencies = new ArrayList<String>();
		NodeList nodes = xpathEvalNodes("/project/dependencies/dependency/artifactId");
		for (int i = 0; i < nodes.getLength(); i++) {
			Node node = nodes.item(i);
			dependencies.add(node.getTextContent());
		}
		return dependencies;
	}

	private String xpathEval(String path) {
		String result = "";
		try {
			result = xpath.evaluate(path, docroot);
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	private NodeList xpathEvalNodes(String path) {
		NodeList result = null;
		try {
			result = (NodeList) xpath.evaluate(path, docroot,
					XPathConstants.NODESET);
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}

Back to the top