1 package com.calipso.reportgenerator.reportmanager; 2 3 4 import com.calipso.reportgenerator.common.*; 5 import com.calipso.reportgenerator.reportcalculator.*; 6 import com.calipso.reportgenerator.reportdefinitions.types.ReportDataType; 7 import com.calipso.common.DateEx; 8 9 import java.io.File ; 10 import java.io.InputStream ; 11 import java.io.FileInputStream ; 12 import java.util.List ; 13 import java.util.ArrayList ; 14 15 import org.xml.sax.InputSource ; 16 import org.w3c.dom.*; 17 import org.apache.poi.hssf.usermodel.HSSFCell; 18 import com.calipso.reportgenerator.reportcalculator.IDataSource; 19 import com.calipso.reportgenerator.common.InfoException; 20 21 27 public class XmlReportDataSource extends ReportDataSource { 28 29 private String url; 30 private IDataSource dataSource; 31 32 public XmlReportDataSource(ReportSpec reportSpec, ReportDataSourceSpec dataSourceSpec, ReportGeneratorConfiguration managerConfiguration) { 33 super(reportSpec, dataSourceSpec); 34 super.setGeneratorConfiguration(managerConfiguration); 35 } 36 37 40 protected void initialize() { 41 url = getReportDataSourceSpec().getExpression(); 42 } 43 44 47 private void loadFromXml(Matrix matrix) throws InfoException { 48 org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(); 49 try { 50 File file = new File (url); 51 InputStream in = new FileInputStream (file); 52 InputSource source = new InputSource (in); 53 parser.parse(source); 54 } catch (Exception e) { 55 throw new InfoException(LanguageTraslator.traslate("100"), e); 56 } 57 Document document = parser.getDocument(); 58 loadXmlDocument(document, matrix); 59 } 60 61 65 private void loadXmlDocument(Document document, Matrix matrix) throws InfoException { 66 Node rootNode = FindElement(document, "DataSource"); 67 loadRowList(rootNode, matrix); 68 } 69 70 74 private void loadRowList(Node node, Matrix matrix) throws InfoException { 75 Node rowsNode = FindElement(node, "Rows"); 76 if (rowsNode != null) { 77 loadRows(rowsNode, matrix); 78 } 79 } 80 81 85 private void loadRows(Node node, Matrix matrix) throws InfoException { 86 try { 87 if (node != null) { 88 Node childNode; 89 NodeList children = node.getChildNodes(); 90 if (children != null) { 91 for (int i = 0; i < children.getLength(); i++) { 92 childNode = children.item(i); 93 if (childNode.getNodeType() == Node.ELEMENT_NODE) { 94 NamedNodeMap attributes = childNode.getAttributes(); 95 Object [] row = getRow(attributes); 96 try { 98 if ((getFilter()== null)||((getFilter()!= null) && (getFilter().matches(row)))) { 99 matrix.add(row); 100 } 101 } catch (InfoException e) { 102 throw new InfoException(LanguageTraslator.traslate("101"), e); 103 } 104 } 105 } 106 } 107 } 108 } catch(OutOfMemoryError e) { 109 throw new InfoException(LanguageTraslator.traslate("326"), e); 110 } 111 } 112 113 private Object [] getRow(NamedNodeMap attributes) throws InfoException{ 114 int dimCount = getReportSpec().getDimensionsByIndex().size(); 115 int colNum = dimCount + getReportSpec().getMetricsByIndex().size(); 116 Object [] collection = new Object [colNum]; 117 for (int i = 0, j = 0; i < (colNum); i++) { 119 if(i < dimCount){ 121 ReportDimensionSpec dimension = getReportSpec().getDimensionFromIndex(i); 123 if(dimension.getCalculated()){ 124 collection[i] = dimension.getValue(collection, getReportDataSourceSpec()); 125 }else{ 126 collection[i] = getValueForDimension(attributes.item(j).getNodeValue(), dimension, collection, i); 127 j++; 128 } 129 }else{ 130 ReportMetricSpec metric = getReportSpec().getMetricFromIndex(i - dimCount); 132 if(metric.getCalculated()){ 133 collection[i] = metric.getValue(collection); 134 }else{ 135 collection[i] = getValueForMetric(attributes.item(j).getNodeValue(), metric, collection, i); 136 j++; 137 } 138 } 139 } 140 return collection; 141 } 142 143 164 165 171 private static Node FindElement(Node node, String rootName) { 172 Node resNode = null; 173 174 if (node.getNodeName().compareTo(rootName) == 0) { 175 resNode = node; 176 } 177 178 if (resNode == null) { 179 NodeList children = node.getChildNodes(); 180 if (children != null) { 181 for (int i = 0; i < children.getLength(); i++) { 182 if (resNode == null) { 183 resNode = FindElement(children.item(i), rootName); 184 } 185 } 186 } 187 } 188 return resNode; 189 } 190 191 192 197 public IDataSource getDataSource(Matrix matrix) throws InfoException { 198 loadFromXml(matrix); 201 return dataSource; 203 } 204 205 209 public int getFilterVarMode() { 210 return ReportFilterBuilder.VARMODE_DATAINDEX; 211 } 212 213 } 214 | Popular Tags |