CodeIdentifier.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //
  2. // System.Xml.Serialization.CodeIdentifier.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System;
  10. namespace System.Xml.Serialization {
  11. public class CodeIdentifier {
  12. public CodeIdentifier ()
  13. {
  14. }
  15. public static string MakeCamel (string identifier)
  16. {
  17. string validIdentifier = MakeValid (identifier);
  18. return (Char.ToLower (validIdentifier[0]) + validIdentifier.Substring (1));
  19. }
  20. public static string MakePascal (string identifier)
  21. {
  22. string validIdentifier = MakeValid (identifier);
  23. return (Char.ToUpper (validIdentifier[0]) + validIdentifier.Substring (1));
  24. }
  25. public static string MakeValid (string identifier)
  26. {
  27. if (identifier == null)
  28. throw new NullReferenceException ();
  29. if (identifier.Length == 0)
  30. return identifier;
  31. string output = "";
  32. if (!Char.IsLetter (identifier[0]) && (identifier[0]!='_') )
  33. output = "Item";
  34. foreach (char c in identifier)
  35. if (Char.IsLetterOrDigit (c) || c == '_')
  36. output += c;
  37. if (output.Length > 400)
  38. output = output.Substring (0,400);
  39. return output;
  40. }
  41. }
  42. }