Package ariba.util.core

Examples of ariba.util.core.FastStringBuffer


    }

    void pushString (String stringValue)
    {
        if (_currentStringBuffer == null) {
            _currentStringBuffer = new FastStringBuffer();
        }
        String strippedStringValue = stripFormattingWhitespace(stringValue);
        _currentStringBuffer.append(strippedStringValue);
    }
View Full Code Here


    private String removeBackslashes (String dirtyString)
    {
        String cleanString = dirtyString;
        if (dirtyString.indexOf('\\') != -1) {
            int dirtyStringLength = dirtyString.length();
            FastStringBuffer stringBuffer = new FastStringBuffer(dirtyStringLength);
            for (int index = 0; index < dirtyStringLength; index++) {
                char currentChar = dirtyString.charAt(index);
                if (currentChar == '\\') {
                    index++;
                    if (index < dirtyStringLength) {
                        currentChar = dirtyString.charAt(index);
                    }
                }
                stringBuffer.append(currentChar);
            }
            cleanString = stringBuffer.toString();
        }
        return cleanString;
    }
View Full Code Here

    {
        public String format (Object value)
        {
            if (value == null) return null;
            int count = ((String)value).length();
            FastStringBuffer buf = new FastStringBuffer(count);
            while (count-- > 0) {
                buf.append("*");
            }
            return buf.toString();
        }
View Full Code Here

        {
            if (value == null) return null;
            Assert.that (value instanceof Number, "Incorrect input type: %s", value.getClass());

            int time = (int)(((Number)value).doubleValue() *1000);
            FastStringBuffer buf = new FastStringBuffer(11);
            int millis = time % 1000;
            int seconds = (time/1000) % 60;
            int minutes = (time/60000) % 60;
            int hours = (time/3600000) % 24;
            if (hours != 0) append(buf, hours, ":");
            append(buf, minutes, ":");
            append(buf, seconds, (millis != 0) ? "." : null);
            if (millis != 0) append(buf, millis, null);
            return buf.toString();
        }
View Full Code Here

            // any necessary zero padding.
        if (scale != value.scale()) {
            value = value.setScale(scale, BigDecimal.ROUND_HALF_UP);
        }

        FastStringBuffer buf = new FastStringBuffer(value.toPlainString());

        DecimalFormatSymbols symbols = fmt.getDecimalFormatSymbols();

            // Extract localized characters for parsing numbers
        char decimalSep = symbols.getDecimalSeparator();
        char groupSep   = symbols.getGroupingSeparator();
        int  groupSize  = fmt.getGroupingSize();
        String negativePrefix = fmt.getNegativePrefix();
        String negativeSuffix = fmt.getNegativeSuffix();
        String positivePrefix = fmt.getPositivePrefix();
        String positiveSuffix = fmt.getPositiveSuffix();

            // Remove any canonical negative sign
        int minusSign = buf.indexOf(CanonicalMinusSign);
        if (minusSign > -1) {
            buf.removeCharAt(minusSign);
        }

            // Replace the canonical decimal separator with a localized version
        int decimal = buf.indexOf(CanonicalDecimalSeparator);
        if (decimal > -1) {
            buf.replace(decimal, decimalSep, 1);
        }

            // temporary hack to work around bug in IE where
            // the group separator of Canadian English is ';'
        if (locale.equals(java.util.Locale.CANADA)) {
            groupSep = CanadianEnglishGroupSeparator;
        }

            // Insert any group separators.  If no decimal separator exists,
            // then start inserting group separators relative to the end of
            // the string.
        if (decimal < 0) {
            decimal = buf.length();
        }
        if (groupSize > 0) {
            for (int i = decimal-groupSize; i > 0; i -= groupSize) {
                buf.insert(groupSep, i);
            }
        }

        if (value.signum() >= 0) {
            buf.insert(positivePrefix, 0);
            buf.append(positiveSuffix);
        }
        else {
            buf.insert(negativePrefix, 0);
            buf.append(negativeSuffix);
        }

        return buf.toString();
    }
