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