diff src/fschmidt/tools/Jtp.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 diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fschmidt/tools/Jtp.java	Sun Oct 05 17:24:15 2025 -0600
@@ -0,0 +1,134 @@
+/*
+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 java.io.PrintWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.CharArrayWriter;
+import fschmidt.util.java.IoUtils;
+
+
+public final class Jtp {
+	public static class JtpException extends Exception {
+		JtpException(String msg) {
+			super(msg);
+		}
+	}
+
+	public static void main(String args[]) throws Exception {
+		try {
+			for( int iFile=0; iFile<args.length; iFile++ ) {
+				String inFile = args[iFile];
+				if( !inFile.endsWith(".jtp") )
+					throw new RuntimeException(inFile+" doesn't end with '.jtp'");
+				String outFile = inFile.substring(0,inFile.length()-4) + ".java";
+				String jtp = IoUtils.read( new File(inFile) );
+				IoUtils.write( new File(outFile), toJava(jtp) );
+			}
+		} catch(JtpException e) {
+			System.err.println(e.getMessage());
+			System.exit(-1);
+		}
+	}
+
+	public static String toJava(String jtp)
+		throws JtpException
+	{
+		if( jtp==null )
+			throw new JtpException("null argument");
+		CharArrayWriter out0 = new CharArrayWriter();
+		PrintWriter out = new PrintWriter(out0);
+		int tabs = 0;
+		int i = 0;
+		while(true) {
+			int i1 = jtp.indexOf("<%",i);
+			boolean done = i1 == -1;
+			if( i1 > i || done && jtp.substring(i).trim().length() > 0 ) {
+				if( done )
+					i1 = jtp.length();
+				out.print("\t\tout.print( \"");
+				for( ; i<i1; i++ ) {
+					char c = jtp.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 && jtp.charAt(i+j+1)=='\t'; j++ );
+						i += j;
+						break;
+					case '\\':
+					case '"':
+						out.print('\\');
+					default:
+						out.print(c);
+					}
+				}
+				out.println("\" );");
+			}
+			if( done ) {
+				out.flush();
+				return out0.toString();
+			}
+			int i2 = jtp.indexOf("%>",i1);
+			if( i2 == -1 ) {
+				throw new JtpException("'<%' not matched with '%>'");
+			}
+			i += 2;
+			char c = jtp.charAt(i);
+			if( c == '=' ) {
+				out.print("\t\tout.print( (");
+				out.print( jtp.substring(i+1,i2) );
+				out.println(") );");
+			} else if( c == '+' ) {
+				out.print("\t\tout.print( (lang.");
+				String methodToCall = jtp.substring(i+1,i2);
+				out.print(methodToCall);
+				if (!methodToCall.endsWith(")"))
+					out.print("()");
+				out.println(") );");
+			} else {
+				out.println( jtp.substring(i,i2) );
+				int tabs2 = 0;
+				loop:
+				for( int j = i2 - 1; true; j-- ) {
+					switch( jtp.charAt(j) ) {
+					case '\t':
+						tabs2++;
+						break;
+					case '\n':
+						tabs = tabs2;
+						break loop;
+					default:
+						break loop;
+					}
+				}
+			}
+			i = i2 + 2;
+		}
+	}
+
+}