Esempio n. 1
0
        //=====================================================================

        /// <summary>
        /// This method applies the branding package transforms.
        /// </summary>
        /// <param name="document">The current document.</param>
        /// <param name="key">The document's unique identifier.</param>
        private void ApplyBranding(XmlDocument document, string key)
        {
            if(m_brandingTransform != null)
            {
#if DEBUG
                WriteMessage(MessageLevel.Info, "  Branding topic {0} ({1}) SelfBranded={2}", key, m_locale, m_selfBranded);
#endif
                try
                {
                    XmlDocument v_tempDocument = new XmlDocument();

                    v_tempDocument.LoadXml(document.OuterXml);

                    // The default xhtml namespace is required for the branding transforms to work,
                    if(String.IsNullOrEmpty(v_tempDocument.DocumentElement.GetAttribute("xmlns")))
                    {
                        v_tempDocument.DocumentElement.SetAttribute("xmlns", s_xhtmlNamespace);
                        v_tempDocument.LoadXml(v_tempDocument.OuterXml);
                    }
                    SetSelfBranding(v_tempDocument, m_selfBranded);
#if DEBUG//_NOT
                    try
                    {
                        String v_tempPath = Path.GetFullPath("PreBranding");
                        if(!Directory.Exists(v_tempPath))
                        {
                            Directory.CreateDirectory(v_tempPath);
                        }
                        v_tempPath = Path.Combine(v_tempPath, key.Replace(':', '_').Replace('.', '_') + ".htm");
                        v_tempDocument.Save(v_tempPath);
                    }
                    catch { }
#endif
#if DEBUG_NOT
                    String v_tempBrandingPath = Path.GetFullPath("TempBranding");
                    if (!Directory.Exists (v_tempBrandingPath))
                    {
                        Directory.CreateDirectory (v_tempBrandingPath);
                    }
                    v_tempBrandingPath = Path.Combine (v_tempBrandingPath, key.Replace (':', '_').Replace ('.', '_') + ".htm");
                    using (FileStream v_stream = new FileStream (v_tempBrandingPath, FileMode.Create, FileAccess.ReadWrite))
#else
                    using(Stream v_stream = new MemoryStream())
#endif
                    {
                        try
                        {
                            XPathNavigator v_navigator = v_tempDocument.CreateNavigator();
                            using(XmlWriter v_writer = XmlWriter.Create(v_stream, m_brandingTransform.OutputSettings))
                            {
                                m_brandingTransform.Transform(v_navigator, m_transformArguments, v_writer);
                            }

                            XmlReaderSettings v_readerSettings = new XmlReaderSettings();
                            v_readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
                            v_readerSettings.CloseInput = true;
                            v_stream.Seek(0, SeekOrigin.Begin);
                            using(XmlReader v_reader = XmlReader.Create(v_stream, v_readerSettings))
                            {
                                v_tempDocument.RemoveAll();
                                v_tempDocument.Load(v_reader);
                            }

                            RemoveUnusedNamespaces(v_tempDocument);

                            using(XmlReader v_reader = new SpecialXmlReader(v_tempDocument.OuterXml, this))
                            {
                                document.RemoveAll();
                                document.Load(v_reader);
                            }
#if DEBUG//_NOT
                            try
                            {
                                String v_tempPath = Path.GetFullPath("PostBranding");
                                if(!Directory.Exists(v_tempPath))
                                {
                                    Directory.CreateDirectory(v_tempPath);
                                }
                                v_tempPath = Path.Combine(v_tempPath, key.Replace(':', '_').Replace('.', '_') + ".htm");
                                document.Save(v_tempPath);
                            }
                            catch { }
#endif
                        }
                        catch(Exception exp)
                        {
                            WriteMessage(key, MessageLevel.Error, exp.Message);
                        }
                    }
                }
                catch(XsltException exp)
                {
                    WriteMessage(key, MessageLevel.Error, "{0} at {1} {2} {3}", exp.Message, exp.SourceUri, exp.LineNumber, exp.LinePosition);
#if DEBUG
                    if(exp.InnerException != null)
                    {
                        WriteMessage(key, MessageLevel.Error, "[{0}] {1}", exp.InnerException.GetType().Name, exp.InnerException.Message);
                    }
#endif
                }
                catch(Exception exp)
                {
                    WriteMessage(key, MessageLevel.Error, exp.Message);
                }
            }
        }
        //=====================================================================

        /// <summary>
        /// This method applies the branding package transforms.
        /// </summary>
        /// <param name="document">The current document.</param>
        /// <param name="key">The document's unique identifier.</param>
        private void ApplyBranding(XmlDocument document, string key)
        {
            if (m_brandingTransform != null)
            {
#if DEBUG
                WriteMessage(MessageLevel.Info, "  Branding topic {0} ({1}) SelfBranded={2}", key, m_locale, m_selfBranded);
#endif
                try
                {
                    XmlDocument v_tempDocument = new XmlDocument();

                    v_tempDocument.LoadXml(document.OuterXml);

                    // The default xhtml namespace is required for the branding transforms to work,
                    if (String.IsNullOrEmpty(v_tempDocument.DocumentElement.GetAttribute("xmlns")))
                    {
                        v_tempDocument.DocumentElement.SetAttribute("xmlns", s_xhtmlNamespace);
                        v_tempDocument.LoadXml(v_tempDocument.OuterXml);
                    }
                    SetSelfBranding(v_tempDocument, m_selfBranded);
#if DEBUG//_NOT
                    try
                    {
                        String v_tempPath = Path.GetFullPath("PreBranding");
                        if (!Directory.Exists(v_tempPath))
                        {
                            Directory.CreateDirectory(v_tempPath);
                        }
                        v_tempPath = Path.Combine(v_tempPath, key.Replace(':', '_').Replace('.', '_') + ".htm");
                        v_tempDocument.Save(v_tempPath);
                    }
                    catch { }
#endif
#if DEBUG_NOT
                    String v_tempBrandingPath = Path.GetFullPath("TempBranding");
                    if (!Directory.Exists(v_tempBrandingPath))
                    {
                        Directory.CreateDirectory(v_tempBrandingPath);
                    }
                    v_tempBrandingPath = Path.Combine(v_tempBrandingPath, key.Replace(':', '_').Replace('.', '_') + ".htm");
                    using (FileStream v_stream = new FileStream(v_tempBrandingPath, FileMode.Create, FileAccess.ReadWrite))
#else
                    using (Stream v_stream = new MemoryStream())
#endif
                    {
                        try
                        {
                            XPathNavigator v_navigator = v_tempDocument.CreateNavigator();
                            using (XmlWriter v_writer = XmlWriter.Create(v_stream, m_brandingTransform.OutputSettings))
                            {
                                m_brandingTransform.Transform(v_navigator, m_transformArguments, v_writer);
                            }

                            XmlReaderSettings v_readerSettings = new XmlReaderSettings();
                            v_readerSettings.ConformanceLevel = ConformanceLevel.Fragment;
                            v_readerSettings.CloseInput       = true;
                            v_stream.Seek(0, SeekOrigin.Begin);
                            using (XmlReader v_reader = XmlReader.Create(v_stream, v_readerSettings))
                            {
                                v_tempDocument.RemoveAll();
                                v_tempDocument.Load(v_reader);
                            }

                            RemoveUnusedNamespaces(v_tempDocument);

                            using (XmlReader v_reader = new SpecialXmlReader(v_tempDocument.OuterXml, this))
                            {
                                document.RemoveAll();
                                document.Load(v_reader);
                            }
#if DEBUG//_NOT
                            try
                            {
                                String v_tempPath = Path.GetFullPath("PostBranding");
                                if (!Directory.Exists(v_tempPath))
                                {
                                    Directory.CreateDirectory(v_tempPath);
                                }
                                v_tempPath = Path.Combine(v_tempPath, key.Replace(':', '_').Replace('.', '_') + ".htm");
                                document.Save(v_tempPath);
                            }
                            catch { }
#endif
                        }
                        catch (Exception exp)
                        {
                            WriteMessage(key, MessageLevel.Error, exp.Message);
                        }
                    }
                }
                catch (XsltException exp)
                {
                    WriteMessage(key, MessageLevel.Error, "{0} at {1} {2} {3}", exp.Message, exp.SourceUri, exp.LineNumber, exp.LinePosition);
#if DEBUG
                    if (exp.InnerException != null)
                    {
                        WriteMessage(key, MessageLevel.Error, "[{0}] {1}", exp.InnerException.GetType().Name, exp.InnerException.Message);
                    }
#endif
                }
                catch (Exception exp)
                {
                    WriteMessage(key, MessageLevel.Error, exp.Message);
                }
            }
        }