public string Execute([NotNull] string databaseName, [NotNull] string xslt)
        {
            Assert.ArgumentNotNull(databaseName, nameof(databaseName));
            Assert.ArgumentNotNull(xslt, nameof(xslt));

            var database = Factory.GetDatabase(databaseName);

            if (database == null)
            {
                throw new Exception("Database not found");
            }

            var item = database.GetRootItem();

            var fileName = TempFolder.GetFilename("TransformCodeGeneration.xslt");
            var path     = FileUtil.MapPath(fileName);

            File.WriteAllText(path, xslt, Encoding.UTF8);

            var xslFile = new XslFile
            {
                Path = fileName
            };

            xslFile.XslExtensions["http://www.sitecore.net/codegeneration"] = new Helper();

            var result = xslFile.Transform(item);

            File.Delete(path);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Loads the rendering from the XSL folder
        /// </summary>
        /// <param name="rendering">The rendering filename without extension</param>
        /// <returns>The rendering from the XSL folder</returns>
        public static string LoadRendering(string rendering)
        {
            Assert.ArgumentNotNullOrEmpty(rendering, "rendering");

            if (Sitecore.Context.Item == null)
            {
                return("Sitecore.Context.Item is null");
            }

            var xslFilename = "/xsl/" + rendering + ".xslt";
            var xslFile     = new XslFile
            {
                Path = xslFilename
            };

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                using (var hw = new HtmlTextWriter(sw))
                {
                    xslFile.RenderControl(hw);
                }
            }

            return(sb.ToString());
        }
        public void IncludeItem()
        {
            var control = new XslFile();
            control.Path = "xsl/dc2011/navigation.xslt";

            Sitecore.Context.Item = Sitecore.Context.Database.GetItem("/sitecore/content/dc2011");
            var output = control.RenderAsText();

            var doc = new HtmlDocument();
            doc.LoadHtml(output);

            var nav = doc.CreateNavigator();
            var link = nav.Select("/nav[@class='primary']/ul/li[a/@href='/en/About Us.aspx']/a");

            Assert.AreEqual(1, link.Count);
        }
Exemple #4
0
        public string Execute([NotNull] string databaseName, [NotNull] string itemId, [NotNull] string contextId)
        {
            Assert.ArgumentNotNull(databaseName, nameof(databaseName));
            Assert.ArgumentNotNull(itemId, nameof(itemId));
            Assert.ArgumentNotNull(contextId, nameof(contextId));

            var database = Factory.GetDatabase(databaseName);

            if (database == null)
            {
                throw new Exception("Database not found");
            }

            var item = database.GetItem(itemId);

            if (item == null)
            {
                return(string.Empty);
            }

            var contextItem = database.GetItem(contextId);

            if (contextItem == null)
            {
                return("Context item does not exist");
            }

            var path = item["Path"];

            if (string.IsNullOrEmpty(path))
            {
                return(string.Empty);
            }

            var xslFile = new XslFile
            {
                Path = path
            };

            using (new DatabaseSwitcher(contextItem.Database))
            {
                using (new ContextItemSwitcher(contextItem))
                {
                    return(xslFile.Transform(contextItem));
                }
            }
        }
