View Javadoc

1   /*
2    * Copyright  1999-2004 The Apache Software Foundation.
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   */
17  
18  package org.apache.axis.message.addressing;
19  
20  import org.apache.axis.encoding.AnyContentType;
21  import org.apache.axis.message.MessageElement;
22  import org.apache.axis.utils.XMLUtils;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.w3c.dom.Document;
28  import org.w3c.dom.Element;
29  import org.w3c.dom.Node;
30  import org.w3c.dom.NodeList;
31  
32  import javax.xml.namespace.QName;
33  import javax.xml.soap.SOAPElement;
34  
35  import java.io.Serializable;
36  import java.util.Iterator;
37  
38  /***
39   * Java content class for ReferencePropertiesType complex type.
40   * <p>The following schema fragment specifies the expected content contained within this java content object. (defined at http://schemas.xmlsoap.org/ws/2004/08/addressing line 62)
41   * <p>
42   * <pre>
43   * &lt;complexType name="ReferencePropertiesType">
44   *   &lt;complexContent>
45   *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
46   *       &lt;sequence>
47   *         &lt;any/>
48   *       &lt;/sequence>
49   *     &lt;/restriction>
50   *   &lt;/complexContent>
51   * &lt;/complexType>
52   * </pre>
53   * 
54   * @author Davanum Srinivas (dims@yahoo.com)
55   */
56  public class ReferencePropertiesType implements Serializable, AnyContentType { 
57  
58      private static Log log = 
59          LogFactory.getLog(ReferencePropertiesType.class.getName());
60  
61      private MessageElement [] _any;
62  
63      /***
64       * Constructor ReferenceProperties
65       */
66      public ReferencePropertiesType() {
67      }
68      
69      /***
70       * Constructor ReferenceProperties
71       */
72      public ReferencePropertiesType(Object element) {
73      	add(element);
74      }
75  
76      /***
77       * Constructor ReferenceProperties (performs shallow copy)
78       * 
79       * @param properties
80       */
81      public ReferencePropertiesType(ReferencePropertiesType properties) {
82          this(properties, false);
83      }
84      
85      /***
86       * Constructor ReferenceProperties
87       * 
88       * @param properties
89       * @param deepCopy
90       */
91      public ReferencePropertiesType(ReferencePropertiesType properties, 
92                                     boolean deepCopy) {
93          if (properties == null) {
94              throw new IllegalArgumentException();
95          }
96          MessageElement elem = null;
97          for (int i = 0; i < properties.size(); i++) {
98              elem = properties._any[i];
99              if (deepCopy) {
100                 // deep copy
101                 try {
102                     add(new MessageElement(elem.getAsDOM()));
103                 } catch (Exception e) {
104                     // if failed just copy reference
105                     add(elem);
106                 }
107             } else {
108                 // shallow copy
109                 add(elem);
110             }
111         }
112     }
113 
114     public int size() {
115         return (this._any == null) ? 0 : this._any.length;
116     }
117 
118     public Object get(int index) {
119         return this._any[index];
120     }
121 
122     /***
123      * Get a (single) MessageElement matching the passed QName
124      * @param qname
125      * @return
126      */
127     public MessageElement get(QName qname) {
128         for (int i = 0; i < _any.length; i++) {
129             MessageElement messageElement = _any[i];
130             if (messageElement.getQName().equals(qname))
131                 return messageElement;
132         }
133         return null;
134     }
135 
136     public void add(Object element) {
137         MessageElement value = null;
138         if (element instanceof MessageElement) {
139             value = (MessageElement)element;
140         } else if (element instanceof Element) {
141             value = new MessageElement((Element)element);
142         } else {
143             throw new IllegalArgumentException();
144         }
145         MessageElement [] newAny = null;
146 
147         if (this._any == null) {
148             this._any = new MessageElement[] {value};
149         } else {
150             MessageElement [] any = null;
151             any = new MessageElement[this._any.length + 1];
152             System.arraycopy(this._any, 0,
153                              any, 0, 
154                              this._any.length);
155             any[this._any.length] = value;
156             this._any = any;
157         }
158     }
159 
160     /***
161      * append DOM node to parent
162      * 
163      * @param parent 
164      */
165     public void append(Element parent, String elementName) {
166         Document doc = parent.getOwnerDocument();
167         Element refProp = doc.createElementNS(Constants.NS_URI_ADDRESSING_DEFAULT,
168                                               elementName);
169         MessageElement [] any = get_any();
170         if (any != null) {
171             try {
172                 for (int i=0;i<any.length;i++) {
173                     refProp.appendChild(doc.importNode(any[i].getAsDOM(), 
174                                                        true));
175                 }
176             } catch (Exception e) {
177                 log.debug("", e);
178             }
179         }
180         parent.appendChild(refProp);
181     }
182 
183     public static ReferencePropertiesType fromSOAPElement(SOAPElement element)
184         throws Exception {
185         ReferencePropertiesType props = null;
186         Iterator iter = element.getChildElements();
187         while(iter.hasNext()) {
188             javax.xml.soap.Node child = (javax.xml.soap.Node)iter.next();
189             if (!(child instanceof SOAPElement)) {
190                 continue;
191             }
192             if (props == null) {
193                 props = new ReferencePropertiesType();
194             }
195 
196             if (child instanceof MessageElement) {
197                 props.add( ((MessageElement)child).getAsDOM() );
198             } else {
199                 props.add(child);
200             }
201         }
202         return props;
203     }
204 
205     public static ReferencePropertiesType fromElement(Element element)
206         throws Exception {
207         ReferencePropertiesType props = null;
208         NodeList children = element.getChildNodes();
209         for (int i=0;i<children.getLength();i++) {
210             Node child = (Node)children.item(i);
211             if (!(child instanceof Element)) {
212                 continue;
213             }
214             if (props == null) {
215                 props = new ReferencePropertiesType();
216             }
217 
218             props.add(child);
219         }
220         return props;
221     }
222 
223     /***
224      * Gets the _any value for this ReferencePropertiesType.
225      * 
226      * @return _any
227      */
228     public MessageElement [] get_any() {
229         return _any;
230     }
231     
232     /***
233      * Sets the _any value for this ReferencePropertiesType.
234      * 
235      * @param _any
236      */
237     public void set_any(MessageElement [] _any) {
238         this._any = _any;
239     }
240     
241     public String toString() {
242         if (this._any == null) {
243             return "";
244         }
245         StringBuffer buf = new StringBuffer();
246         for (int i=0;i<this._any.length;i++) {
247             buf.append("Reference property[" + i + "]:\n");
248             try {
249                 buf.append(XMLUtils.ElementToString(this._any[i].getAsDOM()));
250             } catch (Exception e) {
251                 buf.append("<error converting: " + e.getMessage() + ">");
252             }
253             buf.append("\n");
254         }
255         return buf.toString();
256     }
257 
258     // Type metadata
259     private static org.apache.axis.description.TypeDesc typeDesc =
260         new org.apache.axis.description.TypeDesc(ReferencePropertiesType.class, true);
261     
262     static {
263         typeDesc.setXmlType(new javax.xml.namespace.QName(Constants.NS_URI_ADDRESSING_DEFAULT, "ReferencePropertiesType"));
264     }
265 
266     /***
267      * Return type metadata object
268      */
269     public static org.apache.axis.description.TypeDesc getTypeDesc() {
270         return typeDesc;
271     }
272 
273 
274     /***
275      * Get Custom Serializer
276      */
277     public static org.apache.axis.encoding.Serializer getSerializer(
278            java.lang.String mechType, 
279            java.lang.Class _javaType,  
280            javax.xml.namespace.QName _xmlType) {
281         return 
282           new  org.apache.axis.encoding.ser.BeanSerializer(
283             _javaType, _xmlType, typeDesc);
284     }
285 
286     /***
287      * Get Custom Deserializer
288      */
289     public static org.apache.axis.encoding.Deserializer getDeserializer(
290            java.lang.String mechType, 
291            java.lang.Class _javaType,  
292            javax.xml.namespace.QName _xmlType) {
293         return 
294           new  org.apache.axis.encoding.ser.BeanDeserializer(
295             _javaType, _xmlType, typeDesc);
296     }
297 
298 }
299 
300