Exemple #1
0
        private bool DoInstructionInternal(IDetailBlockStart blockStart,
                                           MetadataContextStack contextStack,
                                           List <IDom> newList,
                                           ref IDom lastPart)
        {
            if (blockStart == null)
            {
                return(false);
            }
            var match = blockStartMatch.Match(blockStart.Text);

            if (!match.Success)
            {
                return(false);
            }

            var         varName           = match.Groups[varNameKey].Value;
            var         loopOver          = match.Groups[loopOverKey].Value;
            IEnumerable propAsIEnumerable = GetPropList(contextStack, loopOver);
            var         blockContents     = blockStart.BlockContents;

            foreach (var item in propAsIEnumerable)
            {
                contextStack.Push(varName, item);
                Helper.RunOneLoop(blockContents, blockStart, contextStack, newList);
                contextStack.Pop();
            }
            lastPart = blockStart.BlockEnd;
            return(true);
        }
        public override void TemplateDone(IRoot sharedRoot, MetadataContextStack contextStack,
                                          IEnumerable <IRoot> retList)
        {
            foreach (var newRoot in retList)
            {
                var blockStart = newRoot
                                 .Descendants
                                 .OfType <IDetailBlockStart>()
                                 .Where(x => x.Text.Contains(Helper.MakeMarker(id)))
                                 .FirstOrDefault();
                if (blockStart == null)
                {
                    return;
                }

                var blockContents = blockStart.BlockContents;
                var blockContentsAsStemMembers = blockContents
                                                 .OfType <IStemMemberAndDetail>()
                                                 .Select(x => Helper.Copy <IDom>(x));
                if (blockContents.Count() != blockContentsAsStemMembers.Count())
                {
                    throw new InvalidOperationException();
                }
                newRoot.StemMembersAll.Clear();
                newRoot.StemMembersAll
                .AddOrMoveRange(blockContentsAsStemMembers.OfType <IStemMemberAndDetail>());
            }
        }
        public void RunOneLoop(IEnumerable <IDom> blockContents, IDetailBlockStart blockStart,
                               MetadataContextStack contextStack, List <IDom> newList)
        {
            var expansionFirstRunner = contextStack.GetValue(Constants.ExpansionFirstRunner) as ExpansionFirstTemplate;
            var member = blockContents.FirstOrDefault();
            var i      = 0;

            while (member != null && blockContents.Contains(member))
            {
                i++; if (i > 1000)
                {
                    throw new InvalidOperationException("Infinite loop detected");
                }
                var lastMember   = member;
                var copiedMember = member.GetType().GetMethod("Copy").Invoke(member, null) as IDom;
                var newMembers   = expansionFirstRunner.Update(copiedMember, contextStack, ref lastMember);
                // don't store the end region that matches this block start, because it's being removed
                newMembers = newMembers
                             .Where(x =>
                {
                    var block = x as IDetailBlockEnd;
                    if (block == null)
                    {
                        return(true);
                    }
                    return(block.GroupGuid != blockStart.GroupGuid);
                });
                newList.AddRange(newMembers);
                member = lastMember.NextSibling();
            }
        }
Exemple #4
0
 private void PushToContext(MetadataContextStack contextStack, string key, object value, bool fullName = false)
 {
     if (!fullName)
     {
         key = key.SubstringAfter(matchString + "_");
     }
     contextStack.Add(key, value);
 }
        //private Regex matchRegEx = new Regex(@"_xf_(?<part>[_\w]+?)(?<format>_as_\w+)*");

        public void DoAlteration(IDom item, MetadataContextStack contextStack)
        {
            ReplaceInName(item, contextStack);
            // TODO: Explore whether to cache this, the same items will be pounded
            IEnumerable <PropertyInfo> props = item.GetType().GetProperties();

            ReplaceInPropertiesNames(item, contextStack, props);
            ReplaceInText(item, contextStack, props);
            ReplaceInExpressions(item, contextStack, props);
        }
Exemple #6
0
        private bool HandleAttribute(IAttribute attribute, IHasAttributes item, MetadataContextStack contextStack, List <IDom> newList, ref IDom lastPart)
        {
            var value = Helper.GetBestMetadata <CodeFirstMetadata>(attribute, contextStack);
            var targetHasStructuredDocs = item as IHasStructuredDocumentation;

            if (value != null && targetHasStructuredDocs != null && !string.IsNullOrWhiteSpace(value.XmlCommentString))
            {
                // targetHasStructuredDocs.StructuredDocumentation = value.XmlCommentString ;
            }
            return(false);
        }
