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