Annotation of Amaya/amaya/templateInstantiate.c, revision 1.40
1.19 vatton 1: /*
2: *
3: * COPYRIGHT INRIA and W3C, 2006-2007
4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
7:
1.1 vatton 8: #include "templates.h"
9:
10: #define THOT_EXPORT extern
11: #include "templateDeclarations.h"
12:
1.8 kia 13: #include "Elemlist.h"
14:
1.1 vatton 15: #include "EDITimage_f.h"
16: #include "HTMLactions_f.h"
17: #include "HTMLsave_f.h"
1.34 vatton 18: #include "HTMLtable_f.h"
1.1 vatton 19: #include "init_f.h"
20: #include "templates_f.h"
21: #include "templateDeclarations_f.h"
22: #include "templateInstantiate_f.h"
23: #include "Templatebuilder_f.h"
24: #include "templateUtils_f.h"
25: #include "fetchHTMLname_f.h"
26: #include "Template.h"
27:
28: #ifdef TEMPLATES
29: #define TEMPLATE_SCHEMA_NAME "Template"
30:
31: typedef struct _InstantiateCtxt
32: {
33: char * templatePath;
34: char * instancePath;
35: char * schemaName;
36: DocumentType docType;
37: ThotBool dontReplace;
38: } InstantiateCtxt;
39: #endif /* TEMPLATES */
40:
41:
42: /*----------------------------------------------------------------------
43: CreateInstance
1.3 vatton 44: ----------------------------------------------------------------------*/
1.1 vatton 45: void CreateInstance(char *templatePath, char *instancePath)
1.3 vatton 46: {
1.1 vatton 47: #ifdef TEMPLATES
1.14 vatton 48: Document doc = 0;
1.5 vatton 49: DocumentType docType;
1.14 vatton 50: ElementType elType;
51: Element root, title, text;
52: char *s;
53: int alreadyOnDoc = 0;
54: ThotBool alreadyViewing = FALSE;
1.1 vatton 55:
1.35 kia 56: XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1 vatton 57: if (t == NULL)
1.29 vatton 58: {
59: // the template cannot be loaded
60: InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
61: return;
62: }
1.1 vatton 63:
1.5 vatton 64: doc = GetTemplateDocument (t);
1.3 vatton 65: while (alreadyOnDoc < DocumentTableLength-1 && !alreadyViewing)
1.1 vatton 66: {
67: alreadyOnDoc++;
68: if (DocumentURLs[alreadyOnDoc])
69: alreadyViewing = !strcmp (DocumentURLs[alreadyOnDoc],instancePath);
70: }
71:
72: if (!TtaPrepareUndo (doc))
73: {
74: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.14 vatton 75: root = TtaGetRootElement(doc);
76: elType = TtaGetElementType (root);
77: // get the target document type
78: s = TtaGetSSchemaName (elType.ElSSchema);
79: if (strcmp (s, "HTML") == 0)
80: docType = docHTML;
81: else if (strcmp (s, "SVG") == 0)
82: docType = docSVG;
83: else if (strcmp (s, "MathML") == 0)
84: docType = docMath;
85: else
86: docType = docXml;
1.30 vatton 87: // update all links
1.33 vatton 88: SetRelativeURLs (doc, instancePath, "", FALSE, FALSE, FALSE);
1.1 vatton 89:
90: switch (docType)
91: {
1.14 vatton 92: case docSVG:
1.1 vatton 93: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "SVGT");
94: break;
1.14 vatton 95: case docMath:
1.1 vatton 96: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "MathMLT");
97: break;
1.14 vatton 98: case docHTML:
99: // Initialize the document title
100: elType.ElTypeNum = HTML_EL_TITLE;
101: title = TtaSearchTypedElement (elType, SearchInTree, root);
102: text = TtaGetFirstChild (title);
103: while (text)
104: {
105: elType = TtaGetElementType (text);
106: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
107: {
108: TtaSetTextContent (text, (unsigned char*)Answer_text,
109: TtaGetDefaultLanguage (), doc);
110: text = NULL;
111: }
112: else if ((elType.ElTypeNum == Template_EL_useEl ||
113: elType.ElTypeNum == Template_EL_useSimple) &&
114: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
115: // Ignore the template use element
116: text = TtaGetFirstChild (text);
117: else
118: // Look for the first text child
119: TtaNextSibling (&text);
120: }
1.1 vatton 121: if (TtaGetDocumentProfile(doc)==L_Xhtml11 || TtaGetDocumentProfile(doc)==L_Basic)
122: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "HTMLT11");
123: else
124: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "HTMLTX");
125: break;
1.14 vatton 126: default:
1.1 vatton 127: TtaExportDocumentWithNewLineNumbers (doc, instancePath, NULL);
128: break;
129: }
130:
131: TtaCloseUndoSequence (doc);
132: TtaUndoNoRedo (doc);
133: TtaClearUndoHistory (doc);
134: }
135:
1.3 vatton 136: if (!alreadyViewing)
137: {
138: // Open the instance
1.1 vatton 139: TtaExtractName (instancePath, DirectoryName, DocumentName);
140: CallbackDialogue (BaseDialog + OpenForm, INTEGER_DATA, (char *) 1);
141: }
1.3 vatton 142: else
143: {
144: // Reload on the existing view
145: Reload (alreadyOnDoc, 0);
146: }
1.40 ! kia 147:
! 148:
! 149: // Intend to fix access rights for templates.
! 150: Template_PrintRights(TtaGetMainRoot(doc));
! 151:
! 152: TtaSetDisplayMode (doc, NoComputedDisplay);
! 153: Template_FixAccessRight(t, TtaGetMainRoot(doc), doc, TRUE);
! 154: TtaSetDisplayMode (doc, DisplayImmediately);
! 155:
! 156: Template_PrintRights(TtaGetMainRoot(doc));
! 157:
1.1 vatton 158: #endif /* TEMPLATES */
159: }
160:
161: /*----------------------------------------------------------------------
1.3 vatton 162: ----------------------------------------------------------------------*/
1.7 cvs 163: void InstantiateTemplate_callback (int newdoc, int status, char *urlName, char *outputfile,
164: char *proxyName, AHTHeaders *http_headers, void * context)
1.1 vatton 165: {
166: #ifdef TEMPLATES
167: InstantiateCtxt *ctx = (InstantiateCtxt*)context;
168:
169: DoInstanceTemplate (ctx->templatePath);
170: CreateInstance (ctx->templatePath, ctx->instancePath);
171: TtaFreeMemory (ctx->templatePath);
172: TtaFreeMemory (ctx->instancePath);
173: TtaFreeMemory (ctx);
174: #endif /* TEMPLATES */
175: }
176:
177: /*----------------------------------------------------------------------
1.3 vatton 178: ----------------------------------------------------------------------*/
1.1 vatton 179: void InstantiateTemplate (Document doc, char *templatename, char *docname,
180: DocumentType docType, ThotBool loaded)
181: {
182: #ifdef TEMPLATES
183: if (!loaded)
1.3 vatton 184: {
185: // Create the callback context
186: InstantiateCtxt *ctx = (InstantiateCtxt *)TtaGetMemory (sizeof (InstantiateCtxt));
187: ctx->templatePath = TtaStrdup (templatename);
188: ctx->instancePath = TtaStrdup (docname);
189: ctx->schemaName = GetSchemaFromDocType(docType);
190: ctx->docType = docType;
1.1 vatton 191:
1.3 vatton 192: GetAmayaDoc (templatename, NULL, doc, doc, CE_MAKEBOOK, FALSE,
1.6 cvs 193: (void (*)(int, int, char*, char*, char*, const AHTHeaders*, void*)) InstantiateTemplate_callback,
1.3 vatton 194: (void *) ctx);
195: }
1.1 vatton 196: else
197: {
198: DoInstanceTemplate (templatename);
199: CreateInstance (templatename, docname);
1.3 vatton 200: }
1.1 vatton 201: #endif /* TEMPLATES */
202: }
203:
204: /*----------------------------------------------------------------------
205: InstantiateAttribute
1.3 vatton 206: ----------------------------------------------------------------------*/
1.1 vatton 207: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
208: {
209: #ifdef TEMPLATES
210: AttributeType useType, nameType, defaultType, attrType;
211: Attribute useAttr, nameAttr, defAttr, attr;
212: ElementType elType;
213: Element parent;
214: char *text, *elementName;
215: ThotBool level;
216: NotifyAttribute event;
217:
218: parent = TtaGetParent (el);
219: if (!parent)
220: return;
221: // if attribute "use" has value "optional", don't do anything
222: useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
223: useType.AttrTypeNum = Template_ATTR_useAt;
224: useAttr = TtaGetAttribute (el, useType);
225: if (useAttr)
226: // there is a "use" attribute. Check its value
227: {
1.10 kia 228: text = GetAttributeStringValue (el, useAttr, NULL);
1.25 vatton 229: if (!text)
1.1 vatton 230: return;
1.25 vatton 231: if (strcmp (text, "optional") == 0)
1.18 kia 232: {
233: TtaFreeMemory(text);
234: return;
235: }
236: TtaFreeMemory(text);
1.1 vatton 237: }
1.18 kia 238:
1.1 vatton 239: // get the "name" and "default" attributes
240: nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.9 vatton 241: nameType.AttrTypeNum = Template_ATTR_ref_name;
1.1 vatton 242: defaultType.AttrTypeNum = Template_ATTR_defaultAt;
243: nameAttr = TtaGetAttribute (el, nameType);
244: defAttr = TtaGetAttribute (el, defaultType);
245: if (nameAttr)
246: {
1.10 kia 247: text = GetAttributeStringValue (el, nameAttr, NULL);
1.1 vatton 248: if (text)
249: {
250: elType = TtaGetElementType (parent);
251: elementName = TtaGetElementTypeName (elType);
252: level = TRUE;
253: MapHTMLAttribute (text, &attrType, elementName, &level, doc);
254: TtaFreeMemory(text);
255: attr = TtaNewAttribute (attrType);
256: if (attr)
257: {
258: TtaAttachAttribute (parent, attr, doc);
259: if (defAttr)
260: {
1.10 kia 261: text = GetAttributeStringValue (el, defAttr, NULL);
1.25 vatton 262: if (text)
1.18 kia 263: TtaSetAttributeText(attr, text, parent, doc);
1.1 vatton 264: TtaFreeMemory(text);
265: // if it's a src arttribute for an image, load the image
266: if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
267: elType.ElTypeNum == HTML_EL_IMG)
268: if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
269: attrType.AttrSSchema == elType.ElSSchema)
270: {
271: event.document = doc;
272: event.element = parent;
273: event.attribute = attr;
274: SRCattrModified (&event);
275: }
276: }
277: }
278: }
279: }
280: #endif /* TEMPLATES */
281: }
282:
283: #ifdef TEMPLATES
284: /*----------------------------------------------------------------------
285: ProcessAttr
1.9 vatton 286: Look for all "attribute" elements in the subtree and instantiate them
1.3 vatton 287: ----------------------------------------------------------------------*/
1.1 vatton 288: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
289: {
290: Element child;
291: ElementType elType;
292:
293: for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
294: {
295: elType = TtaGetElementType (child);
296: if (elType.ElTypeNum == Template_EL_attribute &&
297: !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
298: InstantiateAttribute (t, child, doc);
299: else
300: ProcessAttr (t, child, doc);
301: }
302: }
303: #endif /* TEMPLATES */
304:
1.12 kia 305:
306: /*----------------------------------------------------------------------
307: Template_GetNewSimpleTypeInstance
308: Create an new instance of xt:use/SimpleType
309: The decl attribute must embed SimpleType declaration (no validation).
310: @param decl Declaration of new element
311: @param parent Future parent element
312: @param doc Document
313: @return The new element
314: ----------------------------------------------------------------------*/
315: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl)
316: {
317: Element newEl = NULL;
318: #ifdef TEMPLATES
319: ElementType elType;
320: char *empty = " ";
1.24 vatton 321:
1.38 vatton 322: elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12 kia 323: elType.ElTypeNum = Template_EL_TEXT_UNIT;
324: newEl = TtaNewElement (doc, elType);
325: TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24 vatton 326: #endif /* TEMPLATES */
1.12 kia 327: return newEl;
328: }
329:
330: /*----------------------------------------------------------------------
331: Template_GetNewXmlElementInstance
332: Create an new instance of xt:use/XmlElement
333: The decl attribute must embed XmlElement declaration (no validation).
334: @param decl Declaration of new element
335: @param parent Future parent element
336: @param doc Document
337: @return The new element
338: ----------------------------------------------------------------------*/
339: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
340: {
341: Element newEl = NULL;
342: #ifdef TEMPLATES
343: ElementType elType;
344:
1.24 vatton 345: GIType (decl->name, &elType, doc);
346: if (elType.ElTypeNum != 0)
1.12 kia 347: {
1.23 kia 348: newEl = TtaNewTree (doc, elType, "");
1.12 kia 349: }
350: #endif /* TEMPLATES */
351: return newEl;
352: }
353:
1.34 vatton 354:
355: /*----------------------------------------------------------------------
356: InsertWithNotify applies pre and post functions when inserting the new
357: element el after child (if not NULL) or as first child of parent.
358: ----------------------------------------------------------------------*/
359: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
360: {
361: ElementType elType;
362: NotifyElement event;
363: char *name;
364: ThotBool isRow = FALSE, isCell = FALSE;
365:
366: elType = TtaGetElementType (el);
367: name = TtaGetSSchemaName (elType.ElSSchema);
368: isCell = ((!strcmp (name,"HTML") &&
369: elType.ElTypeNum == HTML_EL_Data_cell ||
370: elType.ElTypeNum == HTML_EL_Heading_cell) ||
371: (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
372: isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
373: (!strcmp (name,"MathML") &&
374: (elType.ElTypeNum == MathML_EL_MTR ||
375: elType.ElTypeNum == MathML_EL_MLABELEDTR)));
376: if (child)
377: TtaInsertSibling (el, child, FALSE, doc);
378: else
379: TtaInsertFirstChild (&el, parent, doc);
380:
381: if (isCell)
382: {
383: // a cell is created
1.39 quint 384: NewCell (el, doc, TRUE, TRUE, TRUE);
1.34 vatton 385: }
386: else if (isRow)
387: {
388: // a row is created
389: event.element = el;
390: event.document = doc;
391: RowPasted (&event);
392: }
393: //else
394: // TtaNotifySubTree (TteElemNew, doc, el, FALSE);
395: return el;
396: }
397:
398:
1.12 kia 399: /*----------------------------------------------------------------------
1.16 kia 400: Template_InsertUseChildren
401: Insert children to a xt:use
402: The dec parameter must be valid and will not be verified. It must be a
403: direct child element (for union elements).
404: @param el element (xt:use) in which insert a new element
405: @param dec Template declaration of the element to insert
406: @return The inserted element (the xt:use element if insertion is multiple as component)
407: ----------------------------------------------------------------------*/
408: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
409: {
1.34 vatton 410: Element newEl = NULL;
1.16 kia 411: #ifdef TEMPLATES
1.19 vatton 412: Element current = NULL;
1.34 vatton 413: Element child = NULL;
1.19 vatton 414: //char *attrCurrentTypeValue;
415: //ElementType elType;
1.17 kia 416:
1.25 vatton 417: if (TtaGetDocumentAccessMode(doc))
1.16 kia 418: {
1.23 kia 419: switch (dec->nature)
420: {
421: case SimpleTypeNat:
422: newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
1.34 vatton 423: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23 kia 424: break;
425: case XmlElementNat:
426: newEl = Template_GetNewXmlElementInstance(doc, el, dec);
1.34 vatton 427: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23 kia 428: break;
429: case ComponentNat:
430: newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.35 kia 431: ProcessAttr (dec->usedIn, newEl, doc);
1.23 kia 432: /* Copy elements from new use to existing use. */
1.34 vatton 433: while ((child = TtaGetFirstChild(newEl)))
1.23 kia 434: {
435: TtaRemoveTree (child, doc);
1.34 vatton 436: child = InsertWithNotify (child, current, el, doc);
1.23 kia 437: current = child;
438: }
439:
440: /* Copy currentType attribute. */
441: //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL);
442: //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue);
443: TtaDeleteTree(newEl, doc);
444: newEl = el;
445: break;
446: case UnionNat :
447: /* Nothing to do.*/
448: // elType.ElTypeNum = Template_EL_useEl;
449: // cont = TtaNewElement (doc, elType);
450: // if (cont)
451: // {
452: // TtaSetAccessRight (cont, ReadWrite, doc);
453: // at = TtaNewAttribute (att);
454: // if (at)
455: // {
456: // TtaAttachAttribute (cont, at, doc);
457: // TtaSetAttributeText(at, types, cont, doc);
458: // }
459: // }
460: /* @@@@@ */
461: break;
462: default :
463: //Impossible
464: break;
465: }
466: }
1.16 kia 467: #endif /* TEMPLATES */
468: return newEl;
469: }
470:
1.40 ! kia 471:
! 472: /*----------------------------------------------------------------------
! 473: Set access rights.
! 474: \param rec if true, set rights for all children
! 475: ----------------------------------------------------------------------*/
! 476: void Template_SetAccessRight(Element el, AccessRight right, Document doc, ThotBool rec)
! 477: {
! 478: #ifdef TEMPLATES
! 479: Element child;
! 480:
! 481: if (el && doc)
! 482: {
! 483: TtaSetAccessRight(el, right, doc);
! 484: if (rec)
! 485: {
! 486: child = TtaGetFirstChild(el);
! 487: while (child)
! 488: {
! 489: Template_SetAccessRight(child, right, doc, rec);
! 490: TtaNextSibling(&child);
! 491: }
! 492: }
! 493: }
! 494: #endif /* TEMPLATES */
! 495: }
! 496:
! 497: /*----------------------------------------------------------------------
! 498: Dump access rights.
! 499: \param rec if true, set rights for all children
! 500: ----------------------------------------------------------------------*/
! 501: void Template_PrintRights(Element el)
! 502: {
! 503: #ifdef TEMPLATES
! 504: #ifdef AMAYA_DEBUG
! 505: ElementType elType;
! 506: Element child;
! 507:
! 508: static int dec = 0;
! 509:
! 510: elType = TtaGetElementType(el);
! 511: printf("PrintAccessRight ");
! 512: for(int i=0; i<dec; i++)
! 513: printf(" ");
! 514: printf("%s:%d:%s : ", TtaGetSSchemaName(elType.ElSSchema), elType.ElTypeNum, TtaGetElementTypeName(elType));
! 515:
! 516: dec++;
! 517:
! 518: switch(TtaGetAccessRight(el))
! 519: {
! 520: case ReadOnly:
! 521: printf("ReadOnly\n");
! 522: break;
! 523: case ReadWrite:
! 524: printf("ReadWrite\n");
! 525: break;
! 526: case Hidden:
! 527: printf("Hidden\n");
! 528: break;
! 529: case Inherited:
! 530: printf("Inherited\n");
! 531: break;
! 532: default:
! 533: printf("other\n");
! 534: break;
! 535: }
! 536: child = TtaGetFirstChild(el);
! 537: while (child)
! 538: {
! 539: Template_PrintRights(child);
! 540: TtaNextSibling(&child);
! 541: }
! 542: dec--;
! 543: #endif /* AMAYA_DEBUG */
! 544: #endif /* TEMPLATES */
! 545: }
! 546:
! 547: /*----------------------------------------------------------------------
! 548: Fix access rights.
! 549: \param rec if true, set rights for all children
! 550: ----------------------------------------------------------------------*/
! 551: void Template_FixAccessRight(XTigerTemplate t, Element el, Document doc, ThotBool rec)
! 552: {
! 553: #ifdef TEMPLATES
! 554: ElementType elType;
! 555: Element child;
! 556: char currentType[MAX_LENGTH];
! 557: Declaration decl;
! 558:
! 559: static int dec = 0;
! 560:
! 561: if (t && el && doc)
! 562: {
! 563: elType = TtaGetElementType(el);
! 564:
! 565: #ifdef AMAYA_DEBUG
! 566: printf("FixAccessRight ");
! 567: for(int i=0; i<dec; i++)
! 568: printf(" ");
! 569: printf("%s:%d:%s : ", TtaGetSSchemaName(elType.ElSSchema), elType.ElTypeNum, TtaGetElementTypeName(elType));
! 570:
! 571: dec++;
! 572: #endif /* AMAYA_DEBUG */
! 573: if (elType.ElSSchema == TtaGetSSchema("Template", doc))
! 574: {
! 575: switch(elType.ElTypeNum)
! 576: {
! 577: case Template_EL_TEXT_UNIT:
! 578: TtaSetAccessRight( el, ReadWrite, doc);
! 579: #ifdef AMAYA_DEBUG
! 580: printf("ReadWrite");
! 581: #endif /* AMAYA_DEBUG */
! 582: break;
! 583: case Template_EL_useEl:
! 584: case Template_EL_useSimple:
! 585: GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
! 586: (char*)currentType, NULL);
! 587: decl = Template_GetDeclaration(t, currentType);
! 588: if (decl)
! 589: {
! 590: switch (decl->nature)
! 591: {
! 592: case SimpleTypeNat:
! 593: case XmlElementNat:
! 594: #ifdef AMAYA_DEBUG
! 595: printf("ReadWrite\n");
! 596: #endif /* AMAYA_DEBUG */
! 597: Template_SetAccessRight(el, ReadWrite, doc, TRUE);
! 598: rec = FALSE;
! 599: break;
! 600: default:
! 601: #ifdef AMAYA_DEBUG
! 602: printf("ReadOnly\n");
! 603: #endif /* AMAYA_DEBUG */
! 604: TtaSetAccessRight(el, ReadOnly, doc);
! 605: break;
! 606: }
! 607: }
! 608: #ifdef AMAYA_DEBUG
! 609: else
! 610: printf("no decl\n");
! 611: #endif /* AMAYA_DEBUG */
! 612: break;
! 613: case Template_EL_bag:
! 614: #ifdef AMAYA_DEBUG
! 615: printf("ReadWrite\n");
! 616: #endif /* AMAYA_DEBUG */
! 617: TtaSetAccessRight(el, ReadWrite, doc);
! 618: break;
! 619: default:
! 620: #ifdef AMAYA_DEBUG
! 621: printf("ReadOnly\n");
! 622: #endif /* AMAYA_DEBUG */
! 623: TtaSetAccessRight(el, ReadOnly, doc);
! 624: break;
! 625: }
! 626: }
! 627: // else (elType.ElSSchema == TtaGetSSchema("HTML", doc))
! 628: // {
! 629: //
! 630: // }
! 631: else
! 632: {
! 633: #ifdef AMAYA_DEBUG
! 634: printf("Inherited\n");
! 635: #endif /* AMAYA_DEBUG */
! 636: TtaSetAccessRight(el, Inherited, doc);
! 637: }
! 638:
! 639: if (rec)
! 640: {
! 641: child = TtaGetFirstChild(el);
! 642: while (child)
! 643: {
! 644: Template_FixAccessRight(t, child, doc, rec);
! 645: TtaNextSibling(&child);
! 646: }
! 647: }
! 648: dec--;
! 649: }
! 650: #endif /* TEMPLATES */
! 651: }
! 652:
1.16 kia 653: /*----------------------------------------------------------------------
1.1 vatton 654: InstantiateUse
1.3 vatton 655: ----------------------------------------------------------------------*/
1.1 vatton 656: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.27 kia 657: ThotBool registerUndo)
1.1 vatton 658: {
659: #ifdef TEMPLATES
1.23 kia 660: Element cont = NULL;
1.1 vatton 661: ElementType elType;
662: Declaration dec;
1.28 kia 663: int size, nbitems, i;
1.1 vatton 664: struct menuType *items;
1.33 vatton 665: char *types, *text = NULL;
1.1 vatton 666: ThotBool oldStructureChecking;
667:
1.25 vatton 668: if (!t)
1.23 kia 669: return NULL;
670:
1.1 vatton 671: /* get the value of the "types" attribute */
672: cont = NULL;
1.12 kia 673: elType = TtaGetElementType (el);
1.32 vatton 674: types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36 vatton 675: if (!types || types[0] == EOS)
676: {
677: TtaFreeMemory (types);
678: return NULL;
679: }
1.1 vatton 680: giveItems (types, size, &items, &nbitems);
681: // No structure checking
682: oldStructureChecking = TtaGetStructureChecking (doc);
683: TtaSetStructureChecking (FALSE, doc);
1.10 kia 684:
1.1 vatton 685: if (nbitems == 1)
686: /* only one type in the "types" attribute */
687: {
1.17 kia 688: dec = Template_GetDeclaration (t, items[0].label);
1.1 vatton 689: if (dec)
1.27 kia 690: {
1.16 kia 691: cont = Template_InsertUseChildren(doc, el, dec);
1.32 vatton 692: if (cont)
1.27 kia 693: {
694: TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1.32 vatton 695: if (registerUndo)
696: TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.27 kia 697: }
698: }
1.1 vatton 699: }
1.33 vatton 700: TtaFreeMemory (text);
1.32 vatton 701: TtaFreeMemory (types);
1.28 kia 702:
1.32 vatton 703: for (i = 0; i < nbitems; i++)
1.28 kia 704: TtaFreeMemory(items[i].label);
1.18 kia 705: TtaFreeMemory(items);
1.1 vatton 706: TtaSetStructureChecking (oldStructureChecking, doc);
1.40 ! kia 707:
1.1 vatton 708: return cont;
1.23 kia 709: #else /* TEMPLATES */
710: return NULL;
1.1 vatton 711: #endif /* TEMPLATES */
712: }
713:
714: /*----------------------------------------------------------------------
1.22 kia 715: InstantiateRepeat
716: Check for min and max param and validate xt:repeat element content.
717: @param registerUndo True to register undo creation sequences.
1.3 vatton 718: ----------------------------------------------------------------------*/
1.22 kia 719: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc, ThotBool registerUndo)
1.1 vatton 720: {
721: #ifdef TEMPLATES
1.32 vatton 722: Element child, newChild;
1.1 vatton 723: Attribute curAtt, minAtt, maxAtt;
724: AttributeType curType, minType, maxType;
1.33 vatton 725: char *text, *types = NULL, *title = NULL;
1.32 vatton 726: int curVal, minVal, maxVal;
727: int childrenCount;
728:
1.1 vatton 729:
1.25 vatton 730: if (!t)
1.23 kia 731: return;
732:
1.1 vatton 733: //Preparing types
734: curType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
735: minType.AttrSSchema = maxType.AttrSSchema = curType.AttrSSchema;
736: curType.AttrTypeNum = Template_ATTR_currentOccurs;
737: minType.AttrTypeNum = Template_ATTR_minOccurs;
738: maxType.AttrTypeNum = Template_ATTR_maxOccurs;
739:
740: //Get currentOccurs, minOccurs and maxOccurs attributes
741: curAtt = TtaGetAttribute (el, curType);
742: minAtt = TtaGetAttribute (el, minType);
743: maxAtt = TtaGetAttribute (el, maxType);
744:
745: //Get the values
746: if (minAtt)
747: {
1.10 kia 748: text = GetAttributeStringValue(el, minAtt, NULL);
1.1 vatton 749: if (text)
750: {
751: minVal = atoi(text);
752: TtaFreeMemory(text);
753: }
754: else
755: //Error : Attribute with no value
756: return;
757: }
758: else
759: minVal = 0;
760:
761: if (maxAtt)
762: {
1.10 kia 763: text = GetAttributeStringValue (el, maxAtt, NULL);
1.1 vatton 764: if (text)
765: {
766: if (!strcmp (text, "*"))
767: maxVal = INT_MAX;
768: else
769: maxVal = atoi (text);
770: TtaFreeMemory (text);
771: }
772: else
773: //Error : Attribute with no value
774: return;
775: }
776: else
777: maxVal = INT_MAX;
778:
779: if (curAtt)
780: {
1.10 kia 781: text = GetAttributeStringValue(el, curAtt, NULL);
1.1 vatton 782: if (text)
783: {
1.2 quint 784: curVal = atoi(text);
1.1 vatton 785: TtaFreeMemory(text);
786: }
787: else
788: //Error : Attribute with no value
789: return;
790: }
791: else
792: curVal = minVal;
793:
794: text = (char*)TtaGetMemory(MAX_LENGTH);
795: //Create non existing attributes
796: if (!minAtt)
797: {
1.32 vatton 798: minAtt = TtaNewAttribute (minType);
799: sprintf (text, "%d", minVal);
800: TtaAttachAttribute (el, minAtt, doc);
801: TtaSetAttributeText (minAtt, text, el, doc);
1.25 vatton 802: if (registerUndo)
1.32 vatton 803: TtaRegisterAttributeCreate (minAtt, el, doc);
1.1 vatton 804: }
805:
806: if (!maxAtt)
807: {
1.32 vatton 808: maxAtt = TtaNewAttribute (maxType);
809: if (maxVal < INT_MAX)
810: sprintf(text, "%d", maxVal);
1.1 vatton 811: else
1.32 vatton 812: sprintf (text, "*");
813: TtaAttachAttribute (el, maxAtt, doc);
814: TtaSetAttributeText (maxAtt, text, el, doc);
1.25 vatton 815: if (registerUndo)
1.32 vatton 816: TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1 vatton 817: }
818:
819: if (!curAtt)
1.2 quint 820: {
1.32 vatton 821: curAtt = TtaNewAttribute (curType);
822: sprintf (text,"%d",curVal);
823: TtaAttachAttribute (el, curAtt, doc);
824: TtaSetAttributeText (curAtt, text, el, doc);
1.25 vatton 825: if (registerUndo)
1.32 vatton 826: TtaRegisterAttributeCreate (curAtt, el, doc);
1.1 vatton 827: }
828: if (text)
829: TtaFreeMemory(text);
830:
831: //We must have currentOccurs children
832: child = TtaGetFirstChild(el);
833: if (!child)
834: //Error : a repeat must have at least one child which will be the model
835: return;
836:
837: for(childrenCount = 0; child; TtaNextSibling(&child))
838: {
839: //TODO : Check that every child is valid
840: childrenCount ++;
841: }
842:
843: if (childrenCount > maxVal)
844: //Error : too many children!
845: return;
846:
847: child = TtaGetLastChild(el);
1.32 vatton 848: types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
849: title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
850: while (childrenCount < curVal)
1.1 vatton 851: {
1.27 kia 852: ElementType newElType;
853: newElType.ElSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
854: newElType.ElTypeNum = Template_EL_useEl;
1.32 vatton 855: newChild = TtaNewElement (doc, newElType);
1.27 kia 856:
857: // Insert it
1.32 vatton 858: TtaInsertSibling (newChild, child, FALSE, doc);
859: SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
860: SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.34 vatton 861: InstantiateUse (t, newChild, doc, TRUE);
1.27 kia 862:
1.25 vatton 863: if (registerUndo)
1.34 vatton 864: TtaRegisterElementCreate (newChild, doc);
1.1 vatton 865: child = newChild;
866: childrenCount++;
867: }
1.27 kia 868:
1.32 vatton 869: TtaFreeMemory (types);
870: TtaFreeMemory (title);
1.1 vatton 871: #endif /* TEMPLATES */
872: }
873:
874: /*----------------------------------------------------------------------
875: ParseTemplate
1.3 vatton 876: ----------------------------------------------------------------------*/
1.1 vatton 877: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
878: ThotBool loading)
879: {
880: #ifdef TEMPLATES
881: AttributeType attType;
1.5 vatton 882: Attribute att;
883: Element aux, child; //Needed when deleting trees
884: char *name;
885: ElementType elType = TtaGetElementType (el);
1.1 vatton 886:
1.37 kia 887: if (!t || !el)
1.23 kia 888: return;
889:
1.5 vatton 890: name = TtaGetSSchemaName (elType.ElSSchema);
891: if (!strcmp (name, "Template"))
1.1 vatton 892: {
1.5 vatton 893: switch(elType.ElTypeNum)
1.1 vatton 894: {
895: case Template_EL_head :
896: //Remove it and all of its children
897: TtaDeleteTree(el, doc);
898: //We must stop searching into this tree
899: return;
900: break;
901: case Template_EL_component :
1.25 vatton 902: // remove the name attribute
1.5 vatton 903: attType.AttrSSchema = elType.ElSSchema;
1.1 vatton 904: attType.AttrTypeNum = Template_ATTR_name;
1.18 kia 905: name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);
1.5 vatton 906: TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.25 vatton 907: // replace the component by a use
1.1 vatton 908: if (NeedAMenu (el, doc))
1.5 vatton 909: TtaChangeElementType (el, Template_EL_useEl);
1.1 vatton 910: else
1.5 vatton 911: TtaChangeElementType (el, Template_EL_useSimple);
1.25 vatton 912: // generate the types attribute
1.1 vatton 913: attType.AttrTypeNum = Template_ATTR_types;
1.5 vatton 914: att = TtaNewAttribute (attType);
915: TtaAttachAttribute (el, att, doc);
1.25 vatton 916: if (name)
1.18 kia 917: TtaSetAttributeText (att, name, el, doc);
1.25 vatton 918: // generate the title attribute
919: attType.AttrTypeNum = Template_ATTR_title;
920: att = TtaNewAttribute (attType);
921: TtaAttachAttribute (el, att, doc);
922: if (name)
923: TtaSetAttributeText (att, name, el, doc);
924: // generate the currentType attribute
1.1 vatton 925: attType.AttrTypeNum = Template_ATTR_currentType;
1.5 vatton 926: att = TtaNewAttribute (attType);
1.18 kia 927: TtaAttachAttribute (el, att, doc);
1.25 vatton 928: if (name)
1.18 kia 929: TtaSetAttributeText (att, name, el, doc);
1.28 kia 930: TtaFreeMemory(name);
1.1 vatton 931: break;
932: case Template_EL_option :
933: aux = NULL;
934: break;
935: case Template_EL_bag :
936: //Link to types
937: //Allow editing the content
938: break;
939: case Template_EL_useEl :
940: case Template_EL_useSimple :
941: /* if this use element is not empty, don't do anything: it is
942: supposed to contain a valid instance. This should be
943: checked, though */
944: if (!TtaGetFirstChild (el))
1.27 kia 945: InstantiateUse (t, el, doc, FALSE);
1.1 vatton 946: break;
947: case Template_EL_attribute :
948: if (!loading)
949: InstantiateAttribute (t, el, doc);
950: break;
951: case Template_EL_repeat :
1.22 kia 952: InstantiateRepeat (t, el, doc, FALSE);
1.1 vatton 953: break;
954: default :
955: break;
956: }
957: }
958:
1.5 vatton 959: child = TtaGetFirstChild (el);
960: while (child)
1.1 vatton 961: {
962: aux = child;
1.5 vatton 963: TtaNextSibling (&aux);
964: ParseTemplate (t, child, doc, loading);
1.1 vatton 965: child = aux;
966: }
967: #endif /* TEMPLATES */
968: }
969:
970: /*----------------------------------------------------------------------
1.3 vatton 971: ----------------------------------------------------------------------*/
1.1 vatton 972: void DoInstanceTemplate (char *templatename)
973: {
974: #ifdef TEMPLATES
975: XTigerTemplate t;
976: ElementType elType;
977: Element root, piElem, doctype, elFound, text;
978: char *s, *charsetname = NULL, buffer[MAX_LENGTH];
979: int pi_type;
980: Document doc;
981:
982: //Instantiate all elements
1.35 kia 983: t = GetXTigerTemplate(templatename);
1.25 vatton 984: if (!t)
1.23 kia 985: return;
986:
1.1 vatton 987: doc = GetTemplateDocument (t);
988: root = TtaGetMainRoot (doc);
989: ParseTemplate (t, root, doc, FALSE);
990:
991: //Look for PIs
992: /* check if the document has a DOCTYPE declaration */
993: #ifdef ANNOTATIONS
994: if (DocumentTypes[doc] == docAnnot)
995: elType = TtaGetElementType (root);
996: else
997: #endif /* ANNOTATIONS */
998: elType = TtaGetElementType (root);
999: s = TtaGetSSchemaName (elType.ElSSchema);
1000: if (strcmp (s, "HTML") == 0)
1001: {
1002: elType.ElTypeNum = HTML_EL_DOCTYPE;
1003: pi_type = HTML_EL_XMLPI;
1004: }
1005: #ifdef _SVG
1006: else if (strcmp (s, "SVG") == 0)
1007: {
1008: elType.ElTypeNum = SVG_EL_DOCTYPE;
1009: pi_type = SVG_EL_XMLPI;
1010: }
1011: #endif /* _SVG */
1012: else if (strcmp (s, "MathML") == 0)
1013: {
1014: elType.ElTypeNum = MathML_EL_DOCTYPE;
1015: pi_type = MathML_EL_XMLPI;
1016: }
1017: else
1018: {
1019: elType.ElTypeNum = XML_EL_doctype;
1020: pi_type = XML_EL_xmlpi;
1021: }
1022: doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1023: if (!doctype)
1024: {
1025: /* generate the XML declaration */
1026: /* Check the Thot abstract tree against the structure schema. */
1027: TtaSetStructureChecking (FALSE, doc);
1028: elType.ElTypeNum = pi_type;
1029: doctype = TtaNewTree (doc, elType, "");
1030: TtaInsertFirstChild (&doctype, root, doc);
1031: elFound = TtaGetFirstChild (doctype);
1032: text = TtaGetFirstChild (elFound);
1033: strcpy (buffer, "xml version=\"1.0\" encoding=\"");
1034: charsetname = UpdateDocumentCharset (doc);
1035: strcat (buffer, charsetname);
1036: strcat (buffer, "\"");
1037: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc);
1038: TtaSetStructureChecking (TRUE, doc);
1.28 kia 1039: TtaFreeMemory(charsetname);
1.1 vatton 1040: }
1041:
1042: /* generate the XTiger PI */
1043: /* Check the Thot abstract tree against the structure schema. */
1044: TtaSetStructureChecking (FALSE, doc);
1045: elType.ElTypeNum = pi_type;
1046: piElem = TtaNewTree (doc, elType, "");
1.34 vatton 1047: TtaInsertSibling (piElem, doctype, FALSE, doc);
1.1 vatton 1048: elFound = TtaGetFirstChild (piElem);
1049: text = TtaGetFirstChild (elFound);
1050: strcpy (buffer, "xtiger template=\"");
1051: strcat (buffer, templatename);
1.17 kia 1052: strcat (buffer, "\" version=\"");
1.20 vatton 1053: if (t->version)
1054: strcat (buffer, t->version);
1055: else
1056: strcat (buffer, "0.8");
1.1 vatton 1057: strcat (buffer, "\"");
1.25 vatton 1058: if (t->templateVersion)
1.20 vatton 1059: {
1060: strcat (buffer, " templateVersion=\"");
1061: strcat (buffer, t->templateVersion);
1062: strcat (buffer, "\"");
1063: }
1.1 vatton 1064: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc);
1065: TtaSetStructureChecking (TRUE, doc);
1.5 vatton 1066:
1067: // update the document title
1068: if (!strcmp (s, "HTML"))
1069: {
1070: elType.ElTypeNum = HTML_EL_TITLE;
1071: elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1072: if (elFound)
1073: {
1074: elFound = TtaGetFirstChild (elFound);
1075: TtaSetTextContent (elFound, (unsigned char *)Answer_text,
1076: TtaGetDefaultLanguage (), doc);
1077: }
1078: }
1.1 vatton 1079: #endif /* TEMPLATES */
1080: }
1081:
1082: /*----------------------------------------------------------------------
1.35 kia 1083: Template_PreInstantiateComponents: Instantiates all components in order to improve
1.3 vatton 1084: editing.
1085: ----------------------------------------------------------------------*/
1.35 kia 1086: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1 vatton 1087: {
1088: #ifdef TEMPLATES
1.25 vatton 1089: if (!t)
1.23 kia 1090: return;
1091:
1.35 kia 1092: HashMap components = GetComponents(t);
1093: ForwardIterator iter = HashMap_GetForwardIterator(components);
1094: Declaration comp;
1095: HashMapNode node;
1096:
1097: ITERATOR_FOREACH(iter, HashMapNode, node)
1.1 vatton 1098: {
1.35 kia 1099: comp = (Declaration) node->elem;
1.1 vatton 1100: ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
1101: }
1.35 kia 1102: TtaFreeMemory(iter);
1.1 vatton 1103: #endif /* TEMPLATES */
1104: }
Webmaster