Source Code Cross Referenced for XMLNodeReader.java in  » Database-ORM » MMBase » org » mmbase » util » 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 » Database ORM » MMBase » org.mmbase.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:
003:         This software is OSI Certified Open Source Software.
004:         OSI Certified is a certification mark of the Open Source Initiative.
005:
006:         The license (Mozilla version 1.0) can be read at the MMBase site.
007:         See http://www.MMBase.org/license
008:
009:         */
010:        package org.mmbase.util;
011:
012:        import java.io.*;
013:        import java.util.Locale;
014:        import java.util.Set;
015:        import java.util.Vector;
016:
017:        import org.mmbase.bridge.Field;
018:        import org.mmbase.module.core.*;
019:        import org.mmbase.util.logging.Logger;
020:        import org.mmbase.util.logging.Logging;
021:        import org.mmbase.util.xml.DocumentReader;
022:        import org.w3c.dom.*;
023:        import org.xml.sax.InputSource;
024:
025:        /**
026:         * This class reads a node from an exported application.
027:         * @application Applications
028:         * @move org.mmbase.util.xml
029:         * @author Daniel Ockeloen
030:         * @author Michiel Meeuwissen
031:         * @version $Id: XMLNodeReader.java,v 1.48 2007/06/21 15:50:22 nklasens Exp $
032:         */
033:        public class XMLNodeReader extends DocumentReader {
034:            private static final Logger log = Logging
035:                    .getLoggerInstance(XMLNodeReader.class);
036:
037:            private ResourceLoader path;
038:
039:            public boolean loadBinaries = true;
040:
041:            /**
042:             * @since MMBase-1.8
043:             */
044:            public XMLNodeReader(InputSource is, ResourceLoader path) {
045:                super (is, false);
046:                this .path = path;
047:            }
048:
049:            /**
050:             * get the name of this application
051:             */
052:            public String getExportSource() {
053:                Node n1 = document.getDocumentElement();
054:                if (n1 != null) {
055:                    NamedNodeMap nm = n1.getAttributes();
056:                    if (nm != null) {
057:                        Node n2 = nm.getNamedItem("exportsource");
058:                        return (n2.getNodeValue());
059:                    }
060:                } else {
061:                    log.warn("exportsource attribute missing");
062:                }
063:                return null;
064:            }
065:
066:            /**
067:             * get the name of this application
068:             */
069:            public int getTimeStamp() {
070:                Node n1 = document.getFirstChild();
071:                if (n1.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
072:                    n1 = n1.getNextSibling();
073:                }
074:                if (n1 != null) {
075:                    NamedNodeMap nm = n1.getAttributes();
076:                    if (nm != null) {
077:                        Node n2 = nm.getNamedItem("timestamp");
078:                        try {
079:                            java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
080:                                    "yyyyMMddhhmmss", Locale.US);
081:                            int times = (int) (formatter.parse(
082:                                    n2.getNodeValue()).getTime() / 1000);
083:                            //int times=DateSupport.parsedatetime(n2.getNodeValue());
084:                            return times;
085:                        } catch (java.text.ParseException e) {
086:                            return -1;
087:                        }
088:                    }
089:                } else {
090:                    log.warn("timestamp attribute missing");
091:                }
092:                return -1;
093:            }
094:
095:            public Vector<MMObjectNode> getNodes(MMBase mmbase) {
096:                Vector<MMObjectNode> nodes = new Vector<MMObjectNode>();
097:                Node n1 = document.getDocumentElement();
098:                while (n1 != null) {
099:                    MMObjectBuilder bul = mmbase.getMMObject(n1.getNodeName());
100:                    if (bul == null) {
101:                        log.error("Can't get builder with name: '"
102:                                + n1.getNodeName() + "'");
103:                    } else {
104:                        Node n2 = n1.getFirstChild();
105:                        while (n2 != null) {
106:                            if (n2.getNodeName().equals("node")) {
107:                                NamedNodeMap nm = n2.getAttributes();
108:                                if (nm != null) {
109:                                    Node n4 = nm.getNamedItem("owner");
110:                                    MMObjectNode newNode = null;
111:                                    if (n4 != null) {
112:                                        newNode = bul.getNewNode(n4
113:                                                .getNodeValue());
114:                                    } else {
115:                                        newNode = bul.getNewNode("import");
116:                                    }
117:                                    n4 = nm.getNamedItem("alias");
118:                                    if (n4 != null) {
119:                                        // tokenize here!
120:                                        String n4value = n4.getNodeValue();
121:                                        String[] aliases = n4value.split(",");
122:                                        for (String alias : aliases) {
123:                                            log.info("Setting alias to "
124:                                                    + alias);
125:                                            newNode.setAlias(alias);
126:                                        }
127:                                    }
128:                                    n4 = nm.getNamedItem("number");
129:                                    try {
130:                                        int num = Integer.parseInt(n4
131:                                                .getNodeValue());
132:                                        newNode.setValue("number", num);
133:                                    } catch (Exception e) {
134:                                    }
135:                                    Node n5 = n2.getFirstChild();
136:                                    while (n5 != null) {
137:                                        if (n5.getNodeType() == Node.ELEMENT_NODE) {
138:                                            String key = n5.getNodeName();
139:                                            NodeList nl = n5.getChildNodes();
140:                                            StringBuffer res = new StringBuffer(
141:                                                    "");
142:                                            for (int i = 0; i < nl.getLength(); i++) {
143:                                                Node n = nl.item(i);
144:                                                if ((n.getNodeType() == Node.TEXT_NODE)
145:                                                        || (n.getNodeType() == Node.CDATA_SECTION_NODE)) {
146:                                                    res.append(n.getNodeValue()
147:                                                            .trim());
148:                                                }
149:                                            }
150:                                            String value = res.toString();
151:
152:                                            setValue(bul, newNode, n5, key,
153:                                                    value);
154:                                        }
155:                                        n5 = n5.getNextSibling();
156:                                    }
157:                                    nodes.add(newNode);
158:                                }
159:                            }
160:                            n2 = n2.getNextSibling();
161:                        }
162:                    }
163:                    n1 = n1.getNextSibling();
164:                }
165:                return nodes;
166:            }
167:
168:            protected void setValue(MMObjectBuilder bul, MMObjectNode newNode,
169:                    Node n5, String key, String value) {
170:                int type = bul.getDBType(key);
171:                if (type != -1) {
172:                    if (type == Field.TYPE_STRING || type == Field.TYPE_XML) {
173:                        if (value == null) {
174:                            value = "";
175:                        }
176:                        newNode.setValue(key, value);
177:                    } else if (type == Field.TYPE_NODE) {
178:                        // do not really set it, because we need syncnodes later for this.
179:                        newNode.storeValue("__" + key, value); // yes, this is hackery, I'm sorry.
180:                        newNode.setValue(key, null);
181:                    } else if (type == Field.TYPE_INTEGER) {
182:                        try {
183:                            newNode.setValue(key, Integer.parseInt(value));
184:                        } catch (Exception e) {
185:                            log.warn("error setting integer-field '" + key
186:                                    + "' to '" + value + "' because " + e);
187:                            newNode.setValue(key, -1);
188:                        }
189:                    } else if (type == Field.TYPE_FLOAT) {
190:                        try {
191:                            newNode.setValue(key, Float.parseFloat(value));
192:                        } catch (Exception e) {
193:                            log.warn("error setting float-field '" + key
194:                                    + "' to '" + value + "' because " + e);
195:                            newNode.setValue(key, -1);
196:                        }
197:                    } else if (type == Field.TYPE_DOUBLE) {
198:                        try {
199:                            newNode.setValue(key, Double.parseDouble(value));
200:                        } catch (Exception e) {
201:                            log.warn("error setting double-field '" + key
202:                                    + "' to '" + value + "' because " + e);
203:                            newNode.setValue(key, -1);
204:                        }
205:                    } else if (type == Field.TYPE_LONG) {
206:                        try {
207:                            newNode.setValue(key, Long.parseLong(value));
208:                        } catch (Exception e) {
209:                            log.warn("error setting long-field '" + key
210:                                    + "' to '" + value + "' because " + e);
211:                            newNode.setValue(key, -1);
212:                        }
213:                    } else if (type == Field.TYPE_DATETIME) {
214:                        newNode.setValue(key, Casting.toDate(value));
215:                    } else if (type == Field.TYPE_BOOLEAN) {
216:                        newNode.setValue(key, Casting.toBoolean(value));
217:                    } else if (type == Field.TYPE_BINARY) {
218:                        NamedNodeMap nm2 = n5.getAttributes();
219:                        Node n7 = nm2.getNamedItem("file");
220:                        try {
221:                            if (loadBinaries) {
222:                                newNode.setValue(key, readBytesStream(n7
223:                                        .getNodeValue()));
224:                            } else {
225:                                newNode.setValue(key, n7.getNodeValue());
226:                            }
227:                        } catch (IOException ioe) {
228:                            log.warn("Could not set field " + key + " " + ioe);
229:                        }
230:                    } else {
231:                        log.error("CoreField not found for #" + type
232:                                + " was not known for field with name: '" + key
233:                                + "' and with value: '" + value + "'");
234:                    }
235:                }
236:            }
237:
238:            private byte[] readBytesStream(String resourceName)
239:                    throws IOException {
240:                InputStream stream = path.getResourceAsStream(resourceName);
241:                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
242:                int c = stream.read();
243:                while (c != -1) {
244:                    buffer.write(c);
245:                    c = stream.read();
246:                }
247:                return buffer.toByteArray();
248:            }
249:
250:            public void loadBinairyFields(MMObjectNode newNode) {
251:                Set<String> fieldNames = newNode.getBuilder().getFieldNames();
252:                if (fieldNames != null && fieldNames.size() > 0) {
253:                    for (String fieldName : fieldNames) {
254:                        int fieldDBType = newNode.getBuilder().getDBType(
255:                                fieldName);
256:                        if (fieldDBType == Field.TYPE_BINARY) {
257:                            try {
258:                                String resource = newNode
259:                                        .getStringValue(fieldName);
260:                                newNode.setValue(fieldName,
261:                                        readBytesStream(resource));
262:                            } catch (Exception setValueEx) {
263:                                log.error(setValueEx);
264:                            }
265:                        }
266:                    }
267:                }
268:            }
269:
270:            public void setLoadBinaries(boolean loadBinaries) {
271:                this.loadBinaries = loadBinaries;
272:            }
273:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.