#!/usr/local/bin/perl # # Java Bean Builder for jsp form processing. # Takes a html form, and spits out java ready to be compiled for use as a generic Bean for the html form. # # Usage: # ./beanbuilder.pl [] [.java] # # The options in brackets are optional. # * If the bean name is excluded, it will ask for one # * If the source is not defined it will print to screen (STDOUT) # # Written by Damien Mascord # # print the usage information unless ($ARGV[0]){ print "Usage:\n$0 [] [.java]\n"; exit; } # open the html source file and read in all the lines with input type on them... open FILE, "<$ARGV[0]"; while(){ if ($_=~/(?:input.*type|select)/i){ $inputs[$x++]=$_; } } close FILE; # go through these input type lines and grab the name of the type... foreach $input (@inputs){ if($input=~/name=/i){ chomp $input; # grab the appropriate name for the input type $input=~s/.*name=\"([^\"]*)\".*/$1/mgi; # change all - to _ so that it works for JSP $input=~s/-/_/g; # change the first character to uppercase for JSP $input=ucfirst $input; # and dump it in a hash table for creating the source. $values{$input}=1; } } # if there is a second argument... then thats the bean name... if not... ask for the bean name... if ($ARGV[1]){ $bean_name=$ARGV[1]; # an idea... but not implemented... #$OUTPUT_FILENAME="$bean_name.java"; }else{ print STDERR "What would you like to call the bean? "; chomp($bean_name=); } # if there is a third argument then they must want an output file if ($ARGV[2]){ # same as previous section... idea but not implemented... #$OUTPUT_FILENAME=$ARGV[2]; open OUTFILE, ">$ARGV[2]"; $OUTPUT=OUTFILE; }else{ $OUTPUT=STDOUT; } #now onto creating the .java # goes through the names and creates a pretty standard bean which can do text based proccessing... and other functions that are needed can easily be added to the source code produced. print $OUTPUT "public class $bean_name {\n"; foreach (keys %values) { print $OUTPUT " private String $_;\n"; } print $OUTPUT "\n public void $bean_name() {\n"; foreach (keys %values) { print $OUTPUT " $_ = null;\n"; } print $OUTPUT " }\n\n"; foreach (keys %values) { print $OUTPUT <