1 19 package org.enhydra.zeus.binder; 20 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.LinkedList ; 24 import java.util.List ; 25 26 import org.enhydra.zeus.Binder; 28 import org.enhydra.zeus.Binding; 29 import org.enhydra.zeus.Source; 30 import org.enhydra.zeus.ZeusDefaults; 31 import org.enhydra.zeus.binding.AtomicProperty; 32 import org.enhydra.zeus.binding.ContainerProperty; 33 import org.enhydra.zeus.util.NamingUtils; 34 import org.enhydra.zeus.util.SchemaUtils; 35 36 import org.jdom.Attribute; 38 import org.jdom.Document; 39 import org.jdom.Element; 40 import org.jdom.Namespace; 41 42 52 public class SchemaBinder extends BaseBinder { 53 54 55 private Namespace schemaNamespace; 56 57 66 public SchemaBinder(Source source) { 67 super(source); 68 69 schemaNamespace = 71 Namespace.getNamespace(ZeusDefaults.SCHEMA_NAMESPACE_URI); 72 } 73 74 81 public String getSchemaNamespaceURI() { 82 return schemaNamespace.getURI(); 83 } 84 85 96 public void setSchemaNamespaceURI(String schemaNamespaceURI) { 97 schemaNamespace = Namespace.getNamespace(schemaNamespaceURI); 98 } 99 100 114 public List getBindings() throws IOException { 115 Document schema = source.getDocument(); 116 Element root = schema.getRootElement(); 117 118 return convertToBindings(root); 119 } 120 121 132 private List convertToBindings(Element element) { 133 134 List bindings = new LinkedList (); 136 137 List elements = element.getChildren("element", schemaNamespace); 139 for (Iterator i = elements.iterator(); i.hasNext(); ) { 140 Element nextElement = (Element)i.next(); 141 Attribute elementName = nextElement.getAttribute("name"); 142 if (elementName == null) { 143 throw new IllegalArgumentException ("All root-level elements " + 145 "should be named types (with a 'name' attribute)."); 146 } 147 148 bindings.add(convertDefinitionToBinding(nextElement)); 150 } 151 152 157 158 160 return bindings; 161 } 162 163 173 private Binding convertDefinitionToBinding(Element element) { 174 175 String xmlName = element.getAttribute("name").getValue(); 177 String xmlType = xmlName; 178 179 ContainerProperty container = 180 new ContainerProperty(xmlName, xmlType); 181 182 Attribute type = element.getAttribute("type"); 184 if (type != null) { 185 String schemaType = type.getValue(); 187 AtomicProperty property = 189 new AtomicProperty("value", schemaType); 190 container.addProperty(property); 191 } else { 192 Element complexType = 194 element.getChild("complexType", schemaNamespace); 195 if (complexType == null) { 196 throw new IllegalArgumentException ("Illegal schema: " + 198 "No complexType defined for this element."); 199 } 200 201 Attribute baseType = complexType.getAttribute("baseType"); 203 if (baseType != null) { 204 String schemaType = baseType.getValue(); 206 207 AtomicProperty property = 209 new AtomicProperty("value", schemaType); 210 container.addProperty(property); 211 } 212 213 Element parent = complexType.getChild("sequence", schemaNamespace); 215 if (parent == null) { 216 parent = complexType; 217 } 218 219 List attributes = parent.getChildren("attribute", schemaNamespace); 221 for (Iterator i = attributes.iterator(); i.hasNext(); ) { 222 Element theAttribute = (Element)i.next(); 223 Attribute propertyName = theAttribute.getAttribute("name"); 224 if (propertyName == null) { 225 throw new IllegalArgumentException ("Attributes must have " + 227 "a name (specified by the 'name' attribute)."); 228 } 229 230 Attribute schemaType = theAttribute.getAttribute("type"); 231 if (schemaType == null) { 232 throw new IllegalArgumentException ("Attributes must have " + 234 "a type (specified by the 'type' attribute)."); 235 } 236 237 AtomicProperty property = 239 new AtomicProperty(propertyName.getValue(), 240 "", 241 "string", 242 ZeusDefaults.SCHEMA_NAMESPACE_URI); 243 container.addProperty(property); 244 } 245 246 List elements = parent.getChildren("element", schemaNamespace); 248 for (Iterator i = elements.iterator(); i.hasNext(); ) { 249 Element theElement = (Element)i.next(); 250 251 Attribute ref = theElement.getAttribute("ref"); 253 if (ref == null) { 254 } else { 255 String propertyName = ref.getValue(); 257 String propertyType = 258 NamingUtils.getJavaClassName(propertyName); 259 260 AtomicProperty property 262 = new AtomicProperty(propertyName, 263 propertyType); 264 265 Attribute maxOccurs = theElement.getAttribute("maxOccurs"); 267 if (maxOccurs != null) { 268 String value = maxOccurs.getValue(); 269 270 if (value.equals("unbounded")) { 272 property.setIsCollection(true); 273 } else { 274 try { 275 int maxOccursInt = Integer.parseInt(value); 276 if (maxOccursInt > 1) { 277 property.setIsCollection(true); 278 } 279 } catch (NumberFormatException ignored) { 280 } 282 } 283 } 284 285 container.addProperty(property); 286 } 287 } 288 } 289 290 return container; 291 } 292 } 293 | Popular Tags |