Beispiel #1
0
		private int GetIntValueForTest(CmObject currentObject, XmlNode node)
		{
			int hvo = 0;
			int flid = GetFlidAndHvo(currentObject, node, ref hvo);
			if (flid == 0 || hvo == 0)
			{
				string sField = XmlUtils.GetOptionalAttributeValue(node, "field");
				if (String.IsNullOrEmpty(sField))
					return 0; // This is rather arbitrary...objects missing, what should each test do?
				try
				{
					Type type = currentObject.GetType();
					PropertyInfo info = type.GetProperty(sField,
						BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
					if (info != null)
					{
						object result = info.GetValue(currentObject, null);
						if (typeof(bool) == result.GetType())
							return ((bool)result) ? 1 : 0;
						else
							return (int)result;
					}
				}
				catch (Exception error)
				{
					throw new ApplicationException(string.Format("There was an error while trying to get the property {0}. One thing that has caused this in the past has been a database which was not migrated properly.", sField), error);
				}
				return 0; // This is rather arbitrary...objects missing, what should each test do?
			}
			switch (currentObject.Cache.GetFieldType(flid))
			{
				case FieldType.kcptBoolean:
					if (currentObject.Cache.GetBoolProperty(hvo, flid))
						return 1;
					else
						return 0;
				case FieldType.kcptInteger:
					return currentObject.Cache.GetIntProperty(hvo, flid);
				case FieldType.kcptOwningAtom:
				case FieldType.kcptReferenceAtom:
					return currentObject.Cache.GetObjProperty(hvo, flid);
				default:
					return 0;
			}
		}
Beispiel #2
0
		private string GetStringValueForTest(CmObject currentObject, XmlNode node)
		{
			int hvo = 0;
			int flid = GetFlidAndHvo(currentObject, node, ref hvo);
			if (flid == 0 || hvo == 0)
			{
				// Try for a property on the object.
				string sField = XmlUtils.GetOptionalAttributeValue(node, "field");
				if (String.IsNullOrEmpty(sField))
					return null;
				try
				{
					Type type = currentObject.GetType();
					PropertyInfo info = type.GetProperty(sField,
						BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);
					if (info != null)
					{
						object result = info.GetValue(currentObject, null);
						return result.ToString();
					}
				}
				catch (Exception error)
				{
					throw new ApplicationException(string.Format("There was an error while trying to get the property {0}. One thing that has caused this in the past has been a database which was not migrated properly.", sField), error);
				}
				return null; // This is rather arbitrary...objects missing, what should each test do?
			}
			switch (currentObject.Cache.GetFieldType(flid))
			{
				case FieldType.kcptUnicode:
				case FieldType.kcptBigUnicode:
					return currentObject.Cache.GetUnicodeProperty(hvo, flid);
				case FieldType.kcptString:
				case FieldType.kcptBigString:
					return currentObject.Cache.GetTsStringProperty(hvo, flid).Text;
				case FieldType.kcptMultiUnicode:
				case FieldType.kcptMultiBigUnicode:
					return currentObject.Cache.GetMultiUnicodeAlt(hvo, flid,
						GetSingleWritingSystemDescriptor(node),
						ViewNameForFlid(flid, currentObject.Cache.MetaDataCacheAccessor));
				case FieldType.kcptMultiString:
				case FieldType.kcptMultiBigString:
					return currentObject.Cache.GetMultiStringAlt(hvo, flid,
						GetSingleWritingSystemDescriptor(node)).Text;
				default:
					return null;
			}
		}
Beispiel #3
0
		protected void DumpObject(TextWriter contentsStream, CmObject currentObject, string sClassTag)
		{
			string className = m_cache.GetClassName((uint)currentObject.ClassID);
			XmlNode classNode = null;
			if (sClassTag != null && sClassTag.Length > 0)
			{
				className = className + "-" + sClassTag;
				classNode = GetClassTemplateNode(className);
			}
			if (classNode == null)
			{
				classNode = FindClassTemplateNode(currentObject.GetType(), sClassTag);
			}
			if (classNode == null)
			{
				return; // would have thrown an exception if that's what the template wanted
			}

			string sPreload = XmlUtils.GetOptionalAttributeValue(classNode, "preload");
			bool previousAssumeCacheSetting = m_cache.TestingOnly_AssumeCacheFullyLoaded;
			if (sPreload != null && sPreload.Length > 0)
			{
				m_cache.TestingOnly_AssumeCacheFullyLoaded = false;
				currentObject.GetType().InvokeMember(sPreload,
					BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance |
					BindingFlags.InvokeMethod, null, currentObject, null);
				m_cache.TestingOnly_AssumeCacheFullyLoaded = true;
			}



			if (m_filters != null)
			{
				foreach(IFilterStrategy filter in m_filters)
				{
					string explanation;
					if (!filter.DoInclude (currentObject, out explanation))
					{
						if (explanation==null)
							explanation = "none";

						XmlTextWriter writer = new XmlTextWriter(contentsStream);
						writer.WriteComment(String.Format(" Object filtered out by filter {0}, reason: {1} ", filter.Label, explanation));

						// would choke the parser later if there were reserved chars in there
						//contentsStream.Write("<!-- Object filtered out by filter " + filter.Label + ", reason: "+ explanation + " ");
						//	contentsStream.Write(" -->");

						return;
					}
				}
			}

			DoChildren(/*null,*/ contentsStream, currentObject, classNode, null);

			m_cache.TestingOnly_AssumeCacheFullyLoaded = previousAssumeCacheSetting;

		}
Beispiel #4
0
		protected object GetMethodResult(CmObject target, string methodName, object[] args)
		{
			Type type = target.GetType();
			MethodInfo mi = type.GetMethod(methodName);
			if (mi == null)
				throw new ConfigurationException ("There is no public method named '" + methodName + ".");
			object result = null;
			try
			{
				result = mi.Invoke(target, args);
			}
			catch (Exception error)
			{
				throw new ApplicationException (string.Format("There was an error while executing the method {0}.", methodName), error);
			}
			return result;
		}
Beispiel #5
0
		protected object GetProperty(CmObject target, string property)
		{
			if (target == null)
			{
				return null;
			}

			if (property == "Hvo")
			{
				return GetIdString(target.Hvo);
			}
			else if (property == "Guid")
			{
				return (target.Guid.ToString());
			}
			else if (property == "Owner")
			{
				return (CmObject.CreateFromDBObject(m_cache, m_cache.GetOwnerOfObject(target.Hvo)));
			}
			else if (property == "IndexInOwner")
			{
				return target.IndexInOwner.ToString();
			}
			Type type = target.GetType();
			PropertyInfo  info = type.GetProperty(property,BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy  );
			if (property.StartsWith("custom"))
			{
				return GetCustomFieldValue(target, property);
			}
			if (info == null)
			{
				throw new ConfigurationException ("There is no public property named '" + property + "' in "+type.ToString()+". Remember, properties often end in a two-character suffix such as OA,OS,RA, or RS.");
			}
			object result = null;
			try
			{
				result = info.GetValue(target,null);
				if (property.EndsWith("Hvo"))
				{
					int hvo = (int)result;
					if (hvo > 0)
						return GetIdString(hvo);
				}
			}
			catch (Exception error)
			{
				throw new ApplicationException (string.Format("There was an error while trying to get the property {0}. One thing that has caused this in the past has been a database which was not migrated properly.", property), error);
			}
			return result;
		}
Beispiel #6
0
		/// <summary>
		/// The &lt;refVector&gt; element is used when you just want to make a list of
		/// references to other elements that will be in the output file.
		/// </summary>
		/// <param name="contentsStream"></param>
		/// <param name="currentObject"></param>
		/// <param name="node"></param>
		protected void DoReferenceVectorElement(TextWriter contentsStream,
			CmObject currentObject, XmlNode node)
		{
			bool ordered = XmlUtils.GetBooleanAttributeValue(node, "ordered");
			string label = XmlUtils.GetOptionalAttributeValue(node, "itemLabel");
			if (label == null)
				label ="object";
			string field = XmlUtils.GetManditoryAttributeValue(node, "field");
			bool fXmlVirtual = XmlUtils.GetOptionalBooleanAttributeValue(node, "virtual", false);
			bool fWriteAsRelation = XmlUtils.GetOptionalBooleanAttributeValue(node, "writeAsRelation", false);
			//Debug.WriteLine ("<refVector field ="+field+">");

			int flid = (int)m_cache.MetaDataCacheAccessor.GetFieldId2(
				(uint)currentObject.ClassID, field, true);
			if (m_mapFlids.ContainsKey(flid))
				flid = m_mapFlids[flid];
			int[] hvos;
			if (flid <= 0)
			{
				if (fXmlVirtual)
				{
					hvos = LoadVirtualField(currentObject, field);
				}
				else
				{
					throw new ConfigurationException ("There is no field named '" + field + "' in "+currentObject.GetType().ToString()+". Remember that fields are the actual CELLAR names, so they do not have FDO suffixes like OA or RS.");
				}
			}
			else
			{
				Dictionary<int, List<int>> values = null;
				bool fMdcVirtual = m_cache.MetaDataCacheAccessor.get_IsVirtual((uint)flid);
				if (fMdcVirtual)
					values = GetCachedVirtuals(node, flid);
				if (values == null)
				{
					//int[] hvos = m_cache.GetVectorProperty(currentObject.Hvo, flid, true);
					// changed to false since for some reason, the data for POS slots and natural
					// classes was no longer being saved in the cache.
					hvos = m_cache.GetVectorProperty(currentObject.Hvo, flid, false);
				}
				else
				{
					List<int> hvoList;
					if (values.TryGetValue(currentObject.Hvo, out hvoList))
						hvos = hvoList.ToArray();
					else
						hvos = new int[0]; // assumes values is complete, if not found, empty.
				}
			}
			string property = XmlUtils.GetOptionalAttributeValue(node, "itemProperty");
			if (property == null)
				property = "ShortName";
			string wsProp = XmlUtils.GetOptionalAttributeValue(node, "itemWsProp");
			if (m_format == "xml")
			{
				int index = 0;
				bool fInternalTraits = XmlUtils.GetOptionalBooleanAttributeValue(node, "internalTraits", false);
				string sFieldMemberOf = XmlUtils.GetOptionalAttributeValue(node, "fieldMemberOf");
				string sFieldMemberOfTrait = XmlUtils.GetOptionalAttributeValue(node, "fieldMemberOfTrait");
				int flidMemberOf = 0;
				if (!String.IsNullOrEmpty(sFieldMemberOf) && !String.IsNullOrEmpty(sFieldMemberOfTrait))
				{
					flidMemberOf = (int)m_cache.MetaDataCacheAccessor.GetFieldId2((uint)currentObject.ClassID,
						sFieldMemberOf, true);
				}
				foreach (int hvo in hvos)
				{
					if (fWriteAsRelation)
					{
						string labelWs;
						string s;
						if (GetRefPropertyData(property, wsProp, hvo, out labelWs, out s))
						{
							if (ordered && hvos.Length > 1)
							{
								contentsStream.Write("<relation type=\"{0}\" ref=\"{1}\" order=\"{2}\">",
									XmlUtils.MakeSafeXmlAttribute(label), XmlUtils.MakeSafeXmlAttribute(s), index);
								++index;
							}
							else
							{
								contentsStream.Write("<relation type=\"{0}\" ref=\"{1}\">",
									XmlUtils.MakeSafeXmlAttribute(label), XmlUtils.MakeSafeXmlAttribute(s));
							}
							if (fInternalTraits)
							{
								if (!ordered || index <= 1)
								{
									contentsStream.WriteLine();
									DoChildren(contentsStream, currentObject, node, String.Empty);
								}
								if (flidMemberOf != 0)
								{
									int[] rghvoT = m_cache.GetVectorProperty(currentObject.Hvo, flidMemberOf, false);
									for (int i = 0; i < rghvoT.Length; ++i)
									{
										if (rghvoT[i] == hvo)
										{
											if (ordered && index > 1)
												contentsStream.WriteLine();
											contentsStream.WriteLine("<trait name=\"{0}\" value=\"true\"/>",
												XmlUtils.MakeSafeXmlAttribute(sFieldMemberOfTrait));
											break;
										}
									}
								}
							}
							contentsStream.WriteLine("</relation>");
						}
					}
					else
					{
						if (ordered)
						{
							contentsStream.WriteLine(String.Format("<{0} dst=\"{1}\" ord=\"{2}\"/>", label, GetIdString(hvo), index.ToString()));
							++index;
						}
						else
						{
							contentsStream.WriteLine(String.Format("<{0} dst=\"{1}\"/>", label, GetIdString(hvo)));
						}
					}
				}
				if (fWriteAsRelation && fInternalTraits)
					return;
			}
			else if (m_format == "sf")
			{
				foreach (int hvo in hvos)
				{
					string labelWs;
					string s;
					if (GetRefPropertyData(property, wsProp, hvo, out labelWs, out s))
					{
						string separator = String.Empty;
						if (!String.IsNullOrEmpty(labelWs))
							separator = "_";
						contentsStream.Write(String.Format("\r\n\\{0}{1}{2} {3}", label, separator, labelWs, s));
					}
				}
			}
			Debug.Assert(node.ChildNodes.Count == 0, "Child nodes are not supported in refVector elements");
		}
Beispiel #7
0
		/// <summary>
		/// The &lt;refObjVector&gt; element is used when you want to expand the referenced
		/// objects as subparts of the same element in the output file.
		/// </summary>
		/// <param name="contentsStream"></param>
		/// <param name="currentObject"></param>
		/// <param name="node"></param>
		protected void DoReferenceObjVectorElement(TextWriter contentsStream,
			CmObject currentObject, XmlNode node)
		{
			if (m_format != "sf")
				throw new ConfigurationException("<refObjVector> is supported only for standard format output.");

			bool ordered = XmlUtils.GetBooleanAttributeValue(node, "ordered");
			string label = XmlUtils.GetOptionalAttributeValue(node, "itemLabel");
			if (label == null)
				label = "subobject";
			string field = XmlUtils.GetManditoryAttributeValue(node, "field");
			string sVirtual = XmlUtils.GetOptionalAttributeValue(node, "virtual");
			bool fVirtual = false;
			if (sVirtual != null)
			{
				sVirtual = sVirtual.ToLower();
				if (sVirtual == "true" || sVirtual == "t" || sVirtual == "yes" || sVirtual == "y")
					fVirtual = true;
			}
			int flid = (int)m_cache.MetaDataCacheAccessor.GetFieldId2(
				(uint)currentObject.ClassID, field, true);
			int[] hvos;
			if (flid <= 0)
			{
				if (fVirtual)
				{
					hvos = LoadVirtualField(currentObject, field);
				}
				else
				{
					throw new ConfigurationException("There is no field named '" + field + "' in " + currentObject.GetType().ToString() + ". Remember that fields are the actual CELLAR names, so they do not have FDO suffixes like OA or RS.");
				}
			}
			else
			{
				if (m_mapFlids.ContainsKey(flid))
					flid = m_mapFlids[flid];
				hvos = m_cache.GetVectorProperty(currentObject.Hvo, flid, false);
			}
			string property = XmlUtils.GetOptionalAttributeValue(node, "itemProperty");
			if (property == null)
				property = "ShortName";
			string wsProp = XmlUtils.GetOptionalAttributeValue(node, "itemWsProp");
			string sClassTag = XmlUtils.GetOptionalAttributeValue(node, "classtag");
			string labelWs = "";
			ILgWritingSystemFactory wsf = m_cache.LanguageWritingSystemFactoryAccessor;
			foreach (int hvo in hvos)
			{
				CmObject co = (CmObject)CmObject.CreateFromDBObject(m_cache, hvo);
				if (String.IsNullOrEmpty(label) || String.IsNullOrEmpty(property))
				{
					contentsStream.WriteLine();
				}
				else
				{
					object obj = GetProperty(co, property);
					if (obj == null)
						continue;
					string s = Icu.Normalize(obj.ToString(), m_eIcuNormalizationMode);
					string separator = "";
					if (wsProp != null)
					{
						obj = GetProperty(co, wsProp);
						if (obj != null)
						{
							ILgWritingSystem ws = obj as ILgWritingSystem;
							if (ws == null)
							{
								int wsHvo = (int)obj;
								ws = LgWritingSystem.CreateFromDBObject(m_cache, wsHvo);
							}
							if (ws != null)
								labelWs = LabelString(ws);
						}
					}
					if (!String.IsNullOrEmpty(labelWs))
						separator = "_";
					string sTmp = String.Format("\r\n\\{0}{1}{2} {3}", label, separator, labelWs, s);
					contentsStream.Write(sTmp);
				}
				DumpObject(contentsStream, co, sClassTag);
			}
		}