2 // System.Windows.Markup.XmlLanguage.cs
5 // Moonlight List (moonlight-list@lists.ximian.com)
7 // Copyright (C) 2008, 2009 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Globalization
;
33 using System
.Windows
.Media
;
35 namespace System
.Windows
.Markup
{
36 public sealed class XmlLanguage
{
39 internal XmlLanguage (string ietfLanguageTag
)
41 lang
= ietfLanguageTag
.ToLower (CultureInfo
.InvariantCulture
);
44 public static XmlLanguage
GetLanguage (string ietfLanguageTag
)
49 // verify language code is all us-ascii alphabetic
50 for (i
= 0; i
< ietfLanguageTag
.Length
; i
++) {
51 if (ietfLanguageTag
[i
] == '-')
54 c
= Char
.ToLower (ietfLanguageTag
[i
]);
56 if (c
< 'a' || c
> 'z')
57 throw new ArgumentException ("ietfLanguageTag");
60 // language code needs at least 2 characters (if non-empty)
62 throw new ArgumentException ("ietfLanguageTag");
64 if (i
< ietfLanguageTag
.Length
) {
67 // country code needs at least 2 characters
68 if ((ietfLanguageTag
.Length
- i
) < 2)
69 throw new ArgumentException ("ietfLanguageTag");
71 // verify country code is all us-ascii alpha-numeric
72 for ( ; i
< ietfLanguageTag
.Length
; i
++) {
73 c
= Char
.ToLower (ietfLanguageTag
[i
]);
75 if (!(c
>= 'a' && c
<= 'z') && !(c
>= '0' && c
<= '9'))
76 throw new ArgumentException ("ietfLanguageTag");
80 return new XmlLanguage (ietfLanguageTag
);
83 public string IetfLanguageTag
{
87 public override bool Equals (object obj
)
89 XmlLanguage lang
= obj
as XmlLanguage
;
94 return IetfLanguageTag
.Equals (lang
.IetfLanguageTag
);
97 public override int GetHashCode ()
99 return IetfLanguageTag
.GetHashCode ();
102 public static bool operator ==(XmlLanguage xmlLanguage1
, XmlLanguage xmlLanguage2
)
104 if ( object.ReferenceEquals(xmlLanguage1
, null)
105 || object.ReferenceEquals(xmlLanguage2
, null) )
108 return xmlLanguage1
.IetfLanguageTag
== xmlLanguage2
.IetfLanguageTag
;
111 public static bool operator !=(XmlLanguage xmlLanguage1
, XmlLanguage xmlLanguage2
)
113 return !(xmlLanguage1
== xmlLanguage2
);