View Javadoc

1   /*
2    *  Copyright (c) 2008 Rodrigo Ruiz
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.apache.axis.message.addressing;
17  
18  import javax.xml.namespace.QName;
19  import javax.xml.soap.SOAPElement;
20  
21  import org.apache.axis.description.AttributeDesc;
22  import org.apache.axis.description.TypeDesc;
23  import org.apache.axis.encoding.Deserializer;
24  import org.apache.axis.encoding.Serializer;
25  import org.apache.axis.encoding.SimpleType;
26  import org.apache.axis.encoding.ser.SimpleDeserializer;
27  import org.apache.axis.encoding.ser.SimpleSerializer;
28  import org.apache.axis.message.addressing.util.TextExtractor;
29  import org.apache.axis.utils.XMLUtils;
30  import org.w3c.dom.Element;
31  
32  /**
33   * Java content class for ServiceNameType complex type.
34   * <p>
35   * The following schema fragment specifies the expected content contained within
36   * this java content object. (defined at
37   * http://schemas.xmlsoap.org/ws/2004/08/addressing line 67)
38   * </p>
39   *
40   * <pre>
41   * &lt;complexType name=&quot;ServiceNameType&quot;&gt;
42   *   &lt;simpleContent&gt;
43   *     &lt;extension base=&quot;&lt;http://www.w3.org/2001/XMLSchema&gt;QName&quot;&gt;
44   *       &lt;attribute name=&quot;PortName&quot;
45   *          type=&quot;{http://www.w3.org/2001/XMLSchema}NCName&quot; /&gt;
46   *     &lt;/extension&gt;
47   *   &lt;/simpleContent&gt;
48   * &lt;/complexType&gt;
49   * </pre>
50   *
51   * @author Davanum Srinivas
52   * @version $Revision: 14 $
53   */
54  public class ServiceNameType extends AttributedQName implements SimpleType {
55  
56    /**
57     * <code>serialVersionUID</code> attribute.
58     */
59    private static final long serialVersionUID = 4494158339591159503L;
60  
61    /**
62     * Type meta-data.
63     */
64    private static final TypeDesc TYPE_DESC = new TypeDesc(ServiceNameType.class, true);
65  
66    static {
67      String ns = Constants.NS_URI_ADDRESSING_DEFAULT;
68      TYPE_DESC.setXmlType(new QName(ns, "ServiceNameType"));
69      AttributeDesc attrField = new AttributeDesc();
70      attrField.setFieldName("port");
71      attrField.setXmlName(new QName("", "PortName"));
72      attrField.setXmlType(new QName("http://www.w3.org/2001/XMLSchema", "string"));
73      TYPE_DESC.addFieldDesc(attrField);
74    }
75  
76    /**
77     * Parses an XML element node and extracts an instance of this class.
78     *
79     * @param element Element to parse
80     * @return Resulting instance
81     */
82    public static ServiceNameType fromElement(Element element) {
83      String value = TextExtractor.getText(element);
84      QName qname = XMLUtils.getQNameFromString(value, element);
85      String portName = element.getAttribute(Constants.PORT_NAME);
86      portName = portName == null || portName.length() == 0 ? null : portName;
87      return new ServiceNameType(qname, portName);
88    }
89  
90    /**
91     * Parses a SOAP element and extracts an instance of this class.
92     *
93     * @param element Element to parse
94     * @return Resulting instance
95     */
96    public static ServiceNameType fromSOAPElement(SOAPElement element) {
97      String value = TextExtractor.getText(element);
98      QName qname = TextExtractor.getQName(value, element);
99      String portName = element.getAttribute(Constants.PORT_NAME);
100     portName = portName == null || portName.length() == 0 ? null : portName;
101     return new ServiceNameType(qname, portName);
102   }
103 
104   /**
105    * Gets a custom Deserializer.
106    *
107    * @param mType not used
108    * @param jType Java type
109    * @param xType XML type
110    * @return A Deserializer instance
111    */
112   public static Deserializer getDeserializer(String mType, Class<?> jType, QName xType) {
113     return new SimpleDeserializer(jType, xType, TYPE_DESC);
114   }
115 
116   /**
117    * Gets a custom Serializer.
118    *
119    * @param mType not used
120    * @param jType Java type
121    * @param xType XML type
122    * @return A Serializer instance
123    */
124   public static Serializer getSerializer(String mType, Class<?> jType, QName xType) {
125     return new SimpleSerializer(jType, xType, TYPE_DESC);
126   }
127 
128   /**
129    * Return the type description object.
130    *
131    * @return Type type description for this class
132    */
133   public static TypeDesc getTypeDesc() {
134     return TYPE_DESC;
135   }
136 
137   /**
138    * Port attribute.
139    */
140   private String port;
141 
142   /**
143    * Creates a new instance.
144    *
145    * @param qname Service qualified name
146    */
147   public ServiceNameType(QName qname) {
148     super(qname);
149   }
150 
151   /**
152    * Creates a new instance.
153    *
154    * @param qname Service qualified name
155    * @param port Service port
156    */
157   public ServiceNameType(QName qname, String port) {
158     super(qname);
159     this.port = port;
160   }
161 
162   // Axis bits to serialize/deserialize the fields correctly
163 
164   /**
165    * Creates a new instance.
166    *
167    * @param serviceName Service name to copy
168    */
169   public ServiceNameType(ServiceNameType serviceName) {
170     super(serviceName);
171     this.port = serviceName.getPort();
172   }
173 
174   /**
175    * Creates a new instance.
176    *
177    * @param namespace Service namespace
178    * @param localName Service local name
179    */
180   public ServiceNameType(String namespace, String localName) {
181     super(namespace, localName);
182   }
183 
184   /**
185    * {@inheritDoc}
186    */
187   @Override
188   public void append(AddressingVersion version, Element parent, String elementName) {
189     Element sn = parent.getOwnerDocument().createElementNS(
190       version.getNamespace(), elementName);
191     String value = XMLUtils.getStringForQName(this, sn);
192     sn.appendChild(parent.getOwnerDocument().createTextNode(value));
193     if (this.getPort() != null) {
194       sn.setAttribute(Constants.PORT_NAME, this.getPort());
195     }
196     parent.appendChild(sn);
197   }
198 
199   /**
200    * {@inheritDoc}
201    */
202   public void append(Element parent) {
203     append(parent, Constants.SERVICE_NAME);
204   }
205 
206   /**
207    * Gets the service port.
208    *
209    * @return Service port name
210    */
211   public String getPort() {
212     return this.port;
213   }
214 
215   /**
216    * Sets the service port.
217    *
218    * @param port New service port name
219    */
220   public void setPort(String port) {
221     this.port = port;
222   }
223 
224 }