KickJava   Java API By Example, From Geeks To Geeks.

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


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.dom4j.Element;
25
26 /**
27  * @author Kevin Roast
28  */

29 public class MimeTypesElementReader implements ConfigElementReader
30 {
31    public final static String JavaDoc ELEMENT_MIMETYPES = "mimetypes";
32    public final static String JavaDoc ELEMENT_MIMEMAPPING = "mime-mapping";
33    public final static String JavaDoc ELEMENT_EXTENSION = "extension";
34    public final static String JavaDoc ELEMENT_MIMETYPE = "mime-type";
35    
36    /**
37     * @see org.alfresco.config.xml.elementreader.ConfigElementReader#parse(org.dom4j.Element)
38     */

39    public ConfigElement parse(Element element)
40    {
41       MimeTypeConfigElement configElement = null;
42       
43       if (element != null)
44       {
45          String JavaDoc name = element.getName();
46          if (name.equals(ELEMENT_MIMETYPES) == false)
47          {
48             throw new ConfigException("MimeTypesElementReader can only parse " +
49                   ELEMENT_MIMETYPES + "elements, the element passed was '" +
50                   name + "'");
51          }
52          
53          configElement = new MimeTypeConfigElement();
54          
55          // walk the mime-mapping elements
56
Iterator JavaDoc<Element> mappings = element.elementIterator(ELEMENT_MIMEMAPPING);
57          while (mappings.hasNext())
58          {
59             Element mapping = mappings.next();
60             Element extensionElement = mapping.element(ELEMENT_EXTENSION);
61             Element mimetypeElement = mapping.element(ELEMENT_MIMETYPE);
62             
63             if (extensionElement == null || mimetypeElement == null)
64             {
65                throw new ConfigException("mime-mapping element must specify 'extension' and 'mime-type'");
66             }
67             
68             String JavaDoc extension = extensionElement.getTextTrim();
69             String JavaDoc mimetype = mimetypeElement.getTextTrim();
70             
71             if (extension == null || extension.length() == 0)
72             {
73                throw new ConfigException("mime-mapping extension element value must be specified");
74             }
75             if (mimetype == null || mimetype.length() == 0)
76             {
77                throw new ConfigException("mime-mapping mimetype element value must be specified");
78             }
79             
80             // add the mimetype extension to the config element
81
configElement.addMapping(extension, mimetype);
82          }
83       }
84       
85       return configElement;
86    }
87 }
88
Popular Tags