KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > api > property > ElementOrTypeOrMessagePartProvider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.wsdl.ui.api.property;
20
21 import javax.xml.namespace.QName JavaDoc;
22
23 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
24 import org.netbeans.modules.xml.wsdl.model.Message;
25 import org.netbeans.modules.xml.wsdl.model.Part;
26 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
27 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
28
29 public class ElementOrTypeOrMessagePartProvider {
30
31     private final ExtensibilityElement extensibilityElement;
32     private final String JavaDoc typeAttributeName;
33     private final String JavaDoc elementAttributeName;
34     private final String JavaDoc messageAttributeName;
35     private final String JavaDoc partAttributeName;
36
37     public ElementOrTypeOrMessagePartProvider(ExtensibilityElement extensibilityElement,
38             String JavaDoc elementAttributeName,
39             String JavaDoc typeAttributeName,
40             String JavaDoc messageAttributeName,
41             String JavaDoc partAttributeName) {
42         this.extensibilityElement = extensibilityElement;
43         this.elementAttributeName = elementAttributeName;
44         this.typeAttributeName = typeAttributeName;
45         this.messageAttributeName = messageAttributeName;
46         this.partAttributeName = partAttributeName;
47     }
48
49     public void setValue(ElementOrTypeOrMessagePart o) {
50         if (o == null) {
51             return;
52         }
53         
54         
55         if (getModel().startTransaction()) {
56             try {
57                 ParameterType pType = o.getParameterType();
58
59                 switch (pType) {
60                 case ELEMENT:
61                     Utility.addNamespacePrefix(o.getElement().getModel().getSchema(), extensibilityElement.getModel(), null);
62                     extensibilityElement.setAttribute(elementAttributeName, o.toString());
63                     extensibilityElement.setAttribute(typeAttributeName, null);
64                     extensibilityElement.setAttribute(messageAttributeName, null);
65                     extensibilityElement.setAttribute(partAttributeName, null);
66                     break;
67                 case TYPE:
68                     Utility.addNamespacePrefix(o.getType().getModel().getSchema(), extensibilityElement.getModel(), null);
69                     extensibilityElement.setAttribute(elementAttributeName, null);
70                     extensibilityElement.setAttribute(typeAttributeName, o.toString());
71                     extensibilityElement.setAttribute(messageAttributeName, null);
72                     extensibilityElement.setAttribute(partAttributeName, null);
73                     break;
74                 case MESSAGEPART:
75                     Part part = o.getMessagePart();
76                     Message message = (Message)part.getParent();
77                     String JavaDoc tns = message.getModel().getDefinitions().getTargetNamespace();
78                     QName JavaDoc qname = new QName JavaDoc(message.getName());
79                     if (tns != null) {
80                         String JavaDoc prefix = Utility.getNamespacePrefix(tns, extensibilityElement.getModel());
81                         qname = prefix != null ? new QName JavaDoc(tns, message.getName(), prefix) :
82                         new QName JavaDoc(tns, message.getName());
83                     }
84                     
85                     extensibilityElement.setAttribute(messageAttributeName, Utility.fromQNameToString(qname));
86                     extensibilityElement.setAttribute(partAttributeName, part.getName());
87                     extensibilityElement.setAttribute(elementAttributeName, null);
88                     extensibilityElement.setAttribute(typeAttributeName, null);
89                     break;
90                 case NONE:
91                     extensibilityElement.setAttribute(elementAttributeName, null);
92                     extensibilityElement.setAttribute(typeAttributeName, null);
93                     extensibilityElement.setAttribute(typeAttributeName, null);
94                     extensibilityElement.setAttribute(elementAttributeName, null);
95                 }
96             } finally {
97                 getModel().endTransaction();
98             }
99         }
100     }
101
102     public ElementOrTypeOrMessagePart getValue() {
103         ParameterType parameterType = ParameterType.NONE;
104
105         String JavaDoc message = extensibilityElement.getAttribute(messageAttributeName);
106         String JavaDoc part = extensibilityElement.getAttribute(partAttributeName);
107         String JavaDoc type = extensibilityElement.getAttribute(typeAttributeName);
108         String JavaDoc element = extensibilityElement.getAttribute(elementAttributeName);
109
110         String JavaDoc value = null;
111         
112         if (element != null) {
113             parameterType = ParameterType.ELEMENT;
114             value = element;
115         } else if (type != null) {
116             value = type;
117             parameterType = ParameterType.TYPE;
118         } else if (message != null) {
119             parameterType = ParameterType.MESSAGEPART;
120             value = message;
121         }
122         
123         
124         
125         if (extensibilityElement.getModel() == null) { //this seems to happen during deletion.
126
return null;
127         }
128         
129         QName JavaDoc qname = getQName(value);
130         if (parameterType == ParameterType.MESSAGEPART) {
131             return new ElementOrTypeOrMessagePart(qname, extensibilityElement.getModel(), part);
132         }
133         return new ElementOrTypeOrMessagePart(qname, extensibilityElement.getModel(), parameterType);
134     }
135
136     private QName JavaDoc getQName(String JavaDoc value) {
137         if (value != null && value.trim().length() > 0) {
138             String JavaDoc[] parts = value.split(":");
139             if (parts != null && parts.length == 2) {
140                 String JavaDoc prefix = parts[0];
141                 String JavaDoc localPart = parts[1];
142                 String JavaDoc namespace = Utility.getNamespaceURI(prefix, extensibilityElement);
143                 return new QName JavaDoc(namespace, localPart, prefix);
144             }
145             return new QName JavaDoc(getModel().getDefinitions().getTargetNamespace(), value);
146         }
147         return new QName JavaDoc("");
148     }
149     
150     public WSDLModel getModel() {
151         return extensibilityElement.getModel();
152     }
153
154     public static enum ParameterType {
155
156         ELEMENT, TYPE, MESSAGEPART, NONE
157     }
158 }
159
Popular Tags