Ejemplo n.º 1
0
 private static bool IsX12RepetitionSeparator(this ParseNode parseNode)
 {
     return(parseNode.Parent != null && parseNode.Parent.Name == "S_ISA" &&
            parseNode.Name == "D_726_11");
 }
Ejemplo n.º 2
0
 internal static ParseNode Root(this ParseNode parseNode)
 {
     return(parseNode.Ancestors().Last());
 }
Ejemplo n.º 3
0
        internal static void ParseSegment(this ParseNode parseNode, string line, Separators separators)
        {
            if (parseNode == null)
            {
                throw new ArgumentNullException("parseNode");
            }
            if (string.IsNullOrEmpty(line))
            {
                throw new ArgumentNullException("line");
            }
            if (separators == null)
            {
                throw new ArgumentNullException("separators");
            }

            if (parseNode.Prefix != Prefixes.S)
            {
                throw new Exception(string.Format("Only segments are supported: {0}", parseNode.Name));
            }

            var dataElementsGrammar = ParseNode.BuldTree(parseNode.Type, false).Children;
            var dataElements        = line.GetDataElements(separators);

            for (var deIndex = 0; deIndex < dataElements.Length; deIndex++)
            {
                var currentDataElement = dataElements[deIndex];
                if (string.IsNullOrEmpty(currentDataElement))
                {
                    continue;
                }
                if (dataElementsGrammar.Count <= deIndex)
                {
                    throw new ParserException(
                              string.Format(
                                  "More data elements ({0}) were found in segment {1} than in the rule class ({2}).",
                                  dataElements.Length, line, dataElementsGrammar.Count));
                }
                var currentDataElementGrammar = dataElementsGrammar.ElementAt(deIndex);

                var repetitions = currentDataElementGrammar.IsX12RepetitionSeparator()
                    ? new[] { currentDataElement }
                    : currentDataElement.GetRepetitions(separators);
                foreach (var repetition in repetitions)
                {
                    var childParseNode = parseNode.AddChild(currentDataElementGrammar.Type,
                                                            currentDataElementGrammar.Name,
                                                            currentDataElementGrammar.Prefix == Prefixes.D ? repetition.UnEscapeLine(separators) : null);

                    if (currentDataElementGrammar.Prefix != Prefixes.C)
                    {
                        continue;
                    }

                    var componentDataElementsGrammar = currentDataElementGrammar.Children;
                    var componentDataElements        = repetition.GetComponentDataElements(separators);
                    for (var cdeIndex = 0; cdeIndex < componentDataElements.Length; cdeIndex++)
                    {
                        var currentComponentDataElement = componentDataElements[cdeIndex];
                        if (string.IsNullOrEmpty(currentComponentDataElement))
                        {
                            continue;
                        }
                        if (componentDataElementsGrammar.Count <= cdeIndex)
                        {
                            throw new ParserException(
                                      string.Format("More data elements ({0}) were found in segment {1} than in the rule class {2}.",
                                                    componentDataElements.Length, currentComponentDataElement, componentDataElementsGrammar.Count));
                        }
                        var currentComponentDataElementGrammar = componentDataElementsGrammar.ElementAt(cdeIndex);

                        childParseNode.AddChild(currentComponentDataElementGrammar.Type,
                                                currentComponentDataElementGrammar.Name,
                                                currentComponentDataElement.UnEscapeLine(separators));
                    }
                }
            }
        }
Ejemplo n.º 4
0
        internal static object ToInstance(this ParseNode parseNode)
        {
            if (parseNode == null)
            {
                throw new ArgumentNullException("parseNode");
            }

            var root          = Activator.CreateInstance(parseNode.Type);
            var instanceLinks = new Dictionary <string, object> {
                { parseNode.Path, root }
            };
            var stack     = new Stack <ParseNode>(new[] { parseNode });
            var listTypes = new Dictionary <string, IList>();

            while (stack.Any())
            {
                var currentNode = stack.Pop();

                var    path = currentNode.Path;
                object currentInstance;
                if (!instanceLinks.TryGetValue(path, out currentInstance))
                {
                    throw new Exception(string.Format("Instance not set for path: {0}", currentNode.Path));
                }

                foreach (var nodeChild in currentNode.Children)
                {
                    var propertyInfo = currentNode.Type.GetProperty(nodeChild.Name);
                    if (nodeChild.Prefix == Prefixes.D)
                    {
                        propertyInfo.SetValue(currentInstance, propertyInfo.GetPropertyValue(nodeChild.Value), null);
                    }
                    else
                    {
                        object child;
                        if (propertyInfo.IsList())
                        {
                            var   repPath = nodeChild.Parent.Path + nodeChild.Name;
                            IList list;
                            if (!listTypes.TryGetValue(repPath, out list))
                            {
                                list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(nodeChild.Type));
                                propertyInfo.SetValue(currentInstance, list, null);

                                listTypes.Add(repPath, list);
                            }

                            child = Activator.CreateInstance(nodeChild.Type);
                            list.Add(child);
                        }
                        else
                        {
                            child = Activator.CreateInstance(nodeChild.Type);
                            propertyInfo.SetValue(currentInstance, child, null);
                        }

                        instanceLinks.Add(nodeChild.Path, child);
                        stack.Push(nodeChild);
                    }
                }

                instanceLinks.Remove(path);
            }

            return(root);
        }
Ejemplo n.º 5
0
        internal static IEnumerable <ParseNode> AncestorsToIntersection(this ParseNode segment, ParseNode lastFoundSegment)
        {
            if (segment.Prefix != Prefixes.S)
            {
                throw new ParserException("Not a segment " + segment.Name);
            }

            var lastParents = lastFoundSegment.Ancestors();
            var parents     = segment.Ancestors().ToList();
            var intersect   = parents.Select(n => n.Name).Intersect(lastParents.Select(n => n.Name)).ToList();
            var result      = parents.TakeWhile(parent => parent.Name != intersect.First()).Reverse().ToList();

            if (!result.Any() && segment.IsTrigger)
            {
                result.Add(segment.Parent);
            }

            result.Add(segment);

            return(result);
        }