587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.ant.data;
|
|
19 |
|
628
|
20 |
import java.util.Collections;
|
587
|
21 |
import java.util.HashMap;
|
|
22 |
import java.util.List;
|
|
23 |
import java.util.Map;
|
|
24 |
|
|
25 |
import org.apache.tools.ant.Project;
|
|
26 |
import org.dom4j.Comment;
|
|
27 |
import org.dom4j.Element;
|
|
28 |
import org.dom4j.Node;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Base class for all Ant Meta objects. Each Ant object is represented by a meta
|
|
32 |
* object that provides core and additional data about it.
|
|
33 |
*/
|
|
34 |
public class AntObjectMeta {
|
|
35 |
|
628
|
36 |
public static final Map<String, Integer> SCOPES;
|
|
37 |
|
|
38 |
static {
|
|
39 |
Map<String, Integer> tempMap = new HashMap<String, Integer>();
|
|
40 |
tempMap.put("public", new Integer(1));
|
|
41 |
tempMap.put("protected", new Integer(2));
|
|
42 |
tempMap.put("private", new Integer(3));
|
|
43 |
SCOPES = Collections.unmodifiableMap(tempMap);
|
|
44 |
}
|
|
45 |
|
587
|
46 |
/** The default scope if an element does not have a defined scope. */
|
|
47 |
public static final String DEFAULT_SCOPE = "public";
|
|
48 |
|
|
49 |
private static AntComment emptyComment;
|
|
50 |
|
628
|
51 |
private Project runtimeProject;
|
587
|
52 |
|
|
53 |
/** The parent meta object. */
|
|
54 |
private AntObjectMeta parent;
|
|
55 |
/** The dom4j XML Element of the Ant object represented by this meta object. */
|
|
56 |
private Node node;
|
|
57 |
/** The AntComment of any preceeding comment block. */
|
|
58 |
private AntComment comment = emptyComment;
|
|
59 |
|
|
60 |
static {
|
628
|
61 |
emptyComment = new AntComment();
|
587
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Constructor.
|
|
66 |
*
|
|
67 |
* @param parent The parent meta object.
|
|
68 |
* @param node The XML node of the Ant object.
|
|
69 |
*/
|
628
|
70 |
public AntObjectMeta(AntObjectMeta parent, Node node) {
|
587
|
71 |
this.parent = parent;
|
|
72 |
this.node = node;
|
|
73 |
processComment();
|
|
74 |
}
|
|
75 |
|
|
76 |
public Project getRuntimeProject() {
|
628
|
77 |
return runtimeProject;
|
587
|
78 |
}
|
|
79 |
|
|
80 |
public void setRuntimeProject(Project project) {
|
628
|
81 |
this.runtimeProject = project;
|
587
|
82 |
}
|
|
83 |
|
|
84 |
/**
|
|
85 |
* Gets an attribute if a value is available, otherwise returns an emtpy
|
|
86 |
* string.
|
|
87 |
*
|
|
88 |
* @param name Attribute name.
|
|
89 |
* @return Attribute value.
|
|
90 |
*/
|
|
91 |
protected String getAttr(String name) {
|
|
92 |
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
|
93 |
String value = ((Element) node).attributeValue(name);
|
|
94 |
if (value != null) {
|
|
95 |
return value;
|
|
96 |
}
|
|
97 |
}
|
|
98 |
return "";
|
|
99 |
}
|
|
100 |
|
|
101 |
protected Node getNode() {
|
|
102 |
return node;
|
|
103 |
}
|
|
104 |
|
|
105 |
protected AntComment getEmptyComment() {
|
|
106 |
return emptyComment;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Returns the meta object of the top-level project for this Ant object.
|
|
111 |
*/
|
|
112 |
public RootAntObjectMeta getRootMeta() {
|
|
113 |
if (parent instanceof RootAntObjectMeta) {
|
|
114 |
return (RootAntObjectMeta) parent;
|
|
115 |
}
|
|
116 |
return parent.getRootMeta();
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Returns the Ant file this Ant object is contained in.
|
|
121 |
*/
|
|
122 |
public AntFile getAntFile() {
|
|
123 |
return getRootMeta().getAntFile();
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Returns the top-level database object.
|
|
128 |
*/
|
|
129 |
public Database getDatabase() {
|
|
130 |
return getAntFile().getDatabase();
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Returns the name of the object or an empty string.
|
|
135 |
*
|
|
136 |
* @return Object name.
|
|
137 |
*/
|
|
138 |
public String getName() {
|
|
139 |
String name = getAttr("name");
|
|
140 |
if (name.length() == 0) {
|
|
141 |
name = getComment().getObjectName();
|
|
142 |
}
|
|
143 |
return name;
|
|
144 |
}
|
|
145 |
|
|
146 |
/**
|
|
147 |
* Returns the location path of the object.
|
|
148 |
*
|
|
149 |
* @return Location path string.
|
|
150 |
*/
|
628
|
151 |
public String getLocation() {
|
587
|
152 |
RootAntObjectMeta rootMeta = getRootMeta();
|
628
|
153 |
String location = rootMeta.getFilePath();
|
|
154 |
if (node instanceof ElementWithLocation) {
|
|
155 |
location += ":" + ((ElementWithLocation)node).getLineNumber();
|
|
156 |
}
|
|
157 |
return location;
|
587
|
158 |
}
|
|
159 |
|
|
160 |
/**
|
|
161 |
* Returns the first line summary from a doc comment.
|
|
162 |
*
|
|
163 |
* @return The documentation text.
|
|
164 |
*/
|
|
165 |
public String getSummary() {
|
|
166 |
return getComment().getSummary();
|
|
167 |
}
|
|
168 |
|
|
169 |
/**
|
|
170 |
* Returns the documentation block from a associated comment.
|
|
171 |
*
|
|
172 |
* @return The documentation text.
|
|
173 |
*/
|
|
174 |
public String getDocumentation() {
|
|
175 |
return getComment().getDocumentation();
|
|
176 |
}
|
|
177 |
|
|
178 |
/**
|
|
179 |
* Returns the scope of the object, or an empty string. This could be
|
|
180 |
* public, protected or private.
|
|
181 |
*
|
|
182 |
* @return The object scope.
|
|
183 |
*/
|
|
184 |
public String getScope() {
|
|
185 |
String scope = getComment().getTagValue("scope");
|
|
186 |
if (scope.equals("")) {
|
|
187 |
scope = DEFAULT_SCOPE;
|
|
188 |
}
|
|
189 |
return scope;
|
|
190 |
}
|
|
191 |
|
|
192 |
public boolean matchesScope(String scopeFilter) {
|
|
193 |
if (!SCOPES.containsKey(scopeFilter)) {
|
|
194 |
throw new IllegalArgumentException("Invalid scope filter: " + scopeFilter);
|
|
195 |
}
|
|
196 |
String scope = getScope();
|
628
|
197 |
if (scope.length() > 0 && !SCOPES.containsKey(scope)) {
|
587
|
198 |
log("Invalid scope: " + scope + ", " + toString(), Project.MSG_WARN);
|
|
199 |
return false;
|
|
200 |
}
|
|
201 |
return SCOPES.get(scope).compareTo(SCOPES.get(scopeFilter)) <= 0;
|
|
202 |
}
|
|
203 |
|
|
204 |
/**
|
|
205 |
* Returns the deprecated text if the object has been deprecated, or an
|
|
206 |
* empty string.
|
|
207 |
*
|
|
208 |
* @return Deprecated descripion.
|
|
209 |
*/
|
|
210 |
public String getDeprecated() {
|
|
211 |
return comment.getTagValue("deprecated");
|
|
212 |
}
|
628
|
213 |
|
|
214 |
/**
|
|
215 |
* Returns the content of the "since" tag that should indicate which release this feature
|
|
216 |
* was first added.
|
|
217 |
*
|
|
218 |
* @return Since release number.
|
|
219 |
*/
|
|
220 |
public String getSince() {
|
|
221 |
return comment.getTagValue("since");
|
|
222 |
}
|
587
|
223 |
|
|
224 |
/**
|
|
225 |
* Returns the source XML of the object.
|
|
226 |
*
|
|
227 |
* @return The XML string.
|
|
228 |
*/
|
|
229 |
public String getSource() {
|
|
230 |
// Add the raw XML content of the element
|
|
231 |
String sourceXml = node.asXML();
|
|
232 |
// Replace the CDATA end notation to avoid nested CDATA sections
|
|
233 |
sourceXml = sourceXml.replace("]]>", "] ]>");
|
|
234 |
return sourceXml;
|
|
235 |
}
|
|
236 |
|
|
237 |
/**
|
|
238 |
* Returns the AntComment that represents a preceeding comment.
|
|
239 |
*
|
|
240 |
* @return An Ant comment.
|
|
241 |
*/
|
|
242 |
protected AntComment getComment() {
|
|
243 |
return comment;
|
|
244 |
}
|
|
245 |
|
|
246 |
protected void setComment(AntComment comment) {
|
|
247 |
this.comment = comment;
|
|
248 |
}
|
|
249 |
|
628
|
250 |
private void processComment() {
|
587
|
251 |
Comment commentNode = getCommentNode();
|
|
252 |
if (commentNode != null) {
|
|
253 |
comment = new AntComment(commentNode);
|
|
254 |
}
|
|
255 |
}
|
|
256 |
|
|
257 |
@SuppressWarnings("unchecked")
|
|
258 |
private Comment getCommentNode() {
|
|
259 |
Node commentNode = null;
|
|
260 |
if (node.getNodeType() == Node.COMMENT_NODE) {
|
|
261 |
commentNode = node;
|
|
262 |
}
|
|
263 |
else {
|
|
264 |
List<Node> children = node.selectNodes("preceding-sibling::node()");
|
|
265 |
if (children.size() > 0) {
|
|
266 |
// Scan past the text nodess, which are most likely whitespace
|
|
267 |
int index = children.size() - 1;
|
|
268 |
Node child = children.get(index);
|
|
269 |
while (index > 0 && child.getNodeType() == Node.TEXT_NODE) {
|
|
270 |
index--;
|
|
271 |
child = children.get(index);
|
|
272 |
}
|
|
273 |
|
|
274 |
// Check if there is a comment node
|
|
275 |
if (child.getNodeType() == Node.COMMENT_NODE) {
|
|
276 |
commentNode = child;
|
|
277 |
log("Node has comment: " + node.getStringValue(), Project.MSG_DEBUG);
|
|
278 |
}
|
|
279 |
else {
|
|
280 |
log("Node has no comment: " + node.toString(), Project.MSG_WARN);
|
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
return (Comment)commentNode;
|
|
285 |
}
|
|
286 |
|
|
287 |
public void log(String text, int level) {
|
|
288 |
Project project = getRuntimeProject();
|
|
289 |
if (project != null) {
|
|
290 |
project.log(text, level);
|
|
291 |
}
|
|
292 |
}
|
628
|
293 |
|
|
294 |
public String toString() {
|
|
295 |
return getName();
|
|
296 |
}
|
587
|
297 |
}
|