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