1 /***
2 *
3 * Copyright 2005 Jeremy Rayner
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.groovy.antlr.java;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.File;
22 import java.io.PrintStream;
23 import java.io.StringReader;
24 import java.util.Arrays;
25 import java.util.Iterator;
26 import java.util.List;
27
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.Options;
30 import org.apache.commons.cli.PosixParser;
31 import org.codehaus.groovy.antlr.AntlrASTProcessor;
32 import org.codehaus.groovy.antlr.SourceBuffer;
33 import org.codehaus.groovy.antlr.UnicodeEscapingReader;
34 import org.codehaus.groovy.antlr.java.Java2GroovyConverter;
35 import org.codehaus.groovy.antlr.java.JavaLexer;
36 import org.codehaus.groovy.antlr.java.JavaRecognizer;
37 import org.codehaus.groovy.antlr.parser.GroovyLexer;
38 import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
39 import org.codehaus.groovy.antlr.treewalker.*;
40 import org.codehaus.groovy.runtime.DefaultGroovyMethods;
41
42 import antlr.collections.AST;
43
44 public class Java2GroovyMain {
45
46 public static void main(String[] args) {
47 try{
48 Options options = new Options();
49 PosixParser cliParser = new PosixParser();
50 CommandLine cli = cliParser.parse(options, args);
51 String[] filenames = cli.getArgs();
52 if( filenames.length == 0 ) {
53 System.err.println("Needs at least one filename");
54 }
55 List filenameList = Arrays.asList(filenames);
56 Iterator i = filenameList.iterator();
57 while (i.hasNext()) {
58 String filename = (String) i.next();
59 File f = new File(filename);
60 String text = DefaultGroovyMethods.getText(f);
61 System.out.println(convert(text, true, true));
62 }
63 } catch (Throwable t) {
64 t.printStackTrace();
65 }
66 }
67
68 public static String convert(String input) throws Exception{
69 return convert(input, false, false);
70 }
71
72 public static String convert(String input,boolean withHeader, boolean withNewLines) throws Exception{
73 JavaRecognizer parser = getJavaParser(input);
74 String[] tokenNames = parser.getTokenNames();
75 parser.compilationUnit();
76 AST ast = parser.getAST();
77
78 modifyJavaASTintoGroovyAST(tokenNames, ast);
79 String[] groovyTokenNames = getGroovyTokenNames(input);
80
81 groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
82
83
84 ByteArrayOutputStream baos = new ByteArrayOutputStream();
85 Visitor visitor = new SourcePrinter(new PrintStream(baos),groovyTokenNames, withNewLines);
86 AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
87
88 traverser.process(ast);
89
90 String header = "";
91 if (withHeader) {
92 header = "/*\n" +
93 " Automatically Converted from Java Source \n" +
94 " \n" +
95 " by java2groovy v0.0.1 Copyright Jeremy Rayner 2007\n" +
96 " \n" +
97 " !! NOT FIT FOR ANY PURPOSE !! \n" +
98 " 'java2groovy' cannot be used to convert one working program into another" +
99 " */\n\n";
100 }
101 return header + new String(baos.toByteArray());
102 }
103
104 /***
105 * @param ast
106 * @param groovyTokenNames
107 */
108 private static void groovifyFatJavaLikeGroovyAST(AST ast, String[] groovyTokenNames) {
109 Visitor groovifier = new Groovifier(groovyTokenNames);
110 AntlrASTProcessor groovifierTraverser = new PreOrderTraversal(groovifier);
111 groovifierTraverser.process(ast);
112 }
113
114 /***
115 * @param tokenNames
116 * @param ast
117 */
118 private static void modifyJavaASTintoGroovyAST(String[] tokenNames, AST ast) {
119 Visitor java2groovyConverter = new Java2GroovyConverter(tokenNames);
120 AntlrASTProcessor java2groovyTraverser = new PreOrderTraversal(java2groovyConverter);
121 java2groovyTraverser.process(ast);
122 }
123
124 /***
125 * @param input
126 * @return
127 */
128 private static JavaRecognizer getJavaParser(String input) {
129 JavaRecognizer parser = null;
130 SourceBuffer sourceBuffer = new SourceBuffer();
131 UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new StringReader(input),sourceBuffer);
132 JavaLexer lexer = new JavaLexer(unicodeReader);
133 unicodeReader.setLexer(lexer);
134 parser = JavaRecognizer.make(lexer);
135 parser.setSourceBuffer(sourceBuffer);
136 return parser;
137 }
138
139 public static String mindmap(String input) throws Exception{
140 JavaRecognizer parser = getJavaParser(input);
141 String[] tokenNames = parser.getTokenNames();
142 parser.compilationUnit();
143 AST ast = parser.getAST();
144
145 modifyJavaASTintoGroovyAST(tokenNames, ast);
146 String[] groovyTokenNames = getGroovyTokenNames(input);
147
148 groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
149
150
151 ByteArrayOutputStream baos = new ByteArrayOutputStream();
152 Visitor visitor = new MindMapPrinter(new PrintStream(baos),groovyTokenNames);
153 AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
154
155 traverser.process(ast);
156
157 return new String(baos.toByteArray());
158 }
159
160 public static String nodePrinter(String input) throws Exception{
161 JavaRecognizer parser = getJavaParser(input);
162 String[] tokenNames = parser.getTokenNames();
163 parser.compilationUnit();
164 AST ast = parser.getAST();
165
166 modifyJavaASTintoGroovyAST(tokenNames, ast);
167 String[] groovyTokenNames = getGroovyTokenNames(input);
168
169 groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
170
171
172 ByteArrayOutputStream baos = new ByteArrayOutputStream();
173 Visitor visitor = new NodePrinter(new PrintStream(baos),groovyTokenNames);
174 AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
175
176 traverser.process(ast);
177
178 return new String(baos.toByteArray());
179 }
180
181 private static String[] getGroovyTokenNames(String input) {
182 GroovyRecognizer groovyParser = null;
183 SourceBuffer groovySourceBuffer = new SourceBuffer();
184 UnicodeEscapingReader groovyUnicodeReader = new UnicodeEscapingReader(new StringReader(input),groovySourceBuffer);
185 GroovyLexer groovyLexer = new GroovyLexer(groovyUnicodeReader);
186 groovyUnicodeReader.setLexer(groovyLexer);
187 groovyParser = GroovyRecognizer.make(groovyLexer);
188 return groovyParser.getTokenNames();
189 }
190
191 }