/// <summary> /// An indexer for the tag's values /// </summary> /// <param name="index">The <c>index</c> to get or set</param> /// <returns>The value at the given <c>index</c></returns> public object this[int index] { get { return(values[index]); } set { valuesDirty = true; values[index] = SDLUtil.CoerceOrFail(value); } }
/// <summary> /// Set the <c>key</c> to the given value and sets the namespace. /// </summary> /// <param name="sdlNamespace">The namespace for this attribute</param> /// <param name="key">The key for this attribute</param> public object this[string sdlNamespace, string key] { set { attributesDirty = true; attributes[SDLUtil.ValidateIdentifier(key)] = SDLUtil.CoerceOrFail(value); if (sdlNamespace == null) { sdlNamespace = ""; } if (sdlNamespace.Length != 0) { SDLUtil.ValidateIdentifier(sdlNamespace); } attributeToNamespace[key] = sdlNamespace; } }
/// <summary> /// Returns a string containing an XML representation of this tag. /// Values will be represented using _val0, _val1, etc. /// </summary> /// <param name="linePrefix">A prefix to insert before every line. /// </param> /// <returns>An XML String describing this Tag</returns> String ToXMLString(string linePrefix) { String newLine = "\r\n"; if (linePrefix == null) { linePrefix = ""; } StringBuilder builder = new StringBuilder(linePrefix + "<"); if (!sdlNamespace.Equals("")) { builder.Append(sdlNamespace + ":"); } builder.Append(name); // output values if (values.Count != 0) { int i = 0; foreach (object val in values) { builder.Append(" "); builder.Append("_val" + i + "=\"" + SDLUtil.Format(val, false) + "\""); i++; } } // output attributes if (attributes.Count != 0) { foreach (string key in attributes.Keys) { builder.Append(" "); String attNamespace = attributeToNamespace[key]; if (!attNamespace.Equals("")) { builder.Append(attNamespace + ":"); } builder.Append(key + "="); builder.Append("\"" + SDLUtil.Format(attributes[key], false) + "\""); } } if (children.Count != 0) { builder.Append(">" + newLine); foreach (Tag t in children) { builder.Append(t.ToXMLString(linePrefix + " ") + newLine); } builder.Append(linePrefix + "</"); if (!sdlNamespace.Equals("")) { builder.Append(sdlNamespace + ":"); } builder.Append(name + ">"); } else { builder.Append("/>"); } return(builder.ToString()); }
/// <summary> /// Produces a full SDL "dump" of this tag using the given prefix before /// each line. /// </summary> /// <param name="linePrefix">Text to be prefixed to each line</param> /// <returns>SDL code describing this tag</returns> string ToString(String linePrefix) { StringBuilder sb = new StringBuilder(); sb.Append(linePrefix); bool skipValueSpace = false; if (sdlNamespace.Length == 0 && name.Equals("content")) { skipValueSpace = true; } else { if (sdlNamespace.Length != 0) { sb.Append(sdlNamespace).Append(':'); } sb.Append(name); } // output values if (values.Count != 0) { if (skipValueSpace == true) { skipValueSpace = false; } else { sb.Append(" "); } int size = values.Count; for (int i = 0; i < size; i++) { sb.Append(SDLUtil.Format(values[i])); if (i < size - 1) { sb.Append(" "); } } } // output attributes if (attributes.Count != 0) { foreach (string key in attributes.Keys) { sb.Append(" "); string attNamespace = AttributeToNamespace[key]; if (!attNamespace.Equals("")) { sb.Append(attNamespace + ":"); } sb.Append(key + "="); sb.Append(SDLUtil.Format(attributes[key])); } } // output children if (children.Count != 0) { sb.Append(" {\r\n"); foreach (Tag t in children) { sb.Append(t.ToString(linePrefix + " ") + "\r\n"); } sb.Append(linePrefix + "}"); } return(sb.ToString()); }
/// <summary> /// Add a value to this tag /// </summary> /// <param name="value">The value to add</param> public void AddValue(object value) { valuesDirty = true; values.Add(SDLUtil.CoerceOrFail(value)); }