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 |
|
|
20 |
import java.text.BreakIterator;
|
|
21 |
import java.util.HashMap;
|
|
22 |
import java.util.StringTokenizer;
|
|
23 |
|
628
|
24 |
import org.apache.log4j.Logger;
|
587
|
25 |
import org.dom4j.Comment;
|
|
26 |
import org.dom4j.Node;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* An XML comment about an Ant object, which could be a property, target,
|
|
30 |
* fileset, etc. It should preceed the object.
|
|
31 |
*/
|
|
32 |
public class AntComment {
|
628
|
33 |
private static Logger log = Logger.getLogger(AntComment.class);
|
|
34 |
|
587
|
35 |
private String summary = "";
|
|
36 |
private String parsedDocText = "";
|
|
37 |
private HashMap<String, String> tags;
|
|
38 |
private String objectName = "";
|
|
39 |
private boolean isMarkedComment;
|
|
40 |
|
628
|
41 |
public AntComment() {
|
587
|
42 |
this(null);
|
|
43 |
}
|
|
44 |
|
628
|
45 |
public AntComment(Comment comment) {
|
587
|
46 |
tags = new HashMap<String, String>();
|
|
47 |
if (comment != null) {
|
|
48 |
|
|
49 |
String text = getCleanedDocNodeText(comment);
|
|
50 |
|
|
51 |
// See if it is a marked comment (a comment that is only
|
|
52 |
// intended to be for documentation generation)
|
|
53 |
if (text.startsWith("*")) {
|
|
54 |
text = text.substring(1).trim();
|
|
55 |
isMarkedComment = true;
|
|
56 |
}
|
|
57 |
|
|
58 |
// See if it is a comment describing an object not defined in Helium
|
|
59 |
// Currently only properties are supported
|
|
60 |
if (text.startsWith("@property")) {
|
|
61 |
String[] splitStrings = text.split("\\s", 3);
|
|
62 |
objectName = splitStrings[1];
|
|
63 |
if (objectName == null) {
|
628
|
64 |
log.warn("Comment block: object name is not defined.");
|
587
|
65 |
objectName = "";
|
|
66 |
}
|
|
67 |
if (splitStrings.length > 2) {
|
|
68 |
text = splitStrings[2];
|
|
69 |
}
|
|
70 |
else {
|
|
71 |
text = "";
|
|
72 |
}
|
|
73 |
}
|
|
74 |
parseCommentText(text);
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
628
|
78 |
private void parseCommentText(String text) {
|
587
|
79 |
if (text.length() > 0) {
|
|
80 |
StringTokenizer tokenizer = new StringTokenizer(text, "@");
|
|
81 |
|
|
82 |
// Parse any free text before the tags
|
|
83 |
if (!text.startsWith("@")) {
|
|
84 |
String freeText = tokenizer.nextToken();
|
|
85 |
|
|
86 |
BreakIterator iterator = BreakIterator.getSentenceInstance();
|
|
87 |
iterator.setText(freeText);
|
|
88 |
if (iterator.next() > 0) {
|
|
89 |
this.summary = freeText.substring(0, iterator.current()).trim();
|
|
90 |
}
|
|
91 |
|
|
92 |
parsedDocText = freeText;
|
|
93 |
}
|
|
94 |
|
|
95 |
// See if there are any tags to parse
|
|
96 |
if (tokenizer.countTokens() > 0) {
|
|
97 |
while (tokenizer.hasMoreElements()) {
|
|
98 |
String tagText = (String) tokenizer.nextElement();
|
|
99 |
String[] tagParts = tagText.split("\\s", 2);
|
628
|
100 |
if (tagParts.length > 1) {
|
588
|
101 |
tags.put(tagParts[0], tagParts[1].trim());
|
628
|
102 |
}
|
587
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* The summary text of the comment, which is the first sentence.
|
|
110 |
*
|
|
111 |
* @return The first comment sentence.
|
|
112 |
*/
|
|
113 |
public String getSummary() {
|
|
114 |
return summary;
|
|
115 |
}
|
|
116 |
|
|
117 |
/**
|
|
118 |
* The full documentation text of the comment.
|
|
119 |
*
|
|
120 |
* @return The doc text.
|
|
121 |
*/
|
|
122 |
public String getDocumentation() {
|
|
123 |
return parsedDocText;
|
|
124 |
|
|
125 |
}
|
|
126 |
|
|
127 |
/**
|
|
128 |
* The value of a comment tag that is used to describe a specific attribute
|
|
129 |
* of the Ant object.
|
|
130 |
*
|
|
131 |
* @param tag The tag name.
|
|
132 |
* @return The value of the tag.
|
|
133 |
*/
|
|
134 |
public String getTagValue(String tag) {
|
|
135 |
return getTagValue(tag, "");
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* The value of a comment tag that is used to describe a specific attribute
|
|
140 |
* of the Ant object.
|
|
141 |
*
|
|
142 |
* @param tag The tag name.
|
|
143 |
* @return The value of the tag.
|
|
144 |
*/
|
|
145 |
public String getTagValue(String tag, String defaultValue) {
|
|
146 |
String value = (String) tags.get(tag);
|
|
147 |
if (value == null) {
|
|
148 |
value = defaultValue;
|
|
149 |
}
|
|
150 |
return value;
|
|
151 |
}
|
|
152 |
|
|
153 |
/**
|
|
154 |
* Returns the name of the object when the object is defined only by a
|
|
155 |
* comment.
|
|
156 |
*
|
|
157 |
* @return An object name.
|
|
158 |
*/
|
|
159 |
public String getObjectName() {
|
|
160 |
return objectName;
|
|
161 |
}
|
|
162 |
|
|
163 |
public boolean isMarkedComment() {
|
|
164 |
return isMarkedComment;
|
|
165 |
}
|
|
166 |
|
|
167 |
/**
|
|
168 |
* Clean the whitespace of the doc text.
|
|
169 |
* Trim the whole string and also remove a consistent indent from the start
|
|
170 |
* of each line.
|
|
171 |
*/
|
|
172 |
static String getCleanedDocNodeText(Node docNode) {
|
|
173 |
Node preceedingWhitespaceNode = docNode.selectSingleNode("preceding-sibling::text()");
|
|
174 |
int indent = 0;
|
|
175 |
if (preceedingWhitespaceNode != null) {
|
|
176 |
String text = preceedingWhitespaceNode.getText();
|
|
177 |
String[] lines = text.split("\n");
|
|
178 |
if (lines.length > 0) {
|
|
179 |
indent = lines[lines.length - 1].length();
|
|
180 |
}
|
|
181 |
}
|
|
182 |
|
|
183 |
String text = docNode.getText();
|
|
184 |
text = text.trim();
|
|
185 |
|
|
186 |
String[] docLines = text.split("\n");
|
|
187 |
// Do not remove from the first line, it is already trimmed.
|
|
188 |
text = docLines[0] + "\n";
|
|
189 |
for (int i = 1; i < docLines.length; i++) {
|
|
190 |
String line = docLines[i].replaceFirst("^[ \t]{" + indent + "}", "");
|
|
191 |
text += line + "\n";
|
|
192 |
}
|
|
193 |
|
|
194 |
return text;
|
|
195 |
}
|
|
196 |
}
|