Source Code Cross Referenced for XXmlTemplateProcessor.java in  » XML-UI » xui32 » com » xoetrope » template » 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 » XML UI » xui32 » com.xoetrope.template 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.xoetrope.template;
002:
003:        import java.io.File;
004:        import java.io.FileOutputStream;
005:        import java.io.FileReader;
006:        import java.io.FileWriter;
007:        import java.io.IOException;
008:        import java.util.Enumeration;
009:        import java.util.Vector;
010:        import net.xoetrope.xml.XmlElement;
011:        import net.xoetrope.xml.XmlSource;
012:        import net.xoetrope.xml.jaxp.JaxpXmlWriter;
013:        import net.xoetrope.xml.nanoxml.NanoXmlElement;
014:        import net.xoetrope.xml.nanoxml.NanoXmlWriter;
015:        import net.xoetrope.xui.XProject;
016:
017:        /**
018:         * <p>A template processor for parameterized XML files.</p> 
019:         * <p>Individual elements can be 
020:         * excludes/included in the copy of the template file by adding the 'te_include'
021:         * attribute to those elements. The attribute value is then used to query the
022:         * template engine's includes method. The method's return value dictates whether
023:         * or not the element is included.</p>
024:         * <p>
025:         * A <code>&lt;Repeat&gt;</code> element can be included so that elements can be 
026:         * repeated. The element is repeated while the <code>while</code> attribute 
027:         * evaluates to <code>true</code>. The same evaluation mechansim as is used
028:         * within XUI's page is employed and the <code>XTemplateEngine</code>'s 
029:         * subclasses can implement the callback methods. As each item is included the
030:         * loop counter is processed and this value can be used with expressoions as the
031:         * value <code>{N}</code>. Each attribute is evaluated so that the necessary
032:         * substitutions can be made. This a field name by be coded as 
033:         * <code>name="addressEdit{N}"</code> such that the name is different for each
034:         * element included.
035:         * </p>
036:         *
037:         * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
038:         * the GNU Public License (GPL), please see license.txt for more details. If
039:         * you make commercial use of this software you must purchase a commercial
040:         * license from Xoetrope.</p>
041:         * <p> $Revision: 1.6 $</p>
042:         */
043:        public class XXmlTemplateProcessor extends XTemplateProcessor {
044:            private XmlElement xml;
045:
046:            public XXmlTemplateProcessor(XProject proj, XTemplateEngine te) {
047:                super (proj, te);
048:            }
049:
050:            public boolean process(String sourceName, String targetName,
051:                    int processingType) {
052:                sourceFile = new File(sourceFileName = sourceName);
053:                targetFile = new File(targetFileName = targetName);
054:
055:                boolean rc = loadFile();
056:                if (rc) {
057:                    rc &= includeElements(xml);
058:                    rc &= excludeElements(xml);
059:                    rc &= repeatElements(null, xml);
060:                    rc &= insertAttribute(xml);
061:                    rc &= copyResources(xml);
062:
063:                    if (rc)
064:                        rc = saveFile();
065:                }
066:
067:                return rc;
068:            }
069:
070:            /**
071:             * Read the XML template
072:             * @return true if the processing completed successfully, otherwise false is returned
073:             */
074:            protected boolean loadFile() {
075:                try {
076:                    xml = XmlSource.read(new FileReader(sourceFileName));
077:                } catch (Exception ex) {
078:                    ex.printStackTrace();
079:                    return false;
080:                }
081:
082:                return true;
083:            }
084:
085:            /**
086:             * Save the XML template
087:             * @return true if the processing completed successfully, otherwise false is returned
088:             */
089:            protected boolean saveFile() {
090:                FileWriter fw = null;
091:                FileOutputStream fos = null;
092:                try {
093:                    targetFile.getParentFile().mkdirs();
094:
095:                    if (xml instanceof  NanoXmlElement) {
096:                        fw = new FileWriter(targetFile);
097:                        NanoXmlWriter xmlWriter = new NanoXmlWriter(fw);
098:                        xmlWriter.write(xml, true, 4);
099:
100:                        fw.flush();
101:                        fw.close();
102:                    } else {
103:                        fos = new FileOutputStream(targetFile);
104:                        JaxpXmlWriter xmlWriter = new JaxpXmlWriter();
105:                        xmlWriter.setOutputStream(fos);
106:                        xmlWriter.write(xml, true, 4);
107:
108:                        fos.close();
109:                    }
110:
111:                    return true;
112:                } catch (Exception ex) {
113:                    ex.printStackTrace();
114:                } finally {
115:                    if (fw != null) {
116:                        try {
117:                            fw.close();
118:                        } catch (IOException ex) {
119:                            ex.printStackTrace();
120:                        }
121:                    } else if (fos != null) {
122:                        try {
123:                            fos.close();
124:                        } catch (IOException ex) {
125:                            ex.printStackTrace();
126:                        }
127:                    }
128:                }
129:
130:                return false;
131:            }
132:
133:            /**
134:             * Include the xml fragements referenced by the include elements
135:             * @param xe the element being processed
136:             * @return true if the processing completed successfully, otherwise false is returned
137:             */
138:            protected boolean includeElements(XmlElement xe) {
139:                return true;
140:            }
141:
142:            /**
143:             * Exclude those elements that are marked as being optional. 
144:             * Calls back to the isIncluded method of the template engine
145:             * @param xe the element being processed
146:             * @return true if the processing completed successfully, otherwise false is returned
147:             */
148:            protected boolean excludeElements(XmlElement xe) {
149:                Vector children = xe.getChildren();
150:                int numChildren = children.size();
151:                for (int i = numChildren - 1; i >= 0; i--) {
152:                    XmlElement child = (XmlElement) children.elementAt(i);
153:                    String include = child.getAttribute("te_include");
154:                    if (include != null) {
155:                        if (!engine.includes(include)) {
156:                            xe.remove(child);
157:                            child.removeAttribute("te_include");
158:                            continue;
159:                        }
160:                    }
161:                    excludeElements(child);
162:                }
163:                return true;
164:            }
165:
166:            /**
167:             * Repeat those elements that are within a repeat element. 
168:             * The while clause of the repeat element is used to determine the number of
169:             * times the element is repeated. When done, the repeat element is removed.
170:             * @param parent the parent of the element being processed
171:             * @param xe the element being processed
172:             * @return true if the processing completed successfully, otherwise false is returned
173:             */
174:            protected boolean repeatElements(XmlElement parent, XmlElement xe) {
175:                String tag = xe.getName();
176:
177:                Vector children = xe.getChildren();
178:                int numChildren = children.size();
179:                for (int i = 0; i < numChildren; i++) {
180:                    boolean repeatElement = false;
181:
182:                    XmlElement child = (XmlElement) children.elementAt(i);
183:
184:                    // Preserve a copy of the original node
185:                    XmlElement clone = (XmlElement) child.clone();
186:
187:                    if ("Repeat".equals(tag)) {
188:                        String repeatReference = child.getAttribute("while");
189:
190:                        int loopCounter = 0;
191:                        do {
192:                            // Replace the loop counter
193:                            Enumeration attribNames = clone
194:                                    .enumerateAttributeNames();
195:                            while (attribNames.hasMoreElements()) {
196:                                String attribName = (String) attribNames
197:                                        .nextElement();
198:                                String attribValue = clone
199:                                        .getAttribute(attribName);
200:                                int counterPos = attribName.indexOf("{N}");
201:                                if (counterPos >= 0)
202:                                    attribValue.replace("{N}", Integer
203:                                            .toString(loopCounter));
204:
205:                                attribValue = engine.evaluateAttribute(
206:                                        attribValue).toString();
207:                                child.setAttribute(attribName, attribValue);
208:                            }
209:
210:                            if ((loopCounter > 0) && tag.equals("Repeat")) {
211:                                // Repeat the child addition.
212:                                repeatElement = ((Boolean) engine
213:                                        .evaluateAttribute(repeatReference))
214:                                        .booleanValue();
215:                                if (repeatElement) {
216:                                    child = (XmlElement) child.clone();
217:                                    parent.addChild(child);
218:                                    repeatElements(xe, child);
219:                                }
220:                            }
221:                            loopCounter++;
222:                        } while (repeatElement);
223:
224:                        parent.remove(xe);
225:                    }
226:                    repeatElements(xe, child);
227:                }
228:                return true;
229:            }
230:
231:            /**
232:             * Sustitute any content, field values etc described in the template
233:             * @param xe the element being processed
234:             * @return true if the processing completed successfully, otherwise false is returned
235:             */
236:            protected boolean insertAttribute(XmlElement xe) {
237:                Vector children = xe.getChildren();
238:                int numChildren = children.size();
239:                for (int i = numChildren - 1; i >= 0; i--) {
240:                    XmlElement child = (XmlElement) children.elementAt(i);
241:                    String custom = child.getAttribute("te_content");
242:                    if (custom != null) {
243:                        String content = engine.getContent(custom);
244:                        if (content != null) {
245:                            child.setAttribute("content", content);
246:                        }
247:                    }
248:                    String style = child.getAttribute("te_style");
249:                    if (style != null) {
250:                        String content = engine.getContent(style);
251:                        if (content != null) {
252:                            child.setAttribute("style", content);
253:                        }
254:                    }
255:                    String tooltip = child.getAttribute("te_hint");
256:                    if (tooltip != null) {
257:                        String content = engine.getContent(tooltip);
258:                        if (content != null) {
259:                            child.removeAttribute("tooltip");
260:                        }
261:                    }
262:
263:                    insertAttribute(child);
264:                }
265:                return true;
266:            }
267:
268:            /**
269:             * Attempt to copy the resources specified in XML template
270:             * @param xe the element being processed
271:             * @return true if the processing completed successfully, otherwise false is returned
272:             */
273:            protected boolean copyResources(XmlElement xe) {
274:                return true;
275:            }
276:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.