File Part Reader

Groovy (beta) code posted
created at 10 Feb 17:11

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* 
 * Copyright (C) 2007, 2008 Martin Kempf, Reto Kleeb, Michael Klenk
 *
 * IFS Institute for Software, HSR Rapperswil, Switzerland
 * http://ifs.hsr.ch/
 *
 */
package org.codehaus.groovy.eclipse.refactoring.core.utils

import org.codehaus.groovy.antlr.*;
import org.eclipse.jface.text.*;

/**
 * Reads parts of a file
 * @author reto kleeb
 */
class FilePartReader {
  
  private static String getLineInFile(filename, line){
    File fileObject = openFile(filename)
    fileObject.readLines().get(line-1)
  }
  
  private static getWords(line, range){
    def relevantPieceOfLine = line.getAt(range)
    relevantPieceOfLine = relevantPieceOfLine.replaceAll(/\(/, " ( ")
    relevantPieceOfLine = relevantPieceOfLine.replaceAll(/\)/, " ) ")
    relevantPieceOfLine.tokenize()
  }
  
  public static String readBackwardsFromCoordinate(IDocument doc, LineColumn coord){
    //make sure that no one reads from a file with impossible coordinates
    if(coord.getLine() > 0 && coord.getColumn() > 0) {
      int lineLength = doc.getLineLength(coord.getLine()-1)
      int offset = doc.getLineOffset(coord.getLine()-1)
      String line = doc.get(offset, lineLength)
      return getWords(line,(0..coord.getColumn()-2))[-1]
    } else {
      return " "
    }
  }
  
  public static String readForwardFromCoordinate(IDocument doc, LineColumn coord){
    //make sure that no one reads from a file with impossible coordinates
    if(coord.getLine() > 0 && coord.getColumn() > 0) {
      int lineLength = doc.getLineLength(coord.getLine()-1)
      int offset = doc.getLineOffset(coord.getLine()-1)
      String line = doc.get(offset, lineLength)
      return getWords(line,(coord.getColumn()-1..-1))[0]
    } else {
      return " "
    }
    
  }
  
  /*
   * DEPRECATED, no filenames anymore
  public static String readBackwardsFromCoordinate(filename, Coord){
    def relevantLine = getLineInFile(filename, Coord.getLine())
    return getWords(relevantLine,(0..Coord.getColumn()) )[-1]
  }
  
  public static String readForwardFromCoordinate(filename, Coord){
    def relevantLine = getLineInFile(filename, Coord.getLine())
    return getWords(relevantLine,(Coord.getColumn()..-1))[0]
  }*/
  
  private static boolean validateCoords(Top, Bottom){
    if(Top.getLine() > Bottom.getLine()) {
      return false
    }
    if(Top.getLine() == Bottom.getLine()){
      if(Top.getColumn() >= Bottom.getColumn() ){
        return false
      }
    }
    return true
  }
  
  private static File openFile(filename){
    File fileObject = new File(filename);
    
    if(  ! (fileObject.exists() && fileObject.isFile() && 
        fileObject.canRead() && !(fileObject.isDirectory()))){
        println "Error Reading File"
        return null
    }else{
      return fileObject
    }
  }
  
  public static String readPartsOfAFile(filename, Top, Bottom){
    
    if(!validateCoords(Top, Bottom)){
      return "IMPOSSIBLE COORDINATES"
    }
    
    int LineCounter = 0
    StringBuilder sb = new StringBuilder()
    
    try{
      File fileObject = openFile(filename)
      String LineDelimiter = getLineDelimiter(fileObject)
      
      int TopLine = Top.getLine()-1
      int BottomLine = Bottom.getLine()-1
      
      def LineArray = fileObject.readLines().getAt(TopLine..BottomLine)
  
      LineArray.each{line ->
        LineCounter++
        if(TopLine == BottomLine){
          return sb.append(line[Top.getColumn()..Bottom.getColumn()])
        }
        else if(LineCounter == 1){
          sb.append(line[Top.getColumn()..-1] + LineDelimiter)
        }
        else if(LineCounter == LineArray.size()){
          sb.append(line[0..Bottom.getColumn()] + LineDelimiter)
          return sb
        }
        else{
          sb.append(line + LineDelimiter)
        }
      }
    }  
    catch(Exception e){
      return "Couldn't read file contents at this position"
    }
    return sb.toString()
  
  }
  
  public static final String DEFAULT_LINE_DELIMITER = System.getProperty("line.separator");
  
  public static String getLineDelimiter(File file){

    def content = file.getText()
    def lineDelimiter = DEFAULT_LINE_DELIMITER
    int index = 0
    for (currentChar in content) {
      if(currentChar == '\r'){
        if(content[index+1] == '\n'){ 
          lineDelimiter = "\r\n"
          break
        }
        lineDelimiter =  "\r"    //mac os 9
        break
      }
      else if(currentChar == '\n'){
        lineDelimiter = "\n"
        break
      }
      index++;
    }
    return lineDelimiter;
  }
}
4.25 KB in 7 ms with coderay