/// <summary>
        /// Enumerates all types in the given assembly hunting for BizTalk types.
        /// </summary>
        /// <param name="assembly">Assembly to enumerate.</param>
        /// <returns>List of decompiled BizTalk artifacts.</returns>
        private static List <DecompiledArtifact> EnumerateAssembly(IAssembly assembly)
        {
            List <DecompiledArtifact> artifacts = new List <DecompiledArtifact>();

            // Look at all types declared in this assembly and extract the BizTalk related ones
            if ((assembly != null) && (assembly.Modules != null))
            {
                foreach (IModule module in assembly.Modules)
                {
                    if (module.Types != null)
                    {
                        foreach (ITypeDeclaration typeDeclaration in module.Types)
                        {
                            // BizTalk artifacts are in sealed, non abstract classes
                            if (!typeDeclaration.Interface && typeDeclaration.Sealed && !typeDeclaration.Abstract)
                            {
                                // Decompile the artifact
                                DecompiledArtifact artifact = DecompileArtifact(typeDeclaration);
                                if (artifact != null)
                                {
                                    artifacts.Add(artifact);
                                }
                            }
                        }
                    }
                }
            }

            return(artifacts);
        }
        /// <summary>
        /// Compute the destination path (a set of nested directories to mimic namespaces).
        /// The hierachy is also created on disk if it does not exist.
        /// </summary>
        /// <param name="basePath">Base path to start from.</param>
        /// <param name="artifact">Artifact to compute the export path for.</param>
        /// <returns>Path to the directory the artifact should be exported.</returns>
        private static string ComputeAndEnsureOutputPathHierachy(string basePath, DecompiledArtifact artifact)
        {
            // Compute the destiantion path (we start with the base path)
            string destinationPath = basePath;

            // Make sure the base path is available
            if (!Directory.Exists(basePath))
            {
                Directory.CreateDirectory(basePath);
            }

            // Create directories as needed to mimic the namespace structure
            if (!String.IsNullOrEmpty(artifact.Namespace))
            {
                string[] namespaces = artifact.Namespace.Split(new char[] { ',' });
                if (namespaces != null)
                {
                    foreach (string segment in namespaces)
                    {
                        destinationPath = Path.Combine(destinationPath, segment);
                        if (!Directory.Exists(destinationPath))
                        {
                            Directory.CreateDirectory(destinationPath);
                        }
                    }
                }
            }

            return(destinationPath);
        }
        /// <summary>
        /// Notify the feedback area of an error during the export process.
        /// </summary>
        /// <param name="e">Exception.</param>
        /// <param name="artifact">Artifact being exported.</param>
        private void NotifyExportError(Exception e, DecompiledArtifact artifact)
        {
            // Update the progress bar
            progressBar.Increment(1);

            // Emit an error message to the feedback area
            string message = String.Format(CultureInfo.CurrentCulture,
                                           Resources.ErrorExportedArtifact,
                                           new object[] {
                artifact.ArtifactKind,
                artifact.Namespace + "." + artifact.Name,
                e.ToString()
            });

            AppendMessageToFeedbackArea(message, System.Diagnostics.TraceLevel.Error);
        }
        /// <summary>
        /// Notify the feedback area that we exported an artifact.
        /// </summary>
        /// <param name="outPath">Destination to where the artifact was exported.</param>
        /// <param name="artifact">Artifact exported.</param>
        private void NotifyArtifactExported(string outPath, DecompiledArtifact artifact)
        {
            // Update the progress bar
            progressBar.Increment(1);

            // Emit a message to the feedback area
            string message = String.Format(CultureInfo.CurrentCulture,
                                           Resources.ExportedArtifact,
                                           new object[] {
                artifact.ArtifactKind,
                artifact.Namespace + "." + artifact.Name,
                outPath
            });

            AppendMessageToFeedbackArea(message, System.Diagnostics.TraceLevel.Info);
        }
        /// <summary>
        /// Get the file extension associated with a given artifact.
        /// </summary>
        /// <param name="artifact">Artifact to consider when computing the extension.</param>
        /// <returns>Extension, as a string.</returns>
        private static string GetExtension(DecompiledArtifact artifact)
        {
            switch (artifact.ArtifactKind)
            {
            case Constants.BizTalkArtifactType.Map:
                return("xslt");

            case Constants.BizTalkArtifactType.Orchestration:
                return("odx");

            case Constants.BizTalkArtifactType.ReceivePipeline:
            case Constants.BizTalkArtifactType.SendPipeline:
                return("btp");

            case Constants.BizTalkArtifactType.Schema:
                return("xsd");

            default:
                return(String.Empty);
            }
        }
        /// <summary>
        /// Decompiles a BizTalk type knowing its Reflector TypeDeclaration.
        /// </summary>
        /// <param name="typeDeclaration">Type declaration to decompile.</param>
        /// <returns>Decompiled Artifact.</returns>
        private static DecompiledArtifact DecompileArtifact(ITypeDeclaration typeDeclaration)
        {
            DecompiledArtifact artifact = null;

            // Build the fully qualified name of the base class for this type
            ITypeReference baseType = typeDeclaration.BaseType as ITypeReference;
            string         fullyQualifiedTypeName = baseType.Name + ", " + baseType.Owner.ToString();

            // Locate the class which will handle this artifact
            foreach (Constants.RecognizedBizTalkArtifact recognizedArtifact in Constants.RecognizedArtifacts)
            {
                // If we found a matching handler, create a new instance of the handler and initialize it
                if (String.CompareOrdinal(recognizedArtifact.ExpectedType, fullyQualifiedTypeName) == 0)
                {
                    // Extract the XML representation of the artifact
                    artifact = Activator.CreateInstance(recognizedArtifact.DecompiledType, new object[] { typeDeclaration }) as DecompiledArtifact;
                    break;
                }
            }

            return(artifact);
        }
