summaryrefslogtreecommitdiff
path: root/sources/scala/tools/scaladoc/Comment.java
blob: db12da4f809314aa1373d2e1f89b8b5ec6fcdb41 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.scaladoc;

import java.util.*;
import java.util.regex.*;
import ch.epfl.lamp.util.Pair;
import scalac.symtab.Symbol;
import scalac.Unit;

import java.io.StringReader;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

/**
 * Class <code>Comment</code> contains all information in comment part.
 * It allows users to get first sentence of this comment, get comment
 * for different tags...
 */
public class Comment extends DefaultHandler {

    /**
     * Holder of the comment.
     */
    public final Symbol holder;

    /**
     * Unit of the symbol associated with this comment.
     */
    Unit unit;

    /**
     * Raw text of the comment.
     */
    public final String rawText;

    /**
     * Text minus any tags.
     */
    public String text;

    /**
     * Tags minus text.
     */
    public Tag[] tags;

    /**
     * Constructor.
     *
     * @param holder
     * @param rawText
     */
    public Comment(String rawText, Symbol holder, Unit unit, HTMLValidator html) {
	this.holder = holder;
	this.rawText = cleanComment(rawText);
        this.unit = unit;
        if (!isEmpty())
            html.validate(this.rawText, this);
	parseComment();
    }

    /**
     * Returns true if this comment is empty.
     */
    public boolean isEmpty() {
	return "".equals(rawText);
    }

    /**
     * Tests if the comment contains a specified tag.
     */
    public boolean containsTag(String name) {
        name = name.startsWith("@") ? name : "@" + name;
        for(int i = 0; i < tags.length; i++)
            if (tags[i].name.equals(name))
                return true;
        return false;
    }

    /**
     * Removes the leading white spaces and asterixes.
     *
     * @param comment
     */
    public static String cleanComment(String s) {
	if (s == null)
	    return "";
        s = s.substring(3, s.length() - 2);
        StringBuffer buf = new StringBuffer();
        String regexp = "^([ \\t]*)([\\*]*)(.*)$";
        Pattern p = Pattern.compile(regexp,
                                    Pattern.MULTILINE);
        Matcher m = p.matcher(s);

        while (m.find()) {
            if (m.group(2).length() == 0)
                buf.append(m.group(1) +
                           m.group(2) +
                           m.group(3));
            else
                buf.append(m.group(3));
            buf.append("\n");
        }
        return buf.toString();
    }

    /**
     * Parses the comment string separating the description
     * text from tags.
     */
    protected void parseComment() {
        String regexp = "\n[ ]*@|\\A[ ]*@";
        String[] parts = rawText.split(regexp);
	if (parts.length == 0) {
	    text = "";
	    tags = new Tag[0];
	}
	else {
	    int startTag;
	    if (parts[0].startsWith("@")) {
		text = "";
		startTag = 0;
	    } else {
		text = parts[0];
		startTag = 1;
	    }
	    List tagList = new LinkedList();
	    for(int i = startTag; i < parts.length; i++) {
		Pair fields = Tag.split(parts[i]);
		String name = (String) fields.fst;
		String description = (String) fields.snd;
		tagList.add(new Tag(holder, "@" + name, description));
	    }
	    tags = (Tag[]) tagList.toArray(new Tag[tagList.size()]);
	}
    }

    /**
     * Returns an array of tags with text and inline.
     *
     * @param holder
     * @param s
     * @see          See Tags for a Doc comment.
     */
    public static Tag[] makeTags(Symbol holder, String s) {
	final List tagList = new LinkedList();
	Pattern p = Pattern.compile("\\{@([^\\}]*)\\}");
	Matcher m = p.matcher(s);

	int start = 0;
	while (m.find()) {
	    String txt = s.substring(start, m.start());
	    if (!txt.equals(""))
		tagList.add(new Tag(holder, "@text", txt));
	    Pair fields = Tag.split(m.group(1));
	    String name = (String) fields.fst;
	    String description = (String) fields.snd;
	    tagList.add(new Tag(holder, "@" + name, description));
	    start = m.end();
	}
	String txt = s.substring(start, s.length());
	if (!txt.equals(""))
	    tagList.add(new Tag(holder, "@text", txt));
	return (Tag[]) tagList.toArray(new Tag[tagList.size()]);
    }

    /**
     * Returns the first sentence of this comment.
     */
    public String firstSentence() {
	Pattern p = Pattern.compile("\\.(\\s)");
	Matcher m = p.matcher(text);
	if (m.find()) {
	    return text.substring(0, m.start(1));;
	} else
	    return text;
    }

    // Implementation methods for the SAX error handler.

    public void warning(SAXParseException e) {
        //showHtmlError(e);
    }

    public void error(SAXParseException e) {
        showHtmlError(e);
    }

    public void fatalError(SAXParseException e) {
        showHtmlError(e);
    }

    void showHtmlError(SAXParseException e) {
        String msg = "";
        msg += "documentation comments should be written in HTML" + "\n";
        msg += e.getMessage() + "\n";
        if (unit != null)
            unit.warning(holder.pos, msg);
    }

}

public class HTMLValidator {

    /** The URL of the file containing the DTD. */
    String dtd;

    /** HTML parser. */
    XMLReader xmlReader;

    public HTMLValidator(String dtd) {
        this.dtd = dtd;
        // parser
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(true);
            factory.setNamespaceAware(true);
            SAXParser saxParser = factory.newSAXParser();
            xmlReader = saxParser.getXMLReader();
        }
        catch(ParserConfigurationException e) { System.err.println(e.toString()); }
        catch(SAXException e) { System.err.println(e.toString()); }
    }

    public String makeDoc(String s) {
        String doc = "";
        // specify the DTD
        doc += "<?xml version=\"1.0\"?>" + "\n";
        doc += "<!DOCTYPE html SYSTEM \"" + dtd + "\">" + "\n";
        // enclose the string into a valid HTML skeletton
        doc += "<html><head><title>notitle</title></head><body>" + "\n";
        doc += s + "\n";
        doc += "</body></html>" + "\n";
        return doc;
    }

    public void validate(String s, ErrorHandler errorHandler) {
        // source to parse
        String doc = makeDoc(s);
        InputSource source = new InputSource(new StringReader(doc));
        try {
            xmlReader.setErrorHandler(errorHandler);
            xmlReader.parse(source);
        }
        catch(SAXException e) { }
        catch(Exception e) { System.err.println(e.toString()); }
    }
}