Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/common/notices/Notices.java


1   /*
2    * Notices.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.common.notices;
11  
12  import java.util.Iterator;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  
16  import com.aendvari.common.osm.Osm;
17  import com.aendvari.common.osm.OsmNode;
18  import com.aendvari.common.osm.SimpleOsmPath;
19  import com.aendvari.common.osm.QueryOsmPath;
20  
21  
22  /**
23   * <p>Provides a container for a hierarchy of notices. </p>
24   *
25   * <p>Uses an {@link Osm} to hold {@link Notice} objects based on their key.</p>
26   *
27   * @author Scott Milne
28   *
29   */
30  
31  public class Notices
32  {
33    /* Variables */
34  
35    /** The OSM for holding all notices. */
36    protected Osm notices;
37  
38  
39    /* Constructors */
40  
41  
42    /**
43     * Constructs a <code>Notices</code> instance.
44     *
45     */
46  
47    public Notices()
48    {
49      // create notices hierarchy
50      notices = new Osm();
51    }
52  
53  
54    /* Notices */
55  
56  
57    /**
58     * Add a {@link Notice} object with the specified name at the given path.
59     *
60     * @param    path            The string OSM path for the parent OSM to add this notice to.
61     * @param    name            The name of the notice.
62     * @param    notice            The {@link Notice} object to add.
63     *
64     */
65  
66    public void addNotice(String path, String name, Notice notice)
67    {
68      // get the node for the given path
69      OsmNode parentNode = SimpleOsmPath.getNode(notices, path, true);
70  
71      // create a new node for the notice
72      OsmNode noticeNode = notices.createNode(name, notice);
73  
74      // get the object value
75      ArrayList list = (ArrayList)noticeNode.getNodeValue(ArrayList.class);
76  
77      // set the value of the node
78      list.add( notice );
79  
80      // set the value in case it was created
81      noticeNode.setNodeValue( list );
82  
83      // add the notice node to the parent
84      parentNode.appendChild(noticeNode);
85    }
86  
87    /**
88     * Add a {@link Notice} object at the given path.
89     *
90     * @param    path            The string OSM path for the notice to be placed.
91     * @param    notice            The {@link Notice} object to add.
92     *
93     */
94  
95    public void addNotice(String path, Notice notice)
96    {
97      // get the node for the given path
98      OsmNode noticeNode = SimpleOsmPath.getNode(notices, path, true);
99  
100     // get the object value
101     ArrayList list = (ArrayList)noticeNode.getNodeValue(ArrayList.class);
102 
103     // set the value of the node
104     list.add( notice );
105 
106     // set the value in case it was created
107     noticeNode.setNodeValue( list );
108   }
109 
110   /**
111    * Removes all {@link Notice} objects found at the given path.
112    *
113    * @param    path            The string OSM path to the notice.
114    * @param    recursive          Should recursion be used for child nodes.
115    *
116    */
117 
118   public void removeNotices(String path, boolean recursive)
119   {
120     // get the nodes for the notices
121     Collection noticesNodes = QueryOsmPath.selectNodes(notices, path);
122 
123     removeNotices(noticesNodes, recursive);
124   }
125 
126   /**
127    * Removes all {@link Notice} objects found at the given path. Non-recursive.
128    *
129    * @param    path            The string OSM path to the notice.
130    *
131    */
132 
133   public void removeNotices(String path)
134   {
135     removeNotices(path, false);
136   }
137 
138   /**
139    * Removes all {@link Notice} objects found at the given path.
140    *
141    * @param    noticesNodes        A collection of {@link OsmNode} objects to search.
142    * @param    recursive          Should recursion be used for child nodes.
143    *
144    */
145 
146   private void removeNotices(Collection noticesNodes, boolean recursive)
147   {
148     // visit Notice objects from all nodes
149     Iterator nodeIterator = noticesNodes.iterator();
150 
151     while (nodeIterator.hasNext())
152     {
153       OsmNode noticesNode = (OsmNode)nodeIterator.next();
154 
155       // only remove from the list if it's valid
156       if (noticesNode.getNodeValue() != null)
157       {
158         // remove all notices from the list
159         ((Collection)noticesNode).clear();
160       }
161 
162       // if recursion is wanted, do it now
163       if (recursive)
164       {
165         // collection children node of this node
166         OsmNode node = noticesNode.getFirstChild();
167         ArrayList children = new ArrayList();
168 
169         while (node != null)
170         {
171           children.add(node);
172 
173           // move onto the next child
174           node = node.getNextSibling();
175         }
176 
177         // visit children
178         removeNotices(children, recursive);
179       }
180     }
181   }
182 
183   /**
184    * Retrieve a Collection of {@link Notice} objects found at the given path.
185    *
186    * @param    path            The string OSM path to the notice.
187    * @param    recursive          Should recusion be used for child nodes.
188    *
189    */
190 
191   public Collection getNotices(String path, boolean recursive)
192   {
193     ArrayList noticeObjects = new ArrayList();
194 
195     // get the nodes for the notices
196     Collection noticesNodes = QueryOsmPath.selectNodes(notices, path);
197 
198     // get the collection for the notices
199     getNotices(noticesNodes, noticeObjects, recursive);
200 
201     return noticeObjects;
202   }
203 
204   /**
205    * Retrieve a Collection of {@link Notice} objects found at the given path.
206    * Non-recursive.
207    *
208    * @param    path            The string OSM path to the notice.
209    *
210    */
211 
212   public Collection getNotices(String path)
213   {
214     // get the collection for the notices
215     return getNotices(path, false);
216   }
217 
218   /**
219    * Retrieve a Collection of {@link Notice} objects found at the given path.
220    *
221    * @param    noticesNodes        A collection of {@link OsmNode} objects to search.
222    * @param    noticeObjects        The collection of {@link Notice} instances to add to.
223    * @param    recursive          Should recursion be used for child nodes.
224    *
225    */
226 
227   private void getNotices(Collection noticesNodes, Collection noticeObjects, boolean recursive)
228   {
229     // collect Notice objects from all nodes
230     Iterator nodeIterator = noticesNodes.iterator();
231 
232     while (nodeIterator.hasNext())
233     {
234       OsmNode noticesNode = (OsmNode)nodeIterator.next();
235 
236       // only add the list if it's valid
237       if (noticesNode.getNodeValue() != null)
238       {
239         // add the notices list at this node to the collection
240         noticeObjects.addAll( (Collection)noticesNode.getNodeValue() );
241       }
242 
243       // if recursion is wanted, do it now
244       if (recursive)
245       {
246         // visit children
247         getNotices(noticesNode.getChildNodes(), noticeObjects, recursive);
248       }
249     }
250   }
251 }
252