KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > impl > AttributeNode


1 package org.sapia.archie.impl;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Properties JavaDoc;
7
8 import org.sapia.archie.NamePart;
9 import org.sapia.archie.NodeFactory;
10 import org.sapia.archie.ProcessingException;
11
12 /**
13  * @author Yanick Duchesne
14  * <dl>
15  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
16  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
17  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
18  * </dl>
19  */

20 public class AttributeNode extends MultiValueNode{
21   
22   protected AttributeNode(Map JavaDoc children, Map JavaDoc values, NodeFactory fac) throws ProcessingException {
23     super(new AttributeNameParser(), children, values, fac);
24   }
25   
26   /**
27    * @see org.sapia.archie.impl.MultiValueNode#getValue(org.sapia.archie.NamePart)
28    */

29   public final Object JavaDoc getValue(NamePart name) {
30     List JavaDoc values = (List JavaDoc) _valueLists.get(name);
31
32     if (values == null) {
33       return null;
34     }
35     
36     // selecting matching offers
37
List JavaDoc matching = selectMatchingOffers(name, values);
38     
39     // if no match, return.
40
if(matching.size() == 0){
41       return null;
42     }
43     
44     // select an offer among the ones that match.
45
Offer selected = (Offer)onSelect(matching);
46     
47     // if the offer is not valid, remove it from internal list
48
// and call this method recursively.
49
if(!isValid(selected)){
50       Offer other;
51       for(int i = 0; i < values.size(); i++){
52         other = (Offer)values.get(i);
53         if(other.getId().equals(selected.getId())){
54           values.remove(i);
55           break;
56         }
57       }
58       return getValue(name);
59     }
60     
61     // else, return the object within the offer.
62
Object JavaDoc toReturn = null;
63     if(selected != null){
64       toReturn = onRead(name, selected.getObject());
65       selected.select();
66     }
67     return toReturn;
68   }
69   
70   /**
71    * @see org.sapia.archie.Node#putValue(NamePart, Object, boolean)
72    */

73   public final boolean putValue(NamePart name, Object JavaDoc value, boolean overwrite) {
74     List JavaDoc values = (List JavaDoc) _valueLists.get(name);
75
76     if (values == null) {
77       values = new ArrayList JavaDoc();
78       _valueLists.put(name, values);
79     }
80
81     if (!overwrite && (values.size() != 0)) {
82       return false;
83     }
84
85     Object JavaDoc toBind = onWrite(name, value);
86     if(toBind != null){
87       if(name instanceof AttributeNamePart){
88         values.add(new Offer(((AttributeNamePart)name).getAttributes(), toBind));
89       }
90       else{
91         values.add(new Offer(new Properties JavaDoc(), toBind));
92       }
93     }
94     return true;
95   }
96   
97   /**
98    * @see org.sapia.archie.Node#removeValue(org.sapia.archie.NamePart)
99    */

100   public final Object JavaDoc removeValue(NamePart name) {
101     Object JavaDoc val = getValue(name);
102     List JavaDoc values = (List JavaDoc) _valueLists.get(name);
103     if (values == null) {
104       return val;
105     }
106     // selecting matching offers
107
List JavaDoc matching = selectMatchingOffers(name, values);
108     
109     // if no match, return.
110
if(matching.size() == 0){
111       return val;
112     }
113     for(int i = 0; i < matching.size(); i++){
114       removeOffer((Offer)matching.get(i), values);
115     }
116     if(values.size() == 0){
117       _valueLists.remove(name);
118     }
119     return val;
120   }
121   
122   /**
123    * Cannot be overridden.
124    *
125    * @see #onSelectOffer(List)
126    */

127   protected final Object JavaDoc onSelect(List JavaDoc offers){
128     return onSelectOffer(offers);
129   }
130
131   /**
132    * Can be overridden to provide custom selection algorithm.
133    *
134    * @param offers the <code>List</code> of <code>Offer</code>s to select from.
135    * @return an <code>Offer</code>
136    */

137   protected Offer onSelectOffer(List JavaDoc offers){
138     return SelectionHelper.selectLeastUsed(offers);
139   }
140   
141   /**
142    * This method is called after the <code>onSelectOffer()</code> method
143    * is called. It can be overriden to provide custom validation behavior.
144    *
145    * @param offer the <code>Offer</code> to validate.
146    * @return <code>true</code> if the offer is valid.
147    *
148    * @see #onSelectOffer(List)
149    */

150   protected boolean isValid(Offer offer){
151     return true;
152   }
153   
154   private void removeOffer(Offer offer , List JavaDoc offers){
155     for(int i = 0; i < offers.size(); i++){
156       if(((Offer)offers.get(i)).getId().equals(offer.getId())){
157         offers.remove(i);
158         i--;
159       }
160     }
161   }
162   
163   static List JavaDoc selectMatchingOffers(NamePart name, List JavaDoc offers){
164     List JavaDoc matching = new ArrayList JavaDoc();
165     Offer offer;
166     for(int i = 0; i < offers.size(); i++){
167       offer = (Offer)offers.get(i);
168       if(name instanceof AttributeNamePart){
169         if(offer.matches(((AttributeNamePart)name).getAttributes())){
170           matching.add(offer);
171         }
172       }
173       else{
174         matching.add(offer);
175       }
176     }
177     return matching;
178   }
179   
180   
181   
182 }
183
Popular Tags