Annotation of parser3/src/types/pa_value.C, revision 1.40
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.40 ! moko 16: volatile const char * IDENT_PA_VALUE_C="$Id: pa_value.C,v 1.39 2015/10/26 01:22:00 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.11 paf 31: return bark("is '%s', it has no elements");
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.10 paf 81: void Method::check_actual_numbered_params(Value& self,
1.28 misha 82: MethodParams* actual_numbered_params) const {
1.10 paf 83: int actual_count=actual_numbered_params?actual_numbered_params->count():0;
1.29 misha 84: if(actual_count<min_numbered_params_count || actual_count>max_numbered_params_count)
1.22 misha 85: throw Exception(PARSER_RUNTIME,
1.10 paf 86: 0,
1.40 ! moko 87: "native method of %s accepts %s %d parameter(s) (%d present)",
1.8 paf 88: self.type(),
1.29 misha 89: actual_count<min_numbered_params_count ? "minimum" : "maximum",
90: actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
1.1 paf 91: actual_count);
92: }
1.4 paf 93:
1.10 paf 94: // attributed meaning
95:
96: static void append_attribute_meaning(String& result,
1.28 misha 97: Value& value, String::Language lang, bool forced) {
1.10 paf 98: if(const String* string=value.get_string())
99: result.append(*string, lang, forced);
100: else
1.28 misha 101: if(Value* vdate=value.as(VDATE_TYPE)) {
1.38 moko 102: result << *static_cast<VDate&>(*vdate).get_gmt_string();
1.10 paf 103: } else
1.38 moko 104: throw Exception(PARSER_RUNTIME, &result, "trying to append here neither string nor date (%s)", value.type());
1.10 paf 105: }
1.38 moko 106:
1.10 paf 107: #ifndef DOXYGEN
108: struct Attributed_meaning_info {
1.31 misha 109: String* header; // header line being constructed
1.10 paf 110: String::Language lang; // language in which to append to that line
1.31 misha 111: bool forced; // do they force that lang?
112: bool allow_bool; // allow bool types during print attributes
1.10 paf 113: };
114: #endif
115: static void append_attribute_subattribute(HashStringValue::key_type akey,
1.31 misha 116: HashStringValue::value_type avalue,
117: Attributed_meaning_info *info) {
1.10 paf 118: if(akey==VALUE_NAME)
119: return;
120:
1.27 misha 121: if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
122: return;
123:
1.10 paf 124: // ...; charset=windows1251
125: *info->header << "; ";
126: info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.31 misha 127: if(!avalue->is_bool()) {
128: if( akey==content_disposition_filename_name ) {
129: *info->header << "=\"";
130: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
131: *info->header << "\"";
132: } else {
133: *info->header << "=";
134: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
135: }
1.21 misha 136: }
1.10 paf 137: }
138: const String& attributed_meaning_to_string(Value& meaning,
1.31 misha 139: String::Language lang, bool forced, bool allow_bool) {
1.10 paf 140: String& result=*new String;
141: if(HashStringValue *hash=meaning.get_hash()) {
142: // $value(value) $subattribute(subattribute value)
143: if(Value* value=hash->get(value_name))
144: append_attribute_meaning(result, *value, lang, forced);
145:
1.21 misha 146: Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20 paf 147: hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10 paf 148: } else // result value
149: append_attribute_meaning(result, meaning, lang, forced);
150:
151: return result;
152: }
E-mail: