예제 #1
0
        /// <summary>
        /// Process the specified file
        /// </summary>
        public AppletAsset Process(string file, bool optimize)
        {
            try
            {
                XElement xe = XElement.Load(file);

                if (xe.Name.Namespace != PakManTool.XS_HTML)
                {
                    Emit.Message("WARN", "File {0} is not in {1}. Setting namespace", file, PakManTool.XS_HTML);
                    xe.Name = (XNamespace)PakManTool.XS_HTML + xe.Name.LocalName;
                }

                // Optimizing?
                if (optimize)
                {
                    xe.DescendantNodesAndSelf().OfType <XComment>().Where(o => !o.Value.Contains("#include")).Remove();
                }

                // Now we have to iterate throuh and add the asset\
                AppletAssetHtml htmlAsset = null;

                if (xe.Elements().OfType <XElement>().Any(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "widget"))
                {
                    var widgetEle = xe.Elements().OfType <XElement>().FirstOrDefault(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "widget");
                    htmlAsset = new AppletWidget()
                    {
                        Icon        = widgetEle.Element((XNamespace)PakManTool.XS_APPLET + "icon")?.Value,
                        Type        = (AppletWidgetType)Enum.Parse(typeof(AppletWidgetType), widgetEle.Attribute("type")?.Value),
                        Size        = (AppletWidgetSize)Enum.Parse(typeof(AppletWidgetSize), widgetEle.Attribute("size")?.Value ?? "Medium"),
                        View        = (AppletWidgetView)Enum.Parse(typeof(AppletWidgetView), widgetEle.Attribute("altViews")?.Value ?? "None"),
                        ColorClass  = widgetEle.Attribute("headerClass")?.Value ?? "bg-light",
                        Priority    = Int32.Parse(widgetEle.Attribute("priority")?.Value ?? "0"),
                        MaxStack    = Int32.Parse(widgetEle.Attribute("maxStack")?.Value ?? "2"),
                        Order       = Int32.Parse(widgetEle.Attribute("order")?.Value ?? "0"),
                        Context     = widgetEle.Attribute("context")?.Value,
                        Description = widgetEle.Elements().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "description").Select(o => new LocaleString()
                        {
                            Value = o.Value, Language = o.Attribute("lang")?.Value
                        }).ToList(),
                        Name       = widgetEle.Attribute("name")?.Value,
                        Controller = widgetEle.Element((XNamespace)PakManTool.XS_APPLET + "controller")?.Value,
                        Guard      = widgetEle.Elements().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "guard").Select(o => o.Value).ToList()
                    };
                }
                else
                {
                    htmlAsset = new AppletAssetHtml();
                    // View state data
                    htmlAsset.ViewState = xe.Elements().OfType <XElement>().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "state").Select(o => new AppletViewState()
                    {
                        Priority   = Int32.Parse(o.Attribute("priority")?.Value ?? "0"),
                        Name       = o.Attribute("name")?.Value,
                        Route      = o.Elements().OfType <XElement>().FirstOrDefault(r => r.Name == (XNamespace)PakManTool.XS_APPLET + "url" || r.Name == (XNamespace)PakManTool.XS_APPLET + "route")?.Value,
                        IsAbstract = Boolean.Parse(o.Attribute("abstract")?.Value ?? "False"),
                        View       = o.Elements().OfType <XElement>().Where(v => v.Name == (XNamespace)PakManTool.XS_APPLET + "view")?.Select(v => new AppletView()
                        {
                            Priority = Int32.Parse(o.Attribute("priority")?.Value ?? "0"),

                            Name       = v.Attribute("name")?.Value,
                            Controller = v.Element((XNamespace)PakManTool.XS_APPLET + "controller")?.Value
                        }).ToList()
                    }).FirstOrDefault();
                    htmlAsset.Titles = xe.Elements().OfType <XElement>().Where(t => t.Name == (XNamespace)PakManTool.XS_APPLET + "title")?.Select(t => new LocaleString()
                    {
                        Language = t.Attribute("lang")?.Value,
                        Value    = t?.Value
                    }).ToList();

                    htmlAsset.Layout = PakManTool.TranslatePath(xe.Attribute((XNamespace)PakManTool.XS_APPLET + "layout")?.Value);
                    htmlAsset.Static = xe.Attribute((XNamespace)PakManTool.XS_APPLET + "static")?.Value == "true";
                }

                htmlAsset.Titles = new List <LocaleString>(xe.Descendants().OfType <XElement>().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "title").Select(o => new LocaleString()
                {
                    Language = o.Attribute("lang")?.Value, Value = o.Value
                }));
                htmlAsset.Bundle = new List <string>(xe.Descendants().OfType <XElement>().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "bundle").Select(o => PakManTool.TranslatePath(o.Value)));
                htmlAsset.Script = new List <AssetScriptReference>(xe.Descendants().OfType <XElement>().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "script").Select(o => new AssetScriptReference()
                {
                    Reference = PakManTool.TranslatePath(o.Value),
                    IsStatic  = Boolean.Parse(o.Attribute("static")?.Value ?? "true")
                }));
                htmlAsset.Style = new List <string>(xe.Descendants().OfType <XElement>().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "style").Select(o => PakManTool.TranslatePath(o.Value)));

                var demand = xe.DescendantNodes().OfType <XElement>().Where(o => o.Name == (XNamespace)PakManTool.XS_APPLET + "demand").Select(o => o.Value).ToList();

                var includes = xe.DescendantNodes().OfType <XComment>().Where(o => o?.Value?.Trim().StartsWith("#include virtual=\"") == true).ToList();
                foreach (var inc in includes)
                {
                    String assetName = inc.Value.Trim().Substring(18); // HACK: Should be a REGEX
                    if (assetName.EndsWith("\""))
                    {
                        assetName = assetName.Substring(0, assetName.Length - 1);
                    }
                    if (assetName == "content")
                    {
                        continue;
                    }
                    var includeAsset = PakManTool.TranslatePath(assetName);
                    inc.AddAfterSelf(new XComment(String.Format("#include virtual=\"{0}\"", includeAsset)));
                    inc.Remove();
                }

                var xel = xe.Descendants().OfType <XElement>().Where(o => o.Name.Namespace == (XNamespace)PakManTool.XS_APPLET).ToList();
                if (xel != null)
                {
                    foreach (var x in xel)
                    {
                        x.Remove();
                    }
                }
                htmlAsset.Html = xe;

                return(new AppletAsset()
                {
                    MimeType = "text/html",
                    Content = htmlAsset,
                    Policies = demand
                });
            }
            catch (XmlException e)
            {
                Emit.Message("ERROR", " {0} is not well formed - {1} - @{2}:{3}", file, e.Message, e.LineNumber, e.LinePosition);
                throw;
            }
            catch (Exception e)
            {
                Emit.Message("ERROR", "Cannot process {0} : {1}", file, e.Message);
                throw;
            }
        }
