1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.common.InfoException; 4 5 import java.io.StringWriter ; 6 import java.io.IOException ; 7 import java.util.Map ; 8 import java.util.Iterator ; 9 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Node ; 13 import org.apache.xerces.dom.DocumentImpl; 14 import org.apache.xml.serialize.OutputFormat; 15 import org.apache.xml.serialize.XMLSerializer; 16 import com.calipso.reportgenerator.reportcalculator.DataTreeNode; 17 import com.calipso.reportgenerator.reportcalculator.DataTreeSubItem; 19 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 20 import com.calipso.reportgenerator.common.*; 21 22 26 27 public class StaticReportXmlWriter implements ReportXmlWriter { 28 private ReportData reportData; 29 private ReportSpec reportSpec; 30 private Map paramValues; 31 private Document document; 32 33 39 public StaticReportXmlWriter(ReportData reportData, ReportSpec reportSpec, Map paramValues) { 40 this.reportData = reportData; 41 this.reportSpec = reportSpec; 42 this.paramValues = paramValues; 43 } 44 45 50 public ReportData getReportData() { 51 return reportData; 52 } 53 54 59 public StaticReportData getStaticReportData() { 60 return (StaticReportData) getReportData(); 61 } 62 63 68 public ReportSpec getReportSpec() { 69 return reportSpec; 70 } 71 72 77 public Map getParamValues() { 78 return paramValues; 79 } 80 81 84 private void startDocument() { 85 document = new DocumentImpl(); 86 } 87 88 96 97 102 public StringWriter getXml() throws InfoException { 103 try { 104 startDocument(); 106 DataTreeNode dataTreeNode = getStaticReportData().getDataTree().getRoot(); 108 dataTreeNode.setValue(getReportSpec().getDescription()); 109 generateNode(dataTreeNode, document); 110 return getStream(); 111 } catch (Exception e) { 112 throw new InfoException(LanguageTraslator.traslate("99")); 113 } 114 } 115 116 121 private void generateNode(DataTreeNode dataTreeNode, Node parent) { 122 Node element = createNodeElement(getTagName(dataTreeNode.getDimensionIndex()), dataTreeNode.getValue()); 123 parent.appendChild(element); 124 fillMetrics(dataTreeNode, element); 125 fillSubItems(dataTreeNode, element); 126 fillSubNodes(dataTreeNode, element); 127 } 128 129 135 private String getTagName(int dimensionIndex) { 136 if (dimensionIndex >= 0) { 137 return ((ReportDimensionSpec) reportSpec.getDimensionsByIndex().get(dimensionIndex)).getName(); 138 } 139 else { 140 return "Report"; 141 } 142 } 143 144 150 private void fillSubNodes(DataTreeNode dataTreeNode, Node parent) { 151 Iterator iterator = dataTreeNode.getSubNodes().values().iterator(); 152 while (iterator.hasNext()) { 153 DataTreeNode current = (DataTreeNode) iterator.next(); 154 generateNode(current, parent); 155 } 156 } 157 158 164 private void fillSubItems(DataTreeNode dataTreeNode, Node element) { 165 int[] dims = getStaticReportData().getDataTree().getQuery().getNoGroupDimensions(); 166 int[] mets = getStaticReportData().getDataTree().getQuery().getMetrics(); 167 int[] accmets = getStaticReportData().getDataTree().getQuery().getAccumulableMetrics(); 168 169 for (int i = 0; i < dataTreeNode.getSubItems().size(); i++) { 170 DataTreeSubItem subItem = (DataTreeSubItem) dataTreeNode.getSubItems().get(i); 171 Element item = createNodeElement("Item", String.valueOf(i)); 172 element.appendChild(item); 173 for (int j = 0; j < dims.length; j++) { 174 int dim = dims[j]; 175 String value = subItem.getNonGroupingDimensionValues()[j]; 176 if (value != null && !value.equals("")) { 177 item.setAttribute(getTagName(dim), value); 178 } 179 } 180 for (int j = 0; j < mets.length; j++) { 181 SharedFloat value = subItem.getMetricValues()[j]; 182 item.setAttribute(getMetricNameFromIndex(j), value.toString()); 183 } 184 185 186 for (int j = 0; j < accmets.length; j++) { 187 SharedFloat value = subItem.getAccumulableMetricValues()[j]; 188 item.setAttribute(getMetricNameFromIndex(subItem.getMetricIndex(j)) + "_ACC", value.toString()); 189 } 190 191 } 192 } 193 214 215 221 private void fillMetrics(DataTreeNode dataTreeNode, Node element) { 222 Object [] metrics = dataTreeNode.getMetrics(); 224 for (int i = 0; i < metrics.length; i++) { 225 SharedFloat metric = (SharedFloat) metrics[i]; 226 ((Element ) element).setAttribute(getMetricNameFromIndex(i), metric.toString()); 227 } 228 229 235 236 } 237 238 243 private String getMetricNameFromIndex(int metricIndex) { 244 245 return ((ReportMetricSpec) reportSpec.getMetricsByIndex().get(metricIndex)).getName(); 246 } 248 249 250 255 private StringWriter getStream() throws IOException { 256 OutputFormat format = new OutputFormat(document, "ISO-8859-1", true); 257 StringWriter stringOut = new StringWriter (); 258 XMLSerializer serial = new XMLSerializer(stringOut, format); 259 serial.asDOMSerializer(); 260 Element elem = document.getDocumentElement(); 261 if (elem != null) { 262 serial.serialize(elem); 263 } 264 return stringOut; 265 } 266 267 273 private Element createNodeElement(String tagName, String value) { 274 Element newElem; 275 newElem = document.createElement(tagName); 276 newElem.setAttribute("Value", value); 277 return newElem; 278 } 279 280 } 281 | Popular Tags |