2002-09-19 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / CodeIdentifier.cs
1 //
2 // System.Xml.Serialization.CodeIdentifier.cs
3 //
4 // Author: 
5 //    Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 using System;
11
12 namespace System.Xml.Serialization {
13         public class CodeIdentifier {
14
15                 public CodeIdentifier ()
16                 {
17                 }
18
19                 public static string MakeCamel (string identifier)
20                 {
21                         string validIdentifier = MakeValid (identifier);
22                         return (Char.ToLower (validIdentifier[0]) + validIdentifier.Substring (1));
23                 }
24
25                 public static string MakePascal (string identifier)
26                 {
27                         string validIdentifier = MakeValid (identifier);
28                         return (Char.ToUpper (validIdentifier[0]) + validIdentifier.Substring (1));
29                 }
30
31                 public static string MakeValid (string identifier)
32                 {
33                         if (identifier == null)
34                                 throw new NullReferenceException ();
35                         if (identifier.Length == 0)
36                                 return identifier;
37
38                         string output = "";
39
40                         if (Char.IsNumber (identifier[0]))
41                                 output = "Item";
42
43                         foreach (char c in identifier) 
44                                 if (Char.IsLetterOrDigit (c) || c == '_')
45                                         output += c;
46
47                         return output;
48                 }
49         }
50 }