View Javadoc

1   package org.overturetool.tex;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileReader;
6   import java.io.FileWriter;
7   import java.io.IOException;
8   import java.io.PrintWriter;
9   
10  import java.io.Writer;
11  import java.util.List;
12  import java.util.Vector;
13  
14  @SuppressWarnings("unused")
15  public class ClassExstractorFromTexFiles
16  {
17  	private static final String CLASS_START = "class ";// "//begin{vdm_al}";
18  	private static final String CLASS_END = "end ";// "//end{vdm_al}";
19  
20  	private static final String VDM_START = "//begin{vdm_al}";
21  	private static final String VDM_END = "//end{vdm_al}";
22  
23  	/***
24  	 * Creates a temporary file where all latex tags have been removed. Line numbers are preserved by insertion of empty lines
25  	 * @param files The files where LaTex should be removed from 
26  	 * @param outputDirrectory The directory to put the temporary files
27  	 * @return A list of Files where the LaTex text have been removed from 
28  	 * @throws IOException If any file error occures
29  	 */
30  	public static List<File> exstract(List<File> files, File outputDirrectory)
31  			throws IOException
32  	{
33  		if (outputDirrectory==null )
34  			return null;
35  
36  		if (!outputDirrectory.exists())
37  			outputDirrectory.mkdir();
38  
39  		List<File> newFiles = new Vector<File>();
40  		for (int i = 0; i < files.size(); i++)
41  		{
42  			File currentFile = files.get(i);
43  
44  			currentFile = new File(outputDirrectory, currentFile.getName());
45  			if (currentFile.getName().endsWith(".tex"))
46  				currentFile = new File(currentFile.getAbsolutePath() + ".vpp");
47  
48  			newFiles.add(currentFile);
49  			System.out.println(currentFile);
50  
51  			FileReader inputFileReader = new FileReader(files.get(i));
52  			FileWriter outputFileReader = new FileWriter(currentFile);
53  
54  			// Create Buffered/PrintWriter Objects
55  			BufferedReader inputStream = new BufferedReader(inputFileReader);
56  			PrintWriter outputStream = new PrintWriter(outputFileReader);
57  
58  			String inLine = null;
59  			Boolean skip = true;
60  			String currentClass = "";
61  			Boolean enabled = false;
62  			Boolean texTagsFound = false;
63  			while ((inLine = inputStream.readLine()) != null)
64  			{
65  				if (inLine.trim().startsWith(CLASS_START))
66  				{
67  					String classString = inLine.trim().substring(
68  							CLASS_START.length()).trim();
69  					int indexOfInh = classString.indexOf(':');
70  					int indexOfSpace = classString.indexOf(' ');
71  					if (indexOfInh >= 0 || indexOfSpace >= 0)
72  					{
73  
74  						if (indexOfInh >= 0)
75  							currentClass = classString.substring(0, indexOfInh);
76  						else
77  							currentClass = classString.substring(
78  									0,
79  									indexOfSpace);
80  
81  					} else
82  						currentClass = classString;
83  
84  					enabled = true;
85  					texTagsFound = true;
86  					// continue;
87  				} else if (inLine.trim().startsWith(CLASS_END + currentClass))
88  				{
89  					outputStream.println(inLine);
90  					enabled = false;
91  					continue;
92  
93  				}
94  				if (inLine.trim().startsWith(VDM_START))
95  				{
96  					enabled = true;
97  					outputStream.println("");
98  					continue;
99  
100 				}
101 				if (inLine.trim().startsWith(VDM_END))
102 				{
103 					enabled = false;
104 					outputStream.println("");
105 					continue;
106 
107 				}
108 
109 				if (enabled)
110 					outputStream.println(inLine);
111 				else
112 					outputStream.println("");
113 			}
114 			outputStream.close();
115 			inputStream.close();
116 			if (!texTagsFound)
117 				newFiles.get(i).delete();
118 
119 		}
120 		return newFiles;
121 	}
122 
123 	public static String exstractAsString(String file) throws IOException
124 	{
125 		FileReader inputFileReader = new FileReader(file);
126 		// Create Buffered/PrintWriter Objects
127 		BufferedReader inputStream = new BufferedReader(inputFileReader);
128 
129 		StringBuilder outputStream = new StringBuilder();
130 
131 		String inLine = null;
132 		Boolean skip = true;
133 		String currentClass = "";
134 		Boolean enabled = false;
135 		Boolean texTagsFound = false;
136 		while ((inLine = inputStream.readLine()) != null)
137 		{
138 			if (inLine.trim().startsWith(CLASS_START))
139 			{
140 				String classString = inLine.trim().substring(
141 						CLASS_START.length()).trim();
142 				int indexOfInh = classString.indexOf(':');
143 				int indexOfSpace = classString.indexOf(' ');
144 				if (indexOfInh >= 0 || indexOfSpace >= 0)
145 				{
146 
147 					if (indexOfInh >= 0)
148 						currentClass = classString.substring(0, indexOfInh);
149 					else
150 						currentClass = classString.substring(0, indexOfSpace);
151 
152 				} else
153 					currentClass = classString;
154 
155 				enabled = true;
156 				texTagsFound = true;
157 				// continue;
158 			} else if (inLine.trim().startsWith(CLASS_END + currentClass))
159 			{
160 				outputStream.append("\n" + inLine);
161 				enabled = false;
162 				continue;
163 
164 			}
165 			if (inLine.trim().startsWith(VDM_START))
166 			{
167 				enabled = true;
168 				outputStream.append("\n" + "");
169 				continue;
170 
171 			}
172 			if (inLine.trim().startsWith(VDM_END))
173 			{
174 				enabled = false;
175 				outputStream.append("\n" + "");
176 				continue;
177 
178 			}
179 
180 			if (enabled)
181 				outputStream.append("\n" + inLine);
182 			else
183 				outputStream.append("\n" + "");
184 		}
185 		inputStream.close();
186 
187 		return outputStream.toString();
188 	}
189 }