示例#1
0
 public bool HasPrefix(XPathExpr prefix)
 {
     if (prefix == null)
     {
         throw new ArgumentNullException("prefix");
     }
     return(this.ToString().StartsWith(prefix));
 }
示例#2
0
        /// <summary>
        /// Converts path <paramref name="oldPath"/>
        /// into a path relative to <paramref name="context"/>.<see cref="XsltGeneratorContext.ProcessedPath"/>.
        /// (and also relative to <paramref name="context"/>.<see cref="XsltGeneratorContext.ContentGroupPath"/> if it is used).
        /// </summary>
        /// <param name="context">current generator context</param>
        /// <param name="oldPath">projected path</param>
        /// <returns>relative xpath expression</returns>
        public static XPathExpr GroupAwareProjectXPathWithoutAttributeCorrection(XsltGeneratorContext context, XPathExpr oldPath)
        {
            if (context.InGroup && oldPath.HasPrefix(context.ContentGroupPath))
            {
                oldPath = oldPath.InsertAfterPrefix(context.ContentGroupPath, "/$cg");
            }
            XPathExpr tmp = ProjectXPath(context.ProcessedPath, oldPath, context.InGroup);

            return(tmp);
        }
示例#3
0
 public bool Equals(XPathExpr other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other._expr, _expr));
 }
示例#4
0
        internal XPathExpr NodeToProcessedPath(PSMElement node)
        {
            Version oldVersion = ChangeSet.OldVersion;

            if (!InGroup)
            {
                return(XPathHelper.GetXPathForNode(node, oldVersion));
            }
            else
            {
                XPathExpr path = XPathHelper.GetXPathForNode(node, oldVersion).Append("/$cg");
                //if (InGroup/* && ProcessedPath.HasPrefix(ContentGroupPath)*/)
                {
                    Debug.Assert(path.HasPrefix(ContentGroupPath));
                    Debug.Assert(path == (!ContentGroupPath.ToString().EndsWith("/$cg") ? ContentGroupPath.Append("/$cg") : ContentGroupPath));
                    //path = path.InsertAfterPrefix(ContentGroupPath, "/$cg");
                }
                return(path);
            }
        }
示例#5
0
        /// <summary>
        /// Adds '[1]' predicate to each step (thus the xpath expression only selects the first child in each step).
        /// </summary>
        /// <param name="xpath">xpath</param>
        /// <param name="leaveLastStep">if set to <c>true</c> leaves the last step intact.</param>
        /// <returns></returns>
        public static XPathExpr AllwaysReferenceFirstChild(XPathExpr xpath, bool leaveLastStep)
        {
            string tmp = xpath.ToString();

            System.Text.RegularExpressions.Regex r =
                new System.Text.RegularExpressions.Regex("/");
            tmp = tmp.Replace("$cg/", "__CG__");
            string tmp2 = r.Replace(tmp, "[1]/", int.MaxValue, 1);

            tmp2 = tmp2.Replace("__CG__", "$cg/");
            int p = tmp2.LastIndexOf("/");

            if (!leaveLastStep && !tmp2.EndsWith("/") && tmp2[p + 1] != '@' && tmp2[p + 1] != '[')
            {
                tmp2 = tmp2 + "[1]";
            }
            //!tmp2.StartsWith("/") && !tmp2.StartsWith("@") && !tmp2.EndsWith(XPathExpr.CurrentGroupVariableExpr))
            //tmp2 = tmp2.Insert(tmp.Contains("/") ? tmp2.IndexOf("/") : tmp2.Length, "[1]");
            return(new XPathExpr(tmp2));
        }
