001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.ecm.webengine.forms; 018 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.Map; 022 023import org.nuxeo.ecm.webengine.forms.validation.Form; 024import org.nuxeo.ecm.webengine.forms.validation.FormManager; 025import org.nuxeo.ecm.webengine.forms.validation.ValidationException; 026 027/** 028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 029 */ 030public class SimpleFormDataProvider extends HashMap<String, String[]> implements FormDataProvider { 031 032 private static final long serialVersionUID = 1L; 033 034 public Collection<String> getKeys() { 035 return keySet(); 036 } 037 038 public String[] getList(String key) { 039 return get(key); 040 } 041 042 public String getString(String key) { 043 String[] v = get(key); 044 if (v != null && v.length > 0) { 045 return v[0]; 046 } 047 return null; 048 } 049 050 public Map<String, String[]> getFormFields() { 051 return this; 052 } 053 054 public void putString(String key, String value) { 055 put(key, new String[] { value }); 056 } 057 058 public void putList(String key, String... values) { 059 put(key, values); 060 } 061 062 public void putList(String key, Collection<String> values) { 063 if (values == null) { 064 return; 065 } 066 String[] ar = values.toArray(new String[values.size()]); 067 put(key, ar); 068 } 069 070 public <T extends Form> T validate(Class<T> type) throws ValidationException { 071 T proxy = FormManager.newProxy(type); 072 try { 073 proxy.load(this, proxy); 074 return proxy; 075 } catch (ValidationException e) { 076 e.setForm(proxy); 077 throw e; 078 } 079 } 080 081}