1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (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.mozilla.org/MPL/MPL-1.1.html 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 17 import org.as2lib.core.BasicClass; 18 import org.as2lib.data.holder.Map; 19 import org.as2lib.data.holder.map.HashMap; 20 import org.as2lib.data.type.Byte; 21 import org.as2lib.env.overload.Overload; 22 import org.as2lib.io.file.SimpleTextFileFactory; 23 import org.as2lib.io.file.TextFile; 24 import org.as2lib.io.file.TextFileFactory; 25 26 /** 27 * {@code CompositeTextFileFactory} uses different {@code TextFileFactory} 28 * implementations depending to the extension of the passed-in {@code uri} in 29 * {@code FileFactory.createTextFile}. 30 * 31 * <p>Its a common case that different file extensions are used for different 32 * kinds of file formats. {@code CompositeTextFileFactory} allows different processing 33 * of resources depending to the extension of the loaded file. 34 * 35 * <p>If a certain extension has not been specially set it uses the {@code defaultTextFileFactory} 36 * to be set with {@code setDefaultTextFileFactory}. 37 * 38 * <p>It uses {@link SimpleTextFileFactory} as default if no other has been set. 39 * 40 * @author Martin Heidegger 41 * @version 1.1 42 */ 43 class org.as2lib.io.file.CompositeTextFileFactory extends BasicClass 44 implements TextFileFactory { 45 46 /** 47 * {@code TextFileFactory} to be used if no other {@code TextFileFactory} 48 * is available. 49 */ 50 private var defaultTextFileFactory:TextFileFactory; 51 52 /** Maps {@code TextFileFactory}s to file extensions. */ 53 private var extensionFactories:Map; 54 55 /** 56 * Constructs a new {@code CompositeTextFileFactory}. 57 */ 58 public function CompositeTextFileFactory(Void) { 59 defaultTextFileFactory = new SimpleTextFileFactory(); 60 extensionFactories = new HashMap(); 61 } 62 63 /** 64 * Creates a {@code TextFile} implementation depending to the set {@code TextFileFactory}s. 65 * 66 * @param source content of the {@code TextFile} to create 67 * @param size size in {@link Byte} of the loaded resource 68 * @param uri location of the loaded resource 69 * @return {@code TextFile} that represents the resource 70 */ 71 public function createTextFile(source:String, size:Byte, uri:String):TextFile { 72 var factory:TextFileFactory = 73 extensionFactories.get(uri.substr(uri.lastIndexOf("."))); 74 if (!factory) { 75 factory = defaultTextFileFactory; 76 } 77 return factory.createTextFile(source, size, uri); 78 } 79 80 /** 81 * Sets the default {@code TextFileFactory} to be used in default case. 82 * 83 * <p>If no other set {@code TextFileFactory} applies to the requested 84 * {@code uri} the passed-in {@code textFileFactory} will be used. 85 * 86 * @param textFileFactory {@code TextFileFactory} to be used in default case 87 */ 88 public function setDefaultTextFileFactory(textFileFactory:TextFileFactory):Void { 89 defaultTextFileFactory = textFileFactory; 90 } 91 92 /** 93 * @overload #putTextFileFactoryByExtension 94 * @overload #putTextFileFactoryByExtensions 95 */ 96 public function putTextFileFactory() { 97 var o:Overload = new Overload(this); 98 o.addHandler(String, putTextFileFactoryByExtension); 99 o.addHandler(Array, putTextFileFactoryByExtensions); 100 return o.forward(arguments); 101 } 102 103 /** 104 * Sets a certain {@code TextFileFactory} to be used for files with the 105 * passed-in {@code extension}. 106 * 107 * <p>The passed-in extension should not contain a leading ".". 108 * 109 * <p>Proper example: 110 * <code> 111 * var textFileFactory:CompositeFileFactory = new CompositeTextFileFactory(); 112 * textFileFactory.putTextFileFactoryByExtension("txt", new SimpleTextFileFactory()); 113 * </code> 114 * 115 * @param extension extension of the file that should be recognized by the 116 * passed-in {@code textFileFactory} 117 * @param textFileFactory {@code TextFileFactory} that creates the files 118 */ 119 public function putTextFileFactoryByExtension(extension:String, 120 fileFactory:TextFileFactory):Void { 121 extensionFactories.put(extension, fileFactory); 122 } 123 124 /** 125 * Sets a certain {@code TextFileFactory} to be used for files with one extension 126 * of the passed-in {@code extensions}. 127 * 128 * <p>Any of the passed-in extension should not contain a leading ".". 129 * 130 * <p>Proper example: 131 * <code> 132 * var textFileFactory:CompositeTextFileFactory = new CompositeTextFileFactory(); 133 * textFileFactory.putTextFileFactoryByExtensions(["txt", "prop"], 134 * new SimpleTextFileFactory()); 135 * </code> 136 * 137 * @param extensions list of extensions of files that should be recognized 138 * by the passed-in {@code textFileFactory} 139 * @param fileFactory {@code TextFileFactory} that creates the files 140 */ 141 public function putTextFileFactoryByExtensions(extensions:Array, 142 fileFactory:TextFileFactory):Void { 143 var i:Number; 144 for( i=0; i<extensions.length; i++) { 145 putTextFileFactoryByExtension(extensions[i], fileFactory); 146 } 147 } 148 }