using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.IO; using NPanday.ProjectImporter.Digest; using NPanday.ProjectImporter.Digest.Model; using NPanday.Utils; using NPanday.Model.Pom; using NPanday.Artifact; using System.Windows.Forms; /// Author: Leopoldo Lee Agdeppa III namespace NPanday.ProjectImporter.Converter.Algorithms { public abstract class AbstractPomConverter : IPomConverter { protected GacUtility gacUtil; protected ArtifactContext artifactContext; protected List localArtifacts; protected string mainPomFile; protected NPanday.Model.Pom.Model parent; protected string groupId; protected string version; protected ProjectDigest projectDigest; public ProjectDigest ProjectDigest { get { return projectDigest; } } protected NPanday.Model.Pom.Model model; public NPanday.Model.Pom.Model Model { get { return model; } } public AbstractPomConverter(ProjectDigest projectDigest, string mainPomFile, NPanday.Model.Pom.Model parent, string groupId) { artifactContext = new ArtifactContext(); this.localArtifacts = artifactContext.GetArtifactRepository().GetArtifacts(); this.projectDigest = projectDigest; this.mainPomFile = mainPomFile; this.parent = parent; this.groupId = FilterID(groupId); this.version = parent != null ? parent.version : null; this.gacUtil = new GacUtility(); this.model = new NPanday.Model.Pom.Model(); // Add build Tag this.model.build = new NPanday.Model.Pom.Build(); } #region AddEmbeddedResources protected void AddEmbeddedResources() { if (projectDigest != null && projectDigest.EmbeddedResources != null && projectDigest.EmbeddedResources.Length > 0) { Plugin embeddedResourcePlugin = AddPlugin( "npanday.plugin", "maven-resgen-plugin", null, true ); List> embeddedResourceList = new List>(); foreach (EmbeddedResource embeddedResource in projectDigest.EmbeddedResources) { Dictionary value = new Dictionary(); string sourceFile = embeddedResource.IncludePath; if (sourceFile == null) continue; value.Add("sourceFile", sourceFile); value.Add("name", parseEmbeddedName(projectDigest.RootNamespace, sourceFile)); embeddedResourceList.Add(value); } AddPluginConfiguration(embeddedResourcePlugin, "embeddedResources", embeddedResourceList); } } string parseEmbeddedName(string nameSpace, string sourceFilePath) { string returnVal = string.Empty; FileInfo sourceFileInfo = new FileInfo(sourceFilePath); if ("vb".Equals(projectDigest.Language)) { returnVal = sourceFileInfo.Name.Replace(".resx", string.Empty); } else if ("csharp".Equals(projectDigest.Language)) { foreach (string name in sourceFilePath.Split("\\".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)) { if (name.IndexOf(".") < 0) { returnVal += name.Trim().Replace(' ', '_') + "."; } else { returnVal += name.Substring(0, name.LastIndexOf(".")); } } } return string.Format("{0}.{1}", nameSpace, returnVal); } #endregion #region AddWebReferences /// /// Adds WebReference as Plugin /// Author: Shein Melicer G. Ernacio /// protected void AddWebReferences() { if (projectDigest != null && projectDigest.WebReferenceUrls != null && projectDigest.WebReferenceUrls.Length > 0) { Plugin webReferencePlugin = AddPlugin( "npanday.plugin", "maven-wsdl-plugin", null, true ); AddPluginExecution(webReferencePlugin, "wsdl", null); List> webReferenceUrlList = new List>(); foreach (WebReferenceUrl webReferenceUrl in projectDigest.WebReferenceUrls) { Dictionary value = new Dictionary(); string name = webReferenceUrl.RelPath; if (name != null) { int startIndex = name.IndexOf("\\"); if (startIndex >= 0) { name = name.Substring(startIndex + 1); } name = getNameSpace(name); value.Add("namespace", name); value.Add("output", webReferenceUrl.RelPath); DirectoryInfo dirInfo = new DirectoryInfo(Path.Combine(projectDigest.FullDirectoryName, webReferenceUrl.RelPath)); FileInfo[] fileInfo = dirInfo.GetFiles("*.wsdl"); if (fileInfo.Length > 0) { value.Add("path", Path.Combine(webReferenceUrl.RelPath, fileInfo[0].Name)); } else { value.Add("path", webReferenceUrl.RelPath + name + ".wsdl"); } webReferenceUrlList.Add(value); } } AddPluginConfiguration(webReferencePlugin, "webreferences", webReferenceUrlList); } } string getNameSpace(string relPath) { if (string.IsNullOrEmpty(relPath)) return string.Empty; if (relPath.EndsWith("\\")) relPath = relPath.Substring(0, relPath.Length - 1); return relPath.Replace("\\", "."); } #endregion public abstract void ConvertProjectToPomModel(bool writePom, string scmTag); public void ConvertProjectToPomModel(string scmTag) { ConvertProjectToPomModel(true,scmTag); } private string FilterID(string partial) { string filtered = string.Empty; if (partial.EndsWith(".")) { partial = partial.Substring(0, partial.Length - 1); } char before = '*'; foreach (char item in partial) { if ((Char.IsNumber(item) || Char.IsLetter(item)) || ((item == '.' && before != '.') || (item == '-' && before != '-'))) { filtered += item; } before = item; } return filtered; } bool HasSpecialCharacters(string partial) { bool isSpecial = false; foreach (char item in partial) { if ((!Char.IsNumber(item) && !Char.IsLetter(item)) && item != '.' && item != '-') { isSpecial = true; } } return isSpecial; } protected void GenerateHeader(string packaging) { // Add Parent Header if (parent != null) { model.parent = new NPanday.Model.Pom.Parent(); model.parent.artifactId = FilterID(parent.artifactId); model.parent.groupId = FilterID(parent.groupId); model.parent.version = parent.version; if (!string.IsNullOrEmpty(mainPomFile)) { DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(projectDigest.FullFileName)); FileInfo file = new FileInfo(mainPomFile); model.parent.relativePath = PomHelperUtility.GetRelativePath(dir, file); } } else { model.groupId = !string.IsNullOrEmpty(groupId) ? FilterID(groupId) : FilterID(projectDigest.AssemblyName); model.version = string.IsNullOrEmpty(version) ? "1.0-SNAPSHOT" : version; } string projectName = projectDigest.AssemblyName; if (HasSpecialCharacters(projectDigest.AssemblyName)) { string[] projectFullName = projectDigest.FullFileName.Split("\\".ToCharArray()); projectName = projectFullName[projectFullName.Length - 1]; if (projectName.Equals(".")) { projectName = projectFullName[projectFullName.Length - 2]; } if (projectName.Contains(".csproj")) { projectName = projectName.Replace(".csproj", ""); } if (projectName.Contains(".vbproj")) { projectName = projectName.Replace(".vbproj", ""); } } model.artifactId = FilterID(projectName); model.modelVersion = "4.0.0"; model.packaging = packaging; model.name = string.Format("{0} : {1}", !string.IsNullOrEmpty(groupId) ? groupId : FilterID(projectDigest.AssemblyName), FilterID(projectDigest.AssemblyName)); if (!string.IsNullOrEmpty(projectDigest.OutputType)) { model.packaging = projectDigest.OutputType.ToLower(); } } protected void AddComReferenceDependency() { foreach (ComReference comReference in projectDigest.ComReferenceList) { Dependency comDependency = new Dependency(); comDependency.groupId = comReference.Include; comDependency.artifactId = comReference.Include; comDependency.version = comReference.VersionMajor + "." + comReference.VersionMinor + ".0" + ".0"; comDependency.type = "com_reference"; comDependency.classifier = string.Format("{0}\\{1}.{2}\\{3}", comReference.Guid, convertDecToHex(comReference.VersionMajor), convertDecToHex(comReference.VersionMinor), convertDecToHex(comReference.Lcid)); comDependency.classifier = comDependency.classifier.Replace("\\", "-"); AddDependency(comDependency); } } string convertDecToHex(string decimalNum) { int decNum; if (int.TryParse(decimalNum, out decNum)) { return decNum.ToString("X"); } return decimalNum; } protected bool IsModelHasDependency(Dependency dependency) { if (model.dependencies == null) { return false; } foreach (Dependency var in model.dependencies) { if (var.artifactId.Equals(dependency.artifactId) && var.groupId.Equals(dependency.groupId)) { return true; } } return false; } protected void AddDependency(Dependency dependency) { // add dependency if it doesnot exists if (!IsModelHasDependency(dependency)) { List dependencies = new List(); if (model.dependencies != null) { dependencies.AddRange(model.dependencies); } dependencies.Add(dependency); model.dependencies = dependencies.ToArray(); return; } } protected void AddInterProjectDependency(ProjectReference projectRef) { Dependency interDependency = new Dependency(); interDependency.artifactId = projectRef.Name; interDependency.groupId = !string.IsNullOrEmpty(groupId) ? groupId : projectRef.Name; interDependency.version = string.IsNullOrEmpty(version) ? "1.0-SNAPSHOT" : version; interDependency.type = "library"; if (projectRef.ProjectReferenceDigest != null && !string.IsNullOrEmpty(projectRef.ProjectReferenceDigest.OutputType)) { interDependency.type = projectRef.ProjectReferenceDigest.OutputType.ToLower(); } AddDependency(interDependency); } protected void AddInterProjectDependenciesToList() { foreach (ProjectReference projectRef in projectDigest.ProjectReferences) { AddInterProjectDependency(projectRef); } } protected virtual void AddProjectReferenceDependency(Reference reference) { Dependency refDependency = ResolveDependency(reference); if (refDependency == null) { return; } if (!"library".Equals(refDependency.type, StringComparison.OrdinalIgnoreCase)) { // ignore gac if already in the RSP if (gacUtil.IsRspIncluded(refDependency.artifactId, projectDigest.Language)) { return; } } AddDependency(refDependency); } protected void AddProjectReferenceDependenciesToList() { foreach (Reference reference in projectDigest.References) { AddProjectReferenceDependency(reference); } } protected Plugin AddPlugin(string groupId, string artifactId) { return AddPlugin(groupId, artifactId, null, true); } protected Plugin AddPlugin(string groupId, string artifactId, string version, bool extensions) { List plugins = new List(); if (model.build.plugins != null) { plugins.AddRange(model.build.plugins); } // Add NPanday compile plugin NPanday.Model.Pom.Plugin plugin = new NPanday.Model.Pom.Plugin(); plugin.groupId = groupId; plugin.artifactId = artifactId; plugin.version = version; plugin.extensions = extensions; plugins.Add(plugin); model.build.plugins = plugins.ToArray(); return plugin; } protected Plugin FindPlugin(string groupId, string artifactId) { return FindPlugin(groupId, artifactId, null); } protected Plugin FindPlugin(string groupId, string artifactId, string version) { if (model.build.plugins == null) { return null; } foreach (Plugin plugin in model.build.plugins) { if (groupId.Equals(plugin.groupId) && artifactId.Equals(plugin.artifactId)) { if (!string.IsNullOrEmpty(version) && version.Equals(plugin.version)) { return plugin; } else if (string.IsNullOrEmpty(version)) { return plugin; } } } return null; } /// /// Adds PluginExecution /// /// /// /// protected void AddPluginExecution(Plugin plugin, string goal, string phase) { if (string.IsNullOrEmpty(goal)) { // there is nothing to write return; } List list = new List(); if (plugin.executions == null) { plugin.executions = new List().ToArray(); list.AddRange(plugin.executions); } PluginExecution exe = new PluginExecution(); exe.goals = new string[] { goal }; list.Add(exe); plugin.executions = list.ToArray(); } protected void AddPluginConfiguration(Plugin plugin, string tag, string value) { if (string.IsNullOrEmpty(tag) || string.IsNullOrEmpty(value)) { // there is nothing to write return; } if (plugin.configuration == null) { plugin.configuration = new PluginConfiguration(); } List elems = new List(); if (plugin.configuration.Any != null) { elems.AddRange(plugin.configuration.Any); } XmlDocument xmlDocument = new XmlDocument(); XmlElement elem = xmlDocument.CreateElement(tag, @"http://maven.apache.org/POM/4.0.0"); elem.InnerText = value; elems.Add(elem); plugin.configuration.Any = elems.ToArray(); } /// /// Design for WebReferenceUrl /// /// the plugin /// myReference /// name-value-property protected void AddPluginConfiguration(Plugin plugin, string parentTag, List> properties) { if (string.IsNullOrEmpty(parentTag) || properties == null) { // there is nothing to write return; } if (properties.Count <= 0) { // there is nothing to write return; } if (plugin.configuration == null) { plugin.configuration = new PluginConfiguration(); } List elems = new List(); if (plugin.configuration.Any != null) { elems.AddRange(plugin.configuration.Any); } XmlDocument xmlDocument = new XmlDocument(); XmlElement elem = xmlDocument.CreateElement(parentTag, @"http://maven.apache.org/POM/4.0.0"); string childElement = parentTag.Substring(0, parentTag.Length - 1); foreach (Dictionary property in properties) { XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, childElement, @"http://maven.apache.org/POM/4.0.0"); if ("embeddedResources".Equals(parentTag)) { XmlNode nodeSourceFile = xmlDocument.CreateNode(XmlNodeType.Element, "sourceFile", @"http://maven.apache.org/POM/4.0.0"); XmlNode nodeName = xmlDocument.CreateNode(XmlNodeType.Element, "name", @"http://maven.apache.org/POM/4.0.0"); nodeName.InnerText = property["name"]; nodeSourceFile.InnerText = property["sourceFile"]; node.AppendChild(nodeSourceFile); node.AppendChild(nodeName); } else { XmlNode nodeName = xmlDocument.CreateNode(XmlNodeType.Element, "namespace", @"http://maven.apache.org/POM/4.0.0"); XmlNode nodePath = xmlDocument.CreateNode(XmlNodeType.Element, "path", @"http://maven.apache.org/POM/4.0.0"); XmlNode nodeOutput = xmlDocument.CreateNode(XmlNodeType.Element, "output", @"http://maven.apache.org/POM/4.0.0"); nodeName.InnerText = property["namespace"]; nodePath.InnerText = property["path"] != null ? property["path"].Replace("\\", "/") : property["path"]; nodeOutput.InnerText = property["output"] != null ? property["output"].Replace("\\", "/") : property["output"]; node.AppendChild(nodeName); node.AppendChild(nodePath); node.AppendChild(nodeOutput); } elem.AppendChild(node); } elems.Add(elem); plugin.configuration.Any = elems.ToArray(); } protected void AddPluginConfiguration(Plugin plugin, string parentTag, string childTags, string[] values) { if (string.IsNullOrEmpty(parentTag) || string.IsNullOrEmpty(childTags) || values == null) { // there is nothing to write return; } if (values.Length <= 0) { // there is nothing to write return; } if (plugin.configuration == null) { plugin.configuration = new PluginConfiguration(); } List elems = new List(); if (plugin.configuration.Any != null) { elems.AddRange(plugin.configuration.Any); } XmlDocument xmlDocument = new XmlDocument(); XmlElement elem = xmlDocument.CreateElement(parentTag, @"http://maven.apache.org/POM/4.0.0"); foreach (string value in values) { XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, childTags, @"http://maven.apache.org/POM/4.0.0"); node.InnerText = value.Trim(); elem.AppendChild(node); } elems.Add(elem); plugin.configuration.Any = elems.ToArray(); } protected Dependency ResolveDependency(Reference reference) { // resolve first from artifact Artifact.Artifact artifact = GetArtifact(reference); if (artifact != null) { Dependency dependency = new Dependency(); dependency.artifactId = artifact.ArtifactId; dependency.groupId = artifact.GroupId; dependency.version = artifact.Version; dependency.type = "library"; return dependency; } // resolve from GAC if (!string.IsNullOrEmpty(gacUtil.GetAssemblyInfo(reference.Name))) { // Assembly is found at the gac Dependency refDependency = new Dependency(); refDependency.artifactId = reference.Name; refDependency.groupId = reference.Name; refDependency.type = "gac"; if ("MSIL".Equals(reference.ProcessorArchitecture, StringComparison.OrdinalIgnoreCase)) { refDependency.type = "gac_msil"; } else if ("x86".Equals(reference.ProcessorArchitecture, StringComparison.OrdinalIgnoreCase)) { refDependency.type = "gac_32"; } refDependency.version = reference.Version ?? "1.0.0.0"; refDependency.scope = "system"; System.Reflection.Assembly a = System.Reflection.Assembly.Load(gacUtil.GetAssemblyInfo(reference.Name)); if (reference.PublicKeyToken != null) { refDependency.classifier = reference.PublicKeyToken; } else { int start = a.FullName.IndexOf("PublicKeyToken="); int length = (a.FullName.Length)-start; refDependency.classifier = a.FullName.Substring(start,length); refDependency.classifier = refDependency.classifier.Replace("PublicKeyToken=", ""); } refDependency.systemPath = a.Location; return refDependency; } bool isPathReference = false; // resolve using system path if (!string.IsNullOrEmpty(reference.HintFullPath) && new FileInfo(reference.HintFullPath).Exists) { // silent for re-import //commented out //if (projectDigest.ExistingPom != null) //{ // return null; //} //else { string prjRefPath = Path.Combine(projectDigest.FullDirectoryName, ".references"); //verbose for new-import if (!reference.HintFullPath.ToLower().StartsWith(prjRefPath.ToLower()) && !reference.Name.Contains("Interop")) { MessageBox.Show( string.Format("Warning: Build may not be portable if local references are used, Reference is not in Maven Repository or in GAC." + "\nReference: {0}" + "\nDeploying the reference to a Repository, will make the code portable to other machines", reference.HintFullPath ), "Add Reference", MessageBoxButtons.OK, MessageBoxIcon.Warning); isPathReference = true; } } // uncomment this if systemPath is supported Dependency refDependency = new Dependency(); if (!isPathReference) { refDependency.artifactId = reference.Name; //get version from the name above the last path string[] pathTokens = reference.HintFullPath.Split("\\\\".ToCharArray()); refDependency.groupId = pathTokens[pathTokens.Length - 3]; refDependency.version = pathTokens[pathTokens.Length-2].Replace(reference.Name+"-","") ?? "1.0.0.0"; //refDependency.version = reference.Version ?? "1.0.0.0"; refDependency.type = "library"; //refDependency.scope = "system"; //refDependency.systemPath = reference.HintFullPath; } else { refDependency.artifactId = reference.Name; refDependency.groupId = reference.Name; refDependency.version = reference.Version ?? "1.0.0.0"; refDependency.type = "library"; refDependency.scope = "system"; refDependency.systemPath = reference.HintFullPath; } return refDependency; } return null; } Artifact.Artifact GetArtifactFromRepoUsingEmbeddedAssemblyVersionNumber(Reference reference) { foreach (Artifact.Artifact artifact in localArtifacts) { if (artifact.ArtifactId.Equals(reference.Name, StringComparison.OrdinalIgnoreCase) && artifact.Version.Equals(reference.Version, StringComparison.OrdinalIgnoreCase)) { return artifact; } } return null; } Artifact.Artifact GetArtifactFromRepoUsingHintPathVersionNumber(Reference reference) { if (string.IsNullOrEmpty(reference.HintFullPath) || !(new FileInfo(reference.HintFullPath).Exists)) { return null; } return artifactContext.GetArtifactRepository().GetArtifact(new FileInfo(reference.HintFullPath)); } protected Artifact.Artifact GetArtifact(Reference reference) { //if (string.IsNullOrEmpty(reference.Name) || string.IsNullOrEmpty(reference.Version)) //{ // return null; //} //if (localArtifacts == null) //{ // return null; //} try { Artifact.Artifact artifact = GetArtifactFromRepoUsingEmbeddedAssemblyVersionNumber(reference); if (artifact == null) { artifact = GetArtifactFromRepoUsingHintPathVersionNumber(reference); } if (artifact == null) { artifact = artifactContext.GetArtifactRepository().GetArtifact(new FileInfo(reference.HintPath)); } return artifact; } catch { return null; } } protected string GetSourceDir() { if (projectDigest.Compiles.Length == 0) { return "./"; } DirectoryInfo common = new DirectoryInfo(Path.GetDirectoryName(projectDigest.Compiles[0].IncludeFullPath)); foreach (Compile compile in projectDigest.Compiles) { DirectoryInfo srcInclude = new DirectoryInfo(Path.GetDirectoryName(compile.IncludeFullPath)); common = PomHelperUtility.GetCommonDirectory(common, srcInclude); } DirectoryInfo prjDir = new DirectoryInfo(Path.GetDirectoryName(projectDigest.FullFileName)); string srcDir = PomHelperUtility.GetRelativePath(prjDir, common); if (string.IsNullOrEmpty(srcDir)) { return "./"; } if (srcDir.Contains("..")) { return "./"; } return srcDir; } protected string GetDefineConfigurationValue() { List defines = new List(); if (!string.IsNullOrEmpty(projectDigest.MyType)) { defines.Add(string.Format("_MyType=\"{0}\"", projectDigest.MyType)); } if (!string.IsNullOrEmpty(projectDigest.Platform)) { defines.Add(string.Format("PLATFORM=\"{0}\"", projectDigest.Platform)); } return string.Join(",", defines.ToArray()); } } }