java.util.FormatFlagsConversionMismatchException Java Examples

The following examples show how to use java.util.FormatFlagsConversionMismatchException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleMessageFormatterTest.java    From flogger with Apache License 2.0 5 votes vote down vote up
private static void assertFormatFlagsConversionMismatchException(String format, Object arg) {
  try {
    log(format, arg);
    fail("expected FormatFlagsConversionMismatchException");
  } catch (FormatFlagsConversionMismatchException expected) {
  }
}
 
Example #2
Source File: FormatterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void assertFormatFlagsConversionMismatchException(Formatter f, String str) {
    try {
        f.format(str);
        fail("should throw FormatFlagsConversionMismatchException: "
                + str);
        /*
        * error on RI, throw IllegalFormatFlagsException specification
        * says FormatFlagsConversionMismatchException should be thrown
        */
    } catch (FormatFlagsConversionMismatchException e) {
        // expected
    }
}
 
Example #3
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
FormatSpecifier(String source, String[] sa)
           throws FormatFlagsConversionMismatchException,
           FormatterNumberFormatException {
	int idx = 0;
	this.source = source;
	index(sa[idx++]);
	flags(sa[idx++]);
	width(sa[idx++]);
	precision(sa[idx++]);

	if (sa[idx] != null) {
		dt = true;
		if (sa[idx].equals("T"))
			f.add(Flags.UPPERCASE);
	}
	conversion(sa[++idx]);

	if (dt)
		checkDateTime();
	else if (Conversion.isGeneral(c))
		checkGeneral();
	else if (Conversion.isCharacter(c))
		checkCharacter();
	else if (Conversion.isInteger(c))
		checkInteger();
	else if (Conversion.isFloat(c))
		checkFloat();
	else if (Conversion.isText(c))
		checkText();
	else
		throw new UnknownFormatConversionException(String.valueOf(c));
}
 
Example #4
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void checkGeneral() throws FormatFlagsConversionMismatchException {
	if ((c == Conversion.BOOLEAN || c == Conversion.HASHCODE)
			&& f.contains(Flags.ALTERNATE))
		failMismatch(Flags.ALTERNATE, c);
	// '-' requires a width
	if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
		throw new MissingFormatWidthException(toString());
	checkBadFlags(Flags.PLUS, Flags.LEADING_SPACE, Flags.ZERO_PAD,
			Flags.GROUP, Flags.PARENTHESES);
}
 
Example #5
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void checkDateTime() throws FormatFlagsConversionMismatchException {
	if (precision != -1)
		throw new IllegalFormatPrecisionException(precision);
	if (!DateTime.isValid(c))
		throw new UnknownFormatConversionException("t" + c);
	checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE,
			Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES);
	// '-' requires a width
	if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
		throw new MissingFormatWidthException(toString());
}
 
Example #6
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void checkCharacter() throws FormatFlagsConversionMismatchException {
	if (precision != -1)
		throw new IllegalFormatPrecisionException(precision);
	checkBadFlags(Flags.ALTERNATE, Flags.PLUS, Flags.LEADING_SPACE,
			Flags.ZERO_PAD, Flags.GROUP, Flags.PARENTHESES);
	// '-' requires a width
	if (width == -1 && f.contains(Flags.LEFT_JUSTIFY))
		throw new MissingFormatWidthException(toString());
}
 
Example #7
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void checkInteger() throws FormatFlagsConversionMismatchException {
	checkNumeric();
	if (precision != -1)
		throw new IllegalFormatPrecisionException(precision);

	if (c == Conversion.DECIMAL_INTEGER)
		checkBadFlags(Flags.ALTERNATE);
	else
		checkBadFlags(Flags.GROUP);
}
 
Example #8
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void checkFloat() throws FormatFlagsConversionMismatchException {
	checkNumeric();
	if (c == Conversion.DECIMAL_FLOAT) {
	} else if (c == Conversion.HEXADECIMAL_FLOAT) {
		checkBadFlags(Flags.PARENTHESES, Flags.GROUP);
	} else if (c == Conversion.SCIENTIFIC) {
		checkBadFlags(Flags.GROUP);
	} else if (c == Conversion.GENERAL) {
		checkBadFlags(Flags.ALTERNATE);
	}
}
 
Example #9
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void printInteger(String arg)
		throws IllegalFormatConversionException,
		FormatFlagsConversionMismatchException {
	if (mightBeUnknown(arg))
		return;
	if (matchSig(arg, Byte.class, Short.class, Integer.class, Long.class))
		printLong();
	else if (matchSig(arg, BigInteger.class)) {
	} else
		failConversion(arg);
}
 
Example #10
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 5 votes vote down vote up
private void printLong() throws FormatFlagsConversionMismatchException {

		if (c == Conversion.OCTAL_INTEGER) {
			checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE, Flags.PLUS);
		} else if (c == Conversion.HEXADECIMAL_INTEGER) {
			checkBadFlags(Flags.PARENTHESES, Flags.LEADING_SPACE, Flags.PLUS);
		}

	}
 
