Source Code Cross Referenced for XMLMappingBuilder.java in  » J2EE » ow2-easybeans » org » ow2 » easybeans » xmlconfig » mapping » 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.xmlconfig.mapping 
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:XMLMappingBuilder.java 1477 2007-06-16 16:50:19Z benoitf $
023:         * --------------------------------------------------------------------------
024:         */package org.ow2.easybeans.xmlconfig.mapping;
025:
026:        import java.net.URL;
027:
028:        import org.ow2.easybeans.util.xml.DocumentParser;
029:        import org.ow2.easybeans.util.xml.DocumentParserException;
030:        import org.ow2.easybeans.util.xml.XMLUtils;
031:        import org.ow2.easybeans.xmlconfig.XMLConfigurationException;
032:        import org.w3c.dom.Document;
033:        import org.w3c.dom.Element;
034:        import org.w3c.dom.NodeList;
035:
036:        /**
037:         * Allows to build the Mapping object for a set of classes.
038:         * @author Florent Benoit
039:         */
040:        public class XMLMappingBuilder {
041:
042:            /**
043:             * Namespace used for the mapping file (for validation).
044:             */
045:            private static final String MAPPING_NS = "http://easybeans.ow2.org/xml/ns/mapping";
046:
047:            /**
048:             * Name of the <package> element.
049:             */
050:            private static final String PACKAGE_ELEMENT = "package";
051:
052:            /**
053:             * Name of the <class> element.
054:             */
055:            private static final String CLASS_ELEMENT = "class";
056:
057:            /**
058:             * Name of the <attribute> element.
059:             */
060:            private static final String ATTRIBUTE_ELEMENT = "attribute";
061:
062:            /**
063:             * URL that reference the mapping file.
064:             */
065:            private URL mappingURL = null;
066:
067:            /**
068:             * The mapping used for the given namespace.
069:             */
070:            private XMLMapping xmlMapping = null;
071:
072:            /**
073:             * Builds a new mapping builder.
074:             * @param mappingURL the given url that reference the mapping file.
075:             */
076:            public XMLMappingBuilder(final URL mappingURL) {
077:                this .mappingURL = mappingURL;
078:                xmlMapping = new XMLMapping();
079:            }
080:
081:            /**
082:             * Build the XMLMapping object by analyzing the mapping file.
083:             * @throws XMLConfigurationException if there is a failure when analyzing
084:             *         the XML file.
085:             */
086:            public void build() throws XMLConfigurationException {
087:
088:                // Parse the XML file and build all configuration process.
089:                Document xmlMappingConfigurationDocument = null;
090:                try {
091:                    xmlMappingConfigurationDocument = DocumentParser
092:                            .getDocument(mappingURL, false, null);
093:                } catch (DocumentParserException e) {
094:                    throw new XMLConfigurationException(
095:                            "Cannot get a document on the given url '"
096:                                    + mappingURL + "'.", e);
097:                }
098:
099:                // Get the root element
100:                Element rootMappingElement = xmlMappingConfigurationDocument
101:                        .getDocumentElement();
102:
103:                // <package> element ?
104:                NodeList packageList = rootMappingElement
105:                        .getElementsByTagNameNS(MAPPING_NS, PACKAGE_ELEMENT);
106:                for (int i = 0; i < packageList.getLength(); i++) {
107:                    Element packageElement = (Element) packageList.item(i);
108:
109:                    // get name
110:                    String packageName = XMLUtils.getAttributeValue(
111:                            packageElement, "name");
112:
113:                    // Create class mapping
114:                    NodeList classList = packageElement.getElementsByTagNameNS(
115:                            MAPPING_NS, CLASS_ELEMENT);
116:                    addClassMapping(classList, packageName + ".", true);
117:                }
118:
119:                // Now analyze single <class> element
120:                NodeList classList = rootMappingElement.getElementsByTagNameNS(
121:                        MAPPING_NS, CLASS_ELEMENT);
122:                addClassMapping(classList, "", false);
123:            }
124:
125:            /**
126:             * Add the mapping for the given list of class elements.
127:             * @param classList the list of elements
128:             * @param packageName the name of the package to use as prefix.
129:             * @param packageParent if true, package as parent node is accepted, else it
130:             *        is denied.
131:             */
132:            private void addClassMapping(final NodeList classList,
133:                    final String packageName, final boolean packageParent) {
134:
135:                // class elements ?
136:                for (int c = 0; c < classList.getLength(); c++) {
137:                    Element classElement = (Element) classList.item(c);
138:
139:                    // if package element is parent but packageElement boolean is false,
140:                    // stop
141:                    if (classElement.getParentNode().getNodeName().equals(
142:                            PACKAGE_ELEMENT)
143:                            && !packageParent) {
144:                        continue;
145:                    }
146:
147:                    // Build mapping object
148:                    ClassMapping classMapping = new ClassMapping();
149:
150:                    // class name
151:                    String name = XMLUtils.getAttributeValue(classElement,
152:                            "name");
153:                    String className = packageName + name;
154:                    classMapping.setName(className);
155:
156:                    // alias
157:                    String alias = XMLUtils.getAttributeValue(classElement,
158:                            "alias");
159:                    classMapping.setAlias(alias);
160:
161:                    // Mapping of the content of an element to an attribute
162:                    String elementAttribute = XMLUtils.getAttributeValue(
163:                            classElement, "element-attribute");
164:                    classMapping.setElementAttribute(elementAttribute);
165:
166:                    // Attributes ?
167:                    NodeList attributeList = classElement
168:                            .getElementsByTagNameNS(MAPPING_NS,
169:                                    ATTRIBUTE_ELEMENT);
170:                    for (int a = 0; a < attributeList.getLength(); a++) {
171:                        Element attributeElement = (Element) attributeList
172:                                .item(a);
173:                        // Build mapping object
174:                        AttributeMapping attributeMapping = new AttributeMapping();
175:
176:                        // name
177:                        String attributeName = XMLUtils.getAttributeValue(
178:                                attributeElement, "name");
179:                        attributeMapping.setName(attributeName);
180:
181:                        // is Element ?
182:                        String isElementName = XMLUtils.getAttributeValue(
183:                                attributeElement, "element");
184:                        if (Boolean.parseBoolean(isElementName)) {
185:                            attributeMapping.setElement();
186:                        }
187:
188:                        // is List Element ?
189:                        String isListElementName = XMLUtils.getAttributeValue(
190:                                attributeElement, "isList");
191:                        if (Boolean.parseBoolean(isListElementName)) {
192:                            attributeMapping.setListElement();
193:                        }
194:
195:                        // getter
196:                        String getter = XMLUtils.getAttributeValue(
197:                                attributeElement, "getter");
198:                        attributeMapping.setGetter(getter);
199:
200:                        // setter
201:                        String setter = XMLUtils.getAttributeValue(
202:                                attributeElement, "setter");
203:                        attributeMapping.setSetter(setter);
204:
205:                        // alias
206:                        String attributeAlias = XMLUtils.getAttributeValue(
207:                                attributeElement, "alias");
208:                        attributeMapping.setAlias(attributeAlias);
209:
210:                        // add attribute
211:                        classMapping.addAttributeMapping(attributeMapping);
212:                    }
213:
214:                    xmlMapping.addClassMapping(classMapping);
215:                }
216:            }
217:
218:            /**
219:             * Gets the XML mapping object.
220:             * @return the mapping object between classname/alias and their mapping
221:             *         description.
222:             */
223:            public XMLMapping getXmlMapping() {
224:                return xmlMapping;
225:            }
226:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.