@@ -93,17 +93,9 @@ public final class DateProcessor extends AbstractProcessor {
9393 formatter = DateFormatter .forPattern (this .outputFormat );
9494 }
9595
96- private static ZoneId newDateTimeZone (String timezone ) {
97- return timezone == null ? ZoneOffset .UTC : ZoneId .of (timezone );
98- }
99-
100- private static Locale newLocale (String locale ) {
101- return locale == null ? Locale .ENGLISH : LocaleUtils .parse (locale );
102- }
103-
10496 @ Override
105- public IngestDocument execute (IngestDocument ingestDocument ) {
106- Object obj = ingestDocument .getFieldValue (field , Object .class );
97+ public IngestDocument execute (IngestDocument document ) {
98+ Object obj = document .getFieldValue (field , Object .class );
10799 String value = null ;
108100 if (obj != null ) {
109101 // Don't use Objects.toString(...) here, because null gets changed to "null" which may confuse some date parsers
@@ -114,10 +106,9 @@ public IngestDocument execute(IngestDocument ingestDocument) {
114106 // extract the timezone and locale to use for date parsing
115107 final ZoneId documentTimezone ;
116108 final Locale documentLocale ;
117- final Map <String , Object > sourceAndMetadata = ingestDocument .getSourceAndMetadata ();
118109 try {
119- documentTimezone = newDateTimeZone ( timezone == null ? null : timezone . newInstance ( sourceAndMetadata ). execute () );
120- documentLocale = newLocale ( locale == null ? null : locale . newInstance ( sourceAndMetadata ). execute () );
110+ documentTimezone = getTimezone ( document );
111+ documentLocale = getLocale ( document );
121112 } catch (Exception e ) {
122113 throw new IllegalArgumentException ("unable to parse date [" + value + "]" , e );
123114 }
@@ -138,21 +129,33 @@ public IngestDocument execute(IngestDocument ingestDocument) {
138129 throw new IllegalArgumentException ("unable to parse date [" + value + "]" , lastException );
139130 }
140131
141- ingestDocument .setFieldValue (targetField , formatter .format (dateTime ));
142- return ingestDocument ;
132+ document .setFieldValue (targetField , formatter .format (dateTime ));
133+ return document ;
143134 }
144135
145136 @ Override
146137 public String getType () {
147138 return TYPE ;
148139 }
149140
150- TemplateScript .Factory getTimezone () {
151- return timezone ;
141+ // visible for testing
142+ ZoneId getTimezone (IngestDocument document ) {
143+ String value = timezone == null ? null : document .renderTemplate (timezone );
144+ if (value == null ) {
145+ return ZoneOffset .UTC ;
146+ } else {
147+ return ZoneId .of (value );
148+ }
152149 }
153150
154- TemplateScript .Factory getLocale () {
155- return locale ;
151+ // visible for testing
152+ Locale getLocale (IngestDocument document ) {
153+ String value = locale == null ? null : document .renderTemplate (locale );
154+ if (value == null ) {
155+ return Locale .ENGLISH ;
156+ } else {
157+ return LocaleUtils .parse (value );
158+ }
156159 }
157160
158161 String getField () {
@@ -179,40 +182,30 @@ public Factory(ScriptService scriptService) {
179182 this .scriptService = scriptService ;
180183 }
181184
182- public DateProcessor create (
183- Map <String , Processor .Factory > registry ,
184- String processorTag ,
185- String description ,
186- Map <String , Object > config
187- ) throws Exception {
188- String field = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "field" );
189- String targetField = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "target_field" , DEFAULT_TARGET_FIELD );
190- String timezoneString = ConfigurationUtils .readOptionalStringProperty (TYPE , processorTag , config , "timezone" );
185+ public DateProcessor create (Map <String , Processor .Factory > registry , String tag , String description , Map <String , Object > config )
186+ throws Exception {
187+ String field = ConfigurationUtils .readStringProperty (TYPE , tag , config , "field" );
188+ String targetField = ConfigurationUtils .readStringProperty (TYPE , tag , config , "target_field" , DEFAULT_TARGET_FIELD );
189+ String timezoneString = ConfigurationUtils .readOptionalStringProperty (TYPE , tag , config , "timezone" );
191190 TemplateScript .Factory compiledTimezoneTemplate = null ;
192191 if (timezoneString != null ) {
193- compiledTimezoneTemplate = ConfigurationUtils .compileTemplate (
194- TYPE ,
195- processorTag ,
196- "timezone" ,
197- timezoneString ,
198- scriptService
199- );
192+ compiledTimezoneTemplate = ConfigurationUtils .compileTemplate (TYPE , tag , "timezone" , timezoneString , scriptService );
200193 }
201- String localeString = ConfigurationUtils .readOptionalStringProperty (TYPE , processorTag , config , "locale" );
194+ String localeString = ConfigurationUtils .readOptionalStringProperty (TYPE , tag , config , "locale" );
202195 TemplateScript .Factory compiledLocaleTemplate = null ;
203196 if (localeString != null ) {
204- compiledLocaleTemplate = ConfigurationUtils .compileTemplate (TYPE , processorTag , "locale" , localeString , scriptService );
197+ compiledLocaleTemplate = ConfigurationUtils .compileTemplate (TYPE , tag , "locale" , localeString , scriptService );
205198 }
206- List <String > formats = ConfigurationUtils .readList (TYPE , processorTag , config , "formats" );
207- String outputFormat = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "output_format" , DEFAULT_OUTPUT_FORMAT );
199+ List <String > formats = ConfigurationUtils .readList (TYPE , tag , config , "formats" );
200+ String outputFormat = ConfigurationUtils .readStringProperty (TYPE , tag , config , "output_format" , DEFAULT_OUTPUT_FORMAT );
208201 try {
209202 DateFormatter .forPattern (outputFormat );
210203 } catch (Exception e ) {
211204 throw new IllegalArgumentException ("invalid output format [" + outputFormat + "]" , e );
212205 }
213206
214207 return new DateProcessor (
215- processorTag ,
208+ tag ,
216209 description ,
217210 compiledTimezoneTemplate ,
218211 compiledLocaleTemplate ,
0 commit comments