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