Esempio n. 1
0
        public int CompareTo(object o)
        {
            if (o == null)
            {
                return(1);
            }
            if (!(o is MultiTextBase))
            {
                throw new ArgumentException("Can not compare to objects other than MultiTextBase.");
            }
            MultiTextBase other           = (MultiTextBase)o;
            int           formLengthOrder = this.Forms.Length.CompareTo(other.Forms.Length);

            if (formLengthOrder != 0)
            {
                return(formLengthOrder);
            }
            for (int i = 0; i < Forms.Length; i++)
            {
                int languageFormOrder = Forms[i].CompareTo(other.Forms[i]);
                if (languageFormOrder != 0)
                {
                    return(languageFormOrder);
                }
            }
            return(0);
        }
Esempio n. 2
0
        public static MultiTextBase Create(Dictionary <string, string> forms)
        {
            MultiTextBase m = new MultiTextBase();

            CopyForms(forms, m);
            return(m);
        }
		public void IterateWithForEach()
		{
			MultiTextBase text = new MultiTextBase();
			text["a"] = "alpha";
			text["b"] = "beta";
			text["g"] = "gamma";
			int i = 0;
			foreach (LanguageForm l in text)
			{
				switch (i)
				{
					case 0:
						Assert.AreEqual("a", l.WritingSystemId);
						Assert.AreEqual("alpha", l.Form);
						break;
					case 1:
						Assert.AreEqual("b", l.WritingSystemId);
						Assert.AreEqual("beta", l.Form);
						break;
					case 2:
						Assert.AreEqual("g", l.WritingSystemId);
						Assert.AreEqual("gamma", l.Form);
						break;
				}
				i++;
			}
		}
Esempio n. 4
0
 public LanguageForm(LanguageForm form, MultiTextBase parent)
     : this(form.WritingSystemId, form.Form, parent)
 {
     foreach (var span in form.Spans)
     {
         AddSpan(span.Index, span.Length, span.Lang, span.Class, span.LinkURL);
     }
 }
		public void Count()
		{
			MultiTextBase text = new MultiTextBase();
			Assert.AreEqual(0, text.Count);
			text["a"] = "alpha";
			text["b"] = "beta";
			text["g"] = "gamma";
			Assert.AreEqual(3, text.Count);
		}
Esempio n. 6
0
 public void AddLanguageString(string key, string value, string writingSystemId, bool isCollectionValue)
 {
     if(!TextVariables.ContainsKey(key))
     {
         var text = new MultiTextBase();
         TextVariables.Add(key, new NamedMutliLingualValue(text, isCollectionValue));
     }
     TextVariables[key].TextAlternatives.SetAlternative(writingSystemId,value);
 }
Esempio n. 7
0
 public void UpdateGenericLanguageString(string key, string value, bool isCollectionValue)
 {
     var text = new MultiTextBase();
     text.SetAlternative("*", value);
     if(TextVariables.ContainsKey(key))
     {
         TextVariables.Remove(key);
     }
     TextVariables.Add(key, new NamedMutliLingualValue(text, isCollectionValue));
 }
Esempio n. 8
0
        /// <summary>
        /// Will merge the two mt's if they are compatible; if they collide anywhere, leaves the original untouched
        /// and returns false
        /// </summary>
        /// <param name="incoming"></param>
        /// <returns></returns>
        public bool TryMergeIn(MultiTextBase incoming)
        {
            //abort if they collide
            if (!CanBeUnifiedWith(incoming))
            {
                return(false);
            }

            MergeIn(incoming);
            return(true);
        }
		public void BasicStuff()
		{
			MultiTextBase text = new MultiTextBase();
			text["foo"] = "alpha";
			Assert.AreSame("alpha", text["foo"]);
			text["foo"] = "beta";
			Assert.AreSame("beta", text["foo"]);
			text["foo"] = "gamma";
			Assert.AreSame("gamma", text["foo"]);
			text["bee"] = "beeeee";
			Assert.AreSame("gamma", text["foo"], "setting a different alternative should not affect this one");
			text["foo"] = null;
			Assert.AreSame(string.Empty, text["foo"]);
		}
Esempio n. 10
0
		public void NullConditions()
		{
			MultiTextBase text = new MultiTextBase();
			Assert.AreSame(string.Empty, text["foo"], "never before heard of alternative should give back an empty string");
			Assert.AreSame(string.Empty, text["foo"], "second time");
			Assert.AreSame(string.Empty, text.GetBestAlternative("fox"));
			text.SetAlternative("zox", "");
			Assert.AreSame(string.Empty, text["zox"]);
			text.SetAlternative("zox", null);
			Assert.AreSame(string.Empty, text["zox"], "should still be empty string after setting to null");
			text.SetAlternative("zox", "something");
			text.SetAlternative("zox", null);
			Assert.AreSame(string.Empty, text["zox"], "should still be empty string after setting something and then back to null");
		}
