1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002 |
3 | // |
4 | // All Rights Reserved |
5 | // |
6 | // This material may be reproduced by or for the U. S. Government |
7 | // pursuant to the copyright license under the clause at |
8 | // DFARS 252.227-7013 (OCT 1988). |
9 | // |
10 | package com.moesol.generator.core; |
11 | |
12 | import java.io.*; |
13 | |
14 | public class MergeTagGroker { |
15 | public static final String k_startKeep = "BEGIN_KEEP"; |
16 | public static final String k_endKeep = "END_KEEP"; |
17 | |
18 | private String m_startKeep; |
19 | private String m_endKeep; |
20 | |
21 | private Reader m_in; |
22 | private Writer m_out; |
23 | |
24 | private boolean m_keepChars; |
25 | |
26 | // yes I know false is default but good to see. |
27 | // private boolean m_keepTagSeen = false; |
28 | |
29 | public MergeTagGroker(Reader in) throws MergeException { |
30 | m_in = in; |
31 | m_startKeep = k_startKeep; |
32 | m_endKeep = k_endKeep; |
33 | m_keepChars = false; |
34 | } |
35 | |
36 | public MergeTagGroker(String startTag, String endTag, Reader in) |
37 | throws MergeException { |
38 | this(in); |
39 | m_startKeep = startTag; |
40 | m_endKeep = endTag; |
41 | } |
42 | |
43 | public void grokeTagsOnto(Writer out) throws MergeException { |
44 | if (out == null) { |
45 | throw new MergeException("The output writer must be assigned a non-null Writer."); |
46 | } |
47 | |
48 | m_out = out; |
49 | try { |
50 | int c = 0; |
51 | while ((c = m_in.read()) != -1) { |
52 | maybeOutput(c); |
53 | if ('/' == c) { |
54 | seenOneSlash(); |
55 | } |
56 | } |
57 | } catch (IOException ioe) { |
58 | throw new MergeException( |
59 | "IOException occured during merge operation.", |
60 | ioe); |
61 | } |
62 | } |
63 | |
64 | private void seenOneSlash() throws IOException { |
65 | int c; |
66 | if ((c = m_in.read()) != -1) { |
67 | maybeOutput(c); |
68 | if ('/' == c) { |
69 | seenLineCommentStart(); |
70 | } |
71 | } |
72 | } |
73 | private void seenLineCommentStart() throws IOException { |
74 | int c; |
75 | while ((c = m_in.read()) != -1) { |
76 | if ('\n' == c) { |
77 | maybeOutput(c); |
78 | return; |
79 | } |
80 | if (Character.isWhitespace((char)c)) { |
81 | maybeOutput(c); |
82 | continue; |
83 | } |
84 | seenNonBlankInComment(c); |
85 | return; |
86 | } |
87 | } |
88 | private void seenNonBlankInComment(int c) throws IOException { |
89 | String state_str = m_keepChars ? m_endKeep : m_startKeep; |
90 | int state_str_index = 0; |
91 | do { |
92 | maybeOutput(c); |
93 | if ('\n' == c) { |
94 | return; |
95 | } |
96 | if (c == state_str.charAt(state_str_index)) { |
97 | state_str_index++; |
98 | if (state_str_index == state_str.length()) { |
99 | if (m_keepChars) { |
100 | m_out.write('\n'); |
101 | m_keepChars = false; |
102 | } else { |
103 | m_out.write("\n// " + m_startKeep); |
104 | m_keepChars = true; |
105 | } |
106 | return; |
107 | } |
108 | } else { |
109 | // comment line, but with tag on it |
110 | eatToEol(); |
111 | return; |
112 | } |
113 | } while ((c = m_in.read()) != -1); |
114 | } |
115 | private void eatToEol() throws IOException { |
116 | int c; |
117 | while ((c = m_in.read()) != -1) { |
118 | maybeOutput(c); |
119 | if ('\n' == c) { |
120 | return; |
121 | } |
122 | } |
123 | } |
124 | private void maybeOutput(int c) throws IOException { |
125 | if (!m_keepChars) { |
126 | return; |
127 | } |
128 | if (c == '\r') { |
129 | return; |
130 | } |
131 | m_out.write((char)c); |
132 | } |
133 | } |