KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > guards > JavaGuardedWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.guards;
21
22 import java.io.CharArrayWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import org.netbeans.api.editor.guards.GuardedSection;
30 import org.netbeans.api.editor.guards.InteriorSection;
31 import org.netbeans.api.editor.guards.SimpleSection;
32
33 /**
34  *
35  * @author Jan Pokorsky
36  */

37 final class JavaGuardedWriter {
38
39     private Iterator JavaDoc<SectionDescriptor> descs;
40     
41     private CharArrayWriter JavaDoc writer;
42     
43     /** Current section from the previous iterator. For filling this
44     * field is used method nextSection.
45     */

46     private SectionDescriptor current;
47
48     /** Current offset in the original document (NOT in the encapsulated
49     * output stream.
50     */

51     private int offsetCounter;
52
53     /** This flag is used during writing. It is complicated to explain. */
54     boolean wasNewLine;
55
56     /** number of consecutive spaces */
57     int spaces;
58     
59     /** Creates a new instance of JavaGuardedWriter */
60     public JavaGuardedWriter() {
61     }
62
63     public void setGuardedSection(List JavaDoc<GuardedSection> sections) {
64         assert this.descs == null; // should be invoked just once
65
this.descs = prepareSections(sections).iterator();
66     }
67
68     public char[] translate(char[] writeBuff) {
69         if (this.descs == null || !this.descs.hasNext()) {
70             return writeBuff;
71         }
72         this.writer = new CharArrayWriter JavaDoc(writeBuff.length);
73         this.offsetCounter = 0;
74         this.wasNewLine = false;
75         
76         nextSection();
77         
78         try {
79             for (char c : writeBuff) {
80                 writeOneChar(c);
81             }
82             return this.writer.toCharArray();
83         } catch (IOException JavaDoc ex) {
84             // it hardly occurs since we write to CharArrayWriter, but for sure
85
throw new IllegalStateException JavaDoc(ex);
86         } finally {
87             this.writer = null;
88             this.current = null;
89         }
90         
91     }
92
93     /** Write one character. If there is a suitable place,
94     * some special comments are written to the underlaying stream.
95     * @param b char to write.
96     */

97     void writeOneChar(int b) throws IOException JavaDoc {
98         if (b == '\r')
99             return;
100
101         if (current != null) {
102             if (offsetCounter == current.getBegin()) {
103                 wasNewLine = false;
104             }
105             if ((b == '\n') && (current.getBegin() <= offsetCounter)) {
106                 switch(current.getType()) {
107                     case LINE:
108
109                         if (!wasNewLine) {
110                             if (offsetCounter + 1 >= current.getEnd()) {
111                                 writeMagic(GuardTag.LINE, current.getName());
112                                 nextSection();
113                             }
114                             else {
115                                 writeMagic(GuardTag.BEGIN, current.getName());
116                                 wasNewLine = true;
117                             }
118                         }
119                         else {
120                             if (offsetCounter + 1 >= current.getEnd()) {
121                                 writeMagic(GuardTag.END, current.getName());
122                                 nextSection();
123                             }
124                         }
125
126                         break;
127                     case FIRST:
128                     case HEADER:
129
130                         if (!wasNewLine) {
131                             if (offsetCounter + 1 >= current.getEnd()) {
132                                 writeMagic(GuardTag.FIRST, current.getName());
133                                 nextSection();
134                             }
135                             else {
136                                 writeMagic(GuardTag.FIRST, current.getName());
137                                 wasNewLine = true;
138                             }
139                         }
140                         else {
141                             if (offsetCounter + 1 >= current.getEnd()) {
142                                 writeMagic(GuardTag.HEADEREND, current.getName());
143                                 nextSection();
144                             }
145                         }
146
147                         break;
148                     case LAST:
149                     case END:
150
151                         writeMagic(GuardTag.LAST, current.getName());
152
153                         nextSection();
154
155                         break;
156                 }
157             }
158         }
159         if (b==' ')
160             spaces++;
161         else {
162             if (spaces > 0) {
163                 char[] sp = new char[spaces];
164                 Arrays.fill(sp,' ');
165                 writer.write(sp);
166                 spaces=0;
167             }
168             writer.write(b);
169         }
170         offsetCounter++;
171     }
172
173     /** Try to get next sectionDesc from the 'sections'
174     * If there is no more section the 'current' will be set to null.
175     */

176     private void nextSection() {
177         current = descs.hasNext() ? descs.next() : null;
178     }
179
180     /** Writes the magic to the underlaying stream.
181     * @param type The type of the magic section - T_XXX constant.
182     * @param name name of the section.
183     */

184     private void writeMagic(GuardTag type, String JavaDoc name) throws IOException JavaDoc {
185         // XXX see #73805 to resolve this hack
186
// if (!shouldReload) {
187
// shouldReload = spaces != SECTION_MAGICS[type].length() + name.length();
188
// }
189
spaces = 0;
190         String JavaDoc magic = JavaGuardedReader.MAGIC_PREFIX + type.name() + ':';
191         writer.write(magic, 0, magic.length());
192         writer.write(name, 0, name.length());
193     }
194
195     /** This method prepares the iterator of the SectionDesc classes
196     * @param list The list of the GuardedSection classes.
197     * @return iterator of the SectionDesc
198     */

199     private List JavaDoc<SectionDescriptor> prepareSections(List JavaDoc<? extends GuardedSection> list) {
200         List JavaDoc<SectionDescriptor> dest = new ArrayList JavaDoc<SectionDescriptor>(list.size());
201
202         for (GuardedSection o: list) {
203             if (o instanceof SimpleSection) {
204                 SectionDescriptor desc = new SectionDescriptor(
205                         GuardTag.LINE,
206                         o.getName(),
207                         o.getStartPosition().getOffset(),
208                         o.getEndPosition().getOffset()
209                         );
210                 dest.add(desc);
211             } else {
212                 SectionDescriptor desc = new SectionDescriptor(
213                         GuardTag.HEADER,
214                         o.getName(),
215                         o.getStartPosition().getOffset(),
216                         ((InteriorSection) o).getBodyStartPosition().getOffset() - 1
217                         );
218                 dest.add(desc);
219
220                 desc = new SectionDescriptor(
221                         GuardTag.END,
222                         o.getName(),
223                         ((InteriorSection) o).getBodyEndPosition().getOffset() + 1,
224                         o.getEndPosition().getOffset()
225                         );
226                 dest.add(desc);
227             }
228         }
229         return dest;
230     }
231     
232 }
233
Popular Tags