Exemplo n.º 1
0
#pragma warning disable 0618 // XmlReaderSettings.ProhibitDtd is obsolete

#if FEATURE_COMPILED_XSL
        /// <summary>
        /// Compiles given stylesheets into an assembly.
        /// </summary>
        private void Compile(string inputFile, string outputFile)
        {
            const string CompiledQueryName = "xslt";
            string       outputDir         = Path.GetDirectoryName(outputFile) + Path.DirectorySeparatorChar;
            XsltSettings xsltSettings      = new XsltSettings(true, true);

            XmlUrlResolver    xmlResolver    = new XmlUrlResolver();
            XmlReaderSettings readerSettings = new XmlReaderSettings();

            AssemblyBuilder asmBldr;

            readerSettings.ProhibitDtd = false;
            readerSettings.XmlResolver = xmlResolver;

            string scriptAsmPathPrefix = outputDir + Path.GetFileNameWithoutExtension(outputFile) + ".script";

            // Create assembly and module builders
            AssemblyName asmName = new AssemblyName();

            asmName.Name = CompiledQueryName;

            asmBldr = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Save, outputDir);

            // Add custom attribute to assembly marking it as security transparent so that Assert will not be allowed
            // and link demands will be converted to full demands.
            asmBldr.SetCustomAttribute(new CustomAttributeBuilder(typeof(System.Security.SecurityTransparentAttribute).GetConstructor(Type.EmptyTypes), new object[] { }));

            // Mark the assembly with GeneratedCodeAttribute to improve profiling experience
            asmBldr.SetCustomAttribute(new CustomAttributeBuilder(typeof(GeneratedCodeAttribute).GetConstructor(new Type[] { typeof(string), typeof(string) }), new object[] { "XsltCompiler", "2.0.0.0" }));

            ModuleBuilder modBldr = asmBldr.DefineDynamicModule(Path.GetFileName(outputFile), Path.GetFileName(outputFile), true);

            string sourceUri   = inputFile;
            string className   = Path.GetFileNameWithoutExtension(inputFile);
            string scriptAsmId = "";

            // Always use the .dll extension; otherwise Fusion won't be able to locate this dependency
            string scriptAsmPath = scriptAsmPathPrefix + scriptAsmId + ".dll";

            // Create TypeBuilder and compile the stylesheet into it
            TypeBuilder typeBldr = modBldr.DefineType(CompiledQueryName, TypeAttributes.Public | TypeAttributes.Abstract | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit);

            CompilerErrorCollection errors = null;

            try
            {
                using (XmlReader reader = XmlReader.Create(sourceUri, readerSettings))
                {
                    errors = XslCompiledTransform.CompileToType(
                        reader, xsltSettings, xmlResolver, false, typeBldr, scriptAsmPath
                        );
                }
            }
            catch (Exception e)
            {
                Assert.True(false, "Compiler didn't work" + e.ToString());
            }

            asmBldr.Save(Path.GetFileName(outputFile), PortableExecutableKinds.ILOnly, ImageFileMachine.I386);
        }
        /// <summary>
        /// Compiles a single XSL file to the _assemblyBuilder, but doesn't save it.
        /// </summary>
        private bool CompileInternal(string xsl, string className, ModuleBuilder builder)
        {
            // Args checking
            if (string.IsNullOrEmpty(xsl))
            {
                throw new ArgumentNullException("No XSL content specified.");
            }

            if (string.IsNullOrEmpty(className))
            {
                throw new ArgumentNullException("No class name specified.");
            }

            if (string.IsNullOrEmpty(Path))
            {
                throw new InvalidOperationException("Path is null or empty.");
            }

            if (builder == null)
            {
                // Default the assembly name to the filename
                if (string.IsNullOrEmpty(AssemblyName))
                {
                    AssemblyName = Filename;
                }

                builder = this.CreateModuleBuilder(new AssemblyName(AssemblyName));
            }

            bool errors = false;

            Errors = new CompilerErrorCollection();

            TypeBuilder typeBuilder = builder.DefineType(className, TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed | TypeAttributes.Abstract | TypeAttributes.Public);

            using (XmlReader xslReader = XmlReader.Create(new StringReader(xsl)))
            {
                Errors = XslCompiledTransform.CompileToType(xslReader, XsltSettings, XmlResolver, Debug, typeBuilder, GetFullPath(AssemblyName + ".script.dll"));
            }

            foreach (CompilerError error in Errors)
            {
                errors |= !error.IsWarning;
            }

            // errors contains a C-like false for no errors, so reverse this as we're returning whether the operation succeeded.
            return(!errors);
        }
Exemplo n.º 3
0
        public Type CreateXsltType(string className, Stream xsltData)
        {
            XmlReader xsltReader = XmlReader.Create(xsltData, ReaderSetting);

            using (xsltReader)
            {
                TypeBuilder             xsltType = fModBuilder.DefineType(className, Attributes);
                CompilerErrorCollection errors   = XslCompiledTransform.CompileToType(xsltReader, XsltSettings.Default,
                                                                                      XmlTransformUtil.Resolver, false, xsltType, @"e:\a.script");
                if (errors.Count > 0)
                {
                    return(null);
                }

                return(xsltType.CreateType());
            }
        }