Exemple #5
0
        public PreservationVerifyReportResponseModel Process(PreservationVerifyReportRequestModel request)
        {
            try
            {
                _logger.Info("Process -> creating report for verifiy action");
                IEnumerable <VerifyReportArchive> verifyReportArchives = request.VerifyModel.jobs.GroupBy(p => p.archiveName, (name, items) => new
                {
                    archiveName = name,
                    jobs        = items
                })
                                                                         .Select(s => new VerifyReportArchive()
                {
                    ArchiveName   = s.archiveName,
                    Preservations = s.jobs.Select(job => new VerifyReportPreservation
                    {
                        IdPreservation = job.idPreservation,
                        Name           = job.preservationLabel ?? string.Empty,
                        Result         = (job.result ?? string.Empty) == "ok" ? "Verifica positiva" : "Verifica NEGATIVA",
                        Errors         = (job.errors ?? string.Empty).Replace("-", "").Replace("<br/>", "")
                    }).ToArray()
                });

                VerifyReport report = new VerifyReport()
                {
                    Archives   = verifyReportArchives.ToArray(),
                    fromDate   = request.VerifyModel.fromDate.ToString("dd/MM/yyyy"),
                    toDate     = request.VerifyModel.toDate.ToString("dd/MM/yyyy"),
                    verifyDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm")
                };

                string xmlFilename = Path.Combine(_reportPath, String.Format("Verifica_cons_chiuse_dal_{0}_al_{1}.xml", request.VerifyModel.fromDate.ToString("dd-MM-yyyy"), request.VerifyModel.toDate.ToString("dd-MM-yyyy")));

                if (File.Exists(xmlFilename))
                {
                    File.Delete(xmlFilename);
                }

                report.SaveAs(xmlFilename);

                //crea html
                string html = XslFile.TransformFile(xmlFilename, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content/reports", "VerifyReport.xsl"));

                //converte e salva in pdf
                string pdfFileName = string.Concat(Path.GetFileNameWithoutExtension(xmlFilename), ".pdf");
                string pdfFullName = Path.Combine(_reportPath, pdfFileName);
                if (File.Exists(pdfFullName))
                {
                    File.Delete(pdfFullName);
                }

                Html2Pdf(html, pdfFullName);

                if (File.Exists(xmlFilename))
                {
                    File.Delete(xmlFilename);
                }

                _logger.Info($"Process -> report generated correctly: {pdfFileName}");
                return(new PreservationVerifyReportResponseModel()
                {
                    Response = "Report generato con successo", FileName = pdfFileName
                });
            }
            catch (Exception ex)
            {
                return(new PreservationVerifyReportResponseModel()
                {
                    Response = $"Errore durante la generazione del report: {ex.Message}", FileName = "error"
                });
            }
        }
Exemple #6
0
        public override bool Execute()
        {
            string[] templates = ApplySecondaryValue(Transform, "Transform", Sources);
            string[] outputs   = ApplySecondaryValue(Outputs, "Output", Sources);

            if (Sources.Length == 0)
            {
                return(true);
            }

            for (int i = 0; i < Sources.Length; i++)
            {
                XslFilename name = new XslFilename(templates[i], _debug);
                XslFile     xsl;
                lock (_files)
                {
                    if (!_files.TryGetValue(name, out xsl))
                    {
                        xsl          = new XslFile(name);
                        _files[name] = xsl;
                    }
                }

                try
                {
                    xsl.EnsureCompiled();
                }
                catch (System.Xml.Xsl.XsltException xc)
                {
                    Log.LogError(null, null, null, xc.SourceUri, xc.LineNumber, xc.LinePosition, xc.LineNumber, xc.LinePosition, xc.Message);
                    Log.LogError(null, null, null, xc.SourceUri, xc.LineNumber, xc.LinePosition, xc.LineNumber, xc.LinePosition, xc.ToString());
                    return(false);
                }

                string   srcFile = Sources[i].ItemSpec;
                FileInfo fOut    = new FileInfo(outputs[i]);

                XsltArgumentList al = new XsltArgumentList();
                al.AddExtensionObject("http://schemas.qqn.nl/2008/02/ApplyXslTransform", new ApplyHelper(srcFile, this));                 // Allows calling back into MSBuild
                al.AddParam("Src", "", srcFile);
                al.AddParam("SrcFilename", "", Path.GetFileName(srcFile));
                al.AddParam("SrcFileTitle", "", Path.GetFileNameWithoutExtension(srcFile));
                al.AddParam("SrcDir", "", Path.GetDirectoryName(Path.GetFullPath(srcFile)));
                al.AddParam("Dest", "", outputs[i]);
                al.AddParam("DestFilename", "", Path.GetFileName(outputs[i]));
                al.AddParam("DestFileTitle", "", Path.GetFileNameWithoutExtension(outputs[i]));
                al.AddParam("DestDir", "", Path.GetDirectoryName(Path.GetFullPath(outputs[i])));
                al.AddParam("BaseDir", "", Environment.CurrentDirectory);
                al.AddParam("ProjectFile", "", BuildEngine.ProjectFileOfTaskNode);
                al.AddParam("TargetDir", "", TargetDir);
                al.AddParam("IntermediateDir", "", IntermediateDir);

                foreach (string iAttr in Attributes.Split(';'))
                {
                    if (string.IsNullOrEmpty(iAttr.Trim()))
                    {
                        continue;
                    }

                    string[] q = iAttr.Split('=');
                    al.AddParam(q[0].Trim(), "", q[1].Trim());
                }

                Log.LogMessage(MessageImportance.High, "Transforming '{0}' into '{1}' using '{2}'", Sources[i].ItemSpec, outputs[i], Path.GetFileName(xsl.Filename));

                try
                {
                    using (Stream wtr = File.Create(outputs[i]))
                    {
                        xsl.Transform.Transform(srcFile, al, wtr);
                    }
                }
                catch (XsltException xe)
                {
                    Log.LogError(null, null, null, xe.SourceUri, xe.LineNumber, xe.LinePosition, xe.LineNumber, xe.LinePosition, xe.Message);
                    Log.LogError(null, null, null, xe.SourceUri, xe.LineNumber, xe.LinePosition, xe.LineNumber, xe.LinePosition, xe.ToString());
                    try
                    {
                        File.Delete(outputs[i]);
                    }
                    catch
                    { }
                }
                catch (Exception)
                {
                    try
                    {
                        File.Delete(outputs[i]);
                    }
                    catch
                    { }
                    throw;
                }

                _filesWritten.Add(new TaskItem(outputs[i]));
            }

            return(true);
        }