1 22 23 package org.xquark.schema.loader; 24 25 import java.util.StringTokenizer ; 26 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.SAXException ; 29 import org.xquark.schema.*; 30 import org.xquark.util.DefaultElementHandler; 31 import org.xquark.util.ElementHandler; 32 33 class SimpleTypeHandler extends DefaultElementHandler implements SchemaConstants 34 { 35 private static final String RCSRevision = "$Revision: 1.1 $"; 36 private static final String RCSName = "$Name: $"; 37 private Loader loader = null; 38 private Type type = null; 39 private boolean simpleContent; 40 private boolean redefine; 41 private boolean alreadyRedefined = false; 42 43 SimpleTypeHandler(Loader loader, Type type, boolean simpleContent, boolean redefine) 44 throws SAXException 45 { 46 this.loader = loader; 47 this.type = type; 48 this.simpleContent = simpleContent; 49 this.redefine = redefine; 50 } 51 52 public ElementHandler startElement(String strNamespaceURI, String localName, Attributes atts) 53 throws SAXException 54 { 55 if ( strNamespaceURI.equals(XMLSCHEMA_URI) ) { 56 if ( localName.equals(RESTRICTION_TAG) ) { 58 if ( !redefine ) { 59 Type baseType = null; 61 try { 62 baseType = loader.getType(atts.getValue("", BASE_ATTR)); 63 } 64 catch ( SchemaException se ) { 65 String errMsg = "Error while processing localName -> " + localName; 66 loader.reportLoadingError(errMsg, se); 67 } 68 type.setBaseType(baseType); 69 if ( simpleContent ) ((ComplexType)type).setExtension(false); 70 } 71 else { 72 alreadyRedefined = true; 74 ((SimpleType)type).setVariety(SimpleType.VARIETY_ATOMIC); 75 String baseTypeName = atts.getValue("", BASE_ATTR); 77 if ( !this.type.getName().equals(baseTypeName) ) { 78 String errCode = "src-redefine.5"; 79 String errMsg = "Error while processing localName -> " + localName; 80 loader.reportLoadingError(errMsg, new SchemaException(errCode)); 81 } 82 } 83 return this; 84 } 85 86 Facet facet = loader.buildFacet(localName, atts); 89 if (facet != null) { 90 if ( simpleContent ) { 91 SimpleType contentType = null; 92 if (((ComplexType)type).getContentModel().getModel() == null) { 93 contentType = new SimpleType(type.getSchema(), null, new SimpleBaseTypeRef(((ComplexType)type))); 94 ((ComplexType)type).getContentModel().setModel(contentType); 95 } else { 96 contentType = (SimpleType)((ComplexType)type).getContentModel().getModel(); 97 } 98 try { 99 contentType.setFacet(facet); 100 } catch ( SchemaException e ) { 101 String errMsg = "Error while processing localName -> " + localName; 102 loader.reportLoadingError(errMsg, e); 103 } 104 } 105 else { 106 if ( redefine && !alreadyRedefined ) { 107 String errCode = "src-redefine.5"; 108 String errMsg = "Error while processing localName -> " + localName; 109 loader.reportLoadingError(errMsg, new SchemaException(errCode)); 110 } 111 try { 112 ((SimpleType)type).setFacet(facet); 113 } catch ( SchemaException e ) { 114 String errMsg = "Error while processing localName -> " + localName; 115 loader.reportLoadingError(errMsg, e); 116 } 117 } 118 119 return this; 120 } 121 122 if ( redefine ) { 124 String errCode = "src-redefine.5"; 125 String errMsg = "Error while processing localName -> " + localName; 126 loader.reportLoadingError(errMsg, new SchemaException(errCode)); 127 } 128 129 if ( simpleContent && localName.equals(EXTENSION_TAG) ) { 132 Type baseType = null; 134 try { 135 baseType = loader.getType(atts.getValue("", BASE_ATTR)); 136 } 137 catch ( SchemaException se ) { 138 String errMsg = "Error while processing localName -> " + localName; 139 loader.reportLoadingError(errMsg, se); 140 } 141 type.setBaseType(baseType); 142 ((ComplexType)type).setExtension(true); 143 return this; 144 } 145 146 else if (localName.equals(SIMPLE_TYPE_TAG)) { 147 SimpleType inlineType = null; 149 try { 150 inlineType = loader.buildSimpleType(atts); 151 } 152 catch ( SchemaException se ) { 153 String errMsg = "Error while processing localName -> " + localName; 154 loader.reportLoadingError(errMsg, se); 155 } 156 if ( simpleContent ) { 157 ((ComplexType)type).getContentModel().setModel(inlineType); 158 return new SimpleTypeHandler(loader, inlineType, false, false); 159 } 160 else { 161 switch ( ((SimpleType)type).getVariety() ) { 162 case SimpleType.VARIETY_ATOMIC: 163 if ( type.getBaseType() != null ) { 165 String errCode = "src-simple-type.2"; 166 String errMsg = "Error while processing localName -> " + localName; 167 loader.reportLoadingError(errMsg, new SchemaException(errCode)); 168 } 169 type.setBaseType(inlineType); 170 171 case SimpleType.VARIETY_LIST: 172 if ( ((SimpleType)type).getItemType() != null ) { 173 String errCode = "src-simple-type.3"; 174 String errMsg = "Error while processing localName -> " + localName; 175 loader.reportLoadingError(errMsg, new SchemaException(errCode)); 176 } 177 ((SimpleType)type).setItemType(inlineType); 178 179 case SimpleType.VARIETY_UNION: 180 ((SimpleType)type).addMemberType(inlineType); 181 } 182 return new SimpleTypeHandler(loader, inlineType, false, false); 183 } 184 } 185 186 else if ( localName.equals(LIST_TAG) ) { 189 ((SimpleType)type).setVariety(SimpleType.VARIETY_LIST); 190 String itemTypeName = atts.getValue("", ITEM_TYPE_ATTR); 192 if ( itemTypeName != null ) { 193 try { 194 ((SimpleType)type).setItemType(loader.getType(itemTypeName)); 195 } 196 catch ( SchemaException se ) { 197 String errMsg = "Error while processing localName -> " + localName; 198 loader.reportLoadingError(errMsg, se); 199 } 200 } 201 return this; 202 } 203 204 else if ( localName.equals(UNION_TAG) ) { 207 ((SimpleType)type).setVariety(SimpleType.VARIETY_UNION); 208 String memberTypes = atts.getValue("", MEMBER_TYPES_ATTR); 210 if ( memberTypes != null ) { 211 StringTokenizer stMemberTypes = new StringTokenizer (memberTypes); 212 while (stMemberTypes.hasMoreTokens()) { 213 String memberTypeName = stMemberTypes.nextToken(); 214 try { 215 ((SimpleType)type).addMemberType(loader.getType(memberTypeName)); 216 } 217 catch ( SchemaException se ) { 218 String errMsg = "Error while processing localName -> " + localName; 219 loader.reportLoadingError(errMsg, se); 220 } 221 } 222 } 223 return this; 224 } 225 226 else if ( localName.equals(ATTRIBUTE_TAG) ) { 229 AttributeDeclaration decl = null; 231 try { 232 decl = loader.buildAttributeDeclaration(atts, ((ComplexType)type)); 233 ((ComplexType)type).register(decl); 234 } catch ( SchemaException e ) { 235 String errMsg = "Error while processing localName -> " + localName; 236 loader.reportLoadingError(errMsg, e); 237 } 238 return new AttributeTypeHandler(loader, decl); 239 } 240 241 else if ( localName.equals(ATTRIBUTE_GROUP_TAG) ) { 244 String refName = atts.getValue("", REF_ATTR); 246 try { 248 SchemaComponent ref = loader.getRef(refName, Schema.ATTRIBUTE_GROUP_DEFINITION); 249 ((ComplexType)type).register(ref); 250 } catch ( SchemaException e ) { 251 String errMsg = "Error while processing localName -> " + localName; 252 loader.reportLoadingError(errMsg, e); 253 } 254 return this; 255 } 256 257 else if (localName.equals(ANY_ATTRIBUTE_TAG)) { 260 Wildcard wildcard = null; 262 try { 263 wildcard = loader.buildWildcard(atts); 264 } catch ( SchemaException e ) { 265 String errMsg = "Error while processing localName -> " + localName; 266 loader.reportLoadingError(errMsg, e); 267 } 268 ((ComplexType)type).setAttributeWildcard(wildcard); 269 return this; 270 } 271 272 else if ( localName.equals(ANNOTATION_TAG) ) { 273 return new AnnotationHandler(); 274 } 275 } 276 277 loader.notifyUnknownElement(strNamespaceURI, localName); 278 return this; 279 } 280 281 } 282 | Popular Tags |