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