Defines a Mime Content Type
コード例 #1
0
		/// <summary>Read Element named mime-type. </summary>
		private MimeType ReadMimeType(XmlElement element)
		{
			String name = null;
			String description = null;
			MimeType type;
			XmlNamedNodeMap attrs = element.Attributes;
			for (int i = 0; i < attrs.Count; i++)
			{
				XmlAttribute attr = (XmlAttribute) attrs.Item(i);
				if (attr.Name.Equals("name"))
				{
					name = attr.Value;
				}
				else if (attr.Name.Equals("description"))
				{
					description = attr.Value;
				}
			}
			if ((name == null) || (name.Trim().Equals("")))
			{
				return null;
			}

			try
			{
				//Trace.WriteLine("Mime type:" + name);
				type = new MimeType(name);
			}
			catch (MimeTypeException mte)
			{
				// Mime Type not valid... just ignore it
				Trace.WriteLine(mte.ToString() + " ... Ignoring!");
				return null;
			}

			type.Description = description;

			XmlNodeList nodes = element.ChildNodes;
			for (int i = 0; i < nodes.Count; i++)
			{
				XmlNode node = nodes.Item(i);
				if (Convert.ToInt16(node.NodeType) == (short) XmlNodeType.Element)
				{
					XmlElement nodeElement = (XmlElement) node;
					if (nodeElement.Name.Equals("ext"))
					{
						ReadExt(nodeElement, type);
					}
					else if (nodeElement.Name.Equals("magic"))
					{
						ReadMagic(nodeElement, type);
					}
				}
			}
			return type;
		}
コード例 #2
0
		/// <summary>Read Element named magic. </summary>
		private void ReadMagic(XmlElement element, MimeType mimeType)
		{
			// element.getValue();
			String offset = null;
			String content = null;
			String type = null;
			var attrs = element.Attributes;
			for (int i = 0; i < attrs.Count; i++)
			{
				XmlAttribute attr = (XmlAttribute) attrs.Item(i);
				if (attr.Name.Equals("offset"))
				{
					offset = attr.Value;
				}
				else if (attr.Name.Equals("type"))
				{
					type = attr.Value;
					if (String.Compare(type, "byte", true) == 0)
					{
						type = "System.Byte";
					}
				}
				else if (attr.Name.Equals("value"))
				{
					content = attr.Value;
				}
			}
			if ((offset != null) && (content != null))
			{
				mimeType.AddMagic(Int32.Parse(offset), type, content);
			}
		}
コード例 #3
0
		/// <summary> Add the specified mime-type in the repository.</summary>
		/// <param name="type">is the mime-type to add.
		/// </param>
		internal void Add(MimeType type)
		{
			_typesIdx[type.Name] = type;
			_types.Add(type);
			// Update minLentgth
			_minLength = Math.Max(_minLength, type.MinLength);
			// Update the extensions index...
			String[] exts = type.Extensions;
			if (exts != null)
			{
				foreach (string ext in exts)
				{
					IList list = (IList) _extIdx[ext];
					if (list == null)
					{
						// No type already registered for this extension...
						// So, create a list of types
						list = new ArrayList();
						_extIdx[ext] = list;
					}
					list.Add(type);
				}
			}
			// Update the magics index...
			if (type.HasMagic())
			{
				_mimeTypes.Add(type);
			}
		}
コード例 #4
0
		/// <summary>Read Element named ext. </summary>
		private void ReadExt(XmlElement element, MimeType type)
		{
			XmlNodeList nodes = element.ChildNodes;
			for (int i = 0; i < nodes.Count; i++)
			{
				XmlNode node = nodes.Item(i);
				if (Convert.ToInt16(node.NodeType) == (short) XmlNodeType.Text)
				{
					type.AddExtension(((XmlText) node).Data);
				}
			}
		}
コード例 #5
0
		/// <summary> Add the specified mime-types in the repository.</summary>
		/// <param name="types">are the mime-types to add.
		/// </param>
		internal void Add(MimeType[] types)
		{
			if (types == null)
			{
				return;
			}
			foreach (MimeType t in types)
			{
				Add(t);
			}
		}