Annotation of Amaya/amaya/templateInstantiate.c, revision 1.109
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.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);
664: DocumentMeta[doc]->charset = ocharsetname;
665: // Now parse the instance
666: // The xtiger PI will be added and components will be removed
667: GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
668: !DontReplaceOldDoc, NULL, NULL);
669: if (DocumentMeta[newdoc])
670: DocumentMeta[newdoc]->method = CE_ABSOLUTE;
671: // Generate the HTML document title
672: root = TtaGetRootElement(newdoc);
673: elType = TtaGetElementType (root);
674: // get the target document type
675: s = TtaGetSSchemaName (elType.ElSSchema);
676: if (strcmp (s, "HTML") == 0)
1.1 vatton 677: {
1.90 vatton 678: // Initialize the document title
679: elType.ElTypeNum = HTML_EL_TITLE;
680: title = TtaSearchTypedElement (elType, SearchInTree, root);
681: text = TtaGetFirstChild (title);
682: while (text)
1.1 vatton 683: {
1.90 vatton 684: elType = TtaGetElementType (text);
685: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1.14 vatton 686: {
1.90 vatton 687: TtaSetTextContent (text, (unsigned char*)Answer_text,
688: TtaGetDefaultLanguage (), newdoc);
689: text = NULL;
690: SetNewTitle (newdoc);
1.14 vatton 691: }
1.90 vatton 692: else if ((elType.ElTypeNum == Template_EL_useEl ||
693: elType.ElTypeNum == Template_EL_useSimple) &&
694: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
695: // Ignore the template use element
696: text = TtaGetFirstChild (text);
697: else
698: // Look for the first text child
699: TtaNextSibling (&text);
1.63 kia 700: }
1.90 vatton 701: }
1.54 vatton 702:
1.90 vatton 703: // Insert XTiger PI
704: Template_InsertXTigerPI(newdoc, t);
705: // Parse template to fill structure and remove extra data
1.92 vatton 706: ParseTemplate (t, root, newdoc, NULL, FALSE);
1.90 vatton 707: TtaFreeMemory (localFile);
708: TtaClearUndoHistory (newdoc);
709: RemoveParsingErrors (newdoc);
710: TtaSetDocumentModified (newdoc);
711: UpdateTemplateMenus(newdoc);
1.1 vatton 712: #endif /* TEMPLATES */
713: }
714:
715:
716: #ifdef TEMPLATES
717: /*----------------------------------------------------------------------
718: ProcessAttr
1.9 vatton 719: Look for all "attribute" elements in the subtree and instantiate them
1.3 vatton 720: ----------------------------------------------------------------------*/
1.1 vatton 721: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
722: {
723: Element child;
724: ElementType elType;
725:
726: for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
727: {
728: elType = TtaGetElementType (child);
729: if (elType.ElTypeNum == Template_EL_attribute &&
730: !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
731: InstantiateAttribute (t, child, doc);
732: else
733: ProcessAttr (t, child, doc);
734: }
735: }
736: #endif /* TEMPLATES */
737:
1.12 kia 738:
739: /*----------------------------------------------------------------------
740: Template_GetNewSimpleTypeInstance
1.109 ! vatton 741: Create an new instance of xt:use/SimpleType for the document doc.
! 742: Return the new element
1.12 kia 743: ----------------------------------------------------------------------*/
1.109 ! vatton 744: Element Template_GetNewSimpleTypeInstance(Document doc)
1.12 kia 745: {
746: Element newEl = NULL;
747: #ifdef TEMPLATES
748: ElementType elType;
1.73 kia 749: const char *empty = " ";
1.24 vatton 750:
1.38 vatton 751: elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12 kia 752: elType.ElTypeNum = Template_EL_TEXT_UNIT;
753: newEl = TtaNewElement (doc, elType);
754: TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24 vatton 755: #endif /* TEMPLATES */
1.12 kia 756: return newEl;
757: }
758:
759: /*----------------------------------------------------------------------
760: Template_GetNewXmlElementInstance
1.109 ! vatton 761: Create an new instance of xt:use/XmlElement for the document doc.
! 762: The parameter decl gives the type of the element of new element.
! 763: Return the new element
1.12 kia 764: ----------------------------------------------------------------------*/
1.109 ! vatton 765: Element Template_GetNewXmlElementInstance(Document doc, Declaration decl)
1.12 kia 766: {
767: Element newEl = NULL;
768: #ifdef TEMPLATES
769: ElementType elType;
770:
1.24 vatton 771: GIType (decl->name, &elType, doc);
772: if (elType.ElTypeNum != 0)
1.23 kia 773: newEl = TtaNewTree (doc, elType, "");
1.12 kia 774: #endif /* TEMPLATES */
775: return newEl;
776: }
777:
1.34 vatton 778:
779: /*----------------------------------------------------------------------
780: InsertWithNotify applies pre and post functions when inserting the new
781: element el after child (if not NULL) or as first child of parent.
782: ----------------------------------------------------------------------*/
783: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
784: {
785: ElementType elType;
786: NotifyElement event;
787: char *name;
788: ThotBool isRow = FALSE, isCell = FALSE;
1.50 vatton 789: ThotBool isImage = FALSE;
790: ThotBool oldStructureChecking;
791:
792: // avoid to check attributes now
793: oldStructureChecking = TtaGetStructureChecking (doc);
794: TtaSetStructureChecking (FALSE, doc);
1.34 vatton 795:
796: elType = TtaGetElementType (el);
797: name = TtaGetSSchemaName (elType.ElSSchema);
798: isCell = ((!strcmp (name,"HTML") &&
799: elType.ElTypeNum == HTML_EL_Data_cell ||
800: elType.ElTypeNum == HTML_EL_Heading_cell) ||
801: (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
802: isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
803: (!strcmp (name,"MathML") &&
804: (elType.ElTypeNum == MathML_EL_MTR ||
805: elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50 vatton 806: isImage = (!strcmp (name,"HTML") &&
807: (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34 vatton 808: if (child)
809: TtaInsertSibling (el, child, FALSE, doc);
810: else
811: TtaInsertFirstChild (&el, parent, doc);
1.50 vatton 812: TtaSetStructureChecking (oldStructureChecking, doc);
1.34 vatton 813:
1.50 vatton 814: if (isImage)
815: InsertImageOrObject (el, doc);
816: else if (isCell)
1.34 vatton 817: {
818: // a cell is created
1.39 quint 819: NewCell (el, doc, TRUE, TRUE, TRUE);
1.34 vatton 820: }
821: else if (isRow)
822: {
823: // a row is created
824: event.element = el;
825: event.document = doc;
826: RowPasted (&event);
827: }
1.50 vatton 828:
829: if (!strcmp (name,"HTML"))
830: {
1.104 vatton 831: // special management for images and objets
1.50 vatton 832: elType.ElTypeNum = HTML_EL_IMG;
833: child = TtaSearchTypedElement (elType, SearchInTree, el);
834: while (child)
835: {
836: InsertImageOrObject (child, doc);
837: child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
838: }
839: elType.ElTypeNum = HTML_EL_Object;
840: child = TtaSearchTypedElement (elType, SearchInTree, el);
841: while (child)
842: {
843: InsertImageOrObject (child, doc);
844: child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
845: }
846: }
1.34 vatton 847: return el;
848: }
849:
850:
1.12 kia 851: /*----------------------------------------------------------------------
1.16 kia 852: Template_InsertUseChildren
853: Insert children to a xt:use
854: The dec parameter must be valid and will not be verified. It must be a
855: direct child element (for union elements).
856: @param el element (xt:use) in which insert a new element
857: @param dec Template declaration of the element to insert
858: @return The inserted element (the xt:use element if insertion is multiple as component)
1.104 vatton 859: The parentLine parameter points to the enclosing line if any.
1.16 kia 860: ----------------------------------------------------------------------*/
1.104 vatton 861: Element Template_InsertUseChildren (Document doc, Element el, Declaration dec,
862: Element parentLine, ThotBool registerUndo)
1.16 kia 863: {
1.99 vatton 864: Element newEl = NULL;
1.16 kia 865: #ifdef TEMPLATES
1.99 vatton 866: Element current = NULL;
1.104 vatton 867: Element child = NULL, prev, next;
868: ElementType childType, elType;
869: SSchema sshtml;
1.99 vatton 870: XTigerTemplate t;
1.17 kia 871:
1.25 vatton 872: if (TtaGetDocumentAccessMode(doc))
1.16 kia 873: {
1.23 kia 874: switch (dec->nature)
875: {
876: case SimpleTypeNat:
1.109 ! vatton 877: newEl = Template_GetNewSimpleTypeInstance(doc);
1.34 vatton 878: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23 kia 879: break;
880: case XmlElementNat:
1.109 ! vatton 881: newEl = Template_GetNewXmlElementInstance(doc, dec);
1.34 vatton 882: newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23 kia 883: break;
884: case ComponentNat:
885: newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74 kia 886: ProcessAttr (dec->usedIn, newEl, doc);
1.104 vatton 887: elType = TtaGetElementType (el);
1.23 kia 888: /* Copy elements from new use to existing use. */
1.98 vatton 889: #ifdef TEMPLATE_DEBUG
1.74 kia 890: DumpSubtree(newEl, doc, 0);
1.98 vatton 891: #endif /* TEMPLATE_DEBUG */
1.104 vatton 892: sshtml = TtaGetSSchema ("HTML", doc);
1.102 vatton 893: t = GetXTigerDocTemplate( doc);
894: child = TtaGetFirstChild (newEl);
1.76 vatton 895: while (child)
896: {
897: // move the new subtree to the document
898: TtaRemoveTree (child, doc);
1.104 vatton 899: childType = TtaGetElementType (child);
900: if (parentLine)
901: {
902: if (childType.ElSSchema == sshtml &&
903: childType.ElTypeNum == HTML_EL_Pseudo_paragraph)
904: {
905: prev = TtaGetFirstChild (child);
906: while (prev)
907: {
908: next = prev;
909: TtaNextSibling (&next);
910: TtaRemoveTree (prev, doc);
911: current = InsertWithNotify (prev, current, el, doc);
912: prev = next;
913: }
1.108 vatton 914: TtaDeleteTree (child, doc);
1.104 vatton 915: }
1.108 vatton 916: else
917: current = InsertWithNotify (child, current, el, doc);
1.104 vatton 918: }
919: else
920: {
921: current = InsertWithNotify (child, current, el, doc);
922: // check if a new paragraph is inserted
923: if (childType.ElSSchema == sshtml &&
924: childType.ElTypeNum == HTML_EL_Paragraph)
925: Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
926: else
927: {
928: childType.ElSSchema = sshtml;
929: childType.ElTypeNum = HTML_EL_Paragraph;
930: child = TtaSearchTypedElement (childType, SearchInTree, current);
931: while (child)
932: {
933: Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
934: child = TtaSearchTypedElement (childType, SearchInTree, child);
935: }
936: }
937: }
1.99 vatton 938: child = TtaGetFirstChild (newEl);
1.76 vatton 939: }
1.99 vatton 940:
1.102 vatton 941: TtaDeleteTree (newEl, doc);
1.23 kia 942: newEl = el;
943: break;
944: default :
945: //Impossible
946: break;
947: }
1.44 kia 948: Template_FixAccessRight (dec->usedIn, el, doc);
1.105 vatton 949: if (dec->nature == ComponentNat)
950: Component_FixAccessRight (el, doc);
1.102 vatton 951: TtaUpdateAccessRightInViews (doc, el);
1.23 kia 952: }
1.16 kia 953: #endif /* TEMPLATES */
954: return newEl;
955: }
956:
1.40 kia 957:
958: /*----------------------------------------------------------------------
1.105 vatton 959: Component_FixAccessRight locks children of the component
960: ----------------------------------------------------------------------*/
961: void Component_FixAccessRight (Element el, Document doc)
962: {
963: #ifdef TEMPLATES
964: Element child;
965:
966: if (el && doc)
967: {
968: TtaSetAccessRight (el, ReadOnly, doc);
969: // fix access right to children
970: child = TtaGetFirstChild (el);
971: while (child)
972: {
973: TtaSetAccessRight (child, ReadOnly, doc);
974: TtaNextSibling (&child);
975: }
976: }
977: #endif /* TEMPLATES */
978: }
979:
980: /*----------------------------------------------------------------------
1.97 vatton 981: Template_FixAccessRight fixes access rights of the el element
1.40 kia 982: ----------------------------------------------------------------------*/
1.41 vatton 983: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40 kia 984: {
985: #ifdef TEMPLATES
986: ElementType elType;
987: Element child;
1.97 vatton 988: Declaration decl;
1.88 vatton 989: char currentType[MAX_LENGTH], *ptr;
1.40 kia 990:
991: if (t && el && doc)
992: {
993: elType = TtaGetElementType(el);
1.41 vatton 994: if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40 kia 995: {
1.41 vatton 996: switch (elType.ElTypeNum)
1.40 kia 997: {
998: case Template_EL_TEXT_UNIT:
1.41 vatton 999: //TtaSetAccessRight( el, ReadWrite, doc);
1000: return;
1.105 vatton 1001: case Template_EL_component:
1002: Component_FixAccessRight (el, doc);
1003: break;
1.40 kia 1004: case Template_EL_useEl:
1005: case Template_EL_useSimple:
1006: GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
1007: (char*)currentType, NULL);
1.88 vatton 1008: if (currentType[0] == EOS)
1009: {
1010: GiveAttributeStringValueFromNum(el, Template_ATTR_types,
1011: (char*)currentType, NULL);
1012: ptr = strstr (currentType, " ");
1013: if (ptr)
1014: *ptr = EOS;
1015: }
1.40 kia 1016: decl = Template_GetDeclaration(t, currentType);
1017: if (decl)
1018: {
1019: switch (decl->nature)
1020: {
1021: case SimpleTypeNat:
1.41 vatton 1022: TtaSetAccessRight (el, ReadWrite, doc);
1023: return;
1.105 vatton 1024: case ComponentNat:
1.107 vatton 1025: TtaSetAccessRight (el, ReadOnly, doc);
1026: break;
1027: //Component_FixAccessRight (el, doc);
1028: //return;
1.97 vatton 1029: case XmlElementNat:
1.105 vatton 1030: if (TtaIsSetReadOnly (el))
1031: break;
1.97 vatton 1032: child = TtaGetFirstChild (el);
1033: if (child)
1034: TtaSetAccessRight (child, ReadWrite, doc);
1.40 kia 1035: default:
1.41 vatton 1036: TtaSetAccessRight (el, ReadOnly, doc);
1037: break;
1.40 kia 1038: }
1039: }
1040: break;
1041: case Template_EL_bag:
1.45 vatton 1042: case Template_EL_repeat:
1.40 kia 1043: TtaSetAccessRight(el, ReadWrite, doc);
1044: break;
1045: default:
1046: TtaSetAccessRight(el, ReadOnly, doc);
1047: break;
1048: }
1049: }
1050:
1.92 vatton 1051: // fix access right to children
1.41 vatton 1052: child = TtaGetFirstChild (el);
1053: while (child)
1.40 kia 1054: {
1.41 vatton 1055: Template_FixAccessRight (t, child, doc);
1056: TtaNextSibling (&child);
1.40 kia 1057: }
1058: }
1059: #endif /* TEMPLATES */
1060: }
1061:
1.16 kia 1062: /*----------------------------------------------------------------------
1.46 vatton 1063: AddPromptIndicator
1064: ----------------------------------------------------------------------*/
1065: void AddPromptIndicator (Element el, Document doc)
1066: {
1067: #ifdef TEMPLATES
1068: ElementType elType;
1069: AttributeType attrType;
1070: Attribute att;
1071:
1.66 vatton 1072: if (el)
1073: {
1074: elType = TtaGetElementType (el);
1075: attrType.AttrSSchema = elType.ElSSchema;
1076: attrType.AttrTypeNum = Template_ATTR_prompt;
1.69 vatton 1077: att = TtaGetAttribute (el, attrType);
1078: if (att == NULL)
1079: {
1080: att = TtaNewAttribute (attrType);
1081: TtaAttachAttribute (el, att, doc);
1082: }
1.66 vatton 1083: }
1.46 vatton 1084: #endif /* TEMPLATES */
1085: }
1086:
1087: /*----------------------------------------------------------------------
1.104 vatton 1088: Template_SetInline manages inline elements
1089: registerUndo says if changes must be registered
1090: ----------------------------------------------------------------------*/
1091: void Template_SetInline (Element el, SSchema sstempl, Document doc, ThotBool registerUndo)
1092: {
1093: #ifdef TEMPLATES
1094: Element child = NULL;
1095: ElementType elType1, elType2, elType3;
1096:
1097: if (el)
1098: {
1099: elType1 = TtaGetElementType (el);
1100: if (elType1.ElSSchema == sstempl)
1101: // apply to hte current template element
1102: SetAttributeIntValue (el, Template_ATTR_SetInLine,
1103: Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
1104: else
1105: elType1.ElSSchema = sstempl;
1106: elType1.ElTypeNum = Template_EL_useSimple;
1107: elType2.ElTypeNum = Template_EL_useEl;
1108: elType2.ElSSchema = elType1.ElSSchema;
1109: elType3.ElTypeNum = Template_EL_repeat;
1110: elType3.ElSSchema = elType1.ElSSchema;
1111: child = TtaSearchElementAmong5Types (elType1, elType2, elType3, elType3, elType3,
1112: SearchForward, el);
1113: while (child && TtaIsAncestor (child, el))
1114: {
1115: SetAttributeIntValue (child, Template_ATTR_SetInLine,
1116: Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
1117: child = TtaSearchElementAmong5Types (elType1, elType2,
1118: elType3, elType3, elType3,
1119: SearchForward, child);
1120: }
1121: }
1122: #endif /* TEMPLATES */
1123: }
1124:
1125: /*----------------------------------------------------------------------
1126: InstantiateUse intantiate the use element el.
1127: The parentLine parameter points to the enclosing line if any.
1.3 vatton 1128: ----------------------------------------------------------------------*/
1.1 vatton 1129: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.104 vatton 1130: Element parentLine, ThotBool registerUndo)
1.1 vatton 1131: {
1132: #ifdef TEMPLATES
1.109 ! vatton 1133: Element child = NULL;
1.1 vatton 1134: ElementType elType;
1135: Declaration dec;
1.109 ! vatton 1136: int size, nbitems, i, option;
1.1 vatton 1137: struct menuType *items;
1.104 vatton 1138: char *types;
1.109 ! vatton 1139: ThotBool oldStructureChecking, hidden;
1.1 vatton 1140:
1.25 vatton 1141: if (!t)
1.23 kia 1142: return NULL;
1143:
1.1 vatton 1144: /* get the value of the "types" attribute */
1.12 kia 1145: elType = TtaGetElementType (el);
1.32 vatton 1146: types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36 vatton 1147: if (!types || types[0] == EOS)
1148: {
1149: TtaFreeMemory (types);
1150: return NULL;
1151: }
1.104 vatton 1152: if (!strcmp (types, "string") || !strcmp (types, "number"))
1.109 ! vatton 1153: {
! 1154: child = TtaGetFirstChild (el);
! 1155: if (child == NULL)
! 1156: {
! 1157: child = Template_GetNewSimpleTypeInstance(doc);
! 1158: child = InsertWithNotify (child, NULL, el, doc);
! 1159: }
! 1160: AddPromptIndicator (el, doc);
! 1161: }
1.104 vatton 1162: else
1.1 vatton 1163: {
1.104 vatton 1164: giveItems (types, size, &items, &nbitems);
1165: // No structure checking
1166: oldStructureChecking = TtaGetStructureChecking (doc);
1167: TtaSetStructureChecking (FALSE, doc);
1.109 ! vatton 1168: hidden = ElementIsOptional (el);
! 1169: if (hidden)
! 1170: {
! 1171: // check the current option value
! 1172: option = GetAttributeIntValueFromNum (el, Template_ATTR_option);
! 1173: hidden = (option == Template_ATTR_option_VAL_option_unset);
! 1174: }
! 1175: if (nbitems == 1 || !hidden)
1.104 vatton 1176: /* only one type in the "types" attribute */
1177: {
1178: dec = Template_GetDeclaration (t, items[0].label);
1179: if (dec)
1180: {
1.109 ! vatton 1181: child = Template_InsertUseChildren (doc, el, dec, parentLine, registerUndo);
! 1182: if (child && elType.ElTypeNum != Template_EL_useSimple)
1.104 vatton 1183: {
1.109 ! vatton 1184: TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
! 1185: if (registerUndo)
! 1186: TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.104 vatton 1187: }
1188: }
1189: }
1190: else
1.27 kia 1191: {
1.104 vatton 1192: // insert almost a pseudo element
1193: elType.ElTypeNum = Template_EL_TemplateObject;
1.109 ! vatton 1194: child = TtaNewElement (doc, elType);
! 1195: TtaInsertFirstChild (&child, el, doc);
1.27 kia 1196: }
1.104 vatton 1197:
1198: for (i = 0; i < nbitems; i++)
1199: TtaFreeMemory(items[i].label);
1200: TtaFreeMemory(items);
1201:
1202: if (parentLine)
1203: // display the element in line
1204: Template_SetInline (el, elType.ElSSchema, doc, registerUndo);
1205: TtaSetStructureChecking (oldStructureChecking, doc);
1.1 vatton 1206: }
1.32 vatton 1207: TtaFreeMemory (types);
1.44 kia 1208: Template_FixAccessRight (t, el, doc);
1209: TtaUpdateAccessRightInViews (doc, el);
1.109 ! vatton 1210: return child;
1.23 kia 1211: #else /* TEMPLATES */
1212: return NULL;
1.1 vatton 1213: #endif /* TEMPLATES */
1214: }
1215:
1216: /*----------------------------------------------------------------------
1.22 kia 1217: InstantiateRepeat
1218: Check for min and max param and validate xt:repeat element content.
1219: @param registerUndo True to register undo creation sequences.
1.104 vatton 1220: The parentLine parameter points to the enclosing line if any.
1.3 vatton 1221: ----------------------------------------------------------------------*/
1.46 vatton 1222: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
1.104 vatton 1223: Element parentLine, ThotBool registerUndo)
1.1 vatton 1224: {
1225: #ifdef TEMPLATES
1.32 vatton 1226: Element child, newChild;
1.52 vatton 1227: ElementType newElType;
1228: Attribute minAtt, maxAtt;
1229: AttributeType minType, maxType;
1230: char *text, *types = NULL, *title = NULL;
1231: int curVal, minVal, maxVal;
1.32 vatton 1232: int childrenCount;
1233:
1.25 vatton 1234: if (!t)
1.23 kia 1235: return;
1236:
1.1 vatton 1237: //Preparing types
1.52 vatton 1238: minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1 vatton 1239: minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52 vatton 1240: maxType.AttrSSchema = minType.AttrSSchema;
1.1 vatton 1241: maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52 vatton 1242: newElType.ElSSchema = minType.AttrSSchema;
1243: //Get minOccurs and maxOccurs attributes
1.1 vatton 1244: minAtt = TtaGetAttribute (el, minType);
1245: maxAtt = TtaGetAttribute (el, maxType);
1246: //Get the values
1247: if (minAtt)
1248: {
1.10 kia 1249: text = GetAttributeStringValue(el, minAtt, NULL);
1.1 vatton 1250: if (text)
1251: {
1252: minVal = atoi(text);
1253: TtaFreeMemory(text);
1.52 vatton 1254: curVal = minVal;
1.1 vatton 1255: }
1256: else
1257: //Error : Attribute with no value
1258: return;
1259: }
1260: else
1.52 vatton 1261: {
1262: minVal = 0;
1263: curVal = 1;
1264: }
1.1 vatton 1265:
1266: if (maxAtt)
1267: {
1.10 kia 1268: text = GetAttributeStringValue (el, maxAtt, NULL);
1.1 vatton 1269: if (text)
1270: {
1271: if (!strcmp (text, "*"))
1272: maxVal = INT_MAX;
1273: else
1274: maxVal = atoi (text);
1275: TtaFreeMemory (text);
1276: }
1277: else
1278: //Error : Attribute with no value
1279: return;
1280: }
1281: else
1282: maxVal = INT_MAX;
1283:
1284: text = (char*)TtaGetMemory(MAX_LENGTH);
1.52 vatton 1285: //Create non existing min max attributes
1286: if (minAtt == NULL)
1.1 vatton 1287: {
1.32 vatton 1288: minAtt = TtaNewAttribute (minType);
1289: sprintf (text, "%d", minVal);
1290: TtaAttachAttribute (el, minAtt, doc);
1291: TtaSetAttributeText (minAtt, text, el, doc);
1.25 vatton 1292: if (registerUndo)
1.32 vatton 1293: TtaRegisterAttributeCreate (minAtt, el, doc);
1.1 vatton 1294: }
1295:
1.52 vatton 1296: if (maxAtt == NULL)
1.1 vatton 1297: {
1.32 vatton 1298: maxAtt = TtaNewAttribute (maxType);
1299: if (maxVal < INT_MAX)
1300: sprintf(text, "%d", maxVal);
1.1 vatton 1301: else
1.32 vatton 1302: sprintf (text, "*");
1303: TtaAttachAttribute (el, maxAtt, doc);
1304: TtaSetAttributeText (maxAtt, text, el, doc);
1.25 vatton 1305: if (registerUndo)
1.32 vatton 1306: TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1 vatton 1307: }
1.52 vatton 1308: TtaFreeMemory(text);
1.1 vatton 1309:
1.52 vatton 1310: //We must have minOccurs children
1.104 vatton 1311: child = TtaGetFirstChild (el);
1.1 vatton 1312: if (!child)
1313: //Error : a repeat must have at least one child which will be the model
1314: return;
1315:
1.104 vatton 1316: for (childrenCount = 0; child; TtaNextSibling(&child))
1317: //TODO : Check that every child is valid
1318: childrenCount ++;
1.1 vatton 1319:
1320: if (childrenCount > maxVal)
1321: //Error : too many children!
1322: return;
1323:
1.104 vatton 1324:
1325: if (parentLine)
1326: // display the element in line
1327: Template_SetInline (el, minType.AttrSSchema, doc, registerUndo);
1328:
1.1 vatton 1329: child = TtaGetLastChild(el);
1.32 vatton 1330: types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
1331: title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52 vatton 1332: newElType.ElTypeNum = Template_EL_useEl;
1.32 vatton 1333: while (childrenCount < curVal)
1.1 vatton 1334: {
1.32 vatton 1335: newChild = TtaNewElement (doc, newElType);
1.27 kia 1336: // Insert it
1.32 vatton 1337: TtaInsertSibling (newChild, child, FALSE, doc);
1338: SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
1339: SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.104 vatton 1340: InstantiateUse (t, newChild, doc, parentLine, TRUE);
1.27 kia 1341:
1.25 vatton 1342: if (registerUndo)
1.34 vatton 1343: TtaRegisterElementCreate (newChild, doc);
1.1 vatton 1344: child = newChild;
1345: childrenCount++;
1346: }
1.27 kia 1347:
1.44 kia 1348: Template_FixAccessRight (t, el, doc);
1349: TtaUpdateAccessRightInViews (doc, el);
1.32 vatton 1350: TtaFreeMemory (types);
1351: TtaFreeMemory (title);
1.1 vatton 1352: #endif /* TEMPLATES */
1353: }
1354:
1355: /*----------------------------------------------------------------------
1.80 kia 1356: Template_InsertXTigerPI
1357: Insert the XTiger PI element in template instance.
1358: Param t is the XTigerTemplate structure of the template,
1359: not the template instance one.
1.3 vatton 1360: ----------------------------------------------------------------------*/
1.80 kia 1361: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1 vatton 1362: {
1363: #ifdef TEMPLATES
1.47 kia 1364: ElementType elType;
1.65 vatton 1365: Element root, piElem, doctype, line, text, elNew, elFound;
1.47 kia 1366: char *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1 vatton 1367: int pi_type;
1368:
1.80 kia 1369: if (!t || !doc)
1.23 kia 1370: return;
1.47 kia 1371:
1.80 kia 1372: root = TtaGetMainRoot (doc);
1.90 vatton 1373: if (root == NULL)
1374: return;
1.1 vatton 1375: //Look for PIs
1376: /* check if the document has a DOCTYPE declaration */
1377: #ifdef ANNOTATIONS
1378: if (DocumentTypes[doc] == docAnnot)
1379: elType = TtaGetElementType (root);
1380: else
1381: #endif /* ANNOTATIONS */
1382: elType = TtaGetElementType (root);
1383: s = TtaGetSSchemaName (elType.ElSSchema);
1384: if (strcmp (s, "HTML") == 0)
1385: {
1386: elType.ElTypeNum = HTML_EL_DOCTYPE;
1387: pi_type = HTML_EL_XMLPI;
1388: }
1389: #ifdef _SVG
1390: else if (strcmp (s, "SVG") == 0)
1391: {
1392: elType.ElTypeNum = SVG_EL_DOCTYPE;
1393: pi_type = SVG_EL_XMLPI;
1394: }
1395: #endif /* _SVG */
1396: else if (strcmp (s, "MathML") == 0)
1397: {
1398: elType.ElTypeNum = MathML_EL_DOCTYPE;
1399: pi_type = MathML_EL_XMLPI;
1400: }
1401: else
1402: {
1403: elType.ElTypeNum = XML_EL_doctype;
1404: pi_type = XML_EL_xmlpi;
1405: }
1.54 vatton 1406:
1.1 vatton 1407: doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65 vatton 1408: if (doctype == NULL)
1.1 vatton 1409: {
1.65 vatton 1410: elType.ElTypeNum = pi_type;
1411: piElem = TtaSearchTypedElement (elType, SearchInTree, root);
1412: if (piElem == NULL)
1413: {
1414: /* generate the XML declaration */
1415: /* Check the Thot abstract tree against the structure schema. */
1416: TtaSetStructureChecking (FALSE, doc);
1417: piElem = TtaNewTree (doc, elType, "");
1418: TtaInsertFirstChild (&piElem, root, doc);
1419: line = TtaGetFirstChild (piElem);
1420: text = TtaGetFirstChild (line);
1421: strcpy (buffer, "xml version=\"1.0\" encoding=\"");
1422: charsetname = UpdateDocumentCharset (doc);
1423: strcat (buffer, charsetname);
1424: strcat (buffer, "\"");
1425: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc);
1426: TtaSetStructureChecking (TRUE, doc);
1.90 vatton 1427: TtaFreeMemory (charsetname);
1428: TtaRegisterElementCreate (piElem, doc);
1.65 vatton 1429: }
1.1 vatton 1430: }
1431:
1432: /* generate the XTiger PI */
1433: /* Check the Thot abstract tree against the structure schema. */
1434: TtaSetStructureChecking (FALSE, doc);
1435: elType.ElTypeNum = pi_type;
1.65 vatton 1436: elNew = TtaNewTree (doc, elType, "");
1437: if (doctype)
1438: TtaInsertSibling (elNew, doctype, FALSE, doc);
1439: else
1440: TtaInsertSibling (elNew, piElem, FALSE, doc);
1441: line = TtaGetFirstChild (elNew);
1442: text = TtaGetFirstChild (line);
1.1 vatton 1443: strcpy (buffer, "xtiger template=\"");
1.100 vatton 1444: if (t->uri)
1445: strcat (buffer, t->uri);
1446: else if (t->base_uri)
1447: strcat (buffer, t->uri);
1.17 kia 1448: strcat (buffer, "\" version=\"");
1.20 vatton 1449: if (t->version)
1450: strcat (buffer, t->version);
1451: else
1452: strcat (buffer, "0.8");
1.1 vatton 1453: strcat (buffer, "\"");
1.25 vatton 1454: if (t->templateVersion)
1.20 vatton 1455: {
1456: strcat (buffer, " templateVersion=\"");
1457: strcat (buffer, t->templateVersion);
1458: strcat (buffer, "\"");
1459: }
1.1 vatton 1460: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc);
1.90 vatton 1461: TtaRegisterElementCreate (elNew, doc);
1.1 vatton 1462: TtaSetStructureChecking (TRUE, doc);
1.5 vatton 1463:
1464: // update the document title
1.47 kia 1465: if (!strcmp (s, "HTML"))
1.5 vatton 1466: {
1467: elType.ElTypeNum = HTML_EL_TITLE;
1468: elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1.90 vatton 1469: text = TtaGetFirstChild (elFound);
1470: while (text)
1.5 vatton 1471: {
1.90 vatton 1472: elType = TtaGetElementType (text);
1473: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1474: {
1475: TtaRegisterElementReplace (text, doc);
1476: TtaSetTextContent (text, (unsigned char*)Answer_text,
1477: TtaGetDefaultLanguage (), doc);
1478: text = NULL;
1479: }
1480: else if ((elType.ElTypeNum == Template_EL_useEl ||
1481: elType.ElTypeNum == Template_EL_useSimple) &&
1482: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
1483: // Ignore the template use element
1484: text = TtaGetFirstChild (text);
1485: else
1486: // Look for the first text child
1487: TtaNextSibling (&text);
1.5 vatton 1488: }
1489: }
1.1 vatton 1490: #endif /* TEMPLATES */
1491: }
1492:
1.80 kia 1493:
1.1 vatton 1494: /*----------------------------------------------------------------------
1.68 kia 1495: Template_PreInstantiateComponents
1496: Instantiates all components in order to improve editing.
1.3 vatton 1497: ----------------------------------------------------------------------*/
1.35 kia 1498: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1 vatton 1499: {
1500: #ifdef TEMPLATES
1.90 vatton 1501: ForwardIterator iter;
1502: Declaration dec;
1503: SearchSetNode node;
1504:
1.25 vatton 1505: if (!t)
1.23 kia 1506: return;
1507:
1.90 vatton 1508: if (Template_IsInstance (t))
1509: {
1.98 vatton 1510: #ifdef TEMPLATE_DEBUG
1.90 vatton 1511: DumpAllDeclarations();
1.98 vatton 1512: #endif /* TEMPLATE_DEBUG */
1.90 vatton 1513: iter = SearchSet_GetForwardIterator(GetComponents(t));
1.98 vatton 1514: #ifdef TEMPLATE_DEBUG
1.90 vatton 1515: printf("Template_PreInstantiateComponents %s\n", t->uri);
1.98 vatton 1516: #endif /* TEMPLATE_DEBUG */
1.90 vatton 1517: ITERATOR_FOREACH(iter, SearchSetNode, node)
1518: {
1519: dec = (Declaration) node->elem;
1.92 vatton 1520: ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE);
1.90 vatton 1521: }
1522: TtaFreeMemory(iter);
1.1 vatton 1523: }
1524: #endif /* TEMPLATES */
1525: }
1.84 kia 1526:
1527: /*----------------------------------------------------------------------
1528: Template_SetName
1529: Set the xt:component or xt:union element xt:name attribute.
1530: Make it unique.
1531: Return TRUE if the name is not modified.
1532: ----------------------------------------------------------------------*/
1.96 vatton 1533: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo)
1.84 kia 1534: {
1535: #ifdef TEMPLATES
1.95 vatton 1536: AttributeType attType;
1537: Attribute attr;
1.96 vatton 1538: ThotBool res, res2;
1.95 vatton 1539:
1540: if (doc && el && name)
1.84 kia 1541: {
1.95 vatton 1542: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
1543: attType.AttrTypeNum = Template_ATTR_name;
1544: attr = TtaGetAttribute(el, attType);
1545: if (attr == NULL)
1546: {
1547: attr = TtaNewAttribute (attType);
1548: TtaAttachAttribute (el, attr, doc);
1.96 vatton 1549: if (withUndo)
1550: TtaRegisterAttributeCreate (attr, el, doc);
1.95 vatton 1551: }
1.96 vatton 1552: if (withUndo)
1553: TtaRegisterAttributeReplace(attr, el, doc);
1.95 vatton 1554: TtaSetAttributeText (attr, name, el, doc);
1.96 vatton 1555: res = TtaIsValidID (attr, TRUE);
1556: res2 = !MakeUniqueName(el, doc, TRUE, FALSE);
1557: return (res || res2);
1558: }
1559: #endif /* TEMPLATES */
1560: return FALSE;
1561: }
1562:
1563: /*----------------------------------------------------------------------
1564: Template_SetName
1565: Set the xt:component or xt:union element xt:name attribute.
1566: Make it unique.
1567: Return TRUE if the name is not modified.
1568: ----------------------------------------------------------------------*/
1569: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo)
1570: {
1571: #ifdef TEMPLATES
1572: AttributeType attType;
1573: Attribute attr;
1574:
1575: if (doc && el && label)
1576: {
1577: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
1578: attType.AttrTypeNum = Template_ATTR_title;
1579: attr = TtaGetAttribute(el, attType);
1580: if (attr == NULL)
1581: {
1582: attr = TtaNewAttribute (attType);
1583: TtaAttachAttribute (el, attr, doc);
1584: if (withUndo)
1585: TtaRegisterAttributeCreate (attr, el, doc);
1586: }
1587: if (withUndo)
1588: TtaRegisterAttributeReplace(attr, el, doc);
1589: TtaSetAttributeText (attr, label, el, doc);
1.95 vatton 1590: return TtaIsValidID (attr, TRUE);
1.84 kia 1591: }
1592: #endif /* TEMPLATES */
1593: return FALSE;
1594: }
1595:
Webmaster