Ejemplo n.º 1
0
        internal static void GenerateGetEncoderMethod(
            TextWriter writer, string entitySetupFieldName, IReadOnlyCollection <WebItemParameter> requiredParameters,
            IReadOnlyCollection <WebItemParameter> optionalParameters, Func <WebItemParameter, string> isSegmentParameterExpressionGetter, bool includeVersionString)
        {
            writer.WriteLine(
                "protected override global::EnterpriseWebLibrary.EnterpriseWebFramework.UrlEncoder getUrlEncoder() => new UrlEncoder({0});".FormatWith(
                    StringTools.ConcatenateWithDelimiter(
                        ", ",
                        (entitySetupFieldName.Any() ? entitySetupFieldName.ToCollection() : Enumerable.Empty <string>())
                        .Concat(requiredParameters.Select(i => i.PropertyName))
                        .Concat(
                            optionalParameters.SelectMany(
                                i => {
                // If a default was specified for the parameter and the default matches the value of our parameter, don't include it.
                // If a default was not specified and the value of our parameter is the default value of the type, don't include it.
                var defaultParameterReference = WebItemGeneralData.ParameterDefaultsFieldName + "." + i.PropertyName;
                var value = "( {0} && {1} ) || ( !{0} && {2} ) ? null : {3}".FormatWith(
                    WebItemGeneralData.ParameterDefaultsFieldName + "." + OptionalParameterPackageStatics.GetWasSpecifiedPropertyName(i),
                    i.IsEnumerable
                                                                                                ? defaultParameterReference + ".SequenceEqual( " + i.PropertyName + " )"
                                                                                                : defaultParameterReference + " == " + i.PropertyName,
                    i.IsEnumerable ? "!" + i.PropertyName + ".Any()" : i.PropertyName + " == " + (i.IsString ? "\"\"" : "default(" + i.TypeName + ")"),
                    i.IsString || i.IsEnumerable ? i.PropertyName : "new SpecifiedValue<{0}>( {1} )".FormatWith(i.TypeName, i.PropertyName));

                return(new[] { value, isSegmentParameterExpressionGetter(i) });
            }))
                        .Concat(includeVersionString ? "getUrlVersionString()".ToCollection() : Enumerable.Empty <string>()))
                    .Surround(" ", " ")));
        }
