30 using System.Reflection;
31 using System.Threading;
34 using System.Net.Sockets;
38 using OpenMetaverse.StructuredData;
39 using OpenSim.Framework;
40 using OpenSim.Region.Framework.Interfaces;
41 using OpenSim.Region.Framework.Scenes;
42 using System.Collections.Generic;
43 using System.Text.RegularExpressions;
49 private static readonly ILog m_log =
50 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
52 protected virtual OSD ValueStore {
get; set; }
56 public string Path {
get; set; }
57 public bool UseJson {
get; set; }
72 protected static Regex m_ParsePassOne =
new Regex(
"({[^}]+}|\\[[0-9]+\\]|\\[\\+\\])");
75 protected static Regex m_ParsePassThree =
new Regex(
"(?<!{[^}]*)\\.([a-zA-Z]+)(?=\\.)");
78 protected static Regex m_ParsePassFour =
new Regex(
"\\.+");
81 protected static Regex m_ValidatePath =
new Regex(
"^\\.(({[^}]+}|\\[[0-9]+\\]|\\[\\+\\])\\.)*$");
84 protected static Regex m_PathComponent =
new Regex(
"\\.({[^}]+}|\\[[0-9]+\\]|\\[\\+\\])");
87 protected static Regex m_SimpleArrayPattern =
new Regex(
"^\\[([0-9]+)\\]$");
88 protected static Regex m_ArrayPattern =
new Regex(
"^\\[([0-9]+|\\+)\\]$");
91 protected static Regex m_HashPattern =
new Regex(
"^{([^}]+)}$");
100 public int StringSpace {
get; set; }
110 if (! ParsePathExpression(ipath,out path))
116 opath = PathExpressionToKey(path);
128 m_TakeStore =
new List<TakeValueCallbackClass>();
129 m_ReadStore =
new List<TakeValueCallbackClass>();
137 if (
String.IsNullOrEmpty(value))
138 ValueStore =
new OSDMap();
140 ValueStore = OSDParser.DeserializeJson(value);
151 if (! ParsePathExpression(expr,out path))
152 return JsonStoreNodeType.Undefined;
154 OSD result = ProcessPathExpression(ValueStore,path);
157 return JsonStoreNodeType.Undefined;
160 return JsonStoreNodeType.Object;
163 return JsonStoreNodeType.Array;
165 if (OSDBaseType(result.Type))
166 return JsonStoreNodeType.Value;
168 return JsonStoreNodeType.Undefined;
179 if (! ParsePathExpression(expr,out path))
180 return JsonStoreValueType.Undefined;
182 OSD result = ProcessPathExpression(ValueStore,path);
185 return JsonStoreValueType.Undefined;
188 return JsonStoreValueType.Undefined;
191 return JsonStoreValueType.Undefined;
193 if (result is OSDBoolean)
194 return JsonStoreValueType.Boolean;
196 if (result is OSDInteger)
197 return JsonStoreValueType.Integer;
199 if (result is OSDReal)
200 return JsonStoreValueType.Float;
202 if (result is OSDString)
203 return JsonStoreValueType.String;
205 return JsonStoreValueType.Undefined;
216 if (! ParsePathExpression(expr,out path))
219 OSD result = ProcessPathExpression(ValueStore,path);
220 if (result != null && result.Type == OSDType.Array)
234 public bool GetValue(
string expr, out
string value,
bool useJson)
237 if (! ParsePathExpression(expr,out path))
243 OSD result = ProcessPathExpression(ValueStore,path);
244 return ConvertOutputValue(result,out value,useJson);
255 return SetValueFromExpression(expr,null);
263 public bool SetValue(
string expr,
string value,
bool useJson)
277 ovalue = OSDParser.DeserializeJson(value);
281 if (value.StartsWith(
"'") && value.EndsWith(
"'"))
283 ovalue =
new OSDString(value.Substring(1,value.Length - 2));
293 ovalue =
new OSDString(value);
296 return SetValueFromExpression(expr,ovalue);
307 if (! ParsePathExpression(expr,out path))
310 string pexpr = PathExpressionToKey(path);
312 OSD result = ProcessPathExpression(ValueStore,path);
319 string value = String.Empty;
320 if (! ConvertOutputValue(result,out value,useJson))
327 SetValueFromExpression(expr,null);
341 if (! ParsePathExpression(expr,out path))
344 string pexpr = PathExpressionToKey(path);
346 OSD result = ProcessPathExpression(ValueStore,path);
353 string value = String.Empty;
354 if (! ConvertOutputValue(result,out value,useJson))
374 if (! ParsePathExpression(expr,out path))
386 string pkey = path.Pop();
387 string pexpr = PathExpressionToKey(path);
391 OSD result = ProcessPathExpression(ValueStore,path);
396 MatchCollection amatches = m_ArrayPattern.Matches(pkey,0);
397 if (amatches.Count > 0)
399 if (result.Type != OSDType.Array)
404 Match match = amatches[0];
405 GroupCollection groups = match.Groups;
406 string akey = groups[1].Value;
410 string npkey = String.Format(
"[{0}]",amap.Count);
414 StringSpace += ComputeSizeOf(ovalue);
417 InvokeNextCallback(pexpr + npkey);
422 int aval = Convert.ToInt32(akey);
423 if (0 <= aval && aval < amap.Count)
427 StringSpace -= ComputeSizeOf(amap[aval]);
432 StringSpace -= ComputeSizeOf(amap[aval]);
433 StringSpace += ComputeSizeOf(ovalue);
435 InvokeNextCallback(pexpr + pkey);
444 MatchCollection hmatches = m_HashPattern.Matches(pkey,0);
445 if (hmatches.Count > 0)
447 Match match = hmatches[0];
448 GroupCollection groups = match.Groups;
449 string hkey = groups[1].Value;
454 OSDMap hmap = result as
OSDMap;
457 StringSpace -= ComputeSizeOf(hmap[hkey]);
458 StringSpace += ComputeSizeOf(ovalue);
461 InvokeNextCallback(pexpr + pkey);
466 if (hmap.ContainsKey(hkey))
468 StringSpace -= ComputeSizeOf(hmap[hkey]);
480 m_log.WarnFormat(
"[JsonStore] invalid path expression");
492 List<TakeValueCallbackClass> reads =
497 m_ReadStore.Remove(readcb);
507 m_TakeStore.Remove(takecb);
524 path =
new Stack<string>();
527 expr =
"." + expr +
".";
530 expr = m_ParsePassOne.Replace(expr,
".$1.",-1,0);
533 expr = m_ParsePassThree.Replace(expr,
".{$1}",-1,0);
536 expr = m_ParsePassFour.Replace(expr,
".",-1,0);
539 if (m_ValidatePath.IsMatch(expr))
541 MatchCollection matches = m_PathComponent.Matches(expr,0);
542 foreach (Match match
in matches)
543 path.Push(match.Groups[1].Value);
562 string pkey = path.Pop();
564 OSD rmap = ProcessPathExpression(map,path);
569 MatchCollection amatches = m_SimpleArrayPattern.Matches(pkey,0);
571 if (amatches.Count > 0)
573 if (rmap.Type != OSDType.Array)
575 m_log.WarnFormat(
"[JsonStore] wrong type for key {2}, expecting {0}, got {1}",OSDType.Array,rmap.Type,pkey);
581 Match match = amatches[0];
582 GroupCollection groups = match.Groups;
583 string akey = groups[1].Value;
584 int aval = Convert.ToInt32(akey);
586 if (aval < amap.Count)
587 return (
OSD) amap[aval];
593 MatchCollection hmatches = m_HashPattern.Matches(pkey,0);
595 if (hmatches.Count > 0)
597 if (rmap.Type != OSDType.Map)
599 m_log.WarnFormat(
"[JsonStore] wrong type for key {2}, expecting {0}, got {1}",OSDType.Map,rmap.Type,pkey);
605 Match match = hmatches[0];
606 GroupCollection groups = match.Groups;
607 string hkey = groups[1].Value;
609 if (hmap.ContainsKey(hkey))
610 return (
OSD) hmap[hkey];
616 m_log.WarnFormat(
"[JsonStore] Path type (unknown) does not match the structure");
627 value = String.Empty;
636 if (result.Type == OSDType.Map)
638 value = OSDParser.SerializeJsonString(result as
OSDMap,
true);
643 if (result.Type == OSDType.Array)
645 value = OSDParser.SerializeJsonString(result as
OSDArray,
true);
649 value =
"'" + result.AsString() +
"'";
653 if (OSDBaseType(result.Type))
655 value = result.AsString();
673 foreach (
string k
in path)
674 pkey = (pkey ==
"") ? k : (k +
"." + pkey);
688 if (type == OSDType.Boolean)
690 if (type == OSDType.Integer)
692 if (type == OSDType.Real)
694 if (type == OSDType.String)
696 if (type == OSDType.UUID)
698 if (type == OSDType.Date)
700 if (type == OSDType.URI)
715 if (ConvertOutputValue(value,out sval,
true))
728 private static readonly ILog m_log =
729 LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
731 private Scene m_scene;
732 private UUID m_objectID;
734 protected override OSD ValueStore
745 return sop.DynAttrs.TopLevelMap;
751 m_log.InfoFormat(
"[JsonStore] cannot set top level value in object store");
761 StringSpace = ComputeSizeOf(ValueStore);
static OSD ProcessPathExpression(OSD map, Stack< string > path)
OpenMetaverse.StructuredData.OSDArray OSDArray
static bool ConvertOutputValue(OSD result, out string value, bool useJson)
bool SetValue(string expr, string value, bool useJson)
bool TakeValue(string expr, bool useJson, TakeValueCallback cback)
bool InvokeNextCallback(string pexpr)
JsonStoreValueType GetValueType(string expr)
bool SetValueFromExpression(string expr, OSD ovalue)
OpenMetaverse.StructuredData.OSDMap OSDMap
List< TakeValueCallbackClass > m_ReadStore
static string PathExpressionToKey(Stack< string > path)
int ArrayLength(string expr)
List< TakeValueCallbackClass > m_TakeStore
TakeValueCallback Callback
delegate void TakeValueCallback(string s)
static bool OSDBaseType(OSDType type)
static int ComputeSizeOf(OSD value)
OpenMetaverse.StructuredData.OSD OSD
bool ReadValue(string expr, bool useJson, TakeValueCallback cback)
bool RemoveValue(string expr)
Interactive OpenSim region server
JsonStoreNodeType GetNodeType(string expr)
JsonObjectStore(Scene scene, UUID oid)
static bool CanonicalPathExpression(string ipath, out string opath)
bool GetValue(string expr, out string value, bool useJson)
TakeValueCallbackClass(string spath, bool usejson, TakeValueCallback cback)
static bool ParsePathExpression(string expr, out Stack< string > path)
Parse the path expression and put the components into a stack. We use a stack because we process the ...