1 /* Fo
2  * fo-property-language.c: 'language' property
3  *
4  * Copyright (C) 2001-2006 Sun Microsystems
5  * Copyright (C) 2007-2010 Menteith Consulting Ltd
6  * Copyright (C) 2011 Mentea
7  *
8  * See COPYING for the status of this software.
9  */
10 
11 #include <string.h>
12 #include "fo-utils.h"
13 #include "fo-context.h"
14 #include "datatype/fo-datatype.h"
15 #include "property/fo-property-private.h"
16 #include "property/fo-property-font-size.h"
17 #include "property/fo-property-language.h"
18 
19 /**
20  * SECTION:fo-property-language
21  * @short_description: 'language' property
22  *
23  * Inherited: TRUE
24  *
25  * Shorthand: FALSE
26  *
27  * Value: none | &lt;language> | inherit
28  *
29  * Initial value: none
30  *
31  * Definition: <ulink url="http://www.w3.org/TR/xsl11/&num;language">http://www.w3.org/TR/xsl11/&num;language</ulink>
32  */
33 
34 struct _FoPropertyLanguage
35 {
36   FoProperty parent_instance;
37 };
38 
39 struct _FoPropertyLanguageClass
40 {
41   FoPropertyClass parent_class;
42 };
43 
44 static void _init         (FoPropertyLanguage      *property_language);
45 static void _class_init   (FoPropertyLanguageClass *klass);
46 
47 static FoDatatype * _resolve_enum (const gchar *token,
48                                    FoContext   *context,
49                                    GError     **error);
50 static FoDatatype * _validate     (FoDatatype  *datatype,
51                                    FoContext   *context,
52                                    GError     **error);
53 
54 static const gchar class_name[] = "language";
55 static gpointer parent_class;
56 
57 /**
58  * fo_property_language_get_type:
59  *
60  * Register the #FoPropertyLanguage type if not already registered and
61  * return its #GType value.
62  *
63  * Return value: #GType of #FoPropertyLanguage.
64  **/
65 GType
fo_property_language_get_type(void)66 fo_property_language_get_type (void)
67 {
68   static GType object_type = 0;
69 
70   if (!object_type)
71     {
72       static const GTypeInfo object_info =
73       {
74         sizeof (FoPropertyLanguageClass),
75         NULL,           /* base_init */
76         NULL,           /* base_finalize */
77         (GClassInitFunc) _class_init,
78         NULL,           /* class_finalize */
79         NULL,           /* class_data */
80         sizeof (FoPropertyLanguage),
81         0,              /* n_preallocs */
82         (GInstanceInitFunc) _init,
83 	NULL		/* value_table */
84       };
85 
86       object_type = g_type_register_static (FO_TYPE_PROPERTY,
87                                             class_name,
88                                             &object_info, 0);
89     }
90 
91   return object_type;
92 }
93 
94 /**
95  * _init:
96  * @language: #FoPropertyLanguage object to initialise.
97  *
98  * Implements #GInstanceInitFunc for #FoPropertyLanguage.
99  **/
100 static void
_init(FoPropertyLanguage * language)101 _init (FoPropertyLanguage *language)
102 {
103   FO_PROPERTY (language)->value =
104     g_object_ref (fo_enum_factory_get_enum_by_value (FO_ENUM_ENUM_NONE));
105 }
106 
107 /**
108  * _class_init:
109  * @klass: #FoPropertyLanguageClass object to initialise.
110  *
111  * Implements #GClassInitFunc for #FoPropertyLanguageClass.
112  **/
113 static void
_class_init(FoPropertyLanguageClass * klass)114 _class_init (FoPropertyLanguageClass *klass)
115 {
116   GObjectClass *object_class = G_OBJECT_CLASS (klass);
117   FoPropertyClass *property_class = FO_PROPERTY_CLASS (klass);
118 
119   parent_class = g_type_class_peek_parent (klass);
120 
121 
122   property_class->is_inherited = TRUE;
123   property_class->is_shorthand = FALSE;
124   property_class->resolve_enum =
125     _resolve_enum;
126   property_class->validate =
127     _validate;
128   property_class->get_initial =
129     fo_property_language_get_initial;
130 }
131 
132 
133 /**
134  * fo_property_language_new:
135  *
136  * Creates a new #FoPropertyLanguage initialized to default value.
137  *
138  * Return value: the new #FoPropertyLanguage.
139  **/
140 FoProperty*
fo_property_language_new(void)141 fo_property_language_new (void)
142 {
143   FoProperty* language;
144 
145   language =
146     FO_PROPERTY (g_object_new (fo_property_language_get_type (),
147                                NULL));
148 
149   return language;
150 }
151 
152 /**
153  * _resolve_enum:
154  * @token:   Token from the XML attribute value to be evaluated as an
155  *           enumeration token.
156  * @context: #FoContext object from which to possibly inherit values.
157  * @error:   Information about any error that has occurred.
158  *
159  * Compare @token against the enumeration tokens that are valid for the
160  * current FO property.  If @token is valid, returns either an #FoEnum datatype
161  * representing the enumeration token or a different datatype representing
162  * the enumeration token's resolved value.  If @token is not valid,
163  * sets @error and returns NULL.
164  *
165  * Return value: Resolved enumeration value or NULL.
166  **/
167 static FoDatatype *
_resolve_enum(const gchar * token,FoContext * context,GError ** error)168 _resolve_enum (const gchar *token,
169                FoContext   *context,
170                GError     **error)
171 {
172   g_return_val_if_fail (token != NULL, NULL);
173   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
174   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
175 
176   if ((strcmp (token, "none") == 0))
177     {
178       return g_object_ref (fo_enum_factory_get_enum_by_nick (token));
179     }
180   else
181     {
182       g_set_error (error,
183 		   FO_FO_ERROR,
184 		   FO_FO_ERROR_ENUMERATION_TOKEN,
185 		   _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
186 		   class_name,
187 		   token);
188       return NULL;
189     }
190 }
191 
192 /**
193  * _validate:
194  * @datatype: #FoDatatype to be validated against allowed datatypes and
195  *            values for current property.
196  * @context:  #FoContext object from which to possibly inherit values.
197  * @error:    Information about any error that has occurred.
198  *
199  * Validates @datatype against allowed values.  Returns @datatype, a
200  * replacement datatype value, or NULL if validation failed.
201  *
202  * Return value: Valid datatype value or NULL.
203  **/
204 FoDatatype*
_validate(FoDatatype * datatype,FoContext * context,GError ** error)205 _validate (FoDatatype *datatype,
206            FoContext  *context,
207            GError    **error)
208 {
209   FoDatatype *new_datatype;
210   GError     *tmp_error = NULL;
211   gchar      *token;
212 
213   g_return_val_if_fail (datatype != NULL, NULL);
214   g_return_val_if_fail (FO_IS_DATATYPE (datatype), NULL);
215   g_return_val_if_fail (context != NULL, NULL);
216   g_return_val_if_fail (FO_IS_CONTEXT (context), NULL);
217   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
218 
219   if (FO_IS_ENUM (datatype))
220     {
221       FoEnumEnum value = fo_enum_get_value (datatype);
222 
223       if ((value == FO_ENUM_ENUM_NONE))
224 	{
225 	  return datatype;
226 	}
227       else
228 	{
229 	  gchar *datatype_sprintf = fo_object_sprintf (datatype);
230 
231 	  g_set_error (error,
232 		       FO_FO_ERROR,
233 		       FO_FO_ERROR_ENUMERATION_TOKEN,
234 		       _(fo_fo_error_messages[FO_FO_ERROR_ENUMERATION_TOKEN]),
235 		       class_name,
236 		       datatype_sprintf,
237 		       g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
238 
239 	  g_object_unref (datatype);
240 
241 	  g_free (datatype_sprintf);
242 
243 	  return NULL;
244 	}
245     }
246   else if (FO_IS_STRING (datatype))
247     {
248       token = fo_string_get_value (datatype);
249 
250       new_datatype =
251         _resolve_enum (token, context, &tmp_error);
252 
253       g_object_unref (datatype);
254 
255       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
256 
257       return new_datatype;
258     }
259   else if (FO_IS_NAME (datatype))
260     {
261       token = fo_name_get_value (datatype);
262 
263       new_datatype =
264         _resolve_enum (token, context, &tmp_error);
265 
266       g_object_unref (datatype);
267 
268       fo_propagate_and_return_val_if_error (error, tmp_error, NULL);
269 
270       return new_datatype;
271     }
272   else if (FO_IS_STRING (datatype))
273     {
274       return datatype;
275     }
276   else
277     {
278       gchar *datatype_sprintf = fo_object_sprintf (datatype);
279 
280       g_set_error (error,
281 		   FO_FO_ERROR,
282 		   FO_FO_ERROR_DATATYPE,
283 		   _(fo_fo_error_messages[FO_FO_ERROR_DATATYPE]),
284 		   class_name,
285 		   datatype_sprintf,
286 		   g_type_name (G_TYPE_FROM_INSTANCE (datatype)));
287 
288       g_object_unref (datatype);
289 
290       g_free (datatype_sprintf);
291 
292       return NULL;
293     }
294 }
295 
296 /**
297  * fo_property_language_get_initial:
298  *
299  * Get an instance of the property with the correct initial value.
300  *
301  * Return value: An instance of the property.
302  **/
303 FoProperty*
fo_property_language_get_initial(void)304 fo_property_language_get_initial (void)
305 {
306   static FoProperty *language = NULL;
307 
308   if (language == NULL)
309     {
310       language =
311 	fo_property_language_new ();
312     }
313 
314   return language;
315 }
316