001: /*
002: * $RCSfile: FileLoadDescriptor.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.3 $
009: * $Date: 2005/11/29 00:08:56 $
010: * $State: Exp $
011: */
012: package javax.media.jai.operator;
013:
014: import com.sun.media.jai.codec.ImageDecodeParam;
015: import java.awt.RenderingHints;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.renderable.ParameterBlock;
018: import java.io.File;
019: import java.io.InputStream;
020: import javax.media.jai.JAI;
021: import javax.media.jai.OperationDescriptorImpl;
022: import javax.media.jai.ParameterBlockJAI;
023: import javax.media.jai.RenderedOp;
024: import javax.media.jai.registry.RenderedRegistryMode;
025:
026: /**
027: * An <code>OperationDescriptor</code> describing the "FileLoad" operation.
028: *
029: * <p>In the default instance the <code>validateParameters()</code> method
030: * checks that the named file exists and is readable. If not, it will return
031: * <code>false</code>, causing <code>JAI.createNS()</code> to throw an
032: * <code>IllegalArgumentException</code>.
033: *
034: * In special cases like when an image is loaded from a Remote system,
035: * the above check for existence of a file on the local system could be bypassed.
036: * This is done by setting the <code>Boolean</code> variable <code>checkFileLocally</code> to <code>FALSE</code> in the <code>ParameterBlock</code>
037: *
038: * <p> The allowable formats are those registered with the
039: * <code>com.sun.media.jai.codec.ImageCodec</code> class.
040: *
041: * <p> The second parameter contains an instance of
042: * <code>ImageDecodeParam</code> to be used during the decoding.
043: * It may be set to <code>null</code> in order to perform default
044: * decoding, or equivalently may be omitted.
045: *
046: * <p><b> The classes in the <code>com.sun.media.jai.codec</code>
047: * package are not a committed part of the JAI API. Future releases
048: * of JAI will make use of new classes in their place. This
049: * class will change accordingly.</b>
050: *
051: * <p><table border=1>
052: * <caption>Resource List</caption>
053: * <tr><th>Name</th> <th>Value</th></tr>
054: * <tr><td>GlobalName</td> <td>fileload</td></tr>
055: * <tr><td>LocalName</td> <td>fileload</td></tr>
056: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
057: * <tr><td>Description</td> <td>Reads an image from a file.</td></tr>
058: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/FileLoadDescriptor.html</td></tr>
059: * <tr><td>Version</td> <td>1.0</td></tr>
060: * <tr><td>arg0Desc</td> <td>The path of the file to read from.</td></tr>
061: * <tr><td>arg1Desc</td> <td>The ImageDecodeParam to use.</td></tr>
062: * <tr><td>arg2Desc</td> <td>Boolean specifying if File existence should be checked locally.</td></tr>
063: * </table></p>
064: *
065: * <p><table border=1>
066: * <caption>Parameter List</caption>
067: * <tr><th>Name</th> <th>Class Type</th>
068: * <th>Default Value</th></tr>
069: * <tr><td>filename</td> <td>java.lang.String</td>
070: * <td>NO_PARAMETER_DEFAULT</td>
071: * <tr><td>param</td> <td>com.sun.media.jai.codec.ImageDecodeParam</td>
072: * <td>null</td>
073: * <tr><td>checkFileLocally</td> <td>java.lang.Boolean</td>
074: * <td>TRUE</td>
075: * </table></p>
076: *
077: * @see javax.media.jai.OperationDescriptor
078: */
079: public class FileLoadDescriptor extends OperationDescriptorImpl {
080:
081: /**
082: * The resource strings that provide the general documentation and
083: * specify the parameter list for the "FileLoad" operation.
084: */
085: private static final String[][] resources = {
086: { "GlobalName", "FileLoad" },
087: { "LocalName", "FileLoad" },
088: { "Vendor", "com.sun.media.jai" },
089: { "Description", JaiI18N.getString("FileLoadDescriptor0") },
090: {
091: "DocURL",
092: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/FileLoadDescriptor.html" },
093: { "Version", JaiI18N.getString("DescriptorVersion") },
094: { "arg0Desc", JaiI18N.getString("FileLoadDescriptor1") },
095: { "arg1Desc", JaiI18N.getString("FileLoadDescriptor4") },
096: { "arg2Desc", JaiI18N.getString("FileLoadDescriptor5") } };
097:
098: /** The parameter names for the "FileLoad" operation. */
099: private static final String[] paramNames = { "filename", "param",
100: "checkFileLocally" };
101:
102: /** The parameter class types for the "FileLoad" operation. */
103: private static final Class[] paramClasses = {
104: java.lang.String.class,
105: com.sun.media.jai.codec.ImageDecodeParam.class,
106: java.lang.Boolean.class };
107:
108: /** The parameter default values for the "FileLoad" operation. */
109: private static final Object[] paramDefaults = {
110: NO_PARAMETER_DEFAULT, null, Boolean.TRUE };
111:
112: /** Constructor. */
113: public FileLoadDescriptor() {
114: super (resources, 0, paramClasses, paramNames, paramDefaults);
115: }
116:
117: /**
118: * Validates the input parameters.
119: *
120: * <p> In addition to the standard checks performed by the
121: * superclass method, this method by default checks that the source file
122: * exists and is readable. This check may be bypassed by setting the
123: * <code>checkFileLocally</code> parameter to <code>FALSE</code>
124: */
125: protected boolean validateParameters(ParameterBlock args,
126: StringBuffer msg) {
127: if (!super .validateParameters(args, msg)) {
128: return false;
129: }
130:
131: Boolean checkFile = (Boolean) args.getObjectParameter(2);
132: if (checkFile.booleanValue()) {
133: String filename = (String) args.getObjectParameter(0);
134: File f = new File(filename);
135: boolean fileExists = f.exists();
136: if (!fileExists) {
137: // Check if the file is accessible as an InputStream resource.
138: // This would be the case if the application and the image file
139: // are packaged in a JAR file
140: InputStream is = this .getClass().getClassLoader()
141: .getResourceAsStream(filename);
142: if (is == null) {
143: msg.append("\"" + filename + "\": "
144: + JaiI18N.getString("FileLoadDescriptor2"));
145: return false;
146: }
147: } else { // file exists
148: if (!f.canRead()) {
149: msg.append("\"" + filename + "\": "
150: + JaiI18N.getString("FileLoadDescriptor3"));
151: return false;
152: }
153: }
154: }
155: return true;
156: }
157:
158: /**
159: * Reads an image from a file.
160: *
161: * <p>Creates a <code>ParameterBlockJAI</code> from all
162: * supplied arguments except <code>hints</code> and invokes
163: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
164: *
165: * @see JAI
166: * @see ParameterBlockJAI
167: * @see RenderedOp
168: *
169: * @param filename The path of the file to read from.
170: * @param param The ImageDecodeParam to use.
171: * May be <code>null</code>.
172: * @param checkFileLocally Boolean specifying if File existence should be checked locally
173: * May be <code>null</code>.
174: * @param hints The <code>RenderingHints</code> to use.
175: * May be <code>null</code>.
176: * @return The <code>RenderedOp</code> destination.
177: * @throws IllegalArgumentException if <code>filename</code> is <code>null</code>.
178: */
179: public static RenderedOp create(String filename,
180: ImageDecodeParam param, Boolean checkFileLocally,
181: RenderingHints hints) {
182: ParameterBlockJAI pb = new ParameterBlockJAI("FileLoad",
183: RenderedRegistryMode.MODE_NAME);
184:
185: pb.setParameter("filename", filename);
186: pb.setParameter("param", param);
187: pb.setParameter("checkFileLocally", checkFileLocally);
188:
189: return JAI.create("FileLoad", pb, hints);
190: }
191: }
|