// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) Ximian, Inc. http://www.ximian.com
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
using Mono.Xml;
-namespace System.Security
-{
+namespace System.Security {
+
+#if NET_2_0
+ [ComVisible (true)]
+#endif
[Serializable]
public sealed class SecurityElement
{
throw new ArgumentException (Locale.GetText ("Invalid XML attribute value") + ": " + value);
_name = name;
- _value = value;
+ _value = SecurityElement.Unescape (value);
}
public string Name {
public SecurityElement (string tag, string text)
{
- this.Tag = tag;
- this.Text = text;
+ if (tag == null)
+ throw new ArgumentNullException ("tag");
+ if (!IsValidTag (tag))
+ throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + tag);
+ this.tag = tag;
+
+ Text = text;
}
// not a deep copy (childs are references)
return result;
}
- set {
+ set {
if (value == null || value.Count == 0) {
attributes.Clear ();
return;
}
set {
if (value == null)
- throw new ArgumentNullException ();
+ throw new ArgumentNullException ("Tag");
if (!IsValidTag (value))
throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + value);
- int colon = value.IndexOf (':');
- tag = colon < 0 ? value : value.Substring (colon + 1);
+ tag = value;
}
}
}
set {
- if (!IsValidText (value))
- throw new ArgumentException (Locale.GetText ("Invalid XML string") + ": " + text);
- text = value;
+ if (value != null) {
+ if (!IsValidText (value))
+ throw new ArgumentException (
+ Locale.GetText ("Invalid XML string")
+ + ": " + value);
+ }
+ text = Unescape (value);
}
}
public static string Escape (string str)
{
StringBuilder sb;
-
+
+ if (str == null)
+ return null;
+
if (str.IndexOfAny (invalid_chars) == -1)
return str;
return sb.ToString ();
}
+ private static string Unescape (string str)
+ {
+ StringBuilder sb;
+
+ if (str == null)
+ return null;
+
+ sb = new StringBuilder (str);
+ sb.Replace ("<", "<");
+ sb.Replace (">", ">");
+ sb.Replace ("&", "&");
+ sb.Replace (""", "\"");
+ sb.Replace ("'", "'");
+ return sb.ToString ();
+ }
+
#if NET_2_0
- public
+ public
#else
internal
#endif
SecurityParser sp = new SecurityParser ();
sp.LoadXml (xml);
return sp.ToXml ();
- }
- catch (Exception e) {
+ } catch (Exception e) {
string msg = Locale.GetText ("Invalid XML.");
throw new XmlSyntaxException (msg, e);
}
return value != null && value.IndexOfAny (invalid_attr_value_chars) == -1;
}
- public static bool IsValidTag (string value)
+ public static bool IsValidTag (string tag)
{
- return value != null && value.IndexOfAny (invalid_tag_chars) == -1;
+ return tag != null && tag.IndexOfAny (invalid_tag_chars) == -1;
}
- public static bool IsValidText (string value)
+ public static bool IsValidText (string text)
{
- if (value == null)
- return true;
- return value.IndexOfAny (invalid_text_chars) == -1;
+ return text != null && text.IndexOfAny (invalid_text_chars) == -1;
}
public SecurityElement SearchForChildByTag (string tag)
return elem;
}
return null;
- }
+ }
public string SearchForTextOfTag (string tag)
{
return result;
}
- return null;
+ return null;
}
public override string ToString ()
#endif
s.Append (sa.Name)
.Append ("=\"")
- .Append (sa.Value)
+ .Append (Escape (sa.Value))
.Append ("\"");
if (i != attributes.Count - 1)
s.Append (Environment.NewLine);
(children == null || children.Count == 0))
s.Append ("/>").Append (Environment.NewLine);
else {
- s.Append (">").Append (text);
+ s.Append (">").Append (Escape (text));
if (children != null) {
s.Append (Environment.NewLine);
foreach (SecurityElement child in children) {