示例#1
0
        /**
         * Returns a version of the original string that has any special characters
         * quoted (or escaped) as appropriate for the cell format type.  The format
         * type object is queried to see what is special.
         *
         * @param repl The original string.
         * @param type The format type representation object.
         *
         * @return A version of the string with any special characters Replaced.
         *
         * @see CellFormatType#isSpecial(char)
         */
        static String QuoteSpecial(String repl, CellFormatType type)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < repl.Length; i++)
            {
                char ch = repl[i];
                if (ch == '\'' && type.IsSpecial('\''))
                {
                    sb.Append('\u0000');
                    continue;
                }

                bool special = type.IsSpecial(ch);
                if (special)
                {
                    sb.Append("'");
                }
                sb.Append(ch);
                if (special)
                {
                    sb.Append("'");
                }
            }
            return(sb.ToString());
        }
示例#2
0
        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);
        }