if(logger.isInfoEnabled()) {
logger.info("start build rule in " + configFile);
}
InputStream in = null;
XMLEventReader r = null;
Report report = null;
StringBuilder globalConditions = new StringBuilder();
StringBuilder globalValuefilter = new StringBuilder();
List<String> globalMapClass = new ArrayList<String>();
String domain = null;
String localdir = new StringBuilder()
.append(System.getProperty("user.dir"))
.append(File.separatorChar).toString();
if (configFile == null || "".equals(configFile)) {
String error = "configFile can not be null !";
logger.error(error);
throw new AnalysisException(error);
}
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (configFile.startsWith("file:")) {
try {
in = new java.io.FileInputStream(new File(
configFile.substring(configFile.indexOf("file:")
+ "file:".length())));
} catch (Exception e) {
logger.error(e,e);
}
if (in == null)
in = new java.io.FileInputStream(new File(localdir
+ configFile.substring(configFile.indexOf("file:")
+ "file:".length())));
} else {
URL url = loader.getResource(configFile);
if (url == null) {
String error = "configFile: " + configFile + " not exist !";
logger.error(error);
throw new java.lang.RuntimeException(error);
}
in = url.openStream();
}
r = factory.createXMLEventReader(in);
List<String> parents = new ArrayList<String>();
while (r.hasNext()) {
XMLEvent event = r.nextEvent();
if (event.isStartElement()) {
StartElement start = event.asStartElement();
String tag = start.getName().getLocalPart();
if (tag.equalsIgnoreCase("domain")) {
if (start.getAttributeByName(new QName("", "value")) != null) {
domain = start.getAttributeByName(
new QName("", "value")).getValue();
rule.setDomain(domain);
}
continue;
}
if (tag.equalsIgnoreCase("alias")) {
Alias alias = new Alias();
alias.setName(start.getAttributeByName(
new QName("", "name")).getValue());
alias.setKey(Integer.valueOf(start.getAttributeByName(
new QName("", "key")).getValue()));
rule.getAliasPool().put(alias.getName(), alias);
continue;
}
if (tag.equalsIgnoreCase("inner-key")){
InnerKey innerKey = new InnerKey();
innerKey.setKey(Integer.parseInt(start.getAttributeByName(
new QName("", "key")).getValue()));
boolean isExist = false;
for(InnerKey ik : rule.getInnerKeyPool())
{
if (ik.getKey() == innerKey.getKey())
{
logger.error("duplicate innerkey define, key :" + innerKey.getKey());
isExist = true;
break;
}
}
if (!isExist)
{
if (innerKey.setFile(start.getAttributeByName(
new QName("", "file")).getValue()))
rule.getInnerKeyPool().add(innerKey);
else
logger.error("inner-key set error, file : " + innerKey.getFile());
}
continue;
}
if (tag.equalsIgnoreCase("global-condition")) {
if (start.getAttributeByName(new QName("", "value")) != null) {
if (globalConditions.length() > 0) {
globalConditions.append("&"
+ start.getAttributeByName(
new QName("", "value"))
.getValue());
} else {
globalConditions.append(start
.getAttributeByName(
new QName("", "value"))
.getValue());
}
}
continue;
}
if (tag.equalsIgnoreCase("global-mapClass")) {
if (start.getAttributeByName(new QName("", "value")) != null) {
globalMapClass.add(start.getAttributeByName(
new QName("", "value")).getValue());
}
continue;
}
if (tag.equalsIgnoreCase("global-valuefilter")) {
if (start.getAttributeByName(new QName("", "value")) != null) {
globalValuefilter.append(
start.getAttributeByName(
new QName("", "value")).getValue())
.append("&");
}
continue;
}
if (tag.equalsIgnoreCase("ReportEntry")
|| tag.equalsIgnoreCase("entry")) {
ReportEntry entry = new ReportEntry();
if (tag.equalsIgnoreCase("ReportEntry"))
setReportEntry(true, start, entry, report,
rule.getEntryPool(), rule.getAliasPool(),
globalConditions, globalValuefilter,
globalMapClass, parents);
else {
setReportEntry(false, start, entry, report,
rule.getEntryPool(), rule.getAliasPool(),
globalConditions, globalValuefilter,
globalMapClass, parents);
}
if (entry.getId() != null) {
if (rule.getEntryPool().get(entry.getId()) != null)
throw new java.lang.RuntimeException(
"ID confict:" + entry.getId());
rule.getEntryPool().put(entry.getId(), entry);
}
// 增加引用标识
if (tag.equalsIgnoreCase("entry")) {
if (entry.getId() != null)
rule.getReferEntrys().put(entry.getId(), entry);
else if (report.getReportEntrys() != null
&& report.getReportEntrys().size() > 0)
rule.getReferEntrys().put(
report.getReportEntrys()
.get(report.getReportEntrys()
.size() - 1).getId(),
report.getReportEntrys()
.get(report.getReportEntrys()
.size() - 1));
}
ReportEntry _tmpEntry = entry;
if (_tmpEntry.getId() == null
&& report.getReportEntrys() != null
&& report.getReportEntrys().size() > 0)
_tmpEntry = report.getReportEntrys().get(
report.getReportEntrys().size() - 1);
List<Object> bindingStack = ((SimpleCalculator)_tmpEntry.getCalculator()).getBindingStack();
String valueExpression = ((SimpleCalculator)_tmpEntry.getCalculator()).getValue();
if (bindingStack != null) {
if (valueExpression != null
&& valueExpression.indexOf(
"entry(") >= 0)
for (Object k : bindingStack) {
rule.getReferEntrys().put((String)k, null);
}
}
continue;
}
if (tag.equalsIgnoreCase("report")) {
if (report != null) {
if(report.getId() != null) {
if(rule.getReportPool().get(report.getId()) != null)
throw new java.lang.RuntimeException(
"ID confict:" + report.getId());
}
rule.getReportPool().put(report.getId(), report);
}
report = new Report();
setReport(start, report, rule.getReportPool());
continue;
}
if (tag.equalsIgnoreCase("entryList")) {
report.setReportEntrys(new ArrayList<ReportEntry>());
continue;
}
}
if (event.isEndElement()) {
EndElement end = event.asEndElement();
String tag = end.getName().getLocalPart();
if (tag.equalsIgnoreCase("reports") && report != null) {
rule.getReportPool().put(report.getId(), report);
continue;
}
}