KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > config > ViewsElementReader


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.config;
18
19 import java.util.Iterator JavaDoc;
20
21 import org.alfresco.config.ConfigElement;
22 import org.alfresco.config.ConfigException;
23 import org.alfresco.config.xml.elementreader.ConfigElementReader;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.dom4j.Element;
27
28 /**
29  * Custom element reader to parse config for client views
30  *
31  * @author Gavin Cornwell
32  */

33 public class ViewsElementReader implements ConfigElementReader
34 {
35    public static final String JavaDoc ELEMENT_VIEW = "view";
36    public static final String JavaDoc ELEMENT_VIEWIMPL = "view-impl";
37    public static final String JavaDoc ELEMENT_VIEWDEFAULTS = "view-defaults";
38    public static final String JavaDoc ELEMENT_PAGESIZE = "page-size";
39    public static final String JavaDoc ELEMENT_SORTCOLUMN = "sort-column";
40    public static final String JavaDoc ELEMENT_SORTDIRECTION = "sort-direction";
41    
42    private static Log logger = LogFactory.getLog(ViewsElementReader.class);
43    
44    /**
45     * @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
46     */

47    @SuppressWarnings JavaDoc("unchecked")
48    public ConfigElement parse(Element element)
49    {
50       ViewsConfigElement configElement = null;
51       
52       if (element != null)
53       {
54          String JavaDoc name = element.getName();
55          if (name.equals(ViewsConfigElement.CONFIG_ELEMENT_ID) == false)
56          {
57             throw new ConfigException("ViewsElementReader can only parse " +
58                   ViewsConfigElement.CONFIG_ELEMENT_ID + " elements, the element passed was '" +
59                   name + "'");
60          }
61          
62          configElement = new ViewsConfigElement();
63          
64          // get the configured views
65
Iterator JavaDoc<Element> renderers = element.elementIterator(ELEMENT_VIEWIMPL);
66          while (renderers.hasNext())
67          {
68             Element renderer = renderers.next();
69             configElement.addView(renderer.getTextTrim());
70          }
71          
72          // get all the view related default settings
73
Element viewDefaults = element.element(ELEMENT_VIEWDEFAULTS);
74          if (viewDefaults != null)
75          {
76             Iterator JavaDoc<Element> pages = viewDefaults.elementIterator();
77             while (pages.hasNext())
78             {
79                Element page = pages.next();
80                String JavaDoc pageName = page.getName();
81                
82                // get the default view mode for the page
83
Element defaultView = page.element(ELEMENT_VIEW);
84                if (defaultView != null)
85                {
86                   String JavaDoc viewName = defaultView.getTextTrim();
87                   configElement.addDefaultView(pageName, viewName);
88                }
89                
90                // get the initial sort column
91
Element sortColumn = page.element(ELEMENT_SORTCOLUMN);
92                if (sortColumn != null)
93                {
94                   String JavaDoc column = sortColumn.getTextTrim();
95                   configElement.addDefaultSortColumn(pageName, column);
96                }
97                
98                // get the sort direction option
99
Element sortDir = page.element(ELEMENT_SORTDIRECTION);
100                if (sortDir != null)
101                {
102                   configElement.addSortDirection(pageName, sortDir.getTextTrim());
103                }
104                
105                // process the page-size element
106
processPageSizeElement(page.element(ELEMENT_PAGESIZE),
107                      pageName, configElement);
108             }
109          }
110       }
111       
112       return configElement;
113    }
114    
115    /**
116     * Processes a page-size element
117     *
118     * @param pageSizeElement The element to process
119     * @param page The page the page-size element belongs to
120     * @param configElement The config element being populated
121     */

122    @SuppressWarnings JavaDoc("unchecked")
123    private void processPageSizeElement(Element pageSizeElement, String JavaDoc page,
124          ViewsConfigElement configElement)
125    {
126       if (pageSizeElement != null)
127       {
128          Iterator JavaDoc<Element> views = pageSizeElement.elementIterator();
129          while (views.hasNext())
130          {
131             Element view = views.next();
132             String JavaDoc viewName = view.getName();
133             String JavaDoc pageSize = view.getTextTrim();
134             try
135             {
136                configElement.addDefaultPageSize(page, viewName, Integer.parseInt(pageSize));
137             }
138             catch (NumberFormatException JavaDoc nfe)
139             {
140                if (logger.isWarnEnabled())
141                {
142                   logger.warn("Failed to set page size for view '" + viewName +
143                         "' in page '" + page + "' as '" + pageSize +
144                         "' is an invalid number!");
145                }
146             }
147          }
148       }
149    }
150 }
151
Popular Tags