1 /*----------------------------------------------------------------------------
  2 Name:      Key.cs
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Access Key informations
  6            Please consult the javadoc of the Java client library for API usage
  7 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  8 Date:      12/2006
  9 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.html
 10 -----------------------------------------------------------------------------*/
 11 using System;
 12 using System.Text;
 13 
 14 namespace org.xmlBlaster.client
 15 {
 16    public class Key : KeyQosParser
 17    {
 18       public const string XPATH = "XPATH";
 19       public const string XPATH_URL_PREFIX = "xpath:";
 20       public const string EXACT = "EXACT";
 21       public const string EXACT_URL_PREFIX = "exact:";
 22       public const string DOMAIN = "DOMAIN";
 23       public const string DOMAIN_URL_PREFIX = "domain:";
 24       public const string REGEX = "REGEX";
 25 
 26       public const string INTERNAL_OID_PREFIX = "__sys__";
 27       public const string OID_DEAD_LETTER = INTERNAL_OID_PREFIX + "deadMessage";
 28       public const string INTERNAL_OID_ADMIN_CMD = "__cmd:";
 29 
 30       public const string DEFAULT_DOMAIN = null;
 31 
 32       public Key(string key)
 33          : base(key)
 34       {
 35       }
 36 
 37       public string ToXml()
 38       {
 39          return this.xml;
 40       }
 41 
 42       public string GetOid()
 43       {
 44          return GetXPath("/key/@oid", "");
 45       }
 46 
 47       public bool IsDeadMessage()
 48       {
 49          return OID_DEAD_LETTER.Equals(GetOid());
 50       }
 51 
 52       /// Messages starting with "__cmd:" are administrative messages

 53       public bool IsAdministrative()
 54       {
 55          string oid = GetOid();
 56          return (oid == null) ? false : oid.StartsWith(INTERNAL_OID_ADMIN_CMD);
 57       }
 58 
 59       public string GetContentMime()
 60       {
 61          return GetXPath("/key/@contentMime", "");
 62       }
 63 
 64       public string GetContentMimeExtended()
 65       {
 66          return GetXPath("/key/@contentMimeExtended", "");
 67       }
 68 
 69       public string GetDomain()
 70       {
 71          return GetXPath("/key/@domain", "");
 72       }
 73 
 74       /**
 75        * @return true if no domain is given (null or empty string). 
 76        */
 77       public bool IsDefaultDomain()
 78       {
 79          string domain = GetDomain();
 80          if (domain == null || domain.Length < 1 || domain.Equals(DEFAULT_DOMAIN))
 81             return true;
 82          return false;
 83       }
 84    }
 85 
 86    public class MsgKey : Key
 87    {
 88       public MsgKey(string key) : base(key) { }
 89       public string toXml()
 90       {
 91          return this.xml;
 92       }
 93       public string GetClientTags()
 94       {
 95          return GetXPath("/key/child::node()", "");
 96       }
 97 
 98    }
 99 
100    public class UpdateKey : MsgKey
101    {
102       public UpdateKey(string qos)
103          : base(qos)
104       {
105       }
106    }
107 
108    public class GetKey : MsgKey
109    {
110       public GetKey(string qos)
111          : base(qos)
112       {
113       }
114    }
115 
116    public class StatusKey : Key
117    {
118       public StatusKey(string qos)
119          : base(qos)
120       {
121       }
122       public string GetQueryType()
123       {
124          return GetXPath("/key/@queryType", "");
125       }
126 
127       public String GetQueryString()
128       {
129          // TODO: handle CDATA

130          return GetXPath("/key/text()", "");
131       }
132 
133       public bool IsExact()
134       {
135          return EXACT.Equals(GetQueryType());
136       }
137 
138       public bool IsQuery()
139       {
140          return XPATH.Equals(GetQueryType()) ||
141                 REGEX.Equals(GetQueryType());
142       }
143 
144       public bool IsXPath()
145       {
146          return XPATH.Equals(GetQueryType());
147       }
148 
149       public bool IsDomain()
150       {
151          return DOMAIN.Equals(GetQueryType());
152       }
153 
154       /// Access simplified URL like string. 

155       /// Examples are "exact:hello", "xpath://key", "domain:sport"

156       public string GetUrl()
157       {
158          if (IsExact())
159             return EXACT_URL_PREFIX + GetOid();
160          else if (IsXPath())
161             return XPATH_URL_PREFIX + GetQueryString().Trim();
162          else if (IsDomain())
163             return DOMAIN_URL_PREFIX + GetDomain();
164          throw new ApplicationException("getUrl() failed: Unknown query type: " + ToXml());
165       }
166 
167       /**
168        * Check if same query is used
169        */
170       public bool Equals(StatusKey other)
171       {
172          string oid = GetOid();
173          string queryString = GetQueryString();
174          string domain = GetDomain();
175          return IsExact() && other.IsExact() && oid.Equals(other.GetOid()) ||
176                 IsQuery() && other.IsQuery() && queryString.Trim().Equals(other.GetQueryString().Trim()) ||
177                 IsDomain() && other.IsDomain() && domain.Trim().Equals(other.GetDomain().Trim());
178       }
179    }
180 }


syntax highlighted by Code2HTML, v. 0.9.1