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>RepeatDelimiterFactoryAssembler</code> implements an assembler for
32  * assembling <code>RepeatDelimiter</code> objects.
33  *
34  *
35  * @author Daniel A. Parker (daniel.parker@servingxml.com)
36  */
37 
38 public class RepeatDelimiterFactoryAssembler {
39 
40   private String start = "";
41   private String end = "";
42   private String value = null;
43   private SeparatorFactory separatorFactory = null;
44   private String escapedBy = "";
45 
setEscapeCharacter(String escapedBy)46   public void setEscapeCharacter(String escapedBy) {
47     this.escapedBy = escapedBy;
48   }
49 
setStart(String start)50   public void setStart(String start) {
51     this.start = start;
52   }
53 
setEnd(String end)54   public void setEnd(String end) {
55     this.end = end;
56   }
57 
setValue(String value)58   public void setValue(String value) {
59     this.value = value;
60   }
61 
injectComponent(SeparatorFactory separatorFactory)62   public void injectComponent(SeparatorFactory separatorFactory) {
63     this.separatorFactory = separatorFactory;
64   }
65 
assemble(ConfigurationContext context)66   public RepeatDelimiterFactory assemble(ConfigurationContext context) {
67 
68     if (separatorFactory == null) {
69       if (start.length() > 0) {
70         if (end.length() == 0) {
71           String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ATTRIBUTE_REQUIRED,context.getElement().getTagName(),"end");
72           throw new ServingXmlException(message);
73         }
74         start = StringHelper.translateEscapeChars(start);
75         end = StringHelper.translateEscapeChars(end);
76         SubstitutionExpr startExpr = SubstitutionExpr.parseString(context.getQnameContext(),start);
77         SubstitutionExpr endExpr = SubstitutionExpr.parseString(context.getQnameContext(),end);
78         separatorFactory = new StartEndSeparatorFactory(startExpr, endExpr);
79       } else {
80         if (value == null) {
81           String message = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ATTRIBUTE_REQUIRED,context.getElement().getTagName(),"value");
82           throw new ServingXmlException(message);
83         }
84         value = StringHelper.translateEscapeChars(value);
85         SubstitutionExpr valueExpr = SubstitutionExpr.parseString(context.getQnameContext(),value);
86         SubstitutionExpr escapedByExpr = SubstitutionExpr.parseString(context.getQnameContext(),escapedBy);
87         separatorFactory = new DefaultSeparatorFactory(valueExpr, escapedByExpr);
88       }
89     }
90 
91     RepeatDelimiterFactory delimiterFactory = new RepeatDelimiterFactory(separatorFactory);
92 
93     return delimiterFactory;
94   }
95 }
96