Source Code Cross Referenced for XMLUtils.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » util » 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 » J2EE » ow2 easybeans » org.ow2.easybeans.util.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * EasyBeans
003:         * Copyright (C) 2006 Bull S.A.S.
004:         * Contact: easybeans@ow2.org
005:         *
006:         * This library is free software; you can redistribute it and/or
007:         * modify it under the terms of the GNU Lesser General Public
008:         * License as published by the Free Software Foundation; either
009:         * version 2.1 of the License, or any later version.
010:         *
011:         * This library is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014:         * Lesser General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU Lesser General Public
017:         * License along with this library; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
019:         * USA
020:         *
021:         * --------------------------------------------------------------------------
022:         * $Id: XMLUtils.java 2049 2007-11-20 14:32:56Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.util.xml;
025:
026:        import java.util.ArrayList;
027:        import java.util.List;
028:        import java.util.Properties;
029:
030:        import org.w3c.dom.Element;
031:        import org.w3c.dom.NamedNodeMap;
032:        import org.w3c.dom.Node;
033:        import org.w3c.dom.NodeList;
034:
035:        /**
036:         * Class with some useful methods on XML document.
037:         */
038:        public final class XMLUtils {
039:
040:            /**
041:             * Utility class, no constructor.
042:             */
043:            private XMLUtils() {
044:            }
045:
046:            /**
047:             * Returns the value of the attribute of the given element.
048:             * @param base the element from where to search.
049:             * @param name of the attribute to get.
050:             * @return the value of this element.
051:             */
052:            public static String getAttributeValue(final Element base,
053:                    final String name) {
054:
055:                // get attribute of this element...
056:                NamedNodeMap mapAttributes = base.getAttributes();
057:                Node node = mapAttributes.getNamedItem(name);
058:                if (node != null) {
059:                    return node.getNodeValue();
060:                }
061:                return null;
062:            }
063:
064:            /**
065:             * Returns the value of the given node.
066:             * @param ns the namespace.
067:             * @param base the element from where to search.
068:             * @param name of the element to get.
069:             * @return the value of this element.
070:             */
071:            public static String getStringValueElement(final String ns,
072:                    final Element base, final String name) {
073:                String value = null;
074:
075:                // Get element
076:                NodeList list = base.getElementsByTagNameNS(ns, name);
077:                if (list.getLength() == 1) {
078:                    Element element = (Element) list.item(0);
079:                    Node node = element.getFirstChild();
080:                    if (node != null) {
081:                        value = node.getNodeValue();
082:                    }
083:                } else if (list.getLength() > 1) {
084:                    throw new IllegalStateException("Element '" + name
085:                            + "' on '" + base
086:                            + "' should be unique but there are '"
087:                            + list.getLength() + "' elements");
088:                }
089:
090:                if (value != null) {
091:                    value = value.trim();
092:                }
093:                return value;
094:            }
095:
096:            /**
097:             * Returns the value of the given node.
098:             * @param base the element from where to search.
099:             * @param name of the element to get.
100:             * @return the value of this element.
101:             */
102:            public static String getStringValueElement(final Element base,
103:                    final String name) {
104:                String value = null;
105:
106:                // Get element
107:                NodeList list = base.getElementsByTagName(name);
108:                if (list.getLength() == 1) {
109:                    Element element = (Element) list.item(0);
110:                    Node node = element.getFirstChild();
111:                    if (node != null) {
112:                        value = node.getNodeValue();
113:                    }
114:                } else if (list.getLength() > 1) {
115:                    throw new IllegalStateException("Element '" + name
116:                            + "' on '" + base
117:                            + "' should be unique but there are '"
118:                            + list.getLength() + "' elements");
119:                }
120:
121:                if (value != null) {
122:                    value = value.trim();
123:                }
124:                return value;
125:            }
126:
127:            /**
128:             * Returns the value of the child node with the given name.
129:             * @param base the element from where to search.
130:             * @param name of the element to get.
131:             * @return the value of this element.
132:             */
133:            public static String getChildStringValueForElement(
134:                    final Element base, final String name) {
135:                String value = null;
136:                NodeList nodeList = base.getChildNodes();
137:                if (nodeList.getLength() > 0) {
138:                    int length = nodeList.getLength();
139:                    for (int i = 0; i < length; i++) {
140:                        Node node = nodeList.item(i);
141:
142:                        // Get an element, create an instance of the element
143:                        if (Node.ELEMENT_NODE == node.getNodeType()) {
144:                            if (name.equals(node.getNodeName())) {
145:                                // Get value of this child
146:                                Node elNode = ((Element) node).getFirstChild();
147:                                if (elNode != null) {
148:                                    value = elNode.getNodeValue();
149:                                    break;
150:                                }
151:                            }
152:                        }
153:                    }
154:                }
155:                return value;
156:            }
157:
158:            /**
159:             * Returns a Properties object matching the given node.
160:             * @param ns the namespace.
161:             * @param base the element from where to search.
162:             * @param name of the element to get.
163:             * @return the value of this element.
164:             */
165:            public static Properties getPropertiesValueElement(final String ns,
166:                    final Element base, final String name) {
167:                Properties returnedProperties = new Properties();
168:
169:                // Get element
170:                NodeList list = base.getElementsByTagNameNS(ns, name);
171:                if (list.getLength() == 1) {
172:                    Element element = (Element) list.item(0);
173:
174:                    // Get property element
175:                    NodeList properties = element.getElementsByTagNameNS(ns,
176:                            "property");
177:
178:                    // If properties is present, analyze them and add them
179:                    if (properties.getLength() > 0) {
180:                        for (int i = 0; i < properties.getLength(); i++) {
181:                            Element elemProperty = (Element) properties.item(i);
182:                            String pName = getAttributeValue(elemProperty,
183:                                    "name");
184:                            String pValue = getAttributeValue(elemProperty,
185:                                    "value");
186:                            if (pName != null && pValue != null) {
187:                                returnedProperties.setProperty(pName, pValue);
188:                            }
189:
190:                        }
191:                    }
192:                } else if (list.getLength() > 1) {
193:                    throw new IllegalStateException("Element '" + name
194:                            + "' on '" + base
195:                            + "' should be unique but there are '"
196:                            + list.getLength() + "' elements");
197:                }
198:
199:                return returnedProperties;
200:            }
201:
202:            /**
203:             * Returns a list of value for the given node.
204:             * @param ns the namespace.
205:             * @param base the element from where to search.
206:             * @param name of the element to get.
207:             * @return the list of value of this element.
208:             */
209:            public static List<String> getStringListValueElement(
210:                    final String ns, final Element base, final String name) {
211:                List<String> returnedlist = new ArrayList<String>();
212:
213:                // Get element
214:                NodeList list = base.getElementsByTagNameNS(ns, name);
215:                int length = list.getLength();
216:
217:                // Get all values of all elements
218:                if (length > 0) {
219:                    for (int i = 0; i < length; i++) {
220:                        Element element = (Element) list.item(i);
221:                        Node node = element.getFirstChild();
222:                        if (node != null) {
223:                            returnedlist.add(node.getNodeValue());
224:                        }
225:                    }
226:                }
227:                return returnedlist;
228:            }
229:
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.