1 19 20 package org.netbeans.modules.xml.wsdl.ui.api.property; 21 22 import java.util.Collection ; 23 import java.util.List ; 24 25 import javax.xml.namespace.QName ; 26 27 import org.netbeans.modules.xml.schema.model.GlobalElement; 28 import org.netbeans.modules.xml.schema.model.GlobalType; 29 import org.netbeans.modules.xml.schema.model.Schema; 30 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 31 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator; 32 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility; 33 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 34 35 public class ElementOrType { 36 GlobalElement mElement; 37 GlobalType mType; 38 QName mQName; 39 boolean isElement; 40 WSDLModel mModel; 41 42 public ElementOrType(GlobalElement ele, WSDLModel model) { 43 mElement = ele; 44 mModel = model; 45 } 46 47 public ElementOrType(GlobalType type, WSDLModel model) { 48 mType = type; 49 mModel = model; 50 } 51 52 public ElementOrType(QName qname, WSDLModel model, boolean isElement) { 53 this.isElement = isElement; 54 mQName = qname; 55 mModel = model; 56 List <Schema> schemas = mModel.findSchemas(qname.getNamespaceURI()); 57 if (schemas != null) { 58 for (Schema schema : schemas) { 59 if (isElement) { 60 Collection <GlobalElement> ges = schema.findAllGlobalElements(); 61 if (ges != null) { 62 for (GlobalElement ele : ges) { 63 if (qname.getLocalPart().equals(ele.getName())) { 64 mElement = ele; 65 } 66 } 67 } 68 } else { 69 Collection <GlobalType> gts = schema.findAllGlobalTypes(); 70 if (gts != null) { 71 for (GlobalType type : gts) { 72 if (qname.getLocalPart().equals(type.getName())) { 73 mType = type; 74 } 75 } 76 } 77 } 78 } 79 } 80 } 81 82 public boolean isElement() { 83 return isElement || mElement != null; 84 } 85 86 public GlobalElement getElement() { 87 return mElement; 88 } 89 90 public GlobalType getType() { 91 return mType; 92 } 93 94 @Override 95 public String toString() { 96 if (mQName != null) { 97 return Utility.fromQNameToString(mQName); 98 } 99 100 String namespace = null; 101 String localPart = null; 102 if (mElement != null) { 103 namespace = mElement.getModel().getSchema().getTargetNamespace(); 104 localPart = mElement.getName(); 105 } 106 if (mType != null) { 107 namespace = mType.getModel().getSchema().getTargetNamespace(); 108 localPart = mType.getName(); 109 } 110 111 if (namespace == null) { 112 return localPart; 113 } 114 if (mModel == null) { 115 return new QName (namespace, localPart).toString(); 116 } 117 String namespacePrefix = Utility.getNamespacePrefix(namespace, mModel); 118 if (namespacePrefix == null) { 119 namespacePrefix = NameGenerator.getInstance().generateNamespacePrefix(null, mModel); 120 boolean isInTransaction = Utility.startTransaction(mModel); 121 ((AbstractDocumentComponent)mModel.getDefinitions()).addPrefix(namespacePrefix, namespace); 122 123 Utility.endTransaction(mModel, isInTransaction); 124 } 125 126 return namespacePrefix + ":" + localPart; 127 } 128 } 129 | Popular Tags |