KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.alfresco.config.ConfigElement;
24 import org.alfresco.config.ConfigException;
25 import org.alfresco.config.xml.elementreader.ConfigElementReader;
26 import org.alfresco.web.config.AdvancedSearchConfigElement.CustomProperty;
27 import org.dom4j.Element;
28
29 /**
30  * Custom element reader to parse config for advanced search
31  *
32  * @author Gavin Cornwell
33  */

34 public class AdvancedSearchElementReader implements ConfigElementReader
35 {
36    public static final String JavaDoc ELEMENT_CONTENTTYPES = "content-types";
37    public static final String JavaDoc ELEMENT_TYPE = "type";
38    public static final String JavaDoc ELEMENT_CUSTOMPROPS = "custom-properties";
39    public static final String JavaDoc ELEMENT_METADATA = "meta-data";
40    public static final String JavaDoc ATTRIBUTE_NAME = "name";
41    public static final String JavaDoc ATTRIBUTE_TYPE = "type";
42    public static final String JavaDoc ATTRIBUTE_PROPERTY = "property";
43    public static final String JavaDoc ATTRIBUTE_ASPECT = "aspect";
44    public static final String JavaDoc ATTRIBUTE_DISPLAYLABEL = "displayLabelId";
45    
46    /**
47     * @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
48     */

49    @SuppressWarnings JavaDoc("unchecked")
50    public ConfigElement parse(Element element)
51    {
52       AdvancedSearchConfigElement configElement = null;
53       
54       if (element != null)
55       {
56          String JavaDoc name = element.getName();
57          if (name.equals(AdvancedSearchConfigElement.CONFIG_ELEMENT_ID) == false)
58          {
59             throw new ConfigException("AdvancedSearchElementReader can only parse " +
60                   AdvancedSearchConfigElement.CONFIG_ELEMENT_ID + " elements, the element passed was '" +
61                   name + "'");
62          }
63          
64          configElement = new AdvancedSearchConfigElement();
65          
66          // get the list of content types
67
Element contentTypes = element.element(ELEMENT_CONTENTTYPES);
68          if (contentTypes != null)
69          {
70             Iterator JavaDoc<Element> typesItr = contentTypes.elementIterator(ELEMENT_TYPE);
71             List JavaDoc<String JavaDoc> types = new ArrayList JavaDoc<String JavaDoc>(5);
72             while (typesItr.hasNext())
73             {
74                Element contentType = typesItr.next();
75                String JavaDoc type = contentType.attributeValue(ATTRIBUTE_NAME);
76                if (type != null)
77                {
78                   types.add(type);
79                }
80             }
81             configElement.setContentTypes(types);
82          }
83          
84          // get the list of custom properties to display
85
Element customProps = element.element(ELEMENT_CUSTOMPROPS);
86          if (customProps != null)
87          {
88             Iterator JavaDoc<Element> propsItr = customProps.elementIterator(ELEMENT_METADATA);
89             List JavaDoc<CustomProperty> props = new ArrayList JavaDoc<CustomProperty>(5);
90             while (propsItr.hasNext())
91             {
92                Element propElement = propsItr.next();
93                String JavaDoc type = propElement.attributeValue(ATTRIBUTE_TYPE);
94                String JavaDoc aspect = propElement.attributeValue(ATTRIBUTE_ASPECT);
95                String JavaDoc prop = propElement.attributeValue(ATTRIBUTE_PROPERTY);
96                String JavaDoc labelId = propElement.attributeValue(ATTRIBUTE_DISPLAYLABEL);
97                props.add(new AdvancedSearchConfigElement.CustomProperty(type, aspect, prop, labelId));
98             }
99             configElement.setCustomProperties(props);
100          }
101       }
102       
103       return configElement;
104    }
105 }
106
Popular Tags