View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.util;
17  
18  import java.util.HashMap;
19  import java.util.HashSet;
20  import java.util.Iterator;
21  
22  import org.apache.struts.action.ActionErrors;
23  import org.apache.struts.action.ActionMessage;
24  import org.apache.struts.action.ActionMessages;
25  
26  /***
27   * 同じメッセージが複数登録されるのを防ぐActionErrors
28   * 
29   * @author hatanaka
30   */
31  public class ActionErrorsSet extends ActionErrors {
32  	private static final long serialVersionUID = 1L;
33  
34  	private HashMap errorsMap=new HashMap();
35  
36  	public void add(String key, ActionMessage message) {
37  		if(!errorsMap.containsKey(key)){
38  			errorsMap.put(key, new HashSet());
39  		}
40  		HashSet errorsSet=(HashSet)errorsMap.get(key);
41  		if(!errorsSet.contains(message.toString())){
42  			errorsSet.add(message.toString());
43  			super.add(key, message);
44  		}
45  	}
46  
47  	public void add(ActionMessages messages) {
48  		for(Iterator props=messages.properties(); props.hasNext();){
49  			String key=(String)props.next();
50  			for(Iterator msgs=messages.get(key); msgs.hasNext();){
51  				ActionMessage message=(ActionMessage)msgs.next();
52  				add(key, message);
53  			}
54  		}
55  	}
56  }