View Full Code Here

        boolean ignoreNegativeSuffix = false;

        DecimalFormatSymbols symbols = fmt.getDecimalFormatSymbols();

            // Initialize the parsing result buffers
        FastStringBuffer numberBuffer = new FastStringBuffer();
        FastStringBuffer prefixStringStringBuffer = new FastStringBuffer();
        FastStringBuffer suffixStringStringBuffer = new FastStringBuffer();

        // Extract localized characters for parsing numbers
            // For the string "1,234.56" '.' is the decimal separator.
        char decimalSep = symbols.getDecimalSeparator();
            //For the string "1,234.56" ',' is the grouping separator.
        char groupSep   = symbols.getGroupingSeparator();
            // The negativePrefix will typically be ( or -, as in (100) or -100.
        String negativePrefix = fmt.getNegativePrefix();
            // The negative suffix will typically be ) or '', as in (100) or -100.
            // Something like 100- is allowed in some locals as well though.
        String negativeSuffix = fmt.getNegativeSuffix();
        int length = string.length();

        Log.util.debug("parseBigDecimal: string = %s, neg prefix = %s, neg suffix = %s",
                       string, negativePrefix, negativeSuffix);

        // housekeeping for negative signs
        boolean negativePrefixIsMissing =
                StringUtil.nullOrEmptyOrBlankString(negativePrefix);
        boolean negativeSuffixIsMissing =
                StringUtil.nullOrEmptyOrBlankString(negativeSuffix);
        if (negativePrefixIsMissing) {
            if (!negativeSuffixIsMissing) {
                ignoreNegativePrefix = true;
            }
            else {
                    // We have no negative prefix or suffix,
                    // so we don't have the (optional) negative pattern at all.
                    // The core java doc says to use '-' as prefix.
                negativePrefix = CanonicalNegativePrefix;
                ignoreNegativeSuffix = true;
            }
        }
            // we have a negative prefix and no negative suffix
        else if (negativeSuffixIsMissing) {
            ignoreNegativeSuffix = true;
        }

        boolean negPrefixFound = false;
        boolean negSuffixFound = false;
            // if we've found any digits (decimal or integer) anywhere ever in the loop
        boolean foundDigits = false;
            // count of the decimal digits, ie 1.23 would have 2 at the end of the loop
        int decimalDigits  = 0;
            // count of the integer digits, ie 1.23 would have 1 at the end of the loop
        int integerDigits  = 0;
            // index of the decimal point, ie 1.23 would have decimal location 1
        int decimalLocation = NOT_FOUND;

        /*
        * Read through input string, character by character.
        * This will fill the buffers and replace any localized punctuation
        * characters with canonical ones
        */
        for (int i = 0; i < length; i++) {
            char curChar = string.charAt(i);
            char nextChar = i < length-1 ? string.charAt(i+1) : ' ';

                // Are we on the format's negative prefix?  If so, skip it
                // We check for the canonical negative prefix below
            if (!negPrefixFound && !foundDigits && negativePrefix.length() > 0 &&
                string.indexOf(negativePrefix, i) == i)
            {
                Log.util.debug("found neg prefix at %s", i);
                negPrefixFound = true;
                i += negativePrefix.length()-1;
            }
                // Are we on the negative suffix?  If so, skip it
            else if (!negSuffixFound && foundDigits &&
                     negativeSuffix.length() > 0 &&
                     string.indexOf(negativeSuffix, i) == i)
            {
                negSuffixFound = true;
                Log.util.debug("found neg suffix at %s", i);
                i += negativeSuffix.length()-1;
            }

                // We found a decimal separator if there's a digit either
                // before or after the separator and there aren't more
                // decimal separators after the current one.
            else if (decimalSep == curChar &&
                     (foundDigits || Character.isDigit(nextChar) &&
                     string.lastIndexOf(decimalSep) == i))
            {

                    // Replace with the canonical decimal separator for the
                    // BigDecimal constructor.
                numberBuffer.append(CanonicalDecimalSeparator);
                decimalLocation = i;
            }

                // We're in the group separator (canonical ',')  or in white space
            else if (Character.isWhitespace(curChar) ||
                     Character.isSpaceChar(curChar||
                     groupSep == curChar)
            {
                    // ignore these characters, no state changes at all
            }

                // We're either in the number itself
            else if (Character.isDigit(curChar)) {

                    // If we found a suffix yet there's still digits,
                    // then we've got exponential notation or an invalid number
                boolean exponentialOrInvalid =
                        suffixStringStringBuffer.length() > 0 || negSuffixFound;
                if (exponentialOrInvalid) {
                    char prevChar = string.charAt(i -1);
                    boolean exponential = prevChar == 'E' || prevChar == 'e';
                    if (exponential) {
                        return BigDecimalParser.parseExponentialBigDecimal(string, fmt);
                    }
                    else {
                        throw makeParseException(InvalidCharacterInNumberKey, 1);
                    }
                }

                numberBuffer.append(curChar);
                foundDigits = true;
                if (decimalLocation > NOT_FOUND) {
                    // decimal place has been found
                    decimalDigits++;
                }
                else {
                    integerDigits++;
                }
            }
                // we're on the prefix string, AKA random text before the number
            else if (!foundDigits)
            {
                prefixStringStringBuffer.append(curChar);
            }
                // must be the string suffix
            else {
                suffixStringStringBuffer.append(curChar);
            }
        } // end input string loop

        /* Convert the buffer data to the format for DecimalParseInfo's constructor */

        // make number buffer negative based on housekeeping data
        boolean negativePrefixWasIgnored =
                !negPrefixFound && ignoreNegativePrefix;
        boolean negativePrefixIsValid =
                negativePrefixWasIgnored || negPrefixFound;

        boolean negativeSuffixWasIgnored =
                !negSuffixFound && ignoreNegativeSuffix;
        boolean negativeSuffixIsValid =
                negativeSuffixWasIgnored || negSuffixFound;

            // Put back a canonical minus sign if the number is negative
        boolean isNegative = negativePrefixIsValid && negativeSuffixIsValid;
        if (isNegative) {
            numberBuffer.insert(CanonicalMinusSign, 0);
        }

        // sanity check
        boolean noNumbers = numberBuffer.length() == 0;
        if (noNumbers) {
            throw makeParseException(NoDigitsFoundKey, 0);
        }

        // convert number buffer to big decimal
        String numberString = numberBuffer.toString();
        BigDecimal amount = Constants.ZeroBigDecimal;
        try {
            amount = new BigDecimal(numberString);
        }
        catch (NumberFormatException e) {
            throw makeParseException(NumberFormatErrorKey, 0);
        }

        // convert other buffers to strings
        String prefixString = prefixStringStringBuffer.toString();
        if (StringUtil.nullOrEmptyOrBlankString(prefixString)) {
            prefixString = null;
        }
        String suffixString = suffixStringStringBuffer.toString();
        if (StringUtil.nullOrEmptyOrBlankString(suffixString)) {
            suffixString = null;
        }

        /* Use converted housekeeping data */
 
View Full Code Here

        if (string.indexOf(TagDelimiter) == -1) {
            return string;
        }

            // Use userBuf if given; else create one
        FastStringBuffer buf;
        if (userBuf == null) {
            buf = new FastStringBuffer();
        }
        else {
            buf = userBuf;
        }

        int pos = 0;
        int idx = -1;
        int end;

        String tag, result;

            // search for tags, i.e. words between our tag delimiter string
        while ((idx = string.indexOf(TagDelimiter, pos)) != -1) {

                // copy the text from our current position up to the tag
            buf.append(string.substring(pos, idx));

                // find the ending tag delimiter
            idx += TagDelimiter.length();
            end = string.indexOf(TagDelimiter, idx);
            if (end == -1) {
                break;
            }

                // get the tag string
            tag = string.substring(idx, end);

                // move our current position to just after the tag
            pos = end + TagDelimiter.length();

                // see if the tag is in the form of a method name
                // and a list of arguments, e.g. "foo(hello, bye)"
            String method = method(tag);
            List arguments = arguments(tag, args);

                // append the string returned by the tag handler
            result = handler.stringForTag(method, arguments);

                // just append the tag itself if the handler returns null
            buf.append((result == null) ? tag : result);
        }

            // copy the rest of the string to the buffer
        buf.append(string.substring(pos, string.length()));

        return buf.toString();
    }
View Full Code Here

        @param s the string to remove the prefix from
        @return the string passed in but with the prefix removed
    */
    private String removeLogPrefix (String s)
    {
        FastStringBuffer buf = new FastStringBuffer(s);
        if (buf.startsWith(StringTablePrefix)) {
            return buf.substring(StringTablePrefix.length(), s.length());
        }

        return s;
    }
View Full Code Here

        int lineNumberToConsume = lineNumber;

        int     ch         = 0;
        int     state      = StateFirstLine;
        List  tokens     = ListUtil.list();
        FastStringBuffer token       = new FastStringBuffer(TokenBufferSize);
        boolean currentTokenIsQuoted = false;

        if (!(reader instanceof BufferedReader)) {
            reader = new BufferedReader(reader);
        }
        reader.mark(1);
        try {
            if (reader.read() == -1) {
                return;
            }
        }
        catch (MalformedInputException e) {
            csvErrorHandler.handleError(
                ErrorIllegalCharacterOrByteSequence, location, lineNumber);
            throw e;
        }
        reader.reset();
        try {
            while (state != StateEOF) {
                switch (state) {

                  case StateFirstLine: {
                      ch = reader.read();
                      state = StateBeginningOfField;
                      break;
                  }

                  case StateBeginningOfLine: {
                      csvConsumer.consumeLineOfTokens(location,
                                                      lineNumberToConsume,
                                                      tokens);
                      tokens = ListUtil.list();
                      state = StateBeginningOfField;
                      break;
                  }

                  case StateBeginningOfField: {
                      while ((ch == Space) || (ch == Tab)) {
                          ch = reader.read();
                      }
                      if (ch == DoubleQuote) {
                          state = StateInQuotedField;
                          currentTokenIsQuoted = true;
                          ch = reader.read();
                      }
                      else {
                          state = StateInUnquotedField;
                          currentTokenIsQuoted = false;
                      }
                      break;
                  }

                  case StateEndOfField: {
                      String currentToken = token.toString();

                          // only trim if the value is not double-quoted
                      if (!currentTokenIsQuoted) {
                          currentToken = currentToken.trim();
                      }

                      while ((ch == Space) || (ch == Tab)) {
                          ch = reader.read();
                      }

                      if (returnNoValueAsNull &&
                          !currentTokenIsQuoted &&
                          currentToken.length() == 0)
                      {
                          tokens.add(null);
                      }
                      else {
                          tokens.add(currentToken);
                      }
                      token.truncateToLength(0);

                      if (ch == Comma) {
                          state = StateBeginningOfField;
                          ch = reader.read();
                      }

                      else if (ch == LF || ch == CR) {
                          lineNumberToConsume = lineNumber;
                          state = StateBeginningOfLine;
                          if (ch == LF) {
                              lineNumber++;
                          }

                          for (; ch == LF || ch == CR;) {
                              ch = reader.read();
                              if (ch == LF) {
                                  lineNumber++;
                              }
                          }

                          if (ch == -1) {
                              state = StateEOF;
                          }
                      }

                      else if (ch == -1) {
                          state = StateEOF;
                          lineNumberToConsume = lineNumber;
                          lineNumber++;
                      }

                      else {
                          csvErrorHandler.handleError(
                              ErrorMissingComma, location, lineNumber);
                          state = StateBeginningOfField;
                      }

                      break;
                  }

                  case StateInUnquotedField: {
                      while (ch >= 0 && ch != Comma && ch != CR && ch != LF) {
                          token.append((char)ch);
                          ch = reader.read();
                      }

                      state = StateEndOfField;
                      break;
                  }

                  case StateInQuotedField: {
                      while (state == StateInQuotedField) {
                          while (ch >= 0 && ch != DoubleQuote) {
                              if (ch != CR) {
                                  token.append((char)ch);
                              }
                              ch = reader.read();
                          }

                          /*
                              A doubleQuote ends the quoted token, unless there
                              are two in a row.  Two doubleQuotes in a row is
                              taken to mean a doubleQuote character value.
                          */
                          if (ch == DoubleQuote) {
                              ch = reader.read();
                              if (ch == DoubleQuote) {
                                  token.append((char)ch);
                                  ch = reader.read();
                              }
                              else {
                                  /* that was the matching quote */
                                  break;
View Full Code Here

        return size(receiver);
    }

    public String toString (Object receiver)
    {
        FastStringBuffer stringBuffer = new FastStringBuffer();
        stringBuffer.append("[Array:");
        int elementCount = Array.getLength(receiver);
        for (int index = elementCount - 1; index >= 0; index--) {
            Object currentElement = Array.get(receiver, index);
            stringBuffer.append("\n    ");
            stringBuffer.append(currentElement.toString());
            stringBuffer.append(",");
        }
        stringBuffer.append("]");
        return stringBuffer.toString();
    }
View Full Code Here

TOP

Related Classes of ariba.util.core.FastStringBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.