001: package org.incava.javadoc;
002:
003: import java.awt.Point;
004: import java.io.*;
005: import java.util.*;
006: import org.incava.text.LineMapping;
007: import org.incava.text.Location;
008:
009: /**
010: * Represents a Javadoc element.
011: */
012: public class JavadocNode {
013: private JavadocDescriptionNode description = null;
014:
015: private JavadocTaggedNode[] tagged = new JavadocTaggedNode[0];
016:
017: private int startLine;
018:
019: private int startColumn;
020:
021: private int endLine;
022:
023: private int endColumn;
024:
025: /**
026: * Parses itself from the given text.
027: */
028: public static JavadocNode parse(String text, int startLine,
029: int startColumn) {
030: JavadocParser parser = new JavadocParser();
031: List subs = parser.parse(text, startLine, startColumn);
032:
033: if (subs == null) {
034: return null;
035: } else {
036: // store line positions, for converting string positions (which are
037: // 0-based) to line:column (which are 1-based)
038:
039: LineMapping lines = new LineMapping(text, startLine,
040: startColumn);
041: JavadocNode jd = new JavadocNode();
042:
043: jd.startLine = startLine;
044: jd.startColumn = startColumn;
045:
046: Location end = lines.getLocation(text.length() - 1);
047: jd.endLine = end.line;
048: jd.endColumn = end.column;
049:
050: if (subs.size() > 0) {
051: Iterator it = subs.iterator();
052:
053: Point descPos = (Point) it.next();
054: if (descPos != null) {
055: Location[] descLocations = lines
056: .getLocations(descPos);
057:
058: // we could trim whitespace, so that the following descriptions are equivalent:
059:
060: // /** \n
061: // * This is a test. \n
062: // */
063:
064: // /** \n
065: // * This is a test. \n
066: // * @tag something
067: // */
068:
069: jd.description = new JavadocDescriptionNode(text
070: .substring(descPos.x, descPos.y),
071: descLocations[0], descLocations[1]);
072: }
073:
074: jd.tagged = new JavadocTaggedNode[subs.size() - 1];
075: for (int i = 0; it.hasNext(); ++i) {
076: Point pos = (Point) it.next();
077: Location[] locations = lines.getLocations(pos);
078:
079: jd.tagged[i] = new JavadocTaggedNode(text
080: .substring(pos.x, pos.y), locations[0],
081: locations[1]);
082: }
083: }
084:
085: return jd;
086: }
087: }
088:
089: public JavadocDescriptionNode getDescription() {
090: return description;
091: }
092:
093: public JavadocTaggedNode[] getTaggedComments() {
094: return tagged;
095: }
096:
097: public int getStartLine() {
098: return startLine;
099: }
100:
101: public int getStartColumn() {
102: return startColumn;
103: }
104:
105: public int getEndLine() {
106: return endLine;
107: }
108:
109: public int getEndColumn() {
110: return endColumn;
111: }
112:
113: }
|