Source Code Cross Referenced for XDataSource.java in  » XML-UI » XUI » net » xoetrope » data » 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 » XUI » net.xoetrope.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package net.xoetrope.data;
002:
003:        import java.io.BufferedWriter;
004:        import java.io.FileOutputStream;
005:        import java.io.IOException;
006:        import java.io.OutputStreamWriter;
007:        import java.io.Reader;
008:        import java.io.Writer;
009:        import java.util.Enumeration;
010:        import java.util.Vector;
011:
012:        import net.xoetrope.debug.DebugLogger;
013:        import net.xoetrope.xml.XmlElement;
014:        import net.xoetrope.xml.XmlSource;
015:        import net.xoetrope.xui.XProjectManager;
016:        import net.xoetrope.xui.XResourceManager;
017:        import net.xoetrope.xui.build.BuildProperties;
018:        import net.xoetrope.xui.data.XModel;
019:
020:        /**
021:         * <p>Loads a model from a reader</p>
022:         * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
023:         * License:      see license.txt
024:         * $Revision: 1.22 $
025:         */
026:        public class XDataSource {
027:            private static int nextId = 0;
028:
029:            public XDataSource() {
030:            }
031:
032:            /**
033:             * Read a model from the Reader
034:             * @param r the Reader
035:             */
036:            public void read(Reader r) {
037:                XmlElement ele = XmlSource.read(r);
038:                read(ele);
039:            }
040:
041:            /**
042:             * Read an file pointed to by an element in the XML description of the data sources
043:             * @param ele the individual data source description
044:             */
045:            public void read(XmlElement ele) {
046:                Vector v = ele.getChildren();
047:                if (v != null) {
048:                    int numFiles = v.size();
049:                    for (int i = 0; i < numFiles; i++) {
050:                        try {
051:                            XmlElement source = (XmlElement) v.elementAt(i);
052:                            String fileName = source.getAttribute("filename");
053:                            if (fileName != null) {
054:                                readDataSource(fileName, source);
055:                            }
056:                        } catch (Exception ex) {
057:                            ex.printStackTrace();
058:                        }
059:                    }
060:                }
061:            }
062:
063:            /**
064:             * Read the data source file.
065:             * @param fileName the nam eof the file to open
066:             * @param source the XML element describing the source
067:             */
068:            protected void readDataSource(String fileName, XmlElement source) {
069:                try {
070:                    Reader sr = XResourceManager.getBufferedReader(fileName,
071:                            null);
072:
073:                    XmlElement src = XmlSource.read(sr);
074:                    String type = source.getAttribute("type");
075:                    if ((type == null) || (type.length() == 0))
076:                        loadTable(src, XProjectManager.getModel());
077:                } catch (Exception ex) {
078:                }
079:            }
080:
081:            /**
082:             * Recursively load the model data
083:             * @param source the source element
084:             * @param model the model for the source element
085:             */
086:            public void loadTable(XmlElement source, XModel model) {
087:                // Store the node tag name so that the XML can be regenerated/output
088:                model.setTagName(source.getName().toString());
089:
090:                Vector children = source.getChildren();
091:                Enumeration attributes = source.enumerateAttributeNames();
092:                boolean hasAutoId = false;
093:
094:                int numChildren = children.size();
095:
096:                // Count the number of attributes
097:                int numAttributes = 0;
098:                while (attributes.hasMoreElements()) {
099:                    attributes.nextElement();
100:                    numAttributes++;
101:                }
102:                model.setNumAttributes(numAttributes);
103:
104:                // Add the attributes of the source element
105:                attributes = source.enumerateAttributeNames();
106:                while (attributes.hasMoreElements()) {
107:                    String attribName = ((String) attributes.nextElement())
108:                            .toLowerCase();
109:                    model.setAttribValue(model.getAttribute(attribName), source
110:                            .getAttribute(attribName));
111:                }
112:
113:                // Add the children of the source element
114:                for (int i = 0; i < numChildren; i++) {
115:                    XmlElement ele = (XmlElement) children.elementAt(i);
116:                    Object elementName = ele.getAttribute("id");
117:                    // Give a unique id for unnamed elements
118:                    if (elementName == null) {
119:                        elementName = String.valueOf(getNextId());
120:                        hasAutoId = true;
121:                    }
122:
123:                    // Create a node/model for the child
124:                    XModel childModel = (XModel) model
125:                            .get((String) elementName);
126:                    //      XModel childModel = ( XModel )model.append( ( String )elementName );
127:
128:                    // Allocate store for the child's children
129:                    childModel.hasAutoId(hasAutoId);
130:                    childModel.setNumChildren(Math.max(childModel
131:                            .getNumChildren(), ele.getChildren().size()));
132:
133:                    // Recurse to add the childs details
134:                    loadTable(ele, childModel);
135:                }
136:            }
137:
138:            /**
139:             * Get the next ID for anonymous elements
140:             * @return
141:             */
142:            private int getNextId() {
143:                return nextId++;
144:            }
145:
146:            /**
147:             * Outputs the datasource as XML
148:             * @param w The Writer of the file.
149:             */
150:            public void write(Writer w) {
151:                write(w, XProjectManager.getModel());
152:            }
153:
154:            /**
155:             * Outputs the datasource as XML
156:             * @param w The Writer of the file.
157:             */
158:            public void write(Writer w, XModel model) {
159:                try {
160:                    outputModel(w, model);
161:                    w.flush();
162:                    w.close();
163:                } catch (IOException ex) {
164:                    ex.printStackTrace();
165:                }
166:            }
167:
168:            /**
169:             * Iterate the XModels and outputs the Elements and their attributes.
170:             * @param w the output writer
171:             * @param model the model to write
172:             */
173:            public static void outputModel(Writer w, XModel model) {
174:                try {
175:                    w.write("<" + model.getTagName());
176:                    for (int j = 0; j < model.getNumAttributes(); j++) {
177:                        if ((model.getAttribValue(j) != null)
178:                                && (model.getAttribValue(j).toString().length() > 0))
179:                            if (!(model.getAttribName(j).compareTo("name") == 0 && model
180:                                    .hasAutoId())) {
181:                                String valueStr = (String) model
182:                                        .getAttribValue(j);
183:                                int pos = valueStr.indexOf("\"");
184:                                if (pos >= 0)
185:                                    w.write(" " + model.getAttribName(j) + "="
186:                                            + "\'" + valueStr + "\'");
187:                                else
188:                                    w.write(" " + model.getAttribName(j) + "="
189:                                            + "\"" + valueStr + "\"");
190:                            }
191:                    }
192:                    w.write(">\n");
193:
194:                    for (int i = 0; i < model.getNumChildren(); i++) {
195:                        XModel childModel = model.get(i);
196:                        outputModel(w, childModel);
197:                    }
198:                    w.write("</" + model.getTagName() + ">\n");
199:                } catch (IOException ex) {
200:                    ex.printStackTrace();
201:                }
202:            }
203:
204:            /**
205:             * Iterate the XModels and outputs the Elements and their attributes.
206:             * @param filename the file to write
207:             * @param model the model to write
208:             */
209:            public void outputModel(String filename, XModel model) {
210:                if (BuildProperties.DEBUG)
211:                    DebugLogger.trace("savePath:" + filename);
212:
213:                if (filename != null) {
214:                    try {
215:                        if (filename.length() > 1) {
216:                            FileOutputStream fos = new FileOutputStream(
217:                                    filename);
218:                            OutputStreamWriter osw = new OutputStreamWriter(
219:                                    fos, "UTF8");
220:                            BufferedWriter bw = new BufferedWriter(osw);
221:                            outputModel(bw, XProjectManager.getModel());
222:                        }
223:                    } catch (Exception ex) {
224:                        if (BuildProperties.DEBUG)
225:                            DebugLogger.logError("Could not write content!");
226:                    }
227:                }
228:            }
229:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.