Source code: com/yaftp/ftp/MvsJobsPrettyPrinter.java
1 /**
2 *
3 * CopyRights Jean-Yves MENGANT 1999,2000,2001,2002
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.yaftp.ftp ;
21
22 import java.io.* ;
23 import java.util.* ;
24
25 /**
26
27 this class is an Html pretty printer for MVS Jobs SYSOUT
28 it transforms sandard MVS job sysout into an HTML decorated
29 page which can be viewed by standard browsers
30
31 */
32 public class MvsJobsPrettyPrinter {
33
34 private final static int _END_OF_STREAM_ = -1 ;
35 private final static int _JES2_JOB_LOG_ = 1 ;
36 private final static int _JCL_STREAM_ = 2 ;
37 private final static int _IEF_STREAM_ = 3 ;
38 private final static int _SYSOUT_STREAM_ = 4 ;
39
40
41 private final static String _END_OF_JES_SPOOL_FILE_ = "!! END OF JES SPOOL" ;
42 private final static String _COND_CODE_ = "COND CODE" ;
43
44 private final static String _NEW_LINE_ = "<BR>\n" ;
45 private final static String _BOLD_START_ = "<strong>" ;
46 private final static String _BOLD_END_ = "</strong>" ;
47 private final static String _H1_START_ = "<h1>" ;
48 private final static String _H1_END_ = "</h1>" ;
49 private final static String _H2_START_ = "<h2>" ;
50 private final static String _H2_END_ = "</h2>" ;
51
52 private final static String _REPORT_HEADER_ = "ReportHeader" ;
53 private final static String _DEFAULT_REPORT_HEADER_ = "<center><h1>MVS job report</h1></center>" ;
54
55 private final static String _JOBLOG_HEADER_ = "JobLogHeader" ;
56 private final static String _DEFAULT_JOBLOG_HEADER_ = "<h2>JES2 JOB LOG</h2>" ;
57
58 private final static String _JCLSOURCE_HEADER_ = "JclSourceHeader" ;
59 private final static String _DEFAULT_JCLSOURCE_HEADER_ = "<h2>JCL source and substitutions</h2>" ;
60
61 private final static String _IEF_HEADER_ = "IefHeader" ;
62 private final static String _DEFAULT_IEF_HEADER_ = "<h2>STEPS completion reporting</h2>" ;
63
64 private final static String _SYSOUT_HEADER_ = "SysoutHeader" ;
65 private final static String _DEFAULT_SYSOUT_HEADER_ = "<h2>JOB SYSOUT and SYSPRINT</h2>" ;
66
67 private final static String _IEFFONT_ = "IefFont" ;
68 private final static String _DEFAULT_IEFFONT_START_ = "<font color=navy size=-1>" ;
69
70 private final static String _JCLFONT_ = "JclFont" ;
71 private final static String _DEFAULT_JCLFONT_START_ = "<font color=blue size=-1>" ;
72
73 private final static String _JOBLOGFONT_ = "JobLogFont" ;
74 private final static String _DEFAULT_JOBLOG_START_ = "<font color=gray size=-1>" ;
75
76
77 private final static String _GOODCODE_FONT_ = "GoodCodeFont" ;
78 private final static String _DEFAULT_GOODCODE_START_ = "<font color=gray >" ;
79
80 private final static String _BADCODE_FONT_ = "BadCodeFont" ;
81 private final static String _DEFAULT_BADCODE_START_ = "<font color=red >" ;
82
83 private final static String _SYSOUTFONT_ = "SysoutFont" ;
84 private final static String _DEFAULT_SYSOUT_START_ = "<font color=teal size=-1>" ;
85
86 private final static String _FONT_LARGER_ = "<font size=+1>" ;
87 private final static String _FONT_SMALLER_ = "<font size=-1>" ;
88
89 private final static String _FONT_END_ = "</font>";
90
91 private boolean _asaCode = true ;
92 private boolean _newPage = false ;
93 private boolean _newLineAsa = false ;
94 private boolean _boldLineAsa = false ;
95 private boolean _sectionStart = true ;
96
97
98 private int _state = _JES2_JOB_LOG_ ;
99 private BufferedReader _stream ;
100
101 private Properties _htmlLayoutProperties = new Properties() ;
102
103 /**
104 try to load html format property , if null return the internal
105 provided default value
106 */
107 private String loadHtmlFormat( String name , String defaultValue )
108 {
109 return _htmlLayoutProperties.getProperty(name,defaultValue) ;
110 }
111
112 private String nextLine()
113 throws ClientFtpError
114 {
115 try {
116 String returned = _stream.readLine() ;
117 if ( ( returned != null ) && ( returned.length() > 0 ) )
118 {
119 if ( _asaCode )
120 {
121 char curAsa = returned.charAt(0) ;
122 _newPage = false ;
123 _newLineAsa = false ;
124 _boldLineAsa = false ;
125 returned = returned.substring(1) ;
126 switch ( curAsa )
127 {
128 case '-' :
129 _newLineAsa = true ;
130 break ;
131 case '0' :
132 case '1' :
133 _newPage = true ;
134 break ;
135 case '+' :
136 _boldLineAsa = true ;
137 break ;
138 }
139 }
140 }
141 else
142 _state = _END_OF_STREAM_ ;
143 return returned ;
144 }
145 catch ( IOException e )
146 { throw new ClientFtpError("IOError reading job Lines : "+e.getMessage() ) ; }
147 }
148
149 private boolean sectionChange( String curLine )
150 {
151 if ( curLine.indexOf(_END_OF_JES_SPOOL_FILE_) != -1 )
152 {
153 _state++ ;
154 _sectionStart = true ;
155 return true ;
156 }
157 return false ;
158 }
159
160 private void condCode(String curLine , StringBuffer production )
161 throws ClientFtpError
162 {
163 int pos = curLine.indexOf(_COND_CODE_) ;
164 String start = curLine.substring(0, pos) ;
165 production.append(start) ;
166 String end = curLine.substring(pos) ;
167 String value = curLine.substring(pos+_COND_CODE_.length()).trim() ;
168 try {
169 int code = Integer.parseInt(value) ;
170 if ( code != 0 )
171 {
172 production.append(loadHtmlFormat( _BADCODE_FONT_ , _DEFAULT_BADCODE_START_) ) ;
173 production.append(end) ;
174 production.append(_FONT_END_) ;
175 }
176 else
177 {
178 production.append(loadHtmlFormat( _GOODCODE_FONT_ , _DEFAULT_GOODCODE_START_) ) ;
179 production.append(end) ;
180 production.append(_FONT_END_) ;
181 }
182 } catch( NumberFormatException e )
183 { throw new ClientFtpError ("invalid condition code : " + value ) ; }
184
185 }
186
187 private void generateJobLog(String curLine , StringBuffer production )
188 {
189 if ( _sectionStart )
190 {
191 production.append(loadHtmlFormat( _JOBLOG_HEADER_ , _DEFAULT_JOBLOG_HEADER_) ) ;
192 production.append(loadHtmlFormat( _JOBLOGFONT_ , _DEFAULT_JOBLOG_START_) ) ;
193 _sectionStart = false ;
194 }
195 production.append(curLine) ;
196 }
197
198 private void generateJclStream(String curLine , StringBuffer production )
199 {
200 if ( _sectionStart )
201 {
202 production.append( _FONT_END_ ) ;
203 production.append(loadHtmlFormat( _JCLSOURCE_HEADER_ , _DEFAULT_JCLSOURCE_HEADER_) ) ;
204 production.append(loadHtmlFormat( _JCLFONT_ , _DEFAULT_JCLFONT_START_) ) ;
205 _sectionStart = false ;
206 }
207 production.append(curLine) ;
208 }
209
210 private void generateIefStream(String curLine , StringBuffer production )
211 throws ClientFtpError
212 {
213 if ( _sectionStart )
214 {
215 production.append( _FONT_END_ ) ;
216 production.append(loadHtmlFormat( _IEF_HEADER_ , _DEFAULT_IEF_HEADER_ )) ;
217 production.append(loadHtmlFormat( _IEFFONT_ , _DEFAULT_IEFFONT_START_) ) ;
218 _sectionStart = false ;
219 }
220 if ( curLine.indexOf("IEF142I") != -1 )
221 {
222 //production.append(_FONT_LARGER_) ;
223 production.append(_BOLD_START_) ;
224 condCode(curLine,production) ;
225 production.append(_BOLD_END_) ;
226 //production.append(_FONT_END_) ;
227 }
228 else
229 production.append(curLine) ;
230 }
231
232 private void generateSysoutStream(String curLine , StringBuffer production )
233 {
234 if ( _sectionStart )
235 {
236 production.append( _FONT_END_ ) ;
237 production.append(loadHtmlFormat( _SYSOUT_HEADER_ , _DEFAULT_SYSOUT_HEADER_ )) ;
238 production.append(loadHtmlFormat( _SYSOUTFONT_ , _DEFAULT_SYSOUT_START_ )) ;
239 _sectionStart = false ;
240 }
241 production.append(curLine) ;
242 }
243
244
245 private void checkState( String curLine , StringBuffer production )
246 throws ClientFtpError
247 {
248 if ( sectionChange(curLine) )
249 curLine = nextLine() ;
250
251 switch ( _state )
252 {
253 case _JES2_JOB_LOG_ :
254 generateJobLog( curLine , production ) ;
255 break ;
256 case _JCL_STREAM_ :
257 generateJclStream( curLine , production ) ;
258 break ;
259 case _IEF_STREAM_ :
260 generateIefStream( curLine , production ) ;
261 break ;
262 case _SYSOUT_STREAM_ :
263 generateSysoutStream( curLine , production ) ;
264 break ;
265 }
266 }
267
268 private void parseNextLine( StringBuffer production )
269 throws ClientFtpError
270 {
271 String curLine = nextLine() ;
272
273 if ( curLine != null )
274 {
275 if ( _boldLineAsa )
276 production.append(_BOLD_START_) ;
277 if ( _newLineAsa )
278 production.append(_NEW_LINE_) ;
279
280 checkState( curLine , production ) ;
281
282 if ( _boldLineAsa )
283 production.append(_BOLD_END_) ;
284 production.append(_NEW_LINE_) ;
285 }
286 }
287
288 /**
289 if asaCode is set to true an ASCODE caracter will be interpreted
290 in position 1 of the listing
291 */
292 public void set_asaCode( boolean asaCode )
293 {
294 _asaCode = asaCode ;
295 }
296
297 public MvsJobsPrettyPrinter( BufferedReader in )
298 throws ClientFtpError
299 {
300 _stream = in ;
301 }
302
303 /**
304 load the property file used to override deffault HTML
305 prettyprinter presentation rules
306 */
307 public void loadHtmlProperties( String fName )
308 throws ClientFtpError
309 {
310 try {
311 _htmlLayoutProperties.load( new FileInputStream(fName) );
312 } catch ( IOException e )
313 { throw new ClientFtpError("fail to load HtmlProperty file "+fName+" : "+
314 e.getMessage()
315 ) ;
316 }
317 }
318
319 /**
320 proceed with prettyPrint process
321
322 @return a BufferedReader HTML ready to browse String buffer
323 */
324 public String prettyPrint()
325 throws ClientFtpError
326 {
327 StringBuffer returned = new StringBuffer() ;
328 returned.append(loadHtmlFormat( _REPORT_HEADER_ , _DEFAULT_REPORT_HEADER_ )) ;
329
330 while ( _state != _END_OF_STREAM_ )
331 parseNextLine( returned ) ;
332
333 return returned.toString() ;
334 }
335
336 public static void main( String args[] )
337 {
338 System.out.println( "testing MvsJobPrettyPrinter") ;
339 try
340 {
341 BufferedReader in = new BufferedReader( new FileReader(args[0]) ) ;
342 MvsJobsPrettyPrinter prettyJobPrinter = new MvsJobsPrettyPrinter(in) ;
343
344 if ( args.length > 1 )
345 prettyJobPrinter.loadHtmlProperties(args[1]);
346
347 long elapse = System.currentTimeMillis() ;
348 String result = prettyJobPrinter.prettyPrint() ;
349 elapse = System.currentTimeMillis() - elapse ;
350 System.out.println("elapse:"+elapse+ " milliseconds") ;
351 System.out.println(result) ;
352 try {
353 BufferedWriter out = new BufferedWriter( new FileWriter(args[0]+".html")) ;
354 out.write(result);
355 out.close() ;
356 } catch( IOException e )
357 { System.out.println(args[0]+".html : " + e.getMessage() ) ; }
358 }
359 catch ( FileNotFoundException e )
360 { System.out.println(args[0] + " inexisting file " ) ; }
361
362 catch ( ClientFtpError e )
363 { System.out.println( e.getMessage() ) ; }
364 }
365
366 }