15 * |
15 * |
16 */ |
16 */ |
17 |
17 |
18 package com.nokia.helium.ant.data; |
18 package com.nokia.helium.ant.data; |
19 |
19 |
20 import java.io.IOException; |
|
21 import java.text.BreakIterator; |
20 import java.text.BreakIterator; |
22 import java.util.HashMap; |
21 import java.util.HashMap; |
23 import java.util.StringTokenizer; |
22 import java.util.StringTokenizer; |
24 |
23 |
25 import org.apache.tools.ant.Project; |
24 import org.apache.log4j.Logger; |
26 import org.dom4j.Comment; |
25 import org.dom4j.Comment; |
27 import org.dom4j.Node; |
26 import org.dom4j.Node; |
28 |
27 |
29 /** |
28 /** |
30 * An XML comment about an Ant object, which could be a property, target, |
29 * An XML comment about an Ant object, which could be a property, target, |
31 * fileset, etc. It should preceed the object. |
30 * fileset, etc. It should preceed the object. |
32 */ |
31 */ |
33 public class AntComment { |
32 public class AntComment { |
|
33 private static Logger log = Logger.getLogger(AntComment.class); |
|
34 |
34 private String summary = ""; |
35 private String summary = ""; |
35 private String parsedDocText = ""; |
36 private String parsedDocText = ""; |
36 private HashMap<String, String> tags; |
37 private HashMap<String, String> tags; |
37 private String objectName = ""; |
38 private String objectName = ""; |
38 private boolean isMarkedComment; |
39 private boolean isMarkedComment; |
39 |
40 |
40 public AntComment() throws IOException { |
41 public AntComment() { |
41 this(null); |
42 this(null); |
42 } |
43 } |
43 |
44 |
44 public AntComment(Comment comment) throws IOException { |
45 public AntComment(Comment comment) { |
45 tags = new HashMap<String, String>(); |
46 tags = new HashMap<String, String>(); |
46 if (comment != null) { |
47 if (comment != null) { |
47 |
48 |
48 String text = getCleanedDocNodeText(comment); |
49 String text = getCleanedDocNodeText(comment); |
49 |
50 |
58 // Currently only properties are supported |
59 // Currently only properties are supported |
59 if (text.startsWith("@property")) { |
60 if (text.startsWith("@property")) { |
60 String[] splitStrings = text.split("\\s", 3); |
61 String[] splitStrings = text.split("\\s", 3); |
61 objectName = splitStrings[1]; |
62 objectName = splitStrings[1]; |
62 if (objectName == null) { |
63 if (objectName == null) { |
63 log("Comment block: object name is not defined.", Project.MSG_WARN); |
64 log.warn("Comment block: object name is not defined."); |
64 objectName = ""; |
65 objectName = ""; |
65 } |
66 } |
66 if (splitStrings.length > 2) { |
67 if (splitStrings.length > 2) { |
67 text = splitStrings[2]; |
68 text = splitStrings[2]; |
68 } |
69 } |
72 } |
73 } |
73 parseCommentText(text); |
74 parseCommentText(text); |
74 } |
75 } |
75 } |
76 } |
76 |
77 |
77 private void parseCommentText(String text) throws IOException { |
78 private void parseCommentText(String text) { |
78 if (text.length() > 0) { |
79 if (text.length() > 0) { |
79 StringTokenizer tokenizer = new StringTokenizer(text, "@"); |
80 StringTokenizer tokenizer = new StringTokenizer(text, "@"); |
80 |
81 |
81 // Parse any free text before the tags |
82 // Parse any free text before the tags |
82 if (!text.startsWith("@")) { |
83 if (!text.startsWith("@")) { |
94 // See if there are any tags to parse |
95 // See if there are any tags to parse |
95 if (tokenizer.countTokens() > 0) { |
96 if (tokenizer.countTokens() > 0) { |
96 while (tokenizer.hasMoreElements()) { |
97 while (tokenizer.hasMoreElements()) { |
97 String tagText = (String) tokenizer.nextElement(); |
98 String tagText = (String) tokenizer.nextElement(); |
98 String[] tagParts = tagText.split("\\s", 2); |
99 String[] tagParts = tagText.split("\\s", 2); |
99 if (tagParts.length > 1) |
100 if (tagParts.length > 1) { |
100 tags.put(tagParts[0], tagParts[1].trim()); |
101 tags.put(tagParts[0], tagParts[1].trim()); |
|
102 } |
101 } |
103 } |
102 } |
104 } |
103 } |
105 } |
104 } |
106 } |
105 |
107 |
159 } |
161 } |
160 |
162 |
161 public boolean isMarkedComment() { |
163 public boolean isMarkedComment() { |
162 return isMarkedComment; |
164 return isMarkedComment; |
163 } |
165 } |
164 |
|
165 private void log(String string, int msgWarn) { |
|
166 System.out.println(string); |
|
167 } |
|
168 |
166 |
169 /** |
167 /** |
170 * Clean the whitespace of the doc text. |
168 * Clean the whitespace of the doc text. |
171 * Trim the whole string and also remove a consistent indent from the start |
169 * Trim the whole string and also remove a consistent indent from the start |
172 * of each line. |
170 * of each line. |
173 */ |
171 */ |
174 static String getCleanedDocNodeText(Node docNode) { |
172 static String getCleanedDocNodeText(Node docNode) { |
175 Node preceedingWhitespaceNode = docNode.selectSingleNode("preceding-sibling::text()"); |
173 Node preceedingWhitespaceNode = docNode.selectSingleNode("preceding-sibling::text()"); |
176 // System.out.println(whitespace); |
|
177 int indent = 0; |
174 int indent = 0; |
178 if (preceedingWhitespaceNode != null) { |
175 if (preceedingWhitespaceNode != null) { |
179 String text = preceedingWhitespaceNode.getText(); |
176 String text = preceedingWhitespaceNode.getText(); |
180 String[] lines = text.split("\n"); |
177 String[] lines = text.split("\n"); |
181 if (lines.length > 0) { |
178 if (lines.length > 0) { |
182 indent = lines[lines.length - 1].length(); |
179 indent = lines[lines.length - 1].length(); |
183 } |
180 } |
184 // System.out.println("indent: " + lines[lines.length - |
|
185 // 1].length()); |
|
186 } |
181 } |
187 |
182 |
188 String text = docNode.getText(); |
183 String text = docNode.getText(); |
189 text = text.trim(); |
184 text = text.trim(); |
190 |
185 |