Annotation of Amaya/amaya/templateInstantiate.c, revision 1.117
1.19 vatton 1: /*
2: *
1.104 vatton 3: * COPYRIGHT INRIA and W3C, 2006-2009
1.19 vatton 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
7:
1.1 vatton 8: #include "templates.h"
9: #define THOT_EXPORT extern
1.71 vatton 10: #include "amaya.h"
1.75 quint 11: #include "css.h"
1.1 vatton 12: #include "templateDeclarations.h"
13:
1.8 kia 14: #include "Elemlist.h"
1.71 vatton 15: #include "AHTURLTools_f.h"
1.63 kia 16: #include "wxdialogapi_f.h"
1.1 vatton 17: #include "EDITimage_f.h"
1.84 kia 18: #include "HTMLedit_f.h"
1.1 vatton 19: #include "HTMLsave_f.h"
1.34 vatton 20: #include "HTMLtable_f.h"
1.49 vatton 21: #include "html2thot_f.h"
1.1 vatton 22: #include "init_f.h"
23: #include "templates_f.h"
24: #include "templateDeclarations_f.h"
25: #include "templateInstantiate_f.h"
26: #include "Templatebuilder_f.h"
1.63 kia 27: #include "templateLoad_f.h"
1.1 vatton 28: #include "templateUtils_f.h"
29: #include "fetchHTMLname_f.h"
30: #include "Template.h"
1.94 vatton 31: #include "fetchXMLname_f.h"
1.86 vatton 32: #include "styleparser_f.h"
1.94 vatton 33:
1.1 vatton 34: #ifdef TEMPLATES
35: #define TEMPLATE_SCHEMA_NAME "Template"
36: #endif /* TEMPLATES */
37:
38:
1.49 vatton 39:
40: /*----------------------------------------------------------------------
41: Template_InsertRepeatChildAfter
42: Insert a child to a xt:repeat
43: The decl parameter must be valid and will not be verified. It must be a
44: direct child element or the "use in the use" for union elements.
45: @param el element (xt:repeat) in which insert a new element
46: @param decl Template declaration of the element to insert
47: @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
48: @return The inserted element
49: ----------------------------------------------------------------------*/
50: Element Template_InsertRepeatChildAfter (Document doc, Element el,
51: Declaration decl, Element elPrev)
52: {
53: #ifdef TEMPLATES
1.104 vatton 54: Element child; /* First xt:use of the repeat.*/
55: Element use, parent; /* xt:use to insert.*/
1.49 vatton 56: ElementType useType; /* type of xt:use.*/
1.102 vatton 57: char *types = NULL;
1.49 vatton 58:
59: if (!TtaGetDocumentAccessMode (doc))
60: return NULL;
1.56 kia 61:
1.49 vatton 62: /* Copy xt:use with xt:types param */
1.104 vatton 63: child = TtaGetFirstChild (el);
64: useType = TtaGetElementType (child);
65: use = TtaCopyElement (child, doc, doc, el);
66: types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
1.117 ! vatton 67: if (IsTemplateInstanceDocument(doc) && useType.ElTypeNum != Template_EL_useSimple)
1.104 vatton 68: TtaChangeElementType (use, Template_EL_useSimple);
1.102 vatton 69: if (types)
70: {
71: SetAttributeStringValueWithUndo (use, Template_ATTR_types, types);
72: TtaFreeMemory (types);
73: }
74: else
75: SetAttributeStringValueWithUndo (use, Template_ATTR_types, decl->name);
1.49 vatton 76: /* insert it */
77: if (elPrev)
1.104 vatton 78: TtaInsertSibling (use, elPrev, FALSE, doc);
1.49 vatton 79: else
1.104 vatton 80: TtaInsertSibling (use, child, TRUE, doc);
81: // look for the enclosing target element
82: parent = GetParentLine (use, useType.ElSSchema);
83: Template_InsertUseChildren(doc, use, decl, parent, TRUE);
1.102 vatton 84: SetAttributeStringValueWithUndo (use, Template_ATTR_title, decl->name);
85: SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, decl->name);
1.49 vatton 86: TtaRegisterElementCreate (use, doc);
87: return use;
88: #else /* TEMPLATES */
89: return NULL;
90: #endif /* TEMPLATES */
91: }
92:
93: /*----------------------------------------------------------------------
94: Template_InsertBagChild
95: Insert a child to a xt:bag at the current insertion point.
96: The decl parameter must be valid and will not be verified.
1.102 vatton 97: @param sel the refered element. If NULL use the selection
98: @param bag element (xt:bag) in which insert a new element
1.49 vatton 99: @param decl Template declaration of the element to insert
100: @return The inserted element
101: ----------------------------------------------------------------------*/
1.102 vatton 102: Element Template_InsertBagChild (Document doc, Element sel, Element bag,
103: Declaration decl, ThotBool before)
1.49 vatton 104: {
105: #ifdef TEMPLATES
106: ElementType newElType, selType;
1.102 vatton 107: Element use = NULL, el, dummy = NULL;
108: SSchema sstempl;
109: int start, end;
110: ThotBool open;
1.49 vatton 111:
112: if (!TtaGetDocumentAccessMode (doc) || !decl)
113: return NULL;
114:
1.102 vatton 115: TtaGiveFirstSelectedElement (doc, &el, &start, &end);
116: if (sel == NULL)
117: sel = el;
118: if (sel == bag || TtaIsAncestor (sel, bag))
119: {
120: // opent the undo sequence if needed
121: open = TtaHasUndoSequence (doc);
122: if (!open)
123: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
124: sstempl = TtaGetSSchema ("Template", doc);
125: selType = TtaGetElementType (sel);
126: if (decl->blockLevel == 2 &&
127: (TtaIsLeaf (selType) || !IsTemplateElement (sel)))
128: {
129: // force the insertion of a block level element at the right position
130: while (sel && IsCharacterLevelElement (sel))
131: sel = TtaGetParent (sel);
132: if (sel)
133: TtaSelectElement (doc, sel);
134: }
135:
136: if (decl->nature == XmlElementNat)
137: {
138: if (el == NULL && sel != bag)
139: // force a selection
140: TtaSelectElement (doc, sel);
141: if (sel == bag)
142: {
143: // insert first an empty element
144: if (DocumentTypes[doc] == docHTML)
145: newElType.ElTypeNum = HTML_EL_Element;
146: else if (DocumentTypes[doc] == docMath)
147: newElType.ElTypeNum = MathML_EL_Construct;
148: else
149: newElType.ElTypeNum = XML_EL_XML_Element;
150: newElType.ElSSchema = TtaGetDocumentSSchema (doc);
151: dummy = TtaNewElement(doc, newElType);
152: TtaInsertFirstChild (&dummy, bag, doc);
153: TtaRegisterElementCreate (dummy, doc);
154: TtaSelectElement (doc, dummy);
155: }
156: GIType (decl->name, &newElType, doc);
157: TtaInsertAnyElement (doc, before);
158: TtaExtendUndoSequence (doc);
159: TtaInsertElement (newElType, doc);
160: TtaGiveFirstSelectedElement (doc, &sel, &start, &end);
161: if (dummy)
162: {
163: TtaRegisterElementDelete (dummy, doc);
164: TtaDeleteTree (dummy, doc);
165: }
166: }
167: else if (decl->nature == ComponentNat)
168: {
169: // create a use element
170: newElType.ElTypeNum = Template_EL_useSimple;
171: newElType.ElSSchema = sstempl;
172: use = TtaNewElement(doc, newElType);
173: if (use)
174: {
1.104 vatton 175: Template_InsertUseChildren (doc, use, decl, NULL, TRUE);
1.102 vatton 176: if (sel != bag)
177: TtaInsertSibling (use, sel, before, doc);
178: else
179: TtaInsertFirstChild (&use, bag, doc);
1.110 vatton 180: SetAttributeStringValueWithUndo (use, Template_ATTR_types, decl->name);
181: SetAttributeStringValueWithUndo (use, Template_ATTR_title, decl->name);
182: SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, decl->name);
1.102 vatton 183: TtaRegisterElementCreate (use, doc);
1.105 vatton 184:
1.102 vatton 185: sel = use;
186: }
187: }
188: else if (decl->nature == UnionNat)
189: {
190: newElType.ElTypeNum = Template_EL_useEl;
191: newElType.ElSSchema = sstempl;
192: }
193: // close the undo sequence
194: if (!open)
195: TtaCloseUndoSequence (doc);
196: return sel;
1.56 kia 197: }
1.49 vatton 198: #endif /* TEMPLATES */
199: return NULL;
200: }
201:
1.80 kia 202: /*----------------------------------------------------------------------
203: InstantiateAttribute
204: ----------------------------------------------------------------------*/
205: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
206: {
207: #ifdef TEMPLATES
208: AttributeType useType, nameType, defaultType, attrType;
209: Attribute useAttr, nameAttr, defAttr, attr;
210: ElementType elType;
211: Element parent;
212: char *text, *elementName;
213: ThotBool level;
214: NotifyAttribute event;
215: int val;
216:
217: parent = TtaGetParent (el);
218: if (!parent)
219: return;
220: // if attribute "use" has value "optional", don't do anything
221: useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
222: useType.AttrTypeNum = Template_ATTR_useAt;
223: useAttr = TtaGetAttribute (el, useType);
224: if (useAttr)
225: // there is a "use" attribute. Check its value
226: {
227: val = TtaGetAttributeValue(useAttr);
228: if (val == Template_ATTR_useAt_VAL_optional)
229: {
230: return;
231: }
232: }
233:
234: // get the "name" and "default" attributes
235: nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
236: nameType.AttrTypeNum = Template_ATTR_ref_name;
237: defaultType.AttrTypeNum = Template_ATTR_defaultAt;
238: nameAttr = TtaGetAttribute (el, nameType);
239: defAttr = TtaGetAttribute (el, defaultType);
240: if (nameAttr)
241: {
242: text = GetAttributeStringValue (el, nameAttr, NULL);
243: if (text)
244: {
245: elType = TtaGetElementType (parent);
246: elementName = TtaGetElementTypeName (elType);
247: level = TRUE;
248: MapHTMLAttribute (text, &attrType, elementName, &level, doc);
249: TtaFreeMemory(text);
250: attr = TtaNewAttribute (attrType);
251: if (attr)
252: {
253: TtaAttachAttribute (parent, attr, doc);
254: if (defAttr)
255: {
256: text = GetAttributeStringValue (el, defAttr, NULL);
257: if (text)
1.116 vatton 258: {
259: TtaSetAttributeText(attr, text, parent, doc);
260: TtaFreeMemory(text);
261: }
262: else if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
263: // if it's a src arttribute for an image, load the image
264: elType.ElTypeNum == HTML_EL_IMG)
1.80 kia 265: if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
266: attrType.AttrSSchema == elType.ElSSchema)
267: {
268: event.document = doc;
269: event.element = parent;
270: event.attribute = attr;
271: SRCattrModified (&event);
272: }
273: }
274: }
275: }
276: }
277: #endif /* TEMPLATES */
278: }
279:
280: /*----------------------------------------------------------------------
281: ParseTemplate
1.92 vatton 282: parentLine points to the enclosing pseudo paragraph or paragraph
1.99 vatton 283: Parameter loading is TRUE when the document is not already loaded.
1.92 vatton 284: Return the parentline to be considered for next elements
1.80 kia 285: ----------------------------------------------------------------------*/
1.113 vatton 286: Element ParseTemplate (XTigerTemplate t, Element el, Document doc,
287: Element parentLine, ThotBool loading)
1.80 kia 288: {
289: #ifdef TEMPLATES
290: AttributeType attType;
291: Attribute att;
1.101 vatton 292: Element next, child = NULL, savedInline, prev, parent = NULL;
1.103 vatton 293: ElementType elType, otherType;
1.92 vatton 294: Declaration dec;
1.114 vatton 295: int option;
1.94 vatton 296: char *name, *types;
1.114 vatton 297: ThotBool opt;
1.80 kia 298:
299: if (!t || !el)
1.92 vatton 300: return parentLine;
301:
302: savedInline = parentLine;
303: elType = TtaGetElementType (el);
1.94 vatton 304: attType.AttrSSchema = elType.ElSSchema;
1.80 kia 305: name = TtaGetSSchemaName (elType.ElSSchema);
306: if (!strcmp (name, "Template"))
307: {
1.92 vatton 308: switch (elType.ElTypeNum)
1.80 kia 309: {
310: case Template_EL_head :
311: //Remove it and all of its children
312: TtaDeleteTree(el, doc);
313: //We must stop searching into this tree
1.92 vatton 314: return parentLine;
1.80 kia 315: case Template_EL_component :
316: // remove the name attribute
317: attType.AttrTypeNum = Template_ATTR_name;
1.87 vatton 318: name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);
1.80 kia 319: TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
320: // replace the component by a use
1.93 vatton 321: prev = el;
322: TtaPreviousSibling (&prev);
323: if (prev == NULL)
324: {
325: next = el;
326: TtaNextSibling (&next);
327: if (next == NULL)
328: parent = TtaGetParent (el);
329: }
330: TtaRemoveTree (el, doc);
331: TtaChangeElementType (el, Template_EL_useSimple);
1.80 kia 332: // generate the types attribute
333: attType.AttrTypeNum = Template_ATTR_types;
334: att = TtaNewAttribute (attType);
335: TtaAttachAttribute (el, att, doc);
336: if (name)
337: TtaSetAttributeText (att, name, el, doc);
338: // generate the title attribute
339: attType.AttrTypeNum = Template_ATTR_title;
340: att = TtaNewAttribute (attType);
341: TtaAttachAttribute (el, att, doc);
342: if (name)
343: TtaSetAttributeText (att, name, el, doc);
344: // generate the currentType attribute
345: attType.AttrTypeNum = Template_ATTR_currentType;
346: att = TtaNewAttribute (attType);
347: TtaAttachAttribute (el, att, doc);
348: if (name)
349: TtaSetAttributeText (att, name, el, doc);
1.93 vatton 350: /* now reinsert the element new map */
351: if (prev != NULL)
352: TtaInsertSibling (el, prev, FALSE, doc);
353: else if (next != NULL)
354: TtaInsertSibling (el, next, TRUE, doc);
355: else
356: TtaInsertFirstChild (&el, parent, doc);
1.80 kia 357: TtaFreeMemory(name);
1.94 vatton 358: Template_FixAccessRight (t, el, doc);
359: TtaUpdateAccessRightInViews (doc, el);
1.80 kia 360: break;
361: case Template_EL_bag :
1.105 vatton 362: Template_FixAccessRight (t, el, doc);
363: TtaUpdateAccessRightInViews (doc, el);
1.80 kia 364: break;
365: case Template_EL_useEl :
366: case Template_EL_useSimple :
367: /* if this use element is not empty, don't do anything: it is
368: supposed to contain a valid instance. This should be
369: checked, though */
370: // add the initial indicator
1.94 vatton 371: types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
372: if (types)
1.87 vatton 373: {
1.94 vatton 374: child = TtaGetFirstChild (el);
375: if (!strcmp (types, "string") || !strcmp (types, "number"))
1.87 vatton 376: AddPromptIndicator (el, doc);
1.92 vatton 377: else
378: {
379: // avoid to have a block element within a pseudo paragraph
1.94 vatton 380: dec = Template_GetDeclaration (t, types);
1.92 vatton 381: if (dec && dec->blockLevel == 2 && parentLine)
382: {
383: // move the use element after the paragraph
384: child = TtaGetParent (el);
1.94 vatton 385: otherType = TtaGetElementType (child);
386: if (otherType.ElSSchema != elType.ElSSchema ||
387: otherType.ElTypeNum == Template_EL_repeat)
1.92 vatton 388: // not need to move the parent element
389: child = el;
390: next = child;
391: prev = parentLine;
392: while (child)
393: {
394: // move the element and next siblings after the pseudo paragraph
395: TtaNextSibling (&next);
396: TtaRemoveTree (child, doc);
397: TtaInsertSibling (child, prev, FALSE, doc);
398: prev = child;
399: child = next;
400: }
401: // elements are now out of the parent line
402: savedInline = NULL;
1.97 vatton 403: parentLine = NULL;
1.109 vatton 404: child = TtaGetFirstChild (el);
1.92 vatton 405: }
1.94 vatton 406:
407: // generate the currentType attribute
408: otherType = TtaGetElementType (child);
1.109 vatton 409: if (otherType.ElSSchema == elType.ElSSchema &&
410: otherType.ElTypeNum == Template_EL_TemplateObject)
1.94 vatton 411: {
1.109 vatton 412: // not already instantiated
413: TtaDeleteTree (child, doc);
414: child = NULL;
1.94 vatton 415: }
416: else
417: {
1.109 vatton 418: attType.AttrTypeNum = Template_ATTR_currentType;
419: att = TtaGetAttribute (el, attType);
420: if (att == NULL)
421: {
422: att = TtaNewAttribute (attType);
423: TtaAttachAttribute (el, att, doc);
424: }
425: if (otherType.ElTypeNum == 1)
426: TtaSetAttributeText (att, "string", el, doc);
427: else
428: {
429: name = (char *)GetXMLElementName (otherType, doc);
430: if (name && strcmp (name,"???"))
431: TtaSetAttributeText (att, name, el, doc);
432: }
1.94 vatton 433: }
1.92 vatton 434: }
1.87 vatton 435: }
1.94 vatton 436: if (child == NULL)
1.114 vatton 437: {
438: option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
1.115 vatton 439: opt = (option == 0 || option == Template_ATTR_option_VAL_option_set);
440: if (opt)
441: InstantiateUse (t, el, doc, parentLine, FALSE);
1.114 vatton 442: }
1.94 vatton 443: else
444: {
445: Template_FixAccessRight (t, el, doc);
446: TtaUpdateAccessRightInViews (doc, el);
447: }
1.109 vatton 448: TtaFreeMemory (types);
1.80 kia 449: break;
450: case Template_EL_attribute :
451: if (!loading)
452: InstantiateAttribute (t, el, doc);
453: break;
454: case Template_EL_repeat :
1.104 vatton 455: InstantiateRepeat (t, el, doc, parentLine, FALSE);
1.80 kia 456: break;
457: default :
458: break;
459: }
460: }
1.92 vatton 461: else if (!strcmp (name, "HTML") &&
462: (elType.ElTypeNum == HTML_EL_Pseudo_paragraph ||
463: elType.ElTypeNum == HTML_EL_Paragraph))
464: parentLine = el;
1.80 kia 465:
466: child = TtaGetFirstChild (el);
467: while (child)
468: {
1.92 vatton 469: next = child;
470: TtaNextSibling (&next);
471: parentLine = ParseTemplate (t, child, doc, parentLine, loading);
472: child = next;
1.80 kia 473: }
1.92 vatton 474: return savedInline;
1.80 kia 475: #endif /* TEMPLATES */
476: }
477:
1.63 kia 478: /*----------------------------------------------------------------------
479: CreateTemplate
480: Create a template from any document.
481: ----------------------------------------------------------------------*/
1.89 vatton 482: void CreateTemplate (Document doc, char *templatePath)
1.63 kia 483: {
484: #ifdef TEMPLATES
485: Element root, head, elem, xt, title, child, last;
486: ElementType elType, xtType;
1.64 kia 487: char *s;
1.63 kia 488: SSchema templSchema;
489: XTigerTemplate t;
490:
1.89 vatton 491: if (IsTemplateInstanceDocument(doc))
1.63 kia 492: {
493: ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
494: TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
495: return;
496: }
497:
1.89 vatton 498: if (IsTemplateDocument(doc))
1.63 kia 499: {
500: ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
501: TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
502: return;
503: }
504:
1.64 kia 505: root = TtaGetRootElement(doc);
506: elType = TtaGetElementType (root);
507: // get the target document type
508: s = TtaGetSSchemaName (elType.ElSSchema);
1.63 kia 509:
1.64 kia 510: TtaNewNature (doc, elType.ElSSchema, NULL, "Template", "TemplateP");
1.96 vatton 511: TtaSetANamespaceDeclaration (doc, root, "xt", Template_URI);
512: templSchema = TtaGetSSchema ("Template", doc);
513: TtaSetUriSSchema (templSchema, Template_URI);
1.64 kia 514:
515: // Insert xt:head and others
516: TtaSetStructureChecking (FALSE, doc);
517: if (strcmp (s, "HTML") == 0)
1.63 kia 518: {
1.64 kia 519: // Initialize the xt:head
520: elType.ElTypeNum = HTML_EL_HEAD;
1.101 vatton 521: xtType.ElSSchema = templSchema;
1.64 kia 522: head = TtaSearchTypedElement (elType, SearchInTree, root);
523: if(head)
524: {
525: xtType.ElTypeNum = Template_EL_head;
526: xt = TtaNewElement(doc, xtType);
527: elem = TtaGetLastChild(head);
528: if(elem)
529: TtaInsertSibling(xt, elem, FALSE, doc);
530: else
531: TtaInsertFirstChild(&xt, head, doc);
532:
533: SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
534: SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
535: }
1.63 kia 536:
1.64 kia 537: // Initialize the document title
538: elType.ElTypeNum = HTML_EL_TITLE;
539: title = TtaSearchTypedElement (elType, SearchInTree, root);
1.96 vatton 540: if (title)
1.63 kia 541: {
1.64 kia 542: // Create xt:use for title
543: xtType.ElTypeNum = Template_EL_useSimple;
1.96 vatton 544: xt = TtaNewElement (doc, xtType);
1.64 kia 545: TtaInsertFirstChild(&xt, title, doc);
1.96 vatton 546: SetAttributeStringValue (xt, Template_ATTR_types, "string");
547: SetAttributeStringValue (xt, Template_ATTR_title, "title");
1.64 kia 548:
549: // Move current title content to xt:use
550: last = NULL;
551: while(child = TtaGetLastChild(title), child!=NULL)
1.63 kia 552: {
1.96 vatton 553: if (child == xt)
1.64 kia 554: break;
1.96 vatton 555: TtaRemoveTree (child, doc);
556: if (last)
557: TtaInsertSibling (child, last, FALSE, doc);
1.63 kia 558: else
1.96 vatton 559: TtaInsertFirstChild (&child, xt, doc);
1.64 kia 560: last = child;
1.63 kia 561: }
562: }
1.64 kia 563: }
564: else
565: {
566: xtType.ElSSchema = templSchema;
567: xtType.ElTypeNum = Template_EL_head;
1.96 vatton 568: xt = TtaNewElement (doc, xtType);
569: TtaInsertFirstChild (&xt, root, doc);
570: SetAttributeStringValue (xt, Template_ATTR_version, Template_Current_Version);
571: SetAttributeStringValue (xt, Template_ATTR_templateVersion, "1.0");
1.64 kia 572: }
573: // Save changes
574: TtaSetStructureChecking (TRUE, doc);
1.86 vatton 575: if (DocumentTypes[doc] == docHTML)
576: // avoid positionned boxes to overlap the xt:head section
577: SetBodyAbsolutePosition (doc);
578:
1.64 kia 579: TtaClearUndoHistory (doc);
580: RemoveParsingErrors (doc);
1.63 kia 581:
1.64 kia 582: TtaFreeMemory(DocumentURLs[doc]);
583: DocumentURLs[doc] = TtaStrdup(templatePath);
584:
585: if(DocumentMeta[doc]==NULL)
586: DocumentMeta[doc] = DocumentMetaDataAlloc();
587:
588: DocumentMeta[doc]->method = CE_TEMPLATE;
589: if(DocumentMeta[doc]->initial_url)
590: {
591: TtaFreeMemory(DocumentMeta[doc]->initial_url);
592: DocumentMeta[doc]->initial_url = NULL;
593: }
594: TtaSetDocumentModified (doc);
1.63 kia 595:
1.64 kia 596: // Load template-related infos :
597: // like LoadTemplate(..)
598: t = LookForXTigerTemplate(templatePath);
599: t->doc = doc;
1.89 vatton 600: Template_PrepareTemplate(t, doc);
1.67 kia 601: // DocumentTypes[doc] = docTemplate;
602: t->state |= templloaded|templTemplate;
1.63 kia 603:
1.98 vatton 604: #ifdef TEMPLATE_DEBUG
1.64 kia 605: DumpAllDeclarations();
1.98 vatton 606: #endif /* TEMPLATE_DEBUG */
1.64 kia 607:
608: /* Update the URL combo box */
609: AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
610: TtaSetTextZone (doc, 1, URL_list);
611: /* Update template menus */
612: UpdateTemplateMenus(doc);
613:
1.63 kia 614: #endif /* TEMPLATES */
615: }
616:
1.1 vatton 617: /*----------------------------------------------------------------------
618: CreateInstance
1.41 vatton 619: basedoc is the displayed doc that launchs the creation of instance
1.3 vatton 620: ----------------------------------------------------------------------*/
1.99 vatton 621: void CreateInstance (char *templatePath, char *instancePath,
622: char *docname, DocumentType docType, int basedoc)
1.3 vatton 623: {
1.1 vatton 624: #ifdef TEMPLATES
1.41 vatton 625: Document doc = 0, newdoc = 0;
1.90 vatton 626: Element root, title, text;
1.41 vatton 627: ElementType elType;
1.90 vatton 628: CHARSET charset, ocharset;
629: char *s, *charsetname, *ocharsetname, *localFile;
1.1 vatton 630:
1.35 kia 631: XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1 vatton 632: if (t == NULL)
1.29 vatton 633: {
634: // the template cannot be loaded
635: InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
636: return;
637: }
1.41 vatton 638: // the template document
1.5 vatton 639: doc = GetTemplateDocument (t);
1.41 vatton 640: // localize the new created document
641: if (DontReplaceOldDoc)
642: newdoc = TtaGetNextDocumentIndex ();
643: else
644: newdoc = basedoc;
1.90 vatton 645:
646: // close current undo sepquence in the template document
647: if (TtaHasUndoSequence (doc))
648: TtaCloseUndoSequence (doc);
649:
650: // update the charset if needed
651: charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
652: charset = TtaGetCharset (charsetname);
653: ocharsetname = DocumentMeta[doc]->charset;
654: ocharset = TtaGetCharset (ocharsetname);
655: if (charset != UNDEFINED_CHARSET &&
656: DocumentMeta[doc]->charset &&
657: strcmp (charsetname, DocumentMeta[doc]->charset))
658: {
659: TtaSetDocumentCharset (doc, charset, FALSE);
660: DocumentMeta[doc]->charset = TtaStrdup (charsetname);
661: SetNamespacesAndDTD (doc, FALSE);
662: }
1.54 vatton 663:
1.72 vatton 664: // register the document type to open the right page model
665: DocumentTypes[newdoc] = docType;
1.90 vatton 666: // Generate the instance content as a copy of the template
667: localFile = SaveDocumentToNewDoc(doc, newdoc, instancePath);
668: Template_PrepareInstance (instancePath, newdoc, t->version, templatePath);
669: Template_AddReference (t);
670:
671: // Revert template changes
672: TtaSetDocumentCharset (doc, ocharset, FALSE);
1.111 vatton 673: TtaFreeMemory (DocumentMeta[doc]->charset);
1.90 vatton 674: DocumentMeta[doc]->charset = ocharsetname;
675: // Now parse the instance
676: // The xtiger PI will be added and components will be removed
677: GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
678: !DontReplaceOldDoc, NULL, NULL);
679: if (DocumentMeta[newdoc])
680: DocumentMeta[newdoc]->method = CE_ABSOLUTE;
681: // Generate the HTML document title
682: root = TtaGetRootElement(newdoc);
683: elType = TtaGetElementType (root);
684: // get the target document type
685: s = TtaGetSSchemaName (elType.ElSSchema);
686: if (strcmp (s, "HTML") == 0)
1.1 vatton 687: {
1.90 vatton 688: // Initialize the document title
689: elType.ElTypeNum = HTML_EL_TITLE;
690: title = TtaSearchTypedElement (elType, SearchInTree, root);
691: text = TtaGetFirstChild (title);
692: while (text)
1.1 vatton 693: {
1.90 vatton 694: elType = TtaGetElementType (text);
695: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1.14 vatton 696: {
1.90 vatton 697: TtaSetTextContent (text, (unsigned char*)Answer_text,
698: TtaGetDefaultLanguage (), newdoc);
699: text = NULL;
700: SetNewTitle (newdoc);
1.14 vatton 701: }
1.90 vatton 702: else if ((elType.ElTypeNum == Template_EL_useEl ||
703: elType.ElTypeNum == Template_EL_useSimple) &&
704: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
705: // Ignore the template use element
706: text = TtaGetFirstChild (text);
707: else
708: // Look for the first text child
709: TtaNextSibling (&text);
1.63 kia 710: }
1.90 vatton 711: }
1.54 vatton 712:
1.90 vatton 713: // Insert XTiger PI
714: Template_InsertXTigerPI(newdoc, t);
715: // Parse template to fill structure and remove extra data
1.92 vatton 716: ParseTemplate (t, root, newdoc, NULL, FALSE);
1.90 vatton 717: TtaFreeMemory (localFile);
718: TtaClearUndoHistory (newdoc);
719: RemoveParsingErrors (newdoc);
720: TtaSetDocumentModified (newdoc);
721: UpdateTemplateMenus(newdoc);
1.1 vatton 722: #endif /* TEMPLATES */
723: }
724:
725:
726: #ifdef TEMPLATES
727: /*----------------------------------------------------------------------
728: ProcessAttr
1.9 vatton 729: Look for all "attribute" elements in the subtree and instantiate them
1.3 vatton 730: ----------------------------------------------------------------------*/
1.1 vatton 731: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
732: {
733: Element child;
734: ElementType elType;
735:
736: for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
737: {
738: elType = TtaGetElementType (child);
739: if (elType.ElTypeNum == Template_EL_attribute &&
740: !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
741: InstantiateAttribute (t, child, doc);
742: else
743: ProcessAttr (t, child, doc);
744: }
745: }
746: #endif /* TEMPLATES */
747:
1.12 kia 748:
749: /*----------------------------------------------------------------------
750: Template_GetNewSimpleTypeInstance
1.109 vatton 751: Create an new instance of xt:use/SimpleType for the document doc.
752: Return the new element
1.12 kia 753: ----------------------------------------------------------------------*/
1.109 vatton 754: Element Template_GetNewSimpleTypeInstance(Document doc)
1.12 kia 755: {
756: Element newEl = NULL;
757: #ifdef TEMPLATES
758: ElementType elType;
1.73 kia 759: const char *empty = " ";
1.24 vatton 760:
1.38 vatton 761: elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12 kia 762: elType.ElTypeNum = Template_EL_TEXT_UNIT;
763: newEl = TtaNewElement (doc, elType);
764: TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24 vatton 765: #endif /* TEMPLATES */
1.12 kia 766: return newEl;
767: }
768:
769: /*----------------------------------------------------------------------
770: Template_GetNewXmlElementInstance
1.109 vatton 771: Create an new instance of xt:use/XmlElement for the document doc.
772: The parameter decl gives the type of the element of new element.
773: Return the new element
1.12 kia 774: ----------------------------------------------------------------------*/
1.109 vatton 775: Element Template_GetNewXmlElementInstance(Document doc, Declaration decl)
1.12 kia 776: {
777: Element newEl = NULL;
778: #ifdef TEMPLATES
779: ElementType elType;
780:
1.24 vatton 781: GIType (decl->name, &elType, doc);
782: if (elType.ElTypeNum != 0)
1.23 kia 783: newEl = TtaNewTree (doc, elType, "");
1.12 kia 784: #endif /* TEMPLATES */
785: return newEl;
786: }
787:
1.34 vatton 788:
789: /*----------------------------------------------------------------------
790: InsertWithNotify applies pre and post functions when inserting the new
791: element el after child (if not NULL) or as first child of parent.
792: ----------------------------------------------------------------------*/
793: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
794: {
795: ElementType elType;
1.116 vatton 796: AttributeType attrType;
797: Attribute attr;
1.34 vatton 798: NotifyElement event;
799: char *name;
800: ThotBool isRow = FALSE, isCell = FALSE;
1.50 vatton 801: ThotBool isImage = FALSE;
802: ThotBool oldStructureChecking;
803:
804: // avoid to check attributes now
805: oldStructureChecking = TtaGetStructureChecking (doc);
806: TtaSetStructureChecking (FALSE, doc);
1.34 vatton 807:
808: elType = TtaGetElementType (el);
809: name = TtaGetSSchemaName (elType.ElSSchema);
810: isCell = ((!strcmp (name,"HTML") &&
811: elType.ElTypeNum == HTML_EL_Data_cell ||
812: elType.ElTypeNum == HTML_EL_Heading_cell) ||
813: (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
814: isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
815: (!strcmp (name,"MathML") &&
816: (elType.ElTypeNum == MathML_EL_MTR ||
817: elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50 vatton 818: isImage = (!strcmp (name,"HTML") &&
819: (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34 vatton 820: if (child)
821: TtaInsertSibling (el, child, FALSE, doc);
822: else
823: TtaInsertFirstChild (&el, parent, doc);
1.50 vatton 824: TtaSetStructureChecking (oldStructureChecking, doc);
1.34 vatton 825:
1.50 vatton 826: if (isImage)
1.116 vatton 827: {
828: // check if the src attribute is there
829: attrType.AttrSSchema = elType.ElSSchema;
830: if (elType.ElTypeNum == HTML_EL_IMG)
831: attrType.AttrTypeNum = HTML_ATTR_SRC;
832: else
833: attrType.AttrTypeNum = HTML_ATTR_data;
834: attr = TtaGetAttribute (el, attrType);
835: if (attr == NULL)
836: InsertImageOrObject (el, doc);
837: }
1.50 vatton 838: else if (isCell)
1.34 vatton 839: {
840: // a cell is created
1.39 quint 841: NewCell (el, doc, TRUE, TRUE, TRUE);
1.34 vatton 842: }
843: else if (isRow)
844: {
845: // a row is created
846: event.element = el;
847: event.document = doc;
848: RowPasted (&event);
849: }
1.50 vatton 850:
851: if (!strcmp (name,"HTML"))
852: {
1.104 vatton 853: // special management for images and objets
1.50 vatton 854: elType.ElTypeNum = HTML_EL_IMG;
855: child = TtaSearchTypedElement (elType, SearchInTree, el);
856: while (child)
857: {
858: InsertImageOrObject (child, doc);
859: child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
860: }
861: elType.ElTypeNum = HTML_EL_Object;
862: child = TtaSearchTypedElement (elType, SearchInTree, el);
863: while (child)
864: {
865: InsertImageOrObject (child, doc);
866: child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
867: }
868: }
1.34 vatton 869: return el;
870: }
871:
872:
1.12 kia 873: /*----------------------------------------------------------------------
1.16 kia 874: Template_InsertUseChildren
875: Insert children to a xt:use
876: The dec parameter must be valid and will not be verified. It must be a
877: direct child element (for union elements).
878: @param el element (xt:use) in which insert a new element
879: @param dec Template declaration of the element to insert
880: @return The inserted element (the xt:use element if insertion is multiple as component)
1.104 vatton 881: The parentLine parameter points to the enclosing line if any.
1.16 kia 882: ----------------------------------------------------------------------*/
1.104 vatton 883: Element Template_InsertUseChildren (Document doc, Element el, Declaration dec,
884: Element parentLine, ThotBool registerUndo)
1.16 kia 885: {
1.99 vatton 886: Element newEl = NULL;
1.16 kia 887: #ifdef TEMPLATES
1.99 vatton 888: Element current = NULL;
1.104 vatton 889: Element child = NULL, prev, next;
890: ElementType childType, elType;
891: SSchema sshtml;
1.99 vatton 892: XTigerTemplate t;
1.17 kia 893:
1.25 vatton 894: if (TtaGetDocumentAccessMode(doc))
1.16 kia 895: {
1.23 kia 896: switch (dec->nature)
897: {
898: case SimpleTypeNat:
1.109 vatton 899: newEl = Template_GetNewSimpleTypeInstance(doc);
1.34 vatton 900: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23 kia 901: break;
902: case XmlElementNat:
1.109 vatton 903: newEl = Template_GetNewXmlElementInstance(doc, dec);
1.34 vatton 904: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23 kia 905: break;
906: case ComponentNat:
907: newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74 kia 908: ProcessAttr (dec->usedIn, newEl, doc);
1.104 vatton 909: elType = TtaGetElementType (el);
1.23 kia 910: /* Copy elements from new use to existing use. */
1.98 vatton 911: #ifdef TEMPLATE_DEBUG
1.74 kia 912: DumpSubtree(newEl, doc, 0);
1.98 vatton 913: #endif /* TEMPLATE_DEBUG */
1.104 vatton 914: sshtml = TtaGetSSchema ("HTML", doc);
1.102 vatton 915: t = GetXTigerDocTemplate( doc);
916: child = TtaGetFirstChild (newEl);
1.76 vatton 917: while (child)
918: {
919: // move the new subtree to the document
920: TtaRemoveTree (child, doc);
1.104 vatton 921: childType = TtaGetElementType (child);
922: if (parentLine)
923: {
924: if (childType.ElSSchema == sshtml &&
925: childType.ElTypeNum == HTML_EL_Pseudo_paragraph)
926: {
927: prev = TtaGetFirstChild (child);
928: while (prev)
929: {
930: next = prev;
931: TtaNextSibling (&next);
932: TtaRemoveTree (prev, doc);
933: current = InsertWithNotify (prev, current, el, doc);
934: prev = next;
935: }
1.108 vatton 936: TtaDeleteTree (child, doc);
1.104 vatton 937: }
1.108 vatton 938: else
939: current = InsertWithNotify (child, current, el, doc);
1.104 vatton 940: }
941: else
942: {
943: current = InsertWithNotify (child, current, el, doc);
944: // check if a new paragraph is inserted
945: if (childType.ElSSchema == sshtml &&
946: childType.ElTypeNum == HTML_EL_Paragraph)
947: Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
948: else
949: {
950: childType.ElSSchema = sshtml;
951: childType.ElTypeNum = HTML_EL_Paragraph;
952: child = TtaSearchTypedElement (childType, SearchInTree, current);
953: while (child)
954: {
955: Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
956: child = TtaSearchTypedElement (childType, SearchInTree, child);
957: }
958: }
959: }
1.99 vatton 960: child = TtaGetFirstChild (newEl);
1.76 vatton 961: }
1.99 vatton 962:
1.102 vatton 963: TtaDeleteTree (newEl, doc);
1.23 kia 964: newEl = el;
965: break;
966: default :
967: //Impossible
968: break;
969: }
1.44 kia 970: Template_FixAccessRight (dec->usedIn, el, doc);
1.105 vatton 971: if (dec->nature == ComponentNat)
972: Component_FixAccessRight (el, doc);
1.102 vatton 973: TtaUpdateAccessRightInViews (doc, el);
1.23 kia 974: }
1.16 kia 975: #endif /* TEMPLATES */
976: return newEl;
977: }
978:
1.40 kia 979:
980: /*----------------------------------------------------------------------
1.105 vatton 981: Component_FixAccessRight locks children of the component
982: ----------------------------------------------------------------------*/
983: void Component_FixAccessRight (Element el, Document doc)
984: {
985: #ifdef TEMPLATES
986: Element child;
987:
988: if (el && doc)
989: {
990: TtaSetAccessRight (el, ReadOnly, doc);
991: // fix access right to children
992: child = TtaGetFirstChild (el);
993: while (child)
994: {
995: TtaSetAccessRight (child, ReadOnly, doc);
996: TtaNextSibling (&child);
997: }
998: }
999: #endif /* TEMPLATES */
1000: }
1001:
1002: /*----------------------------------------------------------------------
1.97 vatton 1003: Template_FixAccessRight fixes access rights of the el element
1.40 kia 1004: ----------------------------------------------------------------------*/
1.41 vatton 1005: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40 kia 1006: {
1007: #ifdef TEMPLATES
1008: ElementType elType;
1009: Element child;
1.97 vatton 1010: Declaration decl;
1.88 vatton 1011: char currentType[MAX_LENGTH], *ptr;
1.40 kia 1012:
1013: if (t && el && doc)
1014: {
1015: elType = TtaGetElementType(el);
1.41 vatton 1016: if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40 kia 1017: {
1.41 vatton 1018: switch (elType.ElTypeNum)
1.40 kia 1019: {
1020: case Template_EL_TEXT_UNIT:
1.41 vatton 1021: //TtaSetAccessRight( el, ReadWrite, doc);
1022: return;
1.105 vatton 1023: case Template_EL_component:
1024: Component_FixAccessRight (el, doc);
1025: break;
1.40 kia 1026: case Template_EL_useEl:
1027: case Template_EL_useSimple:
1028: GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
1029: (char*)currentType, NULL);
1.88 vatton 1030: if (currentType[0] == EOS)
1031: {
1032: GiveAttributeStringValueFromNum(el, Template_ATTR_types,
1033: (char*)currentType, NULL);
1034: ptr = strstr (currentType, " ");
1035: if (ptr)
1036: *ptr = EOS;
1037: }
1.40 kia 1038: decl = Template_GetDeclaration(t, currentType);
1039: if (decl)
1040: {
1041: switch (decl->nature)
1042: {
1043: case SimpleTypeNat:
1.41 vatton 1044: TtaSetAccessRight (el, ReadWrite, doc);
1045: return;
1.105 vatton 1046: case ComponentNat:
1.107 vatton 1047: TtaSetAccessRight (el, ReadOnly, doc);
1048: break;
1049: //Component_FixAccessRight (el, doc);
1050: //return;
1.97 vatton 1051: case XmlElementNat:
1.105 vatton 1052: if (TtaIsSetReadOnly (el))
1053: break;
1.97 vatton 1054: child = TtaGetFirstChild (el);
1055: if (child)
1056: TtaSetAccessRight (child, ReadWrite, doc);
1.40 kia 1057: default:
1.41 vatton 1058: TtaSetAccessRight (el, ReadOnly, doc);
1059: break;
1.40 kia 1060: }
1061: }
1062: break;
1063: case Template_EL_bag:
1.45 vatton 1064: case Template_EL_repeat:
1.40 kia 1065: TtaSetAccessRight(el, ReadWrite, doc);
1066: break;
1067: default:
1068: TtaSetAccessRight(el, ReadOnly, doc);
1069: break;
1070: }
1071: }
1072:
1.92 vatton 1073: // fix access right to children
1.41 vatton 1074: child = TtaGetFirstChild (el);
1075: while (child)
1.40 kia 1076: {
1.41 vatton 1077: Template_FixAccessRight (t, child, doc);
1078: TtaNextSibling (&child);
1.40 kia 1079: }
1080: }
1081: #endif /* TEMPLATES */
1082: }
1083:
1.16 kia 1084: /*----------------------------------------------------------------------
1.46 vatton 1085: AddPromptIndicator
1086: ----------------------------------------------------------------------*/
1087: void AddPromptIndicator (Element el, Document doc)
1088: {
1089: #ifdef TEMPLATES
1090: ElementType elType;
1091: AttributeType attrType;
1092: Attribute att;
1093:
1.66 vatton 1094: if (el)
1095: {
1096: elType = TtaGetElementType (el);
1097: attrType.AttrSSchema = elType.ElSSchema;
1098: attrType.AttrTypeNum = Template_ATTR_prompt;
1.69 vatton 1099: att = TtaGetAttribute (el, attrType);
1100: if (att == NULL)
1101: {
1102: att = TtaNewAttribute (attrType);
1103: TtaAttachAttribute (el, att, doc);
1104: }
1.66 vatton 1105: }
1.46 vatton 1106: #endif /* TEMPLATES */
1107: }
1108:
1109: /*----------------------------------------------------------------------
1.104 vatton 1110: Template_SetInline manages inline elements
1111: registerUndo says if changes must be registered
1112: ----------------------------------------------------------------------*/
1113: void Template_SetInline (Element el, SSchema sstempl, Document doc, ThotBool registerUndo)
1114: {
1115: #ifdef TEMPLATES
1116: Element child = NULL;
1117: ElementType elType1, elType2, elType3;
1118:
1119: if (el)
1120: {
1121: elType1 = TtaGetElementType (el);
1122: if (elType1.ElSSchema == sstempl)
1123: // apply to hte current template element
1124: SetAttributeIntValue (el, Template_ATTR_SetInLine,
1125: Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
1126: else
1127: elType1.ElSSchema = sstempl;
1128: elType1.ElTypeNum = Template_EL_useSimple;
1129: elType2.ElTypeNum = Template_EL_useEl;
1130: elType2.ElSSchema = elType1.ElSSchema;
1131: elType3.ElTypeNum = Template_EL_repeat;
1132: elType3.ElSSchema = elType1.ElSSchema;
1133: child = TtaSearchElementAmong5Types (elType1, elType2, elType3, elType3, elType3,
1134: SearchForward, el);
1135: while (child && TtaIsAncestor (child, el))
1136: {
1137: SetAttributeIntValue (child, Template_ATTR_SetInLine,
1138: Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
1139: child = TtaSearchElementAmong5Types (elType1, elType2,
1140: elType3, elType3, elType3,
1141: SearchForward, child);
1142: }
1143: }
1144: #endif /* TEMPLATES */
1145: }
1146:
1147: /*----------------------------------------------------------------------
1148: InstantiateUse intantiate the use element el.
1149: The parentLine parameter points to the enclosing line if any.
1.3 vatton 1150: ----------------------------------------------------------------------*/
1.1 vatton 1151: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.104 vatton 1152: Element parentLine, ThotBool registerUndo)
1.1 vatton 1153: {
1154: #ifdef TEMPLATES
1.109 vatton 1155: Element child = NULL;
1.1 vatton 1156: ElementType elType;
1157: Declaration dec;
1.109 vatton 1158: int size, nbitems, i, option;
1.1 vatton 1159: struct menuType *items;
1.104 vatton 1160: char *types;
1.109 vatton 1161: ThotBool oldStructureChecking, hidden;
1.1 vatton 1162:
1.25 vatton 1163: if (!t)
1.23 kia 1164: return NULL;
1165:
1.1 vatton 1166: /* get the value of the "types" attribute */
1.12 kia 1167: elType = TtaGetElementType (el);
1.32 vatton 1168: types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36 vatton 1169: if (!types || types[0] == EOS)
1170: {
1171: TtaFreeMemory (types);
1172: return NULL;
1173: }
1.104 vatton 1174: if (!strcmp (types, "string") || !strcmp (types, "number"))
1.109 vatton 1175: {
1176: child = TtaGetFirstChild (el);
1177: if (child == NULL)
1178: {
1179: child = Template_GetNewSimpleTypeInstance(doc);
1180: child = InsertWithNotify (child, NULL, el, doc);
1181: }
1182: AddPromptIndicator (el, doc);
1183: }
1.104 vatton 1184: else
1.1 vatton 1185: {
1.104 vatton 1186: giveItems (types, size, &items, &nbitems);
1187: // No structure checking
1188: oldStructureChecking = TtaGetStructureChecking (doc);
1189: TtaSetStructureChecking (FALSE, doc);
1.109 vatton 1190: hidden = ElementIsOptional (el);
1191: if (hidden)
1192: {
1193: // check the current option value
1194: option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
1195: hidden = (option == Template_ATTR_option_VAL_option_unset);
1196: }
1197: if (nbitems == 1 || !hidden)
1.104 vatton 1198: /* only one type in the "types" attribute */
1199: {
1200: dec = Template_GetDeclaration (t, items[0].label);
1201: if (dec)
1.110 vatton 1202: child = Template_InsertUseChildren (doc, el, dec, parentLine, registerUndo);
1.112 vatton 1203: if (nbitems == 1 && elType.ElTypeNum != Template_EL_useSimple)
1204: {
1205: TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1206: if (registerUndo)
1207: TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1208: }
1.104 vatton 1209: }
1210: else
1.27 kia 1211: {
1.104 vatton 1212: // insert almost a pseudo element
1213: elType.ElTypeNum = Template_EL_TemplateObject;
1.109 vatton 1214: child = TtaNewElement (doc, elType);
1215: TtaInsertFirstChild (&child, el, doc);
1.27 kia 1216: }
1.104 vatton 1217:
1218: for (i = 0; i < nbitems; i++)
1219: TtaFreeMemory(items[i].label);
1220: TtaFreeMemory(items);
1221:
1222: if (parentLine)
1223: // display the element in line
1224: Template_SetInline (el, elType.ElSSchema, doc, registerUndo);
1225: TtaSetStructureChecking (oldStructureChecking, doc);
1.1 vatton 1226: }
1.32 vatton 1227: TtaFreeMemory (types);
1.44 kia 1228: Template_FixAccessRight (t, el, doc);
1229: TtaUpdateAccessRightInViews (doc, el);
1.109 vatton 1230: return child;
1.23 kia 1231: #else /* TEMPLATES */
1232: return NULL;
1.1 vatton 1233: #endif /* TEMPLATES */
1234: }
1235:
1236: /*----------------------------------------------------------------------
1.22 kia 1237: InstantiateRepeat
1238: Check for min and max param and validate xt:repeat element content.
1239: @param registerUndo True to register undo creation sequences.
1.104 vatton 1240: The parentLine parameter points to the enclosing line if any.
1.3 vatton 1241: ----------------------------------------------------------------------*/
1.46 vatton 1242: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
1.104 vatton 1243: Element parentLine, ThotBool registerUndo)
1.1 vatton 1244: {
1245: #ifdef TEMPLATES
1.32 vatton 1246: Element child, newChild;
1.52 vatton 1247: ElementType newElType;
1248: Attribute minAtt, maxAtt;
1249: AttributeType minType, maxType;
1250: char *text, *types = NULL, *title = NULL;
1251: int curVal, minVal, maxVal;
1.32 vatton 1252: int childrenCount;
1253:
1.25 vatton 1254: if (!t)
1.23 kia 1255: return;
1256:
1.1 vatton 1257: //Preparing types
1.52 vatton 1258: minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1 vatton 1259: minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52 vatton 1260: maxType.AttrSSchema = minType.AttrSSchema;
1.1 vatton 1261: maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52 vatton 1262: newElType.ElSSchema = minType.AttrSSchema;
1263: //Get minOccurs and maxOccurs attributes
1.1 vatton 1264: minAtt = TtaGetAttribute (el, minType);
1265: maxAtt = TtaGetAttribute (el, maxType);
1266: //Get the values
1267: if (minAtt)
1268: {
1.10 kia 1269: text = GetAttributeStringValue(el, minAtt, NULL);
1.1 vatton 1270: if (text)
1271: {
1272: minVal = atoi(text);
1273: TtaFreeMemory(text);
1.52 vatton 1274: curVal = minVal;
1.1 vatton 1275: }
1276: else
1277: //Error : Attribute with no value
1278: return;
1279: }
1280: else
1.52 vatton 1281: {
1282: minVal = 0;
1283: curVal = 1;
1284: }
1.1 vatton 1285:
1286: if (maxAtt)
1287: {
1.10 kia 1288: text = GetAttributeStringValue (el, maxAtt, NULL);
1.1 vatton 1289: if (text)
1290: {
1291: if (!strcmp (text, "*"))
1292: maxVal = INT_MAX;
1293: else
1294: maxVal = atoi (text);
1295: TtaFreeMemory (text);
1296: }
1297: else
1298: //Error : Attribute with no value
1299: return;
1300: }
1301: else
1302: maxVal = INT_MAX;
1303:
1304: text = (char*)TtaGetMemory(MAX_LENGTH);
1.52 vatton 1305: //Create non existing min max attributes
1306: if (minAtt == NULL)
1.1 vatton 1307: {
1.32 vatton 1308: minAtt = TtaNewAttribute (minType);
1309: sprintf (text, "%d", minVal);
1310: TtaAttachAttribute (el, minAtt, doc);
1311: TtaSetAttributeText (minAtt, text, el, doc);
1.25 vatton 1312: if (registerUndo)
1.32 vatton 1313: TtaRegisterAttributeCreate (minAtt, el, doc);
1.1 vatton 1314: }
1315:
1.52 vatton 1316: if (maxAtt == NULL)
1.1 vatton 1317: {
1.32 vatton 1318: maxAtt = TtaNewAttribute (maxType);
1319: if (maxVal < INT_MAX)
1320: sprintf(text, "%d", maxVal);
1.1 vatton 1321: else
1.32 vatton 1322: sprintf (text, "*");
1323: TtaAttachAttribute (el, maxAtt, doc);
1324: TtaSetAttributeText (maxAtt, text, el, doc);
1.25 vatton 1325: if (registerUndo)
1.32 vatton 1326: TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1 vatton 1327: }
1.52 vatton 1328: TtaFreeMemory(text);
1.1 vatton 1329:
1.52 vatton 1330: //We must have minOccurs children
1.104 vatton 1331: child = TtaGetFirstChild (el);
1.1 vatton 1332: if (!child)
1333: //Error : a repeat must have at least one child which will be the model
1334: return;
1335:
1.104 vatton 1336: for (childrenCount = 0; child; TtaNextSibling(&child))
1337: //TODO : Check that every child is valid
1338: childrenCount ++;
1.1 vatton 1339:
1340: if (childrenCount > maxVal)
1341: //Error : too many children!
1342: return;
1343:
1.104 vatton 1344:
1345: if (parentLine)
1346: // display the element in line
1347: Template_SetInline (el, minType.AttrSSchema, doc, registerUndo);
1348:
1.1 vatton 1349: child = TtaGetLastChild(el);
1.32 vatton 1350: types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
1351: title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52 vatton 1352: newElType.ElTypeNum = Template_EL_useEl;
1.32 vatton 1353: while (childrenCount < curVal)
1.1 vatton 1354: {
1.32 vatton 1355: newChild = TtaNewElement (doc, newElType);
1.27 kia 1356: // Insert it
1.32 vatton 1357: TtaInsertSibling (newChild, child, FALSE, doc);
1358: SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
1359: SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.104 vatton 1360: InstantiateUse (t, newChild, doc, parentLine, TRUE);
1.27 kia 1361:
1.25 vatton 1362: if (registerUndo)
1.34 vatton 1363: TtaRegisterElementCreate (newChild, doc);
1.1 vatton 1364: child = newChild;
1365: childrenCount++;
1366: }
1.27 kia 1367:
1.44 kia 1368: Template_FixAccessRight (t, el, doc);
1369: TtaUpdateAccessRightInViews (doc, el);
1.32 vatton 1370: TtaFreeMemory (types);
1371: TtaFreeMemory (title);
1.1 vatton 1372: #endif /* TEMPLATES */
1373: }
1374:
1375: /*----------------------------------------------------------------------
1.80 kia 1376: Template_InsertXTigerPI
1377: Insert the XTiger PI element in template instance.
1378: Param t is the XTigerTemplate structure of the template,
1379: not the template instance one.
1.3 vatton 1380: ----------------------------------------------------------------------*/
1.80 kia 1381: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1 vatton 1382: {
1383: #ifdef TEMPLATES
1.47 kia 1384: ElementType elType;
1.65 vatton 1385: Element root, piElem, doctype, line, text, elNew, elFound;
1.47 kia 1386: char *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1 vatton 1387: int pi_type;
1388:
1.80 kia 1389: if (!t || !doc)
1.23 kia 1390: return;
1.47 kia 1391:
1.80 kia 1392: root = TtaGetMainRoot (doc);
1.90 vatton 1393: if (root == NULL)
1394: return;
1.1 vatton 1395: //Look for PIs
1396: /* check if the document has a DOCTYPE declaration */
1397: #ifdef ANNOTATIONS
1398: if (DocumentTypes[doc] == docAnnot)
1399: elType = TtaGetElementType (root);
1400: else
1401: #endif /* ANNOTATIONS */
1402: elType = TtaGetElementType (root);
1403: s = TtaGetSSchemaName (elType.ElSSchema);
1404: if (strcmp (s, "HTML") == 0)
1405: {
1406: elType.ElTypeNum = HTML_EL_DOCTYPE;
1407: pi_type = HTML_EL_XMLPI;
1408: }
1409: #ifdef _SVG
1410: else if (strcmp (s, "SVG") == 0)
1411: {
1412: elType.ElTypeNum = SVG_EL_DOCTYPE;
1413: pi_type = SVG_EL_XMLPI;
1414: }
1415: #endif /* _SVG */
1416: else if (strcmp (s, "MathML") == 0)
1417: {
1418: elType.ElTypeNum = MathML_EL_DOCTYPE;
1419: pi_type = MathML_EL_XMLPI;
1420: }
1421: else
1422: {
1423: elType.ElTypeNum = XML_EL_doctype;
1424: pi_type = XML_EL_xmlpi;
1425: }
1.54 vatton 1426:
1.1 vatton 1427: doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65 vatton 1428: if (doctype == NULL)
1.1 vatton 1429: {
1.65 vatton 1430: elType.ElTypeNum = pi_type;
1431: piElem = TtaSearchTypedElement (elType, SearchInTree, root);
1432: if (piElem == NULL)
1433: {
1434: /* generate the XML declaration */
1435: /* Check the Thot abstract tree against the structure schema. */
1436: TtaSetStructureChecking (FALSE, doc);
1437: piElem = TtaNewTree (doc, elType, "");
1438: TtaInsertFirstChild (&piElem, root, doc);
1439: line = TtaGetFirstChild (piElem);
1440: text = TtaGetFirstChild (line);
1441: strcpy (buffer, "xml version=\"1.0\" encoding=\"");
1442: charsetname = UpdateDocumentCharset (doc);
1443: strcat (buffer, charsetname);
1444: strcat (buffer, "\"");
1445: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc);
1446: TtaSetStructureChecking (TRUE, doc);
1.90 vatton 1447: TtaFreeMemory (charsetname);
1448: TtaRegisterElementCreate (piElem, doc);
1.65 vatton 1449: }
1.1 vatton 1450: }
1451:
1452: /* generate the XTiger PI */
1453: /* Check the Thot abstract tree against the structure schema. */
1454: TtaSetStructureChecking (FALSE, doc);
1455: elType.ElTypeNum = pi_type;
1.65 vatton 1456: elNew = TtaNewTree (doc, elType, "");
1457: if (doctype)
1458: TtaInsertSibling (elNew, doctype, FALSE, doc);
1459: else
1460: TtaInsertSibling (elNew, piElem, FALSE, doc);
1461: line = TtaGetFirstChild (elNew);
1462: text = TtaGetFirstChild (line);
1.1 vatton 1463: strcpy (buffer, "xtiger template=\"");
1.100 vatton 1464: if (t->uri)
1465: strcat (buffer, t->uri);
1466: else if (t->base_uri)
1467: strcat (buffer, t->uri);
1.17 kia 1468: strcat (buffer, "\" version=\"");
1.20 vatton 1469: if (t->version)
1470: strcat (buffer, t->version);
1471: else
1472: strcat (buffer, "0.8");
1.1 vatton 1473: strcat (buffer, "\"");
1.25 vatton 1474: if (t->templateVersion)
1.20 vatton 1475: {
1476: strcat (buffer, " templateVersion=\"");
1477: strcat (buffer, t->templateVersion);
1478: strcat (buffer, "\"");
1479: }
1.1 vatton 1480: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc);
1.90 vatton 1481: TtaRegisterElementCreate (elNew, doc);
1.1 vatton 1482: TtaSetStructureChecking (TRUE, doc);
1.5 vatton 1483:
1484: // update the document title
1.47 kia 1485: if (!strcmp (s, "HTML"))
1.5 vatton 1486: {
1487: elType.ElTypeNum = HTML_EL_TITLE;
1488: elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1.90 vatton 1489: text = TtaGetFirstChild (elFound);
1490: while (text)
1.5 vatton 1491: {
1.90 vatton 1492: elType = TtaGetElementType (text);
1493: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1494: {
1495: TtaRegisterElementReplace (text, doc);
1496: TtaSetTextContent (text, (unsigned char*)Answer_text,
1497: TtaGetDefaultLanguage (), doc);
1498: text = NULL;
1499: }
1500: else if ((elType.ElTypeNum == Template_EL_useEl ||
1501: elType.ElTypeNum == Template_EL_useSimple) &&
1502: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
1503: // Ignore the template use element
1504: text = TtaGetFirstChild (text);
1505: else
1506: // Look for the first text child
1507: TtaNextSibling (&text);
1.5 vatton 1508: }
1509: }
1.1 vatton 1510: #endif /* TEMPLATES */
1511: }
1512:
1.80 kia 1513:
1.1 vatton 1514: /*----------------------------------------------------------------------
1.68 kia 1515: Template_PreInstantiateComponents
1516: Instantiates all components in order to improve editing.
1.3 vatton 1517: ----------------------------------------------------------------------*/
1.35 kia 1518: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1 vatton 1519: {
1520: #ifdef TEMPLATES
1.90 vatton 1521: ForwardIterator iter;
1522: Declaration dec;
1523: SearchSetNode node;
1524:
1.25 vatton 1525: if (!t)
1.23 kia 1526: return;
1527:
1.90 vatton 1528: if (Template_IsInstance (t))
1529: {
1.98 vatton 1530: #ifdef TEMPLATE_DEBUG
1.90 vatton 1531: DumpAllDeclarations();
1.98 vatton 1532: #endif /* TEMPLATE_DEBUG */
1.90 vatton 1533: iter = SearchSet_GetForwardIterator(GetComponents(t));
1.98 vatton 1534: #ifdef TEMPLATE_DEBUG
1.90 vatton 1535: printf("Template_PreInstantiateComponents %s\n", t->uri);
1.98 vatton 1536: #endif /* TEMPLATE_DEBUG */
1.90 vatton 1537: ITERATOR_FOREACH(iter, SearchSetNode, node)
1538: {
1539: dec = (Declaration) node->elem;
1.92 vatton 1540: ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE);
1.90 vatton 1541: }
1542: TtaFreeMemory(iter);
1.1 vatton 1543: }
1544: #endif /* TEMPLATES */
1545: }
1.84 kia 1546:
1547: /*----------------------------------------------------------------------
1548: Template_SetName
1549: Set the xt:component or xt:union element xt:name attribute.
1550: Make it unique.
1551: Return TRUE if the name is not modified.
1552: ----------------------------------------------------------------------*/
1.96 vatton 1553: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo)
1.84 kia 1554: {
1555: #ifdef TEMPLATES
1.95 vatton 1556: AttributeType attType;
1557: Attribute attr;
1.96 vatton 1558: ThotBool res, res2;
1.95 vatton 1559:
1560: if (doc && el && name)
1.84 kia 1561: {
1.95 vatton 1562: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
1563: attType.AttrTypeNum = Template_ATTR_name;
1564: attr = TtaGetAttribute(el, attType);
1565: if (attr == NULL)
1566: {
1567: attr = TtaNewAttribute (attType);
1568: TtaAttachAttribute (el, attr, doc);
1.96 vatton 1569: if (withUndo)
1570: TtaRegisterAttributeCreate (attr, el, doc);
1.95 vatton 1571: }
1.96 vatton 1572: if (withUndo)
1573: TtaRegisterAttributeReplace(attr, el, doc);
1.95 vatton 1574: TtaSetAttributeText (attr, name, el, doc);
1.96 vatton 1575: res = TtaIsValidID (attr, TRUE);
1576: res2 = !MakeUniqueName(el, doc, TRUE, FALSE);
1577: return (res || res2);
1578: }
1579: #endif /* TEMPLATES */
1580: return FALSE;
1581: }
1582:
1583: /*----------------------------------------------------------------------
1584: Template_SetName
1585: Set the xt:component or xt:union element xt:name attribute.
1586: Make it unique.
1587: Return TRUE if the name is not modified.
1588: ----------------------------------------------------------------------*/
1589: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo)
1590: {
1591: #ifdef TEMPLATES
1592: AttributeType attType;
1593: Attribute attr;
1594:
1595: if (doc && el && label)
1596: {
1597: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
1598: attType.AttrTypeNum = Template_ATTR_title;
1599: attr = TtaGetAttribute(el, attType);
1600: if (attr == NULL)
1601: {
1602: attr = TtaNewAttribute (attType);
1603: TtaAttachAttribute (el, attr, doc);
1604: if (withUndo)
1605: TtaRegisterAttributeCreate (attr, el, doc);
1606: }
1607: if (withUndo)
1608: TtaRegisterAttributeReplace(attr, el, doc);
1609: TtaSetAttributeText (attr, label, el, doc);
1.95 vatton 1610: return TtaIsValidID (attr, TRUE);
1.84 kia 1611: }
1612: #endif /* TEMPLATES */
1613: return FALSE;
1614: }
1615:
Webmaster