Ejemplo n.º 1
0
        private static bool TryReadLegacyManifest(IPackageFile manifestFile, IPackage package, ILogger logger, out InstallScript installScript)
        {
            installScript = new InstallScript();
            var scripts  = new List <InstallItem>();
            var manifest = new XmlDocument();

            using (var manifestStream = manifestFile.Open())
                manifest.Load(manifestStream);

            foreach (var pkg in manifest.DocumentElement.Elements("package"))
            {
                if (string.IsNullOrEmpty(installScript.Title))
                {
                    installScript.Title = pkg.Attribute("name", "");
                }
                var folderPath = pkg.Attribute("path");
                if (folderPath == ".\\")
                {
                    folderPath = Utils.CleanFileName(pkg.Attribute("name", "")).Replace('.', '\\');
                }
                foreach (var file in package.Files()
                         .Where(f => f.Path.StartsWith(folderPath + "/", StringComparison.OrdinalIgnoreCase) &&
                                f.Path.EndsWith(".xml", StringComparison.OrdinalIgnoreCase)))
                {
                    try
                    {
                        var doc = new XmlDocument(manifest.NameTable);
                        using (var stream = file.Open())
                            doc.Load(stream);

                        var items = doc.DocumentElement.LocalName == "Item"
              ? new[] { doc.DocumentElement }
              : doc.DocumentElement.Elements("Item");
                        foreach (var item in items)
                        {
                            scripts.Add(InstallItem.FromScript(item, file.Path));
                        }
                    }
                    catch (Exception ex)
                    {
                        ex.Data["path"] = file.Path;
                        throw;
                    }
                }
            }

            installScript.Lines = scripts;
            return(CleanKeyedNames(installScript.Lines, logger));
        }
Ejemplo n.º 2
0
        public static void WriteAml(this IPackageFile file, IPackage package, XmlWriter writer)
        {
            if (file.Path.EndsWith(".xslt", StringComparison.OrdinalIgnoreCase))
            {
                string xsltContent;
                using (var stream = file.Open())
                    using (var reader = new StreamReader(stream))
                    {
                        xsltContent = reader.ReadToEnd();
                    }
                string xml = null;
                if (package.TryAccessFile(file.Path + ".xml", false, out var dataFile))
                {
                    using (var stream = dataFile.Open())
                        using (var reader = new StreamReader(stream))
                        {
                            xml = reader.ReadToEnd();
                        }
                }

                var metaStart = xsltContent.IndexOf(_reportStart);
                var metaEnd   = xsltContent.IndexOf(_reportEnd);

                if (metaStart >= 0 && metaEnd >= 0)
                {
                    var stack = new Stack <bool>();
                    using (var strReader = new StringReader(xsltContent.Substring(metaStart + _reportStart.Length, metaEnd - metaStart - _reportStart.Length).Trim()))
                        using (var reader = XmlReader.Create(strReader))
                        {
                            while (reader.Read())
                            {
                                switch (reader.NodeType)
                                {
                                case XmlNodeType.Element:
                                    writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                                    writer.WriteAttributes(reader, false);
                                    if (reader.IsEmptyElement)
                                    {
                                        writer.WriteEndElement();
                                    }
                                    else if (reader.LocalName == "Item" && reader.GetAttribute("type") == "Report")
                                    {
                                        stack.Push(true);
                                    }
                                    else
                                    {
                                        stack.Push(false);
                                    }
                                    break;

                                case XmlNodeType.Text:
                                    writer.WriteString(reader.Value);
                                    break;

                                case XmlNodeType.CDATA:
                                    writer.WriteCData(reader.Value);
                                    break;

                                case XmlNodeType.EntityReference:
                                    writer.WriteEntityRef(reader.Name);
                                    break;

                                case XmlNodeType.ProcessingInstruction:
                                case XmlNodeType.XmlDeclaration:
                                    writer.WriteProcessingInstruction(reader.Name, reader.Value);
                                    break;

                                case XmlNodeType.Comment:
                                    writer.WriteComment(reader.Value);
                                    break;

                                case XmlNodeType.DocumentType:
                                    writer.WriteDocType(reader.Name, reader.GetAttribute("PUBLIC"), reader.GetAttribute("SYSTEM"), reader.Value);
                                    break;

                                case XmlNodeType.Whitespace:
                                case XmlNodeType.SignificantWhitespace:
                                    writer.WriteWhitespace(reader.Value);
                                    break;

                                case XmlNodeType.EndElement:
                                    if (stack.Pop())
                                    {
                                        var xslt = xsltContent.Substring(metaEnd + _reportEnd.Length).Trim();
                                        if (!string.IsNullOrEmpty(xml))
                                        {
                                            xslt += Environment.NewLine + Environment.NewLine + _reportDataStart + Environment.NewLine + xml + Environment.NewLine + _reportDataEnd;
                                        }
                                        writer.WriteElementString("xsl_stylesheet", xslt);
                                    }
                                    writer.WriteFullEndElement();
                                    break;
                                }
                            }
                        }
                }
                else if (xsltContent.StartsWith("<AML>"))
                {
                    using (var strReader = new StringReader(xsltContent))
                        using (var reader = XmlReader.Create(strReader))
                        {
                            writer.WriteNode(reader, false);
                        }
                }
                else
                {
                    throw new ArgumentException("Invalid xslt file");
                }
            }
            else
            {
                using (var stream = file.Open())
                    using (var reader = XmlReader.Create(stream))
                    {
                        writer.WriteNode(reader, false);
                    }
            }
        }
