Extends the XmlDocument with an advanced loading facilities and additional properties that provide the last modification date and a list of files that the document depends on.
Inheritance: System.Xml.XmlDocument
示例#1
0
        private static void CopyXslElements(SageContext context, string stylesheetPath, CacheableXmlDocument targetDocument)
        {
            CacheableXmlDocument fromDocument = context.Resources.LoadXml(stylesheetPath);
            targetDocument.AddDependencies(fromDocument.Dependencies);

            string xpathOthers = string.Join(" | ",
                new[] { "/*/xsl:preserve-space", "/*/xsl:strip-space", "/*/xsl:namespace-alias", "/*/xsl:attribute-set" });

            XmlNodeList paramNodes = fromDocument.SelectNodes("/*/xsl:param", XmlNamespaces.Manager);
            XmlNodeList variableNodes = fromDocument.SelectNodes("/*/xsl:variable", XmlNamespaces.Manager);
            XmlNodeList templateNodes = fromDocument.SelectNodes("/*/xsl:template", XmlNamespaces.Manager);
            XmlNodeList includeNodes = fromDocument.SelectNodes("/*/xsl:include", XmlNamespaces.Manager);
            XmlNodeList scriptNodes = fromDocument.SelectNodes("/*/msxsl:script", XmlNamespaces.Manager);
            XmlNodeList otherNodes = fromDocument.SelectNodes(xpathOthers, XmlNamespaces.Manager);

            string stylesheetDirectory = Path.GetDirectoryName(stylesheetPath);

            // recursively add any includes
            foreach (XmlElement includeElem in includeNodes)
            {
                string includeHref = includeElem.GetAttribute("href");
                Uri includeHrefUri = new Uri(includeHref, UriKind.RelativeOrAbsolute);
                string includePath = includeHrefUri.IsAbsoluteUri ? includeHref : string.Join("/", stylesheetDirectory, includeHref);

                ModuleConfiguration.CopyXslElements(context, includePath, targetDocument);
                targetDocument.AddDependencies(includePath);
            }

            // templates
            foreach (XmlNode xslNode in templateNodes)
                targetDocument.DocumentElement.AppendChild(targetDocument.ImportNode(xslNode, true));

            foreach (XmlNode xslNode in scriptNodes)
                targetDocument.DocumentElement.AppendChild(targetDocument.ImportNode(xslNode, true));

            XmlNode firstNode = targetDocument.SelectSingleNode("/*/xsl:template[1]", XmlNamespaces.Manager);
            foreach (XmlNode xslNode in variableNodes)
                firstNode = targetDocument.DocumentElement.InsertBefore(targetDocument.ImportNode(xslNode, true), firstNode);

            // other nodes before variables or templates, params before other nodes
            foreach (XmlNode xslNode in otherNodes)
                firstNode = targetDocument.DocumentElement.InsertBefore(targetDocument.ImportNode(xslNode, true), firstNode);

            foreach (XmlNode xslNode in paramNodes)
                targetDocument.DocumentElement.InsertBefore(targetDocument.ImportNode(xslNode, true), targetDocument.DocumentElement.SelectSingleNode("*"));

            foreach (XmlAttribute attrNode in fromDocument.DocumentElement.Attributes)
                targetDocument.DocumentElement.SetAttribute(attrNode.Name, attrNode.InnerText);
        }
示例#2
0
        internal static void OmitNamespacePrefixResults(CacheableXmlDocument document)
        {
            if (document.DocumentElement == null)
            {
                return;
            }

            List<string> prefixes = new List<string>();
            foreach (XmlAttribute attribute in document.DocumentElement.Attributes)
            {
                if (attribute.Name.StartsWith("xmlns:"))
                {
                    prefixes.Add(attribute.Name.Substring(6));
                }
            }

            document.DocumentElement.SetAttribute("exclude-result-prefixes", string.Join(" ", prefixes.ToArray()));
        }
示例#3
0
        private XmlNode ResolveExtraDocumentInclude(XmlNamespaceManager nm, string parse, string includePath, string xpath, string encoding, SageContext context)
        {
            XmlNode result;
            if (parse == "text")
            {
                WebRequest request = WebRequest.Create(includePath);
                WebResponse response = request.GetResponse();
                using (Stream stream = response.GetResponseStream())
                {
                    Encoding enc = Encoding.GetEncoding(encoding);
                    string text = new StreamReader(stream, enc).ReadToEnd();
                    result = this.CreateTextNode(text);
                }
            }
            else
            {
                CacheableXmlDocument temp = new CacheableXmlDocument();
                temp.LoadInternal(includePath, context, false);

                result = temp.DocumentElement;
                if (!string.IsNullOrWhiteSpace(xpath))
                {
                    result = temp.SelectSingleNode(xpath, nm);
                }
            }

            if (result != null && result.NodeType == XmlNodeType.Document)
                result = ((XmlDocument) result).DocumentElement;

            return result;
        }
示例#4
0
        internal static CacheableXmlDocument CombineModuleXslt(SageContext context)
        {
            CacheableXmlDocument resultDoc = new CacheableXmlDocument();
            resultDoc.LoadXml(DefaultXslt);

            foreach (var moduleKey in context.ProjectConfiguration.Modules.Keys)
            {
                var config = context.ProjectConfiguration.Modules[moduleKey];
                foreach (string path in config.Stylesheets)
                {
                    string stylesheetPath = context.Path.GetModulePath(moduleKey, path);
                    ModuleConfiguration.CopyXslElements(context, stylesheetPath, resultDoc);
                }
            }

            XsltTransform.OmitNamespacePrefixResults(resultDoc);
            return resultDoc;
        }
