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:
jsp
files are under v3 folder. I will run that program from anywhere and it will check all the jsp files under that folder(I will define the whole path of that v3 folder in the written program).
It will find all lines that start with <link
and <script
.
If it is <link
it will find href="
If it is <script
it will find src="
After it finds one of them it will check that does it start with /static/
or not.
If starts it is OK but if doesn't it will write the file name to output/text file or anything else.
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