예제 #2
0
        /// <summary>
        /// Process the specified directory
        /// </summary>
        private static IEnumerable <AppletAsset> ProcessDirectory(string source, String path, ConsoleParameters parms)
        {
            List <AppletAsset> retVal = new List <AppletAsset>();

            foreach (var itm in Directory.GetFiles(source))
            {
                if (Path.GetFileName(itm).StartsWith("."))
                {
                    Console.WriteLine("\t Skipping {0}...", itm);
                    continue;
                }
                Console.WriteLine("\t Processing {0}...", itm);

                if (Path.GetFileName(itm).ToLower() == "manifest.xml")
                {
                    continue;
                }
                else
                {
                    switch (Path.GetExtension(itm))
                    {
                    case ".html":
                    case ".htm":
                    case ".xhtml":
                        XElement xe = XElement.Load(itm);

                        // Now we have to iterate throuh and add the asset\
                        AppletAssetHtml htmlAsset = null;

                        if (xe.Elements().OfType <XElement>().Any(o => o.Name == xs_openiz + "widget"))
                        {
                            var widgetEle = xe.Elements().OfType <XElement>().FirstOrDefault(o => o.Name == xs_openiz + "widget");
                            htmlAsset = new AppletWidget()
                            {
                                Icon        = widgetEle.Element(xs_openiz + "icon")?.Value,
                                Type        = (AppletWidgetType)Enum.Parse(typeof(AppletWidgetType), widgetEle.Attribute("type")?.Value),
                                Scope       = (AppletWidgetScope)Enum.Parse(typeof(AppletWidgetScope), widgetEle.Attribute("scope")?.Value),
                                Description = widgetEle.Elements().Where(o => o.Name == xs_openiz + "description").Select(o => new LocaleString()
                                {
                                    Value = o.Value, Language = o.Attribute("lang")?.Value
                                }).ToList(),
                                Name       = widgetEle.Attribute("name")?.Value,
                                Controller = widgetEle.Element(xs_openiz + "controller")?.Value,
                            };
                        }
                        else
                        {
                            htmlAsset = new AppletAssetHtml();
                            // View state data
                            htmlAsset.ViewState = xe.Elements().OfType <XElement>().Where(o => o.Name == xs_openiz + "state").Select(o => new AppletViewState()
                            {
                                Name       = o.Attribute("name")?.Value,
                                Route      = o.Elements().OfType <XElement>().FirstOrDefault(r => r.Name == xs_openiz + "url" || r.Name == xs_openiz + "route")?.Value,
                                IsAbstract = Boolean.Parse(o.Attribute("abstract")?.Value ?? "False"),
                                View       = o.Elements().OfType <XElement>().Where(v => v.Name == xs_openiz + "view")?.Select(v => new AppletView()
                                {
                                    Name  = v.Attribute("name")?.Value,
                                    Title = v.Elements().OfType <XElement>().Where(t => t.Name == xs_openiz + "title")?.Select(t => new LocaleString()
                                    {
                                        Language = t.Attribute("lang")?.Value,
                                        Value    = t?.Value
                                    }).ToList(),
                                    Controller = v.Element(xs_openiz + "controller")?.Value
                                }).ToList()
                            }).FirstOrDefault();
                            htmlAsset.Layout = ResolveName(xe.Attribute(xs_openiz + "layout")?.Value);
                            htmlAsset.Static = xe.Attribute(xs_openiz + "static")?.Value == "true";
                        }

                        htmlAsset.Titles = new List <LocaleString>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "title").Select(o => new LocaleString()
                        {
                            Language = o.Attribute("lang")?.Value, Value = o.Value
                        }));
                        htmlAsset.Bundle = new List <string>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "bundle").Select(o => ResolveName(o.Value)));
                        htmlAsset.Script = new List <AssetScriptReference>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "script").Select(o => new AssetScriptReference()
                        {
                            Reference = ResolveName(o.Value),
                            IsStatic  = Boolean.Parse(o.Attribute("static")?.Value ?? "true")
                        }));
                        htmlAsset.Style = new List <string>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "style").Select(o => ResolveName(o.Value)));

                        var demand = xe.DescendantNodes().OfType <XElement>().Where(o => o.Name == xs_openiz + "demand").Select(o => o.Value).ToList();

                        var includes = xe.DescendantNodes().OfType <XComment>().Where(o => o?.Value?.Trim().StartsWith("#include virtual=\"") == true).ToList();
                        foreach (var inc in includes)
                        {
                            String assetName = inc.Value.Trim().Substring(18);     // HACK: Should be a REGEX
                            if (assetName.EndsWith("\""))
                            {
                                assetName = assetName.Substring(0, assetName.Length - 1);
                            }
                            if (assetName == "content")
                            {
                                continue;
                            }
                            var includeAsset = ResolveName(assetName);
                            inc.AddAfterSelf(new XComment(String.Format("#include virtual=\"{0}\"", includeAsset)));
                            inc.Remove();
                        }

                        var xel = xe.Descendants().OfType <XElement>().Where(o => o.Name.Namespace == xs_openiz).ToList();
                        if (xel != null)
                        {
                            foreach (var x in xel)
                            {
                                x.Remove();
                            }
                        }
                        htmlAsset.Html = xe;

                        retVal.Add(new AppletAsset()
                        {
                            Name     = ResolveName(itm.Replace(path, "")),
                            MimeType = "text/html",
                            Content  = htmlAsset,
                            Policies = demand
                        });
                        break;

                    case ".css":

                        retVal.Add(new AppletAsset()
                        {
                            Name     = ResolveName(itm.Replace(path, "")),
                            MimeType = "text/css",
                            Content  = File.ReadAllText(itm)
                        });
                        break;

                    case ".js":
                        retVal.Add(new AppletAsset()
                        {
                            Name     = ResolveName(itm.Replace(path, "")),
                            MimeType = "text/javascript",
                            Content  = parms.Optimize && !itm.Contains("rules") ? new Microsoft.Ajax.Utilities.Minifier().MinifyJavaScript(File.ReadAllText(itm), new Microsoft.Ajax.Utilities.CodeSettings()
                            {
                                MinifyCode = false, StripDebugStatements = true, LocalRenaming = Microsoft.Ajax.Utilities.LocalRenaming.KeepAll, PreserveFunctionNames = true
                            }) : File.ReadAllText(itm)
                        });
                        break;

                    case ".json":
                        retVal.Add(new AppletAsset()
                        {
                            Name     = ResolveName(itm.Replace(path, "")),
                            MimeType = "application/json",
                            Content  = File.ReadAllText(itm)
                        });
                        break;

                    default:
                        string mt = null;
                        retVal.Add(new AppletAsset()
                        {
                            Name     = ResolveName(itm.Replace(path, "")),
                            MimeType = mime.TryGetValue(Path.GetExtension(itm), out mt) ? mt : "application/octet-stream",
                            Content  = File.ReadAllBytes(itm)
                        });
                        break;
                    }
                }
            }

            // Process sub directories
            foreach (var dir in Directory.GetDirectories(source))
            {
                if (!Path.GetFileName(dir).StartsWith("."))
                {
                    retVal.AddRange(ProcessDirectory(dir, path, parms));
                }
                else
                {
                    Console.WriteLine("Skipping directory {0}", dir);
                }
            }

            return(retVal);
        }
