Example #1
0
        public string Transform(IDocument source, TransformationCollection transformations)
        {
            foreach (var transformation in transformations)
            {
                if (String.IsNullOrWhiteSpace(transformation.Key) == false)
                {
                    source.ReplaceKey(transformation.Key.Trim().Split('/').Select(x => x.Trim()).ToArray(), transformation.Value);
                }
                else
                {
                    throw new ArgumentException("Wrong transformation key.", nameof(transformations));
                }
            }

            return(source.ToString());
        }
Example #2
0
        public string Transform(Type documentType, string source, TransformationCollection transformations)
        {
            IDocument document;

            try
            {
                document = (IDocument)Activator.CreateInstance(documentType, source);
            }
            catch (TargetInvocationException ex)
            {
                if (ex.InnerException.GetType() == typeof(ArgumentException))
                {
                    throw new ArgumentException("Unknown document type.", nameof(source), ex);
                }

                throw;
            }

            return(Transform(document, transformations));
        }
Example #3
0
        public string Transform(IDocument source, TransformationCollection transformations)
        {
            if (transformations.KeysToRemove != null)
            {
                foreach (var key in transformations.KeysToRemove)
                {
                    if (String.IsNullOrWhiteSpace(key))
                    {
                        throw new ArgumentNullException("Transformation key is empty.", nameof(transformations));
                    }

                    source.RemoveKey(TransformationKey.Split(key));
                }
            }

            foreach (var transformation in transformations)
            {
                var transformationKey = new TransformationKey(transformation.Key);

                switch (transformationKey.Type)
                {
                case TransformationKeyType.Remove:
                    source.RemoveKey(transformationKey.Path);
                    break;

                case TransformationKeyType.AddToArray:
                    source.AddElementToArray(transformationKey.Path, transformation.Value);
                    break;

                case TransformationKeyType.Replace:
                    source.ReplaceKey(transformationKey.Path, transformation.Value);
                    break;

                default:
                    throw new InvalidOperationException($"Action is not defined for {nameof(TransformationKeyType)}.{transformationKey.Type}");
                }
            }

            return(source.ToString());
        }
        public string Transform(IDocument source, TransformationCollection transformations)
        {
            if (transformations.KeysToRemove != null)
            {
                foreach (var key in transformations.KeysToRemove)
                {
                    if (String.IsNullOrWhiteSpace(key))
                    {
                        throw new ArgumentNullException("Transformation key is empty.", nameof(transformations));
                    }

                    source.RemoveKey(SplitKey(key));
                }
            }

            foreach (var transformation in transformations)
            {
                if (String.IsNullOrWhiteSpace(transformation.Key))
                {
                    throw new ArgumentException("Transformation key is empty.", nameof(transformations));
                }

                if (transformation.Key.StartsWith("#"))
                {
                    source.RemoveKey(SplitKey(transformation.Key.TrimStart('#')));
                }
                else if (RemoveEndingRegex.Replace(transformation.Key, String.Empty).EndsWith("[]"))
                {
                    source.AddElementToArray(SplitKey(transformation.Key), transformation.Value);
                }
                else
                {
                    source.ReplaceKey(SplitKey(transformation.Key), transformation.Value);
                }
            }

            return(source.ToString());
        }
Example #5
0
        public string Transform(IDocument source, TransformationCollection transformations)
        {
            foreach (var key in transformations.RemoveKeys)
            {
                if (String.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentException("Blank transformation key.", nameof(transformations));
                }
                source.RemoveKey(SplitKey(key));
            }

            foreach (var transformation in transformations)
            {
                if (String.IsNullOrWhiteSpace(transformation.Key))
                {
                    throw new ArgumentException("Blank transformation key.", nameof(transformations));
                }

                source.ReplaceKey(SplitKey(transformation.Key), transformation.Value);
            }

            return(source.ToString());
        }
Example #6
0
        public string Transform <TDocument>(string source, TransformationCollection transformations) where TDocument : IDocument
        {
            var document = (TDocument)Activator.CreateInstance(typeof(TDocument), source);

            return(Transform(document, transformations));
        }