Example #11
Source File: Formatter.java    From bazel with Apache License 2.0 5 votes vote down vote up
private static List<FormatSpecifier> parse(String s)
           throws FormatFlagsConversionMismatchException,
           FormatterNumberFormatException {
	ArrayList<FormatSpecifier> al = new ArrayList<FormatSpecifier>();
	Matcher m = fsPattern.matcher(s);
	int i = 0;
	while (i < s.length()) {
		if (m.find(i)) {
			// Anything between the start of the string and the beginning
			// of the format specifier is either fixed text or contains
			// an invalid format string.
			if (m.start() != i) {
				// Make sure we didn't miss any invalid format specifiers
				checkText(s.substring(i, m.start()));
			}

			// Expect 6 groups in regular expression
			String[] sa = new String[6];
			for (int j = 0; j < m.groupCount(); j++) {
				sa[j] = m.group(j + 1);
			}
			al.add(new FormatSpecifier(m.group(0), sa));
			i = m.end();
		} else {
			// No more valid format specifiers. Check for possible invalid
			// format specifiers.
			checkText(s.substring(i));
			// The rest of the string is fixed text
			break;

		}
	}

	return al;
}
 
Example #12
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 4 votes vote down vote up
private void checkBadFlags(Flags... badFlags)
		throws FormatFlagsConversionMismatchException {
	for (int i = 0; i < badFlags.length; i++)
		if (f.contains(badFlags[i]))
			failMismatch(badFlags[i], c);
}
 
Example #13
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 4 votes vote down vote up
public void print(String arg, int argIndex)
		throws IllegalFormatConversionException,
		FormatFlagsConversionMismatchException {

	try {
		if (arg.charAt(0) == '[')

			failConversion(arg);

		if (dt) {
			printDateTime(arg);
			return;
		}
		switch (c) {
		case Conversion.DECIMAL_INTEGER:
		case Conversion.OCTAL_INTEGER:
		case Conversion.HEXADECIMAL_INTEGER:
			printInteger(arg);
			break;
		case Conversion.SCIENTIFIC:
		case Conversion.GENERAL:
		case Conversion.DECIMAL_FLOAT:
		case Conversion.HEXADECIMAL_FLOAT:
			printFloat(arg);
			break;
		case Conversion.CHARACTER:
		case Conversion.CHARACTER_UPPER:
			printCharacter(arg);
			break;
		case Conversion.BOOLEAN:
			printBoolean(arg);
			break;
		case Conversion.STRING:
		case Conversion.HASHCODE:
		case Conversion.LINE_SEPARATOR:
		case Conversion.PERCENT_SIGN:
			break;
		default:
               throw new UnknownFormatConversionException(String.valueOf(c));
		}
	} catch (IllegalFormatConversionException e) {
		e.setArgIndex(argIndex);
		throw e;
	}
}
 
Example #14
Source File: FormatSpecifier.java    From bazel with Apache License 2.0 4 votes vote down vote up
private void failMismatch(Flags f, char c)
		throws FormatFlagsConversionMismatchException {
	String fs = f.toString();
	throw new FormatFlagsConversionMismatchException(fs, c);
}
 
Example #15
Source File: Formatter.java    From bazel with Apache License 2.0 4 votes vote down vote up
public static void check(String format, String... args)
		throws ExtraFormatArgumentsException,
		IllegalFormatConversionException, IllegalFormatException,
		FormatFlagsConversionMismatchException,
           MissingFormatArgumentException, FormatterNumberFormatException {

	// index of last argument referenced
	int last = -1;
	// last ordinary index
	int lasto = -1;

	// last index used
	int maxIndex = -1;

	for (FormatSpecifier fs : parse(format)) {
		int index = fs.index();
		switch (index) {
		case -2:
			// ignore it
			break;
		case -1: // relative index
			if (last < 0 || last > args.length - 1)
				throw new MissingFormatArgumentException(last,
						fs.toString());
			fs.print(args[last], last);
			break;
		case 0: // ordinary index
			lasto++;
			last = lasto;
			if (lasto > args.length - 1)
				throw new MissingFormatArgumentException(lasto,
						fs.toString());
			maxIndex = Math.max(maxIndex, lasto);
			fs.print(args[lasto], lasto);
			break;
		default: // explicit index
			last = index - 1;
			if (last > args.length - 1)
				throw new MissingFormatArgumentException(last,
						fs.toString());
			maxIndex = Math.max(maxIndex, last);
			fs.print(args[last], last);
			break;
		}

	}
	if (maxIndex < args.length - 1) {
		throw new ExtraFormatArgumentsException(args.length, maxIndex + 1);
	}
}