Ejemplo n.º 3
0
        private static bool TryReadPackage(IPackageFile manifestFile, IPackage package, ILogger logger, out InstallScript installScript)
        {
            installScript = new InstallScript();
            var result   = true;
            var scripts  = new List <InstallItem>();
            var manifest = new XmlDocument();

            using (var manifestStream = manifestFile.Open())
                manifest.Load(manifestStream);

            if (manifest.DocumentElement.HasAttribute("created"))
            {
                installScript.Created = DateTime.Parse(manifest.DocumentElement.GetAttribute("created"));
            }
            installScript.Creator     = manifest.DocumentElement.GetAttribute("creator");
            installScript.Description = manifest.DocumentElement.GetAttribute("description");
            if (manifest.DocumentElement.HasAttribute("modified"))
            {
                installScript.Modified = DateTime.Parse(manifest.DocumentElement.GetAttribute("modified"));
            }
            installScript.Version = manifest.DocumentElement.GetAttribute("revision");
            installScript.Title   = manifest.DocumentElement.GetAttribute("title");
            if (manifest.DocumentElement.HasAttribute("website"))
            {
                installScript.Website = new Uri(manifest.DocumentElement.GetAttribute("website"));
            }

            foreach (var child in manifest.DocumentElement.ChildNodes.OfType <XmlElement>())
            {
                if (child.LocalName == "Item")
                {
                    scripts.Add(InstallItem.FromScript(child));
                }
                else
                {
                    var currPath = child.GetAttribute("path");
                    var files    = Enumerable.Empty <IPackageFile>();
                    if (currPath == "*")
                    {
                        files = package.Files();
                        installScript.DependencySorted = false;
                    }
                    else if (!string.IsNullOrEmpty(currPath))
                    {
                        if (package.TryAccessFile(currPath, false, out var file))
                        {
                            files = new[] { file };
                        }
                        else
                        {
                            result = false;
                            logger.LogError("The file {Path} is referenced in the manifest, but not found in the package.", currPath);
                            continue;
                        }
                    }

                    var reportXmlPaths = new HashSet <string>(files
                                                              .Where(f => f.Path.EndsWith(".xslt", StringComparison.OrdinalIgnoreCase))
                                                              .Select(f => f.Path + ".xml"), StringComparer.OrdinalIgnoreCase);

                    foreach (var file in files
                             .Where(f => !reportXmlPaths.Contains(f.Path)))
                    {
                        try
                        {
                            var doc = new XmlDocument(manifest.NameTable);
                            using (var writer = doc.CreateNavigator().AppendChild())
                                file.WriteAml(package, writer);
                            var items = doc.DocumentElement.LocalName == "Item"
                ? new[] { doc.DocumentElement }
                : doc.DocumentElement.Elements("Item");
                            foreach (var item in items)
                            {
                                scripts.Add(InstallItem.FromScript(item, file.Path));
                            }
                        }
                        catch (XmlException ex)
                        {
                            result = false;
                            logger.LogError(ex, "The AML script at {Path} is malformed", file.Path);
                        }
                        catch (Exception ex)
                        {
                            ex.Data["path"] = file.Path;
                            throw;
                        }
                    }
                }
            }

            installScript.Lines = scripts;
            result = CleanKeyedNames(installScript.Lines, logger) || result;
            return(result);
        }