incubator-kato-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spo...@apache.org
Subject svn commit: r777119 - in /incubator/kato/trunk/org.apache.kato.api: src/org/apache/kato/FactoryRegistry.java src/org/apache/kato/image/ImageFactory.java testsrc/test/apache/kato/TestFactoryRegistry.java
Date Thu, 21 May 2009 13:59:09 GMT
Author: spoole
Date: Thu May 21 13:59:09 2009
New Revision: 777119

URL: http://svn.apache.org/viewvc?rev=777119&view=rev
Log:
added support to api factory registry for a file suffix based mechanism to create an image
from a supporting factory

Added:
    incubator/kato/trunk/org.apache.kato.api/testsrc/test/apache/kato/TestFactoryRegistry.java
  (with props)
Modified:
    incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/FactoryRegistry.java
    incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/image/ImageFactory.java

Modified: incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/FactoryRegistry.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/FactoryRegistry.java?rev=777119&r1=777118&r2=777119&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/FactoryRegistry.java (original)
+++ incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/FactoryRegistry.java Thu
May 21 13:59:09 2009
@@ -13,12 +13,15 @@
  ******************************************************************************/
 package org.apache.kato;
 
+import java.io.File;
+import java.io.IOException;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
 
 import javax.imageio.spi.ServiceRegistry;
 
+import org.apache.kato.image.Image;
 import org.apache.kato.image.ImageFactory;
 
 /**
@@ -121,4 +124,50 @@
 		
 		return factories.toArray(new ImageFactory[0]);
 	}
+	
+	/**
+	 * Returns an appropriate {@link org.apache.kato.image.Image} for the
+	 * provide file by locating the first registered image factory that can handle
+	 * the case insensitive file name extension of the provided file. 
+	 * If no factory can be found to handle the file then null is returned. 
+	 * 
+	 * File names without extension will always return null  
+	 *  
+	 * @throws IllegalArgumentException if file is null
+	 * @throws IOException if errors occur during image creation
+	 * 
+	 * @param file to create image from
+	 * @return Image or null
+	 * 
+	 */
+	public Image getImage(File file) throws IOException {
+			if(file==null) throw new IllegalArgumentException("file is null");
+		
+			String fileName=file.getName();
+			
+			int lastDot=fileName.lastIndexOf('.');
+			
+			if(lastDot<0) {  
+				return null; // no suffix
+			}
+			
+			if( (lastDot+1)==fileName.length()) {
+				return null;  // no suffix
+			}
+			
+			String suffix=fileName.substring(lastDot+1);
+			
+			for(ImageFactory factory:factories) {
+				String[] extensions=factory.getValidFileExtensions();
+				if(extensions==null) continue;
+				for(String value:extensions) {
+					if(suffix.equalsIgnoreCase(value)) {
+						// found match
+						return factory.getImage(file);
+					}
+				}
+				
+			}
+			return null; // no matches
+	}
 }

Modified: incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/image/ImageFactory.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/image/ImageFactory.java?rev=777119&r1=777118&r2=777119&view=diff
==============================================================================
--- incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/image/ImageFactory.java (original)
+++ incubator/kato/trunk/org.apache.kato.api/src/org/apache/kato/image/ImageFactory.java Thu
May 21 13:59:09 2009
@@ -103,4 +103,6 @@
 	 * @since Kato version 1.0
 	 */	
 	public int getKatoModificationLevel();
+
+	public String[] getValidFileExtensions();
 }

