view src/fschmidt/tools/Jmp.java @ 68:00520880ad02

add fschmidt source
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 05 Oct 2025 17:24:15 -0600
parents
children
line wrap: on
line source

/*
Copyright (c) 2008  Franklin Schmidt <fschmidt@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package fschmidt.tools;

import fschmidt.util.java.IoUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;


public final class Jmp {

	public static void main(String args[]) throws Exception {
		for( int iFile=0; iFile<args.length; iFile++ ) {
			String inFile = args[iFile];
			if( !inFile.endsWith(".jmp") )
				throw new RuntimeException(inFile+" doesn't end with '.jmp'");
			String outFile = inFile.substring(0,inFile.length()-4) + ".java";
			PrintWriter out = new PrintWriter( IoUtils.newFileWriter(outFile) );
			int tabs = 0;
			String jsp = IoUtils.read( new File(inFile) );
			int i = 0;
			boolean plus = false;
			while(true) {
				int i1 = jsp.indexOf("<%",i);
				if( i1 == -1 ) {
					out.close();
					break;
				}
				int i2 = jsp.indexOf("%>",i1);
				if( i2 == -1 ) {
					System.err.println("'<%' not matched with '%>'");
					System.exit(-1);
				}
				if( i1 > i ) {
					out.print("\t\t");
					if( plus )
						out.print("+");
					out.print("\"");
					for( ; i<i1; i++ ) {
						char c = jsp.charAt(i);
						switch(c) {
						case '\r':
							out.print("\\r");
							break;
						case '\n':
							out.print("\\n");
							int j;
							for( j=0; j<tabs && i+j+1<i1 && jsp.charAt(i+j+1)=='\t'; j++ );
							i += j;
							break;
						case '\\':
						case '"':
							out.print('\\');
						default:
							out.print(c);
						}
					}
					out.println("\"");
				}
				i += 2;
				char c = jsp.charAt(i);
				if( c == '=' ) {
					out.print("\t\t+(");
					out.print( jsp.substring(i+1,i2) );
					out.println(")");
					plus = true;
				} else if( c == '+' ) {
					out.print("\t\t+(lang.");
					String methodToCall = jsp.substring(i+1,i2);
					out.print(methodToCall);
					if (!methodToCall.endsWith(")"))
						out.print("()");
					out.println(")");
					plus = true;
				} else {
					out.println( jsp.substring(i,i2) );
					plus = false;
					int tabs2 = 0;
					loop:
					for( int j = i2 - 1; true; j-- ) {
						switch( jsp.charAt(j) ) {
						case '\t':
							tabs2++;
							break;
						case '\n':
							tabs = tabs2;
							break loop;
						default:
							break loop;
						}
					}
				}
				i = i2 + 2;
			}
		}
	}

}