package org.jibeframework.core.app.config;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jibeframework.core.service.ConfigService;
import org.jibeframework.core.service.impl.ConfigFile;
import org.jibeframework.core.service.impl.ConfigParser;
public class XMLConfigParser implements ConfigParser {
private List<FactoryWorker> workers = new ArrayList<FactoryWorker>();
private ConfigService configService;
static {
}
public XMLConfigParser() {
workers.add(new ConfigurationConfigFactoryWorker());
}
public void addFactoryWorker(FactoryWorker worker) {
workers.add(worker);
}
public ConfigElement produceElement(Element el) {
String name = el.getNamespacePrefix() + ":" + el.getName();
for (FactoryWorker worker : workers) {
String type = "jibe:" + worker.getType();
if (type.equals(name)) {
return worker.produce(el);
}
}
throw new IllegalStateException("Config element factory worker not found for:" + el.getName());
}
@SuppressWarnings("unchecked")
public void parseConfig(ConfigFile configFile) {
InputStream is = configFile.getInputStream();
SAXReader reader = new SAXReader();
Document document = null;
try {
document = reader.read(is);
} catch (DocumentException e) {
e.printStackTrace();
}
Element rootElement = document.getRootElement();
Iterator<Element> configElementsIter = rootElement.elementIterator();
while (configElementsIter.hasNext()) {
configService.register(produceElement(configElementsIter.next()));
}
}
public void setConfigService(ConfigService configService) {
this.configService = configService;
}
}