Exemple #1
0
        protected override void GetChildItems(string path, bool recurse)
        {
            WriteVerbose(string.Format("XmlNavigationProvider::GetChildItems(Path = '{0}', recurse = '{1}')",
                                       path, recurse));

            string xpath = XmlProviderUtils.NormalizePath(path);

            // Don't need to remove the drive from the path since container provider infrastructure does that
            // for me automatically
            XmlDriveInfo drive = base.PSDriveInfo as XmlDriveInfo;
            XmlDocument  xml   = drive.XmlDocument;
            XmlNodeList  nodes = xml.SelectNodes(xpath, drive.NamespaceManager);

            // ------------------------------
            // NOTE: We could create an ErrorRecord and call WriteError() if SelectNodes() returns null.
            // In this case I decided not to because the fact that get-item returns
            // nothing indicates that case. For other operations such as clear-item or set-item
            // it is a good idea to throw that exception to indicate specifically why you're unable to
            // perform the action.
            // ------------------------------

            foreach (XmlNode node in nodes)
            {
                foreach (XmlNode innerNode in node.ChildNodes)
                {
                    WriteItemObject(innerNode, path, IsNodeContainer(innerNode));
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// get-item cmdlet callback
        /// </summary>
        /// <param name="path"></param>
        protected override void GetItem(string path)
        {
            WriteVerbose(string.Format("XmlItemProvider::GetItem(Path = '{0}')", path));
            string npath = XmlProviderUtils.NormalizePath(path);
            string xpath = XmlProviderUtils.PathNoDrive(npath);

            XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo);

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                WriteError(error);
            }

            XmlDocument xml   = drive.XmlDocument;
            XmlNodeList nodes = xml.SelectNodes(xpath, drive.NamespaceManager);

            // ------------------------------
            // NOTE: We could throw an ItemNotFoundException here if the nodelist returned is null
            // or empty. In this case I decided not to because the fact that get-item returns
            // nothing indicates that case. For other operations such as clear-item or set-item
            // it is a good idea to throw that exception to indicate specifically why you're unable to
            // perform the action.
            // ------------------------------

            foreach (XmlNode node in nodes)
            {
                WriteItemObject(node, path, false);
            }
        }
Exemple #3
0
        /// <summary>
        /// callback for remove-psdrive
        /// </summary>
        /// <param name="drive"></param>
        /// <returns></returns>
        protected override PSDriveInfo RemoveDrive(PSDriveInfo drive)
        {
            WriteVerbose("XmlDriveProvider::RemoveDrive()");
            XmlDriveInfo xmldrive = drive as XmlDriveInfo;

            xmldrive.XmlDocument.Save(xmldrive.Path);
            xmldrive.XmlDocument = null;
            return(base.RemoveDrive(drive));
        }
Exemple #4
0
        public XmlNode GetSingleXmlNodeFromPath(string path)
        {
            string       npath = XmlProviderUtils.NormalizePath(path);
            string       xpath = XmlProviderUtils.PathNoDrive(npath);
            XmlDriveInfo drive = base.PSDriveInfo as XmlDriveInfo;

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                ThrowTerminatingError(error);
            }
            XmlDocument xml = drive.XmlDocument;

            return(xml.SelectSingleNode(xpath));
        }
Exemple #5
0
        public XmlDocument GetXmlDocumentFromCurrentDrive()
        {
            XmlDriveInfo drive = base.PSDriveInfo as XmlDriveInfo;

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                WriteError(error);
                return(null);
            }

            XmlDocument xml = drive.XmlDocument;

            return(xml);
        }
Exemple #6
0
        private XmlNodeList GetXmlNodesFromPath(string path)
        {
            string npath = XmlProviderUtils.NormalizePath(path);
            string xpath = XmlProviderUtils.PathNoDrive(npath);

            XmlDriveInfo drive = XmlProviderUtils.GetDriveFromPath(path, base.ProviderInfo);

            if (drive == null)
            {
                ErrorRecord error = new ErrorRecord(new InvalidProgramException("Unable to retrieve the drive for this path"),
                                                    "drive", ErrorCategory.InvalidData, null);
                WriteError(error);
                return(null);
            }
            XmlDocument xml = drive.XmlDocument;

            return(xml.SelectNodes(xpath));
        }
Exemple #7
0
        protected override void RenameItem(string path, string newName)
        {
            WriteVerbose(string.Format("XmlNavigationProvider::RenameItem(Path = '{0}', newname = '{1}')", path, newName));

            // create new node with new name and then copy childnodes and attributes over
            // --------------------------------------------------------------------------
            string      xpath = XmlProviderUtils.NormalizePath(path);
            XmlNodeList nodes = GetXmlNodesFromPath(xpath);

            XmlDriveInfo drive  = base.PSDriveInfo as XmlDriveInfo;
            XmlDocument  xmldoc = drive.XmlDocument;

            foreach (XmlNode nd in nodes)
            {
                if (ShouldProcess(path))
                {
                    XmlNode newNode = xmldoc.CreateNode(nd.NodeType, newName, nd.ParentNode.NamespaceURI);
                    nd.ParentNode.ReplaceChild(newNode, nd);
                }
            }
        }
