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