1 /*----------------------------------------------------------------------------
  2 Name:      ServiceTO.cs
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   A generic service approach
  6 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7 Date:      04/2007
  8 See:       http://www.xmlblaster.org/
  9 -----------------------------------------------------------------------------*/
 10 using System;
 11 using System.Collections.Generic;
 12 using System.Collections;
 13 using System.Text;
 14 using System.Xml;
 15 using System.Xml.Schema;
 16 using System.Xml.Serialization;
 17 
 18 namespace org.xmlBlaster.contrib.service {
 19    /// <summary>

 20    /// The transfer object for services

 21    /// </summary>

 22    [XmlRootAttribute("s", IsNullable = false)]
 23    public class ServiceTO : IXmlSerializable {
 24 
 25       public static readonly string SERVICE = "s"; // tag name

 26 
 27       private List<PropTO> propTOs;
 28 
 29       public ServiceTO() {
 30          this.propTOs = new List<PropTO>();
 31       }
 32 
 33       public ServiceTO(ServiceTO service)
 34       {
 35          this.propTOs = new List<PropTO>();
 36          if (service == null)
 37          {
 38             return;
 39          }
 40 
 41          PropTO pp = service.getProp(PropTO.KEY_TASKTYPE);
 42          if (pp != null)
 43             addProp(pp);
 44 
 45          pp = service.getProp(PropTO.KEY_TASK);
 46          if (pp != null)
 47             addProp(pp);
 48 
 49          pp = service.getProp(PropTO.KEY_RESULTENCODING);
 50          if (pp != null)
 51             addProp(pp);
 52 
 53          pp = service.getProp(PropTO.KEY_RESULTMIME);
 54          if (pp != null)
 55             addProp(pp);
 56 
 57          List<PropTO> pc = service.getProps();
 58          foreach (PropTO p in pc)
 59          {
 60             if (p.GetKey().StartsWith(PropTO.KEY_BOUNCE))
 61             {
 62                this.propTOs.Add(p);
 63             }
 64          }
 65       }
 66 
 67       public ServiceTO(List<PropTO> propTOs) {
 68          this.propTOs = (propTOs == null) ? new List<PropTO>() : propTOs;
 69       }
 70 
 71       public List<PropTO> getProps() {
 72          if (this.propTOs == null)
 73          {
 74             this.propTOs = new List<PropTO>();
 75          }
 76          return this.propTOs;
 77       }
 78 
 79       public PropTO getProp(String key) {
 80          if (key == null)
 81             return null;
 82          if (this.propTOs == null)
 83             return null;
 84          foreach (PropTO propTO in getProps()) {
 85             if (key.Equals(propTO.GetKey()))
 86                return propTO;
 87          }
 88          return null;
 89       }
 90 
 91       /// <summary>

 92       /// Access the raw property data

 93       /// </summary>

 94       /// <param name="key"></param>

 95       /// <returns>Never null</returns>

 96       public byte[] getPropValueBytes(String key)
 97       {
 98          PropTO propTO = getProp(key);
 99          if (propTO == null)
100             return new byte[0];
101          byte[] val = propTO.getValueBytes();
102          return (val == null) ? new byte[0] : val;
103       }
104 
105       /**
106        * Access the property value.
107        * @param key
108        * @return never null
109        */
110       public String getPropValue(String key) {
111          PropTO propTO = getProp(key);
112          if (propTO == null)
113             return "";
114          String val = propTO.GetValue();
115          return (val == null) ? "" : val;
116       }
117 
118       public String getPropValue(String key, String defaultValue)
119       {
120          PropTO propTO = getProp(key);
121          if (propTO == null)
122             return defaultValue;
123          String val = propTO.GetValue();
124          return (val == null) ? defaultValue : val;
125       }
126 
127       public bool addProp(PropTO propTO) {
128          if (propTO == null) return false;
129          getProps().Add(propTO);
130          return true;
131       }
132 
133       public PropTO setData(byte[] data)
134       {
135          PropTO dat = new PropTO(PropTO.KEY_DATA, data);
136          addProp(dat);
137          if (dat.isBase64Encoding())
138             addProp(new PropTO(PropTO.KEY_DATAENCODING, PropTO.ENCODING_BASE64));
139          return dat;
140       }
141 
142       public void setProps(List<PropTO> propTOs) {
143          this.propTOs = propTOs;
144       }
145 
146       public static List<ServiceTO> ReadSiblings(XmlReader reader) {
147          List<ServiceTO> list = new List<ServiceTO>();
148          /*
149           * true if a matching descendant element is found; otherwise false.
150           * If a matching child element is not found, the XmlReader is positioned
151           * on the end tag (NodeType is XmlNodeType.EndElement) of the element.
152           * If the XmlReader is not positioned on an element when ReadToDescendant
153           * was called, this method returns false and the position of the XmlReader
154           * is not changed. 
155           */
156          bool found = reader.ReadToDescendant(ServiceTO.SERVICE);
157          if (found) {
158             do {
159                ServiceTO service = new ServiceTO(new List<PropTO>());
160                service.setProps(PropTO.ReadSiblings(reader));
161                list.Add(service);
162             } while (reader.ReadToNextSibling(ServiceTO.SERVICE));
163          }
164 
165          return list;
166       }
167 
168       public void ReadXml(XmlReader reader) {
169          bool found = true;
170          if (!SERVICE.Equals(reader.LocalName))
171             found = reader.ReadToDescendant(SERVICE);
172          if (found) {
173             this.propTOs = PropTO.ReadSiblings(reader);
174          }
175       }
176 
177       public void WriteXml(XmlWriter writer) {
178          writer.WriteStartElement(SERVICE);
179          foreach (PropTO propTO in getProps()) {
180             propTO.WriteXml(writer);
181          }
182          writer.WriteEndElement();
183       }
184 
185       public XmlSchema GetSchema() {
186          return null;
187       }
188 
189       public static ServiceTO parse(string xml) {
190          ServiceTO service = org.xmlBlaster.util.Serialization.DeserializeStr<ServiceTO>(xml);
191          return service;
192       }
193 
194       public string ToXml() {
195          string xml = org.xmlBlaster.util.Serialization.SerializeStr<ServiceTO>(this);
196          return xml;
197       }
198    }
199 }


syntax highlighted by Code2HTML, v. 0.9.1