Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb789e6b39b22aa07583c95d69a832331e7e6278 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.text;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Ryan D. Brooks
 */
public class ExtractText {
   protected String line;
   protected int lineNum;
   protected BufferedReader in;
   protected FileWriter out;

   public ExtractText(String sourceFile) {
      try {
         in = new BufferedReader(new FileReader(sourceFile));
         out = new FileWriter("sourceText.txt");
         line = null;
         lineNum = 0;

         while ((line = in.readLine()) != null) {
            int pos = line.indexOf("/*");
            if (pos != -1) { // beginning of block comment unless in a string literal
               if (!insideStringLiteral(line, pos)) {
                  //find whole block and write to file
                  pos = line.indexOf("*/");
                  while (line != null && pos == -1) {
                     writeText();
                     line = in.readLine();
                     pos = line.indexOf("*/");
                  }
                  writeText();
               } else { // line contains a string
                  writeText();
               }
            } else if (line.indexOf("//") != -1) { // line contains a single line comment or string literal
               writeText();
            } else if ((pos = line.indexOf("\"")) != -1 && line.charAt(pos + 1) != '\'') { // line contains a string literal
               writeText();
            }
            // line does not contain text
            lineNum++;
         }
         in.close();
         out.close();
      } catch (IOException ex) {
         System.err.println(ex);
         return;
      }
   }

   protected void writeText() throws IOException {
      String str = String.valueOf(++lineNum);
      out.write(str, 0, str.length());
      out.write(line, 0, line.length());
      out.write('\n');
   }

   public static boolean insideStringLiteral(String str, int pos) {
      int index = 0;
      boolean inside = false;

      char[] chars = new char[str.length()];
      str.getChars(0, chars.length, chars, 0);

      while (index < pos) {
         if (chars[index] == '\\' && chars[index + 1] == '\"') {
            index++; //	skip over literal quotation marks
         } else if (chars[index] == '\"') {
            inside = !inside;
         }
         index++;
      }
      return inside;
   }

   public static void main(String[] args) {
      if (args.length < 1) {
         System.out.println("Usage:	ExtractText [source	file]");
         return;
      }
      new ExtractText(args[0]);
   }
}

Back to the top