1 16 package org.apache.cocoon.components.source.impl; 17 18 import org.apache.avalon.framework.logger.AbstractLogEnabled; 19 import org.apache.cocoon.components.source.SourceInspector; 20 import org.apache.cocoon.components.source.helpers.SourceProperty; 21 import org.apache.excalibur.source.Source; 22 import org.apache.excalibur.source.SourceException; 23 import org.apache.excalibur.source.SourceValidity; 24 import org.apache.excalibur.source.impl.validity.NOPValidity; 25 26 32 public abstract class AbstractImageSourceInspector 33 extends AbstractLogEnabled 34 implements SourceInspector { 35 36 42 public static final String PROPERTY_NS = "http://apache.org/cocoon/inspector/image/1.0"; 43 44 47 public static final String IMAGE_WIDTH_PROPERTY_NAME = "width"; 48 49 52 public static final String IMAGE_HEIGHT_PROPERTY_NAME = "height"; 53 54 private static final SourceValidity VALIDITY = new NOPValidity(); 55 56 private static final String [] HANDLED_PROPERTIES = new String [] { 57 PROPERTY_NS + "#" + IMAGE_HEIGHT_PROPERTY_NAME, 58 PROPERTY_NS + "#" + IMAGE_WIDTH_PROPERTY_NAME 59 }; 60 61 62 public AbstractImageSourceInspector() { 63 } 64 65 public final SourceProperty getSourceProperty(Source source, String namespace, String name) 66 throws SourceException { 67 68 if (handlesProperty(namespace,name) && isImageMimeType(source) && isImageFileType(source)) { 69 if (name.equals(IMAGE_WIDTH_PROPERTY_NAME)) 70 return new SourceProperty(PROPERTY_NS, IMAGE_WIDTH_PROPERTY_NAME, 71 String.valueOf(getImageSize(source)[0])); 72 if (name.equals(IMAGE_HEIGHT_PROPERTY_NAME)) 73 return new SourceProperty(PROPERTY_NS, IMAGE_HEIGHT_PROPERTY_NAME, 74 String.valueOf(getImageSize(source)[1])); 75 } 76 return null; 77 } 78 79 public final SourceProperty[] getSourceProperties(Source source) throws SourceException { 80 if (isImageMimeType(source) && isImageFileType(source)) { 81 int[] size = getImageSize(source); 82 return new SourceProperty[] { 83 new SourceProperty(PROPERTY_NS, IMAGE_WIDTH_PROPERTY_NAME, String.valueOf(size[0])), 84 new SourceProperty(PROPERTY_NS, IMAGE_HEIGHT_PROPERTY_NAME, String.valueOf(size[1])) 85 }; 86 } 87 return null; 88 } 89 90 public final String [] getHandledPropertyTypes() { 91 return HANDLED_PROPERTIES; 92 } 93 94 97 public final boolean handlesProperty(String namespace, String name) { 98 return namespace.equals(PROPERTY_NS) && 99 (name.equals(IMAGE_WIDTH_PROPERTY_NAME) || 100 name.equals(IMAGE_HEIGHT_PROPERTY_NAME)); 101 } 102 103 106 public SourceValidity getValidity(Source source) { 107 return VALIDITY; 108 } 109 110 116 protected abstract boolean isImageMimeType(Source source); 117 118 124 protected abstract boolean isImageFileType(Source source) throws SourceException; 125 126 133 protected abstract int[] getImageSize(Source source) throws SourceException; 134 135 } 136 | Popular Tags |