Json.NET
Code Coverage Statistics for Source File

Newtonsoft.Json\Schema\JsonSchemaModelBuilder.cs

Symbol Coverage: 96.88% (62 of 64)

Branch Coverage: 94.87% (37 of 39)

Cyclomatic Complexity Avg: 3.71 Max:12

Code Lines: 60


L V Source
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.Linq;
29
using System.Text;
30

  
31
namespace Newtonsoft.Json.Schema
32
{
33
  internal class JsonSchemaModelBuilder
34
  {
35
 65
    private JsonSchemaNodeCollection _nodes = new JsonSchemaNodeCollection();
36
 65
    private Dictionary<JsonSchemaNode, JsonSchemaModel> _nodeModels = new Dictionary<JsonSchemaNode, JsonSchemaModel>();
37
    private JsonSchemaNode _node ;
38

  
39
    public JsonSchemaModel Build(JsonSchema schema)
40
    {
41
 62
      _nodes = new JsonSchemaNodeCollection();
42
 62
      _node = AddSchema(null, schema);
43

  
44
 62
      _nodeModels = new Dictionary<JsonSchemaNode, JsonSchemaModel>();
45
 62
      JsonSchemaModel model = BuildNodeModel(_node);
46

  
47
 62
      return model;
48
 62
    }
49

  
50
    public JsonSchemaNode AddSchema(JsonSchemaNode existingNode, JsonSchema schema)
51
    {
52
      string newId;
53
 208
      if (existingNode != null)
54
      {
55
 23
        if (existingNode.Schemas.Contains(schema))
56
 0
          return existingNode;
57

  
58
 23
        newId = JsonSchemaNode.GetId(existingNode.Schemas.Union(new[] { schema }));
59
      }
60
      else
61
      {
62
 185
        newId = JsonSchemaNode.GetId(new[] { schema });
63
      }
64

  
65
 208
      if (_nodes.Contains(newId))
66
 8
        return _nodes[newId];
67

  
68
 200
      JsonSchemaNode currentNode = (existingNode != null)
69
 200
        ? existingNode.Combine(schema)
70
 200
        : new JsonSchemaNode(schema);
71

  
72
 200
      _nodes.Add(currentNode);
73

  
74
 200
      if (schema.Properties != null)
75
      {
76
 40
        foreach (KeyValuePair<string, JsonSchema> property in schema.Properties)
77
        {
78
 103
          AddProperty(currentNode, property.Key, property.Value);
79
        }
80
      }
81

  
82
 200
      if (schema.Items != null)
83
      {
84
 21
        for (int i = 0; i < schema.Items.Count; i++)
85
        {
86
 22
          AddItem(currentNode, i, schema.Items[i]);
87
        }
88
      }
89

  
90
 200
      if (schema.AdditionalProperties != null)
91
 10
        AddAdditionalProperties(currentNode, schema.AdditionalProperties);
92

  
93
 200
      if (schema.Extends != null)
94
 9
        currentNode = AddSchema(currentNode, schema.Extends);
95

  
96
 200
      return currentNode;
97
 208
    }
98

  
99
    public void AddProperty(JsonSchemaNode parentNode, string propertyName, JsonSchema schema)
100
    {
101
      JsonSchemaNode propertyNode;
102
 103
      parentNode.Properties.TryGetValue(propertyName, out propertyNode);
103

  
104
 103
      parentNode.Properties[propertyName] = AddSchema(propertyNode, schema);
105
 103
    }
106

  
107
    public void AddItem(JsonSchemaNode parentNode, int index, JsonSchema schema)
108
    {
109
 22
      JsonSchemaNode existingItemNode = (parentNode.Items.Count > index)
110
 22
                                  ? parentNode.Items[index]
111
 22
                                  : null;
112

  
113
 22
      JsonSchemaNode newItemNode = AddSchema(existingItemNode, schema);
114
      
115
 22
      if (!(parentNode.Items.Count > index))
116
      {
117
 22
        parentNode.Items.Add(newItemNode);
118
      }
119
      else
120
      {
121
 0
        parentNode.Items[index] = newItemNode;
122
      }
123
 22
    }
124

  
125
    public void AddAdditionalProperties(JsonSchemaNode parentNode, JsonSchema schema)
126
    {
127
 10
      parentNode.AdditionalProperties = AddSchema(parentNode.AdditionalProperties, schema);
128
 10
    }
129

  
130
    private JsonSchemaModel BuildNodeModel(JsonSchemaNode node)
131
    {
132
      JsonSchemaModel model;
133
 177
      if (_nodeModels.TryGetValue(node, out model))
134
 7
        return model;
135
      
136
 170
      model = JsonSchemaModel.Create(node.Schemas);
137
 170
      _nodeModels[node] = model;
138

  
139
 87
      foreach (KeyValuePair<string, JsonSchemaNode> property in node.Properties)
140
      {
141
 87
        if (model.Properties == null)
142
 26
          model.Properties = new Dictionary<string, JsonSchemaModel>();
143

  
144
 87
        model.Properties[property.Key] = BuildNodeModel(property.Value);
145
      }
146
 21
      for (int i = 0; i < node.Items.Count; i++)
147
      {
148
 21
        if (model.Items == null)
149
 20
          model.Items = new List<JsonSchemaModel>();
150

  
151
 21
        model.Items.Add(BuildNodeModel(node.Items[i]));
152
      }
153
 170
      if (node.AdditionalProperties != null)
154
 7
        model.AdditionalProperties = BuildNodeModel(node.AdditionalProperties);
155

  
156
 170
      return model;
157
 177
    }
158
  }
159
}