KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > AbstractImageSourceInspector


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
27  * Abstract base class for inspectors that can calculate
28  * the size of an image of a particular type.
29  *
30  * @author <a HREF="mailto:unico@apache.org">Unico Hommes</a>
31  */

32 public abstract class AbstractImageSourceInspector
33     extends AbstractLogEnabled
34     implements SourceInspector {
35
36     /**
37      * The namespace uri of the properties exposed by this SourceInspector.
38      * <p>
39      * The value is <code>http://apache.org/cocoon/inspector/image/1.0</code>.
40      * </p>
41      */

42     public static final String JavaDoc PROPERTY_NS = "http://apache.org/cocoon/inspector/image/1.0";
43     
44     /**
45      * <code>width</code> property name.
46      */

47     public static final String JavaDoc IMAGE_WIDTH_PROPERTY_NAME = "width";
48     
49     /**
50      * <code>height</code> property name.
51      */

52     public static final String JavaDoc IMAGE_HEIGHT_PROPERTY_NAME = "height";
53     
54     private static final SourceValidity VALIDITY = new NOPValidity();
55     
56     private static final String JavaDoc[] HANDLED_PROPERTIES = new String JavaDoc[] {
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 JavaDoc namespace, String JavaDoc 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 JavaDoc[] getHandledPropertyTypes() {
91         return HANDLED_PROPERTIES;
92     }
93     
94     /**
95      * Check whether this inspector handles properties of the given kind.
96      */

97     public final boolean handlesProperty(String JavaDoc namespace, String JavaDoc name) {
98         return namespace.equals(PROPERTY_NS) &&
99                    (name.equals(IMAGE_WIDTH_PROPERTY_NAME) ||
100                    name.equals(IMAGE_HEIGHT_PROPERTY_NAME));
101     }
102     
103     /**
104      * Returns NOPValidity
105      */

106     public SourceValidity getValidity(Source source) {
107         return VALIDITY;
108     }
109     
110     /**
111      * Checks whether the mime mapping yields the type this inspector
112      * handles.
113      *
114      * @param source the Source to test
115      */

116     protected abstract boolean isImageMimeType(Source source);
117     
118     /**
119      * Inspects the input stream to verify this is in fact a file
120      * of the type that this inspector handles.
121      *
122      * @param source the Source to test
123      */

124     protected abstract boolean isImageFileType(Source source) throws SourceException;
125     
126     /**
127      * Calculate the width and the height of the image represented by source.
128      *
129      * @param source the Source to inspect.
130      * @return array carrying the calculated width parameter
131      * in its 0 index, the height under index 1.
132      */

133     protected abstract int[] getImageSize(Source source) throws SourceException;
134
135 }
136
Popular Tags