File: Token.java

package info (click to toggle)
herold 8.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,304 kB
  • sloc: java: 40,460; xml: 513; makefile: 37; sh: 29
file content (435 lines) | stat: -rw-r--r-- 9,523 bytes parent folder | download | duplicates (3)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* 
 * Copyright (C) 2001-2013 Michael Fuchs
 *
 * This file is part of herold.
 * 
 * herold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * herold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with herold.  If not, see <http://www.gnu.org/licenses/>.  
 */
package org.dbdoclet.html.tokenizer;

import java.util.HashMap;

import org.dbdoclet.service.StringServices;

/**
 * Die Klasse <code>MLToken</code> repräsentiert einen einzelnen Token.
 *
 * Ein Token entsteht bei der Zerlegung der Eingabedaten in Teilstücke.
 *
 * @author <a href="mailto:michael.fuchs@unico-group.com">Michael Fuchs</a>
 * @version 1.0
 */
public class Token {

    /** Der Inhalt des Token. */
    private String value;

    /** Der Name des Tags, falls der Token einen Tag repräsentiert.*/
    private String tagName;

    /** Die Attributes des Tags, falls der Token einen Tag repräsentiert.*/
    private HashMap<String, String> tagAttributes;

    /** Der Typ des Tokens. */
    private String type;

    /** Die Zeilenummer, in der der Token gefunden wurde. */
    private int line;

    /** Die Spaltennummer, in der der Token gefunden wurde. */
    private int column;

    public Token() {
        this("");
    }

    /**
     * Erzeugt eine neue Instanz der Klasse <code>MLToken</code>.
     *
     * @param token <code>String</code>
     */
    public Token(String token) {

        if (token == null) {

            throw new IllegalArgumentException(
                "The argument token may not be null!");
        }

        type = "__UNKNOWN__";
        tagAttributes = new HashMap<String, String>();

        this.value = parse(token);
    }

    /**
     * Die Methode <code>getAttribute</code> liefert ein bestimmtes Attribut des Tags.
     *
     * Ist der Token kein Tag, wird null zurückgegeben.
     *
     * @return <code>HashMap</code>
     */
    public String getAttribute(String attrName) {

        if (attrName == null) {
            throw new IllegalArgumentException("The argument attrName must not be null!");
        }

        if (tagAttributes != null) {

            return (String) tagAttributes.get(attrName);
        }

        return null;
    }

    /**
     * Die Methode <code>getAttributes</code> liefert die Attribute des Tags.
     *
     * Ist der Token kein Tag, wird null zurückgegeben.
     *
     * @return <code>HashMap</code>
     */
    public HashMap<String, String> getAttributes() {

        return tagAttributes;
    }

    /**
     * Die Methode <code>getColumn</code> liefert die Spaltennummer.
     *
     * @return <code>int</code>
     */
    public int getColumn() {

        return column;
    }

    /**
     * Die Methode <code>getLine</code> liefert die Zeilennummer.
     *
     * @return <code>int</code>
     */
    public int getLine() {

        return line;
    }

    /**
     * Die Methode <code>getTagName</code> liefert den Namen des Tags.
     *
     * Ist der Token kein Tag, wird null zurückgegeben.
     *
     * @return <code>String</code>
     */
    public String getTagName() {
        return tagName;
    }

    /**
     * Die Methode <code>getToken</code> liefert den Token als Ganzes.
     *
     * @return <code>String</code>
     */
    public String getValue() {

    	if (isComment() && value != null) {

    		String buffer = value.trim();
    		buffer = StringServices.cutPrefix(buffer, "<!--");
    		buffer = StringServices.cutSuffix(buffer, "-->");
    		buffer = buffer.trim();
    		return buffer;
    	}
        return value;
    }

