View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
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   */
17  package com.sun.syndication.feed.synd;
18  
19  import com.sun.syndication.common.ObjectBean;
20  import com.sun.syndication.feed.module.DCSubjectImpl;
21  import com.sun.syndication.feed.module.DCSubject;
22  
23  import java.util.AbstractList;
24  import java.util.List;
25  import java.util.ArrayList;
26  
27  /***
28   * Bean for categories of SyndFeedImpl feeds and entries.
29   * <p>
30   * @author Alejandro Abdelnur
31   *
32   */
33  public class SyndCategoryImpl extends ObjectBean implements SyndCategory {
34      private DCSubject _subject;
35  
36      /***
37       * For implementations extending SyndContentImpl to be able to use the ObjectBean functionality
38       * with extended interfaces.
39       * <p>
40       * @param beanClass
41       * @param subject the DC subject to wrap.
42       */
43      protected SyndCategoryImpl(Class beanClass,DCSubject subject) {
44          super(beanClass);
45          _subject = subject;
46      }
47  
48  
49      /***
50       * Package private constructor, used by SyndCategoryListFacade.
51       * <p>
52       * @param subject the DC subject to wrap.
53       *
54       */
55      SyndCategoryImpl(DCSubject subject) {
56          this(SyndCategory.class,subject);
57      }
58  
59      /***
60       * Package private constructor, used by SyndCategoryListFacade.
61       * <p>
62       * @return the DC subject being wrapped.
63       *
64       */
65      DCSubject getSubject() {
66          return _subject;
67      }
68  
69      /***
70       * Default constructor. All properties are set to <b>null</b>.
71       * <p>
72       *
73       */
74      public SyndCategoryImpl() {
75          this(new DCSubjectImpl());
76      }
77  
78      /***
79       * Returns the category name.
80       * <p>
81       * @return the category name, <b>null</b> if none.
82       *
83       */
84      public String getName() {
85          return _subject.getValue();
86      }
87  
88      /***
89       * Sets the category name.
90       * <p>
91       * @param name the category name to set, <b>null</b> if none.
92       *
93       */
94      public void setName(String name) {
95          _subject.setValue(name);
96      }
97  
98      /***
99       * Returns the category taxonomy URI.
100      * <p>
101      * @return the category taxonomy URI, <b>null</b> if none.
102      *
103      */
104     public String getTaxonomyUri() {
105         return _subject.getTaxonomyUri();
106     }
107 
108     /***
109      * Sets the category taxonomy URI.
110      * <p>
111      * @param taxonomyUri the category taxonomy URI to set, <b>null</b> if none.
112      *
113      */
114     public void setTaxonomyUri(String taxonomyUri) {
115         _subject.setTaxonomyUri(taxonomyUri);
116     }
117 
118 }
119 
120 
121 /***
122  * List implementation for SyndCategoryImpl elements. To be directly used by the SyndFeedImpl
123  * and SyndEntryImpl classes only.
124  * <p>
125  * It acts as a facade on top of the DCSubjectImpl elements of the underlying list
126  * and remains in synch with it. It is possible to work on either list, the categories
127  * one or the subjects one and they remain in synch.
128  * <p>
129  * This is necessary because the SyndFeedImpl categories are just a convenience to access
130  * the DublinCore subjects.
131  * <P>
132  * All this mess to avoid making DCSubjectImpl implement SyndCategory (which it would be odd).
133  * <p>
134  * @author Alejandro Abdelnur
135  *
136  */
137 class SyndCategoryListFacade extends AbstractList {
138     private List _subjects;
139 
140     /***
141      * Default constructor. Creates and empty list.
142      */
143     public SyndCategoryListFacade() {
144         this(new ArrayList());
145     }
146 
147     /***
148      * Creates a facade list of categories on top the given subject list.
149      * <P>
150      * @param subjects the list of subjects to create the facade.
151      *
152      */
153     public SyndCategoryListFacade(List subjects) {
154         _subjects = subjects;
155     }
156 
157     /***
158      * Gets the category by index.
159      * <p>
160      * @param index the index position to retrieve the category.
161      * @return the SyndCategoryImpl in position index, <b>null</b> if none.
162      *
163      */
164     public Object get(int index) {
165         return new SyndCategoryImpl((DCSubject) _subjects.get(index));
166     }
167 
168     /***
169      * Returns the size of the list.
170      * <p>
171      * @return the size of the list.
172      *
173      */
174     public int size() {
175         return _subjects.size();
176     }
177 
178     /***
179      * Sets a category in an existing position in the list.
180      * <p>
181      * @param index position to set the category.
182      * @param obj the SyndCategoryImpl object to set.
183      * @return the SyndCategoryImpl object that is being replaced, <b>null</b> if none.
184      *
185      */
186     public Object set(int index,Object obj) {
187         SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
188         DCSubject subject = (sCat!=null) ? sCat.getSubject() : null;
189         subject = (DCSubject) _subjects.set(index,subject);
190         return (subject!=null) ? new SyndCategoryImpl(subject) : null;
191     }
192 
193     /***
194     * Adds a category to the list.
195     * <p>
196     * @param index position to add the category.
197     * @param obj the SyndCategoryImpl object to add.
198      *
199      */
200     public void add(int index,Object obj) {
201         SyndCategoryImpl sCat = (SyndCategoryImpl) obj;
202         DCSubject subject = (sCat!=null) ? sCat.getSubject() : null;
203         _subjects.add(index,subject);
204     }
205 
206     /***
207      * Removes a category element from a specific position.
208      * <p>
209      * @param index position to remove the category from.
210      * @return the SyndCategoryImpl being removed from position index, <b>null</b> if none.
211      *
212      */
213     public Object remove(int index) {
214         DCSubject subject = (DCSubject) _subjects.remove(index);
215         return (subject!=null) ? new SyndCategoryImpl(subject) : null;
216     }
217 
218     /***
219      * Returns a list with the DCSubject elements of the SyndCategoryImpl list facade.
220      * To be used by the SyndFeedImpl class only.
221      * <p>
222      * @param cList the list with SyndCategoryImpl elements to convert to subject list.
223      * @return a list with DCSubject elements corresponding to the categories in the given list.
224      *
225      */
226     public static List convertElementsSyndCategoryToSubject(List cList) {
227         List sList = null;
228         if (cList!=null) {
229             sList = new ArrayList();
230             for (int i=0;i<cList.size();i++) {
231                 SyndCategoryImpl sCat = (SyndCategoryImpl) cList.get(i);
232                 DCSubject subject = null;
233                 if (sCat!=null) {
234                     subject = sCat.getSubject();
235                 }
236                 sList.add(subject);
237             }
238         }
239         return sList;
240     }
241 
242 }