Skip to content

Instantly share code, notes, and snippets.

@dileeph
Created January 22, 2018 08:10
Show Gist options
  • Save dileeph/d7b17775823fe19af7bdaebaad7ead8e to your computer and use it in GitHub Desktop.
Save dileeph/d7b17775823fe19af7bdaebaad7ead8e to your computer and use it in GitHub Desktop.
import java.util.AbstractMap.SimpleEntry;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class TagPatterns {
public static Map<PatternKey, String> tagdirectiveURLPatternMap() {
return Collections.unmodifiableMap(Stream.of(
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*struts[\\S]*html"), "struts1-html"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*struts[\\S]*logic"), "struts1-logic"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*struts[\\S]*bean"), "struts1-bean"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*struts[\\S]*nested"), "struts1-nested"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*struts-tags"), "struts2"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*struts-dojo"), "struts2-dojo"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*cs-jsp-tags"), "ccf"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*tiles"), "tiles"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*jsf"), "jsf"),
new SimpleEntry<>(new PatternKey("uri[\\s]*=[\\s]*[\\S]*jstl"), "jstl")
)
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue())));
}
}
================================
import java.io.Serializable;
import java.util.regex.Pattern;
/**
* Quite important that the hashcode and equals uses only the String part and not the Pattern.
* Otherwise HashMap key matching will fail.
* @author 347862
*
*/
public class PatternKey implements Serializable {
private String key;
private Pattern pattern;
public PatternKey(String key){
this.key = key;
this.pattern = Pattern.compile(key);
}
public PatternKey(String key, Pattern pattern) {
super();
this.key = key;
this.pattern = pattern;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Pattern getPattern() {
return pattern;
}
public void setPattern(Pattern pattern) {
this.pattern = pattern;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PatternKey other = (PatternKey) obj;
if (key == null) {
if (other.key != null)
return false;
} else if (!key.equals(other.key))
return false;
return true;
}
}
====================================
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ProjectDetail {
private String name;
private Path webRoot;
private Struts2InputOutput io;
private Map<String, List<String>> taglibMap = new HashMap<>();
private List<String> otherTLDs = new ArrayList<>();
public void addToOtherTLD(String tld){
otherTLDs.add(tld);
}
public List<String> getOtherTLDs() {
return otherTLDs;
}
public void setOtherTLDs(List<String> otherTLDs) {
this.otherTLDs = otherTLDs;
}
public void addToTaglibMap(String key, String value){
if(taglibMap.get(key) == null){
taglibMap.put(key, new ArrayList<>());
}
taglibMap.get(key).add(value);
}
public Map<String, List<String>> getTaglibMap() {
return taglibMap;
}
public void setTaglibMap(Map<String, List<String>> taglibMap) {
this.taglibMap = taglibMap;
}
public Struts2InputOutput getIo() {
return io;
}
public void setIo(Struts2InputOutput io) {
this.io = io;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Path getWebRoot() {
return webRoot;
}
public void setWebRoot(Path webRoot) {
this.webRoot = webRoot;
}
@Override
public String toString() {
return "ProjectDetail [name=" + name + ", webRoot=" + webRoot + ", io=" + io + ", taglibMap=" + taglibMap + "]";
}
}
==============================
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.jasper.JasperException;
public class ProjectProcessor implements Callable<ProjectDetail> {
private Path start;
public ProjectProcessor(Path start){
this.start = start;
}
@Override
public ProjectDetail call() throws Exception {
ProjectDetail detail = new ProjectDetail();
detail.setName(start.getFileName().toString());
// Struts2InputOutput io = new Struts2InputOutput();
// detail.setIo(io);
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attr) throws IOException {
// if (attr.isRegularFile() && path.toString().contains("web.xml")) {
//
// Path webroot = path.getParent().getParent();
// detail.setWebRoot(webroot);
//
//
// }else if(attr.isRegularFile() && path.toString().endsWith(".java")){
// //parseJava(path, io);
// }
// else
if(attr.isRegularFile() && path.toString().endsWith(".jsp")){
//System.out.println("Walking file: " + path);
loopJsp(path, detail);
}
return FileVisitResult.CONTINUE;
}
});
return detail;
}
// private void parseJava(Path path, Struts2InputOutput io) throws FileNotFoundException{
//
// CompilationUnit cu = JavaParser.parse(path.toFile());
// cu.accept(new Struts2TargetVisitor(), io);
//
// }
//
// private void parseXml(Path root){
//
// }
//
private void loopJsp(Path file, ProjectDetail detail){
final Pattern prefixAttributeNameRegexp = Pattern.compile("prefix[\\s]*=[\\s]*['|\"](\\S+)['|\"]");
final Pattern uriGenericRegexpPattern = Pattern.compile("uri[\\s]*=[\\s]*[\\S]*");
final Map<String, String> currentPageTldPrefix = new HashMap<>();
final Set<String> lineNumbersNotSelected = new HashSet<>();
try {
Files.lines(file).forEach( line -> {
if(line.contains("taglib")){
lineNumbersNotSelected.add(line);
TagPatterns.tagdirectiveURLPatternMap().keySet().forEach(key -> {
Matcher match = key.getPattern().matcher(line);
if(match.find()){
Matcher prefMatch = prefixAttributeNameRegexp.matcher(line);
if(prefMatch.find()){
String prefixStr = prefMatch.group(0);
System.out.println("pref str=" + prefixStr);
//extractPrefix.matcher(prefixStr);
String[] arr = prefixStr.split("=");
String prefixValue = StringUtils.removeAll(arr[1], "[\"|']");
String regexForEachTagPrefix = "<[\\s]*" + prefixValue + "[\\s]*:[\\s]*[\\S]*";
currentPageTldPrefix.put(regexForEachTagPrefix, TagPatterns.tagdirectiveURLPatternMap().get(key));
System.out.println("CPT:" + currentPageTldPrefix);
System.out.println("bef rem" + lineNumbersNotSelected);
lineNumbersNotSelected.remove(line);
System.out.println("adt rem" +lineNumbersNotSelected);
}
}
});
}else{
currentPageTldPrefix.keySet().forEach( key ->{
Pattern regexForEachTagPrefixPattern = Pattern.compile(key);
Matcher match = regexForEachTagPrefixPattern.matcher(line);
if(match.find()){
String tag = match.group(0);
String name = StringUtils.removePattern(tag, "<[\\s]*[\\w]*[\\s]*:[\\s]*");
name = StringUtils.removePattern(name, "[^\\w]");
detail.addToTaglibMap(currentPageTldPrefix.get(key), name);
}
});
}
// if(line.contains("taglib")){System.out.println("{{"+line);
// Matcher jstl = jstlCoreRegexp.matcher(line);
// if(jstl.find()) System.out.println(">>>" + jstl.group(0));
// Matcher prefix = prefixRegexp.matcher(line);
// if(prefix.find()) System.out.println(">>>" + prefix.group(0)
// + " " + prefix.groupCount() );
//// if(line.matches(jstlCoreRegexp)){
//// Matcher mat = prefixRegexp.matcher(line);
//// System.out.println("))))JSTL Tag PREFIX " + mat);
//// }
// }
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
detail.getOtherTLDs().addAll(lineNumbersNotSelected);
System.out.println(currentPageTldPrefix);
System.out.println(detail.getTaglibMap());
}
private void parseJsp(Path webroot) throws JasperException{
// CustomCompiler c = new CustomCompiler();
// c.setUriroot(webroot.toString());
// c.setFailOnError(false);
// c.execute();
}
}
======================================
@Test
public void walkjspc() throws Exception{
Path start = Paths.get("D:/Tut-wkspc"); //D:/Tutorials/struts-examples-master");
List<Path> subdirs = Files.list(start).filter(Files::isDirectory).collect(Collectors.toList());
//subdirs.parallelStream().forEach();
ExecutorService executorService = Executors.newWorkStealingPool();
List<ProjectProcessor> pps = new ArrayList<>();
subdirs.forEach(path -> pps.add(new ProjectProcessor(path)));
//ProjectProcessor pp = new ProjectProcessor(start);
executorService.invokeAll(pps)
.stream()
.map(future -> {
try {
return future.get();
}
catch (Exception e) {
throw new IllegalStateException(e);
}
})
.forEach(future -> {
System.out.println(future.toString());
try (BufferedWriter writer = Files.newBufferedWriter(
Paths.get("D:/outcome-struts3.csv"),
new StandardOpenOption[]{StandardOpenOption.CREATE, StandardOpenOption.APPEND}
)) {
writer.write(future.getName() + "," + future.getTaglibMap().toString() + "," + future.getOtherTLDs() );
writer.newLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment