KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > pages > PageValidationUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Carsten Woelk [cwoelk at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.pages;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 /**
32  * @author Carsten Woelk [cwoelk at neteye dot de]
33  * @since 6.5
34  */

35 public final class PageValidationUtils {
36
37     private PageValidationUtils() { }
38
39
40     public static boolean isValidChild(PageNode node, Page page) {
41         Collection JavaDoc childs = getChildsWithoutPage(node, page);
42         return !containsPathComponent(childs, page.getPathComponent());
43     }
44
45     public static boolean isTranslatable(Page page, Locale JavaDoc targetLocale) {
46         Collection JavaDoc siblings = getSiblings(page, targetLocale);
47         return !PageValidationUtils.containsPathComponent(siblings,
48                     page.getPathComponent());
49     }
50
51     public static boolean containsPathComponent(Collection JavaDoc pages, String JavaDoc pathComponent) {
52         Iterator JavaDoc it = pages.iterator();
53         while (it.hasNext()) {
54             Page page = (Page) it.next();
55             if (page.getPathComponent().equals(pathComponent)) {
56                 return true;
57             }
58         }
59         return false;
60     }
61
62     /**
63      * Returns all siblings of the page in the given locale. If the locale is
64      * identical to the page's locale the page itself will be contained too.
65      */

66     public static Collection JavaDoc getSiblings(Page page, Locale JavaDoc locale) {
67         return page.getNode().getParent().getChildPages(locale);
68     }
69
70     private static Collection JavaDoc getChildsWithoutPage(PageNode node, Page page) {
71         return without(node.getChildPages(page.getLocale()), page);
72     }
73
74     private static Collection JavaDoc without(Collection JavaDoc collection, Object JavaDoc item) {
75         ArrayList JavaDoc result = new ArrayList JavaDoc(collection);
76         result.remove(item);
77         return result;
78     }
79
80
81 }
82
Popular Tags