1 28 29 33 package net.sf.jasperreports.engine.xml; 34 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.util.ArrayList ; 40 import java.util.Collection ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.Map ; 45 import java.util.Set ; 46 47 import javax.xml.parsers.ParserConfigurationException ; 48 49 import net.sf.jasperreports.engine.JRDatasetRun; 50 import net.sf.jasperreports.engine.JRException; 51 import net.sf.jasperreports.engine.JRGroup; 52 import net.sf.jasperreports.engine.JRVariable; 53 import net.sf.jasperreports.engine.design.JRDesignChart; 54 import net.sf.jasperreports.engine.design.JRDesignDataset; 55 import net.sf.jasperreports.engine.design.JRDesignElement; 56 import net.sf.jasperreports.engine.design.JRDesignElementDataset; 57 import net.sf.jasperreports.engine.design.JRDesignImage; 58 import net.sf.jasperreports.engine.design.JRDesignTextField; 59 import net.sf.jasperreports.engine.design.JRDesignVariable; 60 import net.sf.jasperreports.engine.design.JasperDesign; 61 62 import org.apache.commons.digester.Digester; 63 import org.xml.sax.InputSource ; 64 import org.xml.sax.SAXException ; 65 66 67 71 public class JRXmlLoader 72 { 73 74 77 private JasperDesign jasperDesign = null; 78 private Collection groupReprintedElements = new ArrayList (); 79 private Collection groupEvaluatedImages = new ArrayList (); 80 private Collection groupEvaluatedTextFields = new ArrayList (); 81 private Collection groupEvaluatedCharts = new ArrayList (); 82 private Set groupBoundDatasets = new HashSet (); 83 private List errors = new ArrayList (); 84 85 private Digester digester = null; 86 87 private boolean ignoreConsistencyProblems; 88 89 92 public JRXmlLoader(Digester digester) 93 { 94 this.digester = digester; 95 } 96 97 100 public void setJasperDesign(JasperDesign jasperDesign) 101 { 102 this.jasperDesign = jasperDesign; 103 } 104 105 108 public Collection getGroupReprintedElements() 109 { 110 return this.groupReprintedElements; 111 } 112 113 116 public Collection getGroupEvaluatedImages() 117 { 118 return this.groupEvaluatedImages; 119 } 120 121 124 public Collection getGroupEvaluatedTextFields() 125 { 126 return this.groupEvaluatedTextFields; 127 } 128 129 132 public Collection getGroupEvaluatedCharts() 133 { 134 return groupEvaluatedCharts; 135 } 136 137 140 public Set getGroupBoundDatasets() 141 { 142 return groupBoundDatasets; 143 } 144 145 146 149 public static JasperDesign load(String sourceFileName) throws JRException 150 { 151 return load(new File (sourceFileName)); 152 } 153 154 155 158 public static JasperDesign load(File file) throws JRException 159 { 160 JasperDesign jasperDesign = null; 161 162 FileInputStream fis = null; 163 164 try 165 { 166 fis = new FileInputStream (file); 167 jasperDesign = JRXmlLoader.load(fis); 168 } 169 catch(IOException e) 170 { 171 throw new JRException(e); 172 } 173 finally 174 { 175 if (fis != null) 176 { 177 try 178 { 179 fis.close(); 180 } 181 catch(IOException e) 182 { 183 } 184 } 185 } 186 187 return jasperDesign; 188 } 189 190 191 194 public static JasperDesign load(InputStream is) throws JRException 195 { 196 JasperDesign jasperDesign = null; 197 198 JRXmlLoader xmlLoader = null; 199 200 try 201 { 202 xmlLoader = new JRXmlLoader(JRXmlDigesterFactory.createDigester()); 203 } 204 catch (ParserConfigurationException e) 205 { 206 throw new JRException(e); 207 } 208 catch (SAXException e) 209 { 210 throw new JRException(e); 211 } 212 213 jasperDesign = xmlLoader.loadXML(is); 214 215 return jasperDesign; 216 } 217 218 219 220 223 public JasperDesign loadXML(InputStream is) throws JRException 224 { 225 return loadXML(new InputSource (is)); 226 } 227 228 231 public JasperDesign loadXML(InputSource is) throws JRException 232 { 233 try 234 { 235 digester.push(this); 236 237 238 digester.parse(is); 239 } 240 catch(SAXException e) 241 { 242 throw new JRException(e); 243 } 244 catch(IOException e) 245 { 246 throw new JRException(e); 247 } 248 finally 249 { 250 digester.clear(); 251 } 252 253 if (errors.size() > 0) 254 { 255 Exception e = (Exception )errors.get(0); 256 if (e instanceof JRException) 257 { 258 throw (JRException)e; 259 } 260 throw new JRException(e); 261 } 262 263 264 assignGroupsToVariables(jasperDesign.getMainDesignDataset()); 265 for (Iterator it = jasperDesign.getDatasetsList().iterator(); it.hasNext();) 266 { 267 JRDesignDataset dataset = (JRDesignDataset) it.next(); 268 assignGroupsToVariables(dataset); 269 } 270 271 this.assignGroupsToElements(); 272 this.assignGroupsToImages(); 273 this.assignGroupsToTextFields(); 274 this.assignGroupsToCharts(); 275 this.assignGroupsToDatasets(); 276 277 return this.jasperDesign; 278 } 279 280 283 private void assignGroupsToVariables(JRDesignDataset dataset) throws JRException 284 { 285 JRVariable[] variables = dataset.getVariables(); 286 if (variables != null && variables.length > 0) 287 { 288 Map groupsMap = dataset.getGroupsMap(); 289 for(int i = 0; i < variables.length; i++) 290 { 291 JRDesignVariable variable = (JRDesignVariable)variables[i]; 292 if (variable.getResetType() == JRVariable.RESET_TYPE_GROUP) 293 { 294 String groupName = null; 295 JRGroup group = variable.getResetGroup(); 296 if (group != null) 297 { 298 groupName = group.getName(); 299 group = (JRGroup)groupsMap.get(groupName); 300 } 301 302 if (!ignoreConsistencyProblems && group == null) 303 { 304 throw 305 new JRException( 306 "Unknown reset group '" + groupName 307 + "' for variable : " + variable.getName() 308 ); 309 } 310 311 variable.setResetGroup(group); 312 } 313 else 314 { 315 variable.setResetGroup(null); 316 } 317 318 if (variable.getIncrementType() == JRVariable.RESET_TYPE_GROUP) 319 { 320 String groupName = null; 321 JRGroup group = variable.getIncrementGroup(); 322 if (group != null) 323 { 324 groupName = group.getName(); 325 group = (JRGroup)groupsMap.get(groupName); 326 } 327 328 if (!ignoreConsistencyProblems && group == null) 329 { 330 throw 331 new JRException( 332 "Unknown increment group '" + groupName 333 + "' for variable : " + variable.getName() 334 ); 335 } 336 337 variable.setIncrementGroup(group); 338 } 339 else 340 { 341 variable.setIncrementGroup(null); 342 } 343 } 344 } 345 } 346 347 348 351 private void assignGroupsToElements() throws JRException 352 { 353 Map groupsMap = jasperDesign.getGroupsMap(); 354 JRDesignElement element = null; 355 String groupName = null; 356 JRGroup group = null; 357 for(Iterator it = groupReprintedElements.iterator(); it.hasNext();) 358 { 359 element = (JRDesignElement)it.next(); 360 361 groupName = null; 362 group = element.getPrintWhenGroupChanges(); 363 if (group != null) 364 { 365 groupName = group.getName(); 366 group = (JRGroup)groupsMap.get(group.getName()); 367 } 368 369 if (!ignoreConsistencyProblems && group == null) 370 { 371 throw new JRException("Unknown reprint group '" + groupName + "' for element."); 372 } 373 374 element.setPrintWhenGroupChanges(group); 375 } 376 } 377 378 379 382 private void assignGroupsToImages() throws JRException 383 { 384 Map groupsMap = jasperDesign.getGroupsMap(); 385 JRDesignImage image = null; 386 String groupName = null; 387 JRGroup group = null; 388 for(Iterator it = groupEvaluatedImages.iterator(); it.hasNext();) 389 { 390 image = (JRDesignImage)it.next(); 391 392 groupName = null; 393 group = image.getEvaluationGroup(); 394 if (group != null) 395 { 396 groupName = group.getName(); 397 group = (JRGroup)groupsMap.get(group.getName()); 398 } 399 400 if (!ignoreConsistencyProblems && group == null) 401 { 402 throw new JRException("Unknown evaluation group '" + groupName + "' for image."); 403 } 404 405 image.setEvaluationGroup(group); 406 } 407 } 408 409 410 413 private void assignGroupsToTextFields() throws JRException 414 { 415 Map groupsMap = jasperDesign.getGroupsMap(); 416 JRDesignTextField textField = null; 417 String groupName = null; 418 JRGroup group = null; 419 for(Iterator it = groupEvaluatedTextFields.iterator(); it.hasNext();) 420 { 421 textField = (JRDesignTextField)it.next(); 422 423 groupName = null; 424 group = textField.getEvaluationGroup(); 425 if (group != null) 426 { 427 groupName = group.getName(); 428 group = (JRGroup)groupsMap.get(group.getName()); 429 } 430 431 if (!ignoreConsistencyProblems && group == null) 432 { 433 throw new JRException("Unknown evaluation group '" + groupName + "' for text field."); 434 } 435 436 textField.setEvaluationGroup(group); 437 } 438 } 439 440 441 444 private void assignGroupsToCharts() throws JRException 445 { 446 Map groupsMap = jasperDesign.getGroupsMap(); 447 for(Iterator it = groupEvaluatedCharts.iterator(); it.hasNext();) 448 { 449 JRDesignChart chart = (JRDesignChart)it.next(); 450 451 String groupName = null; 452 JRGroup group = chart.getEvaluationGroup(); 453 if (group != null) 454 { 455 groupName = group.getName(); 456 group = (JRGroup)groupsMap.get(group.getName()); 457 } 458 459 if (!ignoreConsistencyProblems && group == null) 460 { 461 throw new JRException("Unknown evaluation group '" + groupName + "' for chart."); 462 } 463 464 chart.setEvaluationGroup(group); 465 } 466 } 467 468 469 472 private void assignGroupsToDatasets() throws JRException 473 { 474 for(Iterator it = groupBoundDatasets.iterator(); it.hasNext();) 475 { 476 JRDesignElementDataset dataset = (JRDesignElementDataset) it.next(); 477 478 JRDatasetRun datasetRun = dataset.getDatasetRun(); 479 Map groupsMap; 480 if (datasetRun == null) 481 { 482 groupsMap = jasperDesign.getGroupsMap(); 483 } 484 else 485 { 486 Map datasetMap = jasperDesign.getDatasetMap(); 487 String datasetName = datasetRun.getDatasetName(); 488 JRDesignDataset subDataset = (JRDesignDataset) datasetMap.get(datasetName); 489 if (subDataset == null) 490 { 491 throw new JRException("Unknown sub dataset '" + datasetName + "' for chart dataset."); 492 } 493 groupsMap = subDataset.getGroupsMap(); 494 } 495 496 if (dataset.getIncrementType() == JRVariable.RESET_TYPE_GROUP) 497 { 498 String groupName = null; 499 JRGroup group = dataset.getIncrementGroup(); 500 if (group != null) 501 { 502 groupName = group.getName(); 503 group = (JRGroup)groupsMap.get(group.getName()); 504 } 505 506 if (!ignoreConsistencyProblems && group == null) 507 { 508 throw new JRException("Unknown increment group '" + groupName + "' for chart dataset."); 509 } 510 511 dataset.setIncrementGroup(group); 512 } 513 else 514 { 515 dataset.setIncrementGroup(null); 516 } 517 518 if (dataset.getResetType() == JRVariable.RESET_TYPE_GROUP) 519 { 520 String groupName = null; 521 JRGroup group = dataset.getResetGroup(); 522 if (group != null) 523 { 524 groupName = group.getName(); 525 group = (JRGroup)groupsMap.get(group.getName()); 526 } 527 528 if (!ignoreConsistencyProblems && group == null) 529 { 530 throw new JRException("Unknown reset group '" + groupName + "' for chart dataset."); 531 } 532 533 dataset.setResetGroup(group); 534 } 535 else 536 { 537 dataset.setResetGroup(null); 538 } 539 } 540 } 541 542 543 546 public void addError(Exception e) 547 { 548 if(!ignoreConsistencyProblems) 549 this.errors.add(e); 550 } 551 552 556 public boolean isIgnoreConsistencyProblems() { 557 return ignoreConsistencyProblems; 558 } 559 560 567 public void setIgnoreConsistencyProblems(boolean ignoreConsistencyProblems) { 568 this.ignoreConsistencyProblems = ignoreConsistencyProblems; 569 } 570 571 } 572 | Popular Tags |