javaregexperljsp

Searching for a valid pattern in files of under a folder? (Maybe with Perl or with some Apis at Java or anything else)


I have a folder and it's name is v3. There are jsp files in that folder also in v3 folder there are some folders and there are jsp files in that folder too.

My jsp folders have some links as like:

<link rel="stylesheet" href="/static/css/main.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="/css<s:text name="scripts"/>/general_styles.css">
<link rel="stylesheet" type="text/css" href="/v3/css<s:text name="scripts"/>/something.css" >

and scripts:

<script language="javascript" type="text/javascript" src="/static/scripts/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/scripts<s:text name="scripts"/>/prototype-1.6.0.2.js"></script>
<script language="javascript" type="text/javascript" src="/scripts<s:text name="scripts"/>/${a.name}/<s:text name="genericJs"/>"></script>

For links:

href should start with /static/ for example this is valid:

<link rel="stylesheet" href="/static/css/main.css" type="text/css" />

For scripts:

src should start with /static/ too for example this is valid:

<script language="javascript" type="text/javascript" src="/static/scripts/jquery-1.4.2.min.js"></script>

What I want to do that I want to detect which files has not valid definitions.

EDIT: Valid - not valid is a notion for my company's system. We are moving our css and js folders to another and they will be under a folder and that folder's name is v3.

The program will work like that:


Solution

  • Well, something like this will get you going:

    public static void main(String[] args) throws IOException {
        Iterator<File> files = FileUtils.iterateFiles(new File("/path/to/v3"), new String[]{"jsp"}, true);
        while (files.hasNext()) {
            File jsp =  files.next();
            List<String> list = FileUtils.readLines(jsp);
            for (String line : list) {
                if(line.startsWith("<link") || line.startsWith("<script")) {
                    if(!line.contains("/static")) {
                        throw new RuntimeException("invalid file found: " + jsp.getAbsolutePath());
                    }
                }
            }
        }
    }
    

    Edited to contain changes discussed in comments