1 29 30 package it.businesslogic.ireport.gui.wizard; 31 32 import it.businesslogic.ireport.JRField; 33 import it.businesslogic.ireport.ReportWriter; 34 import it.businesslogic.ireport.gui.MainFrame; 35 import it.businesslogic.ireport.util.Misc; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.FileWriter ; 39 import java.io.InputStream ; 40 import javax.xml.transform.TransformerException ; 41 import org.apache.xerces.parsers.DOMParser; 42 import org.apache.xpath.XPathAPI; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Node ; 45 import org.w3c.dom.NodeList ; 46 import org.xml.sax.SAXException ; 47 48 52 public class UserChoicesWizardTemplate { 53 54 private String name = ""; 55 private String templateFile = ""; 56 private java.util.List groupExpressions = new java.util.ArrayList (); 57 private java.util.List displayFields = new java.util.ArrayList (); 58 private String query = ""; 59 private String query_language = ""; 60 private String iRDatasourceName = ""; 61 private boolean saveFieldDescriptions = true; 62 63 64 67 public UserChoicesWizardTemplate() { 68 } 69 70 public String getName() { 71 return name; 72 } 73 74 public void setName(String name) { 75 this.name = name; 76 } 77 78 public java.util.List getGroupExpressions() { 79 return groupExpressions; 80 } 81 82 public void setGroupExpressions(java.util.List groupExpressions) { 83 this.groupExpressions = groupExpressions; 84 } 85 86 public java.util.List getDisplayFields() { 87 return displayFields; 88 } 89 90 public void setDisplayFields(java.util.List displayFields) { 91 this.displayFields = displayFields; 92 } 93 94 public String getQuery() { 95 return query; 96 } 97 98 public void setQuery(String query) { 99 this.query = query; 100 } 101 102 public String getIRDatasourceName() { 103 return iRDatasourceName; 104 } 105 106 public void setIRDatasourceName(String iRDatasourceName) { 107 this.iRDatasourceName = iRDatasourceName; 108 } 109 110 public String toString() 111 { 112 if (getName() != null) return getName(); 113 return "Unnamed template"; 114 115 } 116 117 118 122 public static java.util.List loadWizardTemplates() 123 { 124 java.util.List templates = new java.util.ArrayList (); 125 126 String locationFileName = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR + java.io.File.separator + "wizardUserTemplates.xml"; 127 128 129 java.io.File source = new java.io.File ( locationFileName ); 130 if (!source.exists()) 131 { 132 System.out.println("locationFileName does not exisit"); 133 return templates; 134 } 135 136 137 DOMParser parser = new DOMParser(); 138 try { 141 142 parser.setEntityResolver( new org.xml.sax.EntityResolver () { 143 144 public org.xml.sax.InputSource resolveEntity( 145 String publicId, 146 String systemId 147 ) throws SAXException { 149 org.xml.sax.InputSource inputSource = null; 150 151 if (systemId != null) { 152 String dtd = null; 153 154 if ( systemId.equals("http://ireport.sourceforge.net/dtds/userWizardChoicesTemplate.dtd") ) { 155 dtd = "it/businesslogic/ireport/dtds/userWizardChoicesTemplate.dtd"; 156 } else { 157 return new org.xml.sax.InputSource (systemId); 158 } 159 160 161 ClassLoader classLoader = this.getClass().getClassLoader(); 162 163 java.io.InputStream is = classLoader.getResourceAsStream(dtd); 164 if (is != null) { 165 inputSource = new org.xml.sax.InputSource (is); 166 } 167 168 } 169 170 return inputSource; 171 } 172 }); 173 174 InputStream input_source = null; 175 if ( source instanceof java.io.File ) { 176 input_source = new FileInputStream ((java.io.File )source); 177 178 } 179 185 parser.parse(new org.xml.sax.InputSource ( input_source )); 186 Document document = parser.getDocument(); 187 188 Node node = document.getDocumentElement(); 190 191 NodeList list = XPathAPI.selectNodeList(node, "/userWizardChoicesTemplateSet/userWizardChoicesTemplate"); 192 Node childnode = null; 193 194 for (int n=0; n < list.getLength(); n++) { 195 UserChoicesWizardTemplate template = new UserChoicesWizardTemplate(); 196 childnode = XPathAPI.selectSingleNode(list.item(n), "@name"); 197 if (childnode != null) template.setName(childnode.getNodeValue()); 198 199 childnode = XPathAPI.selectSingleNode(list.item(n), "@saveFieldDescriptions"); 200 if (childnode != null) template.setSaveFieldDescriptions(childnode.getNodeValue().equals("true")); 201 202 childnode = XPathAPI.selectSingleNode(list.item(n), "templateFile/text()"); 203 if (childnode != null) template.setTemplateFile( childnode.getNodeValue()); 204 205 NodeList listExpressions = null; 206 listExpressions = XPathAPI.selectNodeList(list.item(n), "groupExpression"); 207 208 for (int n2=0; n2 < listExpressions.getLength(); n2++) { 209 210 childnode = XPathAPI.selectSingleNode(listExpressions.item(n2), "text()"); 211 if (childnode != null) template.getGroupExpressions().add( childnode.getNodeValue()); 212 213 } 214 NodeList listFields = XPathAPI.selectNodeList(list.item(n), "displayField"); 216 for (int n2=0; n2 < listFields.getLength(); n2++) { 217 218 String fname = ""; 219 String fclass = ""; 220 String fdesc = ""; 221 222 childnode = XPathAPI.selectSingleNode(listFields.item(n2), "@name"); 223 if (childnode != null) fname = childnode.getNodeValue(); 224 225 childnode = XPathAPI.selectSingleNode(listFields.item(n2), "@class"); 226 if (childnode != null) fclass = childnode.getNodeValue(); 227 228 childnode = XPathAPI.selectSingleNode(listFields.item(n2), "fieldDescription/text()"); 229 if (childnode != null) fdesc = childnode.getNodeValue(); 230 231 JRField jrField = new JRField(fname, fclass); 232 jrField.setDescription(fdesc); 233 234 template.getDisplayFields().add(jrField); 235 236 } 237 238 childnode = XPathAPI.selectSingleNode(list.item(n), "query/text()"); 239 if (childnode != null) template.setQuery( childnode.getNodeValue()); 240 241 childnode = XPathAPI.selectSingleNode(list.item(n), "query/@language"); 242 if (childnode != null) template.setQuery_language( childnode.getNodeValue()); 243 244 childnode = XPathAPI.selectSingleNode(list.item(n), "iRDatasourceName/text()"); 245 if (childnode != null) template.setIRDatasourceName( childnode.getNodeValue()); 246 247 templates.add(template); 248 } 249 } catch (SAXException e) { 250 System.err.println(e); 251 } catch (java.io.IOException e) { 252 System.err.println(e); 253 } catch (Exception e) { 254 System.err.println(e); 255 } 256 257 return templates; 258 } 259 260 264 public static void storeWizardTemplates(java.util.List list) 265 { 266 String locationFileName = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR + java.io.File.separator + "wizardUserTemplates.xml"; 267 java.io.File destination = new java.io.File ( locationFileName ); 268 269 StringBuffer output = new StringBuffer (); 270 271 output.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 272 output.append("<userWizardChoicesTemplateSet>\n"); 273 274 for (int i=0; i<list.size(); ++i) 275 { 276 UserChoicesWizardTemplate template = (UserChoicesWizardTemplate)list.get(i); 277 output.append("\t<userWizardChoicesTemplate name=\"" + Misc.xmlEscape( template.getName() ) + "\" saveFieldDescriptions=\"" + template.isSaveFieldDescriptions() + "\">\n"); 278 output.append("\t\t<templateFile>" + ReportWriter.getCDATAString( template.getTemplateFile(), 2) + "</templateFile>\n"); 279 280 for (int k=0; k<template.getGroupExpressions().size(); ++k) 281 output.append("\t\t<groupExpression>" + ReportWriter.getCDATAString( template.getGroupExpressions().get(k)+"", 2) + "</groupExpression>\n"); 282 283 for (int k=0; k<template.getDisplayFields().size(); ++k) 284 { 285 JRField field = (JRField)template.getDisplayFields().get(k); 286 output.append("\t\t<displayField name=\"" + Misc.xmlEscape( field.getName() ) + "\" class=\"" + Misc.xmlEscape( field.getClassType() ) + "\">\n"); 287 output.append("\t\t<fieldDescription>" + ReportWriter.getCDATAString( field.getDescription(), 2) + "</fieldDescription>\n"); 288 output.append("\t\t</displayField>\n"); 289 } 290 291 if (template.getQuery() != null && template.getQuery().length() > 0) 292 output.append("\t\t<query language=\"" + Misc.xmlEscape(template.getQuery_language() ) + "\">" + ReportWriter.getCDATAString( template.getQuery(), 2) + "</query>\n"); 293 294 if (template.getIRDatasourceName() != null && template.getIRDatasourceName().length() > 0) 295 output.append("\t\t<iRDatasourceName>" + ReportWriter.getCDATAString( template.getIRDatasourceName(), 2) + "</iRDatasourceName>\n"); 296 297 output.append("\t</userWizardChoicesTemplate>\n"); 298 } 299 300 output.append("</userWizardChoicesTemplateSet>\n"); 301 302 FileWriter fos = null; 303 try { 304 fos = new FileWriter (destination); 305 fos.write( output.toString() ); 306 } catch (Exception ex) 307 { 308 ex.printStackTrace(); 309 } finally { 310 if (fos != null) 311 { 312 try { fos.close(); } catch (Exception ex2) {} 313 } 314 } 315 } 316 317 public String getTemplateFile() { 318 return templateFile; 319 } 320 321 public void setTemplateFile(String templateFile) { 322 this.templateFile = templateFile; 323 } 324 325 public String getQuery_language() { 326 return query_language; 327 } 328 329 public void setQuery_language(String query_language) { 330 this.query_language = query_language; 331 } 332 333 public boolean isSaveFieldDescriptions() { 334 return saveFieldDescriptions; 335 } 336 337 public void setSaveFieldDescriptions(boolean saveFieldDescriptions) { 338 this.saveFieldDescriptions = saveFieldDescriptions; 339 } 340 } 341 | Popular Tags |