Search code examples
javascriptjavajspstrutsstruts-1

Is there an easy way to add the struts 1.3 html styleId attribute without touching every element?


I am currently working with legacy code to attempt to get it to work correctly in newer browsers. The code is written with Struts 1.3 and makes use of the html tag library extensively in the following manner:

<html:text property="myTextInput" maxlength="10"/>

Which produces the following html when rendered:

<input name="myTextInput" type="text" maxlength="10" value="">

In old versions of IE, one could use document.getElementById('myTextInput') to get a reference even if the element only had a name attribute and didn't have an id attribute. When using the jsp html tags, the name property generates the name attribute in the html code but doesn't generate the id attribute.

I found adding styleId to the html tag in the jsp does add the id attribute to the resulting xml, but this means I would have to touch every single html tag element in all the jsp's and change it similar to:

<html:text property="myTextInput" styleId="myTextInput" maxlength="10"/>

I also found document.getElementByName(), but this results in touching a lot of javascript and also (due to bad code), I don't know if it really is referring to an element by the id or name so this could cause some issues.

Is there an easy way to add the styleId attribute without touching every element?


Solution

  • I ended up writing a small java main method to deal with this. I use regex to find the html elements (select,option. text, hidden, textarea) that don't already have a styleId attribute and then add the styleId attribute with the same value as the property attribute. This could be expanded to do a bunch of files at once but right now I just wanted something to do individual files so I could easily check them against source control and make sure it worked correctly. It's a quick and dirty solution to a problem so I wouldn't have to comb through tons of jsp files manually so I'm sure there are some edge cases it doesn't deal with. With that said:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.channels.FileChannel;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    public class JspModifierStyleId {
    
        public static void main(String[] args) throws IOException {
            String lineEnding = "\r\n";
            String baseDir= "C:/path/to/your/directory/";   //Change this to suit your directory
    
            String origFileName= "OriginalFile.jsp";  //Change this to suit your original file that needs the attribute added
            File origFile = new File(baseDir + origFileName);
    
            String tempFileName = "TemporaryFile.jsp";
            File tempFile = new File(baseDir + tempFileName);
    
            Pattern p = Pattern.compile("^(?!.*styleId)\\s*<html:(?:select|option|text|hidden|textarea)\\s.*property=\"([a-zA-Z1-9.]*)\".+");
    
            FileReader in = new FileReader(origFile);
            FileWriter out = new FileWriter(tempFile);
    
            BufferedReader br = new BufferedReader(in);
            BufferedWriter bw = new BufferedWriter(out);
    
    
            String line;
            while ((line = br.readLine()) != null) {
                Matcher m = p.matcher(line);
                if(m.matches()){
                    String strWithStyleId = line.substring(0, m.start(1)) + m.group(1) + "\" styleId=\"" + line.substring(m.start(1));
                    bw.write(strWithStyleId + lineEnding);
                    System.out.println(strWithStyleId);
                }else {
                    bw.write(line + lineEnding);
                }
            }
    
            br.close();
            bw.close();
    
            //copies back to original file, BE CAREFUL!!! 
            copyFile(tempFile, origFile);
        }
    
        public static void copyFile(File sourceFile, File destFile) throws IOException {
            if(!destFile.exists()) {
                destFile.createNewFile();
            }
    
            FileChannel source = null;
            FileChannel destination = null;
    
            try {
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                destination.transferFrom(source, 0, source.size());
            }
            finally {
                if(source != null) {
                    source.close();
                }
                if(destination != null) {
                    destination.close();
                }
            }
        }
    }