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.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: loading is TRUE when the document is not already loaded 235: parentLine points to the enclosing pseudo paragraph or paragraph 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.93 vatton 244: Element next, child, savedInline, prev, parent; 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; 353: } 1.94 vatton 354: 355: // generate the currentType attribute 356: otherType = TtaGetElementType (child); 357: attType.AttrTypeNum = Template_ATTR_currentType; 358: att = TtaGetAttribute (el, attType); 359: if (att == NULL) 360: { 361: att = TtaNewAttribute (attType); 362: TtaAttachAttribute (el, att, doc); 363: } 364: if (otherType.ElTypeNum == 1) 365: TtaSetAttributeText (att, "string", el, doc); 366: else 367: { 368: name = (char *)GetXMLElementName (otherType, doc); 369: if (name && !strcmp (name,"???")) 370: TtaSetAttributeText (att, name, el, doc); 371: } 1.92 vatton 372: } 1.87 vatton 373: } 1.94 vatton 374: TtaFreeMemory (types); 375: if (child == NULL) 1.80 kia 376: InstantiateUse (t, el, doc, FALSE); 1.94 vatton 377: else 378: { 379: Template_FixAccessRight (t, el, doc); 380: TtaUpdateAccessRightInViews (doc, el); 381: } 1.80 kia 382: break; 383: case Template_EL_attribute : 384: if (!loading) 385: InstantiateAttribute (t, el, doc); 386: break; 387: case Template_EL_repeat : 388: InstantiateRepeat (t, el, doc, FALSE); 389: break; 390: default : 391: break; 392: } 393: } 1.92 vatton 394: else if (!strcmp (name, "HTML") && 395: (elType.ElTypeNum == HTML_EL_Pseudo_paragraph || 396: elType.ElTypeNum == HTML_EL_Paragraph)) 397: parentLine = el; 1.80 kia 398: 399: child = TtaGetFirstChild (el); 400: while (child) 401: { 1.92 vatton 402: next = child; 403: TtaNextSibling (&next); 404: parentLine = ParseTemplate (t, child, doc, parentLine, loading); 405: child = next; 1.80 kia 406: } 1.92 vatton 407: return savedInline; 1.80 kia 408: #endif /* TEMPLATES */ 409: } 410: 1.63 kia 411: 412: /*---------------------------------------------------------------------- 413: CreateTemplate 414: Create a template from any document. 415: ----------------------------------------------------------------------*/ 1.89 vatton 416: void CreateTemplate (Document doc, char *templatePath) 1.63 kia 417: { 418: #ifdef TEMPLATES 419: Element root, head, elem, xt, title, child, last; 420: ElementType elType, xtType; 1.64 kia 421: char *s; 1.63 kia 422: SSchema templSchema; 423: XTigerTemplate t; 424: 1.89 vatton 425: if (IsTemplateInstanceDocument(doc)) 1.63 kia 426: { 427: ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE), 428: TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION)); 429: return; 430: } 431: 1.89 vatton 432: if (IsTemplateDocument(doc)) 1.63 kia 433: { 434: ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE), 435: TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION)); 436: return; 437: } 438: 1.64 kia 439: root = TtaGetRootElement(doc); 440: elType = TtaGetElementType (root); 441: // get the target document type 442: s = TtaGetSSchemaName (elType.ElSSchema); 1.63 kia 443: 1.64 kia 444: TtaNewNature (doc, elType.ElSSchema, NULL, "Template", "TemplateP"); 1.96 ! vatton 445: TtaSetANamespaceDeclaration (doc, root, "xt", Template_URI); ! 446: templSchema = TtaGetSSchema ("Template", doc); ! 447: TtaSetUriSSchema (templSchema, Template_URI); 1.64 kia 448: 449: // Insert xt:head and others 450: TtaSetStructureChecking (FALSE, doc); 451: if (strcmp (s, "HTML") == 0) 1.63 kia 452: { 1.64 kia 453: // Initialize the xt:head 454: elType.ElTypeNum = HTML_EL_HEAD; 455: head = TtaSearchTypedElement (elType, SearchInTree, root); 456: if(head) 457: { 458: xtType.ElSSchema = templSchema; 459: xtType.ElTypeNum = Template_EL_head; 460: xt = TtaNewElement(doc, xtType); 461: 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: 539: #ifdef AMAYA_DEBUG 1.64 kia 540: DumpAllDeclarations(); 1.63 kia 541: #endif /* AMAYA_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.71 vatton 556: void CreateInstance(char *templatePath, char *instancePath, 1.72 vatton 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: ----------------------------------------------------------------------*/ 812: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec) 813: { 1.34 vatton 814: Element newEl = NULL; 1.16 kia 815: #ifdef TEMPLATES 1.19 vatton 816: Element current = NULL; 1.34 vatton 817: Element child = NULL; 1.19 vatton 818: //char *attrCurrentTypeValue; 819: //ElementType elType; 1.17 kia 820: 1.25 vatton 821: if (TtaGetDocumentAccessMode(doc)) 1.16 kia 822: { 1.23 kia 823: switch (dec->nature) 824: { 825: case SimpleTypeNat: 826: newEl = Template_GetNewSimpleTypeInstance(doc, el, dec); 1.34 vatton 827: newEl = InsertWithNotify (newEl, NULL, el, doc); 1.23 kia 828: break; 829: case XmlElementNat: 830: newEl = Template_GetNewXmlElementInstance(doc, el, dec); 1.34 vatton 831: newEl = InsertWithNotify (newEl, NULL, el, doc); 1.23 kia 832: break; 833: case ComponentNat: 834: newEl = TtaCopyTree(dec->componentType.content, doc, doc, el); 1.74 kia 835: ProcessAttr (dec->usedIn, newEl, doc); 836: 1.23 kia 837: /* Copy elements from new use to existing use. */ 1.76 vatton 838: #ifdef AMAYA_DEBUG 1.74 kia 839: DumpSubtree(newEl, doc, 0); 1.76 vatton 840: #endif /* AMAYA_DEBUG */ 841: child = TtaGetFirstChild(newEl); 842: while (child) 843: { 844: // move the new subtree to the document 845: TtaRemoveTree (child, doc); 846: current = InsertWithNotify (child, current, el, doc); 847: child = TtaGetFirstChild(newEl); 848: } 1.23 kia 849: 850: /* Copy currentType attribute. */ 851: //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL); 852: //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue); 853: TtaDeleteTree(newEl, doc); 854: newEl = el; 855: break; 856: default : 857: //Impossible 858: break; 859: } 1.44 kia 860: Template_FixAccessRight (dec->usedIn, el, doc); 861: TtaUpdateAccessRightInViews (doc, el); 1.23 kia 862: } 1.16 kia 863: #endif /* TEMPLATES */ 864: return newEl; 865: } 866: 1.40 kia 867: 868: /*---------------------------------------------------------------------- 869: Fix access rights. 870: ----------------------------------------------------------------------*/ 1.41 vatton 871: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc) 1.40 kia 872: { 873: #ifdef TEMPLATES 874: ElementType elType; 875: Element child; 1.88 vatton 876: char currentType[MAX_LENGTH], *ptr; 1.40 kia 877: Declaration decl; 878: 879: if (t && el && doc) 880: { 881: elType = TtaGetElementType(el); 1.41 vatton 882: if (elType.ElSSchema == TtaGetSSchema ("Template", doc)) 1.40 kia 883: { 1.41 vatton 884: switch (elType.ElTypeNum) 1.40 kia 885: { 886: case Template_EL_TEXT_UNIT: 1.41 vatton 887: //TtaSetAccessRight( el, ReadWrite, doc); 888: return; 1.40 kia 889: case Template_EL_useEl: 890: case Template_EL_useSimple: 891: GiveAttributeStringValueFromNum(el, Template_ATTR_currentType, 892: (char*)currentType, NULL); 1.88 vatton 893: if (currentType[0] == EOS) 894: { 895: GiveAttributeStringValueFromNum(el, Template_ATTR_types, 896: (char*)currentType, NULL); 897: ptr = strstr (currentType, " "); 898: if (ptr) 899: *ptr = EOS; 900: } 1.40 kia 901: decl = Template_GetDeclaration(t, currentType); 902: if (decl) 903: { 904: switch (decl->nature) 905: { 906: case SimpleTypeNat: 907: case XmlElementNat: 1.41 vatton 908: TtaSetAccessRight (el, ReadWrite, doc); 909: return; 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.80 kia 1261: strcat (buffer, DocumentURLs[doc]); 1.17 kia 1262: strcat (buffer, "\" version=\""); 1.20 vatton 1263: if (t->version) 1264: strcat (buffer, t->version); 1265: else 1266: strcat (buffer, "0.8"); 1.1 vatton 1267: strcat (buffer, "\""); 1.25 vatton 1268: if (t->templateVersion) 1.20 vatton 1269: { 1270: strcat (buffer, " templateVersion=\""); 1271: strcat (buffer, t->templateVersion); 1272: strcat (buffer, "\""); 1273: } 1.1 vatton 1274: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc); 1.90 vatton 1275: TtaRegisterElementCreate (elNew, doc); 1.1 vatton 1276: TtaSetStructureChecking (TRUE, doc); 1.5 vatton 1277: 1278: // update the document title 1.47 kia 1279: if (!strcmp (s, "HTML")) 1.5 vatton 1280: { 1281: elType.ElTypeNum = HTML_EL_TITLE; 1282: elFound = TtaSearchTypedElement (elType, SearchInTree, root); 1.90 vatton 1283: text = TtaGetFirstChild (elFound); 1284: while (text) 1.5 vatton 1285: { 1.90 vatton 1286: elType = TtaGetElementType (text); 1287: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS) 1288: { 1289: TtaRegisterElementReplace (text, doc); 1290: TtaSetTextContent (text, (unsigned char*)Answer_text, 1291: TtaGetDefaultLanguage (), doc); 1292: text = NULL; 1293: } 1294: else if ((elType.ElTypeNum == Template_EL_useEl || 1295: elType.ElTypeNum == Template_EL_useSimple) && 1296: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template")) 1297: // Ignore the template use element 1298: text = TtaGetFirstChild (text); 1299: else 1300: // Look for the first text child 1301: TtaNextSibling (&text); 1.5 vatton 1302: } 1303: } 1.1 vatton 1304: #endif /* TEMPLATES */ 1305: } 1306: 1.80 kia 1307: 1.1 vatton 1308: /*---------------------------------------------------------------------- 1.68 kia 1309: Template_PreInstantiateComponents 1310: Instantiates all components in order to improve editing. 1.3 vatton 1311: ----------------------------------------------------------------------*/ 1.35 kia 1312: void Template_PreInstantiateComponents (XTigerTemplate t) 1.1 vatton 1313: { 1314: #ifdef TEMPLATES 1.90 vatton 1315: ForwardIterator iter; 1316: Declaration dec; 1317: SearchSetNode node; 1318: 1.25 vatton 1319: if (!t) 1.23 kia 1320: return; 1321: 1.90 vatton 1322: if (Template_IsInstance (t)) 1323: { 1.76 vatton 1324: #ifdef AMAYA_DEBUG 1.90 vatton 1325: DumpAllDeclarations(); 1.76 vatton 1326: #endif /* AMAYA_DEBUG */ 1.90 vatton 1327: iter = SearchSet_GetForwardIterator(GetComponents(t)); 1.76 vatton 1328: #ifdef AMAYA_DEBUG 1.90 vatton 1329: printf("Template_PreInstantiateComponents %s\n", t->uri); 1.76 vatton 1330: #endif /* AMAYA_DEBUG */ 1.90 vatton 1331: ITERATOR_FOREACH(iter, SearchSetNode, node) 1332: { 1333: dec = (Declaration) node->elem; 1.92 vatton 1334: ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE); 1.90 vatton 1335: } 1336: TtaFreeMemory(iter); 1.1 vatton 1337: } 1338: #endif /* TEMPLATES */ 1339: } 1.84 kia 1340: 1341: /*---------------------------------------------------------------------- 1342: Template_SetName 1343: Set the xt:component or xt:union element xt:name attribute. 1344: Make it unique. 1345: Return TRUE if the name is not modified. 1346: ----------------------------------------------------------------------*/ 1.96 ! vatton 1347: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo) 1.84 kia 1348: { 1349: #ifdef TEMPLATES 1.95 vatton 1350: AttributeType attType; 1351: Attribute attr; 1.96 ! vatton 1352: ThotBool res, res2; 1.95 vatton 1353: 1354: if (doc && el && name) 1.84 kia 1355: { 1.95 vatton 1356: attType.AttrSSchema = TtaGetElementType(el).ElSSchema; 1357: attType.AttrTypeNum = Template_ATTR_name; 1358: attr = TtaGetAttribute(el, attType); 1359: if (attr == NULL) 1360: { 1361: attr = TtaNewAttribute (attType); 1362: TtaAttachAttribute (el, attr, doc); 1.96 ! vatton 1363: if (withUndo) ! 1364: TtaRegisterAttributeCreate (attr, el, doc); 1.95 vatton 1365: } 1.96 ! vatton 1366: if (withUndo) ! 1367: TtaRegisterAttributeReplace(attr, el, doc); 1.95 vatton 1368: TtaSetAttributeText (attr, name, el, doc); 1.96 ! vatton 1369: res = TtaIsValidID (attr, TRUE); ! 1370: res2 = !MakeUniqueName(el, doc, TRUE, FALSE); ! 1371: return (res || res2); ! 1372: } ! 1373: #endif /* TEMPLATES */ ! 1374: return FALSE; ! 1375: } ! 1376: ! 1377: /*---------------------------------------------------------------------- ! 1378: Template_SetName ! 1379: Set the xt:component or xt:union element xt:name attribute. ! 1380: Make it unique. ! 1381: Return TRUE if the name is not modified. ! 1382: ----------------------------------------------------------------------*/ ! 1383: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo) ! 1384: { ! 1385: #ifdef TEMPLATES ! 1386: AttributeType attType; ! 1387: Attribute attr; ! 1388: ! 1389: if (doc && el && label) ! 1390: { ! 1391: attType.AttrSSchema = TtaGetElementType(el).ElSSchema; ! 1392: attType.AttrTypeNum = Template_ATTR_title; ! 1393: attr = TtaGetAttribute(el, attType); ! 1394: if (attr == NULL) ! 1395: { ! 1396: attr = TtaNewAttribute (attType); ! 1397: TtaAttachAttribute (el, attr, doc); ! 1398: if (withUndo) ! 1399: TtaRegisterAttributeCreate (attr, el, doc); ! 1400: } ! 1401: if (withUndo) ! 1402: TtaRegisterAttributeReplace(attr, el, doc); ! 1403: TtaSetAttributeText (attr, label, el, doc); 1.95 vatton 1404: return TtaIsValidID (attr, TRUE); 1.84 kia 1405: } 1406: #endif /* TEMPLATES */ 1407: return FALSE; 1408: } 1409: