private static IPublishedContent FindContentByAlias(ContextualPublishedContentCache cache, int rootNodeId, string alias)
        {
            if (alias == null) throw new ArgumentNullException("alias");

            // the alias may be "foo/bar" or "/foo/bar"
            // there may be spaces as in "/foo/bar,  /foo/nil"
            // these should probably be taken care of earlier on

            alias = alias.TrimStart('/');
            var xpathBuilder = new StringBuilder();
            xpathBuilder.Append(XPathStringsDefinition.Root);

            if (rootNodeId > 0)
                xpathBuilder.AppendFormat(XPathStrings.DescendantDocumentById, rootNodeId);

            XPathVariable var = null;
            if (alias.Contains('\'') || alias.Contains('"'))
            {
                // use a var, as escaping gets ugly pretty quickly
                var = new XPathVariable("alias", alias);
                alias = "$alias";
            }
            xpathBuilder.AppendFormat(XPathStrings.DescendantDocumentByAlias, alias);

            var xpath = xpathBuilder.ToString();

            // note: it's OK if var is null, will be ignored
            return cache.GetSingleByXPath(xpath, var);
        }
        private static IPublishedContent FindContentByAlias(ContextualPublishedContentCache cache, int rootNodeId, string alias)
        {
            if (alias == null)
            {
                throw new ArgumentNullException("alias");
            }

            // the alias may be "foo/bar" or "/foo/bar"
            // there may be spaces as in "/foo/bar,  /foo/nil"
            // these should probably be taken care of earlier on

            alias = alias.TrimStart('/');
            var xpathBuilder = new StringBuilder();


            if (rootNodeId > 0)
            {
                xpathBuilder.AppendFormat(XPathStrings.DescendantDocumentById, rootNodeId);
            }
            else
            {
                xpathBuilder.Append(XPathStringsDefinition.Root);
            }


            XPathVariable var = null;

            if (alias.Contains('\'') || alias.Contains('"'))
            {
                // use a var, as escaping gets ugly pretty quickly
                var   = new XPathVariable("alias", alias);
                alias = "$alias";
            }
            xpathBuilder.AppendFormat(XPathStrings.DescendantDocumentByAlias, alias);

            var xpath = xpathBuilder.ToString();

            // note: it's OK if var is null, will be ignored
            return(cache.GetSingleByXPath(xpath, var));
        }
        private IPublishedContent TypedDocumentByXPath(string xpath, XPathVariable[] vars, ContextualPublishedContentCache cache)
        {
            var doc = cache.GetSingleByXPath(xpath, vars);

            return(doc);
        }
Example #4
0
        /// <summary>
        /// Gets a dynamic content resulting from an XPath query.
        /// </summary>
        /// <param name="cache">The contextual cache.</param>
        /// <param name="xpath">The XPath query.</param>
        /// <param name="vars">Optional XPath variables</param>
        /// <returns>The dynamic content, or null.</returns>
        public static dynamic GetDynamicSingleByXPath(this ContextualPublishedContentCache cache, XPathExpression xpath, params XPathVariable[] vars)
        {
            var content = cache.GetSingleByXPath(xpath, vars);

            return(content == null ? DynamicNull.Null : new DynamicPublishedContent(content).AsDynamic());
        }
        protected IPublishedContent FindContentByAlias(ContextualPublishedContentCache cache, int rootContentId, string alias)
        {
            Mandate.ParameterNotNullOrEmpty(alias, "alias");

            StringBuilder xpathBuilder             = new StringBuilder();
            bool          hideTopLevelNodeFromPath = GlobalSettings.HideTopLevelNodeFromPath;

            // Build xPath

            if (rootContentId == 0)
            {
                if (hideTopLevelNodeFromPath)
                {
                    xpathBuilder.Append(XPathStringsDefinition.RootDocuments);                     // first node is not in the url
                }
                else
                {
                    xpathBuilder.Append(XPathStringsDefinition.Root);
                }
            }
            else
            {
                xpathBuilder.Append(XPathStringsDefinition.Root);
                xpathBuilder.AppendFormat(XPathStringsDefinition.DescendantDocumentById, rootContentId);
                // always "hide top level" when there's a domain
            }

            // the alias may be "foo/bar" or "/foo/bar"
            // there may be spaces as in "/foo/bar,  /foo/nil"
            // these should probably be taken care of earlier on

            alias = alias.RemoveLeadingAndTrailingSlashAndBackslash();            // .TrimStart('/');
            string[] urlNames = alias.Split(SlashChar, StringSplitOptions.RemoveEmptyEntries);

            // We don't care about the level anymore because we do it by child anyway

            string urlName;

            for (int i = 0; i < urlNames.Length; i++)
            {
                urlName = urlNames[i].RemoveLeadingAndTrailingSlashAndBackslash();

                // If the top level nodes are hidden the level is +1

                /*if (GlobalSettings.HideTopLevelNodeFromPath)
                 * {
                 *      // First I thought this is problematic...
                 *      // because we do not know if the backend user has overridden the urls e.g.
                 *      // normal: /test1/test2 means that "/" is the first one (because of hide top level node from path)
                 *      // overridden /test/test1/test2 so "/" is NOT the first one ("/" is overridden to "test")
                 *      // but that's ok because we hide the textboxes in the root if it's HideTopLevelNodeFromPath
                 *      // because then it makes no sense he overrides the url if it's "/"
                 *
                 *      if (level == 1)
                 *      {
                 *              xPathBuilder.Append("/"); // Needs to be descendants because we start at level 2
                 *      }
                 *
                 *      level++;
                 * }*/

                xpathBuilder.AppendFormat(XPathStringsDefinition.ChildDocumentByUrlName,
                                          Core.Constants.Conventions.Content.UrlAlias,
                                          urlName);
            }

            //LogHelper.Info<ContentFinderByUrlAlias>("xPath: " + xpathBuilder.ToString());

            // xPath is like: /root//* [@isDoc and ((@urlName = 'frontpage' or langProperty = 'frontpage') and @level = 1)]/* [@isDoc and ((@urlName = 'textpage' or langProperty = 'textpage') and @level = 2)] ...
            return(cache.GetSingleByXPath(xpathBuilder.ToString() /*, aliasVariable*/));
        }
 /// <summary>
 /// Gets a content resulting from an XPath query.
 ///
 /// </summary>
 /// <param name="preview">A value indicating whether to consider unpublished content.</param><param name="xpath">The XPath query.</param><param name="vars">Optional XPath variables.</param>
 /// <returns>
 /// The content, or null.
 /// </returns>
 ///
 /// <remarks>
 ///
 /// <para>
 /// If <param name="vars"/> is <c>null</c>, or is empty, or contains only one single
 ///             value which itself is <c>null</c>, then variables are ignored.
 /// </para>
 ///
 /// <para>
 /// The XPath expression should reference variables as <c>$var</c>.
 /// </para>
 ///
 /// </remarks>
 public virtual IPublishedContent GetSingleByXPath(bool preview, string xpath, params XPathVariable[] vars)
 {
     return(_contentCache.GetSingleByXPath(preview, xpath, vars));
 }
 private IPublishedContent TypedDocumentByXPath(string xpath, XPathVariable[] vars, ContextualPublishedContentCache cache)
 {
     var doc = cache.GetSingleByXPath(xpath, vars);
     return doc;
 }