示例#5
0
 private static void SendContent(HttpContextBase context, CacheableXmlDocument document)
 {
     context.Response.ContentType = "text/xml";
     context.Response.Write(document.OuterXml);
 }
示例#6
0
            public override string Apply(string content, SageContext context)
            {
                if (transform == null)
                    transform = XsltTransform.Create(context, path);

                var result = new StringWriter();
                var document = new CacheableXmlDocument();
                document.LoadXml(content);
                document.DocumentElement.AppendChild(context.ToXml(document));

                transform.Transform(document, result, context);
                return result.ToString();
            }
示例#7
0
        private static CacheableXmlDocument LoadSourceDocument(string path, SageContext context)
        {
            UrlResolver resolver = new UrlResolver(context);
            CacheableXmlDocument result = new CacheableXmlDocument();
            result.Load(path, context);
            result.AddDependencies(path);
            result.AddDependencies(resolver.Dependencies.ToArray());

            return result;
        }
示例#8
0
        private XmlDocument CombineVariations()
        {
            constituents = new List<string>();

            LocaleInfo localeInfo;
            if (!context.ProjectConfiguration.Locales.TryGetValue(this.Locale, out localeInfo))
                throw new UnconfiguredLocaleException(this.Locale);

            // locales contains the list of locales ordered by priority (high to low)
            List<string> names = new List<string>(localeInfo.DictionaryNames);

            // documents are orderered as defined in the configuration, from high to low priority
            OrderedDictionary<string, List<CacheableXmlDocument>> allDictionaries = new OrderedDictionary<string, List<CacheableXmlDocument>>();
            foreach (string locale in names)
            {
                List<CacheableXmlDocument> langDictionaries = new List<CacheableXmlDocument>();
                string documentPath = context.Path.GetDictionaryPath(locale, context.Category);

                // add extension dictionaries for the current locale
                // langDictionaries.AddRange(Application.Extensions.GetDictionaries(context, locale));

                // add the project dictionary for the current locale
                if (File.Exists(documentPath))
                {
                    CacheableXmlDocument cacheable = new CacheableXmlDocument();
                    cacheable.Load(documentPath);

                    this.Dependencies.AddRange(cacheable.Dependencies);
                    langDictionaries.Add(cacheable);
                }

                if (langDictionaries.Count != 0)
                    allDictionaries.Add(locale, langDictionaries);
            }

            if (allDictionaries.Count == 0)
            {
                log.Error(
                    string.Format("There are no dictionary files for locale '{0}' in category '{1}'.\n", this.Locale, context.Category));

                return null;
            }

            // now create a combined document, adding items from each document starting with high priority
            // and moving through the lower priority ones
            string firstLocale = allDictionaries.Keys.First();

            XmlDocument result = allDictionaries[firstLocale][0];

            XmlElement rootNode = result.DocumentElement;
            XmlNodeList dictNodes = rootNode.SelectNodes("*");

            foreach (XmlElement phrase in dictNodes)
                phrase.SetAttribute("source", firstLocale);

            foreach (string locale in allDictionaries.Keys)
            {
                for (int i = 0; i < allDictionaries[locale].Count; i++)
                {
                    if (locale == firstLocale && i == 0)
                        continue;

                    dictNodes = allDictionaries[locale][i].DocumentElement.SelectNodes("*");
                    foreach (XmlElement node in dictNodes)
                    {
                        string phraseID = node.GetAttribute("id");
                        XmlNode existingNode = result.SelectSingleNode(string.Format("/*/*[@id='{0}']", phraseID.Replace("'", "&apos;")));
                        if (existingNode == null)
                        {
                            XmlElement phrase = rootNode.AppendElement(result.ImportNode(node, true));
                            phrase.SetAttribute("source", locale);
                        }
                    }
                }
            }

            foreach (XmlElement phraseNode in rootNode.SelectNodes("*"))
            {
                string itemId = phraseNode.GetAttribute("id");
                string itemText = phraseNode.InnerText;
                if (this.Items.ContainsKey(itemId))
                {
                    this.Items[itemId] = itemText;
                }
                else
                    this.Items.Add(itemId, itemText);
            }

            return result;
        }
示例#9
0
        internal CacheableXmlDocument LoadSourceDocument(string locale)
        {
            string fullPath = context.Path.Localize(this.FilePath, locale, true);
            if (UrlResolver.GetScheme(fullPath) == "file" && !File.Exists(fullPath))
            {
                throw new FileNotFoundException(string.Format("The resource file '{0}' could not be opened using locale '{1}'",
                    this.FilePath, locale));
            }

            CacheableXmlDocument document = new CacheableXmlDocument();
            document.Load(fullPath, context);
            return document;
        }
示例#10
0
 internal CacheableXmlDocument LoadLocalizedSourceDocument(string locale)
 {
     string fullPath = this.GetSourcePath(locale);
     CacheableXmlDocument document = new CacheableXmlDocument();
     document.Load(fullPath, context);
     return document;
 }
示例#11
0
 internal CacheableXmlDocument LoadGlobalizedDocument(string locale)
 {
     string fullPath = this.GetInternationalizedName(locale, true);
     CacheableXmlDocument document = new CacheableXmlDocument();
     document.Load(fullPath, context);
     return document;
 }