KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > repository > QNameNodeMap


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.bean.repository;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.service.namespace.NamespacePrefixResolver;
25 import org.alfresco.service.namespace.QNameMap;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 /**
30  * A extension of the repo QNameMap to provide custom property resolving support for Node wrappers.
31  *
32  * @author Kevin Roast
33  */

34 public final class QNameNodeMap<K,V> extends QNameMap implements Map JavaDoc, Cloneable JavaDoc
35 {
36    private Node parent = null;
37    private Map JavaDoc<String JavaDoc, NodePropertyResolver> resolvers = new HashMap JavaDoc<String JavaDoc, NodePropertyResolver>(16, 1.0f);
38    
39    /**
40     * Constructor
41     *
42     * @param parent Parent Node of the QNameNodeMap
43     */

44    public QNameNodeMap(NamespacePrefixResolver resolver, Node parent)
45    {
46       super(resolver);
47       if (parent == null)
48       {
49          throw new IllegalArgumentException JavaDoc("Parent Node cannot be null!");
50       }
51       this.parent = parent;
52    }
53    
54    /**
55     * Register a property resolver for the named property.
56     *
57     * @param name Name of the property this resolver is for
58     * @param resolver Property resolver to register
59     */

60    public void addPropertyResolver(String JavaDoc name, NodePropertyResolver resolver)
61    {
62       this.resolvers.put(name, resolver);
63    }
64    
65    /**
66     * Returns if a property resolver with a specific name has been applied to the map
67     *
68     * @param name of property resolver to look for
69     *
70     * @return true if a resolver with the name is found, false otherwise
71     */

72    public boolean containsPropertyResolver(String JavaDoc name)
73    {
74       return this.resolvers.containsKey(name);
75    }
76
77    /**
78     * @see java.util.Map#containsKey(java.lang.Object)
79     */

80    public boolean containsKey(Object JavaDoc key)
81    {
82       return (this.contents.containsKey(Repository.resolveToQNameString((String JavaDoc)key)) ||
83               this.resolvers.containsKey(key));
84    }
85
86    /**
87     * @see java.util.Map#get(java.lang.Object)
88     */

89    public Object JavaDoc get(Object JavaDoc key)
90    {
91       String JavaDoc qnameKey = Repository.resolveToQNameString(key.toString());
92       Object JavaDoc obj = this.contents.get(qnameKey);
93       if (obj == null)
94       {
95          // if a property resolver exists for this property name then invoke it
96
NodePropertyResolver resolver = this.resolvers.get(key.toString());
97          if (resolver != null)
98          {
99             obj = resolver.get(this.parent);
100             // cache the result
101
// obviously the cache is useless if the result is null, in most cases it shouldn't be
102
this.contents.put(qnameKey, obj);
103          }
104       }
105       
106       return obj;
107    }
108    
109    /**
110     * Perform a get without using property resolvers
111     *
112     * @param key item key
113     * @return object
114     */

115    public Object JavaDoc getRaw(Object JavaDoc key)
116    {
117       return this.contents.get(Repository.resolveToQNameString((String JavaDoc)key));
118    }
119    
120    /**
121     * Shallow copy the map by copying keys and values into a new QNameNodeMap
122     */

123    public Object JavaDoc clone()
124    {
125       QNameNodeMap map = new QNameNodeMap(this.resolver, this.parent);
126       map.putAll(this);
127       if (this.resolvers.size() != 0)
128       {
129          map.resolvers = (Map JavaDoc)((HashMap JavaDoc)this.resolvers).clone();
130       }
131       return map;
132    }
133 }
134
Popular Tags