Example #1
0
        /// <summary>
        /// Creates a list of PropertyEditorManifest from the file contents of each manifest file
        /// </summary>
        /// <param name="manifestFileContents"></param>
        /// <returns></returns>
        /// <remarks>
        /// This ensures that comments are removed (but they have to be /* */ style comments
        /// and ensures that virtual paths are replaced with real ones
        /// </remarks>
        internal static IEnumerable <PackageManifest> CreateManifests(params string[] manifestFileContents)
        {
            var result = new List <PackageManifest>();

            foreach (var m in manifestFileContents)
            {
                var manifestContent = m;

                if (manifestContent.IsNullOrWhiteSpace())
                {
                    continue;
                }

                // Strip byte object marker, JSON.NET does not like it
                var preamble = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());

                // Strangely StartsWith(preamble) would always return true
                if (manifestContent.Substring(0, 1) == preamble)
                {
                    manifestContent = manifestContent.Remove(0, preamble.Length);
                }

                if (manifestContent.IsNullOrWhiteSpace())
                {
                    continue;
                }

                //remove any comments first
                var replaced = CommentsSurround.Replace(manifestContent, match => " ");
                replaced = CommentsLine.Replace(replaced, match => "");

                JObject deserialized;
                try
                {
                    deserialized = JsonConvert.DeserializeObject <JObject>(replaced);
                }
                catch (Exception ex)
                {
                    LogHelper.Error <ManifestParser>("An error occurred parsing manifest with contents: " + manifestContent, ex);
                    continue;
                }

                //validate the javascript
                var init = deserialized.Properties().Where(x => x.Name == "javascript").ToArray();
                if (init.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'javascript' element");
                }

                //validate the css
                var cssinit = deserialized.Properties().Where(x => x.Name == "css").ToArray();
                if (cssinit.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'css' element");
                }

                //validate the property editors section
                var propEditors = deserialized.Properties().Where(x => x.Name == "propertyEditors").ToArray();
                if (propEditors.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'propertyEditors' element");
                }

                //validate the parameterEditors section
                var paramEditors = deserialized.Properties().Where(x => x.Name == "parameterEditors").ToArray();
                if (paramEditors.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'parameterEditors' element");
                }

                //validate the gridEditors section
                var gridEditors = deserialized.Properties().Where(x => x.Name == "gridEditors").ToArray();
                if (gridEditors.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'gridEditors' element");
                }

                var jConfig = init.Any() ? (JArray)deserialized["javascript"] : new JArray();
                ReplaceVirtualPaths(jConfig);

                var cssConfig = cssinit.Any() ? (JArray)deserialized["css"] : new JArray();
                ReplaceVirtualPaths(cssConfig);

                //replace virtual paths for each property editor
                if (deserialized["propertyEditors"] != null)
                {
                    foreach (JObject p in deserialized["propertyEditors"])
                    {
                        if (p["editor"] != null)
                        {
                            ReplaceVirtualPaths((JObject)p["editor"]);
                        }
                        if (p["preValues"] != null)
                        {
                            ReplaceVirtualPaths((JObject)p["preValues"]);
                        }
                    }
                }

                //replace virtual paths for each property editor
                if (deserialized["gridEditors"] != null)
                {
                    foreach (JObject p in deserialized["gridEditors"])
                    {
                        if (p["view"] != null)
                        {
                            ReplaceVirtualPaths(p["view"]);
                        }
                        if (p["render"] != null)
                        {
                            ReplaceVirtualPaths(p["render"]);
                        }
                    }
                }

                var manifest = new PackageManifest()
                {
                    JavaScriptInitialize = jConfig,
                    StylesheetInitialize = cssConfig,
                    PropertyEditors      = propEditors.Any() ? (JArray)deserialized["propertyEditors"] : new JArray(),
                    ParameterEditors     = paramEditors.Any() ? (JArray)deserialized["parameterEditors"] : new JArray(),
                    GridEditors          = gridEditors.Any() ? (JArray)deserialized["gridEditors"] : new JArray()
                };
                result.Add(manifest);
            }
            return(result);
        }
        /// <summary>
        /// Creates a list of PropertyEditorManifest from the file contents of each manifest file
        /// </summary>
        /// <param name="manifestFileContents"></param>
        /// <returns></returns>
        /// <remarks>
        /// This ensures that comments are removed (but they have to be /* */ style comments
        /// and ensures that virtual paths are replaced with real ones
        /// </remarks>
        internal static IEnumerable<PackageManifest> CreateManifests(params string[] manifestFileContents)
        {
            var result = new List<PackageManifest>();
            foreach (var m in manifestFileContents)
            {
                if (m.IsNullOrWhiteSpace()) continue;

                //remove any comments first
                var replaced = CommentsSurround.Replace(m, match => " ");
                replaced = CommentsLine.Replace(replaced, match => "");

                JObject deserialized;
                try
                {
                    deserialized = JsonConvert.DeserializeObject<JObject>(replaced);
                }
                catch (Exception ex)
                {
                    LogHelper.Error<ManifestParser>("An error occurred parsing manifest with contents: " + m, ex);
                    continue;
                }

                //validate the javascript
                var init = deserialized.Properties().Where(x => x.Name == "javascript").ToArray();
                if (init.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'javascript' element");
                }

                //validate the css
                var cssinit = deserialized.Properties().Where(x => x.Name == "css").ToArray();
                if (cssinit.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'css' element");
                }

                //validate the property editors section
                var propEditors = deserialized.Properties().Where(x => x.Name == "propertyEditors").ToArray();
                if (propEditors.Length > 1)
                {
                    throw new FormatException("The manifest is not formatted correctly contains more than one 'propertyEditors' element");
                }

                var jConfig = init.Any() ? (JArray)deserialized["javascript"] : new JArray();
                ReplaceVirtualPaths(jConfig);

                var cssConfig = cssinit.Any() ? (JArray)deserialized["css"] : new JArray();
                ReplaceVirtualPaths(cssConfig);

                //replace virtual paths for each property editor
                if (deserialized["propertyEditors"] != null)
                {
                    foreach (JObject p in deserialized["propertyEditors"])
                    {
                        if (p["editor"] != null)
                        {
                            ReplaceVirtualPaths((JObject) p["editor"]);
                        }
                        if (p["preValues"] != null)
                        {
                            ReplaceVirtualPaths((JObject)p["preValues"]);
                        }
                    }
                }
                
                var manifest = new PackageManifest()
                    {
                        JavaScriptInitialize = jConfig,
                        StylesheetInitialize = cssConfig,
                        PropertyEditors = propEditors.Any() ? (JArray)deserialized["propertyEditors"] : new JArray(),
                        ParameterEditors = propEditors.Any() ? (JArray)deserialized["parameterEditors"] : new JArray()
                    };
                result.Add(manifest);
            }
            return result;
        }