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("contentId", form.getContentId()); 059 060 XMLUtils.startElement(contentHandler, "form-errors", atts); 061 062 for (Map.Entry<String, List<I18nizableText>> fieldErrors : errorMap.entrySet()) 063 { 064 String fieldId = fieldErrors.getKey(); 065 Field field = fields.get(fieldId); 066 List<I18nizableText> messages = fieldErrors.getValue(); 067 068 atts.clear(); 069 atts.addCDATAAttribute("id", fieldId); 070 atts.addCDATAAttribute("name", field.getName()); 071 atts.addCDATAAttribute("label", field.getLabel()); 072 atts.addCDATAAttribute("type", field.getType().toString()); 073 074 XMLUtils.startElement(contentHandler, "field", atts); 075 076 for (I18nizableText message : messages) 077 { 078 message.toSAX(contentHandler, "error"); 079 } 080 081 XMLUtils.endElement(contentHandler, "field"); 082 } 083 084 XMLUtils.endElement(contentHandler, "form-errors"); 085 086 contentHandler.endDocument(); 087 } 088 089}