001: /**
002: *
003: * Copyright 2005 Jeremy Rayner
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: **/package org.codehaus.groovy.antlr.java;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.File;
021: import java.io.PrintStream;
022: import java.io.StringReader;
023: import java.util.Arrays;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import org.apache.commons.cli.CommandLine;
028: import org.apache.commons.cli.Options;
029: import org.apache.commons.cli.PosixParser;
030: import org.codehaus.groovy.antlr.AntlrASTProcessor;
031: import org.codehaus.groovy.antlr.SourceBuffer;
032: import org.codehaus.groovy.antlr.UnicodeEscapingReader;
033: import org.codehaus.groovy.antlr.java.Java2GroovyConverter;
034: import org.codehaus.groovy.antlr.java.JavaLexer;
035: import org.codehaus.groovy.antlr.java.JavaRecognizer;
036: import org.codehaus.groovy.antlr.parser.GroovyLexer;
037: import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
038: import org.codehaus.groovy.antlr.treewalker.*;
039: import org.codehaus.groovy.runtime.DefaultGroovyMethods;
040:
041: import antlr.collections.AST;
042:
043: public class Java2GroovyMain {
044:
045: public static void main(String[] args) {
046: try {
047: Options options = new Options();
048: PosixParser cliParser = new PosixParser();
049: CommandLine cli = cliParser.parse(options, args);
050: String[] filenames = cli.getArgs();
051: if (filenames.length == 0) {
052: System.err.println("Needs at least one filename");
053: }
054: List filenameList = Arrays.asList(filenames);
055: Iterator i = filenameList.iterator();
056: while (i.hasNext()) {
057: String filename = (String) i.next();
058: File f = new File(filename);
059: String text = DefaultGroovyMethods.getText(f);
060: System.out.println(convert(text, true, true));
061: }
062: } catch (Throwable t) {
063: t.printStackTrace();
064: }
065: }
066:
067: public static String convert(String input) throws Exception {
068: return convert(input, false, false);
069: }
070:
071: public static String convert(String input, boolean withHeader,
072: boolean withNewLines) throws Exception {
073: JavaRecognizer parser = getJavaParser(input);
074: String[] tokenNames = parser.getTokenNames();
075: parser.compilationUnit();
076: AST ast = parser.getAST();
077: // modify the Java AST into a Groovy AST
078: modifyJavaASTintoGroovyAST(tokenNames, ast);
079: String[] groovyTokenNames = getGroovyTokenNames(input);
080: // groovify the fat Java-Like Groovy AST
081: groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
082:
083: // now output
084: ByteArrayOutputStream baos = new ByteArrayOutputStream();
085: Visitor visitor = new SourcePrinter(new PrintStream(baos),
086: groovyTokenNames, withNewLines);
087: AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
088:
089: traverser.process(ast);
090:
091: String header = "";
092: if (withHeader) {
093: header = "/*\n"
094: + " Automatically Converted from Java Source \n"
095: + " \n"
096: + " by java2groovy v0.0.1 Copyright Jeremy Rayner 2007\n"
097: + " \n"
098: + " !! NOT FIT FOR ANY PURPOSE !! \n"
099: + " 'java2groovy' cannot be used to convert one working program into another"
100: + " */\n\n";
101: }
102: return header + new String(baos.toByteArray());
103: }
104:
105: /**
106: * @param ast
107: * @param groovyTokenNames
108: */
109: private static void groovifyFatJavaLikeGroovyAST(AST ast,
110: String[] groovyTokenNames) {
111: Visitor groovifier = new Groovifier(groovyTokenNames);
112: AntlrASTProcessor groovifierTraverser = new PreOrderTraversal(
113: groovifier);
114: groovifierTraverser.process(ast);
115: }
116:
117: /**
118: * @param tokenNames
119: * @param ast
120: */
121: private static void modifyJavaASTintoGroovyAST(String[] tokenNames,
122: AST ast) {
123: Visitor java2groovyConverter = new Java2GroovyConverter(
124: tokenNames);
125: AntlrASTProcessor java2groovyTraverser = new PreOrderTraversal(
126: java2groovyConverter);
127: java2groovyTraverser.process(ast);
128: }
129:
130: /**
131: * @param input
132: * @return
133: */
134: private static JavaRecognizer getJavaParser(String input) {
135: JavaRecognizer parser = null;
136: SourceBuffer sourceBuffer = new SourceBuffer();
137: UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(
138: new StringReader(input), sourceBuffer);
139: JavaLexer lexer = new JavaLexer(unicodeReader);
140: unicodeReader.setLexer(lexer);
141: parser = JavaRecognizer.make(lexer);
142: parser.setSourceBuffer(sourceBuffer);
143: return parser;
144: }
145:
146: public static String mindmap(String input) throws Exception {
147: JavaRecognizer parser = getJavaParser(input);
148: String[] tokenNames = parser.getTokenNames();
149: parser.compilationUnit();
150: AST ast = parser.getAST();
151: // modify the Java AST into a Groovy AST
152: modifyJavaASTintoGroovyAST(tokenNames, ast);
153: String[] groovyTokenNames = getGroovyTokenNames(input);
154: // groovify the fat Java-Like Groovy AST
155: groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
156:
157: // now output
158: ByteArrayOutputStream baos = new ByteArrayOutputStream();
159: Visitor visitor = new MindMapPrinter(new PrintStream(baos),
160: groovyTokenNames);
161: AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
162:
163: traverser.process(ast);
164:
165: return new String(baos.toByteArray());
166: }
167:
168: public static String nodePrinter(String input) throws Exception {
169: JavaRecognizer parser = getJavaParser(input);
170: String[] tokenNames = parser.getTokenNames();
171: parser.compilationUnit();
172: AST ast = parser.getAST();
173: // modify the Java AST into a Groovy AST
174: modifyJavaASTintoGroovyAST(tokenNames, ast);
175: String[] groovyTokenNames = getGroovyTokenNames(input);
176: // groovify the fat Java-Like Groovy AST
177: groovifyFatJavaLikeGroovyAST(ast, groovyTokenNames);
178:
179: // now output
180: ByteArrayOutputStream baos = new ByteArrayOutputStream();
181: Visitor visitor = new NodePrinter(new PrintStream(baos),
182: groovyTokenNames);
183: AntlrASTProcessor traverser = new SourceCodeTraversal(visitor);
184:
185: traverser.process(ast);
186:
187: return new String(baos.toByteArray());
188: }
189:
190: private static String[] getGroovyTokenNames(String input) {
191: GroovyRecognizer groovyParser = null;
192: SourceBuffer groovySourceBuffer = new SourceBuffer();
193: UnicodeEscapingReader groovyUnicodeReader = new UnicodeEscapingReader(
194: new StringReader(input), groovySourceBuffer);
195: GroovyLexer groovyLexer = new GroovyLexer(groovyUnicodeReader);
196: groovyUnicodeReader.setLexer(groovyLexer);
197: groovyParser = GroovyRecognizer.make(groovyLexer);
198: return groovyParser.getTokenNames();
199: }
200:
201: }
|