public interface IFieldResultFormatter
Example:
public void fieldResultFormatting() throws Exception {
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
FieldResultFormatter formatter = new FieldResultFormatter("$%d", "Date: %tb", "Item # %s:");
doc.getFieldOptions().setResultFormatter(formatter);
// Our field result formatter applies a custom format to newly created fields of three types of formats.
// Field result formatters apply new formatting to fields as they are updated,
// which happens as soon as we create them using this InsertField method overload.
// 1 - Numeric:
builder.insertField(" = 2 + 3 \\# $###");
Assert.assertEquals("$5", doc.getRange().getFields().get(0).getResult());
Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.NUMERIC));
// 2 - Date/time:
builder.insertField("DATE \\@ \"d MMMM yyyy\"");
Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: "));
Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.DATE_TIME));
// 3 - General:
builder.insertField("QUOTE \"2\" \\* Ordinal");
Assert.assertEquals("Item # 2:", doc.getRange().getFields().get(2).getResult());
Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.GENERAL));
formatter.printFormatInvocations();
}
/// <summary>
/// When fields with formatting are updated, this formatter will override their formatting
/// with a custom format, while tracking every invocation.
/// </summary>
private static class FieldResultFormatter implements IFieldResultFormatter {
public FieldResultFormatter(String numberFormat, String dateFormat, String generalFormat) {
mNumberFormat = numberFormat;
mDateFormat = dateFormat;
mGeneralFormat = generalFormat;
}
public String formatNumeric(double value, String format) {
if (mNumberFormat.isEmpty())
return null;
String newValue = String.format(mNumberFormat, (long) value);
mFormatInvocations.add(new FormatInvocation(FormatInvocationType.NUMERIC, value, format, newValue));
return newValue;
}
public String formatDateTime(Date value, String format, int calendarType) {
if (mDateFormat.isEmpty())
return null;
String newValue = String.format(mDateFormat, value);
mFormatInvocations.add(new FormatInvocation(FormatInvocationType.DATE_TIME, MessageFormat.format("{0} ({1})", value, calendarType), format, newValue));
return newValue;
}
public String format(String value, int format) {
return format((Object) value, format);
}
public String format(double value, int format) {
return format((Object) value, format);
}
private String format(Object value, int format) {
if (mGeneralFormat.isEmpty())
return null;
String newValue = String.format(mGeneralFormat, new DecimalFormat("#.####").format(value));
mFormatInvocations.add(new FormatInvocation(FormatInvocationType.GENERAL, value, GeneralFormat.toString(format), newValue));
return newValue;
}
public int countFormatInvocations(int formatInvocationType) {
if (formatInvocationType == FormatInvocationType.ALL)
return getFormatInvocations().size();
return (int) IterableUtils.countMatches(getFormatInvocations(), i -> i.getFormatInvocationType() == formatInvocationType);
}
public void printFormatInvocations() {
for (FormatInvocation f : getFormatInvocations())
System.out.println(MessageFormat.format("Invocation type:\t{0}\n" +
"\tOriginal value:\t\t{1}\n" +
"\tOriginal format:\t{2}\n" +
"\tNew value:\t\t\t{3}\n", f.getFormatInvocationType(), f.getValue(), f.getOriginalFormat(), f.getNewValue()));
}
private final String mNumberFormat;
private final String mDateFormat;
private final String mGeneralFormat;
private ArrayList<FormatInvocation> getFormatInvocations() {
return mFormatInvocations;
}
private final ArrayList<FormatInvocation> mFormatInvocations = new ArrayList<>();
private static class FormatInvocation {
public int getFormatInvocationType() {
return mFormatInvocationType;
}
private final int mFormatInvocationType;
public Object getValue() {
return mValue;
}
private final Object mValue;
public String getOriginalFormat() {
return mOriginalFormat;
}
private final String mOriginalFormat;
public String getNewValue() {
return mNewValue;
}
private final String mNewValue;
public FormatInvocation(int formatInvocationType, Object value, String originalFormat, String newValue) {
mValue = value;
mFormatInvocationType = formatInvocationType;
mOriginalFormat = originalFormat;
mNewValue = newValue;
}
}
public final class FormatInvocationType {
private FormatInvocationType() {
}
public static final int NUMERIC = 0;
public static final int DATE_TIME = 1;
public static final int GENERAL = 2;
public static final int ALL = 3;
public static final int length = 4;
}
}
Method Summary | ||
---|---|---|
abstract java.lang.String | format(double value, int format) | |
Called when Aspose.Words applies a number format switch, i.e. \* Ordinal. | ||
abstract java.lang.String | format(java.lang.String value, int format) | |
Called when Aspose.Words applies a capitalization format switch, i.e. \* Upper. | ||
abstract java.lang.String | formatDateTime(java.util.Date value, java.lang.String format, int calendarType) | |
Called when Aspose.Words applies a date/time format switch, i.e. \@ "dd.MM.yyyy". | ||
abstract java.lang.String | formatNumeric(double value, java.lang.String format) | |
Called when Aspose.Words applies a numeric format switch, i.e. \# "#.##". |
Method Detail |
---|
format | |
public abstract java.lang.String format(double value, int format) |
format
- A GeneralFormat value.Example:
Shows how to automatically apply a custom format to field results as the fields are updated.public void fieldResultFormatting() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldResultFormatter formatter = new FieldResultFormatter("$%d", "Date: %tb", "Item # %s:"); doc.getFieldOptions().setResultFormatter(formatter); // Our field result formatter applies a custom format to newly created fields of three types of formats. // Field result formatters apply new formatting to fields as they are updated, // which happens as soon as we create them using this InsertField method overload. // 1 - Numeric: builder.insertField(" = 2 + 3 \\# $###"); Assert.assertEquals("$5", doc.getRange().getFields().get(0).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.NUMERIC)); // 2 - Date/time: builder.insertField("DATE \\@ \"d MMMM yyyy\""); Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: ")); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.DATE_TIME)); // 3 - General: builder.insertField("QUOTE \"2\" \\* Ordinal"); Assert.assertEquals("Item # 2:", doc.getRange().getFields().get(2).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.GENERAL)); formatter.printFormatInvocations(); } /// <summary> /// When fields with formatting are updated, this formatter will override their formatting /// with a custom format, while tracking every invocation. /// </summary> private static class FieldResultFormatter implements IFieldResultFormatter { public FieldResultFormatter(String numberFormat, String dateFormat, String generalFormat) { mNumberFormat = numberFormat; mDateFormat = dateFormat; mGeneralFormat = generalFormat; } public String formatNumeric(double value, String format) { if (mNumberFormat.isEmpty()) return null; String newValue = String.format(mNumberFormat, (long) value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.NUMERIC, value, format, newValue)); return newValue; } public String formatDateTime(Date value, String format, int calendarType) { if (mDateFormat.isEmpty()) return null; String newValue = String.format(mDateFormat, value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.DATE_TIME, MessageFormat.format("{0} ({1})", value, calendarType), format, newValue)); return newValue; } public String format(String value, int format) { return format((Object) value, format); } public String format(double value, int format) { return format((Object) value, format); } private String format(Object value, int format) { if (mGeneralFormat.isEmpty()) return null; String newValue = String.format(mGeneralFormat, new DecimalFormat("#.####").format(value)); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.GENERAL, value, GeneralFormat.toString(format), newValue)); return newValue; } public int countFormatInvocations(int formatInvocationType) { if (formatInvocationType == FormatInvocationType.ALL) return getFormatInvocations().size(); return (int) IterableUtils.countMatches(getFormatInvocations(), i -> i.getFormatInvocationType() == formatInvocationType); } public void printFormatInvocations() { for (FormatInvocation f : getFormatInvocations()) System.out.println(MessageFormat.format("Invocation type:\t{0}\n" + "\tOriginal value:\t\t{1}\n" + "\tOriginal format:\t{2}\n" + "\tNew value:\t\t\t{3}\n", f.getFormatInvocationType(), f.getValue(), f.getOriginalFormat(), f.getNewValue())); } private final String mNumberFormat; private final String mDateFormat; private final String mGeneralFormat; private ArrayList<FormatInvocation> getFormatInvocations() { return mFormatInvocations; } private final ArrayList<FormatInvocation> mFormatInvocations = new ArrayList<>(); private static class FormatInvocation { public int getFormatInvocationType() { return mFormatInvocationType; } private final int mFormatInvocationType; public Object getValue() { return mValue; } private final Object mValue; public String getOriginalFormat() { return mOriginalFormat; } private final String mOriginalFormat; public String getNewValue() { return mNewValue; } private final String mNewValue; public FormatInvocation(int formatInvocationType, Object value, String originalFormat, String newValue) { mValue = value; mFormatInvocationType = formatInvocationType; mOriginalFormat = originalFormat; mNewValue = newValue; } } public final class FormatInvocationType { private FormatInvocationType() { } public static final int NUMERIC = 0; public static final int DATE_TIME = 1; public static final int GENERAL = 2; public static final int ALL = 3; public static final int length = 4; } }
format | |
public abstract java.lang.String format(java.lang.String value, int format) |
format
- A GeneralFormat value.Example:
Shows how to automatically apply a custom format to field results as the fields are updated.public void fieldResultFormatting() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldResultFormatter formatter = new FieldResultFormatter("$%d", "Date: %tb", "Item # %s:"); doc.getFieldOptions().setResultFormatter(formatter); // Our field result formatter applies a custom format to newly created fields of three types of formats. // Field result formatters apply new formatting to fields as they are updated, // which happens as soon as we create them using this InsertField method overload. // 1 - Numeric: builder.insertField(" = 2 + 3 \\# $###"); Assert.assertEquals("$5", doc.getRange().getFields().get(0).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.NUMERIC)); // 2 - Date/time: builder.insertField("DATE \\@ \"d MMMM yyyy\""); Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: ")); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.DATE_TIME)); // 3 - General: builder.insertField("QUOTE \"2\" \\* Ordinal"); Assert.assertEquals("Item # 2:", doc.getRange().getFields().get(2).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.GENERAL)); formatter.printFormatInvocations(); } /// <summary> /// When fields with formatting are updated, this formatter will override their formatting /// with a custom format, while tracking every invocation. /// </summary> private static class FieldResultFormatter implements IFieldResultFormatter { public FieldResultFormatter(String numberFormat, String dateFormat, String generalFormat) { mNumberFormat = numberFormat; mDateFormat = dateFormat; mGeneralFormat = generalFormat; } public String formatNumeric(double value, String format) { if (mNumberFormat.isEmpty()) return null; String newValue = String.format(mNumberFormat, (long) value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.NUMERIC, value, format, newValue)); return newValue; } public String formatDateTime(Date value, String format, int calendarType) { if (mDateFormat.isEmpty()) return null; String newValue = String.format(mDateFormat, value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.DATE_TIME, MessageFormat.format("{0} ({1})", value, calendarType), format, newValue)); return newValue; } public String format(String value, int format) { return format((Object) value, format); } public String format(double value, int format) { return format((Object) value, format); } private String format(Object value, int format) { if (mGeneralFormat.isEmpty()) return null; String newValue = String.format(mGeneralFormat, new DecimalFormat("#.####").format(value)); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.GENERAL, value, GeneralFormat.toString(format), newValue)); return newValue; } public int countFormatInvocations(int formatInvocationType) { if (formatInvocationType == FormatInvocationType.ALL) return getFormatInvocations().size(); return (int) IterableUtils.countMatches(getFormatInvocations(), i -> i.getFormatInvocationType() == formatInvocationType); } public void printFormatInvocations() { for (FormatInvocation f : getFormatInvocations()) System.out.println(MessageFormat.format("Invocation type:\t{0}\n" + "\tOriginal value:\t\t{1}\n" + "\tOriginal format:\t{2}\n" + "\tNew value:\t\t\t{3}\n", f.getFormatInvocationType(), f.getValue(), f.getOriginalFormat(), f.getNewValue())); } private final String mNumberFormat; private final String mDateFormat; private final String mGeneralFormat; private ArrayList<FormatInvocation> getFormatInvocations() { return mFormatInvocations; } private final ArrayList<FormatInvocation> mFormatInvocations = new ArrayList<>(); private static class FormatInvocation { public int getFormatInvocationType() { return mFormatInvocationType; } private final int mFormatInvocationType; public Object getValue() { return mValue; } private final Object mValue; public String getOriginalFormat() { return mOriginalFormat; } private final String mOriginalFormat; public String getNewValue() { return mNewValue; } private final String mNewValue; public FormatInvocation(int formatInvocationType, Object value, String originalFormat, String newValue) { mValue = value; mFormatInvocationType = formatInvocationType; mOriginalFormat = originalFormat; mNewValue = newValue; } } public final class FormatInvocationType { private FormatInvocationType() { } public static final int NUMERIC = 0; public static final int DATE_TIME = 1; public static final int GENERAL = 2; public static final int ALL = 3; public static final int length = 4; } }
formatDateTime | |
public abstract java.lang.String formatDateTime(java.util.Date value, java.lang.String format, int calendarType) |
calendarType
- A CalendarType value.Example:
Shows how to automatically apply a custom format to field results as the fields are updated.public void fieldResultFormatting() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldResultFormatter formatter = new FieldResultFormatter("$%d", "Date: %tb", "Item # %s:"); doc.getFieldOptions().setResultFormatter(formatter); // Our field result formatter applies a custom format to newly created fields of three types of formats. // Field result formatters apply new formatting to fields as they are updated, // which happens as soon as we create them using this InsertField method overload. // 1 - Numeric: builder.insertField(" = 2 + 3 \\# $###"); Assert.assertEquals("$5", doc.getRange().getFields().get(0).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.NUMERIC)); // 2 - Date/time: builder.insertField("DATE \\@ \"d MMMM yyyy\""); Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: ")); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.DATE_TIME)); // 3 - General: builder.insertField("QUOTE \"2\" \\* Ordinal"); Assert.assertEquals("Item # 2:", doc.getRange().getFields().get(2).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.GENERAL)); formatter.printFormatInvocations(); } /// <summary> /// When fields with formatting are updated, this formatter will override their formatting /// with a custom format, while tracking every invocation. /// </summary> private static class FieldResultFormatter implements IFieldResultFormatter { public FieldResultFormatter(String numberFormat, String dateFormat, String generalFormat) { mNumberFormat = numberFormat; mDateFormat = dateFormat; mGeneralFormat = generalFormat; } public String formatNumeric(double value, String format) { if (mNumberFormat.isEmpty()) return null; String newValue = String.format(mNumberFormat, (long) value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.NUMERIC, value, format, newValue)); return newValue; } public String formatDateTime(Date value, String format, int calendarType) { if (mDateFormat.isEmpty()) return null; String newValue = String.format(mDateFormat, value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.DATE_TIME, MessageFormat.format("{0} ({1})", value, calendarType), format, newValue)); return newValue; } public String format(String value, int format) { return format((Object) value, format); } public String format(double value, int format) { return format((Object) value, format); } private String format(Object value, int format) { if (mGeneralFormat.isEmpty()) return null; String newValue = String.format(mGeneralFormat, new DecimalFormat("#.####").format(value)); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.GENERAL, value, GeneralFormat.toString(format), newValue)); return newValue; } public int countFormatInvocations(int formatInvocationType) { if (formatInvocationType == FormatInvocationType.ALL) return getFormatInvocations().size(); return (int) IterableUtils.countMatches(getFormatInvocations(), i -> i.getFormatInvocationType() == formatInvocationType); } public void printFormatInvocations() { for (FormatInvocation f : getFormatInvocations()) System.out.println(MessageFormat.format("Invocation type:\t{0}\n" + "\tOriginal value:\t\t{1}\n" + "\tOriginal format:\t{2}\n" + "\tNew value:\t\t\t{3}\n", f.getFormatInvocationType(), f.getValue(), f.getOriginalFormat(), f.getNewValue())); } private final String mNumberFormat; private final String mDateFormat; private final String mGeneralFormat; private ArrayList<FormatInvocation> getFormatInvocations() { return mFormatInvocations; } private final ArrayList<FormatInvocation> mFormatInvocations = new ArrayList<>(); private static class FormatInvocation { public int getFormatInvocationType() { return mFormatInvocationType; } private final int mFormatInvocationType; public Object getValue() { return mValue; } private final Object mValue; public String getOriginalFormat() { return mOriginalFormat; } private final String mOriginalFormat; public String getNewValue() { return mNewValue; } private final String mNewValue; public FormatInvocation(int formatInvocationType, Object value, String originalFormat, String newValue) { mValue = value; mFormatInvocationType = formatInvocationType; mOriginalFormat = originalFormat; mNewValue = newValue; } } public final class FormatInvocationType { private FormatInvocationType() { } public static final int NUMERIC = 0; public static final int DATE_TIME = 1; public static final int GENERAL = 2; public static final int ALL = 3; public static final int length = 4; } }
formatNumeric | |
public abstract java.lang.String formatNumeric(double value, java.lang.String format) |
Example:
Shows how to automatically apply a custom format to field results as the fields are updated.public void fieldResultFormatting() throws Exception { Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); FieldResultFormatter formatter = new FieldResultFormatter("$%d", "Date: %tb", "Item # %s:"); doc.getFieldOptions().setResultFormatter(formatter); // Our field result formatter applies a custom format to newly created fields of three types of formats. // Field result formatters apply new formatting to fields as they are updated, // which happens as soon as we create them using this InsertField method overload. // 1 - Numeric: builder.insertField(" = 2 + 3 \\# $###"); Assert.assertEquals("$5", doc.getRange().getFields().get(0).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.NUMERIC)); // 2 - Date/time: builder.insertField("DATE \\@ \"d MMMM yyyy\""); Assert.assertTrue(doc.getRange().getFields().get(1).getResult().startsWith("Date: ")); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.DATE_TIME)); // 3 - General: builder.insertField("QUOTE \"2\" \\* Ordinal"); Assert.assertEquals("Item # 2:", doc.getRange().getFields().get(2).getResult()); Assert.assertEquals(1, formatter.countFormatInvocations(FieldResultFormatter.FormatInvocationType.GENERAL)); formatter.printFormatInvocations(); } /// <summary> /// When fields with formatting are updated, this formatter will override their formatting /// with a custom format, while tracking every invocation. /// </summary> private static class FieldResultFormatter implements IFieldResultFormatter { public FieldResultFormatter(String numberFormat, String dateFormat, String generalFormat) { mNumberFormat = numberFormat; mDateFormat = dateFormat; mGeneralFormat = generalFormat; } public String formatNumeric(double value, String format) { if (mNumberFormat.isEmpty()) return null; String newValue = String.format(mNumberFormat, (long) value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.NUMERIC, value, format, newValue)); return newValue; } public String formatDateTime(Date value, String format, int calendarType) { if (mDateFormat.isEmpty()) return null; String newValue = String.format(mDateFormat, value); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.DATE_TIME, MessageFormat.format("{0} ({1})", value, calendarType), format, newValue)); return newValue; } public String format(String value, int format) { return format((Object) value, format); } public String format(double value, int format) { return format((Object) value, format); } private String format(Object value, int format) { if (mGeneralFormat.isEmpty()) return null; String newValue = String.format(mGeneralFormat, new DecimalFormat("#.####").format(value)); mFormatInvocations.add(new FormatInvocation(FormatInvocationType.GENERAL, value, GeneralFormat.toString(format), newValue)); return newValue; } public int countFormatInvocations(int formatInvocationType) { if (formatInvocationType == FormatInvocationType.ALL) return getFormatInvocations().size(); return (int) IterableUtils.countMatches(getFormatInvocations(), i -> i.getFormatInvocationType() == formatInvocationType); } public void printFormatInvocations() { for (FormatInvocation f : getFormatInvocations()) System.out.println(MessageFormat.format("Invocation type:\t{0}\n" + "\tOriginal value:\t\t{1}\n" + "\tOriginal format:\t{2}\n" + "\tNew value:\t\t\t{3}\n", f.getFormatInvocationType(), f.getValue(), f.getOriginalFormat(), f.getNewValue())); } private final String mNumberFormat; private final String mDateFormat; private final String mGeneralFormat; private ArrayList<FormatInvocation> getFormatInvocations() { return mFormatInvocations; } private final ArrayList<FormatInvocation> mFormatInvocations = new ArrayList<>(); private static class FormatInvocation { public int getFormatInvocationType() { return mFormatInvocationType; } private final int mFormatInvocationType; public Object getValue() { return mValue; } private final Object mValue; public String getOriginalFormat() { return mOriginalFormat; } private final String mOriginalFormat; public String getNewValue() { return mNewValue; } private final String mNewValue; public FormatInvocation(int formatInvocationType, Object value, String originalFormat, String newValue) { mValue = value; mFormatInvocationType = formatInvocationType; mOriginalFormat = originalFormat; mNewValue = newValue; } } public final class FormatInvocationType { private FormatInvocationType() { } public static final int NUMERIC = 0; public static final int DATE_TIME = 1; public static final int GENERAL = 2; public static final int ALL = 3; public static final int length = 4; } }