1 25 26 package org.objectweb.easybeans.xmlconfig.mapping; 27 28 import java.net.URL ; 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 ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NodeList ; 37 38 42 public class XMLMappingBuilder { 43 44 47 private static final String MAPPING_NS = "http://easybeans.objectweb.org/xml/ns/mapping"; 48 49 52 private static final String PACKAGE_ELEMENT = "package"; 53 54 57 private static final String CLASS_ELEMENT = "class"; 58 59 62 private static final String ATTRIBUTE_ELEMENT = "attribute"; 63 64 67 private URL mappingURL = null; 68 69 72 private XMLMapping xmlMapping = null; 73 74 78 public XMLMappingBuilder(final URL mappingURL) { 79 this.mappingURL = mappingURL; 80 xmlMapping = new XMLMapping(); 81 } 82 83 88 public void build() throws XMLConfigurationException { 89 90 Document 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 Element rootMappingElement = xmlMappingConfigurationDocument.getDocumentElement(); 100 101 NodeList packageList = rootMappingElement.getElementsByTagNameNS(MAPPING_NS, PACKAGE_ELEMENT); 103 for (int i = 0; i < packageList.getLength(); i++) { 104 Element packageElement = (Element ) packageList.item(i); 105 106 String packageName = XMLUtils.getAttributeValue(packageElement, "name"); 108 109 NodeList classList = packageElement.getElementsByTagNameNS(MAPPING_NS, CLASS_ELEMENT); 111 addClassMapping(classList, packageName + ".", true); 112 } 113 114 NodeList classList = rootMappingElement.getElementsByTagNameNS(MAPPING_NS, CLASS_ELEMENT); 116 addClassMapping(classList, "", false); 117 } 118 119 126 private void addClassMapping(final NodeList classList, final String packageName, final boolean packageParent) { 127 128 for (int c = 0; c < classList.getLength(); c++) { 130 Element classElement = (Element ) classList.item(c); 131 132 if (classElement.getParentNode().getNodeName().equals(PACKAGE_ELEMENT) && !packageParent) { 135 continue; 136 } 137 138 ClassMapping classMapping = new ClassMapping(); 140 141 String name = XMLUtils.getAttributeValue(classElement, "name"); 143 String className = packageName + name; 144 classMapping.setName(className); 145 146 String alias = XMLUtils.getAttributeValue(classElement, "alias"); 148 classMapping.setAlias(alias); 149 150 NodeList attributeList = classElement.getElementsByTagNameNS(MAPPING_NS, ATTRIBUTE_ELEMENT); 152 for (int a = 0; a < attributeList.getLength(); a++) { 153 Element attributeElement = (Element ) attributeList.item(a); 154 AttributeMapping attributeMapping = new AttributeMapping(); 156 157 String attributeName = XMLUtils.getAttributeValue(attributeElement, "name"); 159 attributeMapping.setName(attributeName); 160 161 String attributeAlias = XMLUtils.getAttributeValue(attributeElement, "alias"); 163 attributeMapping.setAlias(attributeAlias); 164 165 classMapping.addAttributeMapping(attributeMapping); 167 } 168 169 xmlMapping.addClassMapping(classMapping); 170 } 171 } 172 173 178 public XMLMapping getXmlMapping() { 179 return xmlMapping; 180 } 181 } 182 | Popular Tags |