KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > formatter > comment > CommentRange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.formatter.comment;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.jdt.core.compiler.CharOperation;
19 import org.eclipse.jface.text.Position;
20
21 /**
22  * Range in a comment region in comment region coordinates.
23  *
24  * @since 3.0
25  */

26 public class CommentRange extends Position implements ICommentAttributes, IHtmlTagDelimiters {
27
28     /** The attributes of this range */
29     private int fAttributes= 0;
30
31     /**
32      * Creates a new comment range.
33      *
34      * @param position offset of the range
35      * @param count length of the range
36      */

37     public CommentRange(final int position, final int count) {
38         super(position, count);
39     }
40
41     /**
42      * Is the attribute <code>attribute</code> true?
43      *
44      * @param attribute the attribute to get
45      * @return <code>true</code> iff this attribute is <code>true</code>,
46      * <code>false</code> otherwise
47      */

48     protected final boolean hasAttribute(final int attribute) {
49         return (fAttributes & attribute) == attribute;
50     }
51
52     /**
53      * Does this comment range contain a closing HTML tag?
54      *
55      * @param token token belonging to the comment range
56      * @param tag the HTML tag to check
57      * @return <code>true</code> iff this comment range contains a closing
58      * html tag, <code>false</code> otherwise
59      */

60     protected final boolean isClosingTag(final char[] token, final char[] tag) {
61
62         boolean result= (CharOperation.indexOf(HTML_CLOSE_PREFIX, token, false) == 0)
63                 && token[token.length - 1] == HTML_TAG_POSTFIX;
64         if (result) {
65
66             setAttribute(COMMENT_CLOSE);
67             result= CharOperation.equals(tag, token, HTML_CLOSE_PREFIX.length, token.length - 1, false);
68         }
69         return result;
70     }
71
72     /**
73      * Does this comment range contain an opening HTML tag?
74      *
75      * @param token token belonging to the comment range
76      * @param tag the HTML tag to check
77      * @return <code>true</code> iff this comment range contains an
78      * opening html tag, <code>false</code> otherwise
79      */

80     protected final boolean isOpeningTag(final char[] token, final char[] tag) {
81
82         boolean result= token.length > 0
83                 && token[0] == HTML_TAG_PREFIX
84                 && (CharOperation.indexOf(HTML_CLOSE_PREFIX, token, false) != 0)
85                 && token[token.length - 1] == HTML_TAG_POSTFIX;
86         if (result) {
87
88             setAttribute(COMMENT_OPEN);
89             result= CharOperation.indexOf(tag, token, false) == 1;
90         }
91         return result;
92     }
93
94     /**
95      * Mark the comment range with the occurred HTML tags.
96      *
97      * @param tags the HTML tags to test for their occurrence
98      * @param token token belonging to the comment range
99      * @param attribute attribute to set if a HTML tag is present
100      * @param open <code>true</code> iff opening tags should be marked,
101      * <code>false</code> otherwise
102      * @param close <code>true</code> iff closing tags should be marked,
103      * <code>false</code> otherwise
104      */

105     protected final void markHtmlTag(final char[][] tags, final char[] token, final int attribute, final boolean open, final boolean close) {
106         if (token[0] == HTML_TAG_PREFIX && token[token.length - 1] == HTML_TAG_POSTFIX) {
107
108             char[] tag= null;
109             boolean isOpen= false;
110             boolean isClose= false;
111
112             for (int index= 0; index < tags.length; index++) {
113
114                 tag= tags[index];
115
116                 isOpen= isOpeningTag(token, tag);
117                 isClose= isClosingTag(token, tag);
118
119                 if ((open && isOpen) || (close && isClose)) {
120
121                     setAttribute(attribute);
122                     break;
123                 }
124             }
125         }
126     }
127
128     /**
129      * Mark the comment range with the occurred tags.
130      *
131      * @param tags the tags to test for their occurrence
132      * @param prefix the prefix which is common to all the tags to test
133      * @param token the token belonging to the comment range
134      * @param attribute attribute to set if a tag is present
135      */

136     protected final void markPrefixTag(final char[][] tags, final char prefix, final char[] token, final int attribute) {
137
138         if (token[0] == prefix) {
139
140             char[] tag= null;
141             for (int index= 0; index < tags.length; index++) {
142
143                 tag= tags[index];
144                 if (CharOperation.equals(token, tag)) {
145
146                     setAttribute(attribute);
147                     break;
148                 }
149             }
150         }
151     }
152
153     /**
154      * Marks the comment range with the HTML range tag.
155      *
156      * @param token the token belonging to the comment range
157      * @param tag the HTML tag which confines the HTML range
158      * @param level the nesting level of the current HTML range
159      * @param key the key of the attribute to set if the comment range is in
160      * the HTML range
161      * @param html <code>true</code> iff the HTML tags in this HTML range
162      * should be marked too, <code>false</code> otherwise
163      * @return the new nesting level of the HTML range
164      */

165     protected final int markTagRange(final char[] token, final char[] tag, int level, final int key, final boolean html) {
166
167         if (isOpeningTag(token, tag)) {
168             if (level++ > 0)
169                 setAttribute(key);
170         } else if (isClosingTag(token, tag)) {
171             if (--level > 0)
172                 setAttribute(key);
173         } else if (level > 0) {
174             if (html || !hasAttribute(COMMENT_HTML))
175                 setAttribute(key);
176         }
177         return level;
178     }
179
180     /**
181      * Moves this comment range.
182      *
183      * @param delta the delta to move the range
184      */

185     public final void move(final int delta) {
186         offset += delta;
187     }
188
189     /**
190      * Set the attribute <code>attribute</code> to true.
191      *
192      * @param attribute the attribute to set.
193      */

194     protected final void setAttribute(final int attribute) {
195         fAttributes |= attribute;
196     }
197
198     /**
199      * Trims this comment range at the beginning.
200      *
201      * @param delta amount to trim the range
202      */

203     public final void trimBegin(final int delta) {
204         offset += delta;
205         length -= delta;
206     }
207
208     /**
209      * Trims this comment range at the end.
210      *
211      * @param delta amount to trim the range
212      */

213     public final void trimEnd(final int delta) {
214         length += delta;
215     }
216     
217     /*
218      * @see java.lang.Object#toString()
219      * @since 3.1
220      */

221     public String JavaDoc toString() {
222         List JavaDoc attributes= new ArrayList JavaDoc();
223         if (hasAttribute(COMMENT_BLANKLINE))
224             attributes.add("COMMENT_BLANKLINE"); //$NON-NLS-1$
225
if (hasAttribute(COMMENT_BREAK))
226             attributes.add("COMMENT_BREAK"); //$NON-NLS-1$
227
if (hasAttribute(COMMENT_CLOSE))
228             attributes.add("COMMENT_CLOSE"); //$NON-NLS-1$
229
if (hasAttribute(COMMENT_CODE))
230             attributes.add("COMMENT_CODE"); //$NON-NLS-1$
231
if (hasAttribute(COMMENT_HTML))
232             attributes.add("COMMENT_HTML"); //$NON-NLS-1$
233
if (hasAttribute(COMMENT_IMMUTABLE))
234             attributes.add("COMMENT_IMMUTABLE"); //$NON-NLS-1$
235
if (hasAttribute(COMMENT_NEWLINE))
236             attributes.add("COMMENT_NEWLINE"); //$NON-NLS-1$
237
if (hasAttribute(COMMENT_OPEN))
238             attributes.add("COMMENT_OPEN"); //$NON-NLS-1$
239
if (hasAttribute(COMMENT_PARAGRAPH))
240             attributes.add("COMMENT_PARAGRAPH"); //$NON-NLS-1$
241
if (hasAttribute(COMMENT_PARAMETER))
242             attributes.add("COMMENT_PARAMETER"); //$NON-NLS-1$
243
if (hasAttribute(COMMENT_ROOT))
244             attributes.add("COMMENT_ROOT"); //$NON-NLS-1$
245
if (hasAttribute(COMMENT_SEPARATOR))
246             attributes.add("COMMENT_SEPARATOR"); //$NON-NLS-1$
247
if (hasAttribute(COMMENT_FIRST_TOKEN))
248             attributes.add("COMMENT_FIRST_TOKEN"); //$NON-NLS-1$
249
if (hasAttribute(COMMENT_STARTS_WITH_RANGE_DELIMITER))
250             attributes.add("COMMENT_STARTS_WITH_RANGE_DELIMITER"); //$NON-NLS-1$
251

252         StringBuffer JavaDoc buf= new StringBuffer JavaDoc("CommentRange [" + offset + "+" + length + "] {"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
253
for (Iterator JavaDoc it= attributes.iterator(); it.hasNext();) {
254             String JavaDoc string= (String JavaDoc) it.next();
255             buf.append(string);
256             if (it.hasNext())
257                 buf.append(", "); //$NON-NLS-1$
258
}
259         
260         return buf.toString() + "}"; //$NON-NLS-1$
261
}
262 }
263
Popular Tags