1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui.property.view.impl; 30 31 import java.io.IOException ; 32 import java.util.Collection ; 33 import java.util.HashMap ; 34 import java.util.HashSet ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 import java.util.Map ; 38 import java.util.Set ; 39 import java.util.TreeMap ; 40 41 import javax.xml.namespace.QName ; 42 43 import org.netbeans.modules.xml.schema.model.Attribute; 44 import org.netbeans.modules.xml.schema.model.Element; 45 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 46 import org.netbeans.modules.xml.schema.model.SchemaModel; 47 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 48 import org.netbeans.modules.xml.schema.model.SimpleType; 49 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction; 50 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement; 51 import org.netbeans.modules.xml.wsdl.ui.api.property.ExtensibilityElementPropertyAdapter; 52 import org.netbeans.modules.xml.wsdl.ui.api.property.PropertyAdapter; 53 import org.netbeans.modules.xml.wsdl.ui.api.property.StringAttributeProperty; 54 import org.netbeans.modules.xml.wsdl.ui.commands.CommonAttributePropertyAdapter; 55 import org.netbeans.modules.xml.wsdl.ui.commands.OtherAttributePropertyAdapter; 56 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility; 57 import org.netbeans.modules.xml.wsdl.ui.property.model.BuiltInCustomizer; 58 import org.netbeans.modules.xml.wsdl.ui.property.model.BuiltInCustomizerGroupedProperty; 59 import org.netbeans.modules.xml.wsdl.ui.property.model.DependsOnCustomizer; 60 import org.netbeans.modules.xml.wsdl.ui.property.model.ElementProperties; 61 import org.netbeans.modules.xml.wsdl.ui.property.model.GroupedProperty; 62 import org.netbeans.modules.xml.wsdl.ui.property.model.Property; 63 import org.netbeans.modules.xml.wsdl.ui.property.model.PropertyGroup; 64 import org.netbeans.modules.xml.wsdl.ui.property.model.PropertyModelException; 65 import org.netbeans.modules.xml.wsdl.ui.property.model.PropertyModelFactory; 66 import org.netbeans.modules.xml.wsdl.ui.property.model.SimpleCustomizer; 67 import org.netbeans.modules.xml.wsdl.ui.property.model.StaticCustomizer; 68 import org.netbeans.modules.xml.wsdl.ui.property.view.BuiltInCustomizerFactory; 69 import org.netbeans.modules.xml.wsdl.ui.property.view.GroupedBuiltInCustomizerFactory; 70 import org.netbeans.modules.xml.wsdl.ui.property.view.PropertyViewFactory; 71 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.OptionalAttributeFinderVisitor; 72 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.SchemaAttributeTypeFinderVisitor; 73 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.SchemaDocumentationFinderVisitor; 74 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.SchemaElementAttributeFinderVisitor; 75 import org.netbeans.modules.xml.wsdl.ui.schema.visitor.SchemaUtility; 76 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementConfigurator; 77 import org.netbeans.modules.xml.wsdl.ui.spi.ExtensibilityElementConfiguratorFactory; 78 import org.netbeans.modules.xml.wsdl.ui.spi.NewCustomizerProvider; 79 import org.netbeans.modules.xml.wsdl.ui.spi.WSDLLookupProviderFactory; 80 import org.netbeans.modules.xml.wsdl.ui.view.property.BaseAttributeProperty; 81 import org.netbeans.modules.xml.wsdl.ui.view.property.XSDBooleanAttributeProperty; 82 import org.netbeans.modules.xml.wsdl.ui.view.property.XSDEnumeratedAttributeProperty; 83 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.ExtensibilityElementNode; 84 import org.netbeans.modules.xml.wsdl.ui.view.treeeditor.MixedContentFinderVisitor; 85 import org.netbeans.modules.xml.xam.Nameable; 86 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 87 import org.openide.ErrorManager; 88 import org.openide.nodes.Node; 89 import org.openide.nodes.Sheet; 90 import org.openide.util.NbBundle; 91 92 96 public class PropertyViewFactoryImpl extends PropertyViewFactory { 97 98 99 public PropertyViewFactoryImpl() { 100 } 101 102 @Override 103 public Sheet.Set[] getPropertySets(ExtensibilityElement exElement, 104 QName elementQName, 105 Element schemaElement) { 106 Set <QName > processAttributeQNames = new HashSet <QName >(); 107 Sheet.Set defaultPropertiesSheetSet = Sheet.createPropertiesSet(); 108 ElementProperties elemProperties = null; 109 try { 110 PropertyModelFactory instance = PropertyModelFactory.getInstance(); 111 elemProperties = instance.getElementProperties(elementQName); 112 } catch (PropertyModelException e) { 113 e.printStackTrace(); 114 } 115 Sheet.Set[] nodePropertySet = null; 116 if (elemProperties != null) { 117 PropertyGroup[] pGroups = elemProperties.getPropertyGroup(); 118 Map <String , Integer > groupList = new HashMap <String , Integer >(); 119 120 nodePropertySet = new Sheet.Set[pGroups.length + 1]; 121 boolean defaultPropertiesSet = false; 122 for (PropertyGroup group : pGroups) { 124 String gName = group.getName(); 125 int groupNumber = group.getGroupOrder() - 1; groupList.put(gName, new Integer (groupNumber)); 127 nodePropertySet[groupNumber] = new Sheet.Set(); 128 nodePropertySet[groupNumber].setName(gName); 129 String displayName = gName; nodePropertySet[groupNumber].setDisplayName(displayName); 131 if (group.isIsDefault()) { 132 defaultPropertiesSheetSet = nodePropertySet[groupNumber]; 133 defaultPropertiesSet = true; 134 } 135 } 136 137 if (!defaultPropertiesSet) { 139 int lastIndex = nodePropertySet.length - 1; 140 nodePropertySet[lastIndex] = defaultPropertiesSheetSet; 141 groupList.put(null, new Integer (lastIndex)); 142 } 143 144 Property[] props = elemProperties.getProperty(); 146 Map <String , Map <Integer , Node.Property>> map = new HashMap <String , Map <Integer , Node.Property>>(); 147 Map <Integer , Node.Property> list = null; 148 for (Property prop : props) { 149 QName attributeQName = new QName (elementQName.getNamespaceURI(), prop.getAttributeName()); 150 Attribute attribute = getAttribute(attributeQName.getLocalPart(), schemaElement); 151 152 boolean isOptional = isAttributeOptional(attribute, attributeQName.getLocalPart()); 153 154 Node.Property nodeProperty = getNodeProperty(exElement, elementQName, attributeQName, prop, isOptional); 156 157 if (nodeProperty == null) { 159 160 nodeProperty = createDefaultNodeProperty(attribute, exElement, attributeQName, isOptional); 161 162 } 163 processAttributeQNames.add(attributeQName); 165 166 if (map.containsKey(prop.getGroupName())) { 167 list = map.get(prop.getGroupName()); 168 } else { 169 list = new TreeMap <Integer , Node.Property>(); map.put(prop.getGroupName(), list); 171 } 172 setNameAndDescription(nodeProperty, attributeQName.getLocalPart(), null, getAttributeShortDescription(schemaElement, attributeQName)); 174 list.put(new Integer (prop.getPropertyOrder() - 1), nodeProperty); } 176 177 GroupedProperty[] groupedProps = elemProperties.getGroupedProperty(); 178 for (GroupedProperty groupedProp : groupedProps) { 179 String groupedAttrNames = groupedProp.getGroupedAttributeNames(); 180 String [] attrNames = groupedAttrNames.split(" "); 181 Node.Property prop = getGroupedNodeProperty(elementQName.getNamespaceURI(), exElement, groupedProp); 182 if (prop != null) { 183 for (String attrName : attrNames) { 184 processAttributeQNames.add(new QName (elementQName.getNamespaceURI(), attrName)); 185 } 186 } else { 187 189 } 190 191 Map <Integer , Node.Property> nodeProps = null; 192 if (map.containsKey(groupedProp.getGroupName())) { 193 nodeProps = map.get(groupedProp.getGroupName()); 194 } else { 195 nodeProps = new TreeMap <Integer , Node.Property>(); 196 map.put(groupedProp.getGroupName(), nodeProps); 197 } 198 setNameAndDescription(prop, groupedProp.getDisplayName(), groupedProp.getDisplayName(), "TODO: Set documentation for Grouped Property."); 199 nodeProps.put(new Integer (groupedProp.getPropertyOrder() - 1), prop); 200 } 201 202 for (String groupName : map.keySet()) { 204 Integer i = groupList.get(groupName); 205 Map <Integer , Node.Property> properties = map.get(groupName); 206 for (Node.Property prop : properties.values()) { 207 if (prop != null) { 208 nodePropertySet[i.intValue()].put(prop); 209 } 210 } 211 } 212 } else { 213 nodePropertySet = new Sheet.Set[] {defaultPropertiesSheetSet}; 214 } 215 216 populateDefaultPropertySet(defaultPropertiesSheetSet, schemaElement, elementQName, exElement, processAttributeQNames); 218 219 return nodePropertySet; 220 221 } 222 223 private org.openide.nodes.Node.Property getNodeProperty(ExtensibilityElement extensibilityElement, QName elementQName, QName attributeQName, Property prop, boolean isOptional) { 224 BuiltInCustomizer bCustomizer = prop.getBuiltInCustomizer(); 225 if (bCustomizer != null) { 226 DependsOnCustomizer dCustomizer = bCustomizer.getDependsOnCustomizer(); 227 if (dCustomizer != null) { 228 String nameOfCustomizer = dCustomizer.getName(); 229 StaticCustomizer sCustomizer = dCustomizer.getStaticCustomizer(); 230 if (sCustomizer != null) { 231 QName qname = sCustomizer.getDependsOnAttributeName(); 232 return BuiltInCustomizerFactory.getProperty(extensibilityElement, attributeQName, qname, nameOfCustomizer, isOptional); 234 } 235 236 242 assert false : "Invalid Customizer defined for " + attributeQName.toString(); 244 } else { 245 SimpleCustomizer sCustomizer = bCustomizer.getSimpleCustomizer(); 246 if (sCustomizer != null) { 247 String name = sCustomizer.getName(); 248 return BuiltInCustomizerFactory.getProperty(extensibilityElement, attributeQName, name, isOptional); 249 } 250 assert false : "Invalid Customizer defined for " + attributeQName.toString(); 251 } 252 } else { 253 String nCustomizer = prop.getNewCustomizer(); 254 if (nCustomizer != null) { 255 NewCustomizerProvider prov = WSDLLookupProviderFactory.getObject(elementQName.getNamespaceURI(), NewCustomizerProvider.class); 256 assert prov != null : "Couldnt create the NewCustomizer"; 257 Node.Property property = prov.getProperty(extensibilityElement, elementQName, attributeQName, isOptional); 258 assert property != null : "Couldnt create the property using this NewCustomizer"; 259 return property; 260 } 261 } 262 return null; 264 } 265 266 private Node.Property getGroupedNodeProperty(String namespace, ExtensibilityElement extensibilityElement, GroupedProperty gProp) { 267 BuiltInCustomizerGroupedProperty bCustomizer = gProp.getBuiltInCustomizer(); 268 if (bCustomizer != null) { 269 return GroupedBuiltInCustomizerFactory.getProperty(extensibilityElement, bCustomizer); 270 } 271 272 String nCustomizer = gProp.getNewCustomizer(); 273 if (nCustomizer != null) { 274 275 } 276 return null; 277 } 278 279 private org.openide.nodes.Node.Property createDefaultNodeProperty(Attribute attr, ExtensibilityElement exElement, QName attributeQName, boolean isOptional) { 280 Node.Property attrValueProperty = null; 281 QName elementQName = new QName (attributeQName.getNamespaceURI(), exElement.getQName().getLocalPart()); 282 ExtensibilityElementConfigurator configurator = new ExtensibilityElementConfiguratorFactory().getExtensibilityElementConfigurator( 283 elementQName); 284 if (configurator != null) { 285 if (configurator.isHidden(exElement, elementQName, attributeQName.getLocalPart())) { 286 return null; 287 } 288 289 attrValueProperty = configurator.getProperty(exElement, elementQName, attributeQName.getLocalPart()); 290 if (attrValueProperty != null) return attrValueProperty; 291 } 292 293 SimpleType gst = findAttributeType(attr); 294 295 if(gst != null) { 296 try { 297 ExtensibilityElementPropertyAdapter adapter = null; 298 if (attr.getDefault() != null) { 299 adapter = new ExtensibilityElementPropertyAdapter(exElement, attributeQName.getLocalPart(), attr.getDefault()); 300 } else { 301 adapter = new ExtensibilityElementPropertyAdapter(exElement, attributeQName.getLocalPart(), isOptional); 302 } 303 attrValueProperty = processSimpleType(gst, adapter, isOptional); 304 } catch (NoSuchMethodException e) { 305 e.printStackTrace(); 307 } 308 309 if (attrValueProperty == null) { 310 attrValueProperty = createAttributeProperty(exElement, attributeQName, isOptional, attr.getDefault()); 312 } 313 } 314 return attrValueProperty; 315 } 316 317 private boolean isAttributeOptional(Attribute attribute, String localPart) { 318 OptionalAttributeFinderVisitor visitor = new OptionalAttributeFinderVisitor(localPart); 319 attribute.accept(visitor); 320 return visitor.isOptional(); 321 } 322 323 private Attribute getAttribute(String attrName, Element schemaElement) { 324 SchemaElementAttributeFinderVisitor seaFinder = new SchemaElementAttributeFinderVisitor(schemaElement); 325 schemaElement.accept(seaFinder); 326 327 List <Attribute> attributes = seaFinder.getAttributes(); 328 Iterator <Attribute> it = attributes.iterator(); 329 330 while(it.hasNext()) { 331 Attribute attr = it.next(); 332 if(attr instanceof Nameable) { 333 Nameable namedAttr = (Nameable) attr; 334 if(attrName.equals(namedAttr.getName())) { 335 return attr; 336 } 337 } 338 } 339 return null; 340 } 341 342 private SimpleType findAttributeType(Attribute attr) { 343 if (attr == null) return null; 344 345 SchemaAttributeTypeFinderVisitor typeFinder = new SchemaAttributeTypeFinderVisitor(); 346 attr.accept(typeFinder); 347 return typeFinder.getSimpleType(); 348 } 349 350 private Node.Property processSimpleType(SimpleType gst, PropertyAdapter propertyAdapter, boolean isOptional) throws NoSuchMethodException { 351 Node.Property attrValueProperty = null; 352 if(gst != null) { 354 String simpleTypeName = null; 355 if (gst instanceof GlobalSimpleType) { 356 simpleTypeName = ((GlobalSimpleType) gst).getName(); 357 } 358 String namesapce = gst.getModel().getSchema().getTargetNamespace(); 359 SchemaModel primitiveTypesModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 360 String primitiveTypeNamesapce = primitiveTypesModel.getSchema().getTargetNamespace(); 361 if(namesapce != null 362 && namesapce.equals(primitiveTypeNamesapce) 363 && simpleTypeName != null && simpleTypeName.equals("boolean")) { attrValueProperty = 365 new XSDBooleanAttributeProperty(propertyAdapter, String .class, "getValue", "setValue", isOptional); 367 } else if(gst.getDefinition() instanceof SimpleTypeRestriction) { 368 SimpleTypeRestriction sr = (SimpleTypeRestriction) gst.getDefinition(); 371 Collection enumerations = sr.getEnumerations(); 372 if(enumerations != null && enumerations.size() > 0) { 373 attrValueProperty = 374 new XSDEnumeratedAttributeProperty(gst, propertyAdapter, String .class, "getValue", "setValue", isOptional); 376 } 377 } 378 } 379 380 381 return attrValueProperty; 382 } 383 384 private void populateDefaultPropertySet(Sheet.Set propertySet, Element schemaElement, QName elementQName, ExtensibilityElement exElement, Set <QName > processAttributeQNames) { 385 SchemaElementAttributeFinderVisitor seaFinder = new SchemaElementAttributeFinderVisitor(schemaElement, true); 386 schemaElement.accept(seaFinder); 387 List <Attribute> attributes = seaFinder.getAttributes(); 388 for (Attribute attr : attributes) { 389 if(attr instanceof Nameable) { 390 Nameable namedAttr = (Nameable) attr; 391 392 QName attrQName = new QName (schemaElement.getModel().getSchema().getTargetNamespace(), namedAttr.getName()); 393 if (!processAttributeQNames.contains(attrQName)) { 394 boolean isOptional = isAttributeOptional(attr, namedAttr.getName()); 395 Node.Property attrValueProperty = createDefaultNodeProperty(attr, exElement, attrQName, isOptional); 396 setNameAndDescription(attrValueProperty, namedAttr.getName(), null, getAttributeShortDescription(schemaElement, attrQName)); 397 propertySet.put(attrValueProperty); 398 } 399 } 400 } 401 402 MixedContentFinderVisitor aeFinder = new MixedContentFinderVisitor(schemaElement); 404 schemaElement.accept(aeFinder); 405 if (aeFinder.hasMixedContent()) { 406 Node.Property attrValueProperty; 407 try { 408 attrValueProperty = new BaseAttributeProperty(new AnyElementPropertyAdapter(exElement, elementQName), String .class, "value"); 409 attrValueProperty.setName(NbBundle.getMessage(ExtensibilityElementNode.class, "PROP_NAME_ANY_CONTENT")); 410 propertySet.put(attrValueProperty); 411 } catch (NoSuchMethodException e) { 412 ErrorManager.getDefault().notify(e); 413 } 414 } 415 416 } 417 418 private String getAttributeShortDescription(Element schemaElement, QName attrQName) { 419 if (schemaElement != null) { 420 Attribute attribute = SchemaUtility.findAttribute(attrQName, schemaElement); 421 if(attribute != null) { 422 SchemaDocumentationFinderVisitor sdfFinder = new SchemaDocumentationFinderVisitor(); 423 attribute.accept(sdfFinder); 424 return sdfFinder.getDocumentation(); 425 } 426 } 427 return null; 428 } 429 430 private Node.Property createAttributeProperty(ExtensibilityElement exElement, QName attrQName, boolean isOptional, String defaultValue) { 431 Node.Property attrValueProperty = null; 432 433 try { 434 AbstractDocumentComponent adc = (AbstractDocumentComponent) exElement; 435 QName elementQName = adc.getQName(); 436 String namespace = elementQName.getNamespaceURI(); 437 String ns = attrQName.getNamespaceURI(); 438 if(ns == null || ns.trim().equals("") || ns.equals(namespace)) { 442 ExtensibilityElementPropertyAdapter propertyAdapter = 443 defaultValue != null 444 ? new ExtensibilityElementPropertyAdapter(exElement, attrQName.getLocalPart(), defaultValue) 445 : new ExtensibilityElementPropertyAdapter(exElement, attrQName.getLocalPart(), isOptional); 446 447 attrValueProperty = getAttributeNodeProperty(propertyAdapter); 448 449 } else { 450 OtherAttributePropertyAdapter propertyAdapter = 451 new OtherAttributePropertyAdapter(attrQName, exElement); 452 attrValueProperty = getOtherAttributeNodeProperty(attrQName, propertyAdapter); 454 455 } 456 } catch(Exception ex) { 457 ErrorManager.getDefault().notify(ex); 459 } 460 461 return attrValueProperty; 462 } 463 464 protected Node.Property getAttributeNodeProperty(ExtensibilityElementPropertyAdapter propertyAdapter) throws NoSuchMethodException { 465 Node.Property attrValueProperty = new StringAttributeProperty(propertyAdapter, String .class, "getValue", "setValue"); 466 return attrValueProperty; 467 } 468 469 protected Node.Property getOtherAttributeNodeProperty(QName attrQName, 470 OtherAttributePropertyAdapter propertyAdapter) throws NoSuchMethodException { 471 Node.Property attrValueProperty = new BaseAttributeProperty(propertyAdapter, String .class, CommonAttributePropertyAdapter.VALUE); 472 attrValueProperty.setName(Utility.fromQNameToString(attrQName)); 473 return attrValueProperty; 474 } 475 476 private void setNameAndDescription(Node.Property property, String name, String displayName, String description) { 477 if (property == null) return; 478 if (isNullOrBlank(property.getName())) { 479 assert name != null : "Name cannot be null"; 480 property.setName(name); 481 } 482 if (isNullOrBlank(property.getDisplayName()) && displayName != null) { 483 property.setDisplayName(displayName); 484 } 485 if (isNullOrBlank(property.getShortDescription()) || property.getShortDescription().equals(property.getDisplayName())) { 486 property.setShortDescription(description); 487 } 488 } 489 490 private boolean isNullOrBlank(String str) { 491 return str == null || str.trim().length() == 0; 492 } 493 494 495 public class AnyElementPropertyAdapter extends PropertyAdapter { 496 QName qname; 497 498 public AnyElementPropertyAdapter(ExtensibilityElement delegate, QName qname) { 499 super(delegate); 500 this.qname = qname; 501 } 502 503 public String getValue() { 504 String content = ((ExtensibilityElement)getDelegate()).getContentFragment(); 505 if (content != null) { 506 return content; 507 } 508 return ""; 509 } 510 511 public void setValue(String string) { 512 513 boolean isInTransaction = Utility.startTransaction(getDelegate().getModel()); 514 try { 515 ((ExtensibilityElement)getDelegate()).setContentFragment(string); 516 } catch (IOException e) { 517 ErrorManager.getDefault().notify(e); 518 } finally { 519 if (!isInTransaction) { 520 Utility.endTransaction(getDelegate().getModel(), isInTransaction); 521 } 522 } 523 } 524 525 526 } 527 528 } 529 | Popular Tags |