Ejemplo n.º 2
0
        private static void writeInitParametersMethod(
            TextWriter writer, WebItemGeneralData generalData, IReadOnlyCollection <WebItemParameter> requiredParameters,
            IReadOnlyCollection <WebItemParameter> optionalParameters, bool includeEsParameter, bool isEs, string constructorParameters)
        {
            CodeGenerationStatics.AddSummaryDocComment(
                writer,
                "Initializes required and optional parameters. A call to this should be the first line of every non-generated constructor.");
            writer.WriteLine("private void initParameters" + constructorParameters);

            if (includeEsParameter)
            {
                writer.WriteLine("Es = es;");
            }
            foreach (var requiredParameter in requiredParameters)
            {
                if (requiredParameter.IsString || requiredParameter.IsEnumerable)
                {
                    writer.WriteLine(
                        "if( " + requiredParameter.Name +
                        " == null ) throw new ApplicationException( \"You cannot specify null for the value of a string or an IEnumerable.\" );");
                }
                writer.WriteLine(requiredParameter.FieldName + " = " + requiredParameter.Name + ";");
            }

            // Initialize optional parameter fields.
            if (optionalParameters.Any())
            {
                writer.WriteLine("var optionalParametersInitializedFromCurrent = false;");
                writer.WriteLine("if( EwfApp.Instance != null && AppRequestState.Instance != null ) {");

                // If the list of current URL handlers has a matching object, apply its parameter values.
                writer.WriteLine("foreach( var urlHandler in AppRequestState.Instance.UrlHandlers )");
                if (isEs)
                {
                    writer.WriteLine("if( urlHandler is ResourceBase r ) {");
                    writer.WriteLine("if( {0} ) {{".FormatWith(getHandlerMatchExpression(generalData, requiredParameters, true)));
                    generateMatchingHandlerParameterInitStatements(writer, optionalParameters, false);
                    writer.WriteLine("}");
                    writer.WriteLine("}");
                    writer.WriteLine("else {");
                    writer.WriteLine("if( {0} ) {{".FormatWith(getHandlerMatchExpression(generalData, requiredParameters, false)));
                    generateMatchingHandlerParameterInitStatements(writer, optionalParameters, false);
                    writer.WriteLine("}");
                    writer.WriteLine("}");
                }
                else
                {
                    writer.WriteLine("if( {0} ) {{".FormatWith(getHandlerMatchExpression(generalData, requiredParameters, false)));
                    generateMatchingHandlerParameterInitStatements(writer, optionalParameters, false);
                    writer.WriteLine("}");
                }

                // If new parameter values are effective, and the current resource or an ancestor matches this object, apply its new parameter values.
                if (generalData.IsPage() || isEs)
                {
                    writer.WriteLine("if( AppRequestState.Instance.NewUrlParameterValuesEffective ) {");
                    writer.WriteLine("UrlHandler urlHandler = {0}Current;".FormatWith(generalData.IsPage() ? "" : "PageBase."));
                    writer.WriteLine("do");
                    if (isEs)
                    {
                        writer.WriteLine("if( urlHandler is ResourceBase r ) {");
                        writer.WriteLine("if( {0} ) {{".FormatWith(getHandlerMatchExpression(generalData, requiredParameters, true)));
                        generateMatchingHandlerParameterInitStatements(writer, optionalParameters, true);
                        writer.WriteLine("}");
                        writer.WriteLine("}");
                        writer.WriteLine("else {");
                        writer.WriteLine("if( {0} ) {{".FormatWith(getHandlerMatchExpression(generalData, requiredParameters, false)));
                        generateMatchingHandlerParameterInitStatements(writer, optionalParameters, true);
                        writer.WriteLine("}");
                        writer.WriteLine("}");
                    }
                    else
                    {
                        writer.WriteLine("if( {0} ) {{".FormatWith(getHandlerMatchExpression(generalData, requiredParameters, false)));
                        generateMatchingHandlerParameterInitStatements(writer, optionalParameters, true);
                        writer.WriteLine("}");
                    }
                    writer.WriteLine("while( ( urlHandler = urlHandler.GetParent() ) != null );");
                    writer.WriteLine("}");
                }

                writer.WriteLine("}");

                // Apply parameter values from the setter.
                writer.WriteLine("var optionalParameterSpecifier = new OptionalParameterSpecifier();");
                writer.WriteLine(
                    "optionalParameterSetter?.Invoke( {0} );".FormatWith(
                        StringTools.ConcatenateWithDelimiter(
                            ", ",
                            "optionalParameterSpecifier",
                            includeEsParameter ? "es" : "",
                            "new Parameters( {0} )".FormatWith(
                                StringTools.ConcatenateWithDelimiter(
                                    ", ",
                                    requiredParameters.Select(i => i.PropertyName)
                                    .Append(
                                        "optionalParametersInitializedFromCurrent ? new OptionalParameters( {0} ) : null".FormatWith(
                                            StringTools.ConcatenateWithDelimiter(", ", optionalParameters.Select(i => i.PropertyName)))))))));
                foreach (var i in optionalParameters)
                {
                    writer.WriteLine(
                        "if( optionalParameterSpecifier.{0} ) {1} = optionalParameterSpecifier.{2};".FormatWith(
                            OptionalParameterPackageStatics.GetWasSpecifiedPropertyName(i),
                            i.FieldName,
                            i.PropertyName));
                }

                // This is called after all current values and values from the setter have been incorporated since these can affect default values.
                writer.WriteLine(
                    "specifyParameterDefaults( {0} );".FormatWith(
                        StringTools.ConcatenateWithDelimiter(
                            ", ",
                            WebItemGeneralData.ParameterDefaultsFieldName,
                            includeEsParameter ? "es" : "",
                            "new Parameters( {0} )".FormatWith(
                                StringTools.ConcatenateWithDelimiter(
                                    ", ",
                                    requiredParameters.Select(i => i.PropertyName)
                                    .Append(
                                        "new OptionalParameters( {0} )".FormatWith(
                                            StringTools.ConcatenateWithDelimiter(", ", optionalParameters.Select(i => i.PropertyName)))))))));

                // Apply default values to parameters not yet initialized.
                writer.WriteLine("if( !optionalParametersInitializedFromCurrent ) {");
                foreach (var i in optionalParameters)
                {
                    writer.WriteLine(
                        "if( !optionalParameterSpecifier.{0} && {1}.{0} ) {2} = {1}.{3};".FormatWith(
                            OptionalParameterPackageStatics.GetWasSpecifiedPropertyName(i),
                            WebItemGeneralData.ParameterDefaultsFieldName,
                            i.FieldName,
                            i.PropertyName));
                }
                writer.WriteLine("}");
            }

            if (!isEs)
            {
                writer.WriteLine("base.uriFragmentIdentifier = uriFragmentIdentifier;");
            }

            if (optionalParameters.Any())
            {
                writer.WriteLine("this.optionalParameterSetter = optionalParameterSetter;");
            }

            writer.WriteLine("}");               // initParameters method
        }