001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.forms.processing; 017 018import java.io.IOException; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.cocoon.ProcessingException; 023import org.apache.cocoon.environment.ObjectModelHelper; 024import org.apache.cocoon.environment.Request; 025import org.apache.cocoon.generation.AbstractGenerator; 026import org.apache.cocoon.xml.AttributesImpl; 027import org.apache.cocoon.xml.XMLUtils; 028import org.xml.sax.SAXException; 029 030import org.ametys.plugins.forms.Field; 031import org.ametys.plugins.forms.Form; 032import org.ametys.runtime.i18n.I18nizableText; 033 034/** 035 * Action that processes the user submitted data on a form. 036 */ 037public class FormErrorsGenerator extends AbstractGenerator 038{ 039 040 @Override 041 public void generate() throws IOException, SAXException, ProcessingException 042 { 043 Request request = ObjectModelHelper.getRequest(objectModel); 044 045 FormErrors errors = (FormErrors) request.getAttribute("form-errors"); 046 Form form = errors.getForm(); 047 048 Map<String, Field> fields = form.getFieldMap(); 049 050 Map<String, List<I18nizableText>> errorMap = errors.getErrors(); 051 052 contentHandler.startDocument(); 053 054 AttributesImpl atts = new AttributesImpl(); 055 atts.addCDATAAttribute("id", form.getId()); 056 atts.addCDATAAttribute("label", form.getLabel()); 057 atts.addCDATAAttribute("insertionFailed", String.valueOf(errors.hasInsertionFailed())); 058 atts.addCDATAAttribute("limitReached", String.valueOf(errors.hasLimitReached())); 059// atts.addCDATAAttribute("contentId", form.getContentId()); 060 061 XMLUtils.startElement(contentHandler, "form-errors", atts); 062 063 for (Map.Entry<String, List<I18nizableText>> fieldErrors : errorMap.entrySet()) 064 { 065 String fieldId = fieldErrors.getKey(); 066 Field field = fields.get(fieldId); 067 List<I18nizableText> messages = fieldErrors.getValue(); 068 069 atts.clear(); 070 atts.addCDATAAttribute("id", fieldId); 071 atts.addCDATAAttribute("name", field.getName()); 072 atts.addCDATAAttribute("label", field.getLabel()); 073 atts.addCDATAAttribute("type", field.getType().toString()); 074 075 XMLUtils.startElement(contentHandler, "field", atts); 076 077 for (I18nizableText message : messages) 078 { 079 message.toSAX(contentHandler, "error"); 080 } 081 082 XMLUtils.endElement(contentHandler, "field"); 083 } 084 085 XMLUtils.endElement(contentHandler, "form-errors"); 086 087 contentHandler.endDocument(); 088 } 089 090}