示例#6
0
        /// <summary>
        /// Converts path to <paramref name="psmElementNewVersion"/>
        /// into a path relative to <paramref name="context"/>.<see cref="XsltGeneratorContext.ProcessedPath"/>.
        /// (and also relative to <paramref name="context"/>.<see cref="XsltGeneratorContext.ContentGroupPath"/> if it is used).
        /// If context is in group (<see cref="XsltGeneratorContext"/>.<see cref="XsltGeneratorContext.InGroup"/>) which
        /// has group attributes and <paramref name="psmElementNewVersion"/> is one of these attributes, the returned path
        /// is modified to use the '$attributes' variable.
        /// </summary>
        /// <param name="context">current generator context</param>
        /// <param name="psmElementNewVersion">element for which the path is projected</param>
        /// <param name="oldVersion">old version, used to determine the path to <paramref name="psmElementNewVersion"/> in the
        /// old version of the diagram</param>
        /// <returns>relative xpath expression</returns>
        public static XPathExpr GroupAwareProjectXPath(XsltGeneratorContext context, PSMElement psmElementNewVersion, Version oldVersion)
        {
            XPathExpr oldPath = ((PSMElement)psmElementNewVersion.GetInVersion(oldVersion)).XPathE();

            if (context.InGroup && oldPath.HasPrefix(context.ContentGroupPath))
            {
                oldPath = oldPath.InsertAfterPrefix(context.ContentGroupPath, "/$cg");
            }
            XPathExpr tmp = ProjectXPath(context.ProcessedPath, oldPath, context.InGroup);

            if (context.InGroup && context.ContentGroupAttributes != null && psmElementNewVersion is PSMAttribute &&
                context.ContentGroupAttributes.Contains((PSMAttribute)psmElementNewVersion))
            {
                if (tmp.ToString().StartsWith(XPathExpr.CurrentGroupVariableExpr))
                {
                    tmp = new XPathExpr(tmp.ToString().Replace(XPathExpr.CurrentGroupVariableExpr, "$attributes"));
                }
            }

            return(tmp);
        }
示例#7
0
 public static bool IsNullOrEmpty(XPathExpr expr)
 {
     return(expr == null || expr.IsEmpty());
 }
示例#8
0
 public XPathExpr InsertAfterPrefix(XPathExpr prefix, string insertedExpr)
 {
     Debug.Assert(HasPrefix(prefix));
     return(new XPathExpr(this.ToString().Insert(prefix.ToString().Length, insertedExpr)));
 }
示例#9
0
 public static XPathExpr DummyUpStep(XPathExpr xPathExpr)
 {
     return(new XPathExpr("..{0}", xPathExpr._expr.Substring(xPathExpr._expr.LastIndexOf("/"))));
 }
示例#10
0
 public XPathExpr(XPathExpr expression)
 {
     _expr = expression._expr;
 }
示例#11
0
        //public enum EProjectionResultType
        //{
        //    Ancestor,
        //    Descendant,
        //    Self,
        //    Absolute
        //}

        /// <summary>
        /// Converts <paramref name="pathTo"/>
        /// into a path relative to <paramref name="currentPath"/>.
        /// </summary>
        /// <param name="pathTo">projected path</param>
        /// <param name="currentPath">currentPath</param>
        /// <param name="inGroup">flag if current path can is group path</param>
        /// <returns>relative xpath expression</returns>
        private static XPathExpr ProjectXPath(XPathExpr currentPath, XPathExpr pathTo, bool inGroup)
        {
            //EProjectionResultType projectionType = EProjectionResultType.Absolute;
            if (currentPath == null)
            {
                return(pathTo);
            }
            char[]   separator  = new[] { '/' };
            string[] cn_parts   = currentPath.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);
            string[] path_parts = pathTo.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);

            int countSame = 0;

            while (cn_parts.Length > countSame && path_parts.Length > countSame && cn_parts[countSame] == path_parts[countSame])
            {
                countSame++;
            }

            string res       = String.Empty;
            bool   cgHandled = false;

            for (int j = countSame; j < path_parts.Length; j++)
            {
                // removed and replaced by variable construction
                if (inGroup && !cgHandled && cn_parts[j - 1] == XPathExpr.CurrentGroupVariableExpr)
                {
                    string name = path_parts[j];
                    // HACK: for attribute matching in groups @ is stripped from the name
                    name = name.TrimStart('@');
                    res += string.Format("$cg[name() = '{0}']", name);
                    //projectionType = EProjectionResultType.Descendant;
                    if (j < path_parts.Length - 1)
                    {
                        res += "/";
                    }
                    cgHandled = true;
                }
                else
                {
                    res += path_parts[j];
                    //projectionType = EProjectionResultType.Descendant;
                    if (j < path_parts.Length - 1)
                    {
                        res += "/";
                    }
                }
            }
            if (countSame > 0 && path_parts.Length == countSame)
            {
                //projectionType = EProjectionResultType.Self;
                // up
                for (int j = countSame; j < cn_parts.Length; j++)
                {
                    res += "../";
                }
                // and down again
                for (int j = countSame; j < cn_parts.Length; j++)
                {
                    res += cn_parts[j];
                    if (j < cn_parts.Length - 1)
                    {
                        res += "/";
                    }
                }
            }



            return(new XPathExpr(res));
        }
