Coverage Report - org.vostokframework.configuration.parsers.xml.AssetXMLNodeParser
 
Classes in this File Line Coverage Branch Coverage Complexity
AssetXMLNodeParser
100%
29/29
N/A
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.configuration.parsers.xml
 30  
 {
 31  
         import org.as3collections.IList;
 32  
         import org.as3collections.IMap;
 33  
         import org.as3collections.lists.ArrayList;
 34  
         import org.as3collections.maps.HashMap;
 35  
         import org.as3collections.utils.ListUtil;
 36  
         import org.as3collections.utils.MapUtil;
 37  
         import org.vostokframework.configuration.AssetConfiguration;
 38  
         import org.vostokframework.configuration.parsers.xml.errors.XMLConfigurationParserError;
 39  
         import org.vostokframework.domain.assets.AssetType;
 40  
         import org.vostokframework.domain.loading.settings.LoadingSettings;
 41  
 
 42  
         /**
 43  
          * description
 44  
          * 
 45  
          * @author Flávio Silva
 46  
          */
 47  
         public class AssetXMLNodeParser
 48  
         {
 49  
                 
 50  
                 private static const ASSET_ID_ATTRIBUTE_NAME:String = "id";
 51  
                 private static const ASSET_SRC_ATTRIBUTE_NAME:String = "src";
 52  
                 private static const ASSET_TYPE_ATTRIBUTE_NAME:String = "type";
 53  
                 private static const SETTINGS_NODE_NAME:String = "settings";
 54  
                 
 55  
                 private var _settingsParser:LoadingSettingsXMLNodeParser;
 56  
                 
 57  
                 /**
 58  
                  * description
 59  
                  */
 60  
                 public function AssetXMLNodeParser(settingsParser:LoadingSettingsXMLNodeParser)
 61  1
                 {
 62  1
                         _settingsParser = settingsParser;
 63  1
                 }
 64  
                 
 65  
                 public function parse(assets:XMLList):IList
 66  
                 {
 67  1
                         if (!assets) return null;
 68  
                         
 69  1
                         var $assets:IList = parseAssets(assets);
 70  1
                         return $assets;
 71  
                 }
 72  
                 
 73  
                 private function parseAssets(assetsXmlList:XMLList):IList
 74  
                 {
 75  1
                         if (!assetsXmlList) return null;
 76  
                         
 77  1
                         var assets:IList = ListUtil.getTypedList(new ArrayList(), AssetConfiguration);
 78  
                         var assetConfiguration:AssetConfiguration;
 79  
                         var assetAttributesMap:IMap;
 80  
                         var id:String;
 81  
                         var src:String;
 82  
                         var type:AssetType;
 83  1
                         var settingsNode:XML = <settings></settings>;
 84  
                         var settingsXMLList:XMLList;
 85  
                         var settings:LoadingSettings;
 86  
                         
 87  1
                         for each (var assetNode:XML in assetsXmlList)
 88  
                         {
 89  1
                                 assetAttributesMap = new HashMap();
 90  1
                                 MapUtil.feedMapWithXmlList(assetAttributesMap, assetNode.attributes());
 91  
                                 
 92  1
                                 if (!assetAttributesMap.containsKey(ASSET_SRC_ATTRIBUTE_NAME))
 93  
                                 {
 94  1
                                         var errorMessage:String = "Asset node must have < " + ASSET_SRC_ATTRIBUTE_NAME + "> attribute.";
 95  1
                                         throw new XMLConfigurationParserError(errorMessage);
 96  
                                 }
 97  
                                 
 98  1
                                 id = assetAttributesMap.getValue(ASSET_ID_ATTRIBUTE_NAME);
 99  1
                                 src = assetAttributesMap.getValue(ASSET_SRC_ATTRIBUTE_NAME);
 100  1
                                 if (assetAttributesMap.containsKey(ASSET_TYPE_ATTRIBUTE_NAME))
 101  
                                 {
 102  1
                                         type = AssetType.getByName(assetAttributesMap.getValue(ASSET_TYPE_ATTRIBUTE_NAME));
 103  
                                 }
 104  
                                 
 105  
                                 //convert attributes to nodes and append into settings
 106  1
                                 settingsXMLList = assetNode.attributes();
 107  1
                                 for each(var att:XML in settingsXMLList)
 108  
                                 {
 109  1
                                         settingsNode.appendChild(<{att.localName()}>{att.toString()}</{att.localName()}>);
 110  
                                 }
 111  
                                 //
 112  
                                 
 113  
                                 //merge settings nodes together with attributes settings
 114  1
                                 settingsXMLList = assetNode.children().(name().localName == SETTINGS_NODE_NAME).children();
 115  1
                                 settingsNode.appendChild(settingsXMLList);
 116  
                                 //
 117  
                                 
 118  1
                                 settings = _settingsParser.parse(settingsNode);
 119  
                                 
 120  1
                                 assetConfiguration = new AssetConfiguration(src, type, settings, id);
 121  1
                                 assets.add(assetConfiguration);
 122  
                         }
 123  
                         
 124  1
                         return assets;
 125  
                 }
 126  
                 
 127  
         }
 128  
 
 129  
 }