コード例 #1
0
        /// <summary>
        /// Method to check an input tokens min and max based on the min and max field attributes
        /// </summary>
        /// <param name="fieldName">The name of the field</param>
        /// <param name="token"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        private static void CheckTokenMinMax(Field f, String token)
        {
            Decimal result, min, max;

            if ((!String.IsNullOrEmpty(f.decimalMinimum)) &&
                (!String.IsNullOrEmpty(f.decimalMaximum)) &&
                (!String.IsNullOrEmpty(token)) &&
#if PocketPC
                (ParseAssistant.TryParse(token, out result)) &
                (ParseAssistant.TryParse(f.decimalMinimum, out min)) &
                (ParseAssistant.TryParse(f.decimalMaximum, out max)))
#else
                (Decimal.TryParse(token, out result)) &
                (Decimal.TryParse(f.decimalMinimum, out min)) &
                (Decimal.TryParse(f.decimalMaximum, out max)))
#endif
            {
                if (result < min)
                {
                    throw new TDTTranslationException("TDTFieldBelowMinimum Field:" + f.name + " Value:" + token + " Min:" + min.ToString());
                }
                if (result > max)
                {
                    throw new TDTTranslationException("TDTFieldAboveMaximum Field:" + f.name + " Value:" + token + " Max:" + max.ToString());
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Method for processing extract & format rules
        /// </summary>
        private void ProcessRules(Tuple <Scheme, Level, Option> option, ModeList ruleType, Dictionary <String, String> tokens)
        {
            if (option.Item2.rule != null)
            {
                foreach (Rule r in option.Item2.rule)
                {
                    if (r.type == ruleType)
                    {
                        // parse the command name & it's parameters
                        Regex rx = new Regex(@"^(.+?)\((.+?)\)$");
                        Match m  = rx.Match(r.function);
                        if ((m.Success) &
                            (m.Groups.Count == 3)) //TODO fix regex to have 2 groups & not a match on the whole expression
                        {
                            //TODO Switch to a parser & AST
                            String   functionName       = m.Groups[1].Value.ToLower().Trim();
                            String[] functionParameters = m.Groups[2].Value.Split(',');
                            //String field1Name = option.Item3.field.Single(f => f.name == functionParameters[0]).name;
                            Match mx = new Regex(("^[0-9]*$")).Match(functionParameters[0]);

                            String field1Value;
                            if (mx.Success)
                            {
                                field1Value = functionParameters[0];
                            }
                            else
                            {
                                field1Value = tokens[functionParameters[0]];
                            }
                            switch (functionName)
                            {
                            case "tablelookup":
                            {
                                // EX: TABLELOOKUP(gs1companyprefixindex,tdt64bitcpi,gs1companyprefixindex,gs1companyprefix)
                                if (functionParameters[1].Trim().ToLower() == "tdt64bitcpi")
                                {
                                    tokens.Add(r.newFieldName, _gs1cpi[field1Value]);
                                }
                                else
                                {
                                    throw new TDTTranslationException("TDTFileNotFound " + functionParameters[1] + " auxillary file not found");
                                }
                                break;
                            }

                            case "length":
                            {
                                tokens.Add(r.newFieldName, field1Value.Length.ToString());
                                break;
                            }

                            case "gs1checksum":
                            {
                                int checksum;
                                int weight;
                                int total = 0;
                                int len   = field1Value.Length;
                                int d;
                                for (int i = 0; i < len; i++)
                                {
                                    if (i % 2 == 0)
                                    {
                                        weight = -3;
                                    }
                                    else
                                    {
                                        weight = -1;
                                    }
                                    d      = int.Parse(field1Value.Substring(len - 1 - i, 1));    // kasuga fixed
                                    total += weight * d;
                                }
                                checksum = (10 + total % 10) % 10;
                                tokens.Add(r.newFieldName, checksum.ToString());
                                break;
                            }

                            case "substr":
                            {
                                int offset;
#if PocketPC
                                if (!ParseAssistant.TryParse(functionParameters[1], out offset))
#else
                                if (!int.TryParse(functionParameters[1], out offset))
#endif
                                {
                                    offset = int.Parse(tokens[functionParameters[1]]);
                                }
                                if (functionParameters.Length == 2)
                                {
                                    tokens.Add(r.newFieldName, field1Value.Substring(offset));
                                }
                                else if (functionParameters.Length == 3)
                                {
                                    int length;
#if PocketPC
                                    if (!ParseAssistant.TryParse(functionParameters[2], out length))
#else
                                    if (!int.TryParse(functionParameters[2], out length))
#endif
                                    {
                                        length = int.Parse(tokens[functionParameters[2]]);
                                    }
                                    tokens.Add(r.newFieldName, field1Value.Substring(offset, length));
                                }
                                break;
                            }

                            case "concat":
                            {
                                StringBuilder buffer = new StringBuilder();
                                for (int p1 = 0; p1 < functionParameters.Length; p1++)
                                {
                                    Match m2 = new Regex(("^\"(.*?)\"$|^'(.*?)'$|^[0-9]*$")).Match(functionParameters[p1]);
                                    if (m2.Success)
                                    {
                                        buffer.Append(functionParameters[p1]);
                                    }
                                    else
                                    {
                                        if (tokens.ContainsKey(functionParameters[p1]))
                                        {
                                            buffer.Append(tokens[functionParameters[p1]]);
                                        }
                                    }
                                }
                                tokens.Add(r.newFieldName, buffer.ToString());
                                break;
                            }

                            case "add":
                            {
                                int value1 = int.Parse(field1Value);
                                int value2 = int.Parse(functionParameters[1]);
                                tokens.Add(r.newFieldName, (value1 + value2).ToString());
                                break;
                            }

                            case "multiply":
                            {
                                int value1 = int.Parse(field1Value);
                                int value2 = int.Parse(functionParameters[1]);
                                tokens.Add(r.newFieldName, (value1 * value2).ToString());
                                break;
                            }

                            case "divide":
                            {
                                int value1 = int.Parse(field1Value);
                                int value2 = int.Parse(functionParameters[1]);
                                tokens.Add(r.newFieldName, (value1 / value2).ToString());
                                break;
                            }

                            case "subtract":
                            {
                                int value1 = int.Parse(field1Value);
                                int value2 = int.Parse(functionParameters[1]);
                                tokens.Add(r.newFieldName, (value1 - value2).ToString());
                                break;
                            }

                            case "mod":
                            {
                                int value1 = int.Parse(field1Value);
                                int value2 = int.Parse(functionParameters[1]);
                                tokens.Add(r.newFieldName, (value1 % value2).ToString());
                                break;
                            }
                            }
                        }
                    }
                }
            }
        }