示例#1
0
文件: Schema.cs 项目: grarup/SharpE
 public Schema(byte[] data, SchemaManager schemaManager)
 {
     m_schemaManager = schemaManager;
       m_jsonNode = (JsonNode)JsonObject.Load(data);
       m_root = new SchemaObject(m_jsonNode, this);
       m_name = m_root.Id;
 }
 public SelectionCompletionDataViewModel(AutoCompleteValue content, JsonEditorViewModel jsonEditorViewModel)
 {
     m_content = content.Value;
       m_jsonEditorViewModel = jsonEditorViewModel;
       m_text = content.Value.ToString();
       m_schemaObject = content.Value as SchemaObject;
       if (m_schemaObject != null)
     m_description = m_schemaObject.Description;
 }
        public List<AutoCompleteValue> GenerateAutoComplete(bool isInkey, SchemaObject schemaObject, string text, int offset, string relativeStartPath)
        {
            List<AutoCompleteValue> autocompletList = new List<AutoCompleteValue>();
              if (schemaObject == null)
            return autocompletList;

              if (isInkey)
              {
            if (schemaObject.Properties != null)
              foreach (KeyValuePair<string, SchemaObject> keyValuePair in schemaObject.Properties)
            autocompletList.Add(new AutoCompleteValue(AutocompleteType.String, keyValuePair.Value, schemaObject));
              }
              else
              {

            if (schemaObject.SchemaAutoCompletType != SchemaAutoCompletType.Undefined)
            {
              switch (schemaObject.SchemaAutoCompletType)
              {
            case SchemaAutoCompletType.FileRelative:
              {
                string value;
                int endQuatPosition = text.Length - schemaObject.Suffix.Length;
                if (endQuatPosition != offset)
                {
                  List<char> startChars = new List<char> { schemaObject.AutoCompletePathSeperator };
                  int indexPathEnd = offset == 0 ? 0 : text.LastIndexOfAny(startChars.ToArray(), Math.Min(offset - 1, text.Length - 1)) + 1;
                  if (indexPathEnd < 0)
                    indexPathEnd = 0;
                  int indexPathStart = schemaObject.Prefix.Length;
                  if (indexPathStart < indexPathEnd)
                    value = schemaObject.Prefix + text.Substring(indexPathStart, indexPathEnd - indexPathStart) +
                            schemaObject.Suffix;
                  else
                    value = schemaObject.Prefix + schemaObject.Suffix;
                }
                else
                  value = text;
                value = schemaObject.RemovePrefixAndSuffix(value);
                string filePath = Path.GetDirectoryName(relativeStartPath) + "\\" + value.Replace(schemaObject.AutoCompletePathSeperator, '\\');
                if (File.Exists(filePath) || !Directory.Exists(filePath))
                  return autocompletList;
                autocompletList.Add(new AutoCompleteValue(AutocompleteType.File, ".." + schemaObject.AutoCompletePathSeperator, schemaObject));
                foreach (string directory in Directory.GetDirectories(filePath))
                  autocompletList.Add(new AutoCompleteValue(AutocompleteType.File,
                                                              Path.GetFileName(directory) + schemaObject.AutoCompletePathSeperator, schemaObject));
                foreach (string file in Directory.GetFiles(filePath))
                {
                  if (schemaObject.AutoCompleteFilter == null || schemaObject.AutoCompleteFilter.IsMatch(file))
                    autocompletList.Add(new AutoCompleteValue(AutocompleteType.File, Path.GetFileName(file),
                                                                schemaObject));
                }
              }
              break;
            case SchemaAutoCompletType.FileAbsolute:
              {
                string value;
                int endQuatPosition = text.Length - schemaObject.Suffix.Length;
                if (endQuatPosition != offset)
                {
                  List<char> startChars = new List<char> { schemaObject.AutoCompletePathSeperator, };
                  if (schemaObject.Prefix.Length > 0)
                    startChars.Add(schemaObject.Prefix.Last());
                  int indexPathStart = offset == 0 ? 0 : (text.LastIndexOfAny(startChars.ToArray(), Math.Min(offset - 1, text.Length - 1)) + 1);
                  int indexQuatStrat = schemaObject.Prefix.Length;
                  if (indexQuatStrat < indexPathStart)
                    value = schemaObject.Prefix + text.Substring(indexQuatStrat, indexPathStart - indexQuatStrat) +
                            schemaObject.Suffix;
                  else
                    value = schemaObject.Prefix + schemaObject.Suffix;
                }
                else
                  value = text;
                if (value.Length >= schemaObject.Prefix.Length)
                  value = value.Substring(schemaObject.Prefix.Length,
                                          value.Length - schemaObject.Prefix.Length - schemaObject.Suffix.Length);
                string filePath = value.Replace(schemaObject.AutoCompletePathSeperator, '\\');
                if (File.Exists(filePath) || !Directory.Exists(filePath))
                  return autocompletList;
                autocompletList.Add(new AutoCompleteValue(AutocompleteType.File, ".." + schemaObject.AutoCompletePathSeperator, schemaObject));
                foreach (string directory in Directory.GetDirectories(filePath))
                  autocompletList.Add(new AutoCompleteValue(AutocompleteType.File,
                                                              Path.GetFileName(directory) + schemaObject.AutoCompletePathSeperator, schemaObject));
                foreach (string file in Directory.GetFiles(filePath))
                {
                  if (schemaObject.AutoCompleteFilter == null || schemaObject.AutoCompleteFilter.IsMatch(file))
                    autocompletList.Add(new AutoCompleteValue(AutocompleteType.File, Path.GetFileName(file),
                                                                schemaObject));
                }
              }
              break;
            case SchemaAutoCompletType.Key:
              {
                if (schemaObject.AutoCompleteSourceKey == null)
                  break;
                foreach (string autoCompleteValue in GetAutoCompleteValues(schemaObject.AutoCompleteSourceKey))
                  autocompletList.Add(new AutoCompleteValue(AutocompleteType.String, autoCompleteValue, schemaObject));
              }
              break;
              }
            }
            else
            {
              switch (schemaObject.Type)
              {
            case SchemaDataType.Undefined:
              List<SchemaObject> posibilities = schemaObject.GetPosibilties();
              foreach (SchemaObject posibility in posibilities)
              {
                autocompletList.Add(new AutoCompleteValue(AutocompleteType.Selector, posibility, schemaObject));
              }
              break;
            case SchemaDataType.String:
              if (schemaObject.Enums != null)
              {
                foreach (object obj in schemaObject.Enums)
                  autocompletList.Add(new AutoCompleteValue(AutocompleteType.String, obj, schemaObject));
              }
              break;
            case SchemaDataType.Boolean:
              autocompletList.Add(new AutoCompleteValue(AutocompleteType.String, "true", schemaObject));
              autocompletList.Add(new AutoCompleteValue(AutocompleteType.String, "false", schemaObject));
              break;
              }
            }
              }
              return autocompletList;
        }
