Source Code Cross Referenced for Java2GroovyMain.java in  » Scripting » groovy-1.0 » org » codehaus » groovy » antlr » java » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Scripting » groovy 1.0 » org.codehaus.groovy.antlr.java 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.