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

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.44    ! moko       16: volatile const char * IDENT_PA_VALUE_C="$Id: pa_value.C,v 1.43 2016/11/03 16:17:38 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:                Value *params[]={new VString(*new String(options.key, String::L_JSON)), options.params ? options.params : VVoid::get()};
                     59: 
1.43      moko       60:                METHOD_FRAME_ACTION(*method, options.r->method_frame, *this,{
                     61:                        frame.store_params(params, 2);
                     62:                        options.r->call(frame);
                     63:                        return &frame.result().as_string();
                     64:                });
1.36      misha      65:        } else {
                     66:                // specified as method-junction
                     67:                Junction* junction=default_method.get_junction();
                     68: 
                     69:                Value *params[]={new VString(*new String(options.key, String::L_JSON)), this, options.params ? options.params : VVoid::get()};
                     70: 
1.43      moko       71:                METHOD_FRAME_ACTION(*junction->method, options.r->method_frame, junction->self,{
                     72:                        frame.store_params(params, 3);
                     73:                        options.r->call(frame);
                     74:                        return &frame.result().as_string();
                     75:                });
1.36      misha      76:        }
                     77: }
                     78: 
1.1       paf        79: /// call this before invoking to ensure proper actual numbered params count
1.42      moko       80: void Method::check_actual_numbered_params(Value& self, MethodParams* actual_numbered_params) const {
                     81:        int actual_count=actual_numbered_params ? actual_numbered_params->count() : 0;
1.29      misha      82:        if(actual_count<min_numbered_params_count || actual_count>max_numbered_params_count)
1.44    ! moko       83:                throw Exception(PARSER_RUNTIME, name, "native method of '%s' accepts %s %d parameter(s) (%d present)", 
1.42      moko       84:                        self.type(),
                     85:                        actual_count<min_numbered_params_count ? "minimum" : "maximum",
                     86:                        actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
                     87:                        actual_count);
1.1       paf        88: }
1.4       paf        89: 
1.10      paf        90: // attributed meaning
                     91: 
1.42      moko       92: static void append_attribute_meaning(String& result, Value& value, String::Language lang, bool forced) {
1.10      paf        93:        if(const String* string=value.get_string())
                     94:                result.append(*string, lang, forced);
                     95:        else
1.28      misha      96:                if(Value* vdate=value.as(VDATE_TYPE)) {
1.38      moko       97:                        result << *static_cast<VDate&>(*vdate).get_gmt_string();
1.10      paf        98:                } else
1.38      moko       99:                        throw Exception(PARSER_RUNTIME, &result, "trying to append here neither string nor date (%s)", value.type());
1.10      paf       100: }
1.38      moko      101: 
1.10      paf       102: #ifndef DOXYGEN
                    103: struct Attributed_meaning_info {
1.31      misha     104:        String* header;        // header line being constructed
1.10      paf       105:        String::Language lang; // language in which to append to that line
1.31      misha     106:        bool forced;           // do they force that lang?
                    107:        bool allow_bool;       // allow bool types during print attributes
1.10      paf       108: };
                    109: #endif
1.42      moko      110: 
                    111: static void append_attribute_subattribute(HashStringValue::key_type akey, HashStringValue::value_type avalue, Attributed_meaning_info *info) {
1.10      paf       112:        if(akey==VALUE_NAME)
                    113:                return;
                    114: 
1.27      misha     115:        if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
                    116:                return;
                    117: 
1.10      paf       118:        // ...; charset=windows1251
                    119:        *info->header << "; ";
                    120:        info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.31      misha     121:        if(!avalue->is_bool()) {
                    122:                if( akey==content_disposition_filename_name ) {
                    123:                        *info->header << "=\"";
                    124:                        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    125:                        *info->header << "\"";
                    126:                } else {
                    127:                        *info->header << "=";
                    128:                        append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
                    129:                }
1.21      misha     130:        }
1.10      paf       131: }
1.42      moko      132: const String& attributed_meaning_to_string(Value& meaning, String::Language lang, bool forced, bool allow_bool) {
1.10      paf       133:        String& result=*new String;
                    134:        if(HashStringValue *hash=meaning.get_hash()) {
                    135:                // $value(value) $subattribute(subattribute value)
                    136:                if(Value* value=hash->get(value_name))
                    137:                        append_attribute_meaning(result, *value, lang, forced);
                    138: 
1.21      misha     139:                Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20      paf       140:                hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10      paf       141:        } else // result value
                    142:                append_attribute_meaning(result, meaning, lang, forced);
                    143: 
                    144:        return result;
                    145: }

E-mail: