Annotation of parser3/src/types/pa_value.C, revision 1.42

1.1       paf         1: /** @file
                      2:        Parser: Value class.
                      3: 
1.39      moko        4:        Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.2       paf         6: */
1.1       paf         7: 
                      8: #include "pa_value.h"
                      9: #include "pa_vstateless_class.h"
1.4       paf        10: #include "pa_vmethod_frame.h"
1.10      paf        11: #include "pa_vdate.h"
                     12: #include "pa_vobject.h"
1.36      misha      13: #include "pa_request.h"
1.10      paf        14: 
1.36      misha      15: 
1.42    ! moko       16: volatile const char * IDENT_PA_VALUE_C="$Id: pa_value.C,v 1.41 2016/05/18 17:47:22 moko Exp $" IDENT_PA_VALUE_H IDENT_PA_PROPERTY_H;
1.32      moko       17: 
1.10      paf        18: // globals
                     19: 
                     20: const String name_name(NAME_NAME);
                     21: 
                     22: const String value_name(VALUE_NAME);
1.12      paf        23: const String expires_name(EXPIRES_NAME);
1.10      paf        24: const String content_type_name(CONTENT_TYPE_NAME);
                     25: 
                     26: // methods
                     27: 
                     28: Junction* Value::get_junction() { return 0; }
                     29: 
1.28      misha      30: Value* Value::get_element(const String& /*aname*/) {
1.41      moko       31:        return bark("element can not be fetched from %s");
1.10      paf        32: }
                     33: 
                     34: VFile* Value::as_vfile(String::Language /*lang*/, const Request_charsets* /*charsets*/) { 
1.11      paf        35:        bark("is '%s', it does not have file value"); return 0;
1.10      paf        36: }
1.1       paf        37: 
1.35      moko       38: const String* Value::get_json_string(Json_options& options) {
                     39:        if(HashStringValue* hash=get_hash())
1.37      moko       40:                return options.hash_json_string(hash);
1.35      moko       41: 
                     42:        if(!options.skip_unknown)
1.30      misha      43:                throw Exception(PARSER_RUNTIME, 0, "Unsupported value's type (%s)", type());
1.35      moko       44: 
1.30      misha      45:        return new String("null");
                     46: }
                     47: 
1.36      misha      48: const String* Value::default_method_2_json_string(Value& default_method, Json_options& options){
                     49:        if(default_method.is_string()){
                     50:                // specified as string with method name
                     51:                const String& method_name=*default_method.get_string();
                     52:                Method* method=this->get_class()->get_method(method_name);
                     53:                if(!method) {
                     54:                        // class/object does not have method with specified name so serialize it as hash (default)
1.37      moko       55:                        return options.hash_json_string(get_hash());
1.36      misha      56:                }
                     57: 
                     58:                VMethodFrame frame(*method, options.r->method_frame, *this);
                     59: 
                     60:                Value *params[]={new VString(*new String(options.key, String::L_JSON)), options.params ? options.params : VVoid::get()};
                     61:                frame.store_params(params, 2);
                     62: 
                     63:                options.r->execute_method(frame);
                     64: 
                     65:                return &frame.result().as_string();
                     66:        } else {
                     67:                // specified as method-junction
                     68:                Junction* junction=default_method.get_junction();
                     69:                VMethodFrame frame(*junction->method, options.r->method_frame, junction->self);
                     70: 
                     71:                Value *params[]={new VString(*new String(options.key, String::L_JSON)), this, options.params ? options.params : VVoid::get()};
                     72:                frame.store_params(params, 3);
                     73: 
                     74:                options.r->execute_method(frame);
                     75: 
                     76:                return &frame.result().as_string();
                     77:        }
                     78: }
                     79: 
1.1       paf        80: /// call this before invoking to ensure proper actual numbered params count
1.42    ! moko       81: void Method::check_actual_numbered_params(Value& self, MethodParams* actual_numbered_params) const {
        !            82:        int actual_count=actual_numbered_params ? actual_numbered_params->count() : 0;
1.29      misha      83:        if(actual_count<min_numbered_params_count || actual_count>max_numbered_params_count)
1.42    ! moko       84:                throw Exception(PARSER_RUNTIME, 0, "native method of %s accepts %s %d parameter(s) (%d present)", 
        !            85:                        self.type(),
        !            86:                        actual_count<min_numbered_params_count ? "minimum" : "maximum",
        !            87:                        actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
        !            88:                        actual_count);
1.1       paf        89: }
1.4       paf        90: 
1.10      paf        91: // attributed meaning
                     92: 
1.42    ! moko       93: static void append_attribute_meaning(String& result, Value& value, String::Language lang, bool forced) {
1.10      paf        94:        if(const String* string=value.get_string())
                     95:                result.append(*string, lang, forced);
                     96:        else
1.28      misha      97:                if(Value* vdate=value.as(VDATE_TYPE)) {
1.38      moko       98:                        result << *static_cast<VDate&>(*vdate).get_gmt_string();
1.10      paf        99:                } else
1.38      moko      100:                        throw Exception(PARSER_RUNTIME, &result, "trying to append here neither string nor date (%s)", value.type());
1.10      paf       101: }
1.38      moko      102: 
1.10      paf       103: #ifndef DOXYGEN
                    104: struct Attributed_meaning_info {
1.31      misha     105:        String* header;        // header line being constructed
1.10      paf       106:        String::Language lang; // language in which to append to that line
1.31      misha     107:        bool forced;           // do they force that lang?
                    108:        bool allow_bool;       // allow bool types during print attributes
1.10      paf       109: };
                    110: #endif
1.42    ! moko      111: 
        !           112: static void append_attribute_subattribute(HashStringValue::key_type akey, HashStringValue::value_type avalue, Attributed_meaning_info *info) {
1.10      paf       113:        if(akey==VALUE_NAME)
                    114:                return;
                    115: 
1.27      misha     116:        if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
                    117:                return;
                    118: 
1.10      paf       119:        // ...; charset=windows1251
                    120:        *info->header << "; ";
                    121:        info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.31      misha     122:        if(!avalue->is_bool()) {
                    123:                if( akey==content_disposition_filename_name ) {
                    124:                        *info->header << "=\"";
                    125:                        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    126:                        *info->header << "\"";
                    127:                } else {
                    128:                        *info->header << "=";
                    129:                        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    130:                }
1.21      misha     131:        }
1.10      paf       132: }
1.42    ! moko      133: const String& attributed_meaning_to_string(Value& meaning, String::Language lang, bool forced, bool allow_bool) {
1.10      paf       134:        String& result=*new String;
                    135:        if(HashStringValue *hash=meaning.get_hash()) {
                    136:                // $value(value) $subattribute(subattribute value)
                    137:                if(Value* value=hash->get(value_name))
                    138:                        append_attribute_meaning(result, *value, lang, forced);
                    139: 
1.21      misha     140:                Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20      paf       141:                hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10      paf       142:        } else // result value
                    143:                append_attribute_meaning(result, meaning, lang, forced);
                    144: 
                    145:        return result;
                    146: }

E-mail: