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.options;
22 
23 import com.servingxml.util.ServingXmlException;
24 import com.servingxml.ioc.components.ConfigurationContext;
25 import com.servingxml.util.MessageFormatter;
26 import com.servingxml.util.ServingXmlMessages;
27 import com.servingxml.util.StringHelper;
28 import com.servingxml.expr.substitution.SubstitutionExpr;
29 
30 /**
31  * The <code>SubfieldDelimiterFactoryAssembler</code> implements an assembler for
32  * assembling <code>SubfieldDelimiter</code> objects.
33  *
34  *
35  * @author Daniel A. Parker (daniel.parker@servingxml.com)
36  */
37 
38 public class SubfieldDelimiterFactoryAssembler {
39 
40   private String value = null;
41   private SeparatorFactory separatorFactory = null;
42   private String escapedBy = "";
43 
setEscapeCharacter(String escapedBy)44   public void setEscapeCharacter(String escapedBy) {
45     this.escapedBy = escapedBy;
46   }
47 
setValue(String value)48   public void setValue(String value) {
49     this.value = value;
50   }
51 
injectComponent(SeparatorFactory separatorFactory)52   public void injectComponent(SeparatorFactory separatorFactory) {
53     this.separatorFactory = separatorFactory;
54   }
55 
assemble(ConfigurationContext context)56   public SubfieldDelimiterFactory assemble(ConfigurationContext context) {
57 
58     if (separatorFactory == null) {
59       if (value == null) {
60         String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ATTRIBUTE_REQUIRED,context.getElement().getTagName(),"value");
61         throw new ServingXmlException(message);
62       }
63       value = StringHelper.translateEscapeChars(value);
64       SubstitutionExpr valueExpr = SubstitutionExpr.parseString(context.getQnameContext(),value);
65       SubstitutionExpr escapedByExpr = SubstitutionExpr.parseString(context.getQnameContext(),escapedBy);
66       separatorFactory = new DefaultSeparatorFactory(valueExpr, escapedByExpr);
67     }
68 
69     SubfieldDelimiterFactory delimiterFactory = new SubfieldDelimiterFactory(separatorFactory);
70 
71     return delimiterFactory;
72   }
73 }
74