KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > StripJavaComments


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 /**
24  * This is a Java comment and string stripper reader that filters
25  * those lexical tokens out for purposes of simple Java parsing.
26  * (if you have more complex Java parsing needs, use a real lexer).
27  * Since this class heavily relies on the single char read function,
28  * you are recommended to make it work on top of a buffered reader.
29  *
30  */

31 public final class StripJavaComments
32     extends BaseFilterReader
33     implements ChainableReader {
34
35     /**
36      * The read-ahead character, used for effectively pushing a single
37      * character back. A value of -1 indicates that no character is in the
38      * buffer.
39      */

40     private int readAheadCh = -1;
41
42     /**
43      * Whether or not the parser is currently in the middle of a string
44      * literal.
45      */

46     private boolean inString = false;
47
48     /**
49      * Whether or not the last char has been a backslash.
50      */

51     private boolean quoted = false;
52
53     /**
54      * Constructor for "dummy" instances.
55      *
56      * @see BaseFilterReader#BaseFilterReader()
57      */

58     public StripJavaComments() {
59         super();
60     }
61
62     /**
63      * Creates a new filtered reader.
64      *
65      * @param in A Reader object providing the underlying stream.
66      * Must not be <code>null</code>.
67      */

68     public StripJavaComments(final Reader JavaDoc in) {
69         super(in);
70     }
71
72     /**
73      * Returns the next character in the filtered stream, not including
74      * Java comments.
75      *
76      * @return the next character in the resulting stream, or -1
77      * if the end of the resulting stream has been reached
78      *
79      * @exception IOException if the underlying stream throws an IOException
80      * during reading
81      */

82     public int read() throws IOException JavaDoc {
83         int ch = -1;
84         if (readAheadCh != -1) {
85             ch = readAheadCh;
86             readAheadCh = -1;
87         } else {
88             ch = in.read();
89             if (ch == '"' && !quoted) {
90                 inString = !inString;
91                 quoted = false;
92             } else if (ch == '\\') {
93                 quoted = !quoted;
94             } else {
95                 quoted = false;
96                 if (!inString) {
97                     if (ch == '/') {
98                         ch = in.read();
99                         if (ch == '/') {
100                             while (ch != '\n' && ch != -1 && ch != '\r') {
101                                 ch = in.read();
102                             }
103                         } else if (ch == '*') {
104                             while (ch != -1) {
105                                 ch = in.read();
106                                 if (ch == '*') {
107                                     ch = in.read();
108                                     while (ch == '*' && ch != -1) {
109                                         ch = in.read();
110                                     }
111
112                                     if (ch == '/') {
113                                         ch = read();
114                                         break;
115                                     }
116                                 }
117                             }
118                         } else {
119                             readAheadCh = ch;
120                             ch = '/';
121                         }
122                     }
123                 }
124             }
125         }
126
127         return ch;
128     }
129
130     /**
131      * Creates a new StripJavaComments using the passed in
132      * Reader for instantiation.
133      *
134      * @param rdr A Reader object providing the underlying stream.
135      * Must not be <code>null</code>.
136      *
137      * @return a new filter based on this configuration, but filtering
138      * the specified reader
139      */

140
141     public Reader JavaDoc chain(final Reader JavaDoc rdr) {
142         StripJavaComments newFilter = new StripJavaComments(rdr);
143         return newFilter;
144     }
145 }
146
Popular Tags