Exemple #7
0
        private static IEnumerable GetPropList(MetadataContextStack contextStack, string loopOver)
        {
            // TODO: Work out multi-part naming
            var metaVar           = loopOver.SubstringBefore(".");
            var metaProp          = loopOver.SubstringAfter(".");
            var metaVarValue      = contextStack.GetValue(metaVar);
            var metaPropValue     = metaVarValue.GetType().GetProperty(metaProp).GetValue(metaVarValue);
            var propAsIEnumerable = metaPropValue as IEnumerable;

            return(propAsIEnumerable);
        }
Exemple #8
0
 public override bool BeforeCopy(IDom sharedPart,
                                 MetadataContextStack contextStack,
                                 List <IDom> retList,
                                 ref IDom lastPart)
 {
     blockStartMatch = Helper.BuildSpecificParamRegex(id, loopOverKey, varNameKey);
     if (DoInstructionInternal(sharedPart as IDetailBlockStart, contextStack, retList, ref lastPart))
     {
         return(true);
     }
     return(false);
 }
 private bool DoInstruction(IDom part, MetadataContextStack contextStack, List <IDom> retList,
                            ref IDom lastPart)
 {
     // Can't use DoSomeInstruction becuase you can't pass ref to a lambda
     foreach (var instruction in availableInstructions)
     {
         if (instruction.BeforeCopy(part, contextStack, retList, ref lastPart))
         {
             return(true);
         }
     }
     return(false);
 }
        private void HandleAttributes(IHasAttributes newItemHasAttributes, MetadataContextStack contextStack)
        {
            if (newItemHasAttributes == null)
            {
                return;
            }
            var attributes   = newItemHasAttributes.Attributes;
            var xfAttributes = attributes.Where(x => x.Name.StartsWith("_xf_.")).ToList();

            foreach (var attribute in xfAttributes)
            {
                var name = attribute.Name.SubstringAfter("_xf_.");
            }
        }
        private bool HandleAttribute(IAttribute attribute, IHasAttributes item, MetadataContextStack contextStack, List <IDom> newList, ref IDom lastPart)
        {
            if (item == null)
            {
                return(false);
            }
            var value = Helper.GetBestMetadata <IHasAttributes>(attribute, contextStack);

            if (value != null)
            {
                item.Attributes.AddOrMoveAttributeRange(value.Attributes);
            }
            return(false);
        }
        private void ReplaceInName(IDom item, MetadataContextStack contextStack)
        {
            var partHasName = item as IHasName;

            if (partHasName != null)
            {
                var str = ReplacementString(partHasName.Name, contextStack);
                // don't reset if not needed, because that will enventually invalidate the model
                if (str != partHasName.Name)
                {
                    partHasName.Name = str;
                }
            }
        }
        private void ReplaceInText(IDom item, MetadataContextStack contextStack, IEnumerable <PropertyInfo> props)
        {
            var textProp = props.Where(x => x.Name == "Text").FirstOrDefault();

            if (textProp == null)
            {
                return;
            }
            var startString = textProp.GetValue(item) as string;
            var str         = ReplacementInsideString(startString, contextStack);

            if (str != startString)
            {
                textProp.SetValue(item, str);
            }
        }
 private void UpdateProperty(MetadataContextStack contextStack, IProperty newProperty)
 {
     if (newProperty != null)
     {
         // yes, ugly hack
         IDom dummy = null;
         if (newProperty.GetAccessor != null)
         {
             newProperty.GetAccessor = Update(newProperty.GetAccessor, contextStack, ref dummy).First() as IAccessor;
         }
         if (newProperty.SetAccessor != null)
         {
             newProperty.SetAccessor = Update(newProperty.SetAccessor, contextStack, ref dummy).First() as IAccessor;
         }
     }
 }
        public override bool BeforeCopy(IDom sharedPart,
                                        MetadataContextStack contextStack,
                                        List <IDom> retList,
                                        ref IDom lastPart)
        {
            // always return false, even if this does somethign, it doesn't take care of the underlying part
            var candidates = Helper.GetMatchingAttributes(sharedPart, Id);

            foreach (var attribute in candidates)
            {
                if (HandleAttribute(attribute, sharedPart as IHasAttributes, contextStack, retList, ref lastPart))
                {
                    return(false);
                }
            }
            return(false);
        }
        private void ReplaceInPropertiesNames(IDom item, MetadataContextStack contextStack, IEnumerable <PropertyInfo> props)
        {
            var filteredProps = props.Where(x => typeof(IHasName).IsAssignableFrom(x.PropertyType));

            foreach (var prop in filteredProps)
            {
                var propAsHasName = prop.GetValue(item) as IHasName;
                if (propAsHasName != null) // underlying value can be null
                {
                    var str = ReplacementString(propAsHasName.Name, contextStack);
                    if (str != propAsHasName.Name)
                    {
                        propAsHasName.Name = str;
                    }
                }
            }
            return;
        }
        private static string GetNewString(string input, MetadataContextStack contextStack, Match match)
        {
            if (!match.Success)
            {
                return(input);
            }
            var           format       = "";
            var           replaceWith  = match.Groups[1].Value;
            List <string> replaceParts = replaceWith
                                         .Split(new string[] { "_dot_" }, StringSplitOptions.None)
                                         .ToList();

            if (replaceParts.Last().Contains("_as_"))
            {
                var lastPart = replaceParts.Last();
                format   = lastPart.SubstringAfter("_as_");
                lastPart = lastPart.SubstringBefore("_as_");
                replaceParts.Remove(replaceParts.Last());
                replaceParts.Add(lastPart);
            }
            var lookup = contextStack.GetValue(replaceParts.First());

            if (lookup == null)
            {
                return(input);
            }
            replaceParts = replaceParts.Skip(1).ToList(); // get tail
            foreach (var part in replaceParts)
            {
                var lookupProperty = lookup.GetType().GetProperty(part);
                if (lookupProperty == null)
                {
                    return(input);
                }
                lookup = lookupProperty.GetValue(lookup);
                if (lookup == null)
                {
                    return(input);
                }
            }
            return(GetWithFormat(lookup.ToString(), format));
        }
