source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/filters/StripJavaComments.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.6 KB
Line 
1/*
2 * Copyright 2002-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.filters;
18
19import java.io.IOException;
20import java.io.Reader;
21
22/**
23 * This is a Java comment and string stripper reader that filters
24 * those lexical tokens out for purposes of simple Java parsing.
25 * (if you have more complex Java parsing needs, use a real lexer).
26 * Since this class heavily relies on the single char read function,
27 * you are recommended to make it work on top of a buffered reader.
28 *
29 */
30public final class StripJavaComments
31 extends BaseFilterReader
32 implements ChainableReader {
33
34 /**
35 * The read-ahead character, used for effectively pushing a single
36 * character back. A value of -1 indicates that no character is in the
37 * buffer.
38 */
39 private int readAheadCh = -1;
40
41 /**
42 * Whether or not the parser is currently in the middle of a string
43 * literal.
44 */
45 private boolean inString = false;
46
47 /**
48 * Whether or not the last char has been a backslash.
49 */
50 private boolean quoted = false;
51
52 /**
53 * Constructor for "dummy" instances.
54 *
55 * @see BaseFilterReader#BaseFilterReader()
56 */
57 public StripJavaComments() {
58 super();
59 }
60
61 /**
62 * Creates a new filtered reader.
63 *
64 * @param in A Reader object providing the underlying stream.
65 * Must not be <code>null</code>.
66 */
67 public StripJavaComments(final Reader in) {
68 super(in);
69 }
70
71 /**
72 * Returns the next character in the filtered stream, not including
73 * Java comments.
74 *
75 * @return the next character in the resulting stream, or -1
76 * if the end of the resulting stream has been reached
77 *
78 * @exception IOException if the underlying stream throws an IOException
79 * during reading
80 */
81 public final int read() throws IOException {
82 int ch = -1;
83 if (readAheadCh != -1) {
84 ch = readAheadCh;
85 readAheadCh = -1;
86 } else {
87 ch = in.read();
88 if (ch == '"' && !quoted) {
89 inString = !inString;
90 quoted = false;
91 } else if (ch == '\\') {
92 quoted = !quoted;
93 } else {
94 quoted = false;
95 if (!inString) {
96 if (ch == '/') {
97 ch = in.read();
98 if (ch == '/') {
99 while (ch != '\n' && ch != -1 && ch != '\r') {
100 ch = in.read();
101 }
102 } else if (ch == '*') {
103 while (ch != -1) {
104 ch = in.read();
105 if (ch == '*') {
106 ch = in.read();
107 while (ch == '*' && ch != -1) {
108 ch = in.read();
109 }
110
111 if (ch == '/') {
112 ch = read();
113 break;
114 }
115 }
116 }
117 } else {
118 readAheadCh = ch;
119 ch = '/';
120 }
121 }
122 }
123 }
124 }
125
126 return ch;
127 }
128
129 /**
130 * Creates a new StripJavaComments using the passed in
131 * Reader for instantiation.
132 *
133 * @param rdr A Reader object providing the underlying stream.
134 * Must not be <code>null</code>.
135 *
136 * @return a new filter based on this configuration, but filtering
137 * the specified reader
138 */
139
140 public final Reader chain(final Reader rdr) {
141 StripJavaComments newFilter = new StripJavaComments(rdr);
142 return newFilter;
143 }
144}
Note: See TracBrowser for help on using the repository browser.