11package com .vgerbot .propify .generator ;
22
3- import com .ibm .icu .text .MessageFormat ;
4- import com .ibm .icu .text .MeasureFormat ;
5- import com .ibm .icu .text .MessagePattern ;
63import com .squareup .javapoet .*;
7- import com .vgerbot .propify .common .MessageFormatParser ;
84import com .vgerbot .propify .common .Utils ;
5+ import com .vgerbot .propify .i18n .ICUMessageTemplateExtension ;
6+ import com .vgerbot .propify .i18n .ICUTemplateArgumentsParser ;
97import com .vgerbot .propify .i18n .Message ;
108import com .vgerbot .propify .i18n .PropifyI18nResourceBundle ;
11- import com .vgerbot .propify .i18n .ICUMessageTemplateExtension ;
129
1310import javax .lang .model .element .Modifier ;
14- import java .lang .reflect .Field ;
15- import java .text .Format ;
16- import java .util .*;
11+ import java .util .ArrayList ;
12+ import java .util .List ;
13+ import java .util .Locale ;
14+ import java .util .ResourceBundle ;
1715import java .util .stream .Collectors ;
18- import java .util .Date ;
1916
2017public class I18nJavaPoetCodeGenerator {
2118 private static class SingletonHolder {
@@ -69,43 +66,28 @@ private Iterable<MethodSpec> generateInterfaceMethods(ResourceBundle bundle) {
6966 return bundle .keySet ().stream ().map (key -> {
7067 String methodName = Utils .convertToFieldName (key );
7168 Object value = bundle .getObject (key );
72- List <ParameterSpec > parameterSpecs = new ArrayList <>();;
73- Set <String > formatArgumentNames = new HashSet <>();
74- if (value instanceof CharSequence ) {
75- List <MessageFormatParser .PlaceholderInfo > placeholders = MessageFormatParser .parsePlaceholders (value .toString ());
69+ List <ICUTemplateArgumentsParser .Argument > arguments ;
7670
77- placeholders .forEach (placeholder -> {
78- Class <?> type = getArgType (placeholder .getFormatType ());
79- String argName = placeholder .getName ();
80- formatArgumentNames .add (argName );
81- parameterSpecs .add (
82- ParameterSpec .builder (type , Utils .convertToFieldName (argName )).build ()
83- );
84- });
71+ if (value instanceof CharSequence ) {
72+ arguments = ICUTemplateArgumentsParser .parseTemplate (value .toString ());
73+ } else {
74+ arguments = new ArrayList <>(0 );
8575 }
8676
87- String [] parameterNames = parameterSpecs .stream ().map (it -> it .name ).toArray (String []::new );
88- String format = "{" + String .join ("," , Arrays .stream (parameterNames ).map (v -> "$S" ).toArray (String []::new )) + "}" ;
77+ String format = "{" + arguments .stream ().map (v -> "$S" ).collect (Collectors .joining ("," )) + "}" ;
8978 AnnotationSpec annotation = AnnotationSpec .builder (Message .class )
9079 .addMember ("key" , CodeBlock .of ("$S" , key ))
91- .addMember ("arguments" , CodeBlock .of (format , formatArgumentNames .stream ().toArray ()))
80+ .addMember ("arguments" , CodeBlock .of (format , arguments .stream (). map ( ICUTemplateArgumentsParser . Argument :: getName ).toArray ()))
9281 .build ();
9382 return MethodSpec .methodBuilder (methodName )
9483 .addModifiers (Modifier .PUBLIC , Modifier .ABSTRACT )
9584 .returns (String .class )
96- .addParameters (parameterSpecs )
85+ .addParameters (arguments . stream (). map ( it -> ParameterSpec . builder ( it . getType (), Utils . convertToFieldName ( it . getName ())). build ()). collect ( Collectors . toList ()) )
9786 .addAnnotation (annotation )
9887 .build ();
9988 }).collect (Collectors .toList ());
10089 }
10190
102- private List <ParameterSpec > generateMethodParameters (String pattern ) {
103- MessageFormat format = new MessageFormat (pattern );
104- Set <String > argumentNames = format .getArgumentNames ();
105- return argumentNames .stream ().map (argName -> ParameterSpec .builder (String .class , Utils .convertToFieldName (argName )).build ())
106- .collect (Collectors .toList ());
107- }
108-
10991 private MethodSpec generateGetMethod () {
11092 return MethodSpec .methodBuilder ("get" )
11193 .addModifiers (Modifier .PUBLIC , Modifier .STATIC )
@@ -125,61 +107,4 @@ private MethodSpec generateGetDefaultMethod(String defaultLocale) {
125107 : CodeBlock .of ("return get($T.forLanguageTag($S))" , Locale .class , defaultLocale ))
126108 .build ();
127109 }
128-
129- /**
130- * Determines the appropriate Java type based on the format type
131- * @param format The ICU Format object
132- * @return The appropriate Java class type for the parameter
133- */
134- private Class <?> getArgTypeFromFormat (Format format ) {
135- if (format == null ) {
136- return String .class ;
137- }
138-
139- // ICU NumberFormat and its subclasses
140- if (format instanceof com .ibm .icu .text .NumberFormat ) {
141- return Number .class ;
142- }
143-
144- // ICU DateFormat and its subclasses
145- if (format instanceof com .ibm .icu .text .DateFormat ) {
146- return Date .class ;
147- }
148-
149- // MeasureFormat uses Measure objects with Number values
150- if (format instanceof com .ibm .icu .text .MeasureFormat ) {
151- return Number .class ;
152- }
153-
154- // Plural format typically takes numbers
155- if (format instanceof com .ibm .icu .text .PluralFormat ) {
156- return Number .class ;
157- }
158-
159- // Select format takes strings
160- if (format instanceof com .ibm .icu .text .SelectFormat ) {
161- return String .class ;
162- }
163-
164- // For any other format type
165- return Object .class ;
166- }
167- private Class <?> getArgType (String type ) {
168- if (type == null ) {
169- return String .class ;
170- }
171- switch (type ) {
172- case "date" :
173- return Date .class ;
174- case "number" :
175- return Number .class ;
176- case "select" :
177- return String .class ;
178- case "plural" :
179- case "selectordinal" :
180- case "choice" :
181- return Number .class ;
182- }
183- return Object .class ;
184- }
185110}
0 commit comments