    /**
     * Wahr, falls der Token ein schließender Tag ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isClosingTag() {

        if (type.endsWith(".Closing") || type.endsWith(".Empty")) {

            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token ein Kommentar ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isComment() {

        if (type.equals("Text.Comment")) {
            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token eine Doctype-Anweisung ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isDoctype() {

        if (type.equals("Doctype")) {

            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token ein leerer Tag ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isEmptyTag() {

        if (type.equals("Tag.Empty")) {

            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token ein JavaDoc-Tag ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isJavadoc() {

        if (type.startsWith("Tag.Javadoc.")) {

            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token ein öffnender Tag ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isOpeningTag() {

        if (type.endsWith(".Opening") || type.endsWith(".Empty")) {

            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token ein Tag ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isTag() {

        if (type.startsWith("Tag.")) {

            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token nur Text enthält ist.
     *
     * @return <code>boolean</code>
     */
    public boolean isText() {

        if (type.startsWith("Text.")) {
            return true;
        }

        return false;
    }

    /**
     * Wahr, falls der Token nur Trennzeichen enthält.
     *
     * @return <code>boolean</code>
     */
    public boolean isWhitespace() {

        if (type.equals("Text.Whitespace")) {
            return true;
        }

        return false;
    }

    /**
     * Die Methode <code>setAttributes</code> setzt die Attribute des Tags.
     *
     * @param attributes <code>HashMap</code>
     */
    public void setAttributes(HashMap<String, String> attributes) {

        this.tagAttributes = attributes;
    }

    /**
     * Die Methode <code>setColumn</code> setzt die Spaltennummer.
     *
     * @param column <code>int</code>
     */
    public void setColumn(int column) {

        this.column = column;
    }

    /**
     * Die Methode <code>setLine</code> setzt die Zeilennummer.
     *
     * @param line <code>int</code>
     */
    public void setLine(int line) {

        this.line = line;
    }

    /**
     * Die Methode <code>setTagName</code> setzt den Namen des Tags.
     *
     * @param tagName <code>String</code>
     */
    public void setTagName(String tagName) {

        if (tagName == null) {

            throw new IllegalArgumentException("Parameter tagName is null!");
        }

        this.tagName = tagName;
    }

    public void setValue(String value) {
        this.value = value;
        parse(value);
    }

    public void appendValue(String value) {
        this.value += value;
    }

    /**
     * Die Methode <code>toString</code> liefert eine textuelle Darstellung des
     * Tokens.
     *
     * @return <code>String</code>
     */
    @Override
    public String toString() {

    	StringBuilder buffer = new StringBuilder();

    	if (tagName != null) {
    		buffer.append(tagName);
    		buffer.append(", ");
    	}

    	if (type != null) {
    		buffer.append(type);
    		buffer.append(", ");
    	}

    	if (value != null) {
    		buffer.append(value);
    	}

    	if (buffer.length() > 42) {
    		buffer.delete(39, buffer.length());
    		buffer.append("...");
    	}

        return buffer.toString();
    }

    /**
     * Die Methode <code>parse</code> wertet den Inhalt des Tokens aus und setzt
     * danach den Wert des Attributes type.
     *
     * @param token <code>String</code>
     * @return <code>String</code>
     */
    private String parse(String token) {

        if (token == null) {
            return null;
        }

        String value = token.trim();

        if (value.startsWith("<!--") && value.endsWith("-->")) {

            type = "Text.Comment";

        } else if (value.startsWith("<!") && value.endsWith(">")) {

            type = "Doctype";

        } else if (value.startsWith("<?") && value.endsWith("?>")) {

            type = "Declaration";

        } else if (value.startsWith("<") && value.endsWith("/>")) {

            type = "Tag.Empty";

        } else if (value.startsWith("<javadoc:") && value.endsWith(">")) {

            type = "Tag.Javadoc.Opening";

        } else if (value.startsWith("</javadoc:") && value.endsWith(">")) {

            type = "Tag.Javadoc.Closing";

        } else if (value.startsWith("</") && value.endsWith(">")) {

            type = "Tag.Closing";

        } else if (value.startsWith("<") && value.endsWith(">")) {

            type = "Tag.Opening";

        } else if (value.trim().equals("")) {

            type = "Text.Whitespace";

        } else {

            type = "Text.";
        }

        return token;
    }
}