1 19 package org.enhydra.zeus.binder; 20 21 import java.io.IOException ; 22 import java.util.BitSet ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Vector ; 29 30 import org.enhydra.zeus.Binder; 32 import org.enhydra.zeus.Binding; 33 import org.enhydra.zeus.ZeusDefaults; 34 import org.enhydra.zeus.binding.Property; 35 import org.enhydra.zeus.binding.AtomicProperty; 36 import org.enhydra.zeus.binding.Container; 37 import org.enhydra.zeus.binding.ContainerProperty; 38 import org.enhydra.zeus.source.DTDSource; 39 import org.enhydra.zeus.util.CapitalizationUtils; 40 import org.enhydra.zeus.util.DTDUtils; 41 42 import com.wutka.dtd.DTD; 44 import com.wutka.dtd.DTDAny; 45 import com.wutka.dtd.DTDAttribute; 46 import com.wutka.dtd.DTDCardinal; 47 import com.wutka.dtd.DTDContainer; 48 import com.wutka.dtd.DTDDecl; 49 import com.wutka.dtd.DTDElement; 50 import com.wutka.dtd.DTDEmpty; 51 import com.wutka.dtd.DTDEnumeration; 52 import com.wutka.dtd.DTDItem; 53 import com.wutka.dtd.DTDMixed; 54 import com.wutka.dtd.DTDName; 55 import com.wutka.dtd.DTDPCData; 56 57 69 public class DTDBinder extends BaseBinder { 70 71 72 protected DTD dtd; 73 74 75 protected Hashtable elements; 76 77 78 protected String rootElementName; 79 80 90 public DTDBinder(DTDSource source, String rootElementName) { 91 super(source); 92 this.rootElementName = rootElementName; 93 elements = new Hashtable (); 94 } 95 96 105 public DTDBinder(DTDSource source) { 106 this(source, null); 107 } 108 109 116 public String getRootElementName() { 117 return rootElementName; 118 } 119 120 134 public List getBindings() throws IOException { 135 dtd = ((DTDSource)source).getDTD(); 137 138 List bindings = new LinkedList (); 140 141 Vector dtdElements = dtd.getItemsByType(DTDElement.class); 143 144 for (Iterator i = dtdElements.iterator(); i.hasNext(); ) { 146 DTDElement dtdElement = (DTDElement)i.next(); 147 buildNamesAndTypes(dtdElement); 148 } 149 150 for (Iterator i = dtdElements.iterator(); i.hasNext(); ) { 152 DTDElement dtdElement = (DTDElement)i.next(); 153 Binding binding = convertToBinding(dtdElement); 154 if (rootElementName != null) { 155 if (dtdElement.getName().equals(rootElementName)) { 156 binding.setIsXMLRootElement(true); 157 } 158 } else if (dtdElement == dtd.rootElement) { 159 binding.setIsXMLRootElement(true); 160 } 161 bindings.add(binding); 162 } 163 164 return bindings; 165 } 166 167 175 private void buildNamesAndTypes(DTDElement dtdElement) { 176 String xmlName = dtdElement.getName(); 178 179 String xmlType = xmlName; 180 181 if ((isCollapsingSimpleElements) && 183 (DTDUtils.isSimpleElement(dtdElement, isIgnoringIDAttributes))) { 184 185 xmlType = "string"; 186 } 187 188 elements.put(xmlName, xmlType); 189 } 190 191 202 private Binding convertToBinding(DTDElement dtdElement) { 203 String xmlName = dtdElement.getName(); 205 String xmlType = (String )elements.get(xmlName); 206 207 BitSet modifiers = new BitSet (); 209 modifiers.set(Property.SOURCE_ELEMENT); 210 211 ContainerProperty container = 213 new ContainerProperty(xmlName, xmlType); 214 container.setModifier(modifiers); 215 216 handleContent(container, dtdElement.getContent()); 218 219 handleAttributes(container, dtdElement); 221 222 return container; 223 } 224 225 236 private void handleAttributes(Container container, DTDElement dtdElement) { 237 Hashtable elementAttributes = dtdElement.attributes; 238 Enumeration e = elementAttributes.elements(); 239 while (e.hasMoreElements()) { 240 DTDAttribute attribute = (DTDAttribute)e.nextElement(); 241 246 DTDDecl decl = attribute.getDecl(); 247 248 BitSet modifiers = new BitSet (); 250 modifiers.set(Property.ACCESS_PRIVATE); 251 modifiers.set(Property.SOURCE_ATTLIST); 252 253 if ((decl != null) && (decl.equals(DTDDecl.FIXED))) { 257 modifiers.set(Property.MUTABILITY_FINAL); 258 modifiers.set(Property.STORAGE_STATIC); 259 } 260 261 267 String xmlName = attribute.getName(); 268 String xmlType = "string"; 269 270 Vector enumeration = 272 (attribute.getType() instanceof DTDEnumeration) ? 273 ((DTDEnumeration)attribute.getType()).getItemsVec() : 274 null; 275 276 AtomicProperty atomicProperty = 277 new AtomicProperty(xmlName, 278 "", 279 xmlType, 280 ZeusDefaults.SCHEMA_NAMESPACE_URI, 281 modifiers, 282 enumeration, 283 attribute.getDefaultValue()); 284 285 container.addProperty(atomicProperty); 286 } 287 } 288 289 304 private void handleContent(Container container, DTDItem content) { 305 handleContent(container, content, DTDCardinal.NONE); 306 } 307 308 322 private void handleContent(Container container, DTDItem content, 323 DTDCardinal transCardinality) { 324 if (content instanceof DTDEmpty) { 326 } else if (content instanceof DTDPCData) { 328 334 String xmlName = ZeusDefaults.PCDATA_XML_NAME; 335 String xmlType = "string"; 336 337 AtomicProperty property = 338 new AtomicProperty(xmlName, "", 339 xmlType, ZeusDefaults.SCHEMA_NAMESPACE_URI); 340 341 container.addProperty(property); 342 } else if (content instanceof DTDName) { 343 BitSet modifiers = new BitSet (); 345 modifiers.set(Property.ACCESS_PRIVATE); 346 modifiers.set(Property.SOURCE_ELEMENT); 347 348 String xmlName = ((DTDName)content).getValue(); 350 351 String xmlType = (String )elements.get(xmlName); 353 354 AtomicProperty property = new AtomicProperty(xmlName, xmlType); 355 property.setModifier(modifiers); 356 357 DTDCardinal cardinality = ((DTDName)content).getCardinal(); 359 360 if ((cardinality.equals(DTDCardinal.ZEROMANY)) || 361 (cardinality.equals(DTDCardinal.ONEMANY))) { 362 property.setIsCollection(true); 363 } else if ((transCardinality.equals(DTDCardinal.ZEROMANY)) || 364 (transCardinality.equals(DTDCardinal.ONEMANY))) { 365 366 property.setIsCollection(true); 367 } 368 369 container.addProperty(property); 370 } else if (content instanceof DTDAny) { 371 String xmlName = ""; 372 String xmlType = "anyType"; 373 374 AtomicProperty property = 375 new AtomicProperty(xmlName, "", 376 xmlType, ZeusDefaults.SCHEMA_NAMESPACE_URI); 377 378 property.setIsCollection(true); 379 380 container.addProperty(property); 381 } else if (content instanceof DTDContainer) { 382 Vector items = ((DTDContainer)content).getItemsVec(); 384 385 DTDCardinal cardinality = content.getCardinal(); 387 388 for (Iterator i = items.iterator(); i.hasNext(); ) { 389 DTDItem subContent = (DTDItem)i.next(); 390 391 handleContent(container, subContent, cardinality); 392 } 393 } 394 } 395 } 396 | Popular Tags |