package readers.text
{
        import mx.utils.StringUtil;
        
        public class Properties
        {
                public var values:Object;
                
                public function Properties(props:String = "")
                {
                        this.properties = props;
                }

                public function set properties(props:String):void
                {
                        values = Properties.parseProperties(props,values);
                }
                
                public function hasProperty(name:String):Boolean
                {
                        return values.hasOwnProperty(name);
                }
                
                public function getProperty(name:String):String
                {
                        if (values.hasOwnProperty(name))
                                return values[name];
                        
                        return null;
                }
                
                public function serialize():String
                {
                        return Properties.serializeProperties(values);
                }
                
                public static function serializeProperties(values:Object):String
                {
                        var serialized:String = "";
                        
                        if (values == null)
                                return serialized;
                        
                        for (var property:String in values)
                        {
                                serialized += property + "=" + 
String(values[property]) + "\n";
                        }
                        
                        return serialized;
                }

                public static function 
parseProperties(properties:String,values:Object = null):Object
                {
                        if (values == null)
                                values = {};
                        
                        if (properties == null || properties == "")
                                return values;
                                
                        var lines:Array = properties.split("\n");
                        
                        for each (var line:String in lines)
                        {
                                if (line != "")
                                {
                                        var parts:Array = line.split("=");
                                        
                                        var property:String = parts[0];
                                        var value:String = (parts.length > 1) ? 
parts[1] : "";
                                        
                                        if (property.charAt(0) != "#" && 
property.length > 0)
                                                values[property] = 
StringUtil.trim(value);
                                }
                        }
                        
                        return values;
                }
                
        }
}

Reply via email to