Exemplo n.º 1
0
        private void WebServicePropertyChangedEventHandler(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "InputString")
            {
                this.CheckSoap();

                if (this._inputDocument != null)
                {
                    SoapProtocolImporter t = new SoapProtocolImporter();

                    presentation.Dispatcher.Invoke(DispatcherPriority.Normal, (SendOrPostCallback) delegate
                    {
                        this.presentation.NameSpacesTable.Clear();

                        this.presentation.SoapInputItem = null;
                        this.presentation.SoapInputItem = new TreeViewItem();
                        this.presentation._animationTreeView.Items.Clear();
                        presentation.SoapInputItem.IsExpanded = true;
                        StackPanel panel1  = new StackPanel();
                        panel1.Orientation = System.Windows.Controls.Orientation.Horizontal;
                        TextBlock elem1    = new TextBlock();
                        elem1.Text         = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
                        panel1.Children.Insert(0, elem1);
                        this.presentation.SoapInputItem.Header = panel1;
                        this.presentation.CopyXmlToTreeView(this._inputDocument.SelectSingleNode("/*"), this.presentation.SoapInputItem);
                        //   this.presentation.ResetSoapInputItem();
                        this.presentation._animationTreeView.Items.Add(this.presentation.SoapInputItem);
                        this.presentation.FindTreeViewItem(presentation.SoapInputItem, "Envelope", 1).IsExpanded  = true;
                        this.presentation.FindTreeViewItem(presentation.SoapInputItem, "Header", 1).IsExpanded    = true;
                        this.presentation.FindTreeViewItem(presentation.SoapInputItem, "Security", 1).IsExpanded  = true;
                        this.presentation.FindTreeViewItem(presentation.SoapInputItem, "Signature", 1).IsExpanded = true;
                        this.presentation.FindTreeViewItem(presentation.SoapInputItem, "Body", 1).IsExpanded      = true;
                        this.presentation._animationTreeView.Items.Refresh();
                    }, null);
                }
            }
        }
