Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.4/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers/UninitializedMessageException.cs

Last change on this file was 3857, checked in by abeham, 14 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 5.6 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections;
37using System.Collections.Generic;
38using System.Text;
39using Google.ProtocolBuffers.Collections;
40using Google.ProtocolBuffers.Descriptors;
41
42namespace Google.ProtocolBuffers {
43  /// <summary>
44  /// TODO(jonskeet): Write summary text.
45  /// </summary>
46  public sealed class UninitializedMessageException : Exception {
47
48    private readonly IList<string> missingFields;
49
50    public UninitializedMessageException(IMessage message)
51        : this(FindMissingFields(message)) {
52    }
53
54    private UninitializedMessageException(IList<string> missingFields)
55        : base(BuildDescription(missingFields)) {
56      this.missingFields = Lists.AsReadOnly(missingFields);
57    }
58
59   
60    /// <summary>
61    /// Returns a read-only list of human-readable names of
62    /// required fields missing from this message. Each name
63    /// is a full path to a field, e.g. "foo.bar[5].baz"
64    /// </summary>
65    public IList<string> MissingFields {
66      get { return missingFields; }
67    }
68
69    /// <summary>
70    /// Converts this exception into an InvalidProtocolBufferException.
71    /// When a parsed message is missing required fields, this should be thrown
72    /// instead of UninitializedMessageException.
73    /// </summary>
74    public InvalidProtocolBufferException AsInvalidProtocolBufferException() {
75      return new InvalidProtocolBufferException(Message);
76    }
77
78    /// <summary>
79    /// Constructs the description string for a given list of missing fields.
80    /// </summary>
81    private static string BuildDescription(IEnumerable<string> missingFields) {
82      StringBuilder description = new StringBuilder("Message missing required fields: ");
83      bool first = true;
84      foreach(string field in missingFields) {
85        if (first) {
86          first = false;
87        } else {
88          description.Append(", ");
89        }
90        description.Append(field);
91      }
92      return description.ToString();
93    }
94
95    /// <summary>
96    /// Returns a list of the full "paths" of missing required
97    /// fields in the specified message.
98    /// </summary>
99    private static IList<String> FindMissingFields(IMessage message) {
100      List<String> results = new List<String>();
101      FindMissingFields(message, "", results);
102      return results;
103    }
104
105    /// <summary>
106    /// Recursive helper implementing FindMissingFields.
107    /// </summary>
108    private static void FindMissingFields(IMessage message, String prefix, List<String> results) {
109      foreach (FieldDescriptor field in message.DescriptorForType.Fields) {
110        if (field.IsRequired && !message.HasField(field)) {
111          results.Add(prefix + field.Name);
112        }
113      }
114
115      foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields) {
116        FieldDescriptor field = entry.Key;
117        object value = entry.Value;
118
119        if (field.MappedType == MappedType.Message) {
120          if (field.IsRepeated) {
121            int i = 0;
122            foreach (object element in (IEnumerable) value) {
123              FindMissingFields((IMessage) element, SubMessagePrefix(prefix, field, i++), results);
124            }
125          } else {
126            if (message.HasField(field)) {
127              FindMissingFields((IMessage) value, SubMessagePrefix(prefix, field, -1), results);
128            }
129          }
130        }
131      }
132    }
133
134    private static String SubMessagePrefix(String prefix, FieldDescriptor field, int index) {
135      StringBuilder result = new StringBuilder(prefix);
136      if (field.IsExtension) {
137        result.Append('(')
138              .Append(field.FullName)
139              .Append(')');
140      } else {
141        result.Append(field.Name);
142      }
143      if (index != -1) {
144        result.Append('[')
145              .Append(index)
146              .Append(']');
147      }
148      result.Append('.');
149      return result.ToString();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.