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.HashMap ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import javax.xml.XMLConstants ; 30 import javax.xml.namespace.QName ; 31 import org.netbeans.modules.xml.schema.model.Import; 32 import org.netbeans.modules.xml.schema.model.Include; 33 import org.netbeans.modules.xml.schema.model.Redefine; 34 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 35 import org.netbeans.modules.xml.xam.ModelSource; 36 import org.netbeans.modules.xml.schema.model.Schema; 37 import org.netbeans.modules.xml.schema.model.SchemaComponent; 38 import org.netbeans.modules.xml.schema.model.SchemaComponentFactory; 39 import org.netbeans.modules.xml.schema.model.SchemaModel; 40 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 41 import org.netbeans.modules.xml.schema.model.SchemaModelReference; 42 import org.netbeans.modules.xml.schema.model.impl.xdm.SyncUpdateVisitor; 43 import org.netbeans.modules.xml.schema.model.visitor.FindGlobalReferenceVisitor; 44 import org.netbeans.modules.xml.xam.ComponentUpdater; 45 import org.netbeans.modules.xml.xam.Model; 46 import org.netbeans.modules.xml.xam.Model.State; 47 import org.netbeans.modules.xml.xam.NamedReferenceable; 48 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 49 import org.netbeans.modules.xml.xam.dom.ChangeInfo; 50 import org.netbeans.modules.xml.xam.dom.DocumentModelAccess; 51 import org.netbeans.modules.xml.xam.dom.SyncUnit; 52 53 57 public class SchemaModelImpl extends AbstractDocumentModel<SchemaComponent> implements SchemaModel { 58 59 private SchemaImpl schema; 60 private SchemaComponentFactory csef; 61 62 public SchemaModelImpl(ModelSource modelSource) { 63 super(modelSource); 64 csef = new SchemaComponentFactoryImpl(this); 65 } 67 68 76 77 public SchemaImpl getSchema() { 78 return (SchemaImpl)getRootComponent(); 79 } 80 81 86 public SchemaComponentFactory getFactory() { 87 return csef; 88 } 89 90 public SchemaComponent createRootComponent(org.w3c.dom.Element root) { 91 SchemaImpl newSchema = (SchemaImpl)csef.create(root, null); 92 if (newSchema != null) { 93 schema = newSchema; 94 } else { 95 return null; 96 } 97 return getSchema(); 98 } 99 100 101 public SchemaComponent getRootComponent() { 102 return schema; 103 } 104 105 public <T extends NamedReferenceable> 106 T resolve(String namespace, String localName, Class <T> type) 107 { 108 if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)){ 109 SchemaModel sm = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 110 return sm.findByNameAndType(localName, type); 111 } 112 113 return resolve(namespace, localName, type, null, new ArrayList <SchemaModel>()); 114 } 115 116 <T extends NamedReferenceable> 117 T resolve(String namespace, String localName, Class <T> type, SchemaModelReference refToMe, Collection <SchemaModel> checked) 118 { 119 if (getState() != State.VALID) { 120 return null; 121 } 122 123 T found = null; 124 String targetNamespace = getSchema().getTargetNamespace(); 125 if (targetNamespace != null && targetNamespace.equals(namespace) || 126 targetNamespace == null && namespace == null) { 127 found = findByNameAndType(localName, type); 128 } 129 130 if (found == null && ! (refToMe instanceof Import)) { 131 checked.add(this); 132 133 Collection <SchemaModelReference> modelRefs = getSchemaModelReferences(); 134 for (SchemaModelReference r : modelRefs) { 135 if (r instanceof Import) { 137 if (namespace == null || ! namespace.equals(((Import)r).getNamespace())) { 138 continue; 139 } 140 } 141 142 SchemaModelImpl sm = resolve(r); 143 if (sm != null && ! checked.contains(sm)) { 144 found = sm.resolve(namespace, localName, type, r, checked); 145 } 146 if (found != null) { 147 break; 148 } 149 } 150 } 151 152 return found; 153 } 154 155 public SchemaModelImpl resolve(SchemaModelReference ref) { 156 try { 157 return (SchemaModelImpl) ref.resolveReferencedModel(); 158 } catch (CatalogModelException ex) { 159 return null; 160 } 161 } 162 163 public Collection <SchemaModelReference> getSchemaModelReferences() { 164 Collection <SchemaModelReference> refs = new ArrayList <SchemaModelReference>(); 165 refs.addAll(getSchema().getRedefines()); 166 refs.addAll(getSchema().getIncludes()); 167 refs.addAll(getSchema().getImports()); 168 return refs; 169 } 170 171 public <T extends NamedReferenceable> T findByNameAndType(String localName, Class <T> type) { 172 return new FindGlobalReferenceVisitor<T>().find(type, localName, getSchema()); 173 } 174 175 public Set <Schema> findSchemas(String namespace) { 176 Set <Schema> result = new HashSet <Schema>(); 177 178 if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(namespace)){ 180 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 181 result.add(primitiveModel.getSchema()); 182 return result; 183 } 184 185 return _findSchemas(namespace, result, null); 186 } 187 188 protected enum ReferenceType { IMPORT, INCLUDE, REDEFINE } 189 190 Set <Schema> _findSchemas(String namespace, Set <Schema> result, ReferenceType refType) { 191 SchemaImpl schema = getSchema(); 192 if (schema == null) { 194 return result; 195 } 196 197 String targetNamespace = schema.getTargetNamespace(); 198 if (targetNamespace != null && targetNamespace.equals(namespace) || 199 targetNamespace == null && namespace == null) { 200 result.add(schema); 201 } 202 203 if (refType != ReferenceType.IMPORT) { 204 checkIncludeSchemas(namespace, result); 205 checkRedefineSchemas(namespace, result); 206 checkImportedSchemas(namespace, result); 207 } 208 209 return result; 210 } 211 212 private void checkIncludeSchemas(String namespace, Set <Schema> result) { 213 Collection <Include> includes = getSchema().getIncludes(); 214 for (Include include : includes) { 215 try { 216 SchemaModel model = include.resolveReferencedModel(); 217 if (model.getState() == Model.State.NOT_WELL_FORMED) { 218 continue; 219 } 220 221 if (! result.contains(model.getSchema())) { 222 result.addAll(((SchemaModelImpl)model)._findSchemas(namespace, result, ReferenceType.INCLUDE)); 223 } 224 } catch (CatalogModelException ex) { 225 } 227 } 228 } 229 230 private void checkRedefineSchemas(String namespace, Set <Schema> result) { 231 Collection <Redefine> redefines = getSchema().getRedefines(); 232 for (Redefine redefine : redefines) { 233 try { 234 SchemaModel model = redefine.resolveReferencedModel(); 235 if (model.getState() == Model.State.NOT_WELL_FORMED) 236 continue; 237 238 if (! result.contains(model.getSchema())) { 239 result.addAll(((SchemaModelImpl)model)._findSchemas(namespace, result, ReferenceType.REDEFINE)); 240 } 241 } catch (CatalogModelException ex) { 242 } 244 } 245 } 246 247 private void checkImportedSchemas(String namespace, Set <Schema> result) { 248 Collection <Import> imports = getSchema().getImports(); 249 for (Import imp : imports) { 250 try { 251 SchemaModel model = imp.resolveReferencedModel(); 252 if (model.getState() == Model.State.NOT_WELL_FORMED) 253 continue; 254 255 if (! result.contains(model.getSchema())) { 256 result.addAll(((SchemaModelImpl)model)._findSchemas(namespace, result, ReferenceType.IMPORT)); 257 } 258 } catch (CatalogModelException ex) { 259 } 261 } 262 } 263 264 275 public String getEffectiveNamespace(SchemaComponent component) { 276 SchemaModel componentModel = component.getModel(); 277 Schema schema = getSchema(); 278 Schema componentSchema = componentModel.getSchema(); 279 String tns = schema.getTargetNamespace(); 280 String componentTNS = componentSchema.getTargetNamespace(); 281 if (this == componentModel) { 282 return tns; 283 } else if (componentTNS == null && tns != null) { 284 Collection <Import> imports = schema.getImports(); 287 for (Import imp: imports) { 288 SchemaModel m = null; 289 try { 290 m = imp.resolveReferencedModel(); 291 } catch (CatalogModelException ex) { 292 } 294 if(componentModel.equals(m)) { 295 return null; 296 } 297 if (m == null || m.getState() == Model.State.NOT_WELL_FORMED) { 298 continue; 299 } 300 String importedTNS = m.getSchema().getTargetNamespace(); 301 if (importedTNS == null) continue; 302 Set <Schema> visibleSchemas = findSchemas(importedTNS); 303 for (Schema visible : visibleSchemas) { 304 if (componentModel.equals(visible.getModel())) { 305 return null; 306 } 307 } 308 } 309 return tns; 310 } else { 311 return componentTNS; 312 } 313 } 314 315 public SchemaComponent createComponent(SchemaComponent parent, org.w3c.dom.Element element) { 316 return csef.create(element, parent); 317 } 318 319 protected ComponentUpdater<SchemaComponent> getComponentUpdater() { 320 return new SyncUpdateVisitor(); 321 } 322 323 public Set <QName > getQNames() { 324 return SchemaElements.allQNames(); 325 } 326 327 public SyncUnit prepareSyncUnit(ChangeInfo changes, SyncUnit unit) { 328 unit = super.prepareSyncUnit(changes, unit); 329 if (unit != null) { 330 return new SyncUnitReviewVisitor().review(unit); 331 } 332 return null; 333 } 334 335 public DocumentModelAccess getAccess() { 336 if (access == null) { 337 super.getAccess().setAutoSync(true); } 339 return super.getAccess(); 340 } 341 342 public Map <QName ,List <QName >> getQNameValuedAttributes() { 343 return SchemaAttributes.getQNameValuedAttributes(); 344 } 345 } 346 | Popular Tags |