/// <summary> /// Append the given profile to the Xml document stored in the given list of locations /// </summary> /// <param name="profile">The profile to be appended</param> /// <param name="locations">The list of locations containing the Xml documents for which /// the given profile is to be appended to</param> public static void AppendProfile(Profile profile, List<string> locations) { foreach (string path in locations) { try { XmlDocument xmlDoc = CommonXmlHelper.LoadXml(path); if (xmlDoc == null) { //if the xml does not exist, just write it to the place SaveProfile(profile, path); } else { //Loaded XML , replace the old profile data with a new. //Find the Profile with the same name XmlNode node = xmlDoc.SelectSingleNode(@"//" + ELE_PROFILE_ROOT + "[@" + ATTR_PROFILE_NAME + @"='" + profile.ProfileName + @"']"); XmlElement profileNode = CreateProfileElement(profile, xmlDoc); XmlNode profilingRootNode = xmlDoc.SelectSingleNode(@"//" + ELE_PROFILING_ROOT); if (node != null) { profilingRootNode.RemoveChild(node); } profilingRootNode.AppendChild(profileNode); } } catch (Exception) { } } }
public static Profile CreateDefaultProfile(string path) { Profile profile = new Profile("Unnamed Profile"); XmlDocument xml = ConvertToXMLDocument(profile); FileInfo fileInfo = new FileInfo(path); if (!fileInfo.Exists) { FileStream fs = null; try { fs = fileInfo.Create(); } catch (DirectoryNotFoundException) { Directory.CreateDirectory(fileInfo.Directory.FullName); fs = fileInfo.Create(); } finally { Debug.Assert(fs != null); try { fs.Close(); } catch (IOException) { } } } SaveProfile(xml, fileInfo.FullName); return profile; }
/// <summary> /// Save the given profile to the given location /// </summary> /// <param name="profile">The profile to be saved</param> /// <param name="location">The location for which the profile is to be saved to</param> public static void SaveProfile(Profile profile, string location) { XmlDocument xmlDoc = new XmlDocument(); XmlElement root = xmlDoc.CreateElement(ELE_PROFILING_ROOT); xmlDoc.AppendChild(root); XmlElement profileEle = CreateProfileElement(profile, xmlDoc); root.AppendChild(profileEle); CommonXmlHelper.SaveXml(xmlDoc, location); }
/// <summary> /// Merge the current profile to the list of profiles if they have the same profile name /// </summary> /// <param name="currentProfile">The current profile</param> /// <param name="profileList">The list of profiles for which the current profile is to be merged with</param> /// <returns>The current profile</returns> public static Profile Merge(Profile currentProfile, List<Profile> profileList) { foreach (Profile profile in profileList) { if (profile.ProfileName.ToLower().Equals(currentProfile.ProfileName.ToLower())){ currentProfile = Merge(currentProfile, profile); } } return currentProfile; }
public static bool SaveToAllDrive(Profile profile) { DriveInfo[] drives = DriveInfo.GetDrives(); XmlDocument xml = ConvertToXMLDocument(profile); UpdateLastUpdateTime(xml, DateTime.Now.Ticks); //Save to Root Directory FileInfo profileInfo = new FileInfo(ProfilingLayer.RELATIVE_PROFILING_ROOT_SAVE_PATH); SaveProfile(xml, profileInfo.FullName); foreach (DriveInfo driveInfo in drives) { FileInfo fileInfo = new FileInfo(ProfilingHelper.ExtractDriveName(driveInfo) + ":" + ProfilingLayer.RELATIVE_GUID_SAVE_PATH); if (fileInfo.Exists) { //GUID Exist profileInfo = new FileInfo(ProfilingHelper.ExtractDriveName(driveInfo) + ":" + ProfilingLayer.RELATIVE_PROFILING_SAVE_PATH); SaveProfile(xml, profileInfo.FullName); } } return true; }
/// <summary> /// Merge the current profile with the new profile /// </summary> /// <param name="currentProfile">The current profile</param> /// <param name="newProfile">The new profile</param> /// <returns>The current profile</returns> public static Profile Merge(Profile currentProfile, Profile newProfile) { foreach (ProfileDrive drive in newProfile.ProfileDriveList) { ProfileDrive curDrive = currentProfile.FindProfileDriveFromGUID(drive.Guid); if (curDrive == null) { currentProfile.AddProfileDrive(drive); } else { if (drive.DriveName.ToLower().Equals(curDrive.DriveName.ToLower())) { continue; } if (drive.LastUpdated > curDrive.LastUpdated) { curDrive.DriveName = drive.DriveName; curDrive.LastUpdated = DateTime.UtcNow.Ticks; } } } return currentProfile; }
/// <summary> /// Create a profile xml element storing the attributes of the given profile /// </summary> /// <param name="profile">The profile to be used to create the xml element</param> /// <param name="xmlDoc">The xml document for which the created xml element belongs to</param> /// <returns></returns> private static XmlElement CreateProfileElement(Profile profile, XmlDocument xmlDoc) { XmlElement profileElement = xmlDoc.CreateElement(ELE_PROFILE_ROOT); profileElement.SetAttribute(ATTR_PROFILE_NAME, profile.ProfileName); profileElement.SetAttribute(ATTR_PROFILE_LAST_UPDATED, profile.LastUpdatedTime + ""); PopulateDrive(profile.ProfileDriveList, profileElement, xmlDoc); return profileElement; }
/// <summary> /// Initialize the profiling layer /// </summary> /// <param name="paths">The list of paths to Init</param> /// <returns>True if the profile is load.</returns> public bool Init(List<string> paths) { string path = paths[0]; try { Profile p = ProfilingXMLHelper.LoadSingleProfile(path); if (p == null) { throw new ProfileLoadException(); } _profile = p; } catch (FileNotFoundException) { Profile profile = ProfilingXMLHelper.CreateDefaultProfile(path); _profile = profile; //Since the profile is newly created , no need to traverse all the drive } for (int i = 1; i < paths.Count; i++) { if (File.Exists(paths[i])) { List<Profile> profileList = ProfilingXMLHelper.LoadProfile(paths[i]); if (profileList == null) { continue; } ProfileMerger.Merge(_profile, profileList); } } DriveInfo[] driveList = DriveInfo.GetDrives(); foreach (DriveInfo driveinfo in driveList) { UpdateDrive(driveinfo); } return true; }
public static Profile ConvertToProfile(XmlDocument profilexml) { XmlNodeList list = profilexml.GetElementsByTagName("profile"); if (list.Count != 0) { XmlElement element = (XmlElement)list.Item(0); string profilename = element.GetAttribute("name"); string lastupdate = element.GetAttribute("lastupdated"); long lastUpdatedLong = long.Parse(lastupdate); Profile profile = new Profile(profilename); profile.LastUpdatedTime = lastUpdatedLong; profile = ProcessListing(profile, element); return profile; } return null; }
public static Profile ProcessListing(Profile profile, XmlElement root) { XmlNodeList nodeList = root.GetElementsByTagName("drive"); foreach (XmlNode node in nodeList) { XmlElement drive = (XmlElement)node; XmlElement logical = (XmlElement)drive.GetElementsByTagName("logical").Item(0); XmlElement guid = (XmlElement)drive.GetElementsByTagName("guid").Item(0); profile.CreateMapping(logical.InnerText, "", guid.InnerText); } return profile; }
public static XmlDocument ConvertToXMLDocument(Profile profile) { XmlDocument profilexml = new XmlDocument(); XmlElement root = CreateRoot(profilexml, profile); foreach (ProfileMapping mapping in profile.Mappings) { XmlElement map = CreateElementForMapping(profilexml, mapping); root.AppendChild(map); } profilexml.AppendChild(root); return profilexml; }
public static XmlElement CreateRoot(XmlDocument profilexml, Profile profile) { XmlElement root = profilexml.CreateElement("profile"); XmlAttribute nameAttr = profilexml.CreateAttribute("name"); XmlAttribute timeAttr = profilexml.CreateAttribute("lastupdated"); nameAttr.Value = profile.ProfileName; timeAttr.Value = profile.LastUpdatedTime+""; root.SetAttributeNode(nameAttr); root.SetAttributeNode(timeAttr); return root; }
/// <summary> /// Load only the default saved location /// </summary> /// <param name="path">The path for which the profile is to be loaded from</param> public void Init(string path) { try { Profile p = ProfilingXMLHelper.LoadSingleProfile(path); if (p == null) { throw new ProfileLoadException(); } _profile = p; } catch (FileNotFoundException) { Profile profile = ProfilingXMLHelper.CreateDefaultProfile(path); _profile = profile; } SetupDrives(); }
/// <summary> /// Creates a new ProfilingLayer object /// </summary> private ProfilingLayer() { _profile = new Profile(""); }
public Profile Merge(Profile profile) { if (CanMerge(profile)) { foreach (ProfileMapping mapping in profile.Mappings) { if(!Contains(mapping)) { CreateMapping(mapping); } } } return profile; }
public bool Init(string path) { try { Profile p = ProfilingXMLHelper.ConvertToProfile(path); Debug.Assert(p != null); _profile = p; } catch (FileNotFoundException) { Profile profile = ProfilingXMLHelper.CreateDefaultProfile(path); _profile = profile; //Since the profile is newly created , no need to traverse all the drive return true; } DriveInfo[] driveList = DriveInfo.GetDrives(); foreach (DriveInfo driveinfo in driveList) { #region Get Profiling XML FileInfo info = new FileInfo(driveinfo.RootDirectory.Name + RELATIVE_PROFILING_SAVE_PATH); if (info.Exists) { Profile profile = null; try { profile = ProfilingXMLHelper.ConvertToProfile(info.FullName); } catch (FileNotFoundException) { } if (profile == null) { //TODO throw EXCEPTION } else { try { _profile.Merge(profile); } catch (ProfileConflictException pce) { //Log ? } } } #endregion } foreach (DriveInfo driveinfo in driveList) { UpdateDrive(driveinfo); } SaveToAllUsedDrive(); return true; }
/// <summary> /// Create a profile from the attributes of the given profile xml element /// </summary> /// <param name="profileElement">The xml element for which the profile is to be created from</param> /// <returns>The profile created from the given profile xml element</returns> private static Profile CreateProfile(XmlElement profileElement) { string profileName = profileElement.GetAttribute(ATTR_PROFILE_NAME); Profile profile = new Profile(profileName); XmlNodeList driveList = profileElement.GetElementsByTagName(ELE_PROFILE_DRIVE_ROOT); foreach (XmlNode drive in driveList) { XmlElement driveElement = drive as XmlElement; if (driveElement != null) { ProfileDrive driveObj = CreateProfileDrive(driveElement); if (driveObj == null) { //Fail Load return null; } profile.AddProfileDrive(driveObj); } } return profile; }
/// <summary> /// Check whether the current profile has the same name as the new profile /// </summary> /// <param name="currentProfile">The current profile</param> /// <param name="newProfile">The new profile</param> /// <returns>True if current profile has the same name as new profile, else false</returns> public static bool CanMerge(Profile currentProfile, Profile newProfile) { return currentProfile.ProfileName.ToLower().Equals(newProfile.ProfileName.ToLower()); }
/// <summary> /// Create and save the default profile at the given path /// </summary> /// <param name="path">The path for which the default profile is to be saved to</param> /// <returns>The default profile created</returns> public static Profile CreateDefaultProfile(string path) { Profile profile = new Profile(DEFAULT_NAME); SaveProfile(profile, path); return profile; }
private ProfilingLayer() { _profile = new Profile("Unnamed"); }
public bool CanMerge(Profile profile) { try { foreach (ProfileMapping mapping in profile.Mappings) { Contains(mapping); } foreach (ProfileMapping mapping in Mappings) { profile.Contains(mapping); } } catch (ProfileMappingConflictException pmce) { throw new ProfileConflictException(pmce); } return true; }