View Javadoc

1   /**
2    * 
3    */
4   package edu.odu.cs.codeAnnotation;
5   
6   import java.io.BufferedReader;
7   import java.io.BufferedWriter;
8   import java.io.FileReader;
9   import java.io.FileWriter;
10  import java.io.IOException;
11  import java.util.regex.Matcher;
12  import java.util.regex.Pattern;
13  
14  
15  /**
16   * Annotates C++ & Java source code for inclusion in LaTeX documents
17   * (using the listings package).
18   * 
19   * Annotations supported are:
20   *     #...# => <:\smvdots:>  small vertical dots, the # stand for the slash-asterisk asterisk-slash comment markers
21   *     //... => <:\smvdots:>
22   *     #+N# => <+N>    These commands are repeated line-by-line, N an integer 1..3
23   *     #-N# => <-N> 
24   *     #coN# => <:\conum{n}:>   call-out numbers, N=1..9
25   * @author zeil
26   *
27   */
28  public class Code2TeXSimple {
29  	
30  	BufferedReader input;
31  	BufferedWriter output;
32  	
33  	public Code2TeXSimple (String filename) throws IOException
34  	{
35  		input = new BufferedReader (new FileReader(filename));
36  		output = new BufferedWriter (new FileWriter(filename + ".tex"));
37  	}
38  	
39  	public Code2TeXSimple () throws IOException
40  	{
41  		input = null;
42  		output = null;
43  	}
44  
45  	/**
46  	 * @param args
47  	 * @throws IOException 
48  	 */
49  	public static void main(String[] args) throws IOException {
50  		// takes source code file name as first parameter
51  		new Code2TeXSimple(args[0]).process();
52  	}
53  
54  	/**
55  	 * Carry out substitutions across an entire file
56  	 * @throws IOException
57  	 */
58  	public void process() throws IOException {
59  		StringBuffer buf = new StringBuffer();
60  		String line;
61  		while ((line = input.readLine()) != null) {
62  			buf.append(line);
63  			buf.append("\n");
64  		}
65  		String result = process (buf.toString());
66  		output.write (result, 0, result.length());
67  		output.close();
68  	}
69  
70  	/**
71  	 * Carry out substitutions within a multi-line string
72  	 * @param sourceCode
73  	 * @return soureCode with substitutions
74  	 */
75  	public String process(String sourceCode) {
76  		Pattern dots = Pattern.compile("/\\*\\**\\.\\.\\.\\*/");
77  		Matcher m = dots.matcher(sourceCode);
78  		while (m.find()) {
79  			String before = sourceCode.substring(0, m.start());
80  			String block = "<:\\smvdots:>";
81  			String after = sourceCode.substring(m.end());
82  			sourceCode = before + block + after;
83  			m = dots.matcher(sourceCode);
84  		}
85  		Pattern dots2 = Pattern.compile("//\\.\\.\\.");
86  		m = dots2.matcher(sourceCode);
87  		while (m.find()) {
88  			String before = sourceCode.substring(0, m.start());
89  			String block = "<:\\smvdots:>";
90  			String after = sourceCode.substring(m.end());
91  			sourceCode = before + block + after;
92  			m = dots.matcher(sourceCode);
93  		}
94  		
95  		Pattern callout = Pattern.compile("/\\*\\**co(\\d+)\\*/");
96  		m = callout.matcher(sourceCode);
97  		while (m.find()) {
98  			String before = sourceCode.substring(0, m.start());
99  			String block = "<:\\conum{" + m.group(1) + "}:>";
100 			String after = sourceCode.substring(m.end());
101 			sourceCode = before + block + after;
102 			m = dots.matcher(sourceCode);
103 		}
104 		
105 		
106 		Pattern plusBlock = Pattern.compile("(?s)/\\*\\+\\*/(.*?)/\\*\\**-\\*/");
107 		m = plusBlock.matcher(sourceCode);
108 		while (m.find()) {
109 			String before = sourceCode.substring(0, m.start());
110 			String block = m.group(1);
111 			String after = sourceCode.substring(m.end());
112 			block = block.replaceAll("\n", "<-1>\n<+1>");
113 			sourceCode = before + "<+1>" + block + "<-1>" + after;
114 			m = plusBlock.matcher(sourceCode);
115 		}
116 		
117 		plusBlock = Pattern.compile("(?s)/\\*\\+1\\*/(.*?)/\\*\\**-1\\*/");
118 		m = plusBlock.matcher(sourceCode);
119 		while (m.find()) {
120 			String before = sourceCode.substring(0, m.start());
121 			String block = m.group(1);
122 			String after = sourceCode.substring(m.end());
123 			block = block.replaceAll("\n", "<-1>\n<+1>");
124 			sourceCode = before + "<+1>" + block + "<-1>" + after;
125 			m = plusBlock.matcher(sourceCode);
126 		}
127 		
128 		plusBlock = Pattern.compile("(?s)/\\*\\+2\\*/(.*?)/\\*\\**-2\\*/");
129 		m = plusBlock.matcher(sourceCode);
130 		while (m.find()) {
131 			String before = sourceCode.substring(0, m.start());
132 			String block = m.group(1);
133 			String after = sourceCode.substring(m.end());
134 			block = block.replaceAll("\n", "<-2>\n<+2>");
135 			sourceCode = before + "<+2>" + block + "<-2>" + after;
136 			m = plusBlock.matcher(sourceCode);
137 		}
138 
139 		
140 		plusBlock = Pattern.compile("(?s)/\\*\\+3\\*/(.*?)/\\*\\**-3\\*/");
141 		m = plusBlock.matcher(sourceCode);
142 		while (m.find()) {
143 			String before = sourceCode.substring(0, m.start());
144 			String block = m.group(1);
145 			String after = sourceCode.substring(m.end());
146 			block = block.replaceAll("\n", "<-3>\n<+3>");
147 			sourceCode = before + "<+3>" + block + "<-3>" + after;
148 			m = plusBlock.matcher(sourceCode);
149 		}
150 
151 		return sourceCode;
152 	}
153 
154 }