Files
libremetaverse/libsecondlife/LLSD/NotationLLSD.cs
John Hurliman 3013742668 * Increased SIMULATOR_TIMEOUT to 30 seconds
* Converted all timers to System.Threading timers to fix problems running in services and the CF
* UDPBase now uses our own ReaderWriterLock that is more efficient, and CF compatible
* Login uses a hand-created LoginProxy object instead of dynamically building the class with reflection .Emit()
* Replaced ParameterizedThreadStart calls with class-wide variables for CF compat.
* Removed transfer timeout code (irrelevant now that uploads go through CAPS)
* Added several new Helpers methods to wrap desktop and CF conditional code
* Replaced Monitor calls with AutoResetEvent in BlockingQueue
* InventoryNodeDictionary uses generics now
* Removed final lingering piece of XML serialization
* Added CookComputing.XmlRpc.CF.dll for the CF

git-svn-id: http://libopenmetaverse.googlecode.com/svn/trunk@1479 52acb1d6-8a22-11de-b505-999d5b087335
2007-11-06 09:26:10 +00:00

176 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace libsecondlife.LLSD
{
public static partial class LLSDParser
{
public static object DeserializeNotation(string notationData)
{
int unused;
return ParseNotationElement(notationData, out unused);
}
private static object ParseNotationElement(string notationData, out int endPos)
{
if (notationData.Length == 0)
{
endPos = 0;
return null;
}
// Identify what type of object this is
switch (notationData[0])
{
case '!':
endPos = 1;
return null;
case '1':
endPos = 1;
return true;
case '0':
endPos = 1;
return false;
case 'i':
{
if (notationData.Length < 2)
{
endPos = notationData.Length;
return 0;
}
int value;
endPos = FindEnd(notationData, 1);
if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
return value;
else
return 0;
}
case 'r':
{
if (notationData.Length < 2)
{
endPos = notationData.Length;
return 0d;
}
double value;
endPos = FindEnd(notationData, 1);
if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
return value;
else
return 0d;
}
case 'u':
{
if (notationData.Length < 17)
{
endPos = notationData.Length;
return LLUUID.Zero;
}
LLUUID value;
endPos = FindEnd(notationData, 1);
if (Helpers.TryParse(notationData.Substring(1, endPos - 1), out value))
return value;
else
return LLUUID.Zero;
}
case 'b':
throw new NotImplementedException("Notation binary type is unimplemented");
case 's':
case '"':
case '\'':
if (notationData.Length < 2)
{
endPos = notationData.Length;
return String.Empty;
}
endPos = FindEnd(notationData, 1);
return notationData.Substring(1, endPos - 1).Trim(new char[] { '"', '\'' });
case 'l':
throw new NotImplementedException("Notation URI type is unimplemented");
case 'd':
throw new NotImplementedException("Notation date type is unimplemented");
case '[':
{
if (notationData.IndexOf(']') == -1)
throw new LLSDException("Invalid notation array");
int pos = 0;
List<object> array = new List<object>();
while (notationData[pos] != ']')
{
++pos;
// Advance past comma if need be
if (notationData[pos] == ',') ++pos;
// Allow a single whitespace character
if (pos < notationData.Length && notationData[pos] == ' ') ++pos;
int end;
array.Add(ParseNotationElement(notationData.Substring(pos), out end));
pos += end;
}
endPos = pos + 1;
return array;
}
case '{':
{
if (notationData.IndexOf('}') == -1)
throw new LLSDException("Invalid notation map");
int pos = 0;
Dictionary<string, object> hashtable = new Dictionary<string, object>();
while (notationData[pos] != '}')
{
++pos;
// Advance past comma if need be
if (notationData[pos] == ',') ++pos;
// Allow a single whitespace character
if (pos < notationData.Length && notationData[pos] == ' ') ++pos;
if (notationData[pos] != '\'')
throw new LLSDException("Expected a map key");
int endquote = notationData.IndexOf('\'', pos + 1);
if (endquote == -1 || (endquote + 1) >= notationData.Length || notationData[endquote + 1] != ':')
throw new LLSDException("Invalid map format");
string key = notationData.Substring(pos, endquote - pos);
key = key.Trim(new char[] { '"', '\'' }); //key.Replace("'", String.Empty);
pos += (endquote - pos) + 2;
int end;
hashtable[key] = ParseNotationElement(notationData.Substring(pos), out end);
pos += end;
}
endPos = pos + 1;
return hashtable;
}
default:
throw new LLSDException("Unknown notation value type");
}
}
private static int FindEnd(string llsd, int start)
{
int end = llsd.IndexOfAny(new char[] { ',', ']', '}' });
if (end == -1) end = llsd.Length - 1;
return end;
}
}
}