001: /*
002: * Copyright (C) 2005-2007 JasperSoft http://www.jaspersoft.com
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed WITHOUT ANY WARRANTY; and without the
010: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: * See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
015: * or write to:
016: *
017: * Free Software Foundation, Inc.,
018: * 59 Temple Place - Suite 330,
019: * Boston, MA USA 02111-1307
020: *
021: *
022: * UserChoicesWizardTemplate.java
023: *
024: * Created on December 19, 2006, 9:49 AM
025: *
026: * To change this template, choose Tools | Template Manager
027: * and open the template in the editor.
028: */
029:
030: package it.businesslogic.ireport.gui.wizard;
031:
032: import it.businesslogic.ireport.JRField;
033: import it.businesslogic.ireport.ReportWriter;
034: import it.businesslogic.ireport.gui.MainFrame;
035: import it.businesslogic.ireport.util.Misc;
036: import java.io.FileInputStream;
037: import java.io.FileOutputStream;
038: import java.io.FileWriter;
039: import java.io.InputStream;
040: import javax.xml.transform.TransformerException;
041: import org.apache.xerces.parsers.DOMParser;
042: import org.apache.xpath.XPathAPI;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Node;
045: import org.w3c.dom.NodeList;
046: import org.xml.sax.SAXException;
047:
048: /**
049: *
050: * @author gtoffoli
051: */
052: public class UserChoicesWizardTemplate {
053:
054: private String name = "";
055: private String templateFile = "";
056: private java.util.List groupExpressions = new java.util.ArrayList();
057: private java.util.List displayFields = new java.util.ArrayList();
058: private String query = "";
059: private String query_language = "";
060: private String iRDatasourceName = "";
061: private boolean saveFieldDescriptions = true;
062:
063: /**
064: * Creates a new instance of UserChoicesWizardTemplate
065: */
066: public UserChoicesWizardTemplate() {
067: }
068:
069: public String getName() {
070: return name;
071: }
072:
073: public void setName(String name) {
074: this .name = name;
075: }
076:
077: public java.util.List getGroupExpressions() {
078: return groupExpressions;
079: }
080:
081: public void setGroupExpressions(java.util.List groupExpressions) {
082: this .groupExpressions = groupExpressions;
083: }
084:
085: public java.util.List getDisplayFields() {
086: return displayFields;
087: }
088:
089: public void setDisplayFields(java.util.List displayFields) {
090: this .displayFields = displayFields;
091: }
092:
093: public String getQuery() {
094: return query;
095: }
096:
097: public void setQuery(String query) {
098: this .query = query;
099: }
100:
101: public String getIRDatasourceName() {
102: return iRDatasourceName;
103: }
104:
105: public void setIRDatasourceName(String iRDatasourceName) {
106: this .iRDatasourceName = iRDatasourceName;
107: }
108:
109: public String toString() {
110: if (getName() != null)
111: return getName();
112: return "Unnamed template";
113:
114: }
115:
116: /**
117: * Load the user choices templates from IREPORT_USER_HOME_DIR/wizardUserTemplates.xml
118: * A void list is returned if the file does not exists.
119: */
120: public static java.util.List loadWizardTemplates() {
121: java.util.List templates = new java.util.ArrayList();
122:
123: String locationFileName = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR
124: + java.io.File.separator + "wizardUserTemplates.xml";
125:
126: java.io.File source = new java.io.File(locationFileName);
127: if (!source.exists()) {
128: System.out.println("locationFileName does not exisit");
129: return templates;
130: }
131:
132: DOMParser parser = new DOMParser();
133: // Parse the Document
134: // and traverse the DOM
135: try {
136:
137: parser.setEntityResolver(new org.xml.sax.EntityResolver() {
138: /* Code by Teodor Danciu */
139: public org.xml.sax.InputSource resolveEntity(
140: String publicId, String systemId)
141: throws SAXException//, java.io.IOException
142: {
143: org.xml.sax.InputSource inputSource = null;
144:
145: if (systemId != null) {
146: String dtd = null;
147:
148: if (systemId
149: .equals("http://ireport.sourceforge.net/dtds/userWizardChoicesTemplate.dtd")) {
150: dtd = "it/businesslogic/ireport/dtds/userWizardChoicesTemplate.dtd";
151: } else {
152: return new org.xml.sax.InputSource(systemId);
153: }
154:
155: ClassLoader classLoader = this .getClass()
156: .getClassLoader();
157:
158: java.io.InputStream is = classLoader
159: .getResourceAsStream(dtd);
160: if (is != null) {
161: inputSource = new org.xml.sax.InputSource(
162: is);
163: }
164:
165: }
166:
167: return inputSource;
168: }
169: });
170: /* End Code by Teodor Danciu */
171: InputStream input_source = null;
172: if (source instanceof java.io.File) {
173: input_source = new FileInputStream(
174: (java.io.File) source);
175:
176: }
177: //else if ( source instanceof java.net.URL){
178: //
179: // input_source = ((java.net.URL)source).openStream();
180: //
181: //}
182:
183: parser.parse(new org.xml.sax.InputSource(input_source));
184: Document document = parser.getDocument();
185:
186: //System.out.println("traverse");
187: Node node = document.getDocumentElement();
188:
189: NodeList list = XPathAPI
190: .selectNodeList(node,
191: "/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),
197: "@name");
198: if (childnode != null)
199: template.setName(childnode.getNodeValue());
200:
201: childnode = XPathAPI.selectSingleNode(list.item(n),
202: "@saveFieldDescriptions");
203: if (childnode != null)
204: template.setSaveFieldDescriptions(childnode
205: .getNodeValue().equals("true"));
206:
207: childnode = XPathAPI.selectSingleNode(list.item(n),
208: "templateFile/text()");
209: if (childnode != null)
210: template.setTemplateFile(childnode.getNodeValue());
211:
212: NodeList listExpressions = null;
213: listExpressions = XPathAPI.selectNodeList(list.item(n),
214: "groupExpression");
215:
216: for (int n2 = 0; n2 < listExpressions.getLength(); n2++) {
217:
218: childnode = XPathAPI.selectSingleNode(
219: listExpressions.item(n2), "text()");
220: if (childnode != null)
221: template.getGroupExpressions().add(
222: childnode.getNodeValue());
223:
224: }
225: // Read fields...
226: NodeList listFields = XPathAPI.selectNodeList(list
227: .item(n), "displayField");
228: for (int n2 = 0; n2 < listFields.getLength(); n2++) {
229:
230: String fname = "";
231: String fclass = "";
232: String fdesc = "";
233:
234: childnode = XPathAPI.selectSingleNode(listFields
235: .item(n2), "@name");
236: if (childnode != null)
237: fname = childnode.getNodeValue();
238:
239: childnode = XPathAPI.selectSingleNode(listFields
240: .item(n2), "@class");
241: if (childnode != null)
242: fclass = childnode.getNodeValue();
243:
244: childnode = XPathAPI.selectSingleNode(listFields
245: .item(n2), "fieldDescription/text()");
246: if (childnode != null)
247: fdesc = childnode.getNodeValue();
248:
249: JRField jrField = new JRField(fname, fclass);
250: jrField.setDescription(fdesc);
251:
252: template.getDisplayFields().add(jrField);
253:
254: }
255:
256: childnode = XPathAPI.selectSingleNode(list.item(n),
257: "query/text()");
258: if (childnode != null)
259: template.setQuery(childnode.getNodeValue());
260:
261: childnode = XPathAPI.selectSingleNode(list.item(n),
262: "query/@language");
263: if (childnode != null)
264: template
265: .setQuery_language(childnode.getNodeValue());
266:
267: childnode = XPathAPI.selectSingleNode(list.item(n),
268: "iRDatasourceName/text()");
269: if (childnode != null)
270: template.setIRDatasourceName(childnode
271: .getNodeValue());
272:
273: templates.add(template);
274: }
275: } catch (SAXException e) {
276: System.err.println(e);
277: } catch (java.io.IOException e) {
278: System.err.println(e);
279: } catch (Exception e) {
280: System.err.println(e);
281: }
282:
283: return templates;
284: }
285:
286: /**
287: * Load the user choices templates from IREPORT_USER_HOME_DIR/wizardUserTemplates.xml
288: * A void list is returned if the file does not exists.
289: */
290: public static void storeWizardTemplates(java.util.List list) {
291: String locationFileName = MainFrame.getMainInstance().IREPORT_USER_HOME_DIR
292: + java.io.File.separator + "wizardUserTemplates.xml";
293: java.io.File destination = new java.io.File(locationFileName);
294:
295: StringBuffer output = new StringBuffer();
296:
297: output.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
298: output.append("<userWizardChoicesTemplateSet>\n");
299:
300: for (int i = 0; i < list.size(); ++i) {
301: UserChoicesWizardTemplate template = (UserChoicesWizardTemplate) list
302: .get(i);
303: output.append("\t<userWizardChoicesTemplate name=\""
304: + Misc.xmlEscape(template.getName())
305: + "\" saveFieldDescriptions=\""
306: + template.isSaveFieldDescriptions() + "\">\n");
307: output.append("\t\t<templateFile>"
308: + ReportWriter.getCDATAString(template
309: .getTemplateFile(), 2)
310: + "</templateFile>\n");
311:
312: for (int k = 0; k < template.getGroupExpressions().size(); ++k)
313: output.append("\t\t<groupExpression>"
314: + ReportWriter.getCDATAString(template
315: .getGroupExpressions().get(k)
316: + "", 2) + "</groupExpression>\n");
317:
318: for (int k = 0; k < template.getDisplayFields().size(); ++k) {
319: JRField field = (JRField) template.getDisplayFields()
320: .get(k);
321: output.append("\t\t<displayField name=\""
322: + Misc.xmlEscape(field.getName())
323: + "\" class=\""
324: + Misc.xmlEscape(field.getClassType())
325: + "\">\n");
326: output.append("\t\t<fieldDescription>"
327: + ReportWriter.getCDATAString(field
328: .getDescription(), 2)
329: + "</fieldDescription>\n");
330: output.append("\t\t</displayField>\n");
331: }
332:
333: if (template.getQuery() != null
334: && template.getQuery().length() > 0)
335: output.append("\t\t<query language=\""
336: + Misc.xmlEscape(template.getQuery_language())
337: + "\">"
338: + ReportWriter.getCDATAString(template
339: .getQuery(), 2) + "</query>\n");
340:
341: if (template.getIRDatasourceName() != null
342: && template.getIRDatasourceName().length() > 0)
343: output.append("\t\t<iRDatasourceName>"
344: + ReportWriter.getCDATAString(template
345: .getIRDatasourceName(), 2)
346: + "</iRDatasourceName>\n");
347:
348: output.append("\t</userWizardChoicesTemplate>\n");
349: }
350:
351: output.append("</userWizardChoicesTemplateSet>\n");
352:
353: FileWriter fos = null;
354: try {
355: fos = new FileWriter(destination);
356: fos.write(output.toString());
357: } catch (Exception ex) {
358: ex.printStackTrace();
359: } finally {
360: if (fos != null) {
361: try {
362: fos.close();
363: } catch (Exception ex2) {
364: }
365: }
366: }
367: }
368:
369: public String getTemplateFile() {
370: return templateFile;
371: }
372:
373: public void setTemplateFile(String templateFile) {
374: this .templateFile = templateFile;
375: }
376:
377: public String getQuery_language() {
378: return query_language;
379: }
380:
381: public void setQuery_language(String query_language) {
382: this .query_language = query_language;
383: }
384:
385: public boolean isSaveFieldDescriptions() {
386: return saveFieldDescriptions;
387: }
388:
389: public void setSaveFieldDescriptions(boolean saveFieldDescriptions) {
390: this.saveFieldDescriptions = saveFieldDescriptions;
391: }
392: }
|