1 19 20 package org.netbeans.modules.xml.schema.model.impl; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.Set ; 27 import org.netbeans.modules.xml.schema.model.Annotation; 28 import org.netbeans.modules.xml.xam.Component; 29 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 30 import org.netbeans.modules.xml.schema.model.GlobalType; 31 import org.netbeans.modules.xml.schema.model.Notation; 32 import org.netbeans.modules.xml.schema.model.SchemaComponent; 33 import org.netbeans.modules.xml.schema.model.Form; 34 import org.netbeans.modules.xml.schema.model.GlobalAttribute; 35 import org.netbeans.modules.xml.schema.model.GlobalAttributeGroup; 36 import org.netbeans.modules.xml.schema.model.GlobalComplexType; 37 import org.netbeans.modules.xml.schema.model.GlobalElement; 38 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 39 import org.netbeans.modules.xml.schema.model.GlobalGroup; 40 import org.netbeans.modules.xml.schema.model.Import; 41 import org.netbeans.modules.xml.schema.model.Include; 42 import org.netbeans.modules.xml.schema.model.Redefine; 43 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 44 import org.netbeans.modules.xml.schema.model.Schema; 45 import org.netbeans.modules.xml.schema.model.Schema.Block; 46 import org.netbeans.modules.xml.schema.model.Schema.Final; 47 import org.netbeans.modules.xml.schema.model.SchemaModel; 48 import org.netbeans.modules.xml.schema.model.SchemaModelReference; 49 import org.netbeans.modules.xml.schema.model.visitor.SchemaVisitor; 50 import org.w3c.dom.Element ; 51 52 56 57 public class SchemaImpl extends SchemaComponentImpl implements Schema { 58 59 public static final String TNS = "tns"; 61 private Component foreignParent; 62 63 64 public SchemaImpl(SchemaModelImpl model) { 65 this(model, createNewComponent(SchemaElements.SCHEMA, model)); 66 } 67 68 public SchemaImpl(SchemaModelImpl model, Element e){ 69 super(model,e); 70 } 71 72 76 public Class <? extends SchemaComponent> getComponentType() { 77 return Schema.class; 78 } 79 80 public Collection <SchemaModelReference> getSchemaReferences() { 81 return getChildren(SchemaModelReference.class); 82 } 83 84 87 public void accept(SchemaVisitor visitor) { 88 visitor.visit(this); 89 } 90 91 public Collection <GlobalElement> getElements() { 92 return getChildren(GlobalElement.class); 93 } 94 95 public void removeElement(GlobalElement element) { 96 removeChild(ELEMENTS_PROPERTY, element); 97 } 98 99 public void addElement(GlobalElement element) { 100 appendChild(ELEMENTS_PROPERTY, element); 101 } 102 103 public Collection <GlobalAttributeGroup> getAttributeGroups() { 104 return getChildren(GlobalAttributeGroup.class); 105 } 106 107 public void removeAttributeGroup(GlobalAttributeGroup group) { 108 removeChild(ATTRIBUTE_GROUPS_PROPERTY, group); 109 } 110 111 public void addAttributeGroup(GlobalAttributeGroup group) { 112 appendChild(ATTRIBUTE_GROUPS_PROPERTY, group); 113 } 114 115 public void removeExternalReference(SchemaModelReference ref) { 116 removeChild(SCHEMA_REFERENCES_PROPERTY, ref); 117 } 118 119 public void addExternalReference(SchemaModelReference ref) { 120 List <Class <? extends SchemaComponent>> afterList = new ArrayList <Class <? extends SchemaComponent>>(); 121 afterList.add(Annotation.class); 122 afterList.add(SchemaModelReference.class); 123 addAfter(SCHEMA_REFERENCES_PROPERTY, ref, afterList); 124 } 125 126 public Collection <GlobalComplexType> getComplexTypes() { 127 return getChildren(GlobalComplexType.class); 128 } 129 130 public void removeComplexType(GlobalComplexType type) { 131 removeChild(COMPLEX_TYPES_PROPERTY, type); 132 } 133 134 public void addComplexType(GlobalComplexType type) { 135 appendChild(COMPLEX_TYPES_PROPERTY, type); 136 } 137 138 public Collection <GlobalAttribute> getAttributes() { 139 return getChildren(GlobalAttribute.class); 140 } 141 142 public void addAttribute(GlobalAttribute attr) { 143 appendChild(ATTRIBUTES_PROPERTY, attr); 144 } 145 146 public void removeAttribute(GlobalAttribute attr) { 147 removeChild(ATTRIBUTES_PROPERTY, attr); 148 } 149 150 public void setVersion(String ver) { 151 setAttribute(VERSION_PROPERTY, SchemaAttributes.VERSION, ver); 152 } 153 154 public String getVersion() { 155 return getAttribute(SchemaAttributes.VERSION); 156 } 157 158 public void setLanguage(String language) { 159 setAttribute(LANGUAGE_PROPERTY, SchemaAttributes.LANGUAGE, language); 160 } 161 162 public String getLanguage() { 163 return getAttribute(SchemaAttributes.LANGUAGE); 164 } 165 166 public void setFinalDefault(Set <Final> finalDefault) { 167 setAttribute(FINAL_DEFAULT_PROPERTY, SchemaAttributes.FINAL_DEFAULT, 168 finalDefault == null ? null : 169 Util.convertEnumSet(Final.class, finalDefault)); 170 } 171 172 public Set <Final> getFinalDefault() { 173 String s = getAttribute(SchemaAttributes.FINAL_DEFAULT); 174 return s == null ? null : Util.valuesOf(Final.class, s); 175 } 176 177 public Set <Final> getFinalDefaultEffective() { 178 Set <Final> v = getFinalDefault(); 179 return v == null ? getFinalDefaultDefault() : v; 180 } 181 182 public Set <Final> getFinalDefaultDefault() { 183 return new DerivationsImpl.DerivationSet<Final>(); 184 } 185 186 public void setTargetNamespace(String uri) { 187 String currentTargetNamespace = getTargetNamespace(); 188 setAttribute(TARGET_NAMESPACE_PROPERTY, SchemaAttributes.TARGET_NS, uri); 189 ensureValueNamespaceDeclared(uri, currentTargetNamespace, TNS); 190 } 191 192 public String getTargetNamespace() { 193 return getAttribute(SchemaAttributes.TARGET_NS); 194 } 195 196 public void setElementFormDefault(Form form) { 197 setAttribute(ELEMENT_FORM_DEFAULT_PROPERTY, SchemaAttributes.ELEM_FORM_DEFAULT, form); 198 } 199 200 public Form getElementFormDefault() { 201 String s = getAttribute(SchemaAttributes.ELEM_FORM_DEFAULT); 202 return s == null ? null : Util.parse(Form.class, s); 203 } 204 205 public void setAttributeFormDefault(Form form) { 206 setAttribute(ATTRIBUTE_FORM_DEFAULT_PROPERTY, SchemaAttributes.ATTR_FORM_DEFAULT, form); 207 } 208 209 public Form getAttributeFormDefault() { 210 String s = getAttribute(SchemaAttributes.ATTR_FORM_DEFAULT); 211 return s == null ? null : Util.parse(Form.class, s); 212 } 213 214 public Collection <GlobalSimpleType> getSimpleTypes() { 215 return getChildren(GlobalSimpleType.class); 216 } 217 218 public void removeSimpleType(GlobalSimpleType type) { 219 removeChild(SIMPLE_TYPES_PROPERTY, type); 220 } 221 222 public void addSimpleType(GlobalSimpleType type) { 223 appendChild(SIMPLE_TYPES_PROPERTY, type); 224 } 225 226 public Collection <GlobalGroup> getGroups() { 227 return getChildren(GlobalGroup.class); 228 } 229 230 public void removeGroup(GlobalGroup group) { 231 removeChild(GROUPS_PROPERTY, group); 232 } 233 234 public void addGroup(GlobalGroup group) { 235 appendChild(GROUPS_PROPERTY, group); 236 } 237 238 public Collection <Notation> getNotations() { 239 return getChildren(Notation.class); 240 } 241 242 public void removeNotation(Notation notation) { 243 removeChild(NOTATIONS_PROPERTY, notation); 244 } 245 246 public void addNotation(Notation notation) { 247 appendChild(NOTATIONS_PROPERTY, notation); 248 } 249 250 public void setBlockDefault(Set <Block> blockDefault) { 251 setAttribute(BLOCK_DEFAULT_PROPERTY, SchemaAttributes.BLOCK_DEFAULT, 252 blockDefault == null ? null : 253 Util.convertEnumSet(Block.class, blockDefault)); 254 } 255 256 public Set <Block> getBlockDefault() { 257 String s = getAttribute(SchemaAttributes.BLOCK_DEFAULT); 258 return s == null ? null : Util.valuesOf(Block.class, s); 259 } 260 261 public Set <Block> getBlockDefaultEffective() { 262 Set <Block> v = getBlockDefault(); 263 return v == null ? getBlockDefaultDefault() : v; 264 } 265 266 public Set <Block> getBlockDefaultDefault() { 267 return new DerivationsImpl.DerivationSet<Block>(); 268 } 269 270 public Form getElementFormDefaultEffective() { 271 Form v = getElementFormDefault(); 272 return v == null ? getElementFormDefaultDefault() : v; 273 } 274 275 public Form getElementFormDefaultDefault() { 276 return Form.UNQUALIFIED; 277 } 278 279 public Form getAttributeFormDefaultEffective() { 280 Form v = getAttributeFormDefault(); 281 return v == null ? getAttributeFormDefaultDefault() : v; 282 } 283 284 public Form getAttributeFormDefaultDefault() { 285 return Form.UNQUALIFIED; 286 } 287 288 public Collection <Redefine> getRedefines() { 289 return getChildren(Redefine.class); 290 } 291 292 public Collection <Include> getIncludes() { 293 return getChildren(Include.class); 294 } 295 296 public Collection <Import> getImports() { 297 return getChildren(Import.class); 298 } 299 300 public Collection <GlobalElement> findAllGlobalElements() { 301 Collection <GlobalElement> result = new ArrayList <GlobalElement>(); 302 Collection <GlobalElement> tempCollection = this.getElements(); 303 if(tempCollection != null) { 304 result.addAll(tempCollection); 305 } 306 result.addAll(getExternalGlobalElements(getImports())); 308 result.addAll(getExternalGlobalElements(getIncludes())); 309 result.addAll(getExternalGlobalElements(getRedefines())); 310 return result; 311 } 312 313 private Collection <GlobalElement> getExternalGlobalElements( 314 Collection <? extends SchemaModelReference> externalRefs){ 315 316 Collection <GlobalElement> result = new ArrayList <GlobalElement>(); 317 for(SchemaModelReference smr : externalRefs) { 318 try { 319 SchemaModel sm = smr.resolveReferencedModel(); 320 if (sm.getState().equals(SchemaModel.State.VALID)) { 321 result.addAll(sm.getSchema().findAllGlobalElements()); 322 } 323 } catch (CatalogModelException ex) { 324 } 327 } 328 return result; 329 } 330 331 public Collection <GlobalType> findAllGlobalTypes() { 332 Collection <GlobalType> result = new ArrayList <GlobalType>(); 333 Collection <? extends GlobalType> tempCollection = this.getSimpleTypes(); 335 if(tempCollection != null) 336 result.addAll(tempCollection); 337 tempCollection = this.getComplexTypes(); 339 if(tempCollection != null) 340 result.addAll(tempCollection); 341 result.addAll(getExternalGlobalTypes(getImports())); 343 result.addAll(getExternalGlobalTypes(getIncludes())); 344 result.addAll(getExternalGlobalTypes(getRedefines())); 345 return result; 346 } 347 348 private Collection <GlobalType> getExternalGlobalTypes( 349 Collection <? extends SchemaModelReference> externalRefs){ 350 351 Collection <GlobalType> result = new ArrayList <GlobalType>(); 352 for(SchemaModelReference smr : externalRefs){ 353 try { 354 SchemaModel sm = smr.resolveReferencedModel(); 355 if (sm.getState().equals(SchemaModel.State.VALID)) { 356 result.addAll(sm.getSchema().findAllGlobalTypes()); 357 } 358 } catch (CatalogModelException ex) { 359 } 361 } 362 return result; 363 } 364 365 public Component getForeignParent() { 366 return foreignParent; 367 } 368 369 public void setForeignParent(Component component) { 370 foreignParent = component; 371 } 372 } 373 | Popular Tags |