| 
68
 | 
     1 /*
 | 
| 
 | 
     2 Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
| 
 | 
     5 of this software and associated documentation files (the "Software"), to deal
 | 
| 
 | 
     6 in the Software without restriction, including without limitation the rights
 | 
| 
 | 
     7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
| 
 | 
     8 copies of the Software, and to permit persons to whom the Software is
 | 
| 
 | 
     9 furnished to do so, subject to the following conditions:
 | 
| 
 | 
    10 
 | 
| 
 | 
    11 The above copyright notice and this permission notice shall be included in
 | 
| 
 | 
    12 all copies or substantial portions of the Software.
 | 
| 
 | 
    13 
 | 
| 
 | 
    14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
| 
 | 
    15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
| 
 | 
    16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
| 
 | 
    17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
| 
 | 
    18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
| 
 | 
    19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
| 
 | 
    20 THE SOFTWARE.
 | 
| 
 | 
    21 */
 | 
| 
 | 
    22 
 | 
| 
 | 
    23 package fschmidt.tools;
 | 
| 
 | 
    24 
 | 
| 
 | 
    25 import fschmidt.util.java.IoUtils;
 | 
| 
 | 
    26 
 | 
| 
 | 
    27 import java.io.File;
 | 
| 
 | 
    28 import java.io.FileWriter;
 | 
| 
 | 
    29 import java.io.PrintWriter;
 | 
| 
 | 
    30 
 | 
| 
 | 
    31 
 | 
| 
 | 
    32 public final class Jmp {
 | 
| 
 | 
    33 
 | 
| 
 | 
    34 	public static void main(String args[]) throws Exception {
 | 
| 
 | 
    35 		for( int iFile=0; iFile<args.length; iFile++ ) {
 | 
| 
 | 
    36 			String inFile = args[iFile];
 | 
| 
 | 
    37 			if( !inFile.endsWith(".jmp") )
 | 
| 
 | 
    38 				throw new RuntimeException(inFile+" doesn't end with '.jmp'");
 | 
| 
 | 
    39 			String outFile = inFile.substring(0,inFile.length()-4) + ".java";
 | 
| 
 | 
    40 			PrintWriter out = new PrintWriter( IoUtils.newFileWriter(outFile) );
 | 
| 
 | 
    41 			int tabs = 0;
 | 
| 
 | 
    42 			String jsp = IoUtils.read( new File(inFile) );
 | 
| 
 | 
    43 			int i = 0;
 | 
| 
 | 
    44 			boolean plus = false;
 | 
| 
 | 
    45 			while(true) {
 | 
| 
 | 
    46 				int i1 = jsp.indexOf("<%",i);
 | 
| 
 | 
    47 				if( i1 == -1 ) {
 | 
| 
 | 
    48 					out.close();
 | 
| 
 | 
    49 					break;
 | 
| 
 | 
    50 				}
 | 
| 
 | 
    51 				int i2 = jsp.indexOf("%>",i1);
 | 
| 
 | 
    52 				if( i2 == -1 ) {
 | 
| 
 | 
    53 					System.err.println("'<%' not matched with '%>'");
 | 
| 
 | 
    54 					System.exit(-1);
 | 
| 
 | 
    55 				}
 | 
| 
 | 
    56 				if( i1 > i ) {
 | 
| 
 | 
    57 					out.print("\t\t");
 | 
| 
 | 
    58 					if( plus )
 | 
| 
 | 
    59 						out.print("+");
 | 
| 
 | 
    60 					out.print("\"");
 | 
| 
 | 
    61 					for( ; i<i1; i++ ) {
 | 
| 
 | 
    62 						char c = jsp.charAt(i);
 | 
| 
 | 
    63 						switch(c) {
 | 
| 
 | 
    64 						case '\r':
 | 
| 
 | 
    65 							out.print("\\r");
 | 
| 
 | 
    66 							break;
 | 
| 
 | 
    67 						case '\n':
 | 
| 
 | 
    68 							out.print("\\n");
 | 
| 
 | 
    69 							int j;
 | 
| 
 | 
    70 							for( j=0; j<tabs && i+j+1<i1 && jsp.charAt(i+j+1)=='\t'; j++ );
 | 
| 
 | 
    71 							i += j;
 | 
| 
 | 
    72 							break;
 | 
| 
 | 
    73 						case '\\':
 | 
| 
 | 
    74 						case '"':
 | 
| 
 | 
    75 							out.print('\\');
 | 
| 
 | 
    76 						default:
 | 
| 
 | 
    77 							out.print(c);
 | 
| 
 | 
    78 						}
 | 
| 
 | 
    79 					}
 | 
| 
 | 
    80 					out.println("\"");
 | 
| 
 | 
    81 				}
 | 
| 
 | 
    82 				i += 2;
 | 
| 
 | 
    83 				char c = jsp.charAt(i);
 | 
| 
 | 
    84 				if( c == '=' ) {
 | 
| 
 | 
    85 					out.print("\t\t+(");
 | 
| 
 | 
    86 					out.print( jsp.substring(i+1,i2) );
 | 
| 
 | 
    87 					out.println(")");
 | 
| 
 | 
    88 					plus = true;
 | 
| 
 | 
    89 				} else if( c == '+' ) {
 | 
| 
 | 
    90 					out.print("\t\t+(lang.");
 | 
| 
 | 
    91 					String methodToCall = jsp.substring(i+1,i2);
 | 
| 
 | 
    92 					out.print(methodToCall);
 | 
| 
 | 
    93 					if (!methodToCall.endsWith(")"))
 | 
| 
 | 
    94 						out.print("()");
 | 
| 
 | 
    95 					out.println(")");
 | 
| 
 | 
    96 					plus = true;
 | 
| 
 | 
    97 				} else {
 | 
| 
 | 
    98 					out.println( jsp.substring(i,i2) );
 | 
| 
 | 
    99 					plus = false;
 | 
| 
 | 
   100 					int tabs2 = 0;
 | 
| 
 | 
   101 					loop:
 | 
| 
 | 
   102 					for( int j = i2 - 1; true; j-- ) {
 | 
| 
 | 
   103 						switch( jsp.charAt(j) ) {
 | 
| 
 | 
   104 						case '\t':
 | 
| 
 | 
   105 							tabs2++;
 | 
| 
 | 
   106 							break;
 | 
| 
 | 
   107 						case '\n':
 | 
| 
 | 
   108 							tabs = tabs2;
 | 
| 
 | 
   109 							break loop;
 | 
| 
 | 
   110 						default:
 | 
| 
 | 
   111 							break loop;
 | 
| 
 | 
   112 						}
 | 
| 
 | 
   113 					}
 | 
| 
 | 
   114 				}
 | 
| 
 | 
   115 				i = i2 + 2;
 | 
| 
 | 
   116 			}
 | 
| 
 | 
   117 		}
 | 
| 
 | 
   118 	}
 | 
| 
 | 
   119 
 | 
| 
 | 
   120 }
 |