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.recordmapping; 22 23 import java.util.ArrayList; 24 import java.util.StringTokenizer; 25 import java.util.Comparator; 26 27 import com.servingxml.util.ServingXmlException; 28 import com.servingxml.ioc.components.ConfigurationContext; 29 import com.servingxml.expr.substitution.SubstitutionParser; 30 import com.servingxml.expr.substitution.SubstitutionExpr; 31 import com.servingxml.expr.ExpressionException; 32 import com.servingxml.app.ParameterDescriptor; 33 import com.servingxml.util.MessageFormatter; 34 import com.servingxml.util.ServingXmlMessages; 35 import com.servingxml.util.Name; 36 import com.servingxml.components.xsltconfig.XsltConfiguration; 37 import com.servingxml.app.Environment; 38 39 /** 40 * The <code>GroupByFactoryAssembler</code> implements an assembler for 41 * assembling <code>GroupByFactory</code> objects. 42 * 43 * 44 * @author Daniel A. Parker (daniel.parker@servingxml.com) 45 */ 46 47 public class GroupByFactoryAssembler { 48 private MapXmlFactory[] childFactories = new MapXmlFactory[0]; 49 private String fieldQnames = ""; 50 private ParameterDescriptor[] parameterDescriptors = ParameterDescriptor.EMPTY_ARRAY; 51 private XsltConfiguration xsltConfiguration; 52 private Name recordTypeName = Name.EMPTY; 53 private Sort[] sorts = new Sort[0]; 54 injectComponent(XsltConfiguration xsltConfiguration)55 public void injectComponent(XsltConfiguration xsltConfiguration) { 56 this.xsltConfiguration = xsltConfiguration; 57 } 58 injectComponent(ParameterDescriptor[] parameterDescriptors)59 public void injectComponent(ParameterDescriptor[] parameterDescriptors) { 60 61 this.parameterDescriptors = parameterDescriptors; 62 } 63 injectComponent(Sort[] sorts)64 public void injectComponent(Sort[] sorts) { 65 66 this.sorts = sorts; 67 } 68 setRecordType(Name recordType)69 public void setRecordType(Name recordType) { 70 this.recordTypeName = recordType; 71 } 72 setFields(String fieldQnames)73 public void setFields(String fieldQnames) { 74 this.fieldQnames = fieldQnames; 75 } 76 injectComponent(MapXmlFactory[] childFactories)77 public void injectComponent(MapXmlFactory[] childFactories) { 78 this.childFactories = childFactories; 79 } 80 assemble(ConfigurationContext context)81 public MapXmlFactory assemble(ConfigurationContext context) { 82 83 if (xsltConfiguration == null) { 84 xsltConfiguration = XsltConfiguration.getDefault(); 85 } 86 87 try { 88 89 if (childFactories.length == 0) { 90 String msg = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_ELEMENT_CHOICE_REQUIRED, 91 context.getElement().getTagName(), 92 "literal, sx:fieldElementMap, sx:group, sx:onRecord"); 93 throw new ServingXmlException(msg); 94 } 95 96 StringTokenizer fieldNameTokenizer = new StringTokenizer(fieldQnames," ,"); 97 ArrayList<SubstitutionExpr> fieldNameList = new ArrayList<SubstitutionExpr>(); 98 while (fieldNameTokenizer.hasMoreTokens()) { 99 String fieldQname = fieldNameTokenizer.nextToken(); 100 SubstitutionParser parser = new SubstitutionParser(context.getQnameContext(),fieldQname.trim()); 101 SubstitutionExpr expr = parser.parse(); 102 fieldNameList.add(expr); 103 } 104 SubstitutionExpr[] fieldNames = new SubstitutionExpr[fieldNameList.size()]; 105 fieldNames = fieldNameList.toArray(fieldNames); 106 107 MapXmlFactory rmf = new MultipleMapXmlFactory(context.getQnameContext(), xsltConfiguration, childFactories); 108 109 if (sorts.length > 0) { 110 Comparator comparator = new SortComparator(sorts); 111 rmf = new SortGroupFactory(rmf,comparator); 112 } 113 114 Environment env = new Environment(parameterDescriptors,context.getQnameContext()); 115 MapXmlFactory recordMapFactory = new GroupByFactory(env, recordTypeName, fieldNames, rmf); 116 117 return recordMapFactory; 118 } catch (ExpressionException e) { 119 String msg = MessageFormatter.getInstance().getMessage(ServingXmlMessages.COMPONENT_SUB_EXPR_PARSE_FAILED, 120 context.getElement().getTagName()); 121 throw new ServingXmlException(msg + " " + e.getMessage()); 122 } 123 } 124 } 125