Source Code Cross Referenced for JasperTemplate.java in  » Report » iReport-2.0.5 » it » businesslogic » ireport » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » iReport 2.0.5 » it.businesslogic.ireport 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JasperTemplate.java
003:         * 
004:         * Created on Aug 21, 2007, 3:55:35 PM
005:         * 
006:         * To change this template, choose Tools | Template Manager
007:         * and open the template in the editor.
008:         */
009:
010:        /*
011:         * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
012:         *
013:         * This program is free software; you can redistribute it and/or modify
014:         * it under the terms of the GNU General Public License as published by
015:         * the Free Software Foundation; either version 2 of the License, or
016:         * (at your option) any later version.
017:         *
018:         * This program is distributed WITHOUT ANY WARRANTY; and without the
019:         * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
020:         * See the GNU General Public License for more details.
021:         *
022:         * You should have received a copy of the GNU General Public License
023:         * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
024:         * or write to:
025:         *
026:         * Free Software Foundation, Inc.,
027:         * 59 Temple Place - Suite 330,
028:         * Boston, MA  USA  02111-1307
029:         */
030:
031:        package it.businesslogic.ireport;
032:
033:        import it.businesslogic.ireport.gui.MainFrame;
034:        import it.businesslogic.ireport.util.Misc;
035:        import java.io.File;
036:        import java.io.IOException;
037:        import java.io.PrintWriter;
038:        import java.net.URL;
039:        import java.util.ArrayList;
040:        import java.util.List;
041:        import org.apache.xerces.parsers.DOMParser;
042:        import org.w3c.dom.Document;
043:        import org.w3c.dom.NamedNodeMap;
044:        import org.w3c.dom.Node;
045:        import org.w3c.dom.NodeList;
046:        import org.w3c.dom.NamedNodeMap;
047:        import org.xml.sax.SAXException;
048:
049:        /**
050:         *
051:         *  A JasperTemplate is a container of a set of predefined styles normally contained in external files.
052:         * @author gtoffoli
053:         */
054:        public class JasperTemplate {
055:
056:            /**
057:             *  If the JasperTemplate is stored in a file, save the complete path here...
058:             */
059:            private String filename = null;
060:
061:            public String getFilename() {
062:                return filename;
063:            }
064:
065:            public void setFilename(String filename) {
066:                this .filename = filename;
067:            }
068:
069:            private JasperTemplate parent = null;
070:
071:            public JasperTemplate getParent() {
072:                return parent;
073:            }
074:
075:            public void setParent(JasperTemplate parent) {
076:                this .parent = parent;
077:            }
078:
079:            public JasperTemplate() {
080:                // Try to load this expression....
081:                reloadTemplate();
082:            }
083:
084:            public JasperTemplate(String expression) {
085:                this .setExpression(expression);
086:                // Try to load this expression....
087:            }
088:
089:            /**
090:             * This method tries to resolve the current expression as file...
091:             * If the file is correctly loaded, filename is set to the file path,
092:             * otherwise it is set to null.
093:             */
094:            public void reloadTemplate() {
095:                if (filename == null)
096:                    return;
097:
098:                try {
099:                    /*    
100:                        File f = new File(getExpression());
101:                        if (!f.exists())
102:                        {
103:                            // try to resolve all as resource...
104:                            URL url = MainFrame.getMainInstance().getReportClassLoader().getResource(getExpression());
105:                            if (url != null)
106:                            {
107:                                filename = url.getFile();
108:                            }
109:                        }
110:                        else
111:                        {
112:                            filename = f.toString();
113:                        }
114:                     */
115:                    readTemplateFile(filename);
116:
117:                } catch (Exception ex) {
118:                    ex.printStackTrace();
119:                }
120:
121:            }
122:
123:            private void readTemplateFile(String xmlFile) throws Exception {
124:                this .getStyles().clear();
125:                this .getTemplates().clear();
126:
127:                File ffile = new File(xmlFile);
128:                java.io.InputStream input_source = null;
129:
130:                if (ffile.exists()) {
131:                    input_source = new java.io.FileInputStream(ffile);
132:                    try {
133:                        DOMParser parser = new DOMParser();
134:
135:                        parser
136:                                .setEntityResolver(new org.xml.sax.EntityResolver() {
137:
138:                                    /* Code by Teodor Danciu */
139:                                    public org.xml.sax.InputSource resolveEntity(
140:                                            String publicId, String systemId)
141:                                            throws SAXException, IOException {
142:                                        org.xml.sax.InputSource inputSource = null;
143:
144:                                        //System.out.println("Resolving : " + publicId + " " + systemId);
145:
146:                                        if (systemId != null) {
147:                                            String dtd = null;
148:
149:                                            if (systemId
150:                                                    .equals("http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd")
151:                                                    || systemId
152:                                                            .equals("http://www.jasperreports.com/dtds/jaspertemplate.dtd")) {
153:                                                //dtd = "dori/jasper/engine/dtds/jasperreport.dtd";
154:                                                dtd = "net/sf/jasperreports/engine/dtds/jaspertemplate.dtd";
155:
156:                                            }
157:
158:                                            ClassLoader classLoader = this 
159:                                                    .getClass()
160:                                                    .getClassLoader();
161:                                            java.net.URL url = null;
162:
163:                                            if (classLoader != null) {
164:                                                url = classLoader
165:                                                        .getResource(dtd);
166:                                                if (url == null) {
167:                                                    classLoader = this 
168:                                                            .getClass()
169:                                                            .getClassLoader();
170:                                                }
171:
172:                                            } else {
173:                                                // url is certainly null
174:                                                // classLoader stays null
175:                                            }
176:
177:                                            java.io.InputStream is = classLoader
178:                                                    .getResourceAsStream(dtd);
179:                                            if (is != null) {
180:                                                java.io.InputStreamReader isr = new java.io.InputStreamReader(
181:                                                        is);
182:                                                inputSource = new org.xml.sax.InputSource(
183:                                                        isr);
184:                                            } else {
185:                                                // dtd could not be found
186:                                                // this error occurs e.g. when the package name / path changes to this dtd
187:                                                // the error will be caught by MainFrame.openFile() en the report file won't open
188:                                                throw new java.io.IOException(
189:                                                        "iReport Internal error in JasperTemplate.java: Could not find: "
190:                                                                + dtd + "\n");
191:                                            }
192:                                        }
193:                                        return inputSource;
194:
195:                                    }
196:                                });
197:
198:                        org.xml.sax.InputSource input_sss = new org.xml.sax.InputSource(
199:                                input_source);
200:                        input_sss.setSystemId("file:///" + xmlFile);
201:
202:                        parser.parse(input_sss);
203:                        //System.out.println(parser.getFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes"));
204:
205:                        Document document = parser.getDocument();
206:                        Node node = document.getDocumentElement();
207:
208:                        if (node.getNodeType() == Node.ELEMENT_NODE
209:                                && node.getNodeName().equals("jasperTemplate")) {
210:
211:                            NodeList children = node.getChildNodes();
212:                            if (children != null) {
213:                                for (int i = 0; i < children.getLength(); i++) {
214:                                    Node child = (Node) children.item(i);
215:
216:                                    if (child.getNodeType() == Node.ELEMENT_NODE
217:                                            && child.getNodeName().equals(
218:                                                    "style")) {
219:                                        getStyles().add(readStyle(child));
220:                                    } else if (child.getNodeType() == Node.ELEMENT_NODE
221:                                            && child.getNodeName().equals(
222:                                                    "template")) {
223:
224:                                        String newTemplate = Report
225:                                                .readPCDATA(child);
226:
227:                                        JasperTemplate jt = new JasperTemplate(
228:                                                newTemplate);
229:                                        getTemplates().add(jt);
230:                                        jt.setParent(this );
231:                                    }
232:                                }
233:                            }
234:                        }
235:
236:                    } catch (Exception ex) {
237:                        ex.printStackTrace();
238:                    }
239:                }
240:            }
241:
242:            private TemplateStyle readStyle(Node styleNode) {
243:                TemplateStyle style = new TemplateStyle();
244:                style.setJasperTemplate(this );
245:                NamedNodeMap nnm = styleNode.getAttributes();
246:
247:                for (int i = 0; i < Style.JRXMLStyleAttributes.length; ++i) {
248:                    if (nnm.getNamedItem(Style.JRXMLStyleAttributes[i]) != null) {
249:                        style.getAttributes().put(
250:                                Style.JRXMLStyleAttributes[i],
251:                                nnm.getNamedItem(Style.JRXMLStyleAttributes[i])
252:                                        .getNodeValue());
253:                    }
254:                }
255:                return style;
256:            }
257:
258:            public boolean canWrite() {
259:                try {
260:                    if (this .filename == null)
261:                        return false;
262:                    File f = new File(filename);
263:                    return f.canWrite();
264:
265:                } catch (Exception ex) {
266:                    return false;
267:                }
268:            }
269:
270:            public void saveTemplateFile() throws Exception {
271:                if (this .filename == null)
272:                    throw new Exception("Unable to save this template!");
273:                saveTemplateFile(this .filename);
274:            }
275:
276:            private void saveTemplateFile(String xmlFile) throws Exception {
277:                PrintWriter pw = new java.io.PrintWriter(xmlFile);
278:                this .filename = xmlFile;
279:
280:                try {
281:                    pw.println("<?xml version=\"1.0\"?>");
282:                    pw
283:                            .println("<!DOCTYPE jasperTemplate PUBLIC \"-//JasperReports//DTD Template//EN\" \"http://jasperreports.sourceforge.net/dtds/jaspertemplate.dtd\">");
284:                    pw.println("<!-- Created by iReport -->");
285:                    pw.println("<jasperTemplate>");
286:                    for (JasperTemplate jt : getTemplates()) {
287:                        pw.println("\t<template><![CDATA[" + jt.getExpression()
288:                                + "]]></template>");
289:                    }
290:
291:                    for (TemplateStyle style : getStyles()) {
292:                        String tabs = "\t";
293:
294:                        pw.println(tabs + "<style ");
295:                        for (int i = 0; i < Style.JRXMLStyleAttributes.length; ++i) {
296:                            if (style.getAttributes().containsKey(
297:                                    Style.JRXMLStyleAttributes[i])
298:                                    && style.getAttributes().get(
299:                                            Style.JRXMLStyleAttributes[i]) != null) {
300:                                Object obj = style.getAttributes().get(
301:                                        Style.JRXMLStyleAttributes[i]);
302:                                String value = "" + obj;
303:                                if (obj instanceof  java.awt.Color) {
304:                                    value = Report
305:                                            .writeEncodedColor((java.awt.Color) obj);
306:                                }
307:                                if (Style.JRXMLStyleAttributes[i].toLowerCase()
308:                                        .endsWith("color")
309:                                        && value.startsWith("[")) {
310:                                    // The color is in the form [r,g,b]...
311:                                    try {
312:                                        value = Report
313:                                                .writeEncodedColor(it.businesslogic.ireport.gui.sheet.ColorSelectorPanel
314:                                                        .parseColorString(value));
315:                                    } catch (Exception ex) {
316:                                        value = "black";
317:                                    }
318:                                }
319:                                if (Style.JRXMLStyleAttributes[i]
320:                                        .equals("style")) {
321:                                    if (value == null
322:                                            || value.trim().length() == 0)
323:                                        continue;
324:                                }
325:                                pw.println(tabs + "\t"
326:                                        + Style.JRXMLStyleAttributes[i] + "=\""
327:                                        + value + "\"");
328:                            }
329:                        }
330:                        pw.println(tabs + "/>");
331:                    }
332:
333:                    pw.println("</jasperTemplate>");
334:
335:                } finally {
336:                    pw.close();
337:                }
338:
339:            }
340:
341:            /**
342:             * List of the styles contained in this template
343:             */
344:            private ArrayList<TemplateStyle> styles = new ArrayList<TemplateStyle>();
345:
346:            /**
347:             * List of sub-templates
348:             */
349:            private ArrayList<JasperTemplate> templates = new ArrayList<JasperTemplate>();
350:
351:            private String expression = "";
352:
353:            public String getExpression() {
354:                return expression;
355:            }
356:
357:            public void setExpression(String expression) {
358:                this .expression = expression;
359:            }
360:
361:            public ArrayList<JasperTemplate> getTemplates() {
362:                return templates;
363:            }
364:
365:            public void setTemplates(ArrayList<JasperTemplate> templates) {
366:                this .templates = templates;
367:            }
368:
369:            public ArrayList<TemplateStyle> getStyles() {
370:                return styles;
371:            }
372:
373:            public void setStyles(ArrayList<TemplateStyle> styles) {
374:                this .styles = styles;
375:            }
376:
377:            public String toString() {
378:                if (getFilename() != null && getFilename().length() > 0) {
379:                    File f = new File(getFilename());
380:                    return f.getName();
381:                }
382:
383:                return getExpression();
384:            }
385:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.