1 17 package org.alfresco.web.config; 18 19 import java.util.Iterator ; 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 29 public class MimeTypesElementReader implements ConfigElementReader 30 { 31 public final static String ELEMENT_MIMETYPES = "mimetypes"; 32 public final static String ELEMENT_MIMEMAPPING = "mime-mapping"; 33 public final static String ELEMENT_EXTENSION = "extension"; 34 public final static String ELEMENT_MIMETYPE = "mime-type"; 35 36 39 public ConfigElement parse(Element element) 40 { 41 MimeTypeConfigElement configElement = null; 42 43 if (element != null) 44 { 45 String 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 Iterator <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 extension = extensionElement.getTextTrim(); 69 String 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 configElement.addMapping(extension, mimetype); 82 } 83 } 84 85 return configElement; 86 } 87 } 88 | Popular Tags |