How can I convert my 0.9 JSP files to 1.1?

Terence Parr

Converting your JSP 0.9 to 1.1 is a straightforward syntax translation for the majority of your files--you won't have to change the semantics. I built a script, updatejsp, to take care of translating 95% of my files; the others, I converted by hand since it was easier/faster than making a perfect script.

Given the one-to-one nature of the translation, sed/awk (could've used perl) are sufficient--full parser generators, such as ANTLR are overkill. On the other hand sed/awk don't deal well with constructs that cross newline boundaries. Oh well.

What translations does updatejsp perform? The script translates includes, imports, <SCRIPT> tags, and <BEAN> tags:

JSP 0.9 JSP 1.1
<!--#INCLUDE FILE="include/stdhdr.inc"-->
<%@ include file="include/stdhdr.inc" %>
<%@ import="javax.servlet.http.*" %>
<%@ import="java.util.*" %>
<%@ import="java.net.*" %>
<%@ import="java.io.*" %>
<%@ page import="
        javax.servlet.http.*,
        java.util.*,
        java.net.*,
        java.io.*,
"%>
<SCRIPT runat="server">
interface Verbosity {
        final int TERSE = 1;
        final int NORMAL = 2;
        final int VERBOSE = 3;
}
</SCRIPT>
<%!
interface Verbosity {
        final int TERSE = 1;
        final int NORMAL = 2;
        final int VERBOSE = 3;
}
%>

Here is the start of an 0.9 JSP file with a <BEAN> tag I have broken the long line for formatting reasons here, but in "reality" the <BEAN> tag is on a single line:
<BEAN name="view" type="com.epicentric.portalbeans.PortalBeanView" introspect="no" create="no" 
 scope="request" >
</BEAN>

<%@ import="java.util.*" %>
<%@ import="java.net.*" %>
<%@ import="java.io.*" %>

<!--#INCLUDE FILE="include/PortalEntityClient.inc"-->


<SCRIPT runat="server">
static public int sloganIndex = 0;
</SCRIPT>

The updatejsp script translates it to the following:

<%@ page import="
        java.util.*,
        java.net.*,
        java.io.*,
"%>
<jsp:useBean id="view" type="com.epicentric.portalbeans.PortalBeanView" scope="request">
<jsp:useBean>

<%@ include file="include/PortalEntityClient.inc" %>

<%!
static public int sloganIndex = 0;
%>

What does updatejsp not do? The script doesn't deal with random whitespace (for example, it likes a space before the 0.9 import word) nor does it deal with tags broken across lines. If you have large whitespace variation in your JSP files, I would suggest fixing this script. If you have lots of tags broken across lines, I'd write a real parser with ANTLR that sees a stream of token objects not characters (making it not care about newlines and weird whitespace).

Here is the code you'll need. The updatejsp script is written in bash and you'll need awk for genimports.awk, which is presumed to live in the same dir as the updatejsp script. "Your mileage may vary." :)

updatejsp:

#!/bin/bash
#
# Convert bean, include, SCRIPT, and import syntax to JSP 1.1
# Handles modules and regular JSP files. Does not do a good
# job with the <BEAN...> tag.  Assumes all on one list for example.
#
# Terence Parr, jGuru.com
#

# F is the file we're dealing with
F=$1

#
# compute location of genimport.awk; assume same dir as executable
# $0 is full pathname to this executable script
# I hack off the path (which assumes at least "./" as path) and
# then append "/genimport.awk" to get that script
#
UPDATEJSP=$0
GENIMPORT="`expr $UPDATEJSP : '(.*)/.*'`/genimport.awk"

# make sure a filename is specified
if !(test $1)
then
        echo "Usage: updatejsp filename"
        echo "Example: updatejsp view.jsp"
        exit
fi

# make sure a filename is writable
if !(test -w $1)
then
        echo "File $1 is not writable"
        exit
fi

# make sure the file is indeed an old style file
if !(grep -s -q '%@ import' $F || 
     grep -s -q '#INCLUDE' $F || 
     grep -s -q '<SCRIPT' $F || 
     grep -s -q '<BEAN' $F)
then
        echo "Sorry, but $F doesn't look like an old-style JSP file to me"
	echo "I couldn't find '<%@ import', '<BEAN...>', "<SCRIPT", or '#INCLUDE'"
        exit
fi

# convert includes to new form and change "portal/" to "/portal/" and for jguru
sed 's/<!--#INCLUDE FILE="(.*)"-->/<%@ include file="1" %>/g' < $F 
  > /tmp/updatejsp.tmp

# convert BEAN to jsp:useBean and /BEAN to /jsp:useBean
# this does NOT work when <BEAN...> is on more than one line
# The next line is broken up for formatting purposes
# but the  should be removed and the associated newline
# when you try to run the script
sed 's/<BEAN.*>/<jsp:useBean id="view" type="com.epicentric.portalbeans.PortalBeanView" 
 scope="request">/' < /tmp/updatejsp.tmp | 
sed 's/</BEAN>/</jsp:useBean>/' > /tmp/updatejsp.tmp2

# print out the combined import as page directive
if ( grep -s -q '%@ import' $F )
then
    awk -f $GENIMPORT < /tmp/updatejsp.tmp2 > /tmp/updatejsp.tmpF
fi

# convert SCRIPT to <%! block
sed 's/<SCRIPT.*"server">/<%!/' < /tmp/updatejsp.tmp2 | 
sed 's/</SCRIPT>/%>/' > /tmp/updatejsp.tmp3

# print file minus old import lines
sed '/<%@ import=".*" %>/D' < /tmp/updatejsp.tmp3 >> /tmp/updatejsp.tmpF

# back up old file
mv $F $F.bak

# mv in the new file
mv /tmp/updatejsp.tmpF $F

genimports.awk:

			#
			# Walk an old jsp file, grabbing import names.
			# Then generate a new style import statement
			#
			# Terence Parr, jGuru.com
			#
BEGIN			{n=0;}
/<%@ import=".*" %>/	{a=index($0, "=""); b=index($0, "" "); #print substr($0, a+2, b-a-2);
			 files[n++] = substr($0, a+2, b-a-2);
			 next;
			}
END			{ print "<%@ page import="";
			  for (i=0; i<n; i++) {
			    if ( i<n-1 ) sep=",";
                            else sep="";
                            printf "	%s%s
", files[i], sep;
                          }
			  print ""%>";
			}
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact