예제 #1
0
 public HandlerParams(string name, IPartHandler handler, bool forceRetrieve, bool forceLink)
 {
     Name          = name;
     Handler       = handler;
     ForceRetrieve = forceRetrieve;
     ForceLink     = forceLink;
     PrimaryKey    = false;
 }
        /// <summary>
        /// Adds an <see cref="IPartHandler"/> instance.
        /// </summary>
        /// <param name="handler">Part handler to be added.</param>
        /// <returns><see cref="InterceptionConfiguration"/> instance to enable fluent configuration.</returns>
        public InterceptionConfiguration AddHandler(IPartHandler handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            this.partHandlers.Add(handler);
            return(this);
        }
        public static StringBuilder ParseFormat(String fdesc, CellFormatType type,
                                                IPartHandler partHandler)
        {
            // Quoting is very awkward.  In the Java classes, quoting is done
            // between ' chars, with '' meaning a single ' char. The problem is that
            // in Excel, it is legal to have two adjacent escaped strings.  For
            // example, consider the Excel format "\a\b#".  The naive (and easy)
            // translation into Java DecimalFormat is "'a''b'#".  For the number 17,
            // in Excel you would Get "ab17", but in Java it would be "a'b17" -- the
            // '' is in the middle of the quoted string in Java.  So the trick we
            // use is this: When we encounter a ' char in the Excel format, we
            // output a \u0000 char into the string.  Now we know that any '' in the
            // output is the result of two adjacent escaped strings.  So After the
            // main loop, we have to do two passes: One to eliminate any ''
            // sequences, to make "'a''b'" become "'ab'", and another to replace any
            // \u0000 with '' to mean a quote char.  Oy.
            //
            // For formats that don't use "'" we don't do any of this

            MatchCollection mc        = SPECIFICATION_PAT.Matches(fdesc);
            StringBuilder   fmt       = new StringBuilder();
            Match           lastMatch = null;

            //while (m.Find())
            foreach (Match m in mc)
            {
                String part = Group(m, 0);
                if (part.Length > 0)
                {
                    String repl = partHandler.HandlePart(m, part, type, fmt);
                    if (repl == null)
                    {
                        switch (part[0])
                        {
                        case '\"':
                            repl = QuoteSpecial(part.Substring(1,
                                                               part.Length - 2), type);
                            break;

                        case '\\':
                            repl = QuoteSpecial(part.Substring(1), type);
                            break;

                        case '_':
                            repl = " ";
                            break;

                        case '*':     //!! We don't do this for real, we just Put in 3 of them
                            repl = ExpandChar(part);
                            break;

                        default:
                            repl = part;
                            break;
                        }
                    }
                    //m.AppendReplacement(fmt, Match.QuoteReplacement(repl));
                    fmt.Append(part.Replace(m.Captures[0].Value, repl));
                    if (m.NextMatch().Index - (m.Index + part.Length) > 0)
                    {
                        fmt.Append(fdesc.Substring(m.Index + part.Length, m.NextMatch().Index - (m.Index + part.Length)));
                    }
                    lastMatch = m;
                }
            }
            if (lastMatch != null)
            {
                fmt.Append(fdesc.Substring(lastMatch.Index + lastMatch.Groups[0].Value.Length));
            }
            //m.AppendTail(fmt);

            if (type.IsSpecial('\''))
            {
                // Now the next pass for quoted characters: Remove '' chars, making "'a''b'" into "'ab'"
                int pos = 0;
                while ((pos = fmt.ToString().IndexOf("''", pos)) >= 0)
                {
                    //fmt.Delete(pos, pos + 2);
                    fmt.Remove(pos, 2);
                }

                // Now the pass for quoted chars: Replace any \u0000 with ''
                pos = 0;
                while ((pos = fmt.ToString().IndexOf("\u0000", pos)) >= 0)
                {
                    //fmt.Replace(pos, pos + 1, "''");
                    fmt.Remove(pos, 1);
                    fmt.Insert(pos, "''");
                }
            }

            return(fmt);
        }
        /// <summary>
        /// Adds an <see cref="IPartHandler"/> instance.
        /// </summary>
        /// <param name="handler">Part handler to be added.</param>
        /// <returns><see cref="InterceptionConfiguration"/> instance to enable fluent configuration.</returns>
        public InterceptionConfiguration AddHandler(IPartHandler handler)
        {
            if (handler == null) throw new ArgumentNullException("handler");

            this.partHandlers.Add(handler);
            return this;
        }