@@ -94,17 +94,9 @@ public final class DateProcessor extends AbstractProcessor {
9494 formatter = DateFormatter .forPattern (this .outputFormat );
9595 }
9696
97- private static ZoneId newDateTimeZone (String timezone ) {
98- return timezone == null ? ZoneOffset .UTC : ZoneId .of (timezone );
99- }
100-
101- private static Locale newLocale (String locale ) {
102- return locale == null ? Locale .ENGLISH : LocaleUtils .parse (locale );
103- }
104-
10597 @ Override
106- public IngestDocument execute (IngestDocument ingestDocument ) {
107- Object obj = ingestDocument .getFieldValue (field , Object .class );
98+ public IngestDocument execute (IngestDocument document ) {
99+ Object obj = document .getFieldValue (field , Object .class );
108100 String value = null ;
109101 if (obj != null ) {
110102 // Don't use Objects.toString(...) here, because null gets changed to "null" which may confuse some date parsers
@@ -115,10 +107,9 @@ public IngestDocument execute(IngestDocument ingestDocument) {
115107 // extract the timezone and locale to use for date parsing
116108 final ZoneId documentTimezone ;
117109 final Locale documentLocale ;
118- final Map <String , Object > sourceAndMetadata = ingestDocument .getSourceAndMetadata ();
119110 try {
120- documentTimezone = newDateTimeZone ( timezone == null ? null : timezone . newInstance ( sourceAndMetadata ). execute () );
121- documentLocale = newLocale ( locale == null ? null : locale . newInstance ( sourceAndMetadata ). execute () );
111+ documentTimezone = getTimezone ( document );
112+ documentLocale = getLocale ( document );
122113 } catch (Exception e ) {
123114 throw new IllegalArgumentException ("unable to parse date [" + value + "]" , e );
124115 }
@@ -139,21 +130,33 @@ public IngestDocument execute(IngestDocument ingestDocument) {
139130 throw new IllegalArgumentException ("unable to parse date [" + value + "]" , lastException );
140131 }
141132
142- ingestDocument .setFieldValue (targetField , formatter .format (dateTime ));
143- return ingestDocument ;
133+ document .setFieldValue (targetField , formatter .format (dateTime ));
134+ return document ;
144135 }
145136
146137 @ Override
147138 public String getType () {
148139 return TYPE ;
149140 }
150141
151- TemplateScript .Factory getTimezone () {
152- return timezone ;
142+ // visible for testing
143+ ZoneId getTimezone (IngestDocument document ) {
144+ String value = timezone == null ? null : document .renderTemplate (timezone );
145+ if (value == null ) {
146+ return ZoneOffset .UTC ;
147+ } else {
148+ return ZoneId .of (value );
149+ }
153150 }
154151
155- TemplateScript .Factory getLocale () {
156- return locale ;
152+ // visible for testing
153+ Locale getLocale (IngestDocument document ) {
154+ String value = locale == null ? null : document .renderTemplate (locale );
155+ if (value == null ) {
156+ return Locale .ENGLISH ;
157+ } else {
158+ return LocaleUtils .parse (value );
159+ }
157160 }
158161
159162 String getField () {
@@ -182,39 +185,33 @@ public Factory(ScriptService scriptService) {
182185
183186 public DateProcessor create (
184187 Map <String , Processor .Factory > registry ,
185- String processorTag ,
188+ String tag ,
186189 String description ,
187190 Map <String , Object > config ,
188191 ProjectId projectId
189192 ) throws Exception {
190- String field = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "field" );
191- String targetField = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "target_field" , DEFAULT_TARGET_FIELD );
192- String timezoneString = ConfigurationUtils .readOptionalStringProperty (TYPE , processorTag , config , "timezone" );
193+ String field = ConfigurationUtils .readStringProperty (TYPE , tag , config , "field" );
194+ String targetField = ConfigurationUtils .readStringProperty (TYPE , tag , config , "target_field" , DEFAULT_TARGET_FIELD );
195+ String timezoneString = ConfigurationUtils .readOptionalStringProperty (TYPE , tag , config , "timezone" );
193196 TemplateScript .Factory compiledTimezoneTemplate = null ;
194197 if (timezoneString != null ) {
195- compiledTimezoneTemplate = ConfigurationUtils .compileTemplate (
196- TYPE ,
197- processorTag ,
198- "timezone" ,
199- timezoneString ,
200- scriptService
201- );
198+ compiledTimezoneTemplate = ConfigurationUtils .compileTemplate (TYPE , tag , "timezone" , timezoneString , scriptService );
202199 }
203- String localeString = ConfigurationUtils .readOptionalStringProperty (TYPE , processorTag , config , "locale" );
200+ String localeString = ConfigurationUtils .readOptionalStringProperty (TYPE , tag , config , "locale" );
204201 TemplateScript .Factory compiledLocaleTemplate = null ;
205202 if (localeString != null ) {
206- compiledLocaleTemplate = ConfigurationUtils .compileTemplate (TYPE , processorTag , "locale" , localeString , scriptService );
203+ compiledLocaleTemplate = ConfigurationUtils .compileTemplate (TYPE , tag , "locale" , localeString , scriptService );
207204 }
208- List <String > formats = ConfigurationUtils .readList (TYPE , processorTag , config , "formats" );
209- String outputFormat = ConfigurationUtils .readStringProperty (TYPE , processorTag , config , "output_format" , DEFAULT_OUTPUT_FORMAT );
205+ List <String > formats = ConfigurationUtils .readList (TYPE , tag , config , "formats" );
206+ String outputFormat = ConfigurationUtils .readStringProperty (TYPE , tag , config , "output_format" , DEFAULT_OUTPUT_FORMAT );
210207 try {
211208 DateFormatter .forPattern (outputFormat );
212209 } catch (Exception e ) {
213210 throw new IllegalArgumentException ("invalid output format [" + outputFormat + "]" , e );
214211 }
215212
216213 return new DateProcessor (
217- processorTag ,
214+ tag ,
218215 description ,
219216 compiledTimezoneTemplate ,
220217 compiledLocaleTemplate ,
0 commit comments