1 28 package net.sf.jasperreports.engine.xml; 29 30 import java.io.FileInputStream ; 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 import javax.xml.parsers.ParserConfigurationException ; 37 import javax.xml.parsers.SAXParser ; 38 import javax.xml.parsers.SAXParserFactory ; 39 40 import net.sf.jasperreports.engine.JRBox; 41 import net.sf.jasperreports.engine.JRException; 42 import net.sf.jasperreports.engine.JRFont; 43 import net.sf.jasperreports.engine.JRPrintElement; 44 import net.sf.jasperreports.engine.JRPrintHyperlinkParameter; 45 import net.sf.jasperreports.engine.JRPrintPage; 46 import net.sf.jasperreports.engine.JRReportFont; 47 import net.sf.jasperreports.engine.JRStyle; 48 import net.sf.jasperreports.engine.JasperPrint; 49 import net.sf.jasperreports.engine.util.JRProperties; 50 51 import org.apache.commons.digester.SetNestedPropertiesRule; 52 import org.xml.sax.ErrorHandler ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.SAXParseException ; 55 import org.xml.sax.XMLReader ; 56 57 58 62 public class JRPrintXmlLoader implements ErrorHandler 63 { 64 65 68 private JasperPrint jasperPrint = null; 69 private List errors = new ArrayList (); 70 71 72 75 protected JRPrintXmlLoader() 76 { 77 } 78 79 80 83 public void setJasperPrint(JasperPrint jasperPrint) 84 { 85 this.jasperPrint = jasperPrint; 86 } 87 88 89 92 public static JasperPrint load(String sourceFileName) throws JRException 93 { 94 JasperPrint jasperPrint = null; 95 96 FileInputStream fis = null; 97 98 try 99 { 100 fis = new FileInputStream (sourceFileName); 101 JRPrintXmlLoader printXmlLoader = new JRPrintXmlLoader(); 102 jasperPrint = printXmlLoader.loadXML(fis); 103 } 104 catch(IOException e) 105 { 106 throw new JRException(e); 107 } 108 finally 109 { 110 if (fis != null) 111 { 112 try 113 { 114 fis.close(); 115 } 116 catch(IOException e) 117 { 118 } 119 } 120 } 121 122 return jasperPrint; 123 } 124 125 126 129 public static JasperPrint load(InputStream is) throws JRException 130 { 131 JasperPrint jasperPrint = null; 132 133 JRPrintXmlLoader printXmlLoader = new JRPrintXmlLoader(); 134 jasperPrint = printXmlLoader.loadXML(is); 135 136 return jasperPrint; 137 } 138 139 140 143 private JasperPrint loadXML(InputStream is) throws JRException 144 { 145 try 146 { 147 JRXmlDigester digester = prepareDigester(); 148 149 150 digester.parse(is); 151 } 152 catch(ParserConfigurationException e) 153 { 154 throw new JRException(e); 155 } 156 catch(SAXException e) 157 { 158 throw new JRException(e); 159 } 160 catch(IOException e) 161 { 162 throw new JRException(e); 163 } 164 165 if (errors.size() > 0) 166 { 167 Exception e = (Exception )errors.get(0); 168 if (e instanceof JRException) 169 { 170 throw (JRException)e; 171 } 172 throw new JRException(e); 173 } 174 175 return this.jasperPrint; 176 } 177 178 179 182 private JRXmlDigester prepareDigester() throws ParserConfigurationException , SAXException 183 { 184 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 185 186 boolean validating = JRProperties.getBooleanProperty(JRProperties.EXPORT_XML_VALIDATION); 187 saxParserFactory.setValidating(validating); 188 189 SAXParser saxParser = saxParserFactory.newSAXParser(); 190 XMLReader xmlReader = saxParser.getXMLReader(); 192 193 xmlReader.setFeature("http://xml.org/sax/features/validation", validating); 194 195 JRXmlDigester digester = new JRXmlDigester(xmlReader); 196 digester.push(this); 197 digester.setErrorHandler(this); 199 digester.setValidating(true); 200 201 202 digester.addFactoryCreate("jasperPrint", JasperPrintFactory.class.getName()); 203 digester.addSetNext("jasperPrint", "setJasperPrint", JasperPrint.class.getName()); 204 205 206 digester.addFactoryCreate("jasperPrint/reportFont", JRReportFontFactory.class.getName()); 207 digester.addSetNext("jasperPrint/reportFont", "addFont", JRReportFont.class.getName()); 208 209 210 digester.addFactoryCreate("jasperPrint/style", JRStyleFactory.class.getName()); 211 digester.addSetNext("jasperPrint/style", "addStyle", JRStyle.class.getName()); 212 213 214 digester.addFactoryCreate("jasperPrint/page", JRPrintPageFactory.class.getName()); 215 digester.addSetNext("jasperPrint/page", "addPage", JRPrintPage.class.getName()); 216 217 218 digester.addFactoryCreate("*/line", JRPrintLineFactory.class.getName()); 219 digester.addSetNext("*/line", "addElement", JRPrintElement.class.getName()); 220 221 222 digester.addFactoryCreate("*/reportElement", JRPrintElementFactory.class.getName()); 223 224 225 digester.addFactoryCreate("*/graphicElement", JRPrintGraphicElementFactory.class.getName()); 226 227 228 digester.addFactoryCreate("*/rectangle", JRPrintRectangleFactory.class.getName()); 229 digester.addSetNext("*/rectangle", "addElement", JRPrintElement.class.getName()); 230 231 232 digester.addFactoryCreate("*/ellipse", JRPrintEllipseFactory.class.getName()); 233 digester.addSetNext("*/ellipse", "addElement", JRPrintElement.class.getName()); 234 235 236 digester.addFactoryCreate("*/image", JRPrintImageFactory.class.getName()); 237 digester.addSetNext("*/image", "addElement", JRPrintElement.class.getName()); 238 239 240 digester.addFactoryCreate("*/box", JRBoxFactory.class.getName()); 241 digester.addSetNext("*/box", "setBox", JRBox.class.getName()); 242 243 244 digester.addFactoryCreate("*/image/imageSource", JRPrintImageSourceFactory.class.getName()); 245 digester.addCallMethod("*/image/imageSource", "setImageSource", 0); 246 247 248 digester.addFactoryCreate("*/text", JRPrintTextFactory.class.getName()); 249 digester.addSetNext("*/text", "addElement", JRPrintElement.class.getName()); 250 SetNestedPropertiesRule textRule = new SetNestedPropertiesRule(new String []{"textContent", "reportElement", "box", "font"}, new String []{"text"}); 251 textRule.setTrimData(false); 252 textRule.setAllowUnknownChildElements(true); 253 digester.addRule("*/text", textRule); 254 255 256 digester.addFactoryCreate("*/text/font", JRPrintFontFactory.class.getName()); 257 digester.addSetNext("*/text/font", "setFont", JRFont.class.getName()); 258 259 addFrameRules(digester); 260 261 addHyperlinkParameterRules(digester); 262 263 return digester; 264 } 265 266 267 private void addFrameRules(JRXmlDigester digester) 268 { 269 digester.addFactoryCreate("*/frame", JRPrintFrameFactory.class.getName()); 270 digester.addSetNext("*/frame", "addElement", JRPrintElement.class.getName()); 271 } 272 273 274 protected void addHyperlinkParameterRules(JRXmlDigester digester) 275 { 276 String parameterPattern = "*/" + JRPrintHyperlinkParameterFactory.ELEMENT_hyperlinkParameter; 277 digester.addFactoryCreate(parameterPattern, JRPrintHyperlinkParameterFactory.class); 278 digester.addSetNext(parameterPattern, "addHyperlinkParameter", JRPrintHyperlinkParameter.class.getName()); 279 280 String parameterValuePattern = parameterPattern + "/" + JRPrintHyperlinkParameterValueFactory.ELEMENT_hyperlinkParameterValue; 281 digester.addFactoryCreate(parameterValuePattern, JRPrintHyperlinkParameterValueFactory.class); 282 digester.addCallMethod(parameterValuePattern, "setData", 0); 283 } 284 285 286 289 public void addError(Exception e) 290 { 291 this.errors.add(e); 292 } 293 294 297 public void error(SAXParseException e) 298 { 299 this.errors.add(e); 300 } 301 302 305 public void fatalError(SAXParseException e) 306 { 307 this.errors.add(e); 308 } 309 310 313 public void warning(SAXParseException e) 314 { 315 this.errors.add(e); 316 } 317 318 319 } 320 | Popular Tags |