Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use some helper methods.

  • Loading branch information...
commit 1a74c3c5d3edec4e050a5c82ab6d4196e52a5eb9 1 parent 6b5a1fc
@nrk authored
View
12 src/Data/AttributeData.cs
@@ -3,7 +3,6 @@
namespace IronRuby.Hpricot {
public class AttributeData : BasicData {
- // TODO: Hash or MutableString?
private Object _attr;
public Object Attr {
@@ -11,8 +10,15 @@ public class AttributeData : BasicData {
set { _attr = value; }
}
- // TODO: this smells, will fix later
- public bool AttrIsNull {
+ public MutableString AttrAsMutableString {
+ get { return (MutableString)_attr; }
+ }
+
+ public Hash AttrAsHash {
+ get { return (Hash)_attr; }
+ }
+
+ public bool AttrIsEmpty {
get { return _attr == null; }
}
}
View
4 src/Data/BasicData.cs
@@ -15,5 +15,9 @@ public class BasicData {
get { return _parent; }
set { _parent = value; }
}
+
+ public T As<T>() where T : BasicData {
+ return (T)this;
+ }
}
}
View
4 src/Elements/BaseEle.cs
@@ -29,12 +29,12 @@ public BaseElement(RubyContext context)
}
public T GetData<T>() where T : BasicData {
- return _data as T;
+ return _data.As<T>();
}
protected virtual MutableString RawString {
get {
- return _data.Tag != null ? _data.Tag as MutableString : MutableString.CreateEmpty();
+ return _data.Tag != null ? _data.Tag : MutableString.CreateEmpty();
}
}
View
30 src/Elements/DocumentType.cs
@@ -32,8 +32,8 @@ public DocumentType(RubyContext context)
MutableString doctype = MutableString.CreateEmpty();
doctype.AppendFormat("<!DOCTYPE {0} ", GetTarget(this));
- AttributeData data = _data as AttributeData;
- if (!data.AttrIsNull) {
+ var data = _data.As<AttributeData>();
+ if (!data.AttrIsEmpty) {
object publicId = GetPublicId(this);
doctype.Append(publicId != null ? String.Format("PUBLIC \"{0}\"", publicId) : "SYSTEM");
@@ -60,44 +60,42 @@ public DocumentType(RubyContext context)
[RubyMethod("public_id")]
public static Object GetPublicId(DocumentType/*!*/ self) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
return null;
}
-
Object value;
- (data.Attr as Hash).TryGetValue(self._publicId, out value);
+ data.AttrAsHash.TryGetValue(self._publicId, out value);
return value;
}
[RubyMethod("public_id=")]
public static void SetPublicId(RubyContext context/*!*/, DocumentType/*!*/ self, Object/*!*/ publicId) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
data.Attr = new Hash(context);
}
- (data.Attr as Hash)[self._publicId] = publicId;
+ data.AttrAsHash[self._publicId] = publicId;
}
[RubyMethod("system_id")]
public static Object GetSystemId(DocumentType/*!*/ self) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
return null;
}
-
Object value;
- (data.Attr as Hash).TryGetValue(self._systemId, out value);
+ data.AttrAsHash.TryGetValue(self._systemId, out value);
return value;
}
[RubyMethod("system_id=")]
public static void SetSystemId(RubyContext context/*!*/, DocumentType/*!*/ self, Object/*!*/ systemId) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
data.Attr = new Hash(context);
}
- (data.Attr as Hash)[self._systemId] = systemId;
+ data.AttrAsHash[self._systemId] = systemId;
}
}
}
View
6 src/Elements/ETag.cs
@@ -24,14 +24,12 @@ public ETag(RubyContext context)
[RubyMethod("raw_string")]
public static MutableString GetRawString(ETag/*!*/ self) {
- Debug.Assert(self._data is AttributeData, "self._data is not an instance of AttributeData");
- return (self._data as AttributeData).Attr as MutableString;
+ return self.GetData<AttributeData>().AttrAsMutableString;
}
[RubyMethod("clear_raw")]
public static bool ClearRaw(ETag/*!*/ self) {
- Debug.Assert(self._data is AttributeData, "self._data is not an instance of AttributeData");
- (self._data as AttributeData).Attr = null;
+ self.GetData<AttributeData>().Attr = null;
return true;
}
View
16 src/Elements/Element.cs
@@ -24,43 +24,43 @@ public Element(RubyContext context)
[RubyMethod("raw_string")]
public static Object GetRawString(Element/*!*/ self) {
- return (self._data as ElementData).Raw;
+ return self.GetData<ElementData>().Raw;
}
[RubyMethod("clear_raw")]
public static bool ClearRaw(Element/*!*/ self) {
- (self._data as ElementData).Raw = null;
+ self.GetData<ElementData>().Raw = null;
return true;
}
[RubyMethod("raw_attributes")]
public static Object GetRawAttributes(Element/*!*/ self) {
- return (self._data as ElementData).Attr;
+ return self.GetData<ElementData>().Attr;
}
[RubyMethod("raw_attributes=")]
public static void SetRawAttributes(Element/*!*/ self, Object/*!*/ rawAttributes) {
- (self._data as ElementData).Attr = rawAttributes;
+ self.GetData<ElementData>().Attr = rawAttributes;
}
[RubyMethod("children")]
public static IList<Object> GetChildren(Element/*!*/ self) {
- return (self._data as ElementData).Children;
+ return self.GetData<ElementData>().Children;
}
[RubyMethod("children=")]
public static void SetChildren(Element/*!*/ self, IList<Object>/*!*/ children) {
- (self._data as ElementData).Children = children;
+ self.GetData<ElementData>().Children = children;
}
[RubyMethod("etag")]
public static IHpricotDataContainer GetEtag(Element/*!*/ self) {
- return (self._data as ElementData).ETag;
+ return self.GetData<ElementData>().ETag;
}
[RubyMethod("etag=")]
public static void SetEtag(Element/*!*/ self, IHpricotDataContainer/*!*/ etag) {
- (self._data as ElementData).ETag = etag;
+ self.GetData<ElementData>().ETag = etag;
}
[RubyMethod("name")]
View
4 src/Elements/ProcedureInstruction.cs
@@ -30,12 +30,12 @@ public ProcedureInstruction(RubyContext context)
[RubyMethod("content")]
public static MutableString GetContent(ProcedureInstruction/*!*/ self) {
- return (self._data as AttributeData).Attr as MutableString;
+ return self.GetData<AttributeData>().AttrAsMutableString;
}
[RubyMethod("content=")]
public static void SetContent(ProcedureInstruction/*!*/ self, Object/*!*/ content) {
- (self._data as AttributeData).Attr = content;
+ self.GetData<AttributeData>().Attr = content;
}
[RubyMethod("target")]
View
43 src/Elements/XmlDeclaration.cs
@@ -35,8 +35,8 @@ public XmlDeclaration(RubyContext context)
MutableString xmldecl = MutableString.CreateEmpty();
xmldecl.AppendFormat("<?xml version=\"{0}\"", GetVersion(this));
- AttributeData data = _data as AttributeData;
- if (!data.AttrIsNull) {
+ var data = _data.As<AttributeData>();
+ if (!data.AttrIsEmpty) {
object encoding = GetEncoding(this);
if (encoding != null) {
xmldecl.AppendFormat(" encoding=\"{0}\"", encoding);
@@ -55,65 +55,62 @@ public XmlDeclaration(RubyContext context)
[RubyMethod("encoding")]
public static Object GetEncoding(XmlDeclaration/*!*/ self) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
return null;
}
-
Object value;
- (data.Attr as Hash).TryGetValue(self._encoding, out value);
+ data.AttrAsHash.TryGetValue(self._encoding, out value);
return value;
}
[RubyMethod("encoding=")]
public static void SetEncoding(RubyContext/*!*/ context, XmlDeclaration/*!*/ self, Object/*!*/ encoding) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
data.Attr = new Hash(context);
}
- (data.Attr as Hash)[self._encoding] = encoding;
+ data.AttrAsHash[self._encoding] = encoding;
}
[RubyMethod("standalone")]
public static Object GetStandalone(XmlDeclaration/*!*/ self) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
return null;
}
-
Object value;
- (data.Attr as Hash).TryGetValue(self._standalone, out value);
+ data.AttrAsHash.TryGetValue(self._standalone, out value);
return value;
}
[RubyMethod("standalone=")]
public static void SetStandalone(RubyContext/*!*/ context, XmlDeclaration/*!*/ self, Object/*!*/ standalone) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
data.Attr = new Hash(context);
}
- (data.Attr as Hash)[self._standalone] = standalone;
+ data.AttrAsHash[self._standalone] = standalone;
}
[RubyMethod("version")]
public static Object GetVersion(XmlDeclaration/*!*/ self) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
return null;
}
-
Object value;
- (data.Attr as Hash).TryGetValue(self._version, out value);
+ data.AttrAsHash.TryGetValue(self._version, out value);
return value;
}
[RubyMethod("version=")]
public static void SetVersion(RubyContext/*!*/ context, XmlDeclaration/*!*/ self, Object/*!*/ version) {
- AttributeData data = self._data as AttributeData;
- if (data.AttrIsNull) {
+ var data = self.GetData<AttributeData>();
+ if (data.AttrIsEmpty) {
data.Attr = new Hash(context);
}
- (data.Attr as Hash)[self._version] = version;
+ data.AttrAsHash[self._version] = version;
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.