public override void Apply(XmlDocument document, string key)
        {
            try
            {
                XPathNavigator docNavigator = document.CreateNavigator();

                base.Apply(document, docNavigator, key);

                _pageCount++;

                BuildComponentStyle style = this.Style;

                // 1. Apply the math...
                if (_mathApply)
                {
                    ApplyMath(docNavigator);
                }

                // 2. Apply the codes...
                if (_codeApply)
                {
                    if (style == BuildComponentStyle.Vs2005)
                    {
                        ApplyCode(docNavigator);
                    }
                    else if (style == BuildComponentStyle.Hana)
                    {
                        if (_codeHanaSelector == null)
                        {
                            _codeHanaSelector = XPathExpression.Compile(
                                "//div[@class='code']//*/pre");
                        }

                        ApplyCode(docNavigator, _codeHanaSelector);
                    }
                    else if (style == BuildComponentStyle.Prototype)
                    {
                        if (_codeProtoSelector == null)
                        {
                            _codeProtoSelector = XPathExpression.Compile(
                                "//div[@class='code']//pre");
                        }

                        ApplyCode(docNavigator, _codeProtoSelector);
                    }
                }

                // 3. Apply the header for logo and others
                ApplyHeader(docNavigator);

                // 4. Apply the tokens...
                ApplyTokens(docNavigator);
            }
            catch (Exception ex)
            {
                this.WriteMessage(MessageLevel.Error, ex);
            }
        }
        private void ApplyMath(XPathNavigator docNavigator)
        {
            XPathNodeIterator iterator = docNavigator.Select(_mathSelector);

            if (iterator == null || iterator.Count == 0)
            {
                return;
            }

            BuildComponentStyle builderStyle = this.Style;

            XPathNavigator navigator = null;

            XPathNavigator[] arrNavigator =
                BuildComponentUtilities.ConvertNodeIteratorToArray(iterator);

            int itemCount = arrNavigator.Length;

            for (int i = 0; i < itemCount; i++)
            {
                navigator = arrNavigator[i];
                if (navigator == null)
                {
                    continue;
                }

                string artTarget = navigator.GetAttribute("target", String.Empty);
                if (String.IsNullOrEmpty(artTarget))
                {
                    if (navigator.MoveToParent())
                    {
                        navigator.DeleteSelf();
                    }

                    this.WriteMessage(MessageLevel.Warn,
                                      "The equation media link target is not defined.");
                    continue;
                }

                int separator = artTarget.IndexOf(':');
                if (separator <= 0 || separator == (artTarget.Length - 1))
                {
                    if (navigator.MoveToParent())
                    {
                        navigator.DeleteSelf();
                    }

                    this.WriteMessage(MessageLevel.Warn,
                                      "The equation media link target is not valid.");
                    continue;
                }

                string mathFormat = artTarget.Substring(0, separator);
                string mathFile   = artTarget.Substring(separator + 1);
                if (String.IsNullOrEmpty(mathFormat) ||
                    String.IsNullOrEmpty(mathFile))
                {
                    if (navigator.MoveToParent())
                    {
                        navigator.DeleteSelf();
                    }

                    this.WriteMessage(MessageLevel.Warn,
                                      "The equation media link target is not valid.");
                    continue;
                }

                string[] formatLines = mathFormat.Split(new char[] { '|' });
                int      formatCount = formatLines.Length;
                if (formatCount == 2)
                {
                    navigator.MoveToParent();
                    XmlWriter xmlWriter = navigator.InsertAfter();

                    xmlWriter.WriteStartElement("div");
                    xmlWriter.WriteAttributeString("class", MathController.MathNone);
                    xmlWriter.WriteStartElement("p");

                    xmlWriter.WriteStartElement("img");
                    xmlWriter.WriteAttributeString("class", formatLines[1]);
                    xmlWriter.WriteAttributeString("alt", String.Empty);
                    //xmlWriter.WriteAttributeString("src", mathFile);

                    this.WriteIncludeAttribute(xmlWriter, "src", "mathPath", mathFile);

                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndElement();

                    xmlWriter.Close();
                    navigator.DeleteSelf();
                }
                else if (formatCount == 3)
                {
                    XmlWriter xmlWriter   = null;
                    int       equationNum = Convert.ToInt32(formatLines[2]);

                    if (equationNum == 0) // for inline-equations
                    {
                        xmlWriter = navigator.InsertAfter();

                        xmlWriter.WriteStartElement("span");  // start - span
                        xmlWriter.WriteAttributeString("class", formatLines[1]);
                        xmlWriter.WriteStartElement("img");   // start - img
                        xmlWriter.WriteAttributeString("alt", String.Empty);
                        //xmlWriter.WriteAttributeString("src", mathFile);

                        this.WriteIncludeAttribute(xmlWriter, "src", "mathPath", mathFile);

                        xmlWriter.WriteEndElement();          // end - img
                        xmlWriter.WriteEndElement();          // end - span
                    }
                    else
                    {
                        navigator.MoveToParent();
                        xmlWriter = navigator.InsertAfter();

                        xmlWriter.WriteStartElement("div");  // start - div
                        //xmlWriter.WriteAttributeString("align", "center");
                        xmlWriter.WriteAttributeString("class",
                                                       MathController.MathDiv);
                        //xmlWriter.WriteStartElement("p");  // start - p

                        if (_mathNumber && equationNum > 0)
                        {
                            xmlWriter.WriteStartElement("table");  // start - table
                            xmlWriter.WriteAttributeString("class",
                                                           MathController.MathTable);
                            xmlWriter.WriteStartElement("tr");     // start - tr
                            xmlWriter.WriteAttributeString("class",
                                                           MathController.MathRow);

                            xmlWriter.WriteStartElement("td");  // start - td
                            xmlWriter.WriteAttributeString("class",
                                                           MathController.MathLeft);
                            xmlWriter.WriteAttributeString("style",
                                                           "background-color:white;border:0");

                            xmlWriter.WriteStartElement("img");
                            xmlWriter.WriteAttributeString("class", formatLines[1]);
                            xmlWriter.WriteAttributeString("alt", String.Empty);
                            //xmlWriter.WriteAttributeString("src", mathFile);

                            this.WriteIncludeAttribute(xmlWriter, "src", "mathPath", mathFile);

                            xmlWriter.WriteEndElement();

                            xmlWriter.WriteEndElement();        // end - tr

                            xmlWriter.WriteStartElement("td");  // start - td
                            xmlWriter.WriteAttributeString("class",
                                                           MathController.MathRight);
                            xmlWriter.WriteAttributeString("style",
                                                           "background-color:white;border:0");
                            if (_mathNumIncludePage)
                            {
                                xmlWriter.WriteString(String.Format(_mathNumFormat,
                                                                    _pageCount, equationNum));
                            }
                            else
                            {
                                xmlWriter.WriteString(String.Format(_mathNumFormat,
                                                                    equationNum));
                            }
                            xmlWriter.WriteEndElement();  // end - tr

                            xmlWriter.WriteEndElement();  // end - tr
                            xmlWriter.WriteEndElement();  // end - table
                        }
                        else
                        {
                            if (builderStyle == BuildComponentStyle.Hana)
                            {
                                xmlWriter.WriteStartElement("div");
                                xmlWriter.WriteAttributeString("class", "mathHana");
                            }
                            xmlWriter.WriteStartElement("img");
                            xmlWriter.WriteAttributeString("class", formatLines[1]);
                            xmlWriter.WriteAttributeString("alt", String.Empty);
                            //xmlWriter.WriteAttributeString("src", mathFile);

                            this.WriteIncludeAttribute(xmlWriter, "src", "mathPath", mathFile);

                            xmlWriter.WriteEndElement();

                            if (builderStyle == BuildComponentStyle.Hana)
                            {
                                xmlWriter.WriteEndElement();
                            }
                        }

                        //xmlWriter.WriteEndElement();   // end - p
                        xmlWriter.WriteEndElement();   // end - div
                    }

                    if (xmlWriter != null)
                    {
                        xmlWriter.Close();
                        navigator.DeleteSelf();
                    }
                }
                else
                {
                    if (navigator.MoveToParent())
                    {
                        navigator.DeleteSelf();
                    }

                    this.WriteMessage(MessageLevel.Warn,
                                      "The equation styling text is not valid.");
                    continue;
                }
            }
        }