Annotation of Amaya/amaya/templateUtils.c, revision 1.28
1.9 vatton 1: /*
2: *
1.18 vatton 3: * COPYRIGHT INRIA and W3C, 2006-2008
1.9 vatton 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
7:
1.3 quint 8: #include "templates.h"
1.14 kia 9: #include "Templatename.h"
10: #include "templates_f.h"
1.1 francesc 11:
1.21 kia 12: #include "AHTURLTools_f.h"
13: #include "HTMLsave_f.h"
14:
15:
1.19 kia 16: #include <stdarg.h>
17:
1.1 francesc 18: /*----------------------------------------------------------------------
19: GetSchemaFromDocType: Returns the name of the schema corresponding to
20: a doc type.
21: ----------------------------------------------------------------------*/
1.24 kia 22: const char *GetSchemaFromDocType (DocumentType docType)
1.1 francesc 23: {
24: #ifdef TEMPLATES
25: switch (docType)
26: {
27: case docAnnot :
28: return "Annot";
29: case docBookmark :
30: return "Topics";
31: case docSVG :
32: return "SVG";
33: case docMath :
34: return "MathML";
35: case docXml :
36: return "XML";
37: default :
38: return "HTML";
39: }
40: #endif // TEMPLATES
41: return "HTML";
42: }
43:
1.5 kia 44: /*----------------------------------------------------------------------
45: Set the value of a string attribute
46: ----------------------------------------------------------------------*/
1.24 kia 47: void SetAttributeStringValue (Element el, int att, const char* value)
1.5 kia 48: {
49: #ifdef TEMPLATES
1.7 vatton 50: Document doc = TtaGetDocument(el);
51: AttributeType attType;
52: Attribute attribute;
1.5 kia 53:
1.10 kia 54: if (doc == 0 || !TtaGetDocumentAccessMode(doc))
1.9 vatton 55: return;
1.5 kia 56: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
57: attType.AttrTypeNum = att;
1.7 vatton 58: attribute = TtaGetAttribute(el, attType);
59: if (attribute == NULL)
60: {
61: attribute = TtaNewAttribute (attType);
62: TtaAttachAttribute(el, attribute, doc);
63: }
1.5 kia 64: TtaSetAttributeText(attribute, value, el, doc);
65: #endif /* TEMPLATES */
66: }
67:
1.11 kia 68: /*----------------------------------------------------------------------
69: Set the value of a string attribute and registering it in undo sequence.
70: ----------------------------------------------------------------------*/
71: void SetAttributeStringValueWithUndo (Element el, int att, char* value)
72: {
73: #ifdef TEMPLATES
74: Document doc = TtaGetDocument(el);
75: AttributeType attType;
76: Attribute attribute;
77:
78: if (doc == 0 || !TtaGetDocumentAccessMode(doc))
79: return;
80: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
81: attType.AttrTypeNum = att;
82: attribute = TtaGetAttribute(el, attType);
83: if (attribute == NULL)
84: {
85: attribute = TtaNewAttribute (attType);
86: TtaAttachAttribute(el, attribute, doc);
87: TtaRegisterAttributeCreate(attribute, el, doc);
88: }
89: TtaSetAttributeText(attribute, value, el, doc);
90: TtaRegisterAttributeReplace(attribute, el, doc);
91: #endif /* TEMPLATES */
92: }
93:
1.13 kia 94: /*----------------------------------------------------------------------
95: Returns the value of a string attribute without copy it
96: ----------------------------------------------------------------------*/
97: void GiveAttributeStringValueFromNum (Element el, int att, char* buff, int* sz)
98: {
99: #ifdef TEMPLATES
100: AttributeType attType;
101: Attribute attribute;
102: int size;
103:
104: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
105: attType.AttrTypeNum = att;
106: attribute = TtaGetAttribute(el, attType);
107:
108: size = TtaGetTextAttributeLength(attribute);
109: TtaGiveTextAttributeValue (attribute, buff, &size);
110: buff[size] = EOS;
111: if(sz)
112: *sz = size;
113: #endif /* TEMPLATES */
114: }
115:
116:
1.1 francesc 117:
118: /*----------------------------------------------------------------------
119: Returns the value of a string attribute
120: ----------------------------------------------------------------------*/
1.8 kia 121: char *GetAttributeStringValueFromNum (Element el, int att, int* sz)
1.1 francesc 122: {
123: #ifdef TEMPLATES
124: AttributeType attType;
1.12 vatton 125: Attribute attribute;
126: char *aux;
127: int size;
128:
1.1 francesc 129: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
130: attType.AttrTypeNum = att;
1.12 vatton 131: attribute = TtaGetAttribute(el, attType);
1.1 francesc 132:
1.12 vatton 133: size = TtaGetTextAttributeLength(attribute);
134: aux = (char*) TtaGetMemory(size+1);
1.1 francesc 135: TtaGiveTextAttributeValue (attribute, aux, &size);
1.12 vatton 136: aux[size] = EOS;
1.4 kia 137: if(sz)
138: *sz = size;
1.1 francesc 139: return aux;
140: #else
1.8 kia 141: return NULL;
1.1 francesc 142: #endif /* TEMPLATES */
143: }
1.2 francesc 144:
145: /*----------------------------------------------------------------------
146: Returns the value of a string attribute
147: ----------------------------------------------------------------------*/
1.4 kia 148: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz)
1.2 francesc 149: {
150: #ifdef TEMPLATES
151: int size = TtaGetTextAttributeLength(attribute);
152: char *aux = (char*) TtaGetMemory(size+1);
1.12 vatton 153:
1.2 francesc 154: TtaGiveTextAttributeValue (attribute, aux, &size);
1.12 vatton 155: aux[size] = EOS;
1.4 kia 156: if(sz)
157: *sz = size;
1.2 francesc 158: return aux;
159: #else
1.8 kia 160: return NULL;
1.2 francesc 161: #endif /* TEMPLATES */
162: }
1.6 kia 163:
164:
165: /*----------------------------------------------------------------------
166: GetFirstEditableElement
167: Returns the first descendant element which is modifiable.
168: ----------------------------------------------------------------------*/
169: Element GetFirstEditableElement (Element el)
170: {
171: Element res = NULL;
172: Element current = TtaGetFirstChild(el);
173:
174: while(!res && current)
175: {
176: res = GetFirstEditableElement(current);
177: TtaNextSibling(¤t);
178: }
179:
180: if(!res && !TtaIsReadOnly(el))
181: res = el;
182:
183: return res;
184: }
1.14 kia 185:
186: /*----------------------------------------------------------------------
1.15 kia 187: TemplateCanInsertFirstChild
188: Test if an element can be inserted as child of another, bypassing xt.
189: ----------------------------------------------------------------------*/
190: ThotBool TemplateCanInsertFirstChild(ElementType elementType, Element parent, Document document)
191: {
192: #ifdef TEMPLATES
193: SSchema templateSSchema = TtaGetSSchema ("Template", document);
194: ElementType parType;
195:
196: while(parent)
197: {
198: parType = TtaGetElementType(parent);
199: if(parType.ElSSchema != templateSSchema)
200: break;
201: parent = TtaGetParent(parent);
202: }
203: if(!parent)
204: return FALSE;
205: #endif /* TEMPLATES */
206: return TtaCanInsertFirstChild(elementType, parent, document);
207: }
208:
209: /*----------------------------------------------------------------------
1.14 kia 210: ValidateTemplateAttrInMenu
211: Validate the status of an attribute according to xt::atribute rules.
212: ----------------------------------------------------------------------*/
213: ThotBool ValidateTemplateAttrInMenu (NotifyAttribute * event)
214: {
215: #ifdef TEMPLATES
216: Element elem;
217: Element parent;
218: ElementType elType;
219: AttributeType attrType;
220: Attribute attr;
221: char* attrName;
222: char buffer[MAX_LENGTH];
223: int sz;
224: int useAt, type;
225:
226: /* Prevent from showing attributes for template instance but not templates. */
227: if(IsTemplateInstanceDocument(event->document))
228: {
229: /* Prevent if attribute's element is not a descendant of xt:use */
1.17 kia 230: /* Dont prevent if descendant of xt:bag. */
1.14 kia 231: parent = event->element;
232: elem = GetFirstTemplateParentElement(parent);
233: if(!elem)
234: return TRUE;
235: elType = TtaGetElementType(elem);
1.17 kia 236: if(elType.ElTypeNum==Template_EL_bag)
237: return FALSE;
1.14 kia 238: if(elType.ElTypeNum!=Template_EL_useSimple)
239: return TRUE;
240:
241: /* Search for the corresponding xt:attribute element*/
242: attrName = TtaGetAttributeName(event->attributeType);
243: attrType.AttrSSchema = TtaGetSSchema ("Template", event->document);
244: for(elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem))
245: {
246: attrType.AttrTypeNum = Template_ATTR_ref_name;
247: elType = TtaGetElementType(elem);
248: if(elType.ElTypeNum==Template_EL_attribute &&
249: elType.ElSSchema==TtaGetSSchema ("Template", event->document))
250: {
251: attr = TtaGetAttribute(elem, attrType);
252: if(attr)
253: {
254: sz = MAX_LENGTH;
255: TtaGiveTextAttributeValue(attr, buffer, &sz);
256: if(!strcmp(buffer, attrName))
257: {
258: /* Process the attribute filtering */
259: /* Get 'useAt' attr value. */
260: attrType.AttrTypeNum = Template_ATTR_useAt;
261: attr = TtaGetAttribute(elem, attrType);
262: if(attr)
263: useAt = TtaGetAttributeValue(attr);
264: else
265: useAt = Template_ATTR_useAt_VAL_required;
266: /* Get 'type' attr value. */
267: attrType.AttrTypeNum = Template_ATTR_type;
268: attr = TtaGetAttribute(elem, attrType);
269: if(attr)
270: type = TtaGetAttributeValue(attr);
271: else
272: type = Template_ATTR_type_VAL_string;
273: event->restr.RestrType = (RestrictionContentType)type;
274: /* If attr is prohibited, dont show it.*/
275: if(useAt==Template_ATTR_useAt_VAL_prohibited)
1.16 kia 276: return TRUE;
1.14 kia 277: if(useAt==Template_ATTR_useAt_VAL_required)
278: {
279: /* Force the usage of this attribute.*/
280: event->restr.RestrFlags |= attr_mandatory;
281: }
282:
283: /* Get 'fixed' attr value. */
284: attrType.AttrTypeNum = Template_ATTR_fixed;
285: attr = TtaGetAttribute(elem, attrType);
286: if(attr)
287: {
288: sz = MAX_LENGTH;
289: TtaGiveTextAttributeValue(attr, buffer, &sz);
290: event->restr.RestrFlags |= attr_readonly;
291: event->restr.RestrDefVal = TtaStrdup(buffer);
292: return FALSE;
293: }
294:
295: /* Get 'default' attr value.*/
296: attrType.AttrTypeNum = Template_ATTR_defaultAt;
297: attr = TtaGetAttribute(elem, attrType);
298: if(attr)
299: {
300: sz = MAX_LENGTH;
301: TtaGiveTextAttributeValue(attr, buffer, &sz);
302: event->restr.RestrDefVal = TtaStrdup(buffer);
303: }
304:
305: /* Get 'values' attr value.*/
306: attrType.AttrTypeNum = Template_ATTR_values;
307: attr = TtaGetAttribute(elem, attrType);
308: if(attr)
309: {
310: sz = MAX_LENGTH;
311: TtaGiveTextAttributeValue(attr, buffer, &sz);
312: event->restr.RestrEnumVal = TtaStrdup(buffer);
313: event->restr.RestrFlags |= attr_enum;
314: }
315: return FALSE;
316: }
317: }
318: }
319: }
320:
1.16 kia 321: return TRUE;
1.14 kia 322: }
323: else
324: #endif /* TEMPLATES */
325: return FALSE;
326: }
1.19 kia 327:
328: /*----------------------------------------------------------------------
1.20 kia 329: * Dump element path
330: ----------------------------------------------------------------------*/
331: void DumpElementSubPath(Element el, char* buffer)
332: {
1.27 vatton 333: #ifdef AMAYA_DEBUG
1.20 kia 334: Element parent = TtaGetParent(el);
335: if(parent==NULL)
336: strcpy(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
337: else
338: {
339: DumpElementSubPath(parent, buffer);
340: strcat(buffer, "/");
341: strcat(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
342: }
1.27 vatton 343: #endif /* AMAYA_DEBUG */
1.20 kia 344: }
345:
346: /*----------------------------------------------------------------------
347: * Dump element path
348: ----------------------------------------------------------------------*/
349: void DumpElementPath(Element el)
350: {
1.27 vatton 351: #ifdef AMAYA_DEBUG
1.20 kia 352: char buffer[MAX_LENGTH];
353: DumpElementSubPath(el, buffer);
354: printf("%s\n", buffer);
1.27 vatton 355: #endif /* AMAYA_DEBUG */
1.20 kia 356: }
357:
358:
359: /*----------------------------------------------------------------------
1.19 kia 360: * Dump template element
361: ----------------------------------------------------------------------*/
362: void DumpTemplateElement(Element el, Document doc)
363: {
1.27 vatton 364: #ifdef AMAYA_DEBUG
1.28 ! vatton 365: ElementType elType;
! 366: AttributeType attType;
! 367: Attribute att;
! 368: SSchema schema = TtaGetSSchema ("Template", doc);
! 369: char* str;
! 370: char buffer[MAX_LENGTH];
! 371: int len;
! 372: Language lang;
1.26 kia 373:
1.19 kia 374: if(el && doc)
375: {
376: elType = TtaGetElementType(el);
1.26 kia 377: printf("- %p %d ", elType.ElSSchema, elType.ElTypeNum);
378: printf(" %s", TtaGetSSchemaName(elType.ElSSchema));
379: printf(":%s", TtaGetElementTypeName(elType));
380: if(elType.ElTypeNum==1)
381: {
382: len = MAX_LENGTH-1;
383: TtaGiveTextContent(el, (unsigned char*)buffer, &len, &lang);
384: buffer[len] = EOS;
385: printf(" \"%s\"", buffer);
386: }
387:
1.19 kia 388: if(elType.ElSSchema==schema)
389: {
390: switch(elType.ElTypeNum)
391: {
392: case Template_EL_head:
393: str = GetAttributeStringValueFromNum(el, Template_ATTR_version, NULL);
394: printf(" version=%s", str);
395: TtaFreeMemory(str);
396: str = GetAttributeStringValueFromNum(el, Template_ATTR_templateVersion, NULL);
397: printf(" templateVersion=%s", str);
398: TtaFreeMemory(str);
399: break;
400: case Template_EL_component:
401: str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
402: printf(" name=%s", str);
403: TtaFreeMemory(str);
404: break;
405: case Template_EL_union:
406: str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
407: printf(" name=%s", str);
408: TtaFreeMemory(str);
409: str = GetAttributeStringValueFromNum(el, Template_ATTR_includeAt, NULL);
410: printf(" include=%s", str);
411: TtaFreeMemory(str);
412: str = GetAttributeStringValueFromNum(el, Template_ATTR_exclude, NULL);
413: printf(" exclude=%s", str);
414: TtaFreeMemory(str);
415: break;
416: case Template_EL_import:
417: str = GetAttributeStringValueFromNum(el, Template_ATTR_src, NULL);
418: printf(" src=%s", str);
419: TtaFreeMemory(str);
420: break;
421: case Template_EL_repeat:
422: str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
423: printf(" label=%s", str);
424: TtaFreeMemory(str);
425: str = GetAttributeStringValueFromNum(el, Template_ATTR_minOccurs, NULL);
426: printf(" minOccurs=%s", str);
427: TtaFreeMemory(str);
428: str = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
429: printf(" maxOccurs=%s", str);
430: TtaFreeMemory(str);
431: break;
432: case Template_EL_useSimple:
433: case Template_EL_useEl:
434: str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
435: printf(" label=%s", str);
436: TtaFreeMemory(str);
437: str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
438: printf(" types=%s", str);
439: TtaFreeMemory(str);
1.28 ! vatton 440: attType.AttrSSchema = elType.ElSSchema;
! 441: attType.AttrTypeNum = Template_ATTR_option;
! 442: att = TtaGetAttribute (el, attType);
! 443: if (att)
! 444: printf(" option");
1.19 kia 445: break;
446: case Template_EL_bag:
447: str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
448: printf(" label=%s", str);
449: TtaFreeMemory(str);
450: str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
451: printf(" types=%s", str);
452: TtaFreeMemory(str);
453: break;
454: case Template_EL_attribute:
455: str = GetAttributeStringValueFromNum(el, Template_ATTR_ref_name, NULL);
456: printf(" name=%s", str);
457: TtaFreeMemory(str);
458: str = GetAttributeStringValueFromNum(el, Template_ATTR_type, NULL);
459: printf(" type=%s", str);
460: TtaFreeMemory(str);
461: str = GetAttributeStringValueFromNum(el, Template_ATTR_useAt, NULL);
462: printf(" use=%s", str);
463: TtaFreeMemory(str);
464: str = GetAttributeStringValueFromNum(el, Template_ATTR_defaultAt, NULL);
465: printf(" default=%s", str);
466: TtaFreeMemory(str);
467: str = GetAttributeStringValueFromNum(el, Template_ATTR_fixed, NULL);
468: printf(" fixed=%s", str);
469: TtaFreeMemory(str);
470: break;
471: }
472: }
473: }
1.27 vatton 474: #endif /* AMAYA_DEBUG */
1.19 kia 475: }
1.21 kia 476:
477: /*----------------------------------------------------------------------
1.25 kia 478: * Dump subtree
479: ----------------------------------------------------------------------*/
480: void DumpSubtree(Element el, Document doc, int off)
481: {
1.27 vatton 482: #ifdef AMAYA_DEBUG
1.25 kia 483: Element child = TtaGetFirstChild(el);
484: int i;
485:
486: for(i=0; i<off; i++)
487: printf(" ");
488: DumpTemplateElement(el, doc);
489: printf("\n");
490:
491: while(child)
492: {
493: DumpSubtree(child, doc, off+1);
494: TtaNextSibling(&child);
495: }
1.27 vatton 496: #endif /* AMAYA_DEBUG */
1.25 kia 497: }
498:
499: /*----------------------------------------------------------------------
1.21 kia 500: * Save an opened document to a specified path in order to open.
1.23 vatton 501: * param doc Original doc to save
502: * param newdoc Document where reopen it
503: * param newpath URI where save the doc
1.21 kia 504: ----------------------------------------------------------------------*/
1.23 vatton 505: ThotBool SaveDocumentToNewDoc(Document doc, Document newdoc, char* newpath)
1.21 kia 506: {
507: ElementType elType;
508: Element root;
509: char *localFile, *s;
510: ThotBool res = FALSE;
511:
512: localFile = GetLocalPath (newdoc, newpath);
1.22 kia 513: // update all links
1.24 kia 514: SetRelativeURLs (doc, newpath, NULL, FALSE, FALSE, FALSE);
1.22 kia 515: // prepare the new document view
516: TtaExtractName (newpath, DirectoryName, DocumentName);
1.21 kia 517:
1.22 kia 518: root = TtaGetRootElement(doc);
519: elType = TtaGetElementType (root);
520: // get the target document type
521: s = TtaGetSSchemaName (elType.ElSSchema);
522: if (strcmp (s, "HTML") == 0)
523: {
524: /* docType = docHTML; */
525: if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
526: TtaGetDocumentProfile(doc) == L_Basic)
527: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11", FALSE);
1.21 kia 528: else
1.22 kia 529: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX", FALSE);
530: }
531: else if (strcmp (s, "SVG") == 0)
532: /* docType = docSVG; */
533: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT", FALSE);
534: else if (strcmp (s, "MathML") == 0)
535: /* docType = docMath; */
536: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT", FALSE);
537: else
538: /* docType = docXml; */
539: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL, FALSE);
1.21 kia 540: return res;
541: }
Webmaster