示例#4
0
        private void Load()
        {
            lock (this)
              {
            if (m_isLoaded)
              return;
            JsonNode jsonNode = m_jsonObject as JsonNode;
            if (jsonNode != null)
            {
              string refPath = jsonNode.GetObjectOrDefault<string>("$ref", null);
              if (refPath != null)
              {
            m_ref = m_schema.GetSchemaObjectByRef(refPath);
            return;
              }
              m_id = jsonNode.GetObjectOrDefault("id", "undefined");
              m_type = jsonNode.GetObjectOrDefault("type", SchemaDataType.Undefined);
              JsonNode definitions = jsonNode.GetObjectOrDefault<JsonNode>("definitions", null);
              InitDictionary(out m_definitions, definitions);
              JsonNode properties = jsonNode.GetObjectOrDefault<JsonNode>("properties", null);
              InitDictionary(out m_properties, properties);
              if (m_properties != null && m_type == SchemaDataType.Undefined)
            m_type = SchemaDataType.Object;

              JsonObject items = jsonNode.GetObjectOrDefault<JsonObject>("items", null);
              InitArray(out m_items, items);
              JsonArray enums = jsonNode.GetObjectOrDefault<JsonArray>("enum", null);
              if (enums != null)
              {
            m_enums = new List<object>();
            foreach (object obj in enums)
              m_enums.Add(obj);
              }
              JsonNode additionalProperties = jsonNode.GetObjectOrDefault<JsonNode>("additionalProperties", null);
              if (additionalProperties != null)
              {
            m_additionalProperties = new SchemaObject(additionalProperties, m_schema);
              }
              JsonArray allOf = jsonNode.GetObjectOrDefault<JsonObject>("allOf", null) as JsonArray;
              InitArray(out m_allOf, allOf);
              m_anyOfArray = jsonNode.GetObjectOrDefault<JsonObject>("anyOf", null) as JsonArray;
              InitArray(out m_anyOf, m_anyOfArray);
              m_oneOfArray = jsonNode.GetObjectOrDefault<JsonObject>("oneOf", null) as JsonArray;
              InitArray(out m_oneOf, m_oneOfArray);
              m_description = jsonNode.GetObjectOrDefault("description", default(string));
              JsonNode autoCompleteNode = jsonNode.GetObjectOrDefault<JsonNode>("AutoComplet", null);
              if (autoCompleteNode != null)
              {
            m_schemaAutoCompletType = autoCompleteNode.GetObjectOrDefault("type", SchemaAutoCompletType.Undefined);
            m_prefix = autoCompleteNode.GetObjectOrDefault("prefix", "");
            m_suffix = autoCompleteNode.GetObjectOrDefault("suffix", "");
            string filter = autoCompleteNode.GetObjectOrDefault<string>("filter", null);
            if (filter != null)
              m_autoCompleteFilter = new Regex(filter);
            m_autoCompleteTargetKey = autoCompleteNode.GetObjectOrDefault<string>("targetkey", null);
            m_autoCompleteSourceKey = autoCompleteNode.GetObjectOrDefault<string>("sourcekey", null);
            m_autoCompletePathSeperator = autoCompleteNode.GetObjectOrDefault("pathseperator", "\\")[0];
              }
              object mulipleOfObject = jsonNode.GetObjectOrDefault<object>("multipleOf", null);
              if (mulipleOfObject != null)
              {
            if (mulipleOfObject is int)
              m_multipleOf = (int)mulipleOfObject;
            else if (mulipleOfObject is double)
              m_multipleOf = (double)mulipleOfObject;
              }
              JsonNode patternPropertiesNode = jsonNode.GetObjectOrDefault<object>("patternProperties", null) as JsonNode;
              Dictionary<string, SchemaObject> patternProperties;
              InitDictionary(out patternProperties, patternPropertiesNode);
              if (patternProperties != null)
              {
            m_patternProperties = new Dictionary<Regex, SchemaObject>();
            foreach (KeyValuePair<string, SchemaObject> patternProperty in patternProperties)
              m_patternProperties.Add(new Regex(patternProperty.Key), patternProperty.Value);
              }
              if (m_type == SchemaDataType.Undefined)
              {
            if (jsonNode.ContainsKey("minLength") || jsonNode.ContainsKey("maxLength") || jsonNode.ContainsKey("pattern"))
              m_type = SchemaDataType.String;
            if (jsonNode.ContainsKey("minimum") || jsonNode.ContainsKey("maximun") || jsonNode.ContainsKey("exclusiveMinimum") || jsonNode.ContainsKey("exclusiveMaximum") || jsonNode.ContainsKey("multipleOf"))
              m_type = SchemaDataType.Number;
            if (jsonNode.ContainsKey("minItems") || jsonNode.ContainsKey("maxItems") || jsonNode.ContainsKey("uniqueItems"))
              m_type = SchemaDataType.Array;
            if (jsonNode.ContainsKey("minProperties") || jsonNode.ContainsKey("maxProperties") || jsonNode.ContainsKey("required"))
              m_type = SchemaDataType.Object;

              }

              switch (m_type)
              {
            case SchemaDataType.String:
              m_min = jsonNode.GetObjectOrDefault("minLength", 0);
              m_max = jsonNode.GetObjectOrDefault("maxLength", int.MaxValue);
              string pattern = jsonNode.GetObjectOrDefault<string>("pattern", null);
              if (pattern != null)
                m_pattern = new Regex(pattern);
              break;
            case SchemaDataType.Integer:
              m_min = jsonNode.GetObjectOrDefault("minimum", int.MinValue);
              m_max = jsonNode.GetObjectOrDefault("maximum", int.MaxValue);
              m_exclusiveMin = jsonNode.GetObjectOrDefault("exclusiveMinimum", false);
              m_exclusiveMax = jsonNode.GetObjectOrDefault("exclusiveMaximum", false);
              break;
            case SchemaDataType.Number:
              object minJsonValue = jsonNode.GetObjectOrDefault<object>("minimum", null);
              if (minJsonValue != null)
              {
                if (minJsonValue is int)
                  m_min = (int)minJsonValue;
                else if (minJsonValue is double)
                  m_min = (double)minJsonValue;
                else
                  m_min = double.MinValue;
              }
              else
              {
                m_min = double.MinValue;
              }
              object maxJsonValue = jsonNode.GetObjectOrDefault<object>("maximum", null);
              if (maxJsonValue != null)
              {
                if (maxJsonValue is int)
                  m_max = (int)maxJsonValue;
                else if (maxJsonValue is double)
                  m_max = (double)maxJsonValue;
                else
                  m_max = double.MaxValue;
              }
              else
              {
                m_max = double.MaxValue;
              }
              m_exclusiveMin = jsonNode.GetObjectOrDefault("exclusiveMinimum", false);
              m_exclusiveMax = jsonNode.GetObjectOrDefault("exclusiveMaximum", false);
              break;
            case SchemaDataType.Array:
              m_min = jsonNode.GetObjectOrDefault("minItems", 0);
              m_max = jsonNode.GetObjectOrDefault("maxItems", int.MaxValue);
              m_uniqueItems = jsonNode.GetObjectOrDefault("uniqueItems", false);
              break;
            case SchemaDataType.Object:
              m_min = jsonNode.GetObjectOrDefault("minProperties", 0);
              m_max = jsonNode.GetObjectOrDefault("maxProperties", int.MaxValue);
              JsonArray jsonArray = jsonNode.GetObjectOrDefault<JsonArray>("required", null);
              if (jsonArray != null)
              {
                m_requiredProperties = new List<string>();
                foreach (JsonValue jsonValue in jsonArray)
                  m_requiredProperties.Add((string)jsonValue.Value);
              }
              break;
            case SchemaDataType.Undefined:
              break;
              }
            }
            m_isLoaded = true;
              }
        }
示例#5
0
 private void InitDictionary(out Dictionary<string, SchemaObject> list, IEnumerable<JsonElement> node)
 {
     if (node == null)
       {
     list = null;
     return;
       }
       list = new Dictionary<string, SchemaObject>();
       foreach (JsonElement jsonElement in node)
       {
     SchemaObject schemaObject = new SchemaObject(jsonElement, m_schema);
     list.Add(schemaObject.Name, schemaObject);
       }
 }
示例#6
0
 public AutoCompleteValue(AutocompleteType type, object value, SchemaObject schemaObject)
 {
     m_type = type;
       m_value = value;
       m_schemaObject = schemaObject;
 }
示例#7
-1
文件: Schema.cs 项目: grarup/SharpE
 public Schema(string path, SchemaManager schemaManager)
 {
     m_path = path;
       m_schemaManager = schemaManager;
       m_jsonNode = (JsonNode)JsonObject.Load(path);
       m_name = System.IO.Path.GetFileName(path);
       m_root = new SchemaObject(m_jsonNode, this);
 }