Beispiel #7
0
        /// <summary>
        /// Handles the Click event fired by the "Decompile..." button.
        /// </summary>
        /// <param name="sender">Object sending this message.</param>
        /// <param name="e">Parameter associated with this event.</param>
        private void decompileButton_Click(object sender, EventArgs e)
        {
            // Compile the list of artifacts to export ()
            List <DecompiledArtifact> artifactsToDecompile = new List <DecompiledArtifact>();

            foreach (ListViewItem lvi in artifactsListView.CheckedItems)
            {
                DecompiledArtifact artifact = lvi.Tag as DecompiledArtifact;
                if (artifact != null)
                {
                    artifactsToDecompile.Add(artifact);
                }
            }

            // Delegate the work to a popup dialog
            if (artifactsToDecompile.Count > 0)
            {
                using (ProgressDialog dialog = new ProgressDialog(artifactsToDecompile))
                {
                    dialog.ShowDialog();
                }
            }
        }
        /// <summary>
        /// Export the given artifact to a file under the given path.
        /// </summary>
        /// <param name="path">Path to export the artifact to.</param>
        /// <param name="artifact">Artifact to export.</param>
        private static void ExportFormattedXml(string path, DecompiledArtifact artifact)
        {
            // Construct the output file path
            string filePath = Path.ChangeExtension(Path.Combine(path, artifact.Name), GetExtension(artifact));

            // Delete the file if it already exists
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            // Write the Xml and format it at the same time
            using (XmlTextWriter xmlWriter = new XmlTextWriter(filePath, System.Text.Encoding.UTF8))
            {
                XmlDocument artifactDOM = new XmlDocument();
                artifactDOM.LoadXml(artifact.ArtifactValue);

                // Configure the writer so it formats output
                xmlWriter.Formatting = Formatting.Indented;

                // Write to the output file
                artifactDOM.WriteTo(xmlWriter);
            }
        }