KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > util > AttributesFieldHandler


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.util;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.cocoon.portal.coplet.CopletData;
23 import org.apache.cocoon.portal.coplet.CopletInstanceData;
24 import org.exolab.castor.mapping.MapItem;
25
26 /**
27  * Field handler for attributes of a CopletData object.
28  *
29  * FIXME This is a little bit hacky and should be changed by using
30  * reflection
31  *
32  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
33  *
34  * @version CVS $Id: AttributesFieldHandler.java 123407 2004-12-27 13:51:59Z cziegeler $
35  */

36 public class AttributesFieldHandler extends AbstractFieldHandler {
37
38     protected Map JavaDoc getAttributes(Object JavaDoc object) {
39         if (object instanceof CopletData) {
40             return ((CopletData) object).getAttributes();
41         }
42         return ((CopletInstanceData) object).getAttributes();
43     }
44
45     public Object JavaDoc getValue(Object JavaDoc object) {
46         HashMap JavaDoc map = new HashMap JavaDoc();
47         Iterator JavaDoc iterator = this.getAttributes(object).entrySet().iterator();
48         Map.Entry JavaDoc entry;
49         Object JavaDoc key;
50         while (iterator.hasNext()) {
51             entry = (Map.Entry JavaDoc) iterator.next();
52             key = entry.getKey();
53             map.put(key, new MapItem(key, entry.getValue()));
54         }
55         return map;
56     }
57
58     public Object JavaDoc newInstance(Object JavaDoc parent) {
59         return new MapItem();
60     }
61
62     public void resetValue(Object JavaDoc object) {
63         this.getAttributes(object).clear();
64     }
65
66     public void setValue(Object JavaDoc object, Object JavaDoc value) {
67         MapItem item = (MapItem) value;
68         this.getAttributes(object).put(item.getKey(), item.getValue());
69     }
70 }
71
Popular Tags