背景前提
使用jar包「jsoup-1.15.1.jar」「commons-io-2.2.jar」
以下参考JSF源码类NavigationConfigProcessor
和ApplicationAssociate
源码分析: 源码分析JSF画面跳转下一个画面流程,关联faces-config.xml
1.JsoupMain
解析xml
1.1初始化
private static String path = "F:\\Project\\development\\Team";
/**
* <p>/faces-config/navigation-rule</p>
*/
private static final String NAVIGATION_RULE = "navigation-rule";
/**
* <p>/faces-config/navigation-rule/from-view-id</p>
*/
private static final String FROM_VIEW_ID = "from-view-id";
/**
* <p>/faces-config/navigation-rule/navigation-case</p>
*/
private static final String NAVIGATION_CASE = "navigation-case";
/**
* <p>/faces-config/navigation-rule/navigation-case/from-action</p>
*/
private static final String FROM_ACTION = "from-action";
/**
* <p>/faces-config/navigation-rule/navigation-case/from-outcome</p>
*/
private static final String FROM_OUTCOME = "from-outcome";
/**
* <p>/faces-config/navigation-rule/navigation-case/to-view-id</p>
*/
private static final String TO_VIEW_ID = "to-view-id";
/**
* <p>/faces-config/navigation-rule/navigation-case/redirect</p>
*/
private static final String REDIRECT = "redirect";
/**
* <p>If <code>from-view-id</code> is not defined.<p>
*/
private static final String FROM_VIEW_ID_DEFAULT = "*";
private static Map<String, List<ConfigNavigationCase>> caseListMap = null;
private static TreeSet<String> wildcardMatchList = null;
1.2main方法
public static void main(String[] args) throws IOException {
caseListMap = new HashMap<String, List<ConfigNavigationCase>>();
wildcardMatchList = new TreeSet<String>(new SortIt());
Collection<File> fileList = JsoupUtil.getFileListSuffixes(path, new String[] {"xml"});
for (File file : fileList) {
if(!file.getAbsolutePath().contains("\\target\\") && !file.getAbsolutePath().contains("\\.settings\\") &&
!file.getAbsolutePath().contains("\\.metadata\\") && !file.getAbsolutePath().contains("\\bin\\") &&
file.getAbsolutePath().contains("\\WebContent\\")) {
jsonupParse(file);
}
}
System.out.print(false);
}
1.2解析faces-config.xml
public static void jsonupParse(File file){
try {
Document strutsDocument = null;
if(file.getAbsolutePath().endsWith("faces-config.xml")){
strutsDocument = JsoupUtil.returnDocument(file);
//获取<navigation-rule>
Elements navigationRuleElements = strutsDocument.getElementsByTag(NAVIGATION_RULE);
if(navigationRuleElements != null && navigationRuleElements.size() > 0) {
for(Element navigationRuleElement : navigationRuleElements) {
String fromViewId = FROM_VIEW_ID_DEFAULT;
Elements fromViewIdElements = navigationRuleElement.getElementsByTag(FROM_VIEW_ID);
Elements navigationCaseElements = navigationRuleElement.getElementsByTag(NAVIGATION_CASE);
//获取<from-view-id>
if(fromViewIdElements != null && fromViewIdElements.size() > 0) {
fromViewId = fromViewIdElements.get(0).text();
}
if (!fromViewId.equals(FROM_VIEW_ID_DEFAULT) && fromViewId.charAt(0) != '/') {
fromViewId = '/' + fromViewId;
}
if(fromViewId.endsWith(".jsp")) {
fromViewId = fromViewId.replace(".jsp", "");
}
//获取<navigation-case>
if(navigationCaseElements != null && navigationCaseElements.size() > 0) {
for(Element navigationCaseElement : navigationCaseElements) {
String outcome = null;
String action = null;
String toViewId = null;
boolean redirect = false;
Elements fromOutComeElements = navigationCaseElement.getElementsByTag(FROM_OUTCOME);
Elements fromActionElements = navigationCaseElement.getElementsByTag(FROM_ACTION);
Elements toViewIdElements = navigationCaseElement.getElementsByTag(TO_VIEW_ID);
Elements redirectElements = navigationCaseElement.getElementsByTag(REDIRECT);
if(fromOutComeElements != null && fromOutComeElements.size() > 0) {
Element element = fromOutComeElements.get(0);
outcome = element.text();
}
if(fromActionElements != null && fromActionElements.size() > 0) {
Element element = fromActionElements.get(0);
action = element.text();
}
if(toViewIdElements != null && toViewIdElements.size() > 0) {
Element element = toViewIdElements.get(0);
toViewId = element.text();
if (toViewId.charAt(0) != '/') {
toViewId = '/' + toViewId;
}
}
if(redirectElements != null && redirectElements.size() > 0) {
redirect = true;
}
ConfigNavigationCase cnc = new ConfigNavigationCase(fromViewId, action, outcome, toViewId, redirect);
addNavigationCase(cnc);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
1.3将解析结果存放在caseListMap和wildcardMatchList
public static void addNavigationCase(ConfigNavigationCase navigationCase) {
String fromViewId = navigationCase.getFromViewId();
List<ConfigNavigationCase> caseList = caseListMap.get(fromViewId);
if (caseList == null) {
//noinspection CollectionWithoutInitialCapacity
caseList = new ArrayList<ConfigNavigationCase>();
caseList.add(navigationCase);
caseListMap.put(fromViewId, caseList);
} else {
String key = navigationCase.getKey();
boolean foundIt = false;
for (int i = 0; i < caseList.size(); i++) {
ConfigNavigationCase navCase = caseList.get(i);
// if there already is a case existing for the
// fromviewid/fromaction.fromoutcome combination,
// replace it ... (last one wins).
//
if (key.equals(navCase.getKey())) {
caseList.set(i, navigationCase);
foundIt = true;
break;
}
}
if (!foundIt) {
caseList.add(navigationCase);
}
}
if (fromViewId.endsWith("*")) {
fromViewId =
fromViewId.substring(0, fromViewId.lastIndexOf('*'));
wildcardMatchList.add(fromViewId);
}
}
1.4获取caseListMap和wildcardMatchList
public Map<String, List<ConfigNavigationCase>> getNavigationCaseListMappings() {
if (caseListMap == null) {
return Collections.emptyMap();
}
return caseListMap;
}
public TreeSet<String> getNavigationWildCardList() {
return wildcardMatchList;
}
1.5内部类SortIt
static class SortIt implements Comparator<String> {
public int compare(String fromViewId1, String fromViewId2) {
return -(fromViewId1.compareTo(fromViewId2));
}
}
2.JsoupUtil
工具类
public static Collection<File> getFileListSuffixes(String path, String[] suffixes) {
Collection<File> list = null;
list = (List<File>) FileUtils.listFiles(new File(path), suffixes, true);
return list;
}