Added: incubator/kato/trunk/org.apache.kato.api/testsrc/test/apache/kato/TestFactoryRegistry.java
URL: http://svn.apache.org/viewvc/incubator/kato/trunk/org.apache.kato.api/testsrc/test/apache/kato/TestFactoryRegistry.java?rev=777119&view=auto
==============================================================================
--- incubator/kato/trunk/org.apache.kato.api/testsrc/test/apache/kato/TestFactoryRegistry.java
(added)
+++ incubator/kato/trunk/org.apache.kato.api/testsrc/test/apache/kato/TestFactoryRegistry.java
Thu May 21 13:59:09 2009
@@ -0,0 +1,245 @@
+/*******************************************************************************
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ ******************************************************************************/
+package test.apache.kato;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.kato.FactoryRegistry;
+import org.apache.kato.image.CorruptDataException;
+import org.apache.kato.image.DataUnavailable;
+import org.apache.kato.image.Image;
+import org.apache.kato.image.ImageFactory;
+
+public class TestFactoryRegistry extends TestCase {
+
+	
+	private final class MockFactory implements ImageFactory {
+		
+		private String[] suffixes=null;
+		
+		public MockFactory(String[] suffixes) {
+			this.suffixes=suffixes;
+		}
+		public Image getImage(File imageFile) throws IOException {
+			
+			return new MockImage();
+		}
+
+		public Image getImage(File imageFile, File metadata)
+				throws IOException {
+
+			return null;
+		}
+
+		public int getKatoMajorVersion() {
+
+			return 0;
+		}
+
+		public int getKatoMinorVersion() {
+
+			return 0;
+		}
+
+		public int getKatoModificationLevel() {
+
+			return 0;
+		}
+
+		public String[] getValidFileExtensions() {
+		
+			return suffixes;
+		}
+	}
+
+	private final class MockImage implements Image {
+		public List getAddressSpaces() {
+			
+			return null;
+		}
+
+		public long getCreationTime() throws DataUnavailable {
+			
+			return 0;
+		}
+
+		public String getHostName() throws DataUnavailable,
+				CorruptDataException {
+			
+			return null;
+		}
+
+		public Iterator getIPAddresses() throws DataUnavailable {
+			
+			return null;
+		}
+
+		public long getInstalledMemory() throws DataUnavailable {
+			
+			return 0;
+		}
+
+		public int getProcessorCount() throws DataUnavailable {
+			
+			return 0;
+		}
+
+		public String getProcessorSubType() throws DataUnavailable,
+				CorruptDataException {
+			
+			return null;
+		}
+
+		public String getProcessorType() throws DataUnavailable,
+				CorruptDataException {
+
+			return null;
+		}
+
+		public String getSystemSubType() throws DataUnavailable,
+				CorruptDataException {
+
+			return null;
+		}
+
+		public String getSystemType() throws DataUnavailable,
+				CorruptDataException {
+
+			return null;
+		}
+	}
+
+	public void testConstructor() {
+		
+		new FactoryRegistry();
+	}
+	
+	public void testSimpleConstructorHasNoFactories() {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		ImageFactory[] factories=reg.getFactories();
+		
+		assertEquals(0,factories.length);
+		
+	}
+	
+	public void testNullFileNameThrowsError() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		try {
+		reg.getImage(null);
+		fail("Expected illegal argument exception");
+		
+		} catch(IllegalArgumentException e) {
+			; // ok
+		}
+		
+	}
+	
+	public void testNoSuffixFileNameReturnsNull() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		File f=new File("testfile");
+		
+		Image result=reg.getImage(f);
+		assertNull(result);
+		
+	}
+	
+	public void testDotSuffixFileNameReturnsNull() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		File f=new File("testfile.");
+		
+		Image result=reg.getImage(f);
+		assertNull(result);
+		
+	}
+	public void testSimpleNoSuffixes() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		reg.addFactory(new MockFactory(null));
+		
+		File f=new File("testfile.suffix");
+		
+		Image result=reg.getImage(f);
+		assertNull(result);
+		
+	}
+	public void testSimpleSuffixMatch() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		String[] suffixes=new String[]{"suffix"};
+		
+		reg.addFactory(new MockFactory(suffixes));
+		
+		File f=new File("testfile.suffix");
+		
+		Image result=reg.getImage(f);
+		assertNotNull(result);
+		
+	}
+
+	public void testSimpleMixedCaseSuffixMatch() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		String[] suffixes=new String[]{"suFFix"};
+		
+		reg.addFactory(new MockFactory(suffixes));
+		
+		File f=new File("testfile.suffix");
+		
+		Image result=reg.getImage(f);
+		assertNotNull(result);
+		
+	}
+	
+	public void testTwoChoiceSuffixMatch() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		String[] suffixes=new String[]{"suFFix","other"};
+		
+		reg.addFactory(new MockFactory(suffixes));
+		
+		File f=new File("testfile.other");
+		
+		Image result=reg.getImage(f);
+		assertNotNull(result);
+		
+		
+	}
+	
+public void testTwoFactorySuffixMatch() throws IOException {
+		
+		FactoryRegistry reg=new FactoryRegistry();
+		
+		String[] suffixes=new String[]{"suFFix","other"};
+		reg.addFactory(new MockFactory(suffixes));
+		
+		suffixes=new String[]{"rubarb","CustARD"};
+		reg.addFactory(new MockFactory(suffixes));
+		
+		File f=new File("testfile.cusTard");
+		
+		Image result=reg.getImage(f);
+		assertNotNull(result);
+		
+		
+	}
+}

Propchange: incubator/kato/trunk/org.apache.kato.api/testsrc/test/apache/kato/TestFactoryRegistry.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message