Ejemplo n.º 1
0
        void preprocessor_ProcessedStream(object sender, ProcessedStreamEventArgs e)
        {
            ProcessedStreamEventArgs args = e as ProcessedStreamEventArgs;
            Stream processed = args.Processed;
            // save the old position of the stream
            long oldPosition = processed.Position;

            HashAlgorithm sha = new SHA1CryptoServiceProvider();
            processed.Position = 0;
            byte[] hashBytes = sha.ComputeHash(processed);

            StringBuilder sb = new StringBuilder();
            for (int i = 0, j = hashBytes.GetLength(0); i < j; ++i)
            {
                sb.AppendFormat("{0:X02}", hashBytes[i]);
            }
            this.hash = sb.ToString();
            // restore the old position
            processed.Position = oldPosition;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Fires an event after the file is preprocessed.
 /// </summary>
 /// <param name="ea">Included file event arguments.</param>
 private void OnProcessedStream(ProcessedStreamEventArgs ea)
 {
     if (null != this.ProcessedStream)
     {
         this.ProcessedStream(this, ea);
     }
 }
Ejemplo n.º 3
0
        public XmlDocument Process(string sourceFile, Hashtable variables)
        {
            Stream processed = new MemoryStream();
            XmlDocument sourceDocument = new XmlDocument();

            try
            {
                this.core = new PreprocessorCore(this.extensionsByPrefix, this.Message, sourceFile, variables);
                this.core.ResolvedVariableHandler = this.ResolvedVariable;
                this.core.CurrentPlatform = this.currentPlatform;
                this.currentLineNumberWritten = false;
                this.currentLineNumber = new SourceLineNumber(sourceFile);
                this.currentFileStack.Clear();
                this.currentFileStack.Push(this.core.GetVariableValue(this.GetCurrentSourceLineNumbers(), "sys", "SOURCEFILEDIR"));

                // open the source file for processing
                using (Stream sourceStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // inspect the raw source
                    InspectorCore inspectorCore = new InspectorCore(this.Message);
                    foreach (InspectorExtension inspectorExtension in this.inspectorExtensions)
                    {
                        inspectorExtension.Core = inspectorCore;
                        inspectorExtension.InspectSource(sourceStream);

                        // reset
                        inspectorExtension.Core = null;
                        sourceStream.Position = 0;
                    }

                    if (inspectorCore.EncounteredError)
                    {
                        return null;
                    }

                    XmlReader reader = new XmlTextReader(sourceStream);
                    XmlTextWriter writer = new XmlTextWriter(processed, Encoding.UTF8);

                    // process the reader into the writer
                    try
                    {
                        foreach (PreprocessorExtension extension in this.extensions)
                        {
                            extension.Core = this.core;
                            extension.InitializePreprocess();
                        }
                        this.PreprocessReader(false, reader, writer, 0);
                    }
                    catch (XmlException e)
                    {
                        this.UpdateInformation(reader, 0);
                        throw new WixException(WixErrors.InvalidXml(this.GetCurrentSourceLineNumbers(), "source", e.Message));
                    }

                    writer.Flush();
                }

                // fire event with processed stream
                ProcessedStreamEventArgs args = new ProcessedStreamEventArgs(sourceFile, processed);
                this.OnProcessedStream(args);

                // create an XML Document from the post-processed memory stream
                XmlTextReader xmlReader = null;

                try
                {
                    // create an XmlReader with the path to the original file
                    // to properly set the BaseURI property of the XmlDocument
                    processed.Position = 0;
                    xmlReader = new XmlTextReader(new Uri(Path.GetFullPath(sourceFile)).AbsoluteUri, processed);
                    sourceDocument.Load(xmlReader);

                }
                catch (XmlException e)
                {
                    this.UpdateInformation(xmlReader, 0);
                    throw new WixException(WixErrors.InvalidXml(this.GetCurrentSourceLineNumbers(), "source", e.Message));
                }
                finally
                {
                    if (null != xmlReader)
                    {
                        xmlReader.Close();
                    }
                }

                // preprocess the generated XML Document
                foreach (PreprocessorExtension extension in this.extensions)
                {
                    extension.PreprocessDocument(sourceDocument);
                }
            }
            finally
            {
                // finalize the preprocessing
                foreach (PreprocessorExtension extension in this.extensions)
                {
                    extension.FinalizePreprocess();
                    extension.Core = null;
                }
            }

            if (this.core.EncounteredError)
            {
                return null;
            }
            else
            {
                if (null != this.preprocessOut)
                {
                    sourceDocument.Save(this.preprocessOut);
                    this.preprocessOut.Flush();
                }

                return sourceDocument;
            }
        }