1 /** 2 * ServingXML 3 * 4 * Copyright (C) 2006 Daniel Parker 5 * daniel.parker@servingxml.com 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 **/ 20 21 package com.servingxml.components.flatfile.recordtype; 22 23 import com.servingxml.app.ServiceContext; 24 import com.servingxml.util.Formatter; 25 import com.servingxml.util.Name; 26 import com.servingxml.util.record.Record; 27 import com.servingxml.app.Flow; 28 import com.servingxml.components.parameter.DefaultValue; 29 import com.servingxml.components.quotesymbol.QuoteSymbol; 30 import com.servingxml.components.flatfile.options.FlatFileOptions; 31 import com.servingxml.components.flatfile.options.Delimiter; 32 import com.servingxml.components.flatfile.RecordOutput; 33 34 public class DelimitedNamedFieldWriter implements FlatRecordFieldWriter { 35 private final int start; 36 private final Formatter fieldFormatter; 37 private final DefaultValue defaultValueEvaluator; 38 private final FlatFileOptions flatFileOptions; 39 private final Delimiter fieldDelimiter; 40 private final StringBuilder buf; 41 DelimitedNamedFieldWriter(int start, Formatter fieldFormatter, DefaultValue defaultValueEvaluator, FlatFileOptions flatFileOptions)42 public DelimitedNamedFieldWriter(int start, Formatter fieldFormatter, 43 DefaultValue defaultValueEvaluator, FlatFileOptions flatFileOptions) { 44 this.start = start; 45 this.fieldFormatter = fieldFormatter; 46 this.defaultValueEvaluator = defaultValueEvaluator; 47 this.flatFileOptions = flatFileOptions; 48 this.buf = new StringBuilder(); 49 Delimiter[] fieldDelimiters = flatFileOptions.getFieldDelimiters(); 50 this.fieldDelimiter = fieldDelimiters.length == 0 ? Delimiter.NULL : fieldDelimiters[0]; 51 } 52 writeField(ServiceContext context, Flow flow, RecordOutput recordOutput)53 public void writeField(ServiceContext context, Flow flow, RecordOutput recordOutput) { 54 writeField(context, flow, Name.EMPTY, recordOutput); 55 } 56 writeField(ServiceContext context, Flow flow, Name fieldName, RecordOutput recordOutput)57 public void writeField(ServiceContext context, Flow flow, Name fieldName, RecordOutput recordOutput) { 58 int offset = flatFileOptions.rebaseIndex(start); 59 if (offset >= 0) { 60 recordOutput.setPosition(offset); 61 } 62 63 Record record = flow.getRecord(); 64 65 buf.setLength(0); 66 67 String v = record.getString(fieldName); 68 if (v == null) { 69 v = defaultValueEvaluator.evaluateString(context, flow); 70 } 71 String value = fieldFormatter.format(v); 72 73 final QuoteSymbol quoteSymbol = flatFileOptions.getQuoteSymbol(); 74 75 boolean insertQuotes = flatFileOptions.useQuotes(value); 76 if (insertQuotes) { 77 buf.append(quoteSymbol.getCharacter()); 78 quoteSymbol.escape(value, buf); 79 buf.append(quoteSymbol.getCharacter()); 80 } else { 81 buf.append(value); 82 } 83 recordOutput.writeString(buf.toString()); 84 } 85 writeEndDelimiterTo(RecordOutput recordOutput)86 public void writeEndDelimiterTo(RecordOutput recordOutput) { 87 fieldDelimiter.writeEndDelimiterTo(recordOutput); 88 } 89 } 90