Exemple #8
0
        protected override bool ItemExists(string path)
        {
            WriteVerbose(string.Format("XmlNavigationProvider::ItemExists(Path = '{0}')", path));

            string xpath = XmlProviderUtils.NormalizePath(path);

            XmlDriveInfo drive = base.PSDriveInfo as XmlDriveInfo;

            if (drive == null)
            {
                return(false);
            }
            XmlDocument xml = drive.XmlDocument;

            if (xml.SelectSingleNode(xpath, drive.NamespaceManager) == null)
            {
                return(false);
            }
            else
            {
                return(true);
            }
        }
Exemple #9
0
        protected override void GetItem(string path)
        {
            WriteVerbose(string.Format("XmlNavigationProvider::GetItem(Path = '{0}')", path));
            string xpath = XmlProviderUtils.NormalizePath(path);

            // Don't need to remove the drive from the path since container provider infrastructure does that
            // for me automatically
            XmlDriveInfo drive = base.PSDriveInfo as XmlDriveInfo;
            XmlDocument  xml   = drive.XmlDocument;
            XmlNodeList  nodes = xml.SelectNodes(xpath, drive.NamespaceManager);

            // ------------------------------
            // NOTE: We could throw an ItemNotFoundException here if the nodelist returned is null
            // or empty. In this case I decided not to because the fact that get-item returns
            // nothing indicates that case. For other operations such as clear-item or set-item
            // it is a good idea to throw that exception to indicate specifically why you're unable to
            // perform the action.
            // ------------------------------

            foreach (XmlNode node in nodes)
            {
                WriteItemObject(node, path, false);
            }
        }
Exemple #10
0
        /// <summary>
        /// new-item cmdlet callback
        /// </summary>
        /// <param name="path"></param>
        /// <param name="itemTypeName"></param>
        /// <param name="newItemValue"></param>
        protected override void NewItem(string path, string itemTypeName, object newItemValue)
        {
            WriteVerbose(string.Format("XmlContainerProvider::RemoveItemNewItem(Path = '{0}', itemtype = '{1}', newvalue = '{2}')",
                                       path, itemTypeName, newItemValue));

            // first check if item already exists at that path
            // -----------------------------------------------
            string xpath = XmlProviderUtils.NormalizePath(path);

            // we need to get the parent of the new node so we can add to its children
            // we do this by chooping the last item from the path
            // for example: new item path = drive:/root/one/two
            // the parent node would be at drive:/root/one
            // --------------------------------------------
            XmlNode parent = GetParentNodeFromLeaf(xpath);

            string       endName = GetLeafNameFromPath(xpath);
            XmlDriveInfo drive   = base.PSDriveInfo as XmlDriveInfo;
            XmlDocument  xmldoc  = drive.XmlDocument;

            XmlNode newNode = xmldoc.CreateNode(itemTypeName, endName, parent.NamespaceURI);

            parent.AppendChild(newNode);
        }
Exemple #11
0
        protected override void NewItem(string path, string itemTypeName, object newItemValue)
        {
            WriteVerbose(string.Format("XmlNavigationProvider::RemoveItemNewItem(Path = '{0}', itemtype = '{1}', newvalue = '{2}')",
                                       path, itemTypeName, newItemValue));

            // first check if item already exists at that path
            // -----------------------------------------------
            string xpath = XmlProviderUtils.NormalizePath(path);

            // we need to get the parent of the new node so we can add to its children
            // we do this by chopping the last item from the path if there isn't already an item
            // at the path. in which case we need to check force flag or error out
            // for example: new item path = drive:/root/one/two
            // the parent node would be at drive:/root/one
            // --------------------------------------------
            XmlNode parent = null;

            XmlNode destNode = GetSingleXmlNodeFromPath(xpath);

            if (destNode != null)
            {
                parent = destNode.ParentNode;
                if (base.Force)
                {
                    destNode.ParentNode.RemoveChild(destNode);
                }
                else
                {
                    // write error
                    ErrorRecord err = new ErrorRecord(new ArgumentException("item already exists!"), "AlreadyExists",
                                                      ErrorCategory.InvalidArgument, path);
                    WriteError(err);
                    return;
                }
            }
            else
            {
                parent = GetParentNodeFromLeaf(xpath);
            }

            if (parent == null)
            {
                // write error
                ErrorRecord err = new ErrorRecord(new ItemNotFoundException("ParentPath doesn't exist"), "ObjectNotFound",
                                                  ErrorCategory.ObjectNotFound, path);
                WriteError(err);
                return;
            }

            string       endName = GetLastPathName(xpath);
            XmlDriveInfo drive   = base.PSDriveInfo as XmlDriveInfo;
            XmlDocument  xmldoc  = drive.XmlDocument;

            XmlNode newNode = xmldoc.CreateNode(itemTypeName, endName, parent.NamespaceURI);

            // lets call shouldprocess
            if (ShouldProcess(path))
            {
                parent.AppendChild(newNode);
            }
        }