KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > wizard > UserChoicesWizardTemplate


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  *
21  *
22  * UserChoicesWizardTemplate.java
23  *
24  * Created on December 19, 2006, 9:49 AM
25  *
26  * To change this template, choose Tools | Template Manager
27  * and open the template in the editor.
28  */

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 JavaDoc;
37 import java.io.FileOutputStream JavaDoc;
38 import java.io.FileWriter JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import javax.xml.transform.TransformerException JavaDoc;
41 import org.apache.xerces.parsers.DOMParser;
42 import org.apache.xpath.XPathAPI;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.w3c.dom.NodeList JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47
48 /**
49  *
50  * @author gtoffoli
51  */

52 public class UserChoicesWizardTemplate {
53     
54     private String JavaDoc name = "";
55     private String JavaDoc templateFile = "";
56     private java.util.List JavaDoc groupExpressions = new java.util.ArrayList JavaDoc();
57     private java.util.List JavaDoc displayFields = new java.util.ArrayList JavaDoc();
58     private String JavaDoc query = "";
59     private String JavaDoc query_language = "";
60     private String JavaDoc iRDatasourceName = "";
61     private boolean saveFieldDescriptions = true;
62     
63     
64     /**
65      * Creates a new instance of UserChoicesWizardTemplate
66      */

67     public UserChoicesWizardTemplate() {
68     }
69
70     public String JavaDoc getName() {
71         return name;
72     }
73
74     public void setName(String JavaDoc name) {
75         this.name = name;
76     }
77
78     public java.util.List JavaDoc getGroupExpressions() {
79         return groupExpressions;
80     }
81
82     public void setGroupExpressions(java.util.List JavaDoc groupExpressions) {
83         this.groupExpressions = groupExpressions;
84     }
85
86     public java.util.List JavaDoc getDisplayFields() {
87         return displayFields;
88     }
89
90     public void setDisplayFields(java.util.List JavaDoc displayFields) {
91         this.displayFields = displayFields;
92     }
93
94     public String JavaDoc getQuery() {
95         return query;
96     }
97
98     public void setQuery(String JavaDoc query) {
99         this.query = query;
100     }
101
102     public String JavaDoc getIRDatasourceName() {
103         return iRDatasourceName;
104     }
105
106     public void setIRDatasourceName(String JavaDoc iRDatasourceName) {
107         this.iRDatasourceName = iRDatasourceName;
108     }
109     
110     public String JavaDoc toString()
111     {
112         if (getName() != null) return getName();
113         return "Unnamed template";
114         
115     }
116     
117     
118     /**
119      * Load the user choices templates from IREPORT_USER_HOME_DIR/wizardUserTemplates.xml
120      * A void list is returned if the file does not exists.
121      */

122     public static java.util.List JavaDoc loadWizardTemplates()
123     {
124          java.util.List JavaDoc templates = new java.util.ArrayList JavaDoc();
125          
126          String JavaDoc locationFileName = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR + java.io.File.separator + "wizardUserTemplates.xml";
127          
128          
129          java.io.File JavaDoc source = new java.io.File JavaDoc( 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                 // Parse the Document
139
// and traverse the DOM
140
try {
141                     
142                     parser.setEntityResolver( new org.xml.sax.EntityResolver JavaDoc() {
143                         /* Code by Teodor Danciu */
144                         public org.xml.sax.InputSource JavaDoc resolveEntity(
145                                 String JavaDoc publicId,
146                                 String JavaDoc systemId
147                                 ) throws SAXException JavaDoc//, java.io.IOException
148
{
149                             org.xml.sax.InputSource JavaDoc inputSource = null;
150                             
151                             if (systemId != null) {
152                                 String JavaDoc 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 JavaDoc(systemId);
158                                 }
159                                 
160                                 
161                                 ClassLoader JavaDoc classLoader = this.getClass().getClassLoader();
162                                 
163                                 java.io.InputStream JavaDoc is = classLoader.getResourceAsStream(dtd);
164                                 if (is != null) {
165                                     inputSource = new org.xml.sax.InputSource JavaDoc(is);
166                                 }
167                                 
168                             }
169                             
170                             return inputSource;
171                         }
172                     });
173                     /* End Code by Teodor Danciu */
174                     InputStream JavaDoc input_source = null;
175                     if ( source instanceof java.io.File JavaDoc ) {
176                         input_source = new FileInputStream JavaDoc((java.io.File JavaDoc)source);
177                         
178                     }
179                     //else if ( source instanceof java.net.URL){
180
//
181
// input_source = ((java.net.URL)source).openStream();
182
//
183
//}
184

185                     parser.parse(new org.xml.sax.InputSource JavaDoc( input_source ));
186                     Document JavaDoc document = parser.getDocument();
187                     
188                     //System.out.println("traverse");
189
Node JavaDoc node = document.getDocumentElement();
190                     
191                     NodeList JavaDoc list = XPathAPI.selectNodeList(node, "/userWizardChoicesTemplateSet/userWizardChoicesTemplate");
192                     Node JavaDoc 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 JavaDoc 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                         // Read fields...
215
NodeList JavaDoc listFields = XPathAPI.selectNodeList(list.item(n), "displayField");
216                         for (int n2=0; n2 < listFields.getLength(); n2++) {
217                             
218                             String JavaDoc fname = "";
219                             String JavaDoc fclass = "";
220                             String JavaDoc 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 JavaDoc e) {
250                     System.err.println(e);
251                 } catch (java.io.IOException JavaDoc e) {
252                     System.err.println(e);
253                 } catch (Exception JavaDoc e) {
254                     System.err.println(e);
255                 }
256             
257             return templates;
258     }
259     
260     /**
261      * Load the user choices templates from IREPORT_USER_HOME_DIR/wizardUserTemplates.xml
262      * A void list is returned if the file does not exists.
263      */

264     public static void storeWizardTemplates(java.util.List JavaDoc list)
265     {
266         String JavaDoc locationFileName = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR + java.io.File.separator + "wizardUserTemplates.xml";
267         java.io.File JavaDoc destination = new java.io.File JavaDoc( locationFileName );
268          
269         StringBuffer JavaDoc output = new StringBuffer JavaDoc();
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 JavaDoc fos = null;
303         try {
304             fos = new FileWriter JavaDoc(destination);
305             fos.write( output.toString() );
306         } catch (Exception JavaDoc ex)
307         {
308             ex.printStackTrace();
309         } finally {
310             if (fos != null)
311             {
312                 try { fos.close(); } catch (Exception JavaDoc ex2) {}
313             }
314         }
315     }
316
317     public String JavaDoc getTemplateFile() {
318         return templateFile;
319     }
320
321     public void setTemplateFile(String JavaDoc templateFile) {
322         this.templateFile = templateFile;
323     }
324
325     public String JavaDoc getQuery_language() {
326         return query_language;
327     }
328
329     public void setQuery_language(String JavaDoc 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