Exemple #18
0
 private bool DoInstructionInternal(IPublicAnnotation publicAnnotation,
                                    MetadataContextStack contextStack,
                                    IEnumerable <IDom> newList)
 {
     if (publicAnnotation == null)
     {
         return(false);
     }
     if (!NameMatches(publicAnnotation.Name, true))
     {
         return(false);
     }
     foreach (var key in publicAnnotation.Keys)
     {
         var value = publicAnnotation.GetValue(key);
         PushToContext(contextStack, key, value, true);
         //contextStack.Current.AddValue(key, value);
     }
     return(true);
 }
        private void ReplaceInExpressions(IDom item, MetadataContextStack contextStack, IEnumerable <PropertyInfo> props)
        {
            var filteredProps = props.Where(x => typeof(IExpression).IsAssignableFrom(x.PropertyType));

            foreach (var prop in filteredProps)
            {
                var expr = prop.GetValue(item) as IExpression;
                if (expr == null)
                {
                    return;           // underlying value might be null
                }
                // TODO: Allow evaluating expressions in the following using the current language
                var str = ReplacementInsideString(expr.InitialExpressionString, contextStack);
                if (str != expr.InitialExpressionString)
                {
                    expr.InitialExpressionString = str;
                }
            }
            return;
        }
Exemple #20
0
        public override bool BeforeCopy(IDom sharedPart,
                                        MetadataContextStack contextStack,
                                        List <IDom> retList,
                                        ref IDom lastPart)
        {
            var ret = new List <IDom>(); // block changes to the passed list

            if (DoInstructionInternal(sharedPart as IPublicAnnotation, contextStack, ret))
            {
                return(true);
            }
            if (DoInstructionInternal(sharedPart as IDeclarationStatement, contextStack, ret))
            {
                return(true);
            }
            if (DoInstructionInternal(sharedPart as IField, contextStack, ret))
            {
                return(true);
            }
            return(false);
        }
 private void UpdateContainer(MetadataContextStack contextStack, IContainer newContainer)
 {
     if (newContainer != null)
     {
         var member        = newContainer.GetMembers().FirstOrDefault();
         var i             = 0;
         var newMemberList = new List <IDom>();
         while (member != null)
         {
             i++; if (i > 1000)
             {
                 throw new InvalidOperationException("Infinite loop detected");
             }
             var lastMember = member;
             newMemberList.AddRange(Update(member, contextStack, ref lastMember));
             var nextMember = lastMember.NextSibling();
             member = nextMember;
         }
         UpdateItem(newContainer, newMemberList);
     }
 }
        public T GetBestMetadata <T>(IAttribute attribute, MetadataContextStack contextStack)
            where T : class
        {
            var attributeValue = attribute.AttributeValues.FirstOrDefault();
            T   value          = null;

            if (attributeValue != null)
            {
                var metaName = attributeValue.Value.ToString();
                value = contextStack.GetValue(metaName) as T;
            }
            else
            {
                // Convention
                var tuple = contextStack.RecentOfType <T>();
                if (tuple != null)
                {
                    value = contextStack.RecentOfType <T>().Item2;
                }
            }
            return(value);
        }
        private string ReplacementInsideString(string input, MetadataContextStack contextStack)
        {
            var ret          = input;
            var matches      = matchRegEx.Matches(input);
            var replacements = new List <Tuple <int, int, string> >();

            for (int i = 0; i < matches.Count; i++)
            {
                var match  = matches[i];
                var str    = match.Value;
                var newStr = GetNewString(str, contextStack, match);
                replacements.Add(Tuple.Create(match.Index, match.Length, newStr));
                //if (newStr != str) ret = ret.Replace(str, newStr);
            }
            replacements.Reverse();
            foreach (var replacement in replacements)
            {
                var start = ret.Substring(0, replacement.Item1);
                var end   = ret.Substring(replacement.Item1 + replacement.Item2);
                ret = start + replacement.Item3 + end;
            }
            return(ret);
        }
