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: }
|