KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > aciitemeditor > sourceeditor > ACIFormattingStrategy


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

20 package org.apache.directory.ldapstudio.aciitemeditor.sourceeditor;
21
22 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants;
23 import org.eclipse.jface.text.formatter.IFormattingStrategy;
24 import org.eclipse.jface.text.source.ISourceViewer;
25
26 /**
27  * This class implements the formatting strategy for the ACI Item Editor.
28  * <ul>
29  * <li>New line after a comma
30  * <li>New line after a opened left curly
31  * </ul>
32  *
33  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
34  * @version $Rev$, $Date$
35  */

36 public class ACIFormattingStrategy implements IFormattingStrategy
37 {
38
39     /** The Constant INDENT_STRING. */
40     public static final String JavaDoc INDENT_STRING = " "; //$NON-NLS-1$
41

42     /** The Constant NEWLINE. */
43     public static final String JavaDoc NEWLINE = BrowserCoreConstants.LINE_SEPARATOR;
44     
45     /** The source viewer. */
46     private ISourceViewer sourceViewer;
47     
48     
49     /**
50      * Creates a new instance of ACIFormattingStrategy.
51      *
52      * @param sourceViewer the source viewer
53      */

54     public ACIFormattingStrategy( ISourceViewer sourceViewer )
55     {
56         this.sourceViewer = sourceViewer;
57     }
58
59
60     /**
61      * {@inheritDoc}
62      */

63     public String JavaDoc format( String JavaDoc content, boolean isLineStart, String JavaDoc indentation, int[] positions )
64     {
65         String JavaDoc oldContent = sourceViewer.getDocument().get();
66         String JavaDoc newContent = internFormat ( oldContent );
67         sourceViewer.getDocument().set( newContent );
68         
69         return null;
70     }
71
72
73     /**
74      * {@inheritDoc}
75      */

76     public void formatterStarts( String JavaDoc initialIndentation )
77     {
78     }
79
80
81     /**
82      * {@inheritDoc}
83      */

84     public void formatterStops()
85     {
86     }
87
88     private String JavaDoc internFormat( String JavaDoc content )
89     {
90         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91
92         // flag to track if a new line was started
93
boolean newLineStarted = true;
94         
95         // flag to track if we are within a quoted string
96
boolean inQuotedString = false;
97         
98         // flag to track if the current expression is appended in one-line mode
99
boolean oneLineMode = false;
100         
101         // the current indent
102
int indent = 0;
103
104         int contentLength = content.length();
105         for (int i=0; i<contentLength; i++)
106         {
107             char c = content.charAt( i );
108             
109             // track quotes
110
if( c == '"')
111             {
112                 inQuotedString = !inQuotedString;
113             }
114             
115             if( c == '{' && !inQuotedString )
116             {
117                 // check one-line mode
118
oneLineMode = checkInOneLine(i, content);
119                 
120                 if(oneLineMode)
121                 {
122                     // no new line in one-line mode
123
sb.append( c );
124                     newLineStarted = false;
125                 }
126                 else
127                 {
128                     // start a new line, but avoid blank lines if there are multiple opened curlies
129
if( !newLineStarted )
130                     {
131                         sb.append( NEWLINE );
132                         for ( int x = 0; x < indent; x++ )
133                         {
134                             sb.append( INDENT_STRING );
135                         }
136                     }
137                     
138                     // append the curly
139
sb.append( c );
140                     
141                     // start a new line and increment indent
142
sb.append( NEWLINE );
143                     newLineStarted = true;
144                     indent++;
145                     for ( int x = 0; x < indent; x++ )
146                     {
147                         sb.append( INDENT_STRING );
148                     }
149                 }
150             }
151             else if (c == '}' && !inQuotedString )
152             {
153                 if(oneLineMode)
154                 {
155                     // no new line in one-line mode
156
sb.append( c );
157                     newLineStarted = false;
158                     
159                     // closed curly indicates end of one-line mode
160
oneLineMode = false;
161                 }
162                 else
163                 {
164                     // decrement indent
165
indent--;
166     
167                     // start a new line, but avoid blank lines if there are multiple closed curlies
168
if( newLineStarted )
169                     {
170                         // delete one indent
171
sb.delete( sb.length()-INDENT_STRING.length(), sb.length() );
172                     }
173                     else
174                     {
175                         sb.append( NEWLINE );
176                         for ( int x = 0; x < indent; x++ )
177                         {
178                             sb.append( INDENT_STRING );
179                         }
180                     }
181                     
182                     // append the curly
183
sb.append( c );
184                     
185                     // start a new line
186
sb.append( NEWLINE );
187                     newLineStarted = true;
188                     for ( int x = 0; x < indent; x++ )
189                     {
190                         sb.append( INDENT_STRING );
191                     }
192                 }
193             }
194             else if (c == ',' && !inQuotedString )
195             {
196                 // start new line on comma
197
if(oneLineMode)
198                 {
199                     sb.append( c );
200                     newLineStarted = false;
201                 }
202                 else
203                 {
204                     sb.append( c );
205                     
206                     sb.append( NEWLINE );
207                     newLineStarted = true;
208                     
209                     for ( int x = 0; x < indent; x++ )
210                     {
211                         sb.append( INDENT_STRING );
212                     }
213                 }
214             }
215             else if ( Character.isWhitespace( c ) )
216             {
217                 char c1 = 'A';
218                 if(i+1 < contentLength )
219                 {
220                     c1 = content.charAt( i+1 );
221                 }
222                 
223                 if( newLineStarted )
224                 {
225                     // ignore space after starting a new line
226
}
227                 else if (c == '\n' || c == '\r' )
228                 {
229                     // ignore new lines
230
}
231                 else if( Character.isWhitespace( c1 ) || c1 == '\n' || c1 == '\r' )
232                 {
233                     // compress whitespaces
234
}
235                 else
236                 {
237                     sb.append( c );
238                 }
239             }
240             
241             else
242             {
243                 // default case: append the char
244
sb.append( c );
245                 newLineStarted = false;
246             }
247         }
248
249         return sb.toString();
250     }
251
252
253     /**
254      * Checks if an expression could be appended in one line. That is
255      * if the expression starting at index i doesn't contain nested
256      * expressions and only one comma.
257      *
258      * @param i the starting index of the expression
259      * @param content the content
260      *
261      * @return true, if the expression could be appended in one line
262      */

263     private boolean checkInOneLine( int i, String JavaDoc content )
264     {
265         // flag to track if we are within a quoted string
266
boolean inQuote = false;
267         
268         // counter for commas
269
int commaCounter = 0;
270
271         int contentLength = content.length();
272         for ( int k=i+1; k<contentLength; k++)
273         {
274             char c = content.charAt( k );
275
276             // track quotes
277
if( c == '"')
278             {
279                 inQuote = !inQuote;
280             }
281             
282             // open curly indicates nested expression
283
if( ( c == '{' ) && !inQuote )
284             {
285                 return false;
286             }
287             
288             // closing curly indicates end of expression
289
if ( c == '}' && !inQuote )
290             {
291                 return true;
292             }
293             
294             // allow only single comma in an expression in one line
295
if (c == ',' && !inQuote )
296             {
297                 commaCounter++;
298                 if(commaCounter > 1)
299                 {
300                     return false;
301                 }
302             }
303         }
304         
305         return false;
306     }
307     
308 }
309
Popular Tags