| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| AssetFactory |
|
| 0.0;0 |
| 1 | /* | |
| 2 | * Licensed under the MIT License | |
| 3 | * | |
| 4 | * Copyright 2011 (c) Flávio Silva, flsilva.com | |
| 5 | * | |
| 6 | * Permission is hereby granted, free of charge, to any person | |
| 7 | * obtaining a copy of this software and associated documentation | |
| 8 | * files (the "Software"), to deal in the Software without | |
| 9 | * restriction, including without limitation the rights to use, | |
| 10 | * copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 11 | * copies of the Software, and to permit persons to whom the | |
| 12 | * Software is furnished to do so, subject to the following | |
| 13 | * conditions: | |
| 14 | * | |
| 15 | * The above copyright notice and this permission notice shall be | |
| 16 | * included in all copies or substantial portions of the Software. | |
| 17 | * | |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
| 20 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| 21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| 22 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| 23 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| 25 | * OTHER DEALINGS IN THE SOFTWARE. | |
| 26 | * | |
| 27 | * http://www.opensource.org/licenses/mit-license.php | |
| 28 | */ | |
| 29 | 1 | package org.vostokframework.domain.assets |
| 30 | { | |
| 31 | import org.as3utils.StringUtil; | |
| 32 | import org.vostokframework.VostokIdentification; | |
| 33 | import org.vostokframework.domain.assets.errors.UnsupportedAssetTypeError; | |
| 34 | ||
| 35 | /** | |
| 36 | * description | |
| 37 | * | |
| 38 | * @author Flávio Silva | |
| 39 | */ | |
| 40 | public class AssetFactory | |
| 41 | { | |
| 42 | /** | |
| 43 | * @private | |
| 44 | */ | |
| 45 | private var _urlAssetParser:UrlAssetParser; | |
| 46 | ||
| 47 | public function AssetFactory() | |
| 48 | 1 | { |
| 49 | 1 | _urlAssetParser = createUrlAssetParser(); |
| 50 | 1 | } |
| 51 | ||
| 52 | /** | |
| 53 | * description | |
| 54 | * | |
| 55 | * @param src | |
| 56 | * @param assetPackage | |
| 57 | * @param priority | |
| 58 | * @param settings | |
| 59 | * @param id | |
| 60 | * @param type | |
| 61 | * @throws ArgumentError if the <code>src</code> argument is <code>null</code> or <code>empty</code>. | |
| 62 | * @throws ArgumentError if the <code>assetPackage</code> argument is <code>null</code>. | |
| 63 | * @throws org.vostokframework.domain.assets.errors.UnsupportedAssetType if the <code>type</code> argument is <code>null</code> and the framework cannot get the Asset Type over its <code>src</code> argument or the file extension in the <code>src</code> argument is not supported. | |
| 64 | * @return | |
| 65 | */ | |
| 66 | public function create(src:String, assetPackage:AssetPackage, id:String = null, type:AssetType = null): Asset | |
| 67 | { | |
| 68 | 1 | if (StringUtil.isBlank(src)) throw new ArgumentError("Argument <src> must not be null nor an empty String."); |
| 69 | 1 | if (!assetPackage) throw new ArgumentError("Argument <assetPackage> must not be null."); |
| 70 | ||
| 71 | 1 | var identification:VostokIdentification = createIdentification(src, assetPackage, id); |
| 72 | 1 | if (!type) type = getType(src); |
| 73 | ||
| 74 | 1 | if (!type) |
| 75 | { | |
| 76 | 1 | var message:String = "It was not possible to get the correct asset type over the provided <src> argument OR the provided <src> argument contains an extension that is not supported.\n"; |
| 77 | 1 | message += "Provided src: <" + src + ">\n"; |
| 78 | 1 | message += "Provided id: <" + id + ">\n"; |
| 79 | 1 | message += "Final identification: <" + identification + ">\n"; |
| 80 | 1 | message += "For further information please read the documentation section about the supported Asset types."; |
| 81 | ||
| 82 | 1 | throw new UnsupportedAssetTypeError(identification, message); |
| 83 | } | |
| 84 | ||
| 85 | 1 | return instanciate(identification, src, type); |
| 86 | } | |
| 87 | ||
| 88 | /** | |
| 89 | * description | |
| 90 | * | |
| 91 | * @param settings | |
| 92 | * @throws ArgumentError if the <code>settings</code> argument is <code>null</code>. | |
| 93 | */ | |
| 94 | public function setUrlAssetParser(parser:UrlAssetParser): void | |
| 95 | { | |
| 96 | 0 | if (!parser) throw new ArgumentError("Argument <parser> must not be null."); |
| 97 | 0 | _urlAssetParser = parser; |
| 98 | 0 | } |
| 99 | ||
| 100 | /** | |
| 101 | * @private | |
| 102 | */ | |
| 103 | protected function instanciate(id:VostokIdentification, src:String, type:AssetType): Asset | |
| 104 | { | |
| 105 | 1 | return new Asset(id, src, type); |
| 106 | } | |
| 107 | ||
| 108 | /** | |
| 109 | * @private | |
| 110 | */ | |
| 111 | protected function createIdentification(src:String, assetPackage:AssetPackage, id:String = null): VostokIdentification | |
| 112 | { | |
| 113 | 1 | if (StringUtil.isBlank(id)) id = src; |
| 114 | 1 | var identification:VostokIdentification = new VostokIdentification(id, assetPackage.identification.locale); |
| 115 | ||
| 116 | 1 | return identification; |
| 117 | } | |
| 118 | ||
| 119 | /** | |
| 120 | * @private | |
| 121 | */ | |
| 122 | protected function getType(src:String): AssetType | |
| 123 | { | |
| 124 | 1 | return _urlAssetParser.getAssetType(src); |
| 125 | } | |
| 126 | ||
| 127 | private function createUrlAssetParser():UrlAssetParser | |
| 128 | { | |
| 129 | 1 | return new UrlAssetParser(); |
| 130 | } | |
| 131 | ||
| 132 | } | |
| 133 | ||
| 134 | } |