677c17f048c0bae1943d61889dce6525c0ffe602
[mono.git] / mcs / class / RabbitMQ.Client / src / client / content / PrimitiveParser.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007, 2008 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
10 //   Licensed under the Apache License, Version 2.0 (the "License");
11 //   you may not use this file except in compliance with the License.
12 //   You may obtain a copy of the License at
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
16 //   Unless required by applicable law or agreed to in writing, software
17 //   distributed under the License is distributed on an "AS IS" BASIS,
18 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 //   See the License for the specific language governing permissions and
20 //   limitations under the License.
21 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
25 //---------------------------------------------------------------------------
26 //   The contents of this file are subject to the Mozilla Public License
27 //   Version 1.1 (the "License"); you may not use this file except in
28 //   compliance with the License. You may obtain a copy of the License at
29 //   http://www.rabbitmq.com/mpl.html
30 //
31 //   Software distributed under the License is distributed on an "AS IS"
32 //   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 //   License for the specific language governing rights and limitations
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd.,
39 //   Cohesive Financial Technologies LLC., and Rabbit Technologies Ltd.
40 //
41 //   Portions created by LShift Ltd., Cohesive Financial Technologies
42 //   LLC., and Rabbit Technologies Ltd. are Copyright (C) 2007, 2008
43 //   LShift Ltd., Cohesive Financial Technologies LLC., and Rabbit
44 //   Technologies Ltd.;
45 //
46 //   All Rights Reserved.
47 //
48 //   Contributor(s): ______________________________________.
49 //
50 //---------------------------------------------------------------------------
51 using System;
52 using System.Net;
53 using System.Text;
54 using System.IO;
55
56 using RabbitMQ.Client;
57 using RabbitMQ.Util;
58
59 namespace RabbitMQ.Client.Content {
60     ///<summary>Utility class for extracting typed values from strings.</summary>
61     public class PrimitiveParser {
62         ///<summary>Causes ProtocolViolationException to be thrown;
63         ///called by the various "Parse*" methods when a syntax error
64         ///is detected.</summary>
65         ///<exception cref="ProtocolViolationException"/>
66         public static void InvalidConversion(string target, object source) {
67             throw new ProtocolViolationException(string.Format("Invalid conversion to {0}: {1}",
68                                                                target, source));
69         }
70
71         ///<summary>Attempt to parse a string representation of a bool.</summary>
72         ///<exception cref="ProtocolViolationException"/>
73         public static bool ParseBool(string value) {
74             try {
75                 return bool.Parse(value);
76             } catch {
77                 InvalidConversion("bool", value);
78                 return false;
79             }
80         }
81
82         ///<summary>Attempt to parse a string representation of an int.</summary>
83         ///<exception cref="ProtocolViolationException"/>
84         public static int ParseInt(string value) {
85             try {
86                 return int.Parse(value);
87             } catch {
88                 InvalidConversion("int", value);
89                 return 0;
90             }
91         }
92
93         ///<summary>Attempt to parse a string representation of a short.</summary>
94         ///<exception cref="ProtocolViolationException"/>
95         public static short ParseShort(string value) {
96             try {
97                 return short.Parse(value);
98             } catch {
99                 InvalidConversion("short", value);
100                 return 0;
101             }
102         }
103
104         ///<summary>Attempt to parse a string representation of a byte.</summary>
105         ///<exception cref="ProtocolViolationException"/>
106         public static byte ParseByte(string value) {
107             try {
108                 return byte.Parse(value);
109             } catch {
110                 InvalidConversion("byte", value);
111                 return 0;
112             }
113         }
114
115         ///<summary>Attempt to parse a string representation of a long.</summary>
116         ///<exception cref="ProtocolViolationException"/>
117         public static long ParseLong(string value) {
118             try {
119                 return long.Parse(value);
120             } catch {
121                 InvalidConversion("long", value);
122                 return 0;
123             }
124         }
125
126         ///<summary>Attempt to parse a string representation of a float.</summary>
127         ///<exception cref="ProtocolViolationException"/>
128         public static float ParseFloat(string value) {
129             try {
130                 return float.Parse(value);
131             } catch {
132                 InvalidConversion("float", value);
133                 return 0;
134             }
135         }
136
137         ///<summary>Attempt to parse a string representation of a double.</summary>
138         ///<exception cref="ProtocolViolationException"/>
139         public static double ParseDouble(string value) {
140             try {
141                 return double.Parse(value);
142             } catch {
143                 InvalidConversion("double", value);
144                 return 0;
145             }
146         }
147     }
148 }