Thursday, May 26, 2005

HtmlListToArrayList (C#)

This function returns an ArrayList when you supply a string containing a list as the parameter.

string text = "<ul><li class='foo' id='bar'>Item 1</li><li></li><li>Item 2</li><li>Item 3</li></ul>";
ArrayList ar;
ar = HtmlListToArrayList(text);
Response.Write(ar[1]); // returns 'Item 2'
public System.Collections.ArrayList HtmlListToArrayList(string input)
{
 string tag = @"</?(li)(\s+\w+=(\w+|""[^""]*""|'[^']*'))*\s*?/?>";
 string pat = tag + "([^<]+)";
 System.Collections.ArrayList ar = new System.Collections.ArrayList();
 // Compile the regular expression.
 System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(pat, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
 foreach(System.Text.RegularExpressions.Match m in r.Matches(input))
 {
  ar.Add(m.Groups[4].Value);
 }
 return ar;
}

Tags: ,

Monday, May 23, 2005

Object Detection (JavaScript)

Object detection is a better way of writing JavaScript as the DOM cannot be faked like the user agent. Scripting Methods has a useful reference of DOM objects and what browser they are implemented in. It is also means forwards compatibility is less of an issue.

So for instance, to get an element on a page (works in IE4+ and most modern browsers) :

var myelement;
if (document.getElementById) {
  myelement = document.getElementById("myelement");
} else if (document.all) {
  myelement = document.all["myelement"];
}
if (myelement) {
  // do something
}

Tags: , ,

Monday, May 16, 2005

Strip Tags (VBScript)

The following function strips tags from the supplied input string. Set 'tags' argument to blank string - "" - to strip all tags. Use:

' remove spans
input = StripTags(input,"span")
' remove b and u
input = StripTags(input,"b|u")
' remove all tags
input = StripTags(input,"")
Function StripTags(input,tags)
 ' set 'tags' to empty string to strip all tags
 If tags = "" Then tags = "[a-zA-Z]+"
 Dim regEx : Set regEx = New RegExp
 regEx.IgnoreCase = True
 regEx.Global = True
 ' tag to remove (based on http://regexplib.com/REDetails.aspx?regexp_id=211)
 regEx.Pattern = "</?("+tags+")(\s+\w+=(\w+|""[^""]*""|'[^']*'))*\s*?/?>"
 StripTags = regEx.Replace(input, "")
End Function

Edit (19 May 2005): Fix to regular expression to match multiple spaces before > , i.e. <p  >


Tags: , ,

Thursday, May 12, 2005

Update to UrlQuery Class (C#)

Update to UrlQuery Class for easier working with Url's and QueryStrings in ASP.NET. Thanks to Levi Rosol (UrlQueryString) for the code. Previous versions: Url Manipulation (C#) - Jul 22nd, 2004, Url Manipulation v2 (C#) - Sep 22nd, 2004.

You can now add a NameValueCollection to the QueryString. Replaces existing items when one with the same key exists in the collection.

For redirecting to another page, including current page QueryString, supply the page location in the constructor:

// current page is page.aspx?query=1
UrlQuery NewPage = new UrlQuery("newpage.aspx");
// redirect to newpage.aspx?query=1
Response.Redirect(NewPage.AbsoluteUri);

More examples:

UrlQuery MyPage = new UrlQuery("/path/to/file.aspx"); // parameters on the current page are added automatically
// or to get the current page: MyQuery = new UrlQuery();
// add parameter
MyPage["myparam"] = "myval1";
// add another parameter (or replace existing one)
MyPage["myparam2"] = "myval2";
Trace.Write(MyPage["myparam2"]); // returns 'myval2'
// remove parameter
MyPage["myparam2"] = null; // or string.Empty
Trace.Write(MyPage["myparam2"]); // returns ''
// create a new collection and add items
NameValueCollection nvc = new NameValueCollection();
nvc.Add("myparam","changed");
nvc.Add("myparam3","myval3");
Trace.Write(MyPage["myparam"]); // returns 'myval1'
MyQuery.Set(nvc);
Trace.Write(MyPage["myparam"]); // returns 'changed'
Trace.Write(MyPage["myparam3"]); // returns 'myval3'
Trace.Write(MyPage.Url); // returns '/path/to/file.aspx'
Trace.Write(MyPage.VirtualFolder); // returns '/path/to/'
Trace.Write(MyPage.AbsoluteUri); // returns '/path/to/file.aspx?myparam=changed&myparam3=myval3'
Trace.Write(MyPage.Get()); // returns '?myparam=changed&myparam3=myval3'
Trace.Write(MyPage.Get().Substring(1)); // returns 'myparam=changed&myparam3=myval3'

UrlQuery Class:

public class UrlQuery
{
 /// <summary>
 /// Base on current page
 /// </summary>
 public UrlQuery()
 {
  this.url = HttpContext.Current.Request.Url.AbsolutePath;
 }
 /// <summary>
 /// Base on other page
 /// </summary>
 /// <param name="value">The url of the page to reference, i.e.: '/path/to/folder/page.aspx?param1=1&amp;param2=2'</param>
 public UrlQuery(string value)
 {
  int q = value.IndexOf('?');
  if (q != -1)
  {
   this.url = value.Substring(0, q);
   this.queryString = NameValueCollection(value);
  }
  else
  {
   this.url = value;
  }
 }
 /// <summary>
 /// Get and set Url parameters
 /// </summary>
 public string this[string param]
 {
  get
  {
   return this.Get(param);
  }
  set
  {
   this.Set(param,value);
  }
 }

 private string url;
 /// <summary>
 /// The Url of the page, without QueryString
 /// </summary>
 /// <value>/path/to/folder/page.aspx</value>
 public string Url
 {
  get
  {
   return this.url;
  }
 }
 /// <summary>
 /// Returns the virtual folder the page is in
 /// </summary>
 /// <value>/path/to/folder/</value>
 public string VirtualFolder
 {
  get
  {
   return this.Url.Substring(0, Url.LastIndexOf("/") + 1);
  }
 }
 /// <summary>
 /// The AbsoluteUri
 /// </summary>
 /// <value>page.aspx?param1=1&amp;param2=2</value>
 public string AbsoluteUri
 {
  get
  {
   return this.Url + this.Get();
  }
 }
 private NameValueCollection queryString;
 /// <summary>
 /// Get the QueryString for the page
 /// </summary>
 public NameValueCollection QueryString
 {
  get
  {
   if (this.queryString != null)
   {
    return this.queryString;
   }
   else
   {
    this.queryString = new NameValueCollection(HttpContext.Current.Request.QueryString);
    return this.queryString;
   }
  }
 }
 /// <summary>
 /// Get the QueryString
 /// </summary>
 /// <returns>String in the format ?param1=1&amp;param2=2</returns>
 public string Get()
 {
  string query = "";
  if (this.QueryString.Count != 0)
  {
   query = "?";
   for (int i = 0; i <= this.QueryString.Count - 1; i++)
   {
    if (i != 0)
    {
     query += "&";
    }
    query += this.QueryString.GetKey(i) + "=" + this.QueryString.Get(i);
   }
  }
  return query;
 }
 /// <summary>
 /// Get parameter from QueryString
 /// </summary>
 /// <param name="param">Parameter to get</param>
 /// <returns>Parameter Value</returns>
 public string Get(string param)
 {
  return this.QueryString[param];
 }
 /// <summary>
 /// Set QueryString parameter
 /// </summary>
 /// <param name="param">Parameter to set</param>
 /// <param name="value">Value of parameter</param>
 public void Set(string param, string value)
 {
  if (param != string.Empty)
  {
   if (value == string.Empty || value == null)
   {
    this.QueryString.Remove(param);
   }
   else
   {
    this.QueryString[param] = value;
   }
  }
 }
 /// <summary>
 /// Use this method to add a NameValueCollection object to the parent object.
 /// Validation is done to ensure the Key does not already exist in the QueryString object.
 /// </summary>
 /// <param name="collection"></param>
 public void Set(NameValueCollection collection)
 {
  foreach(string key in collection.Keys)
  {
   if(this.QueryString[key] != null)
   {
    this.QueryString.Remove(key);
   }
   this.Set(key,collection[key]);
  }
 }
 /// <summary>
 /// Convert QueryString string to NameValueCollection
 /// http://groups.google.co.uk/groups?hl=en&amp;lr=&amp;ie=UTF-8&amp;safe=off&amp;selm=uyMZ2oaZDHA.652%40tk2msftngp13.phx.gbl
 /// </summary>
 public static NameValueCollection NameValueCollection(string qs)
 {
  NameValueCollection nvc = new NameValueCollection();
  //strip string data before the question mark
  qs = qs.IndexOf('?') > 0 ? qs.Remove(0, qs.IndexOf('?') + 1) : qs;
  Array sqarr = qs.Split("&".ToCharArray());
  for (int i = 0; i < sqarr.Length; i++)
  {
   string[] pairs = sqarr.GetValue(i).ToString().Split("=".ToCharArray());
   nvc.Add(pairs[0], pairs[1]);
  }
  return nvc;
 }
 /// <summary>
 /// Copies a form parameter to the QueryString
 /// </summary>
 /// <param name="param">Form Parameter</param>
 public void FormToQuery(string param)
 {

  this.Set(param, HttpContext.Current.Request.Form[param]);
 }
}

Tags: , ,

Tuesday, May 10, 2005

The Portable Freeware Collection

The Portable Freeware Collection. A list of freeware software you can run on removable devices (external hard drives, usb flash drive etc). Definition of portable:

  • Run from any directory
  • Not dependent on registry for operation
  • Not runtime dependent (HTMLHelp, Internet Explorer discluded)

Definition of freeware:

  • Unrestricted distribution
  • Personal use only freeware included (alongside freeware for all uses)
  • May be 'lite' version of commercial product

Tags: ,

Wednesday, May 04, 2005

uQueue - URL Queue

uQueue is a useful utility by Björn Graf that you can add URL's to (for visiting later). Useful when you come across pages with links that you wish to visit. For Firefox, Mozilla and Internet Explorer. Requires Windows XP and MSXML 3.0 or later (you already version 4 if you have IE6, but you can also download it seperately).


Tags: , ,