예제 #3
0
        /// <summary>
        /// Get applet asset
        /// </summary>
        public object ResolveAppletAsset(AppletAsset navigateAsset)
        {
            String itmPath = System.IO.Path.Combine(
                this.m_appletBaseDir[navigateAsset.Manifest],
                navigateAsset.Name);

            if (navigateAsset.MimeType == "text/html")
            {
                XElement xe = XElement.Load(itmPath);

                // Now we have to iterate throuh and add the asset\
                AppletAssetHtml htmlAsset = null;

                if (xe.Elements().OfType <XElement>().Any(o => o.Name == xs_openiz + "widget"))
                {
                    var widgetEle = xe.Elements().OfType <XElement>().FirstOrDefault(o => o.Name == xs_openiz + "widget");
                    htmlAsset = new AppletWidget()
                    {
                        Icon        = widgetEle.Element(xs_openiz + "icon")?.Value,
                        Type        = (AppletWidgetType)Enum.Parse(typeof(AppletWidgetType), widgetEle.Attribute("type")?.Value),
                        Scope       = (AppletWidgetScope)Enum.Parse(typeof(AppletWidgetScope), widgetEle.Attribute("scope")?.Value),
                        Description = widgetEle.Elements().Where(o => o.Name == xs_openiz + "description").Select(o => new LocaleString()
                        {
                            Value = o.Value, Language = o.Attribute("lang")?.Value
                        }).ToList(),
                        Name       = widgetEle.Attribute("name")?.Value,
                        Controller = widgetEle.Element(xs_openiz + "controller")?.Value,
                    };
                }
                else
                {
                    htmlAsset = new AppletAssetHtml();
                    // View state data
                    htmlAsset.ViewState = xe.Elements().OfType <XElement>().Where(o => o.Name == xs_openiz + "state").Select(o => new AppletViewState()
                    {
                        Name       = o.Attribute("name")?.Value,
                        Route      = o.Elements().OfType <XElement>().FirstOrDefault(r => r.Name == xs_openiz + "url" || r.Name == xs_openiz + "route")?.Value,
                        IsAbstract = Boolean.Parse(o.Attribute("abstract")?.Value ?? "False"),
                        View       = o.Elements().OfType <XElement>().Where(v => v.Name == xs_openiz + "view")?.Select(v => new AppletView()
                        {
                            Name  = v.Attribute("name")?.Value,
                            Title = v.Elements().OfType <XElement>().Where(t => t.Name == xs_openiz + "title")?.Select(t => new LocaleString()
                            {
                                Language = t.Attribute("lang")?.Value,
                                Value    = t?.Value
                            }).ToList(),
                            Controller = v.Element(xs_openiz + "controller")?.Value
                        }).ToList()
                    }).FirstOrDefault();
                    htmlAsset.Layout = ResolveName(xe.Attribute(xs_openiz + "layout")?.Value);
                    htmlAsset.Static = xe.Attribute(xs_openiz + "static")?.Value == "true";
                }

                htmlAsset.Titles = new List <LocaleString>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "title").Select(o => new LocaleString()
                {
                    Language = o.Attribute("lang")?.Value, Value = o.Value
                }));
                htmlAsset.Bundle = new List <string>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "bundle").Select(o => ResolveName(o.Value)));
                htmlAsset.Script = new List <AssetScriptReference>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "script").Select(o => new AssetScriptReference()
                {
                    Reference = ResolveName(o.Value),
                    IsStatic  = Boolean.Parse(o.Attribute("static")?.Value ?? "true")
                }));
                htmlAsset.Style = new List <string>(xe.Descendants().OfType <XElement>().Where(o => o.Name == xs_openiz + "style").Select(o => ResolveName(o.Value)));

                var demand = xe.DescendantNodes().OfType <XElement>().Where(o => o.Name == xs_openiz + "demand").Select(o => o.Value).ToList();

                var includes = xe.DescendantNodes().OfType <XComment>().Where(o => o?.Value?.Trim().StartsWith("#include virtual=\"") == true).ToList();
                foreach (var inc in includes)
                {
                    String assetName = inc.Value.Trim().Substring(18); // HACK: Should be a REGEX
                    if (assetName.EndsWith("\""))
                    {
                        assetName = assetName.Substring(0, assetName.Length - 1);
                    }
                    if (assetName == "content")
                    {
                        continue;
                    }
                    var includeAsset = ResolveName(assetName);
                    inc.AddAfterSelf(new XComment(String.Format("#include virtual=\"{0}\"", includeAsset)));
                    inc.Remove();
                }

                var xel = xe.Descendants().OfType <XElement>().Where(o => o.Name.Namespace == xs_openiz).ToList();
                if (xel != null)
                {
                    foreach (var x in xel)
                    {
                        x.Remove();
                    }
                }
                htmlAsset.Html = xe;
                return(htmlAsset);
            }
            else if (navigateAsset.MimeType == "text/javascript" ||
                     navigateAsset.MimeType == "text/css" ||
                     navigateAsset.MimeType == "application/json" ||
                     navigateAsset.MimeType == "text/xml")
            {
                var script = File.ReadAllText(itmPath);
                if (itmPath.Contains("openiz.js") || itmPath.Contains("openiz.min.js"))
                {
                    script += this.GetShimMethods();
                }
                return(script);
            }
            else
            {
                return(File.ReadAllBytes(itmPath));
            }
        }