Exemple #24
0
        private bool DoInstructionInternal(IDeclarationStatement declaration,
                                           MetadataContextStack contextStack,
                                           IEnumerable <IDom> newList)
        {
            if (declaration == null)
            {
                return(false);
            }
            if (!NameMatches(declaration.Name))
            {
                return(false);
            }
            var    name  = declaration.Name;
            object value = null;

            if (declaration.Initializer != null)
            {
                // TODO: Allow evaluating expressions in the following using the current language
                value = declaration.Initializer.InitialExpressionString.ToString();
            }
            PushToContext(contextStack, name, value);
            //contextStack.Current.AddValue(name, value);
            return(true);
        }
        internal IEnumerable <IDom> Update(IDom part, MetadataContextStack contextStack, ref IDom lastPart)
        {
            var newItem = helper.Copy <IDom>(part);

            // Returning true from DoInstruction means the part is fully handled and no more should be done
            {
                var newMemberList = new List <IDom>();
                if (DoInstruction(part, contextStack, newMemberList, ref lastPart))
                {
                    return(newMemberList.OfType <IDom>());
                }
            }

            // TODO: I'm not convinced there are three actions, it may be 3 instructions.
            DoReplacements(newItem, contextStack);
            DoAfterCopy(newItem);
            HandleAttributes(newItem as IHasAttributes, contextStack);

            // Manage children. Property has a special case of multiple children. Events may be the same.
            UpdateProperty(contextStack, newItem as IProperty);
            UpdateContainer(contextStack, newItem as IContainer);

            return(new IDom[] { newItem });
        }
 public virtual bool BeforeCopy(IDom sharedPart, MetadataContextStack contextStack, List <IDom> retList, ref IDom lastPart)
 {
     return(false);
 }
 public string PushToContext(string key, object value, MetadataContextStack contextStack)
 {
     contextStack.Current.AddValue(key, value);
     return(key);
 }
 public virtual void AfterCopy(IDom newPart, MetadataContextStack contextStack)
 {
 }
        private string ReplacementString(string input, MetadataContextStack contextStack)
        {
            var match = matchRegEx.Match(input);

            return(GetNewString(input, contextStack, match));
        }
 private void DoReplacements <T>(T newItem, MetadataContextStack metaContextStack)
     where T : IDom
 {
     replacementAlteration.DoAlteration(newItem, metaContextStack);
     // TODO: Do Replacements
 }