Annotation of parser3/src/types/pa_value.C, revision 1.35
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"
13:
1.35 ! moko 14: volatile const char * IDENT_PA_VALUE_C="$Id: pa_value.C,v 1.34 2012-04-18 21:42:51 moko Exp $" IDENT_PA_VALUE_H IDENT_PA_PROPERTY_H;
1.32 moko 15:
1.10 paf 16: // globals
17:
18: const String name_name(NAME_NAME);
19:
20: const String value_name(VALUE_NAME);
1.12 paf 21: const String expires_name(EXPIRES_NAME);
1.10 paf 22: const String content_type_name(CONTENT_TYPE_NAME);
23:
24: // methods
25:
26: Junction* Value::get_junction() { return 0; }
27:
1.28 misha 28: Value* Value::get_element(const String& /*aname*/) {
1.11 paf 29: return bark("is '%s', it has no elements");
1.10 paf 30: }
31:
32: VFile* Value::as_vfile(String::Language /*lang*/, const Request_charsets* /*charsets*/) {
1.11 paf 33: bark("is '%s', it does not have file value"); return 0;
1.10 paf 34: }
1.1 paf 35:
1.35 ! moko 36: const String* Value::get_json_string(Json_options& options) {
! 37: if(HashStringValue* hash=get_hash())
! 38: return options.hash_json_string(*hash);
! 39:
! 40: if(!options.skip_unknown)
1.30 misha 41: throw Exception(PARSER_RUNTIME, 0, "Unsupported value's type (%s)", type());
1.35 ! moko 42:
1.30 misha 43: return new String("null");
44: }
45:
1.1 paf 46: /// call this before invoking to ensure proper actual numbered params count
1.10 paf 47: void Method::check_actual_numbered_params(Value& self,
1.28 misha 48: MethodParams* actual_numbered_params) const {
1.10 paf 49: int actual_count=actual_numbered_params?actual_numbered_params->count():0;
1.29 misha 50: if(actual_count<min_numbered_params_count || actual_count>max_numbered_params_count)
1.22 misha 51: throw Exception(PARSER_RUNTIME,
1.10 paf 52: 0,
1.29 misha 53: "native method of %s (%s) accepts %s %d parameter(s) (%d present)",
1.8 paf 54: self.get_class()->name_cstr(),
55: self.type(),
1.29 misha 56: actual_count<min_numbered_params_count ? "minimum" : "maximum",
57: actual_count<min_numbered_params_count ? min_numbered_params_count : max_numbered_params_count,
1.1 paf 58: actual_count);
59: }
1.4 paf 60:
1.10 paf 61: // attributed meaning
62:
63: static String::C date_attribute(const VDate& vdate) {
64: time_t when=vdate.get_time();
65: struct tm *tms=gmtime(&when);
66: if(!tms)
1.25 misha 67: throw Exception(DATE_RANGE_EXCEPTION_TYPE,
1.10 paf 68: 0,
1.15 paf 69: "bad time in attribute value (seconds from epoch=%u)", when);
1.24 misha 70: return date_gmt_string(tms);
71: }
1.10 paf 72:
73: static void append_attribute_meaning(String& result,
1.28 misha 74: Value& value, String::Language lang, bool forced) {
1.10 paf 75: if(const String* string=value.get_string())
76: result.append(*string, lang, forced);
77: else
1.28 misha 78: if(Value* vdate=value.as(VDATE_TYPE)) {
1.10 paf 79: String::C attribute=date_attribute(static_cast<VDate&>(*vdate));
80:
81: result.append_help_length(attribute.str, attribute.length, String::L_CLEAN);
82: } else
1.22 misha 83: throw Exception(PARSER_RUNTIME,
1.10 paf 84: &result,
85: "trying to append here neither string nor date (%s)",
86: value.type());
87: }
88: #ifndef DOXYGEN
89: struct Attributed_meaning_info {
1.31 misha 90: String* header; // header line being constructed
1.10 paf 91: String::Language lang; // language in which to append to that line
1.31 misha 92: bool forced; // do they force that lang?
93: bool allow_bool; // allow bool types during print attributes
1.10 paf 94: };
95: #endif
96: static void append_attribute_subattribute(HashStringValue::key_type akey,
1.31 misha 97: HashStringValue::value_type avalue,
98: Attributed_meaning_info *info) {
1.10 paf 99: if(akey==VALUE_NAME)
100: return;
101:
1.27 misha 102: if(avalue->is_bool() && (!info->allow_bool || avalue->as_bool()==false))
103: return;
104:
1.10 paf 105: // ...; charset=windows1251
106: *info->header << "; ";
107: info->header->append(String(akey, String::L_TAINTED), info->lang, info->forced);
1.31 misha 108: if(!avalue->is_bool()) {
109: if( akey==content_disposition_filename_name ) {
110: *info->header << "=\"";
111: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
112: *info->header << "\"";
113: } else {
114: *info->header << "=";
115: append_attribute_meaning(*info->header, *avalue, info->lang, info->forced);
116: }
1.21 misha 117: }
1.10 paf 118: }
119: const String& attributed_meaning_to_string(Value& meaning,
1.31 misha 120: String::Language lang, bool forced, bool allow_bool) {
1.10 paf 121: String& result=*new String;
122: if(HashStringValue *hash=meaning.get_hash()) {
123: // $value(value) $subattribute(subattribute value)
124: if(Value* value=hash->get(value_name))
125: append_attribute_meaning(result, *value, lang, forced);
126:
1.21 misha 127: Attributed_meaning_info attributed_meaning_info={&result, lang, false, allow_bool};
1.20 paf 128: hash->for_each<Attributed_meaning_info*>(append_attribute_subattribute, &attributed_meaning_info);
1.10 paf 129: } else // result value
130: append_attribute_meaning(result, meaning, lang, forced);
131:
132: return result;
133: }
E-mail: