Annotation of parser3/src/types/pa_value.C, revision 1.37
1.1 paf 1: /** @file
2: Parser: Value class.
3:
1.32 moko 4: Copyright (c) 2001-2012 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.37 ! moko 16: volatile const char * IDENT_PA_VALUE_C="$Id: pa_value.C,v 1.36 2015/03/17 07:28:43 misha 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.29 misha 87: "native method of %s (%s) accepts %s %d parameter(s) (%d present)",
1.8 paf 88: self.get_class()->name_cstr(),
89: self.type(),
1.29 misha 90: actual_count<min_numbered_params_count ? "minimum" : "maximum",
91: actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
1.1 paf 92: actual_count);
93: }
1.4 paf 94:
1.10 paf 95: // attributed meaning
96:
97: static String::C date_attribute(const VDate& vdate) {
98: time_t when=vdate.get_time();
99: struct tm *tms=gmtime(&when);
100: if(!tms)
1.25 misha 101: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.10 paf 102: 0,
1.15 paf 103: "bad time in attribute value (seconds from epoch=%u)", when);
1.24 misha 104: return date_gmt_string(tms);
105: }
1.10 paf 106:
107: static void append_attribute_meaning(String& result,
1.28 misha 108: Value& value, String::Language lang, bool forced) {
1.10 paf 109: if(const String* string=value.get_string())
110: result.append(*string, lang, forced);
111: else
1.28 misha 112: if(Value* vdate=value.as(VDATE_TYPE)) {
1.10 paf 113: String::C attribute=date_attribute(static_cast<VDate&>(*vdate));
114:
115: result.append_help_length(attribute.str, attribute.length, String::L_CLEAN);
116: } else
1.22 misha 117: throw Exception(PARSER_RUNTIME,
1.10 paf 118: &result,
119: "trying to append here neither string nor date (%s)",
120: value.type());
121: }
122: #ifndef DOXYGEN
123: struct Attributed_meaning_info {
1.31 misha 124: String* header; // header line being constructed
1.10 paf 125: String::Language lang; // language in which to append to that line
1.31 misha 126: bool forced; // do they force that lang?
127: bool allow_bool; // allow bool types during print attributes
1.10 paf 128: };
129: #endif
130: static void append_attribute_subattribute(HashStringValue::key_type akey,
1.31 misha 131: HashStringValue::value_type avalue,
132: Attributed_meaning_info *info) {
1.10 paf 133: if(akey==VALUE_NAME)
134: return;
135:
1.27 misha 136: if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
137: return;
138:
1.10 paf 139: // ...; charset=windows1251
140: *info->header << "; ";
141: info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.31 misha 142: if(!avalue->is_bool()) {
143: if( akey==content_disposition_filename_name ) {
144: *info->header << "=\"";
145: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
146: *info->header << "\"";
147: } else {
148: *info->header << "=";
149: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
150: }
1.21 misha 151: }
1.10 paf 152: }
153: const String& attributed_meaning_to_string(Value& meaning,
1.31 misha 154: String::Language lang, bool forced, bool allow_bool) {
1.10 paf 155: String& result=*new String;
156: if(HashStringValue *hash=meaning.get_hash()) {
157: // $value(value) $subattribute(subattribute value)
158: if(Value* value=hash->get(value_name))
159: append_attribute_meaning(result, *value, lang, forced);
160:
1.21 misha 161: Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20 paf 162: hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10 paf 163: } else // result value
164: append_attribute_meaning(result, meaning, lang, forced);
165:
166: return result;
167: }
E-mail: