private void button5_Click( object sender, EventArgs e ) { Tag newTag = new Tag(tagListFull[textBox4.Text], textBox5.Text); TagFactory factory = TagFactory.getInstance(); factory.writeTag(textBox3.Text, newTag); }
//Reads all Tags from the XML file, //creates Tag objects, and puts them in a lookup table public Dictionary<string, Dictionary<string, Tag>> readTags( Dictionary<string, TagType> tagTypeList ) { Dictionary<string, Dictionary<string, Tag>> list = new Dictionary<string, Dictionary<string, Tag>>(); Dictionary<string, Tag> tagList; Tag newTag; TagType type; foreach (XmlElement node in tagRoot.ChildNodes) { tagList = new Dictionary<string, Tag>(); foreach (XmlElement tagNode in node.ChildNodes) { if (tagTypeList.ContainsKey(tagNode.GetAttribute("type"))) { type = tagTypeList[tagNode.GetAttribute("type")]; string val = tagNode.GetAttribute("value"); switch (type.dataType) { case Constants.DATATYPE_DATE: newTag = new Tag(type, DateTime.Parse(val)); break; case Constants.DATATYPE_INT: newTag = new Tag(type, Int16.Parse(val)); break; case Constants.DATATYPE_STRING: newTag = new Tag(type, val); break; default: newTag = new Tag(type, val); break; } tagList.Add(newTag.tagType.name, newTag); } } list.Add(node.GetAttribute("name"), tagList); } return list; }
//Creates or modifies a Tag in the XML file. Creates an image listing if necessary public void writeTag( string imageName, Tag tag ) { bool found = false; foreach (XmlElement node in tagRoot.ChildNodes) { if (node.GetAttribute("name").Equals(imageName)) { foreach (XmlElement tagNode in node.ChildNodes) { if (tagNode.GetAttribute("type").Equals(tag.tagType.name)) { //found the right type, change the value tagNode.SetAttribute("value", tag.tagValue.ToString()); found = true; } } if (found != true) { //create a new tag for the image XmlElement newTag = tagDoc.CreateElement("tag"); newTag.SetAttribute("type", tag.tagType.name); newTag.SetAttribute("value", tag.tagValue.ToString()); node.AppendChild(newTag); found = true; } } } if (found != true) { //create a new image, and a tag for it XmlElement newImage = tagDoc.CreateElement("image"); XmlElement newTag = tagDoc.CreateElement("tag"); newTag.SetAttribute("type", tag.tagType.name); newTag.SetAttribute("value", tag.tagValue.ToString()); newImage.SetAttribute("name", imageName); newImage.AppendChild(newTag); tagRoot.AppendChild(newImage); } //save! tagDoc.Save(FILE_TAG); }
//Remove the specified Tag from the XML file public void removeTag( string imageName, Tag tag ) { //loop through each image, looking for correct one foreach (XmlElement node in tagRoot.ChildNodes) { if (node.GetAttribute("name").Equals(imageName)) { foreach (XmlElement tagNode in node.ChildNodes) { if (tagNode.GetAttribute("type").Equals(tag.tagType.name)) { node.RemoveChild(tagNode); break; } } if (node.ChildNodes.Count == 0) { tagRoot.RemoveChild(node); } } } tagDoc.Save(FILE_TAG); }
/// <summary> /// Returns a list of all Tag objects in the tag type reference file specified /// in Constants.FILE_TAGS. /// </summary> /// <returns>A Tag list in <![CDATA[Dictionary<string name, Dictionary<string tagName, Tag tag>>]]> /// format where "name" is the name of the image the tag is applied to, "tagName" is the /// name of the tag.tagType object, and "tag" is the Tag object.</returns> public Dictionary<string, Dictionary<string, Tag>> readTags( Dictionary<string, TagType> tagTypeList ) { //create a new list of images Dictionary<string, Dictionary<string, Tag>> list = new Dictionary<string, Dictionary<string, Tag>>(); Dictionary<string, Tag> tagList; Tag newTag; TagType type; // SAMPLE tags.xml // <root> // <image name="image1.jpg"> // <tag name="tagType1" value="A String" /> // <tag name="tagType2" value="32" /> // </image> // </root> // //for each image foreach (XmlElement node in tagRoot.ChildNodes) { //Create a new list of tags tagList = new Dictionary<string, Tag>(); //for each TagType foreach (XmlElement tagNode in node.ChildNodes) { //there should only be one tag for each tagType if (tagTypeList.ContainsKey(tagNode.GetAttribute("type"))) { type = tagTypeList[tagNode.GetAttribute("type")]; string val = tagNode.GetAttribute("value"); switch (type.dataType) { case Constants.DATATYPE_DATE: newTag = new Tag(type, DateTime.Parse(val)); break; case Constants.DATATYPE_INT: newTag = new Tag(type, Int32.Parse(val)); break; case Constants.DATATYPE_STRING: newTag = new Tag(type, val); break; default: newTag = new Tag(type, val); break; } //Add the tag to the list of tags for a particular image tagList.Add(newTag.tagType.name, newTag); } } //Add the image to the list of images list.Add(node.GetAttribute("name"), tagList); } return list; }
/// <summary> /// Removes the specified Tag object from the specified image from the tags reference file /// specified in Constants.FILE_TAGS. /// </summary> /// <param name="imageName">The file name of the image to remove the Tag object from.</param> /// <param name="tag">The Tag object to be removed.</param> public void removeTag( string imageName, Tag tag ) { //loop through each image, looking for correct one foreach (XmlElement node in tagRoot.ChildNodes) { //if it's the correct image if (node.GetAttribute("name").Equals(imageName)) { //for every tag applied to the image foreach (XmlElement tagNode in node.ChildNodes) { //if it's the correct tag if (tagNode.GetAttribute("type").Equals(tag.tagType.name)) { //remove it from the list node.RemoveChild(tagNode); break; } } //remove from the reference file if (node.ChildNodes.Count == 0) { tagRoot.RemoveChild(node); } } } //save! tagDoc.Save(Constants.FILE_TAGS); }
/// <summary> /// Adds a new Tag to the system, and calls TagFactory to add it to the /// reference file. /// </summary> /// <param name="imageName">The full name of the image, including file extension, /// that the Tag object should be added to.</param> /// <param name="newValue">The value of the Tag object to be created.</param> /// <param name="tagType">The TagType of the Tag object to be created.</param> public void addTag( string imageName, string tagType, object newValue ) { //check that the tag type exists first if (tagTypeList.ContainsKey(tagType)) { //create a new tag Tag newTag = new Tag(tagTypeList[tagType], newValue); //write to the xml file tagFactory.writeTag(imageName, newTag); //if an image listing exists, add the tag if( tagList.ContainsKey( imageName )) { tagList[imageName].Add(tagType, newTag); } //create the image listing, and then add the tag else { Dictionary< string, Tag > newTaglist = new Dictionary<string, Tag>(); newTaglist.Add(tagType, newTag); tagList.Add(imageName, newTaglist); } } }
/// <summary> /// Removes the specified TagType from the system, and then calls /// TagFactory to remove the TagType from the reference file. /// /// This will remove all Tags of this TagType from any images which /// have them applied. /// </summary> /// <param name="imageName">The full name of the image, including file extension, /// of the image to remove the Tag from.</param> /// <param name="tag">The Tag object to be removed from the system.</param> public void removeTag( string imageName, Tag tag ) { tagFactory.removeTag(imageName, tag); tagList[imageName].Remove(tag.tagType.name); //removes the image from the tags reference file, if no tags exist; //this creates a smaller file size if (tagList[imageName].Count == 0) { tagList.Remove(imageName); } }
//remove an existing tag from the list, and from the xml file public void removeTag( string imageName, Tag tag ) { tagFactory.removeTag(imageName, tag); tagList[imageName].Remove(tag.tagType.name); if (tagList[imageName].Count == 0) { tagList.Remove(imageName); } }