Ejemplo n.º 1
0
        /// <summary>
        /// Performs encoding transformation for each <c>XmlElement</c> of <c>App.config</c> where <c>Encode</c> transform is applied for.
        /// </summary>
        protected override void Apply()
        {
            //If target node has no attributes - skip the transform:
            if (TargetNode.Attributes == null)
            {
                return;
            }

            //Getting list of passed XPath arguments to this Encode instance:
            var encodeXPathArguments = new List <string>();

            if (Arguments != null)
            {
                encodeXPathArguments.AddRange(Arguments);
            }

            //Adding DefaultAttributeName to XPath arguments if it's not present:
            if (!encodeXPathArguments.Contains(DefaultAttributeName))
            {
                encodeXPathArguments.Add(DefaultAttributeName);
            }

            //Looping thru TargetNodes
            foreach (var targetNode in TargetNodes.OfType <XmlElement>())
            {
                //For each TargetNode looping thru encodeXPathArguments
                foreach (var arg in encodeXPathArguments)
                {
                    //Getting TargetNode.XmlNode by XPath arg:
                    var destinationNode = targetNode.SelectSingleNode(arg);

                    if (destinationNode == null)
                    {
                        continue;
                    }

                    //TransformNode.XmlNode by XPath arg:
                    var applyTransformNode = TransformNode.SelectSingleNode(arg);

                    //Determine which XmlNode.InnerText to encode:
                    string valueToEncode =
                        applyTransformNode == null || string.IsNullOrEmpty(applyTransformNode.InnerText)
                            ? destinationNode.InnerText     //Getting default destination XmlNode.InnerText
                            : applyTransformNode.InnerText; //Getting XmlNode.InnerText specified in App.config transformer

                    if (string.IsNullOrEmpty(valueToEncode))
                    {
                        continue;
                    }

                    //If valueToEncode is OK - then encrypt it and assign to destination XmlNode.InnerText:
                    destinationNode.InnerText = _encryptorDecryptor.Encrypt(valueToEncode);
                }
            }
        }