~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/Newtonsoft.Json/Src/Newtonsoft.Json/Linq/JPath.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
// Copyright (c) 2007 James Newton-King
 
3
//
 
4
// Permission is hereby granted, free of charge, to any person
 
5
// obtaining a copy of this software and associated documentation
 
6
// files (the "Software"), to deal in the Software without
 
7
// restriction, including without limitation the rights to use,
 
8
// copy, modify, merge, publish, distribute, sublicense, and/or sell
 
9
// copies of the Software, and to permit persons to whom the
 
10
// Software is furnished to do so, subject to the following
 
11
// conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be
 
14
// included in all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
17
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
18
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
19
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
20
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
21
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
23
// OTHER DEALINGS IN THE SOFTWARE.
 
24
#endregion
 
25
 
 
26
using System;
 
27
using System.Collections.Generic;
 
28
using System.Globalization;
 
29
using Newtonsoft.Json.Utilities;
 
30
 
 
31
namespace Newtonsoft.Json.Linq
 
32
{
 
33
  internal class JPath
 
34
  {
 
35
    private readonly string _expression;
 
36
    public List<object> Parts { get; private set; }
 
37
 
 
38
    private int _currentIndex;
 
39
 
 
40
    public JPath(string expression)
 
41
    {
 
42
      ValidationUtils.ArgumentNotNull(expression, "expression");
 
43
      _expression = expression;
 
44
      Parts = new List<object>();
 
45
 
 
46
      ParseMain();
 
47
    }
 
48
 
 
49
    private void ParseMain()
 
50
    {
 
51
      int currentPartStartIndex = _currentIndex;
 
52
      bool followingIndexer = false;
 
53
 
 
54
      while (_currentIndex < _expression.Length)
 
55
      {
 
56
        char currentChar = _expression[_currentIndex];
 
57
 
 
58
        switch (currentChar)
 
59
        {
 
60
          case '[':
 
61
          case '(':
 
62
            if (_currentIndex > currentPartStartIndex)
 
63
            {
 
64
              string member = _expression.Substring(currentPartStartIndex, _currentIndex - currentPartStartIndex);
 
65
              Parts.Add(member);
 
66
            }
 
67
 
 
68
            ParseIndexer(currentChar);
 
69
            currentPartStartIndex = _currentIndex + 1;
 
70
            followingIndexer = true;
 
71
            break;
 
72
          case ']':
 
73
          case ')':
 
74
            throw new JsonException("Unexpected character while parsing path: " + currentChar);
 
75
          case '.':
 
76
            if (_currentIndex > currentPartStartIndex)
 
77
            {
 
78
              string member = _expression.Substring(currentPartStartIndex, _currentIndex - currentPartStartIndex);
 
79
              Parts.Add(member);
 
80
            }
 
81
            currentPartStartIndex = _currentIndex + 1;
 
82
            followingIndexer = false;
 
83
            break;
 
84
          default:
 
85
            if (followingIndexer)
 
86
              throw new JsonException("Unexpected character following indexer: " + currentChar);
 
87
            break;
 
88
        }
 
89
 
 
90
        _currentIndex++;
 
91
      }
 
92
 
 
93
      if (_currentIndex > currentPartStartIndex)
 
94
      {
 
95
        string member = _expression.Substring(currentPartStartIndex, _currentIndex - currentPartStartIndex);
 
96
        Parts.Add(member);
 
97
      }
 
98
    }
 
99
 
 
100
    private void ParseIndexer(char indexerOpenChar)
 
101
    {
 
102
      _currentIndex++;
 
103
 
 
104
      char indexerCloseChar = (indexerOpenChar == '[') ? ']' : ')';
 
105
      int indexerStart = _currentIndex;
 
106
      int indexerLength = 0;
 
107
      bool indexerClosed = false;
 
108
 
 
109
      while (_currentIndex < _expression.Length)
 
110
      {
 
111
        char currentCharacter = _expression[_currentIndex];
 
112
        if (char.IsDigit(currentCharacter))
 
113
        {
 
114
          indexerLength++;
 
115
        }
 
116
        else if (currentCharacter == indexerCloseChar)
 
117
        {
 
118
          indexerClosed = true;
 
119
          break;
 
120
        }
 
121
        else
 
122
        {
 
123
          throw new JsonException("Unexpected character while parsing path indexer: " + currentCharacter);
 
124
        }
 
125
 
 
126
        _currentIndex++;
 
127
      }
 
128
 
 
129
      if (!indexerClosed)
 
130
        throw new JsonException("Path ended with open indexer. Expected " + indexerCloseChar);
 
131
 
 
132
      if (indexerLength == 0)
 
133
        throw new JsonException("Empty path indexer.");
 
134
 
 
135
      string indexer = _expression.Substring(indexerStart, indexerLength);
 
136
      Parts.Add(Convert.ToInt32(indexer, CultureInfo.InvariantCulture));
 
137
    }
 
138
 
 
139
    internal JToken Evaluate(JToken root, bool errorWhenNoMatch)
 
140
    {
 
141
      JToken current = root;
 
142
 
 
143
      foreach (object part in Parts)
 
144
      {
 
145
        string propertyName = part as string;
 
146
        if (propertyName != null)
 
147
        {
 
148
          JObject o = current as JObject;
 
149
          if (o != null)
 
150
          {
 
151
            current = o[propertyName];
 
152
 
 
153
            if (current == null && errorWhenNoMatch)
 
154
              throw new JsonException("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, propertyName));
 
155
          }
 
156
          else
 
157
          {
 
158
            if (errorWhenNoMatch)
 
159
              throw new JsonException("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, propertyName, current.GetType().Name));
 
160
 
 
161
            return null;
 
162
          }
 
163
        }
 
164
        else
 
165
        {
 
166
          int index = (int) part;
 
167
 
 
168
          JArray a = current as JArray;
 
169
          JConstructor c = current as JConstructor;
 
170
 
 
171
          if (a != null)
 
172
          {
 
173
            if (a.Count <= index)
 
174
            {
 
175
              if (errorWhenNoMatch)
 
176
                throw new IndexOutOfRangeException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index));
 
177
              
 
178
              return null;
 
179
            }
 
180
 
 
181
            current = a[index];
 
182
          }
 
183
          else if (c != null)
 
184
          {
 
185
            if (c.Count <= index)
 
186
            {
 
187
              if (errorWhenNoMatch)
 
188
                throw new IndexOutOfRangeException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index));
 
189
 
 
190
              return null;
 
191
            }
 
192
 
 
193
            current = c[index];
 
194
          }
 
195
          else
 
196
          {
 
197
            if (errorWhenNoMatch)
 
198
              throw new JsonException("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, index, current.GetType().Name));
 
199
 
 
200
            return null;
 
201
          }
 
202
        }
 
203
      }
 
204
 
 
205
      return current;
 
206
    }
 
207
  }
 
208
}
 
 
b'\\ No newline at end of file'