Esempio n. 11
0
 public bool HasFormWithSameContent(MultiTextBase other)
 {
     if (other.Count == 0 && Count == 0)
     {
         return(true);
     }
     foreach (LanguageForm form in other)
     {
         if (ContainsEqualForm(form))
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 12
0
 public void MergeIn(MultiTextBase incoming)
 {
     foreach (LanguageForm form in incoming)
     {
         LanguageForm f = Find(form.WritingSystemId);
         if (f != null)
         {
             f.Form = form.Form;
         }
         else
         {
             AddLanguageForm(form);                     //this actually copies the meat of the form
         }
     }
 }
Esempio n. 13
0
 /// <summary>
 /// False if they have different forms on any single writing system. If true, they can be safely merged.
 /// </summary>
 /// <param name="incoming"></param>
 /// <returns></returns>
 public bool CanBeUnifiedWith(MultiTextBase incoming)
 {
     foreach (var form in incoming.Forms)
     {
         if (!ContainsAlternative(form.WritingSystemId))
         {
             continue;                    //no problem, we don't have one of those
         }
         if (GetExactAlternative(form.WritingSystemId) != form.Form)
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 14
0
 public void UpdateLanguageString(string key,  string value, string writingSystemId,bool isCollectionValue)
 {
     NamedMutliLingualValue namedMutliLingualValue;
     MultiTextBase text;
     if(TextVariables.TryGetValue(key,out namedMutliLingualValue))
         text = namedMutliLingualValue.TextAlternatives;
     else
     {
         text = new MultiTextBase();
     }
     text.SetAlternative(DealiasWritingSystemId(writingSystemId), value);
     TextVariables.Remove(key);
     if(text.Count>0)
         TextVariables.Add(key, new NamedMutliLingualValue(text, isCollectionValue));
 }
Esempio n. 15
0
 public bool Equals(MultiTextBase other)
 {
     if (other == null)
     {
         return(false);
     }
     if (other.Count != Count)
     {
         return(false);
     }
     if (!_forms.SequenceEqual(other.Forms))
     {
         return(false);
     }
     return(true);
 }
Esempio n. 16
0
		protected static void CopyForms(Dictionary<string, string> forms, MultiTextBase m)
		{
			if (forms != null && forms.Keys != null)
			{
				foreach (string key in forms.Keys)
				{
					LanguageForm f = m.Find(key);
					if (f != null)
					{
						f.Form = forms[key];
					}
					else
					{
						m.SetAlternative(key, forms[key]);
					}
				}
			}
		}
Esempio n. 17
0
 protected static void CopyForms(Dictionary <string, string> forms, MultiTextBase m)
 {
     if (forms != null && forms.Keys != null)
     {
         foreach (string key in forms.Keys)
         {
             LanguageForm f = m.Find(key);
             if (f != null)
             {
                 f.Form = forms[key];
             }
             else
             {
                 m.SetAlternative(key, forms[key]);
             }
         }
     }
 }
Esempio n. 18
0
 public LanguageForm GetFirstAlternativeForm(MultiTextBase alternatives)
 {
     foreach (LanguageForm form in alternatives.Forms)
     {
         if (form.Form.Trim().Length > 0)
         {
             return form;
         }
     }
     return null;
 }
Esempio n. 19
0
		public void CompareTo_MultiTextWithMoreForms_ReturnsLess()
		{
			MultiTextBase multiTextBase = new MultiTextBase();
			MultiTextBase multiTextBaseToCompare = new MultiTextBase();
			multiTextBaseToCompare.SetAlternative("de", "Word 1");
			Assert.AreEqual(-1, multiTextBase.CompareTo(multiTextBaseToCompare));
		}
Esempio n. 20
0
		public void CompareTo_MultiTextWithFewerForms_ReturnsGreater()
		{
			MultiTextBase multiTextBase = new MultiTextBase();
			multiTextBase.SetAlternative("de", "Word 1");
			MultiTextBase multiTextBaseToCompare = new MultiTextBase();
			Assert.AreEqual(1, multiTextBase.CompareTo(multiTextBaseToCompare));
		}
Esempio n. 21
0
        private WeSayTextBox AddTextBox(WritingSystem writingSystem, MultiTextBase multiText)
        {
            WeSayTextBox box = new WeSayTextBox(writingSystem, Name);
            box.ReadOnly = (_visibility == CommonEnumerations.VisibilitySetting.ReadOnly);
            box.Multiline = true;
            box.WordWrap = true;
            box.IsSpellCheckingEnabled = _isSpellCheckingEnabled;
            //box.Enabled = !box.ReadOnly;

            _textBoxes.Add(box);
            box.Name = Name.Replace("-mtc", "") + "_" + writingSystem.Id;
            //for automated tests to find this particular guy
            box.Text = multiText[writingSystem.Id];

            box.TextChanged += OnTextOfSomeBoxChanged;
            box.KeyDown += OnKeyDownInSomeBox;
            box.MouseWheel += subControl_MouseWheel;

            return box;
        }
Esempio n. 22
0
 public NamedMutliLingualValue(MultiTextBase text, bool isCollectionValue)
 {
     TextAlternatives = text;
     IsCollectionValue = isCollectionValue;
 }
Esempio n. 23
0
		private void WriteOneElementPerFormIfNonEmpty(string propertyName,
													  string wrapperName,
													  MultiTextBase text,
													  char delimeter)
		{
			if (!MultiTextBase.IsEmpty(text))
			{
				foreach (LanguageForm alternative in GetOrderedAndFilteredForms(text, propertyName))
				{
					foreach (string part in alternative.Form.Split(new char[] {delimeter}))
					{
						string trimmed = part.Trim();
						if (part != string.Empty)
						{
							Writer.WriteStartElement(wrapperName);
							Writer.WriteAttributeString("lang", alternative.WritingSystemId);
							Writer.WriteStartElement("text");
							Writer.WriteString(trimmed);
							Writer.WriteEndElement();
							WriteFlags(alternative);
							Writer.WriteEndElement();
						}
					}
				}
			}
		}
Esempio n. 24
0
		protected override LanguageForm[] GetOrderedAndFilteredForms(MultiTextBase text,
																	 string propertyName)
		{
			Field f = Template.GetField(propertyName);
			if (f == null)
			{
				return text.Forms;
			}
			return text.GetOrderedAndFilteredForms(f.WritingSystemIds);
		}
Esempio n. 25
0
 public bool HasFormWithSameContent(MultiTextBase other)
 {
     if (other.Count == 0 && Count == 0)
     {
         return true;
     }
     foreach (LanguageForm form in other)
     {
         if (ContainsEqualForm(form))
         {
             return true;
         }
     }
     return false;
 }
Esempio n. 26
0
        /// <summary>
        /// Will merge the two mt's if they are compatible; if they collide anywhere, leaves the original untouched
        /// and returns false
        /// </summary>
        /// <param name="incoming"></param>
        /// <returns></returns>
        public bool TryMergeIn(MultiTextBase incoming)
        {
            //abort if they collide
            if (!CanBeUnifiedWith(incoming))
                return false;

            MergeIn(incoming);
            return true;
        }
Esempio n. 27
0
 public static MultiTextBase Create(Dictionary<string, string> forms)
 {
     MultiTextBase m = new MultiTextBase();
     CopyForms(forms, m);
     return m;
 }
Esempio n. 28
0
 /// <summary>
 /// False if they have different forms on any single writing system. If true, they can be safely merged.
 /// </summary>
 /// <param name="incoming"></param>
 /// <returns></returns>
 public bool CanBeUnifiedWith(MultiTextBase incoming)
 {
     foreach (var form in incoming.Forms)
     {
         if (!ContainsAlternative(form.WritingSystemId))
             continue;//no problem, we don't have one of those
         if (GetExactAlternative(form.WritingSystemId) != form.Form)
             return false;
     }
     return true;
 }
Esempio n. 29
0
		public void CompareTo_MultiTextWithNonIdenticalWritingSystemsAndFirstNonidenticalWritingSystemIsAlphabeticallyEarlier_ReturnsGreater()
		{
			MultiTextBase multiTextBase = new MultiTextBase();
			multiTextBase.SetAlternative("en", "Word 1");
			MultiTextBase multiTextBaseToCompare = new MultiTextBase();
			multiTextBaseToCompare.SetAlternative("de", "Word 1");
			Assert.AreEqual(1, multiTextBase.CompareTo(multiTextBaseToCompare));
		}
Esempio n. 30
0
		public void CompareTo_MultiTextWithNonIdenticalFormsAndFirstNonidenticalformIsAlphabeticallyLater_ReturnsLess()
		{
			MultiTextBase multiTextBase = new MultiTextBase();
			multiTextBase.SetAlternative("de", "Word 1");
			MultiTextBase multiTextBaseToCompare = new MultiTextBase();
			multiTextBaseToCompare.SetAlternative("de", "Word 2");
			Assert.AreEqual(-1, multiTextBase.CompareTo(multiTextBaseToCompare));
		}
Esempio n. 31
0
		/// <summary>
		/// this is used both when we're just exporting to lift, and dont' want to filter or order, and
		/// when we are writing presentation-ready lift, when we do want to filter and order
		/// </summary>
		/// <param name="text"></param>
		/// <param name="propertyName"></param>
		/// <returns></returns>
		protected virtual LanguageForm[] GetOrderedAndFilteredForms(MultiTextBase text,
																	string propertyName)
		{
			return text.Forms;
		}
Esempio n. 32
0
 public static bool IsEmpty(MultiTextBase mt)
 {
     return(mt == null || mt.Empty);
 }
Esempio n. 33
0
		public void CompareTo_IdenticalMultiText_ReturnsEqual()
		{
			MultiTextBase multiTextBase = new MultiTextBase();
			multiTextBase.SetAlternative("de", "Word 1");
			MultiTextBase multiTextBaseToCompare = new MultiTextBase();
			multiTextBaseToCompare.SetAlternative("de", "Word 1");
			Assert.AreEqual(0, multiTextBase.CompareTo(multiTextBaseToCompare));
		}
Esempio n. 34
0
 public static bool IsEmpty(MultiTextBase mt)
 {
     return mt == null || mt.Empty;
 }
Esempio n. 35
0
        /// <summary>
        /// walk throught the sourceDom, collecting up values from elements that have data-book or data-collection attributes.
        /// </summary>
        private void GatherDataItemsFromXElement(DataSet data, XmlNode sourceElement
			/* can be the whole sourceDom or just a page */)
        {
            string elementName = "*";
            try
            {
                string query = String.Format(".//{0}[(@data-book or @data-library or @data-collection)]", elementName);

                XmlNodeList nodesOfInterest = sourceElement.SafeSelectNodes(query);

                foreach (XmlElement node in nodesOfInterest)
                {
                    bool isCollectionValue = false;

                    string key = node.GetAttribute("data-book").Trim();
                    if (key == String.Empty)
                    {
                        key = node.GetAttribute("data-collection").Trim();
                        if (key == String.Empty)
                        {
                            key = node.GetAttribute("data-library").Trim(); //the old (pre-version 1) name of collections was 'library'
                        }
                        isCollectionValue = true;
                    }

                    string value = node.InnerXml.Trim(); //may contain formatting
                    if (node.Name.ToLower() == "img")
                    {
                        value = node.GetAttribute("src");
                        //Make the name of the image safe for showing up in raw html (not just in the relatively safe confines of the src attribut),
                        //becuase it's going to show up between <div> tags.  E.g. "Land & Water.png" as the cover page used to kill us.
                        value = WebUtility.HtmlEncode(WebUtility.HtmlDecode(value));
                    }
                    if (!String.IsNullOrEmpty(value) && !value.StartsWith("{"))
                        //ignore placeholder stuff like "{Book Title}"; that's not a value we want to collect
                    {
                        string lang = node.GetOptionalStringAttribute("lang", "*");
                        if (lang == "") //the above doesn't stop a "" from getting through
                            lang = "*";
                        if ((elementName.ToLower() == "textarea" || elementName.ToLower() == "input" ||
                             node.GetOptionalStringAttribute("contenteditable", "false") == "true") &&
                            (lang == "V" || lang == "N1" || lang == "N2"))
                        {
                            throw new ApplicationException(
                                "Editable element (e.g. TextArea) should not have placeholder @lang attributes (V,N1,N2)\r\n\r\n" +
                                node.OuterXml);
                        }

                        //if we don't have a value for this variable and this language, add it
                        if (!data.TextVariables.ContainsKey(key))
                        {
                            var t = new MultiTextBase();
                            t.SetAlternative(lang, value);
                            data.TextVariables.Add(key, new NamedMutliLingualValue(t, isCollectionValue));
                        }
                        else if (!data.TextVariables[key].TextAlternatives.ContainsAlternative(lang))
                        {
                            MultiTextBase t = data.TextVariables[key].TextAlternatives;
                            t.SetAlternative(lang, value);
                        }
                    }
                }
            }
            catch (Exception error)
            {
                throw new ApplicationException(
                    "Error in GatherDataItemsFromDom(," + elementName + "). RawDom was:\r\n" + sourceElement.OuterXml,
                    error);
            }
        }
Esempio n. 36
0
 public bool Equals(MultiTextBase other)
 {
     if (other == null) return false;
     if (other.Count != Count) return false;
     if (!_forms.SequenceEqual(other.Forms)) return false;
     return true;
 }
Esempio n. 37
0
        private void UpdateSingleTextVariableThroughoutDOM(string key, MultiTextBase multiText)
        {
            //Debug.WriteLine("before: " + dataDiv.OuterXml);

            if(multiText.Count==0)
            {
                RemoveDataDivElementIfEmptyValue(key,null);
            }
            foreach (LanguageForm languageForm in multiText.Forms)
            {
                XmlNode node =
                    _dataDiv.SelectSingleNode(String.Format("div[@data-book='{0}' and @lang='{1}']", key,
                                                           languageForm.WritingSystemId));

                _dataset.UpdateLanguageString(key, languageForm.Form, languageForm.WritingSystemId, false);

                if (null == node)
                {
                    if (!string.IsNullOrEmpty(languageForm.Form))
                    {
                        Debug.WriteLine("creating in datadiv: {0}[{1}]={2}", key, languageForm.WritingSystemId,
                                        languageForm.Form);
                        Debug.WriteLine("nop: " + _dataDiv.OuterXml);
                        AddDataDivBookVariable(key, languageForm.WritingSystemId, languageForm.Form);
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(languageForm.Form)) //a null value removes the entry entirely
                    {
                        node.ParentNode.RemoveChild(node);
                    }
                    else
                    {
                        node.InnerXml = languageForm.Form;
                    }
                    //Debug.WriteLine("updating in datadiv: {0}[{1}]={2}", key, languageForm.WritingSystemId,
                    //				languageForm.Form);
                    //Debug.WriteLine("now: " + _dataDiv.OuterXml);
                }
            }
        }
Esempio n. 38
0
 public void MergeInWithAppend(MultiTextBase incoming, string separator)
 {
     foreach (LanguageForm form in incoming)
     {
         LanguageForm f = Find(form.WritingSystemId);
         if (f != null)
         {
             f.Form += separator + form.Form;
         }
         else
         {
             AddLanguageForm(form); //this actually copies the meat of the form
         }
     }
 }
Esempio n. 39
0
		/// <summary>
		/// use this for multitexts that were somehow constructed during export, with no corresponding single property
		/// </summary>
		private void WriteMultiTextAsArtificialField(string outputFieldName, MultiTextBase text)
		{
			if (!MultiTextBase.IsEmpty(text))
			{
				Writer.WriteStartElement("field");

				Writer.WriteAttributeString("type", outputFieldName);

				if (!MultiTextBase.IsEmpty(text))
				{
					Add(text.Forms, true);
				}

				Writer.WriteEndElement();
			}
		}
Esempio n. 40
0
		/// <summary>
		/// Try to add the sense to a matching entry. If none found, make a new entry with the sense
		/// </summary>
		private void AddSenseToLexicon(MultiTextBase lexemeForm, LexSense sense)
		{
			//review: the desired semantics of this find are unclear, if we have more than one ws
			ResultSet<LexEntry> entriesWithSameForm =
					LexEntryRepository.GetEntriesWithMatchingLexicalForm(
							lexemeForm[_lexicalUnitWritingSystem.Id], _lexicalUnitWritingSystem);
			if (entriesWithSameForm.Count == 0)
			{
				LexEntry entry = LexEntryRepository.CreateItem();
				entry.LexicalForm.MergeIn(lexemeForm);
				entry.Senses.Add(sense);
				LexEntryRepository.SaveItem(entry);
			}
			else
			{
				LexEntry entry = entriesWithSameForm[0].RealObject;

				foreach (LexSense s in entry.Senses)
				{
					if (sense.Gloss.Forms.Length > 0)
					{
						LanguageForm glossWeAreAdding = sense.Gloss.Forms[0];
						string glossInThisWritingSystem =
								s.Gloss.GetExactAlternative(glossWeAreAdding.WritingSystemId);
						if (glossInThisWritingSystem == glossWeAreAdding.Form)
						{
							return; //don't add it again
						}
					}
				}
				entry.Senses.Add(sense);
				LexEntryRepository.NotifyThatLexEntryHasBeenUpdated(entry);
			}
		}
Esempio n. 41
0
 public LanguageForm(string writingSystemId, string form, MultiTextBase parent)
 {
     _parent          = parent;
     _writingSystemId = writingSystemId;
     _form            = form;
 }