Source Code Cross Referenced for XmlDomHelper.java in  » Database-ORM » ODAL » com » completex » objective » 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 » ODAL » com.completex.objective.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Objective Database Abstraction Layer (ODAL)
003:         *  Copyright (c) 2004, The ODAL Development Group
004:         *  All rights reserved.
005:         *  For definition of the ODAL Development Group please refer to LICENCE.txt file
006:         *
007:         *  Distributable under LGPL license.
008:         *  See terms of license at gnu.org.
009:         */package com.completex.objective.util;
010:
011:        import org.w3c.dom.Document;
012:        import org.w3c.dom.Element;
013:        import org.w3c.dom.NamedNodeMap;
014:        import org.w3c.dom.Node;
015:        import org.w3c.dom.NodeList;
016:
017:        import java.math.BigDecimal;
018:        import java.text.ParseException;
019:        import java.util.ArrayList;
020:        import java.util.Date;
021:
022:        /**
023:         * @author Gennady Krizhevsky
024:         */
025:        public class XmlDomHelper {
026:
027:            private final Document document;
028:
029:            public static final Node[] EMPTY_NODE_ARRAY = new Node[0];
030:
031:            public XmlDomHelper(Document document) {
032:                this .document = document;
033:            }
034:
035:            public static String D2S(Date date) {
036:                return TypeUtil.D2S(date, TypeUtil.yyyyMMdd_hhmmssSSS());
037:            }
038:
039:            public static Date S2D(String value) {
040:                try {
041:                    return TypeUtil.S2D(value, TypeUtil.yyyyMMdd_hhmmssSSS());
042:                } catch (ParseException e) {
043:                    new XmlHelperRuntimeException(
044:                            "ParseException: Cannot parse date value : "
045:                                    + e.getMessage());
046:                }
047:                return null;
048:            }
049:
050:            public static Boolean S2B(String value) {
051:                return TypeUtil.S2B(value, TypeUtil.TRUE_FALSE);
052:            }
053:
054:            public static boolean S2b(String value) {
055:                return TypeUtil.S2b(value, TypeUtil.TRUE_FALSE);
056:            }
057:
058:            public static String B2S(Boolean value) {
059:                return TypeUtil.B2S(value, TypeUtil.TRUE_FALSE);
060:            }
061:
062:            public static String b2S(boolean value) {
063:                return TypeUtil.b2S(value, TypeUtil.TRUE_FALSE);
064:            }
065:
066:            public static Long S2L(String value) {
067:                return TypeUtil.S2L(value);
068:            }
069:
070:            public static BigDecimal S2BigDecimal(String value) {
071:                return TypeUtil.S2BigDecimal(value);
072:            }
073:
074:            public Node getFirstNodeByType(Node parentNode, int type) {
075:                Node[] childNodes = getNodesByType(parentNode, type);
076:                return childNodes.length > 0 ? childNodes[0] : null;
077:            }
078:
079:            public Element getFirstElement(Node parentNode) {
080:                return (Element) getFirstNodeByType(parentNode,
081:                        Node.ELEMENT_NODE);
082:            }
083:
084:            public Element getElementByName(Node parentNode, String tagName,
085:                    boolean assertExists) {
086:                return (Element) getNodeByName(parentNode, tagName,
087:                        Node.ELEMENT_NODE, assertExists);
088:            }
089:
090:            public Node getNodeByName(Node parentNode, String tagName,
091:                    int type, boolean assertExists) {
092:                Node[] resultNodes = getNodesByName(parentNode, tagName, type,
093:                        assertExists);
094:                if (resultNodes.length == 0 && assertExists) {
095:                    throw new XmlHelperRuntimeException(
096:                            "Cannot find node by tag " + tagName);
097:                }
098:                return resultNodes.length == 0 ? null : resultNodes[0];
099:            }
100:
101:            public Element[] getElementsByName(Node parentNode, String tagName,
102:                    boolean assertExists) {
103:                Node[] nodes = getNodesByName(parentNode, tagName,
104:                        Node.ELEMENT_NODE, assertExists);
105:                return nodes2elements(nodes);
106:            }
107:
108:            public static Element[] nodes2elements(Node[] nodes) {
109:                Element[] elements = new Element[nodes.length];
110:                for (int i = 0; i < elements.length; i++) {
111:                    elements[i] = (Element) nodes[i];
112:                }
113:                return elements;
114:            }
115:
116:            public Node[] getNodesByName(Node parentNode, String tagName,
117:                    int type, boolean assertExists) {
118:                ArrayList nodeList = new ArrayList();
119:                NodeList childNodes = parentNode.getChildNodes();
120:                for (int i = 0; i < childNodes.getLength(); i++) {
121:                    Node node = childNodes.item(i);
122:                    if (tagName.equals(node.getNodeName())
123:                            && node.getNodeType() == type) {
124:                        nodeList.add(node);
125:                    }
126:                }
127:                if (nodeList.isEmpty() && assertExists) {
128:                    throw new XmlHelperRuntimeException(
129:                            "Cannot find nodes by tag " + tagName);
130:                }
131:                return (Node[]) nodeList.toArray(new Node[nodeList.size()]);
132:            }
133:
134:            public Node[] getNodesByType(Node parentNode, int type) {
135:                ArrayList nodeList = new ArrayList();
136:                NodeList childNodes = parentNode.getChildNodes();
137:                for (int i = 0; i < childNodes.getLength(); i++) {
138:                    Node node = childNodes.item(i);
139:                    if (node.getNodeType() == type) {
140:                        nodeList.add(node);
141:                    }
142:                }
143:                return (Node[]) nodeList.toArray(new Node[nodeList.size()]);
144:            }
145:
146:            public String getAttributeByName(Node node, String tagName,
147:                    boolean assertExist) {
148:                NamedNodeMap attributes = node.getAttributes();
149:                if (attributes == null) {
150:                    if (assertExist) {
151:                        throw new XmlHelperRuntimeException(
152:                                "Cannot find element by tag " + tagName);
153:                    } else {
154:                        return null;
155:                    }
156:                }
157:                Node namedItem = attributes.getNamedItem(tagName);
158:                if (namedItem == null && assertExist) {
159:                    throw new XmlHelperRuntimeException(
160:                            "Cannot find element by tag " + tagName);
161:                }
162:                return namedItem == null ? null : namedItem.getNodeValue();
163:            }
164:
165:            public boolean getAttributeByNameAsBoolean(Node node,
166:                    String tagName, boolean assertExist) {
167:                String value = getAttributeByName(node, tagName, assertExist);
168:                return value != null && S2b(value);
169:            }
170:
171:            public int getAttributeByNameAsInt(Node node, String tagName,
172:                    boolean assertExist) {
173:                String value = getAttributeByName(node, tagName, assertExist);
174:                int idx;
175:                try {
176:                    idx = Integer.valueOf(value).intValue();
177:                } catch (NumberFormatException e) {
178:                    throw new XmlHelperRuntimeException(
179:                            "Cannot convert idx value " + value + " to number");
180:                }
181:                return idx;
182:            }
183:
184:            public String getNodeText(Node valueNode) {
185:                if (valueNode == null) {
186:                    return null;
187:                }
188:                Node firstChild = valueNode.getFirstChild();
189:                return firstChild == null ? null : firstChild.getNodeValue();
190:            }
191:
192:            public Element createElement(Node parentNode, String tagName) {
193:                Element childElement = createElement(tagName);
194:                parentNode.appendChild(childElement);
195:                return childElement;
196:            }
197:
198:            public Element createElement(String tagName) {
199:                return document.createElement(tagName);
200:            }
201:
202:            public Element getFirstElementByName(Node parentNode,
203:                    String tagName, boolean assertExist) {
204:                return (Element) getFirstNodeByName(parentNode, tagName,
205:                        Node.ELEMENT_NODE, assertExist);
206:            }
207:
208:            public Node getFirstNodeByName(Node parentNode, String tagName,
209:                    int type, boolean assertExist) {
210:                Node[] childNodes = getNodesByType(parentNode, type);
211:                if (assertExist
212:                        && (childNodes == null || childNodes.length == 0)) {
213:                    throw new XmlHelperRuntimeException(
214:                            "Cannot find child node by tag " + tagName);
215:                }
216:                Node childNode = null;
217:                if (childNodes != null && childNodes.length > 0) {
218:                    childNode = extractPoNode(childNodes, tagName, assertExist);
219:                }
220:                return childNode;
221:            }
222:
223:            public Element[] getElements(Node node) {
224:                return nodes2elements(getNodesByType(node, Node.ELEMENT_NODE));
225:            }
226:
227:            public Node extractPoNode(Node[] poElements, String tagName,
228:                    boolean assertExist) {
229:                Node poNode = null;
230:                for (int i = 0; i < poElements.length; i++) {
231:                    Node node = poElements[i];
232:                    if (tagName.equals(node.getNodeName())) {
233:                        poNode = node;
234:                        break;
235:                    }
236:                }
237:                if (assertExist && (poNode == null)) {
238:                    throw new XmlHelperRuntimeException(
239:                            "Cannot find element by tag " + tagName);
240:                }
241:                return poNode;
242:            }
243:
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.