示例#12
0
        /// <summary>
        /// Performs <see cref="GroupAwareProjectXPath"/> and substitutes steps in the result path according
        /// to structural representative information. (So the path is relative to the structural representative)
        /// </summary>
        /// <param name="context">The generator context.</param>
        /// <param name="element">The element.</param>
        /// <param name="structuralRepresentativeReplacement">The structural representative replacement.</param>
        /// <returns></returns>
        public static XPathExpr GroupAwareProjectXPathWithSRSubstitution(XsltGeneratorContext context, PSMElement element, IFromStructuralRepresentative structuralRepresentativeReplacement)
        {
            XPathExpr expr;

            if (structuralRepresentativeReplacement != null)
            {
                var cp = context.ProcessedPath;

                //if (PSMTreeIterator.AreInTheSamePSMTree(structuralRepresentativeReplacement.RepresentedPSMClass, structuralRepresentativeReplacement.StructuralRepresentative))


                PSMElement representedOldAnc = PSMTreeIterator.GetSignificantAncestorOrSelf((PSMClass)structuralRepresentativeReplacement.RepresentedPSMClass.GetInVersion(context.ChangeSet.OldVersion));
                PSMElement structuralRepresentativeOldAnc = PSMTreeIterator.GetSignificantAncestorOrSelf((PSMClass)structuralRepresentativeReplacement.StructuralRepresentative.GetInVersion(context.ChangeSet.OldVersion));

                if (representedOldAnc != null)
                {
                    context.ProcessedPath = GetXPathForNode(representedOldAnc, context.ChangeSet.OldVersion);
                }
                else
                {
                    context.ProcessedPath = new XPathExpr("<virt-root>");
                    if (context.InGroup)
                    {
                        context.ProcessedPath = context.ProcessedPath.Append("/$cg");
                    }
                }

                string from = representedOldAnc != null?XsltTemplateNameManager.GetElementNameForSignificantElement(representedOldAnc) : null;

                string to = structuralRepresentativeOldAnc != null?XsltTemplateNameManager.GetElementNameForSignificantElement(structuralRepresentativeOldAnc) : null;

                XPathExpr e = null;

                if (from != null && to != null &&
                    !(context.ProcessedPath.ToString().EndsWith(from) && cp.ToString().EndsWith(to)))
                {
                    e = ProjectXPath(cp, context.ProcessedPath, false);
                    if (!XPathExpr.IsNullOrEmpty(e) && !String.IsNullOrEmpty(to) && !String.IsNullOrEmpty(from))
                    {
                        if (e.ToString().Contains(from))
                        {
                            e = new XPathExpr(e.ToString().Replace(from, to));
                        }
                    }
                }
                expr = GroupAwareProjectXPath(context, element, context.ChangeSet.OldVersion);

                if (!XPathExpr.IsNullOrEmpty(e))
                {
                    // ReSharper disable PossibleNullReferenceException
                    expr = e.Append("/" + expr);
                }
                // ReSharper restore PossibleNullReferenceException

                context.ProcessedPath = cp;
            }
            else
            {
                expr = GroupAwareProjectXPath(context, element, context.ChangeSet.OldVersion);
            }

            return(expr);
        }