Source Code Cross Referenced for XMLUtils.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » minerva » pool » xml » 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 » ERP CRM Financial » SourceTap CRM » org.ofbiz.minerva.pool.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed under the X license (see http://www.x.org/terms.htm)
003:         */
004:        package org.ofbiz.minerva.pool.xml;
005:
006:        import java.lang.reflect.*;
007:        import java.util.*;
008:
009:        import org.w3c.dom.*;
010:
011:        /**
012:         * Utility functions for parsing XML (DOM).
013:         *
014:         * @author Aaron Mulder (ammulder@alumni.princeton.edu)
015:         */
016:        public abstract class XMLUtils {
017:
018:            public static String getChildText(Element parent, String name) {
019:                Element e = getChildByName(parent, name);
020:                if (e == null)
021:                    return "";
022:                return getText(e);
023:            }
024:
025:            public static String getText(Element e) {
026:                NodeList nl = e.getChildNodes();
027:                int max = nl.getLength();
028:                for (int i = 0; i < max; i++) {
029:                    Node n = nl.item(i);
030:                    if (n.getNodeType() == Node.TEXT_NODE) {
031:                        return n.getNodeValue();
032:                    }
033:                }
034:                return "";
035:            }
036:
037:            public static Element getChildByName(Element e, String name) {
038:                Element[] list = getChildrenByName(e, name);
039:                if (list.length == 0)
040:                    return null;
041:                if (list.length > 1)
042:                    throw new IllegalStateException("Too many (" + list.length
043:                            + ") '" + name + "' elements found!");
044:                return list[0];
045:            }
046:
047:            public static Element[] getChildrenByName(Element e, String name) {
048:                NodeList nl = e.getChildNodes();
049:                int max = nl.getLength();
050:                LinkedList list = new LinkedList();
051:                for (int i = 0; i < max; i++) {
052:                    Node n = nl.item(i);
053:                    if (n.getNodeType() == Node.ELEMENT_NODE
054:                            && n.getNodeName().equals(name)) {
055:                        list.add(n);
056:                    }
057:                }
058:                return (Element[]) list.toArray(new Element[list.size()]);
059:            }
060:
061:            public static String[] splitOnWhitespace(String source) {
062:                int pos = -1;
063:                LinkedList list = new LinkedList();
064:                int max = source.length();
065:                for (int i = 0; i < max; i++) {
066:                    char c = source.charAt(i);
067:                    if (Character.isWhitespace(c)) {
068:                        if (i - pos > 1)
069:                            list.add(source.substring(pos + 1, i));
070:                        pos = i;
071:                    }
072:                }
073:                return (String[]) list.toArray(new String[list.size()]);
074:            }
075:
076:            public static Element createChild(Document doc, Element root,
077:                    String name) {
078:                Element elem = doc.createElement(name);
079:                root.appendChild(elem);
080:                return elem;
081:            }
082:
083:            public static void createChildText(Document doc, Element elem,
084:                    String name, String value) {
085:                Element child = doc.createElement(name);
086:                child.appendChild(doc
087:                        .createTextNode(value == null ? "" : value));
088:                elem.appendChild(child);
089:            }
090:
091:            public static void createOptionalChildText(Document doc,
092:                    Element elem, String name, String value) {
093:                if (value == null || value.length() == 0)
094:                    return;
095:                Element child = doc.createElement(name);
096:                child.appendChild(doc.createTextNode(value));
097:                elem.appendChild(child);
098:            }
099:
100:            public static Map getProperties(Element root) {
101:                Map map = new HashMap();
102:                Element[] list = getChildrenByName(root, "property");
103:                for (int i = 0; i < list.length; i++) {
104:                    String name = list[i].getAttribute("name");
105:                    String type = list[i].getAttribute("type");
106:                    String valueString = getText(list[i]);
107:                    try {
108:                        Class cls = Class.forName(type);
109:                        Constructor con = cls
110:                                .getConstructor(new Class[] { String.class });
111:                        Object value = con
112:                                .newInstance(new Object[] { valueString });
113:                        map.put(name, value);
114:                    } catch (Exception e) {
115:                        System.out.println("Unable to parse property '" + name
116:                                + "'='" + valueString + "': " + e);
117:                    }
118:                }
119:                return map;
120:            }
121:
122:            public static void applyProperties(Object o, Element root) {
123:                Map map = getProperties(root);
124:                Iterator it = map.keySet().iterator();
125:                Field[] fields = o.getClass().getFields();
126:                Method[] methods = o.getClass().getMethods();
127:                while (it.hasNext()) {
128:                    String name = (String) it.next();
129:                    Object value = map.get(name);
130:                    try {
131:                        for (int i = 0; i < fields.length; i++) {
132:                            if (fields[i].getName().equalsIgnoreCase(name)
133:                                    && isTypeMatch(fields[i].getType(), value
134:                                            .getClass())) {
135:                                fields[i].set(o, value);
136:                                System.out.println("Set field "
137:                                        + fields[i].getName() + "=" + value);
138:                                break;
139:                            }
140:                        }
141:                        for (int i = 0; i < methods.length; i++) {
142:                            if (methods[i].getName().equalsIgnoreCase(
143:                                    "set" + name)
144:                                    && methods[i].getParameterTypes().length == 1
145:                                    && isTypeMatch(methods[i]
146:                                            .getParameterTypes()[0], value
147:                                            .getClass())) {
148:                                methods[i].invoke(o, new Object[] { value });
149:                                System.out.println("Set method "
150:                                        + methods[i].getName() + "=" + value);
151:                                break;
152:                            }
153:                        }
154:                    } catch (Exception e) {
155:                        System.out.println("Unable to apply property '" + name
156:                                + "': " + e);
157:                    }
158:                }
159:            }
160:
161:            private static boolean isTypeMatch(Class one, Class two) {
162:                if (one.equals(two))
163:                    return true;
164:                if (one.isPrimitive()) {
165:                    if (one.getName().equals("int")
166:                            && two.getName().equals("java.lang.Integer"))
167:                        return true;
168:                    if (one.getName().equals("long")
169:                            && two.getName().equals("java.lang.Long"))
170:                        return true;
171:                    if (one.getName().equals("float")
172:                            && two.getName().equals("java.lang.Float"))
173:                        return true;
174:                    if (one.getName().equals("double")
175:                            && two.getName().equals("java.lang.Double"))
176:                        return true;
177:                    if (one.getName().equals("char")
178:                            && two.getName().equals("java.lang.Character"))
179:                        return true;
180:                    if (one.getName().equals("byte")
181:                            && two.getName().equals("java.lang.Byte"))
182:                        return true;
183:                    if (one.getName().equals("short")
184:                            && two.getName().equals("java.lang.Short"))
185:                        return true;
186:                    if (one.getName().equals("boolean")
187:                            && two.getName().equals("java.lang.Boolean"))
188:                        return true;
189:                }
190:                return false;
191:            }
192:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.