Пример #1
0
 public Rule(Map parent, int pos, RuleTypes type, RuleDirections dir, string pattern, string replacement)
     : this(parent, pos)
 {
     this.Type = type;
     this.Direction = dir;
     this.Pattern = pattern;
     this.Replacement = replacement;
 }
Пример #2
0
 public void RemoveMap(Map m)
 {
     if (maps.ContainsKey(m.pos))
     maps.Remove(m.pos);
 }
Пример #3
0
        public Rule(Map parent, int pos)
        {
            this._parent = parent;
            this.pos = pos;

            this.TypeName = this._parent.KeyName + "_rtype" + pos;
            this.DirName = this._parent.KeyName + "_rdir" + pos;
            this.IfName = this._parent.KeyName + "_rif" + pos;
            this.ThenName = this._parent.KeyName + "_rthen" + pos;
        }
Пример #4
0
 public Map GetMap(int pos)
 {
     if (maps.ContainsKey(pos))
     return maps[pos];
     else
     {
     Map m = new Map(this, pos);
     maps.Add(pos, m);
     if (this.NextMapPos < pos)
     this.NextMapPos = pos + 1;
     return m;
     }
 }
Пример #5
0
 public Map AddMap(string key, string character, bool shift)
 {
     Map m = new Map(this, this.NextMapPos);
     maps.Add(this.NextMapPos, m);
     m.Key = key;
     m.Character = character;
     m.Shift = shift;
     this.NextMapPos ++;
     return m;
 }
Пример #6
0
 public Map(Map parent, int pos)
     : this()
 {
     this._parent = parent;
     this.pos = pos;
     if (string.IsNullOrEmpty(this._parent.KeyName))
     {
     this.KeyName = "key" + pos;
     this.CharName = "char" + pos;
     this.ShiftName = "chkshift" + pos;
     }
     else
     {
     this.KeyName = this._parent.KeyName+ "_key" + pos;
     this.CharName = this._parent.KeyName + "_char" + pos;
     this.ShiftName = this._parent.KeyName + "_chkshift" + pos;
     }
 }
Пример #7
0
        private bool TranslateToXMappings(Map mapp, XElement parent)
        {
            XElement xel;
            bool isValid = true;
            foreach (Map m in mapp.Maps)
            {
            if (string.IsNullOrEmpty(m.Key) || (m.Maps.Length == 0 && string.IsNullOrEmpty(m.Character) ))
            continue;
            xel = new XElement("map", new XAttribute("key", m.Key), new XAttribute("shift", m.Shift), new XAttribute("text",( string.IsNullOrEmpty(m.Character) ? string.Empty : m.Character) ));
            parent.Add(xel);

            if (m.Maps.Length > 0)
            {
            xel.SetAttributeValue("text", string.Empty);
            isValid = TranslateToXMappings(m, xel);
            }
            else
            {
            int lmax = 0, rmax = 0;
            foreach (Rule r in m.Rules)
            {
            if (string.IsNullOrEmpty(r.Pattern) || string.IsNullOrEmpty(r.Replacement))
            continue;

            Match test = null;
            if (r.Type == RuleTypes.TEXT)
            {
            if (r.Pattern.Length > 3)
            {
                r.PatternError = "Out of maximum Length";
                isValid = false;
                continue;
            }
            }
            else
            {
            test = Regex.Match(r.Pattern, Rule.ValidPattern);
            if (!test.Success)
            {
                r.PatternError = "Invalid Pattern";
                isValid = false;
                continue;
            }
            }

            var xrule = new XElement("rule", new XAttribute("type", r.Type), new XAttribute("direction", r.Direction), new XAttribute("pattern", r.Pattern), new XAttribute("text", r.Replacement));
            xel.Add(xrule);

            int inter = 0;
            if (r.Direction == RuleDirections.LEFT)
            {
            lmax = (test != null ? (inter = test.Groups[1].Captures.Count + test.Groups[2].Captures.Count) > lmax ? inter : lmax : r.Pattern.Length > lmax ? r.Pattern.Length : lmax);
            } else {
            rmax = (test != null ? (inter = test.Groups[1].Captures.Count + test.Groups[2].Captures.Count) > rmax ? inter : rmax : r.Pattern.Length > rmax ? r.Pattern.Length : rmax);
            }
            if (r.Type == RuleTypes.REGX)
            xrule.SetAttributeValue("len", inter);
            } // foreach rule

            //if (xel.Elements().Any()) {
            //if (!string.IsNullOrEmpty(m.Character)) xel.SetAttributeValue("default", m.Character);
            //} else
            //xel.Value = m.Character;
            if (xel.Elements().Any())
            xel.Add(new XAttribute("rleftlen", lmax), new XAttribute("rrightlen", rmax));
            }
            }
            return isValid;
        }
Пример #8
0
 private void TranslateFromXMappings(XElement xelem, Map parent)
 {
     Map m;
     foreach (var xel in xelem.Elements())
     {
     if (xel.Name == "map")
     {
     m = parent.AddMap(xel.Attribute("key").Value, xel.Attribute("text").Value, bool.Parse(xel.Attribute("shift").Value));
     if (xel.Elements().Any())
         TranslateFromXMappings(xel, m);
     }
     else
     {
     parent.AddRule((RuleTypes)Enum.Parse(typeof(RuleTypes), xel.Attribute("type").Value), (RuleDirections)Enum.Parse(typeof(RuleDirections), xel.Attribute("direction").Value), xel.Attribute("pattern").Value, xel.Attribute("text").Value);
     }
     }
 }