public PublishingPageTransformator(ClientContext sourceClientContext, ClientContext targetClientContext, string pageTransformationFile, string publishingPageTransformationFile)
        {
#if DEBUG && MEASURE
            InitMeasurement();
#endif

            this.sourceClientContext = sourceClientContext ?? throw new ArgumentException("sourceClientContext must be provided.");
            this.targetClientContext = targetClientContext ?? throw new ArgumentException("targetClientContext must be provided.");;

            this.version           = GetVersion();
            this.pageTelemetry     = new PageTelemetry(version);
            this.pageLayoutManager = new PageLayoutManager(base.RegisteredLogObservers);

            // Load xml mapping data
            XmlSerializer xmlMapping = new XmlSerializer(typeof(PageTransformation));
            using (var stream = new FileStream(pageTransformationFile, FileMode.Open))
            {
                this.pageTransformation = (PageTransformation)xmlMapping.Deserialize(stream);
            }

            // Load the page layout mapping data
            this.publishingPageTransformation = this.pageLayoutManager.LoadDefaultPageLayoutMappingFile();

            if (!string.IsNullOrEmpty(publishingPageTransformationFile))
            {
                // Load page layout mapping from file
                var customPublishingPageTransformation = this.pageLayoutManager.LoadPageLayoutMappingFile(publishingPageTransformationFile);

                // Merge these custom layout mappings with the default ones
                this.publishingPageTransformation = this.pageLayoutManager.MergePageLayoutMappingFiles(this.publishingPageTransformation, customPublishingPageTransformation);
            }
        }
        internal PublishingPageTransformation MergePageLayoutMappingFiles(PublishingPageTransformation oobMapping, PublishingPageTransformation customMapping)
        {
            PublishingPageTransformation merged = new PublishingPageTransformation();

            // Handle the page layouts
            List <PageLayout> pageLayouts = new List <PageLayout>();

            foreach (var oobPageLayout in oobMapping.PageLayouts.ToList())
            {
                // If there's the same page layout used in the custom mapping then that one overrides the default
                if (!customMapping.PageLayouts.Where(p => p.Name.Equals(oobPageLayout.Name, StringComparison.InvariantCultureIgnoreCase)).Any())
                {
                    pageLayouts.Add(oobPageLayout);
                }
            }

            // Take over the custom ones
            pageLayouts.AddRange(customMapping.PageLayouts);
            merged.PageLayouts = pageLayouts.ToArray();

            // Handle the add-ons
            merged.AddOns = customMapping.AddOns;

            return(merged);
        }
        /// <summary>
        /// Load to PageLayout that will be used to transform the given page
        /// </summary>
        /// <param name="publishingPageTransformation">Publishing page transformation data to get the correct page layout mapping from</param>
        /// <param name="page">Page for which we're looking for a mapping</param>
        /// <returns>The page layout mapping that will be used for the passed page</returns>
        public PageLayout GetPageLayoutMappingModel(PublishingPageTransformation publishingPageTransformation, ListItem page)
        {
            // Load relevant model data for the used page layout
            string usedPageLayout = Path.GetFileNameWithoutExtension(page.PageLayoutFile());
            var    publishingPageTransformationModel = publishingPageTransformation.PageLayouts.Where(p => p.Name.Equals(usedPageLayout, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

            // No dedicated layout mapping found, let's see if there's an other page layout mapping that also applies for this page layout
            if (publishingPageTransformationModel == null)
            {
                // Fill a list of additional page layout mappings that can be used
                Dictionary <string, string> additionalMappings = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
                foreach (var pageLayout in publishingPageTransformation.PageLayouts.Where(p => !String.IsNullOrEmpty(p.AlsoAppliesTo)))
                {
                    var possiblePageLayouts = pageLayout.AlsoAppliesTo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                    if (possiblePageLayouts.Length > 0)
                    {
                        foreach (var possiblePageLayout in possiblePageLayouts)
                        {
                            // Only add the first possible page layout mapping, if a given page layout is defined multiple times then the first reference wins
                            if (!additionalMappings.ContainsKey(possiblePageLayout))
                            {
                                additionalMappings.Add(possiblePageLayout, pageLayout.Name);
                            }
                        }
                    }
                }

                if (additionalMappings.Count > 0)
                {
                    if (additionalMappings.ContainsKey(usedPageLayout))
                    {
                        publishingPageTransformationModel = publishingPageTransformation.PageLayouts.Where(p => p.Name.Equals(additionalMappings[usedPageLayout], StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
                    }
                }
            }

            // No layout provided via either the default mapping or custom mapping file provided
            if (publishingPageTransformationModel == null)
            {
                publishingPageTransformationModel = CacheManager.Instance.GetPageLayoutMapping(page);
                LogInfo(string.Format(LogStrings.PageLayoutMappingGeneration, usedPageLayout), LogStrings.Heading_PageLayoutManager);
            }

            // Still no layout...can't continue...
            if (publishingPageTransformationModel == null)
            {
                LogError(string.Format(LogStrings.Error_NoPageLayoutTransformationModel, usedPageLayout), LogStrings.Heading_PageLayoutManager);
                throw new Exception(string.Format(LogStrings.Error_NoPageLayoutTransformationModel, usedPageLayout));
            }

            LogInfo(string.Format(LogStrings.PageLayoutMappingBeingUsed, publishingPageTransformationModel.Name, usedPageLayout), LogStrings.Heading_PageLayoutManager);

            return(publishingPageTransformationModel);
        }
        internal PublishingPageTransformation MergePageLayoutMappingFiles(PublishingPageTransformation oobMapping, PublishingPageTransformation customMapping)
        {
            PublishingPageTransformation merged = new PublishingPageTransformation();

            // Handle the page layouts
            List <PageLayout>           pageLayouts             = new List <PageLayout>();
            Dictionary <string, string> customPageLayoutsMapped = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);

            // First process the ones that apply to multiple page layouts
            foreach (var pageLayout in customMapping.PageLayouts.Where(p => !String.IsNullOrEmpty(p.AlsoAppliesTo)))
            {
                var possiblePageLayouts = pageLayout.AlsoAppliesTo.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                if (possiblePageLayouts.Length > 0)
                {
                    foreach (var possiblePageLayout in possiblePageLayouts)
                    {
                        // Only add the first possible page layout mapping, if a given page layout is defined multiple times then the first reference wins
                        if (!customPageLayoutsMapped.ContainsKey(possiblePageLayout))
                        {
                            customPageLayoutsMapped.Add(possiblePageLayout, pageLayout.Name);
                        }
                    }
                }
            }

            // Next cover the mappings that apply to a single page layout
            foreach (var pageLayout in customMapping.PageLayouts.Where(p => String.IsNullOrEmpty(p.AlsoAppliesTo)))
            {
                if (!customPageLayoutsMapped.ContainsKey(pageLayout.Name))
                {
                    customPageLayoutsMapped.Add(pageLayout.Name, pageLayout.Name);
                }
            }

            // Keep the custom page layouts which are not overriden by a custom mapping
            foreach (var oobPageLayout in oobMapping.PageLayouts.ToList())
            {
                // If there's the same page layout used in the custom mapping then that one overrides the default

                if (!customPageLayoutsMapped.ContainsKey(oobPageLayout.Name))
                {
                    pageLayouts.Add(oobPageLayout);
                }
            }

            // Take over the custom ones
            pageLayouts.AddRange(customMapping.PageLayouts);
            merged.PageLayouts = pageLayouts.ToArray();

            // Handle the add-ons
            merged.AddOns = customMapping.AddOns;

            return(merged);
        }
        public PublishingPageHeaderTransformator(PublishingPageTransformationInformation publishingPageTransformationInformation, ClientContext sourceClientContext, ClientContext targetClientContext, PublishingPageTransformation publishingPageTransformation, IList <ILogObserver> logObservers = null)
        {
            // Register observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.publishingPageTransformationInformation = publishingPageTransformationInformation;
            this.publishingPageTransformation            = publishingPageTransformation;
            this.sourceClientContext = sourceClientContext;
            this.targetClientContext = targetClientContext;
            this.functionProcessor   = new PublishingFunctionProcessor(publishingPageTransformationInformation.SourcePage, sourceClientContext, targetClientContext, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers);
        }
        public PublishingMetadataTransformator(PublishingPageTransformationInformation publishingPageTransformationInformation, ClientContext sourceClientContext, ClientContext targetClientContext, ClientSidePage page, PageLayout publishingPageLayoutModel, PublishingPageTransformation publishingPageTransformation, IList <ILogObserver> logObservers = null)
        {
            // Register observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.publishingPageTransformationInformation = publishingPageTransformationInformation;
            this.targetClientContext = targetClientContext;
            this.page = page;
            this.pageLayoutMappingModel       = publishingPageLayoutModel;
            this.publishingPageTransformation = publishingPageTransformation;
            this.functionProcessor            = new PublishingFunctionProcessor(publishingPageTransformationInformation.SourcePage, sourceClientContext, targetClientContext, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers);
        }
Example #7
0
        public PublishingFunctionProcessor(ListItem page, ClientContext sourceClientContext, ClientContext targetClientContext, PublishingPageTransformation publishingPageTransformation, IList <ILogObserver> logObservers = null)
        {
            //Register any existing observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.page = page;
            this.publishingPageTransformation = publishingPageTransformation;
            this.sourceClientContext          = sourceClientContext;
            this.targetClientContext          = targetClientContext;

            RegisterAddons();
        }
        /// <summary>
        /// Analyse Page Layouts class constructor
        /// </summary>
        /// <param name="sourceContext">This should be the context of the source web</param>
        /// <param name="logObservers">List of log observers</param>
        public PageLayoutAnalyser(ClientContext sourceContext, IList <ILogObserver> logObservers = null)
        {
            // Register observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            _mapping = new PublishingPageTransformation();
            _contentTypeFieldCache = new Dictionary <string, FieldCollection>();
            _pageLayoutFileCache   = new Dictionary <string, string>();

            EnsureSiteCollectionContext(sourceContext);

            //parser = new HtmlParser(new HtmlParserOptions() { IsEmbedded = true,}, Configuration.Default.WithDefaultLoader().WithCss());
            parser = new HtmlParser();
        }
Example #9
0
        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="page">Page to operate on</param>
        /// <param name="sourceClientContext">Clientcontext of the source site</param>
        /// <param name="targetClientContext">Clientcontext of the target site</param>
        /// <param name="publishingPageTransformation">Publishing page layout mapping</param>
        /// <param name="baseTransformationInformation">Page transformation information</param>
        /// <param name="logObservers">Connected loggers</param>
        public PublishingFunctionProcessor(ListItem page, ClientContext sourceClientContext, ClientContext targetClientContext, PublishingPageTransformation publishingPageTransformation, BaseTransformationInformation baseTransformationInformation, IList <ILogObserver> logObservers = null)
        {
            //Register any existing observers
            if (logObservers != null)
            {
                foreach (var observer in logObservers)
                {
                    base.RegisterObserver(observer);
                }
            }

            this.page = page;
            this.publishingPageTransformation  = publishingPageTransformation;
            this.sourceClientContext           = sourceClientContext;
            this.targetClientContext           = targetClientContext;
            this.baseTransformationInformation = baseTransformationInformation;

            // we may have null values being passed over from unit tests
            if (this.sourceClientContext != null && this.targetClientContext != null && this.baseTransformationInformation != null)
            {
                RegisterAddons();
            }
        }
        public PublishingPageTransformator(ClientContext sourceClientContext, ClientContext targetClientContext, PageTransformation pageTransformationModel, PublishingPageTransformation customPublishingPageTransformationModel)
        {
#if DEBUG && MEASURE
            InitMeasurement();
#endif

            this.sourceClientContext = sourceClientContext ?? throw new ArgumentException("sourceClientContext must be provided.");
            this.targetClientContext = targetClientContext ?? throw new ArgumentException("targetClientContext must be provided.");;

            this.version           = GetVersion();
            this.pageTelemetry     = new PageTelemetry(version);
            this.pageLayoutManager = new PageLayoutManager(base.RegisteredLogObservers);

            this.pageTransformation = pageTransformationModel;

            // Load the page layout mapping data
            this.publishingPageTransformation = this.pageLayoutManager.LoadDefaultPageLayoutMappingFile();

            // Merge these custom layout mappings with the default ones
            if (customPublishingPageTransformationModel != null)
            {
                this.publishingPageTransformation = this.pageLayoutManager.MergePageLayoutMappingFiles(this.publishingPageTransformation, customPublishingPageTransformationModel);
            }
        }