Exemplo n.º 2
0
    public override void ImportMethod(
        CodeAttributeDeclarationCollection metadata)
    {
        SoapProtocolImporter importer = ImportContext;
        // Check whether the XML specified in the YMLOperationBinding
        // is in the service description.
        YMLOperationBinding yml =
            (YMLOperationBinding)importer.OperationBinding.Extensions.Find(
                typeof(YMLOperationBinding));

        if (yml != null)
        {
            // Only apply the YMLAttribute to the method when the XML should
            // be reversed.
            if (yml.Reverse)
            {
                CodeAttributeDeclaration attr =
                    new CodeAttributeDeclaration(typeof(YMLAttribute).FullName);
                attr.Arguments.Add(
                    new CodeAttributeArgument(new CodePrimitiveExpression(true)));
                metadata.Add(attr);
            }
        }
    }
        /// <summary>
        ///     Method called by the tool importing the wsdl whenever a operation/web method is found in the wsdl file.
        /// </summary>
        /// <remarks>
        ///     <para>
        ///         This is called once per binding in the wsdl. Each binding is associated with a different
        ///         web method. ImportMethod add attributes to each web method and the web service class
        ///         to add client (proxy) validation capabilities.
        ///     </para>
        ///     <para>
        ///         This class depends on the wsdl file containing the definition of
        ///         just a single service. At present the wsdl files generated by
        ///         the WebService class create wsdl files that meet this restriction.
        ///     </para>
        /// </remarks>
        /// <param name="metadata">Code DOM being created by tool</param>
        public override void ImportMethod(CodeAttributeDeclarationCollection metadata)
        {
            Argument.Assert.IsNotNull(metadata, "metadata");

            // get the ImportContext, it provides access
            // to Wsdl File
            SoapProtocolImporter importer = ImportContext;

            // if there are no validation attributes in any bindings then no
            // DevInterop.Web.Service attributes should be added to
            // generated code. This prevents unneeded attributes from
            // being added to code that does not make use of them
            if (false == CheckedForValidation)
            {
                // this is what does the checking for validation
                CheckedForValidation = true;
                // check every binding in wsdl file for this service
                foreach (Binding binding in importer.Service.ServiceDescription.Bindings)
                {
                    // check every operation in the binding
                    foreach (OperationBinding operationBinding in binding.Operations)
                    {
                        // check to see if the binding has a validation extensibility element
                        HasValidation = null != operationBinding.Input.Extensions.Find("validation", ValidationNamespaces.DevInterop);
                        if (HasValidation)
                        {
                            break;
                        }
                    }
                    if (HasValidation)
                    {
                        break;
                    }
                }
            }
            if (false == HasValidation)
            {
                return;
            }
            // do web service (rather than web method) specific code
            // generation when first method is processed
            if (MethodPos == 0)
            {
                // add a comment to the class to indicate the source of the extension creating the code
                importer.CodeTypeDeclaration.Comments.Add(new CodeCommentStatement("Proxy validation code generated by http://www.DevInterop.net"));
                // Add an attribute to the web service for each schema
                // found in the wsdl file. This will be used for client side validation
                // of messages
                foreach (XmlSchema s in importer.ConcreteSchemas)
                {
                    // string builder is needed to hold serialzation fo schems
                    StringBuilder sb = new StringBuilder();
                    // text writer wraped around string builder captures schema
                    XmlTextWriter xwr = new XmlTextWriter(new StringWriter(sb));
                    // write schema to string builder
                    s.Write(xwr);
                    // convert string builder rto string
                    string schema = sb.ToString();
                    // strip off xml declaration, it is not needed
                    // and its encoding may cause problems
                    schema = schema.Substring(schema.IndexOf("?>") + 2);
                    // build an attribute (a ValdiationSchemaValueAttribute) to be
                    // inserted into the code dom
                    CodeAttributeDeclaration attr =
                        new CodeAttributeDeclaration(typeof(ValidationSchemaValueAttribute).FullName);
                    // build an unnamed attribute argument.
                    CodeAttributeArgument arg =
                        new CodeAttributeArgument(string.Empty,
                                                  // the value of this argument is the string which represents the schema
                                                  new CodePrimitiveExpression(schema));
                    // add this argument to the attribute
                    attr.Arguments.Add(arg);
                    // add this attribute to the code dom. This will add it
                    // to the web service class being processed
                    importer.CodeTypeDeclaration.CustomAttributes.Add(attr);
                }
                // The first binding is being looked at to find the namespace
                // declaration extensibility elements in the binding that contains it.
                // In the current implementation only the binding for the
                // first operation found is checked.
                // This would have to be enhanced
                // to support multiple services defined in a single wsdl file
                Binding    firstBinding = importer.Binding;
                XmlElement ov           = firstBinding.Extensions.Find("validationNS", ValidationNamespaces.DevInterop);
                // no need to process if no validation extensibiltity element exists
                if (null != ov)
                {
                    // make an XPathDocument out of the XmlElement that was found.
                    XPathDocument xpdoc = new XPathDocument(new XmlNodeReader(ov));
                    // now make a navigator to use to find things
                    XPathNavigator xpnav = xpdoc.CreateNavigator();
                    // A namespace manager will be required to use XPathExpressions
                    XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xpnav.NameTable);
                    // add the DevInterop.Web.Services to the collection of namespaces
                    namespaceManager.AddNamespace("dm", ValidationNamespaces.DevInterop);
                    // compile an XPath expression that will find all of the
                    // namespace bindings in the extensibility element
                    XPathExpression exp = xpnav.Compile("/dm:validationNS/dm:namespaces/dm:namespace");
                    // add the namespace to the expression
                    exp.SetContext(namespaceManager);
                    // find all of the namespace bindings
                    XPathNodeIterator itr = xpnav.Select(exp);
                    // iterate through all of the namespace bindings
                    // that were found
                    while (itr.MoveNext())
                    {
                        // clone the iterator so it is not moved while you look
                        // for parts of the namespace binding
                        XPathNodeIterator assert = itr.Clone();
                        // first child of the binding is the prefix
                        assert.Current.MoveToFirstChild();
                        // create an AssertNamspaceBindingAttribute to be inserted into the code dom
                        CodeAttributeDeclaration attr = new CodeAttributeDeclaration(typeof(AssertNamespaceBindingAttribute).FullName);
                        // make the argurments for the AssertNamespaceBindingAttribute
                        CodeAttributeArgument arg =
                            new CodeAttributeArgument(string.Empty,
                                                      new CodePrimitiveExpression(assert.Current.Value));
                        // add the first argument, that is the prefix

                        attr.Arguments.Add(arg);
                        // move to the next sibling of the prefix, that is the namespace uri itself
                        assert.Current.MoveToNext();
                        // build an argument out of the namespace uri
                        arg =
                            new CodeAttributeArgument(string.Empty,
                                                      new CodePrimitiveExpression(assert.Current.Value));
                        // add the namespace uri as the second argument of the constructor
                        attr.Arguments.Add(arg);
                        // add this AssertNamespaceBindingAttribute to the class
                        importer.CodeTypeDeclaration.CustomAttributes.Add(attr);
                    }
                }
                // keep method count up to date
                MethodPos++;
            }
            // get binding for method being processed
            OperationBinding methodBinding = importer.OperationBinding;
            // now see if it has a validation extensibility element from the DevInterop.Web.Services namespace
            XmlElement validationExtensibity =
                methodBinding.Input.Extensions.Find("validation", ValidationNamespaces.DevInterop);

            if (null != validationExtensibity)
            {
                // if there was an extensibilty element, process it
                // turn extensibility element into XPathDocument so
                // you can use XPath to find the parts of it you want.
                XPathDocument xpdoc = new XPathDocument(new XmlNodeReader(validationExtensibity));
                // make a navigator to look things up
                XPathNavigator xpnav = xpdoc.CreateNavigator();
                // make a namespace manager for the XPath expressions
                XmlNamespaceManager namespaceManager = new XmlNamespaceManager(xpnav.NameTable);
                // add the DevInterop.Web.Services namespac to it
                namespaceManager.AddNamespace("dm", ValidationNamespaces.DevInterop);
                // make an XPath expression that can be used to look for
                // asserts in the extensibilty element
                XPathExpression expression = xpnav.Compile("/dm:validation/dm:assertions/dm:assert");
                // add namespace to XPath expression
                expression.SetContext(namespaceManager);
                // make an XPath expression that can be evaluated to see
                // if schema validation is enabled
                XPathExpression validate = xpnav.Compile("/dm:validation/@SchemaValidation='true'");
                // add the namepaces to the validate XPath expression
                validate.SetContext(namespaceManager);
                // build a attribute that can be used to add a Validation attribute
                CodeAttributeDeclaration validateAttr = new CodeAttributeDeclaration(typeof(ValidationAttribute).FullName);
                // build argument for the validation attribute
                CodeAttributeArgument schemaArg =
                    new CodeAttributeArgument("SchemaValidation",
                                              // evaluate the validation
                                              new CodePrimitiveExpression((bool)xpnav.Evaluate(validate)));
                // added the arguement to the validation attribute, this
                // determines whether or not shchema validation is done
                validateAttr.Arguments.Add(schemaArg);
                // add the attribute to the method
                metadata.Add(validateAttr);
                // now find all of the asserts in the extensibilty element
                XPathNodeIterator pathNodeIterator = xpnav.Select(expression);
                // iterate through all of them
                while (pathNodeIterator.MoveNext())
                {
                    // clone the iterator so it is not changed while looking for its parts
                    XPathNodeIterator assert = pathNodeIterator.Clone();
                    // move to the first child, this is the XPath expression itself
                    assert.Current.MoveToFirstChild();
                    // create an AssertAttribute
                    CodeAttributeDeclaration attr = new CodeAttributeDeclaration(typeof(AssertAttribute).FullName);
                    // create an argument for the AssertAttribute constructor
                    CodeAttributeArgument arg =
                        new CodeAttributeArgument(string.Empty,
                                                  // use the XPath expression string as the first argument
                                                  new CodePrimitiveExpression(assert.Current.Value));
                    // add the first argument to the constructor
                    attr.Arguments.Add(arg);
                    // move to the next sibling of the xpath expression,
                    // that is the message to be passed back if the
                    // assert is not true
                    assert.Current.MoveToNext();
                    // now create an argument for the second parameter of
                    // the AssertAttribute constructor
                    arg =
                        new CodeAttributeArgument(string.Empty,
                                                  // use the error message itself as the second argument value
                                                  new CodePrimitiveExpression(assert.Current.Value));
                    // add the second argument to the constructor
                    attr.Arguments.Add(arg);
                    // add the AssertAttribute to the method
                    metadata.Add(attr);
                }
            }
        }