Mercurial Hosting > nabble
comparison 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 |
comparison
equal
deleted
inserted
replaced
67:9d0fefce6985 | 68:00520880ad02 |
---|---|
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 java.io.PrintWriter; | |
26 import java.io.File; | |
27 import java.io.FileWriter; | |
28 import java.io.CharArrayWriter; | |
29 import fschmidt.util.java.IoUtils; | |
30 | |
31 | |
32 public final class Jtp { | |
33 public static class JtpException extends Exception { | |
34 JtpException(String msg) { | |
35 super(msg); | |
36 } | |
37 } | |
38 | |
39 public static void main(String args[]) throws Exception { | |
40 try { | |
41 for( int iFile=0; iFile<args.length; iFile++ ) { | |
42 String inFile = args[iFile]; | |
43 if( !inFile.endsWith(".jtp") ) | |
44 throw new RuntimeException(inFile+" doesn't end with '.jtp'"); | |
45 String outFile = inFile.substring(0,inFile.length()-4) + ".java"; | |
46 String jtp = IoUtils.read( new File(inFile) ); | |
47 IoUtils.write( new File(outFile), toJava(jtp) ); | |
48 } | |
49 } catch(JtpException e) { | |
50 System.err.println(e.getMessage()); | |
51 System.exit(-1); | |
52 } | |
53 } | |
54 | |
55 public static String toJava(String jtp) | |
56 throws JtpException | |
57 { | |
58 if( jtp==null ) | |
59 throw new JtpException("null argument"); | |
60 CharArrayWriter out0 = new CharArrayWriter(); | |
61 PrintWriter out = new PrintWriter(out0); | |
62 int tabs = 0; | |
63 int i = 0; | |
64 while(true) { | |
65 int i1 = jtp.indexOf("<%",i); | |
66 boolean done = i1 == -1; | |
67 if( i1 > i || done && jtp.substring(i).trim().length() > 0 ) { | |
68 if( done ) | |
69 i1 = jtp.length(); | |
70 out.print("\t\tout.print( \""); | |
71 for( ; i<i1; i++ ) { | |
72 char c = jtp.charAt(i); | |
73 switch(c) { | |
74 case '\r': | |
75 out.print("\\r"); | |
76 break; | |
77 case '\n': | |
78 out.print("\\n"); | |
79 int j; | |
80 for( j=0; j<tabs && i+j+1<i1 && jtp.charAt(i+j+1)=='\t'; j++ ); | |
81 i += j; | |
82 break; | |
83 case '\\': | |
84 case '"': | |
85 out.print('\\'); | |
86 default: | |
87 out.print(c); | |
88 } | |
89 } | |
90 out.println("\" );"); | |
91 } | |
92 if( done ) { | |
93 out.flush(); | |
94 return out0.toString(); | |
95 } | |
96 int i2 = jtp.indexOf("%>",i1); | |
97 if( i2 == -1 ) { | |
98 throw new JtpException("'<%' not matched with '%>'"); | |
99 } | |
100 i += 2; | |
101 char c = jtp.charAt(i); | |
102 if( c == '=' ) { | |
103 out.print("\t\tout.print( ("); | |
104 out.print( jtp.substring(i+1,i2) ); | |
105 out.println(") );"); | |
106 } else if( c == '+' ) { | |
107 out.print("\t\tout.print( (lang."); | |
108 String methodToCall = jtp.substring(i+1,i2); | |
109 out.print(methodToCall); | |
110 if (!methodToCall.endsWith(")")) | |
111 out.print("()"); | |
112 out.println(") );"); | |
113 } else { | |
114 out.println( jtp.substring(i,i2) ); | |
115 int tabs2 = 0; | |
116 loop: | |
117 for( int j = i2 - 1; true; j-- ) { | |
118 switch( jtp.charAt(j) ) { | |
119 case '\t': | |
120 tabs2++; | |
121 break; | |
122 case '\n': | |
123 tabs = tabs2; | |
124 break loop; | |
125 default: | |
126 break loop; | |
127 } | |
128 } | |
129 } | |
130 i = i2 + 2; | |
131 } | |
132 } | |
133 | |
134 } |