/// <summary>
        /// Gets the path of the data file directory used by <see cref="FileDataProvider"/>s
        /// managed by <c>this</c>, relative to the base uri of the <see cref="Presentation"/>
        /// owning the file data provider manager.
        /// </summary>
        /// <returns>The path</returns>
        /// <remarks>
        /// The DataFileDirectory is initialized lazily:
        /// If the DataFileDirectory has not been explicitly initialized using the <see cref="DataFileDirectory"/> setter,
        /// retrieving <see cref="DataFileDirectory"/> will assing it the default value "Data"</remarks>

        ////{
        ////    get
        ////    {
        ////        if (mDataFileDirectory == null) mDataFileDirectory = "Data";
        ////        return mDataFileDirectory;
        ////    }
        ////set
        ////{
        ////    if (value == null)
        ////    {
        ////        throw new exception.MethodParameterIsNullException(
        ////            "The DataFileDirectory can not be null");
        ////    }
        ////    if (mDataFileDirectory != null)
        ////    {
        ////        throw new exception.IsAlreadyInitializedException(
        ////            "The DataProviderManager has already been initialized with a DataFileDirectory");
        ////    }
        ////    Uri tmp;
        ////    if (!Uri.TryCreate(value, UriKind.Relative, out tmp))
        ////    {
        ////        throw new exception.InvalidUriException(String.Format(
        ////                                                    "DataFileDirectory must be a relative Uri, '{0}' is not",
        ////                                                    value));
        ////    }

        ////    if (!Directory.Exists(value))
        ////    {
        ////        Directory.CreateDirectory(value);
        ////    }
        ////    mDataFileDirectory = value;
        ////}
        ////}

        ///// <summary>
        ///// Moves the data file directory of the manager
        ///// </summary>
        ///// <param name="newDataFileDir">The new data file direcotry</param>
        ///// <param name="deleteSource">A <see cref="bool"/> indicating if the source/old data files shlould be deleted</param>
        ///// <param name="overwriteDestDir">A <see cref="bool"/> indicating if the new data directory should be overwritten</param>
        //public void MoveDataFiles(string newDataFileDir, bool deleteSource, bool overwriteDestDir)
        //{
        //    if (Path.IsPathRooted(newDataFileDir))
        //    {
        //        throw new exception.MethodParameterIsOutOfBoundsException(
        //            "The data file directory path must be relative");
        //    }
        //    string oldPath = DataFileDirectoryFullPath;
        //    mDataFileDirectory = newDataFileDir;
        //    string newPath = DataFileDirectoryFullPath;
        //    try
        //    {
        //        if (Directory.Exists(newPath))
        //        {
        //            if (overwriteDestDir)
        //            {
        //                Directory.Delete(newPath);
        //            }
        //            else
        //            {
        //                throw new exception.OperationNotValidException(
        //                    String.Format("Directory {0} already exists", newPath));
        //            }
        //        }
        //        CopyDataFiles(oldPath, newPath);
        //        if (deleteSource && Directory.Exists(oldPath))
        //        {
        //            Directory.Delete(oldPath);
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        throw new exception.OperationNotValidException(
        //            String.Format("Could not move data files to {0}: {1}", newPath, e.Message),
        //            e);
        //    }
        //}

        private string getDataFileDirectoryFullPath(Uri baseUri)
        {
            if (!baseUri.IsFile)
            {
                throw new exception.InvalidUriException(
                          "The base Uri of the presentation to which the DataProviderManager belongs must be a file Uri");
            }

            string localPathPresentation = baseUri.LocalPath;

            if (File.Exists(localPathPresentation))
            {
                localPathPresentation = Path.GetDirectoryName(localPathPresentation);
            }

            string localPath = Path.Combine(localPathPresentation, DataFileDirectory);

            //Uri dataFileDirUri = new Uri(baseUri, DataFileDirectory);
            //string localPath = dataFileDirUri.LocalPath;

            if (!Directory.Exists(localPath))
            {
                FileDataProvider.CreateDirectory(localPath);
            }

            return(localPath);
        }
Esempio n. 2
0
        public static void MoveDirectory(string sourcePath, string destPath)
        {
            if (!Directory.Exists(destPath))
            {
                FileDataProvider.CreateDirectory(destPath);
            }

            foreach (string file in Directory.GetFiles(sourcePath))
            {
                string dest = Path.Combine(destPath, Path.GetFileName(file));
                File.Move(file, dest);
                try
                {
                    File.SetAttributes(dest, FileAttributes.Normal);
                }
                catch
                {
                }
            }

            foreach (string folder in Directory.GetDirectories(sourcePath))
            {
                string dest = Path.Combine(destPath, Path.GetFileName(folder));
                FileDataProvider.MoveDirectory(folder, dest);
            }

            FileDataProvider.DeleteDirectory(sourcePath);
        }
Esempio n. 3
0
        public override bool ValueEquals(WithPresentation other)
        {
            if (!base.ValueEquals(other))
            {
                return(false);
            }

            FileDataProvider otherz = other as FileDataProvider;

            if (otherz == null)
            {
                return(false);
            }

            if (otherz.MimeType != MimeType)
            {
                //System.Diagnostics.Debug.Fail("! ValueEquals !");
                return(false);
            }

            if (Presentation.DataProviderManager.CompareByteStreamsDuringValueEqual &&
                other.Presentation.DataProviderManager.CompareByteStreamsDuringValueEqual)
            {
                if (!DataProviderManager.CompareDataProviderContent(this, otherz))
                {
                    //System.Diagnostics.Debug.Fail("! ValueEquals !");
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Reads the attributes of a DataProviderManager xuk element.
        /// </summary>
        /// <param name="source">The source <see cref="XmlReader"/></param>
        protected override void XukInAttributes(XmlReader source)
        {
            base.XukInAttributes(source);

            string dataFileDirectoryPath = source.GetAttribute(XukStrings.DataFileDirectoryPath);

            if (string.IsNullOrEmpty(dataFileDirectoryPath))
            {
                throw new exception.XukException(
                          "dataFileDirectoryPath attribute is missing from DataProviderManager element");
            }

            DataFileDirectory = FileDataProvider.UriDecode(dataFileDirectoryPath);
        }
Esempio n. 5
0
        public void CopyFileDataProvidersToDataFolder(string fullDataFolderPath)
        {
            if (!Directory.Exists(fullDataFolderPath))
            {
                FileDataProvider.CreateDirectory(fullDataFolderPath);
            }

            reportProgress(-1, Path.GetFileName(fullDataFolderPath));

            int       index        = 0;
            const int progressStep = 10;
            int       progress     = progressStep;

            List <FileDataProvider> list = new List <FileDataProvider>(m_Presentation.DataProviderManager.ManagedFileDataProviders);

            foreach (FileDataProvider fdp in list)
            {
                index++;
                progress = 100 * index / list.Count;

                reportProgress_Throttle(progress, string.Format("{1}/{2} ({0})", fdp.DataFileRelativePath, index, list.Count));


                if (RequestCancellation)
                {
                    return;
                }

                string pathSource = Path.Combine(m_Presentation.DataProviderManager.DataFileDirectoryFullPath, fdp.DataFileRelativePath);
                if (!File.Exists(pathSource))
                {
                    throw new exception.DataMissingException(String.Format("File does not exist: {0}", pathSource));
                }
                string pathDest = Path.Combine(fullDataFolderPath, fdp.DataFileRelativePath);
                if (!File.Exists(pathDest))
                {
                    File.Copy(pathSource, pathDest);
                    try
                    {
                        File.SetAttributes(pathDest, FileAttributes.Normal);
                    }
                    catch
                    {
                    }
                }
            }
        }
Esempio n. 6
0
        public void InitByMovingExistingFile(string path)
        {
            if (!File.Exists(path))
            {
                throw new exception.DataMissingException(
                          String.Format("The data file {0} does not exist", path));
            }

            if (File.Exists(DataFileFullPath))
            {
                throw new exception.OperationNotValidException(
                          String.Format("The data file {0} already exists", DataFileFullPath));
            }

            foreach (DataProvider dp in Presentation.DataProviderManager.ManagedObjects.ContentsAs_Enumerable)
            {
                if (dp is FileDataProvider)
                {
                    FileDataProvider fdp = (FileDataProvider)dp;
                    if (fdp.DataFileFullPath == path)
                    {
                        throw new exception.OperationNotValidException(
                                  String.Format("The data file {0} is already managed", path));
                    }
                }
            }

            //Directory.GetParent(filePath).FullName
            //if (Path.GetDirectoryName(path) != Presentation.DataProviderManager.DataFileDirectoryFullPath)
            //{
            //    throw new exception.OperationNotValidException(
            //        String.Format("The data file {0} is not in the data directory {1}", path, Presentation.DataProviderManager.DataFileDirectoryFullPath));
            //}

            File.Move(path, DataFileFullPath);
            try
            {
                File.SetAttributes(DataFileFullPath, FileAttributes.Normal);
            }
            catch
            {
            }

            HasBeenInitialized = true;
        }
Esempio n. 7
0
 /// <summary>
 /// Exports <c>this</c> to a given destination <see cref="Presentation"/>
 /// </summary>
 /// <param name="destPres">The destination <see cref="Presentation"/></param>
 /// <returns>The exported <see cref="FileDataProvider"/></returns>
 public override DataProvider Export(Presentation destPres)
 {
     lock (m_lock)
     {
         FileDataProvider expFDP  = destPres.DataProviderFactory.Create <FileDataProvider>(MimeType);
         Stream           thisStm = OpenInputStream_NoLock();
         try
         {
             if (thisStm.Length > 0)
             {
                 expFDP.AppendData(thisStm, thisStm.Length);
             }
         }
         finally
         {
             thisStm.Close();
         }
         return(expFDP);
     }
 }
        private void XukInDataProviderItem(XmlReader source, IProgressHandler handler)
        {
            if (!source.IsEmptyElement)
            {
                string uid = XukAble.ReadXukAttribute(source, XukAble.Uid_NAME);

                bool addedProvider = false;
                while (source.Read())
                {
                    if (source.NodeType == XmlNodeType.Element)
                    {
                        DataProvider prov = Presentation.DataProviderFactory.Create_SkipManagerInitialization("", source.LocalName, source.NamespaceURI);
                        if (prov != null)
                        {
                            if (addedProvider)
                            {
                                throw new exception.XukException(
                                          "Multiple DataProviders within the same mDataProviderItem is not supported");
                            }

                            prov.XukIn(source, handler);

                            //string uid_ = source.GetAttribute(XukStrings.Uid);
                            if (string.IsNullOrEmpty(prov.Uid) && !string.IsNullOrEmpty(uid))
                            {
                                prov.Uid = uid;
                            }

                            Presentation.DataProviderManager.AddManagedObject_NoSafetyChecks(prov, prov.Uid);

                            //if (IsManagerOf(prov.Uid))
                            //{
                            //    if (GetManagedObject(prov.Uid) != prov)
                            //    {
                            //        throw new exception.XukException(
                            //            String.Format("Another DataProvider exists in the manager with uid {0}", prov.Uid));
                            //    }
                            //}
                            //else
                            //{
                            //    SetUidOfManagedObject(prov, prov.Uid);
                            //}
                            if (prov is FileDataProvider)
                            {
                                FileDataProvider fdProv = (FileDataProvider)prov;

                                foreach (string path in mXukedInFilDataProviderPaths)
                                {
                                    if (path.Equals(fdProv.DataFileRelativePath, StringComparison.OrdinalIgnoreCase))
                                    {
                                        throw new exception.XukException(String.Format(
                                                                             "Another FileDataProvider using data file {0} has already been Xukked in",
                                                                             fdProv.DataFileRelativePath));
                                    }
                                }

                                if (!File.Exists(fdProv.DataFileFullPath))
                                {
                                    Presentation.DataProviderManager.RemoveManagedObject(fdProv);
                                    return;
                                }
                                mXukedInFilDataProviderPaths.Add(fdProv.DataFileRelativePath);
                            }

                            addedProvider = true;
                        }
                        else if (!source.IsEmptyElement)
                        {
                            source.ReadSubtree().Close();
                        }
                    }
                    else if (source.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }
                    if (source.EOF)
                    {
                        throw new exception.XukException("Unexpectedly reached EOF");
                    }
                }
            }
        }
        private void XukInDataProvider(XmlReader source, IProgressHandler handler)
        {
            if (source.NodeType == XmlNodeType.Element)
            {
                DataProvider prov = Presentation.DataProviderFactory.Create_SkipManagerInitialization("", source.LocalName, source.NamespaceURI);
                if (prov != null)
                {
                    prov.XukIn(source, handler);

                    //if (prov is FileDataProvider
                    //    && !File.Exists(((FileDataProvider)prov).DataFileFullPath))
                    //{
                    // SEE BELOW
                    //}

                    //string uid = source.GetAttribute(XukStrings.Uid);
                    if (string.IsNullOrEmpty(prov.Uid))
                    {
                        throw new exception.XukException("uid attribute of mDataProviderItem element is missing");
                    }

                    Presentation.DataProviderManager.AddManagedObject_NoSafetyChecks(prov, prov.Uid);
                    //if (IsManagerOf(prov.Uid))
                    //{
                    //    if (GetManagedObject(prov.Uid) != prov)
                    //    {
                    //        throw new exception.XukException(
                    //            String.Format("Another DataProvider exists in the manager with uid {0}", prov.Uid));
                    //    }
                    //}
                    //else
                    //{
                    //    SetUidOfManagedObject(prov, prov.Uid);
                    //}

                    if (prov is FileDataProvider)
                    {
                        FileDataProvider fdProv = (FileDataProvider)prov;

                        foreach (string path in mXukedInFilDataProviderPaths)
                        {
                            if (path.Equals(fdProv.DataFileRelativePath, StringComparison.OrdinalIgnoreCase))
                            {
                                throw new exception.XukException(String.Format(
                                                                     "Another FileDataProvider using data file {0} has already been Xukked in",
                                                                     fdProv.DataFileRelativePath));
                            }
                        }


                        if (!File.Exists(fdProv.DataFileFullPath))
                        {
                            Presentation.DataProviderManager.RemoveManagedObject(prov);
                            return;
                        }
                        mXukedInFilDataProviderPaths.Add(fdProv.DataFileRelativePath);
                    }
                }
                else if (!source.IsEmptyElement)
                {
                    source.ReadSubtree().Close();
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Writes data from data provider stream to a file external to project
        /// </summary>
        /// <param name="exportFilePath"></param>
        /// <param name="canOverwrite"></param>
        public virtual void ExportDataStreamToFile(string exportFilePath, bool canOverwrite)
        {
            if (exportFilePath == null)
            {
                throw new exception.MethodParameterIsNullException("external file path cannot be null");
            }

            if (File.Exists(exportFilePath) && !canOverwrite)
            {
                throw new System.Exception("Export file with same name already exists");
            }

            Stream source = OpenInputStream();

            if (source.Length == 0)
            {
                source.Close();
                throw new exception.InputStreamIsTooShortException("The data provider has no data to export to external file");
            }

            const uint BUFFER_SIZE = 1024 * 1024; // 1 MB MAX BUFFER

            string parentdir = Path.GetDirectoryName(exportFilePath);

            if (!Directory.Exists(parentdir))
            {
                FileDataProvider.CreateDirectory(parentdir);
            }

            FileStream exportFileStream = null;

            try
            {
                //exportFileStream = File.Create(exportFilePath);
                exportFileStream = new FileStream(exportFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
            }
            catch (Exception ex)
            {
                source.Close();

#if DEBUG
                Debugger.Break();
#endif
                throw;
            }

            try
            {
                StreamUtils.Copy(source, 0, exportFileStream, BUFFER_SIZE);

                //if (source.Length <= BUFFER_SIZE)
                //{
                //    byte[] buffer = new byte[source.Length];
                //    int read = source.Read(buffer, 0, (int)source.Length);
                //    exportFileStream.Write(buffer, 0, read);
                //}
                //else
                //{
                //    byte[] buffer = new byte[BUFFER_SIZE];
                //    int bytesRead = 0;
                //    while ((bytesRead = source.Read(buffer, 0, BUFFER_SIZE)) > 0)
                //    {
                //        exportFileStream.Write(buffer, 0, bytesRead);
                //    }

                //}
            }
            finally
            {
                exportFileStream.Close();
                source.Close();
            }
        }
Esempio n. 11
0
        public void Rename(string prefix)
        {
            if (mOpenOutputStream != null)
            {
#if DEBUG
                Debugger.Break();
#endif
                return;
            }
            if (mOpenInputStreams.Count > 0)
            {
#if DEBUG
                Debugger.Break();
#endif
                return;
            }

            string oldPrefix = m_NamePrefix;
            string oldName   = DataFileRelativePath;
            string oldPath   = DataFileFullPath;

            m_NamePrefix = prefix;

            mDataFileRelativePath = null;

            string newName = DataFileRelativePath;
            string newPath = DataFileFullPath;

#if DEBUG
            DebugFix.Assert(!File.Exists(newPath));
#endif

#if DEBUG
            foreach (DataProvider dp in Presentation.DataProviderManager.ManagedObjects.ContentsAs_Enumerable)
            {
                if (dp is FileDataProvider)
                {
                    FileDataProvider fdp = (FileDataProvider)dp;
                    if (fdp == this)
                    {
                        continue;
                    }

                    if (fdp.DataFileFullPath == newPath)
                    {
                        Debugger.Break();
                    }
                }
            }
#endif

            if (!File.Exists(oldPath))
            {
#if DEBUG
                Debugger.Break();
#endif
                HasBeenInitialized = false;
                return;
            }

            try
            {
                File.Move(oldPath, DataFileFullPath);
                try
                {
                    File.SetAttributes(DataFileFullPath, FileAttributes.Normal);
                }
                catch
                {
                }

                HasBeenInitialized = true;
            }
            catch (Exception ex)
            {
#if DEBUG
                Debugger.Break();
#endif
                m_NamePrefix          = oldPrefix;
                mDataFileRelativePath = oldName;

                newName = DataFileRelativePath;
                newPath = DataFileFullPath;
            }
        }