KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > nls > SimpleLineReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.nls;
12
13 import org.eclipse.jface.text.BadLocationException;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IRegion;
16
17 import org.eclipse.jdt.internal.ui.JavaPlugin;
18
19 /**
20  * Simple LineReader Helper. Returns lines including "line-break" characters.
21  */

22 public class SimpleLineReader {
23
24     private IDocument fInput;
25     private int fCurrLine;
26
27     public SimpleLineReader(IDocument input) {
28         fInput = input;
29         fCurrLine= 0;
30     }
31     
32     public String JavaDoc readLine() {
33         int nLines= fInput.getNumberOfLines();
34         if (fCurrLine >= nLines) {
35             return null;
36         }
37         
38         try {
39             IRegion region= fInput.getLineInformation(fCurrLine++);
40             String JavaDoc content= fInput.get(region.getOffset(), region.getLength());
41             
42             int start= region.getOffset();
43                 
44             boolean continuesOnNext= content.endsWith("\\") && !isCommentOrWhiteSpace(content); //$NON-NLS-1$
45

46             while (continuesOnNext && fCurrLine < nLines) {
47                 region= fInput.getLineInformation(fCurrLine++);
48                 content= fInput.get(region.getOffset(), region.getLength());
49                 continuesOnNext= content.endsWith("\\") && !isCommentOrWhiteSpace(content); //$NON-NLS-1$
50
}
51             int end;
52             if (fCurrLine < nLines) {
53                 end= fInput.getLineOffset(fCurrLine); // beginning of next
54
} else {
55                 end= fInput.getLength();
56                 if (end == start) {
57                     return null; // nd of file, empty line -> null
58
}
59             }
60             return fInput.get(start, end - start);
61         } catch (BadLocationException e) {
62             // should not happen
63
JavaPlugin.log(e);
64         }
65         return null;
66      }
67     
68     public static boolean isCommentOrWhiteSpace(String JavaDoc line) {
69         line = line.trim();
70         return (line.length() == 0) || line.charAt(0) == '!' || line.charAt(0) == '#';
71     }
72 }
73
Popular Tags