1 16 17 package org.apache.commons.betwixt.schema; 18 19 import java.beans.IntrospectionException ; 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Collection ; 24 25 import org.apache.commons.betwixt.ElementDescriptor; 26 import org.apache.commons.betwixt.XMLBeanInfo; 27 import org.apache.commons.betwixt.XMLIntrospector; 28 29 35 public class Schema { 36 37 private List elements = new ArrayList (); 38 private List complexTypes = new ArrayList (); 39 private List simpleTypes = new ArrayList (); 40 41 private XMLIntrospector introspector; 42 43 public Schema() { 44 this(new XMLIntrospector()); 45 } 46 47 public Schema(XMLIntrospector introspector) { 48 this.introspector = introspector; 49 } 50 51 57 public XMLBeanInfo introspect(Class type) throws IntrospectionException { 58 return introspector.introspect(type); 59 } 60 61 65 public List getComplexTypes() { 66 return complexTypes; 67 } 68 69 70 74 public void addComplexType(GlobalComplexType complexType) { 75 complexTypes.add(complexType); 76 } 77 78 79 83 public List getElements() { 84 return elements; 85 } 86 87 91 public void addElement(GlobalElement element) { 92 elements.add(element); 93 } 94 95 99 public List getSimpleTypes() { 100 return simpleTypes; 101 } 102 103 107 public void addSimpleType(SimpleType simpleType) { 108 simpleTypes.add(simpleType); 109 } 110 111 112 116 public void addGlobalElementType(TranscriptionConfiguration configuration, ElementDescriptor elementDescriptor) throws IntrospectionException { 117 GlobalElement element = new GlobalElement( 120 elementDescriptor.getLocalName(), 121 elementDescriptor.getPropertyType().getName()); 122 addElement(element); 123 124 GlobalComplexType type = new GlobalComplexType(configuration, elementDescriptor, this); 125 addComplexType(type); 126 } 127 128 public boolean equals(Object obj) { 129 boolean result = false; 130 if (obj instanceof Schema) { 131 Schema schema = (Schema) obj; 132 result = 133 equalContents(elements, schema.elements) && 134 equalContents(complexTypes, schema.complexTypes) && 135 equalContents(simpleTypes, schema.simpleTypes); 136 } 137 return result; 138 } 139 140 private boolean equalContents(Collection one, Collection two) 141 { 142 if (one.size() != two.size()) { 144 return false; 145 } 146 for (Iterator it=one.iterator();it.hasNext();) { 147 Object object = it.next(); 148 if (!two.contains(object)) { 149 return false; 150 } 151 } 152 return true; 153 } 154 155 public int hashCode() { 156 return 0; 157 } 158 159 160 161 public String toString() { 162 StringBuffer buffer = new StringBuffer (); 163 buffer.append("<?xml version='1.0'?>"); 164 buffer.append("<xsd:schema xmlns:xsd='http://www.w3c.org/2001/XMLSchema'>"); 165 166 for (Iterator it=simpleTypes.iterator(); it.hasNext();) { 167 buffer.append(it.next()); 168 } 169 170 for (Iterator it=complexTypes.iterator(); it.hasNext();) { 171 buffer.append(it.next()); 172 } 173 174 175 for (Iterator it=elements.iterator(); it.hasNext();) { 176 buffer.append(it.next()); 177 } 178 buffer.append("</xsd:schema>"); 179 return buffer.toString(); 180 } 181 } 182 | Popular Tags |