|
/**************************************************************************
Gmail Agent API
Copyright (C) 2004 Johnvey Hwang
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**************************************************************************/
using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace Johnvey.GmailAgent
{
/// <summary>
/// A collection of auxilliary tools used to deal with Gmail data.
/// </summary>
public class Utilities
{
/// <summary>
/// Converts a string representation of a JavaScript array to an ArrayList() object.
/// </summary>
/// <param name="input">String containing a JavaScript array.</param>
/// <returns>The converted ArrayList.</returns>
public static ArrayList ParseJSArray(string input) {
int outReturnOffset = 0;
return ParseJSArrayRecurse(input, ref outReturnOffset);
}
/// <summary>
/// Internal recursive sub-function to ParseJSArray
/// </summary>
/// <param name="input">Incoming string.</param>
/// <param name="outReturnOffset">Reference to cursor state tracker</param>
/// <returns></returns>
private static ArrayList ParseJSArrayRecurse(string input, ref int outReturnOffset) {
// instantiate output
ArrayList output = new ArrayList();
// state variables
bool isQuoted = false; // track when we are inside quotes
string dataHold = ""; // temporary data container
char lastCharacter = ' ';
// loop through the entire string
for(int i=1; i < input.Length; i++) {
switch(input[i]) {
case '[': // handle start of array marker
if(!isQuoted) {
// recurse any nested arrays
output.Add(ParseJSArrayRecurse(input.Substring(i), ref outReturnOffset));
// the returning recursive function write out the total length of the characters consumed
i += outReturnOffset;
// assume that the last character is a closing bracket
lastCharacter = ']';
} else {
dataHold += "[";
}
break;
case ']': // handle end of array marker
if(!isQuoted) {
if(dataHold != "") {
output.Add(dataHold);
}
// determine total number of characters consumed (write to reference)
outReturnOffset = i;
return output;
} else {
dataHold += "]";
break;
}
case '"': // toggle quoted state
if(isQuoted) {
isQuoted = false;
} else {
isQuoted = true;
lastCharacter = '"';
}
break;
case ',': // find end of element marker and add to array
if(!isQuoted) {
if(dataHold != "") { // this is to filter out adding extra elements after an empty array
output.Add(dataHold);
dataHold = "";
} else if(lastCharacter == '"') { // this is to catch empty strings
output.Add("");
}
} else {
dataHold += ",";
}
break;
case '\\':
if(i < input.Length - 1 && input[i+1] == '\\') {
// TODO: the right thing to do here is to catch all the escape characters, i.e. \n, \r ... maybe another time.
dataHold += '\\';
lastCharacter = '\\';
}
break;
default: // regular characters are added to the data container
dataHold += input[i];
break;
}
}
return output;
}
/// <summary>
/// Removes HTML tags and decodes HTML entities.
/// </summary>
/// <param name="dirtyHtml">The HTML string to clean.</param>
/// <returns>The cleansed string.</returns>
public static string CleanHtml(string dirtyHtml) {
string output = Regex.Replace(dirtyHtml, "<[^>]+>", "");
output = System.Web.HttpUtility.HtmlDecode(output);
return output;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.