private bool SetValue(object currentValue, out object newValue)
        {
            if (currentValue != null)
            {
                newValue = null;
                return(false);
            }

            EnvDTE.DTE dte = this.GetService <EnvDTE.DTE>(true);

            //first we need the xpath, which is needed by the manifest-recipe
            string manifestXpath          = "";
            string manifestXpathNamespace = "";
            IConfigurationService b       = (IConfigurationService)GetService(typeof(IConfigurationService));

            Microsoft.Practices.RecipeFramework.Configuration.Recipe recipe = b.CurrentRecipe;
            if (recipe.HostData.Any.Attributes["XPath"] != null)
            {
                manifestXpath = recipe.HostData.Any.Attributes["XPath"].Value;
            }
            if (recipe.HostData.Any.Attributes["XPathNamespace"] != null)
            {
                manifestXpathNamespace = recipe.HostData.Any.Attributes["XPathNamespace"].Value;
            }

            //if file is selected we check, if the xpath can be found in this item
            //if folder is selected we search in this folder for the elements.xml which contains the xpath
            if (dte.SelectedItems.Count > 0)
            {
                SelectedItem item         = dte.SelectedItems.Item(1);
                ProjectItem  selectedItem = null;

                if (item is ProjectItem)
                {
                    selectedItem = item as ProjectItem;
                }
                else if (item.ProjectItem is ProjectItem)
                {
                    selectedItem = item.ProjectItem as ProjectItem;
                }

                if (selectedItem != null)
                {
                    if (selectedItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                    {
                        //is the xml file clicked?
                        try
                        {
                            if (Helpers2.IsXPathInFile(selectedItem, manifestXpath, manifestXpathNamespace))
                            {
                                newValue = selectedItem;
                                return(true);
                            }
                        }
                        catch { }
                    }
                    else
                    {
                        //folder is clicked, we search for xml files below the folder which contains the needed xpath
                        foreach (ProjectItem childItem in selectedItem.ProjectItems)
                        {
                            if (Helpers2.IsXPathInFile(childItem, manifestXpath, manifestXpathNamespace))
                            {
                                newValue = childItem;
                                return(true);
                            }
                        }
                    }
                }
            }
            //string projectId = currentProject.

            newValue = null;
            return(false);
        }
Beispiel #2
0
        public override bool IsEnabledFor(object target)
        {
            Helpers.Log("XmlMergeReference");

            if (!(target is ProjectItem))
            {
                return(false);
            }

            if (!string.IsNullOrEmpty(this.SharePointVersions))
            {
                try
                {
                    if (!Helpers.SolutionHasVersion(Helpers.GetDTEFromTarget(target), SharePointVersions))
                    {
                        return(false);
                    }
                }
                catch (Exception)
                {
                }
            }

            try
            {
                if (XPath != "")
                {
                    //ok, item is clicked, we search now for a child element which contains the needed xpath,
                    //if we find a element, we return true
                    if (target is ProjectItem)
                    {
                        ProjectItem pitem = target as ProjectItem;
                        if (pitem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                        {
                            //is the xml file clicked?
                            try
                            {
                                if (pitem.Name.EndsWith(".XML", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return(Helpers2.IsXPathInFile(pitem, this.XPath, this.XPathNamespace));
                                }
                            }
                            catch { }
                        }
                        else
                        {
                            if (pitem.ProjectItems != null)
                            {
                                //folder is clicked, we search for xml files below the folder which contains the needed xpath
                                foreach (ProjectItem childItem in pitem.ProjectItems)
                                {
                                    if (childItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                                    {
                                        if (childItem.Name.EndsWith(".XML", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            if (Helpers2.IsXPathInFile(childItem, this.XPath, this.XPathNamespace))
                                            {
                                                return(true);
                                            }
                                        }
                                    }
                                    //sometimes files have childs
                                    foreach (ProjectItem subchildItem in childItem.ProjectItems)
                                    {
                                        if (subchildItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                                        {
                                            if (subchildItem.Name.EndsWith(".XML",
                                                                           StringComparison.InvariantCultureIgnoreCase))
                                            {
                                                if (Helpers2.IsXPathInFile(subchildItem, this.XPath, this.XPathNamespace))
                                                {
                                                    return(true);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return(false);
        }
        public override void Execute()
        {
            DTE     dte     = (DTE)this.GetService(typeof(DTE));
            Project project = this.GetTargetProject(dte);

            //1. transformiere Template
            string evaluatedTemplateFileName = EvaluateParameterAsString(dte, TemplateFileName);
            string contents = GenerateContent(evaluatedTemplateFileName, "");

            if (Helpers2.TemplateContentIsEmpty(contents))
            {
                return;
            }
            string evaluatedXPath = EvaluateParameterAsString(dte, XPath);

            //2. Finde das ProjectItem wo der XPath enthalten ist
            ProjectItem xmlElementManifestFile = null;

            if (SelectedItem != null)
            {
                if (SelectedItem.Kind == EnvDTE.Constants.vsProjectItemKindPhysicalFile)
                {
                    //is the xml file clicked?
                    try
                    {
                        if (Helpers2.IsXPathInFile(SelectedItem, evaluatedXPath, XPathNamespace))
                        {
                            xmlElementManifestFile = SelectedItem;
                        }
                    }
                    catch { }
                }
                else
                {
                    //folder is clicked, we search for xml files below the folder which contains the needed xpath
                    foreach (ProjectItem childItem in SelectedItem.ProjectItems)
                    {
                        if (Helpers2.IsXPathInFile(childItem, evaluatedXPath, XPathNamespace))
                        {
                            xmlElementManifestFile = childItem;
                        }
                        foreach (ProjectItem subchildItem in childItem.ProjectItems)
                        {
                            if (Helpers2.IsXPathInFile(subchildItem, evaluatedXPath, XPathNamespace))
                            {
                                xmlElementManifestFile = subchildItem;
                            }
                        }
                    }
                }

                if (xmlElementManifestFile != null)
                {
                    Helpers.EnsureCheckout(dte, xmlElementManifestFile);

                    //settings output parameters
                    CreatedElementFolder = xmlElementManifestFile.Collection.Parent as ProjectItem;
                    CreatedElementFile   = xmlElementManifestFile;

                    string pathToElementsXmlFile = Helpers.GetFullPathOfProjectItem(xmlElementManifestFile);

                    //bestehendes Dokument laden
                    XmlDocument existingDoc = new XmlDocument();
                    existingDoc.Load(pathToElementsXmlFile);

                    //backup for undo action
                    previousXmlContent = existingDoc.OuterXml;
                    previousXmlPath    = pathToElementsXmlFile;

                    //neues Xml laden
                    XmlDocument newdoc = new XmlDocument();
                    newdoc.LoadXml(contents);

                    if (string.IsNullOrEmpty(evaluatedXPath))
                    {
                        //Fall 1. kein xpath angegeben, documente werden gemerged
                        XmlDocument d   = AddXmlToDoc(existingDoc, newdoc);
                        string      xml = d.InnerXml;

                        XmlWriter xw2 = XmlWriter.Create(previousXmlPath, Helpers.GetXmlWriterSettings(previousXmlPath));
                        existingDoc.Save(xw2);
                        xw2.Flush();
                        xw2.Close();
                    }
                    else
                    {
                        //Fall 2. ein xpath ist angegeben für die Node, an die etwas angehängt werden soll
                        existingDoc = Transform(existingDoc, newdoc, evaluatedXPath, XPathNamespace);

                        XmlWriter xw2 = XmlWriter.Create(previousXmlPath, Helpers.GetXmlWriterSettings(previousXmlPath));
                        existingDoc.Save(xw2);
                        xw2.Flush();
                        xw2.Close();
                    }
                }
            }

            if (Open)
            {
                if (xmlElementManifestFile != null)
                {
                    Window window = xmlElementManifestFile.Open("{00000000-0000-0000-0000-000000000000}");
                    window.Visible = true;
                    window.Activate();

                    try
                    {
                        XPathDocument  doc       = new XPathDocument(previousXmlPath);
                        XPathNavigator navigator = doc.CreateNavigator();

                        XmlNamespaceManager newnsmgr = new XmlNamespaceManager(navigator.NameTable);
                        newnsmgr.AddNamespace("ns", XPathNamespace);
                        XPathNavigator iterator = navigator.SelectSingleNode(evaluatedXPath, newnsmgr);
                        IXmlLineInfo   lineInfo = ((IXmlLineInfo)iterator);

                        ((TextDocument)dte.ActiveDocument.Object("TextDocument")).Selection.GotoLine(lineInfo.LineNumber);
                        ((TextDocument)dte.ActiveDocument.Object("TextDocument")).Selection.PadToColumn(lineInfo.LinePosition);
                    }
                    catch { }
                }
            }
        }