Clover Coverage Report - Pebble 2.5-SNAPSHOT
Coverage timestamp: Sat Jun 12 2010 09:39:29 EST
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
60   232   24   5,45
24   108   0,4   11
11     2,18  
1    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  CategoryBuilder       Line # 46 60 0% 24 5 94,7% 0.94736844
 
  (128)
 
1    /*
2    * Copyright (c) 2003-2006, Simon Brown
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * - Redistributions of source code must retain the above copyright
9    * notice, this list of conditions and the following disclaimer.
10    *
11    * - Redistributions in binary form must reproduce the above copyright
12    * notice, this list of conditions and the following disclaimer in
13    * the documentation and/or other materials provided with the
14    * distribution.
15    *
16    * - Neither the name of Pebble nor the names of its contributors may
17    * be used to endorse or promote products derived from this software
18    * without specific prior written permission.
19    *
20    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21    * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23    * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30    * POSSIBILITY OF SUCH DAMAGE.
31    */
32    package net.sourceforge.pebble.domain;
33   
34    import java.util.ArrayList;
35    import java.util.Collections;
36    import java.util.Iterator;
37    import java.util.List;
38   
39    import net.sourceforge.pebble.util.I18n;
40   
41    /**
42    * A class to manage blog categories.
43    *
44    * @author Simon Brown
45    */
 
46    public class CategoryBuilder {
47   
48    /** the category separator */
49    private static final String CATEGORY_SEPARATOR = "/";
50   
51    /** the owning blog */
52    private Blog blog;
53   
54    /** the root category */
55    private Category rootCategory;
56   
57    /**
58    * Creates a new instance.
59    */
 
60  1362 toggle public CategoryBuilder(Blog blog) {
61  1362 this.blog = blog;
62    }
63   
64    /**
65    * Creates a new instance.
66    */
 
67  418 toggle public CategoryBuilder(Blog blog, Category rootCategory) {
68  418 this.blog = blog;
69  418 this.rootCategory = rootCategory;
70    }
71   
72    /**
73    * Adds a category.
74    *
75    * @param category a Category instance
76    */
 
77  1474 toggle public void addCategory(Category category) {
78  1474 category.setBlog(blog);
79   
80  1474 if (category.isRootCategory()) {
81  1362 this.rootCategory = category;
82    } else {
83  112 Category parent = getParent(category, true);
84  112 parent.addSubCategory(category);
85    }
86    }
87   
88    /**
89    * Removes a category.
90    *
91    * @param category a Category instance
92    */
 
93  0 toggle public void removeCategory(Category category) {
94  0 Category parent = getParent(category);
95  0 parent.removeSubCategory(category);
96    }
97   
98    /**
99    * Gets (and creates if necessary), the parent of the specified category.
100    *
101    * @param category the Category to find the parent of
102    * @param create true if the parent should be created if it doesn't
103    * exist, false otherwise
104    * @return a Category instance
105    *
106    */
 
107  112 toggle private Category getParent(Category category, boolean create) {
108  112 String id = category.getId();
109  112 int index = id.lastIndexOf(CATEGORY_SEPARATOR);
110  112 String parentId = id.substring(0, index);
111  112 if (parentId.equals("")) {
112    // the parent is the root category
113  98 parentId = "/";
114    }
115   
116  112 return getCategory(parentId, create);
117    }
118   
119    /**
120    * Gets the parent of the specified category.
121    *
122    * @return a Category instance
123    */
 
124  0 toggle private Category getParent(Category category) {
125  0 return getParent(category, false);
126    }
127   
128    /**
129    * Gets (and creates if necessary), the specified category.
130    *
131    * @param id the id of the category to find
132    * @param create true if the category should be created if it doesn't
133    * exist, false otherwise
134    * @return a Category instance
135    */
 
136  2032 toggle private Category getCategory(String id, boolean create) {
137  2032 if (id == null || !id.startsWith("/")) {
138  6 id = "/" + id;
139    }
140   
141  2032 if (id.equals("/")) {
142  1846 if (rootCategory == null) {
143  1354 Category category = new Category("/", I18n.getMessage(blog.getLocale(), "category.all"));
144  1354 addCategory(category);
145    }
146   
147  1846 return rootCategory;
148    } else {
149  186 Category parentCategory = getRootCategory();
150  186 Category category = null;
151  186 boolean found = false;
152  186 int index = 0;
153  186 do {
154  188 index = id.indexOf("/", index+1);
155  188 String categoryId;
156  188 if (index == -1) {
157  186 categoryId = id.substring(0, id.length());
158    } else {
159  2 categoryId = id.substring(0, index);
160    }
161   
162  188 found = false;
163  188 Iterator it = parentCategory.getSubCategories().iterator();
164  228 while (it.hasNext()) {
165  104 category = (Category)it.next();
166  104 if (category.getId().equals(categoryId)) {
167  64 found = true;
168  64 break;
169    }
170    }
171   
172  188 if (!found) {
173  124 if (create) {
174  4 category = new Category(categoryId, categoryId);
175  4 addCategory(category);
176    } else {
177  120 return null;
178    }
179    }
180   
181  68 parentCategory = category;
182  68 } while (index != -1);
183   
184  66 return category;
185    }
186    }
187   
188    /**
189    * Gets (and creates if necessary), the specified category.
190    *
191    * @param id the id of the category to find
192    * @return a Category instance
193    */
 
194  172 toggle public Category getCategory(String id) {
195  172 return getCategory(id, false);
196    }
197   
198    /**
199    * Gets the root category.
200    *
201    * @return a Category instance
202    */
 
203  1748 toggle public Category getRootCategory() {
204  1748 return getCategory("/", true);
205    }
206   
207    /**
208    * Gets a collection containing all blog categories,
209    * ordered by category name.
210    *
211    * @return a sorted List of Category instances
212    */
 
213  188 toggle public List<Category> getCategories() {
214  188 List<Category> allCategories = new ArrayList<Category>();
215  188 allCategories.addAll(getCategories(getRootCategory()));
216  188 Collections.sort(allCategories);
217   
218  188 return allCategories;
219    }
220   
 
221  240 toggle public List<Category> getCategories(Category category) {
222  240 List<Category> allCategories = new ArrayList<Category>();
223  240 allCategories.add(category);
224  240 Iterator it = category.getSubCategories().iterator();
225  292 while (it.hasNext()) {
226  52 allCategories.addAll(getCategories((Category)it.next()));
227    }
228   
229  240 return allCategories;
230    }
231   
232    }