我敢肯定,這對於專家來說是非常基本的東西,但對於我來說,作爲一個新手,這給我一個很難。java動態選擇類執行
我有3個解析器,每個解析器都有自己的功能,將來會有更多的解析器。現在我想要做的是:我希望我的應用程序在運行時根據頁面來選擇正確的解析器。
要做到這一點,我做了以下內容:我有一個接口(IWebParser):
public interface IWebParser {
public abstract Object execute(String page, URL url);
public abstract List<SimpleWebPosting> parse(String page, URL url, List<String> tokens);
public abstract Boolean canExecute(URL url);
}
的每一個我的解析器實現了這個接口。我有另一個叫ParserControl的類,其中有一個方法submit(String page,URL url) - 這是我的程序總是調用的一個方法,只要有一個頁面需要解析。該類ParserControl從xml文件中獲得可用的解析器,並嘗試(在while語句中)任何解析器是否可以解析所討論的頁面。這是通過canExecute(URL url)方法完成的。現在,在canExecute上接收到true時,我想執行該特定的解析器。
我班ParserControl看起來是這樣的:
public class ParserControl {
private static final Logger logger = Logger.getLogger("de.comlineag.snc.parser.ParserControl");
// the list of operational web parser as taken from the properties file is stored within this structure
private static List<IWebParser> webParser;
// the ParserControl instance - used during instantiation of the class and later to retrieve the list
private static ParserControl pc = null;
// ParserControl is not to be directly instantiated by other classes
private ParserControl() {
try {
webParser = getAllParser();
} catch (XPathExpressionException | IOException
| ParserConfigurationException | SAXException e) {
logger.error("EXCEPTION :: error during parser execution " + e.getMessage());
e.printStackTrace();
}
};
// Static 'instance' method - this method is called every time
// the submit method is called but can also be called implicitely to get
// an instance of ParserControl
public static ParserControl getInstance() throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
if (pc == null) {pc = new ParserControl();}
return pc;
}
public static List<SimpleWebPosting> submit(String page, URL url, ArrayList<String> tTerms) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException{
logger.trace("ParserControl called");
pc = getInstance();
while (pc.webParser.iterator().hasNext()) {
logger.trace("trying parser " + pc.webParser.iterator().getClass().getSimpleName().toString());
if (((IWebParser) pc.webParser.iterator().getClass().getClassLoader()).canExecute(url)) {
return ((IWebParser) pc.webParser.iterator().getClass().getClassLoader()).parse(page, url, tTerms);
} else {
logger.trace("parser " + pc.webParser.iterator().getClass().getSimpleName().toString() + " returned false to canExecute()");
}
}
return null;
}
// retrieves all configured parser from the properties file and creates the parser list
@SuppressWarnings("unchecked")
private <T> ArrayList<T> getAllParser() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException {
String fileName = "webapp/WEB-INF/properties/webparser.xml";
ArrayList<T> ar = new ArrayList<T>();
File file = new File(fileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
String expression = "//parser[@type='webparser']/value";
NodeList nodeList= (NodeList) xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);
for (int i = 0 ; i < nodeList.getLength() ; i++) {
ar.add((T) nodeList.item(i).getTextContent());
logger.trace("found parser " + nodeList.item(i).getTextContent().toString() + " in configuration file " + fileName);
}
return ar;
}
}
現在,這個漫長的介紹後,我的問題:當執行這一點,我不能實例解析類,而是得到一個NullPointerException異常。在while循環內的logger.trace返回:
TRACE ParserControl - trying parser Itr <--- I would expect the class name here!!!
ERROR SimpleWebCrawler - WEBCRAWLER-Crawler Exception java.lang.NullPointerException
誰能告訴我,我在做什麼錯在這裏?
什麼行是NPE被拋出? (if(IWebParser)pc.webParser.iterator()。getClass()。getClassLoader())。canExecute()方法在while循環while(pc.webParser.iterator()。hasNext())內部的 – 2014-10-07 18:37:39
url)){ – siliconchris 2014-10-07 18:41:38