001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source.impl;
018:
019: import org.apache.avalon.framework.logger.AbstractLogEnabled;
020: import org.apache.cocoon.components.source.SourceInspector;
021: import org.apache.cocoon.components.source.helpers.SourceProperty;
022: import org.apache.excalibur.source.Source;
023: import org.apache.excalibur.source.SourceException;
024: import org.apache.excalibur.source.SourceValidity;
025: import org.apache.excalibur.source.impl.validity.NOPValidity;
026:
027: /**
028: * Abstract base class for inspectors that can calculate
029: * the size of an image of a particular type.
030: *
031: * @author <a href="mailto:unico@apache.org">Unico Hommes</a>
032: */
033: public abstract class AbstractImageSourceInspector extends
034: AbstractLogEnabled implements SourceInspector {
035:
036: /**
037: * The namespace uri of the properties exposed by this SourceInspector.
038: * <p>
039: * The value is <code>http://apache.org/cocoon/inspector/image/1.0</code>.
040: * </p>
041: */
042: public static final String PROPERTY_NS = "http://apache.org/cocoon/inspector/image/1.0";
043:
044: /**
045: * <code>width</code> property name.
046: */
047: public static final String IMAGE_WIDTH_PROPERTY_NAME = "width";
048:
049: /**
050: * <code>height</code> property name.
051: */
052: public static final String IMAGE_HEIGHT_PROPERTY_NAME = "height";
053:
054: private static final SourceValidity VALIDITY = new NOPValidity();
055:
056: private static final String[] HANDLED_PROPERTIES = new String[] {
057: PROPERTY_NS + "#" + IMAGE_HEIGHT_PROPERTY_NAME,
058: PROPERTY_NS + "#" + IMAGE_WIDTH_PROPERTY_NAME };
059:
060: public AbstractImageSourceInspector() {
061: }
062:
063: public final SourceProperty getSourceProperty(Source source,
064: String namespace, String name) throws SourceException {
065:
066: if (handlesProperty(namespace, name) && isImageMimeType(source)
067: && isImageFileType(source)) {
068: if (name.equals(IMAGE_WIDTH_PROPERTY_NAME))
069: return new SourceProperty(PROPERTY_NS,
070: IMAGE_WIDTH_PROPERTY_NAME, String
071: .valueOf(getImageSize(source)[0]));
072: if (name.equals(IMAGE_HEIGHT_PROPERTY_NAME))
073: return new SourceProperty(PROPERTY_NS,
074: IMAGE_HEIGHT_PROPERTY_NAME, String
075: .valueOf(getImageSize(source)[1]));
076: }
077: return null;
078: }
079:
080: public final SourceProperty[] getSourceProperties(Source source)
081: throws SourceException {
082: if (isImageMimeType(source) && isImageFileType(source)) {
083: int[] size = getImageSize(source);
084: return new SourceProperty[] {
085: new SourceProperty(PROPERTY_NS,
086: IMAGE_WIDTH_PROPERTY_NAME, String
087: .valueOf(size[0])),
088: new SourceProperty(PROPERTY_NS,
089: IMAGE_HEIGHT_PROPERTY_NAME, String
090: .valueOf(size[1])) };
091: }
092: return null;
093: }
094:
095: public final String[] getHandledPropertyTypes() {
096: return HANDLED_PROPERTIES;
097: }
098:
099: /**
100: * Check whether this inspector handles properties of the given kind.
101: */
102: public final boolean handlesProperty(String namespace, String name) {
103: return namespace.equals(PROPERTY_NS)
104: && (name.equals(IMAGE_WIDTH_PROPERTY_NAME) || name
105: .equals(IMAGE_HEIGHT_PROPERTY_NAME));
106: }
107:
108: /**
109: * Returns NOPValidity
110: */
111: public SourceValidity getValidity(Source source) {
112: return VALIDITY;
113: }
114:
115: /**
116: * Checks whether the mime mapping yields the type this inspector
117: * handles.
118: *
119: * @param source the Source to test
120: */
121: protected abstract boolean isImageMimeType(Source source);
122:
123: /**
124: * Inspects the input stream to verify this is in fact a file
125: * of the type that this inspector handles.
126: *
127: * @param source the Source to test
128: */
129: protected abstract boolean isImageFileType(Source source)
130: throws SourceException;
131:
132: /**
133: * Calculate the width and the height of the image represented by source.
134: *
135: * @param source the Source to inspect.
136: * @return array carrying the calculated width parameter
137: * in its 0 index, the height under index 1.
138: */
139: protected abstract int[] getImageSize(Source source)
140: throws SourceException;
141:
142: }
|