KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > xmlconfig > mapping > XMLMappingBuilder


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: XMLMappingBuilder.java 1022 2006-08-04 11:02:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.xmlconfig.mapping;
27
28 import java.net.URL JavaDoc;
29
30 import org.objectweb.easybeans.util.xml.DocumentParser;
31 import org.objectweb.easybeans.util.xml.DocumentParserException;
32 import org.objectweb.easybeans.util.xml.XMLUtils;
33 import org.objectweb.easybeans.xmlconfig.XMLConfigurationException;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38 /**
39  * Allows to build the Mapping object for a set of classes.
40  * @author Florent Benoit
41  */

42 public class XMLMappingBuilder {
43
44     /**
45      * Namespace used for the mapping file (for validation).
46      */

47     private static final String JavaDoc MAPPING_NS = "http://easybeans.objectweb.org/xml/ns/mapping";
48
49     /**
50      * Name of the <package> element.
51      */

52     private static final String JavaDoc PACKAGE_ELEMENT = "package";
53
54     /**
55      * Name of the <class> element.
56      */

57     private static final String JavaDoc CLASS_ELEMENT = "class";
58
59     /**
60      * Name of the <attribute> element.
61      */

62     private static final String JavaDoc ATTRIBUTE_ELEMENT = "attribute";
63
64     /**
65      * URL that reference the mapping file.
66      */

67     private URL JavaDoc mappingURL = null;
68
69     /**
70      * The mapping used for the given namespace.
71      */

72     private XMLMapping xmlMapping = null;
73
74     /**
75      * Builds a new mapping builder.
76      * @param mappingURL the given url that reference the mapping file.
77      */

78     public XMLMappingBuilder(final URL JavaDoc mappingURL) {
79         this.mappingURL = mappingURL;
80         xmlMapping = new XMLMapping();
81     }
82
83     /**
84      * Build the XMLMapping object by analyzing the mapping file.
85      * @throws XMLConfigurationException if there is a failure when analyzing
86      * the XML file.
87      */

88     public void build() throws XMLConfigurationException {
89
90         // Parse the XML file and build all configuration process.
91
Document JavaDoc xmlMappingConfigurationDocument = null;
92         try {
93             xmlMappingConfigurationDocument = DocumentParser.getDocument(mappingURL, false, null);
94         } catch (DocumentParserException e) {
95             throw new XMLConfigurationException("Cannot get a document on the given url '" + mappingURL + "'.", e);
96         }
97
98         // Get the root element
99
Element JavaDoc rootMappingElement = xmlMappingConfigurationDocument.getDocumentElement();
100
101         // <package> element ?
102
NodeList JavaDoc packageList = rootMappingElement.getElementsByTagNameNS(MAPPING_NS, PACKAGE_ELEMENT);
103         for (int i = 0; i < packageList.getLength(); i++) {
104             Element JavaDoc packageElement = (Element JavaDoc) packageList.item(i);
105
106             // get name
107
String JavaDoc packageName = XMLUtils.getAttributeValue(packageElement, "name");
108
109             // Create class mapping
110
NodeList JavaDoc classList = packageElement.getElementsByTagNameNS(MAPPING_NS, CLASS_ELEMENT);
111             addClassMapping(classList, packageName + ".", true);
112         }
113
114         // Now analyze single <class> element
115
NodeList JavaDoc classList = rootMappingElement.getElementsByTagNameNS(MAPPING_NS, CLASS_ELEMENT);
116         addClassMapping(classList, "", false);
117     }
118
119     /**
120      * Add the mapping for the given list of class elements.
121      * @param classList the list of elements
122      * @param packageName the name of the package to use as prefix.
123      * @param packageParent if true, package as parent node is accepted, else it
124      * is denied.
125      */

126     private void addClassMapping(final NodeList JavaDoc classList, final String JavaDoc packageName, final boolean packageParent) {
127
128         // class elements ?
129
for (int c = 0; c < classList.getLength(); c++) {
130             Element JavaDoc classElement = (Element JavaDoc) classList.item(c);
131
132             // if package element is parent but packageElement boolean is false,
133
// stop
134
if (classElement.getParentNode().getNodeName().equals(PACKAGE_ELEMENT) && !packageParent) {
135                 continue;
136             }
137
138             // Build mapping object
139
ClassMapping classMapping = new ClassMapping();
140
141             // class name
142
String JavaDoc name = XMLUtils.getAttributeValue(classElement, "name");
143             String JavaDoc className = packageName + name;
144             classMapping.setName(className);
145
146             // alias
147
String JavaDoc alias = XMLUtils.getAttributeValue(classElement, "alias");
148             classMapping.setAlias(alias);
149
150             // Attributes ?
151
NodeList JavaDoc attributeList = classElement.getElementsByTagNameNS(MAPPING_NS, ATTRIBUTE_ELEMENT);
152             for (int a = 0; a < attributeList.getLength(); a++) {
153                 Element JavaDoc attributeElement = (Element JavaDoc) attributeList.item(a);
154                 // Build mapping object
155
AttributeMapping attributeMapping = new AttributeMapping();
156
157                 // name
158
String JavaDoc attributeName = XMLUtils.getAttributeValue(attributeElement, "name");
159                 attributeMapping.setName(attributeName);
160
161                 // alias
162
String JavaDoc attributeAlias = XMLUtils.getAttributeValue(attributeElement, "alias");
163                 attributeMapping.setAlias(attributeAlias);
164
165                 // add attribute
166
classMapping.addAttributeMapping(attributeMapping);
167             }
168
169             xmlMapping.addClassMapping(classMapping);
170         }
171     }
172
173     /**
174      * Gets the XML mapping object.
175      * @return the mapping object between classname/alias and their mapping
176      * description.
177      */

178     public XMLMapping getXmlMapping() {
179         return xmlMapping;
180     }
181 }
182
Popular Tags