Return to templateInstantiate.c CVS log | Up to [Public] / Amaya / amaya |
1.19 ! vatton 1: /* ! 2: * ! 3: * COPYRIGHT INRIA and W3C, 2006-2007 ! 4: * Please first read the full copyright statement in file COPYRIGHT. ! 5: * ! 6: */ ! 7: 1.1 vatton 8: #include "templates.h" 9: 10: #define THOT_EXPORT extern 11: #include "templateDeclarations.h" 12: 1.8 kia 13: #include "Elemlist.h" 14: 1.1 vatton 15: #include "EDITimage_f.h" 16: #include "HTMLactions_f.h" 17: #include "HTMLsave_f.h" 18: #include "init_f.h" 19: #include "mydictionary_f.h" 20: #include "templates_f.h" 21: #include "templateDeclarations_f.h" 22: #include "templateInstantiate_f.h" 23: #include "Templatebuilder_f.h" 24: #include "templateUtils_f.h" 25: #include "fetchHTMLname_f.h" 26: #include "Template.h" 27: 28: #ifdef TEMPLATES 29: #define TEMPLATE_SCHEMA_NAME "Template" 30: 31: typedef struct _InstantiateCtxt 32: { 33: char * templatePath; 34: char * instancePath; 35: char * schemaName; 36: DocumentType docType; 37: ThotBool dontReplace; 38: } InstantiateCtxt; 39: #endif /* TEMPLATES */ 40: 41: 42: /*---------------------------------------------------------------------- 43: CreateInstance 1.3 vatton 44: ----------------------------------------------------------------------*/ 1.1 vatton 45: void CreateInstance(char *templatePath, char *instancePath) 1.3 vatton 46: { 1.1 vatton 47: #ifdef TEMPLATES 1.14 vatton 48: Document doc = 0; 1.5 vatton 49: DocumentType docType; 1.14 vatton 50: ElementType elType; 51: Element root, title, text; 52: char *s; 53: int alreadyOnDoc = 0; 54: ThotBool alreadyViewing = FALSE; 1.1 vatton 55: 1.8 kia 56: XTigerTemplate t = (XTigerTemplate)Dictionary_Get (Templates_Dic, templatePath); 1.1 vatton 57: if (t == NULL) 58: //The template must be loaded before calling this function 59: return; 60: 1.5 vatton 61: doc = GetTemplateDocument (t); 1.3 vatton 62: while (alreadyOnDoc < DocumentTableLength-1 && !alreadyViewing) 1.1 vatton 63: { 64: alreadyOnDoc++; 65: if (DocumentURLs[alreadyOnDoc]) 66: alreadyViewing = !strcmp (DocumentURLs[alreadyOnDoc],instancePath); 67: } 68: 69: if (!TtaPrepareUndo (doc)) 70: { 71: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0); 1.14 vatton 72: root = TtaGetRootElement(doc); 73: elType = TtaGetElementType (root); 74: // get the target document type 75: s = TtaGetSSchemaName (elType.ElSSchema); 76: if (strcmp (s, "HTML") == 0) 77: docType = docHTML; 78: else if (strcmp (s, "SVG") == 0) 79: docType = docSVG; 80: else if (strcmp (s, "MathML") == 0) 81: docType = docMath; 82: else 83: docType = docXml; 84: 1.1 vatton 85: SetRelativeURLs (doc, instancePath); 86: 87: switch (docType) 88: { 1.14 vatton 89: case docSVG: 1.1 vatton 90: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "SVGT"); 91: break; 1.14 vatton 92: case docMath: 1.1 vatton 93: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "MathMLT"); 94: break; 1.14 vatton 95: case docHTML: 96: // Initialize the document title 97: elType.ElTypeNum = HTML_EL_TITLE; 98: title = TtaSearchTypedElement (elType, SearchInTree, root); 99: text = TtaGetFirstChild (title); 100: while (text) 101: { 102: elType = TtaGetElementType (text); 103: if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS) 104: { 105: TtaSetTextContent (text, (unsigned char*)Answer_text, 106: TtaGetDefaultLanguage (), doc); 107: text = NULL; 108: } 109: else if ((elType.ElTypeNum == Template_EL_useEl || 110: elType.ElTypeNum == Template_EL_useSimple) && 111: !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template")) 112: // Ignore the template use element 113: text = TtaGetFirstChild (text); 114: else 115: // Look for the first text child 116: TtaNextSibling (&text); 117: } 1.1 vatton 118: if (TtaGetDocumentProfile(doc)==L_Xhtml11 || TtaGetDocumentProfile(doc)==L_Basic) 119: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "HTMLT11"); 120: else 121: TtaExportDocumentWithNewLineNumbers (doc, instancePath, "HTMLTX"); 122: break; 1.14 vatton 123: default: 1.1 vatton 124: TtaExportDocumentWithNewLineNumbers (doc, instancePath, NULL); 125: break; 126: } 127: 128: TtaCloseUndoSequence (doc); 129: TtaUndoNoRedo (doc); 130: TtaClearUndoHistory (doc); 131: } 132: 1.3 vatton 133: if (!alreadyViewing) 134: { 135: // Open the instance 1.1 vatton 136: TtaExtractName (instancePath, DirectoryName, DocumentName); 137: CallbackDialogue (BaseDialog + OpenForm, INTEGER_DATA, (char *) 1); 138: } 1.3 vatton 139: else 140: { 141: // Reload on the existing view 142: Reload (alreadyOnDoc, 0); 143: } 1.1 vatton 144: #endif /* TEMPLATES */ 145: } 146: 147: /*---------------------------------------------------------------------- 1.3 vatton 148: ----------------------------------------------------------------------*/ 1.7 cvs 149: void InstantiateTemplate_callback (int newdoc, int status, char *urlName, char *outputfile, 150: char *proxyName, AHTHeaders *http_headers, void * context) 1.1 vatton 151: { 152: #ifdef TEMPLATES 153: InstantiateCtxt *ctx = (InstantiateCtxt*)context; 154: 155: DoInstanceTemplate (ctx->templatePath); 156: CreateInstance (ctx->templatePath, ctx->instancePath); 157: TtaFreeMemory (ctx->templatePath); 158: TtaFreeMemory (ctx->instancePath); 159: TtaFreeMemory (ctx); 160: #endif /* TEMPLATES */ 161: } 162: 163: /*---------------------------------------------------------------------- 1.3 vatton 164: ----------------------------------------------------------------------*/ 1.1 vatton 165: void InstantiateTemplate (Document doc, char *templatename, char *docname, 166: DocumentType docType, ThotBool loaded) 167: { 168: #ifdef TEMPLATES 169: if (!loaded) 1.3 vatton 170: { 171: // Create the callback context 172: InstantiateCtxt *ctx = (InstantiateCtxt *)TtaGetMemory (sizeof (InstantiateCtxt)); 173: ctx->templatePath = TtaStrdup (templatename); 174: ctx->instancePath = TtaStrdup (docname); 175: ctx->schemaName = GetSchemaFromDocType(docType); 176: ctx->docType = docType; 1.1 vatton 177: 1.3 vatton 178: GetAmayaDoc (templatename, NULL, doc, doc, CE_MAKEBOOK, FALSE, 1.6 cvs 179: (void (*)(int, int, char*, char*, char*, const AHTHeaders*, void*)) InstantiateTemplate_callback, 1.3 vatton 180: (void *) ctx); 181: } 1.1 vatton 182: else 183: { 184: DoInstanceTemplate (templatename); 185: CreateInstance (templatename, docname); 1.3 vatton 186: } 1.1 vatton 187: #endif /* TEMPLATES */ 188: } 189: 190: /*---------------------------------------------------------------------- 191: InstantiateAttribute 1.3 vatton 192: ----------------------------------------------------------------------*/ 1.1 vatton 193: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc) 194: { 195: #ifdef TEMPLATES 196: AttributeType useType, nameType, defaultType, attrType; 197: Attribute useAttr, nameAttr, defAttr, attr; 198: ElementType elType; 199: Element parent; 200: char *text, *elementName; 201: ThotBool level; 202: NotifyAttribute event; 203: 204: parent = TtaGetParent (el); 205: if (!parent) 206: return; 207: // if attribute "use" has value "optional", don't do anything 208: useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc); 209: useType.AttrTypeNum = Template_ATTR_useAt; 210: useAttr = TtaGetAttribute (el, useType); 211: if (useAttr) 212: // there is a "use" attribute. Check its value 213: { 1.10 kia 214: text = GetAttributeStringValue (el, useAttr, NULL); 1.18 kia 215: if(!text) 1.1 vatton 216: return; 1.18 kia 217: if(strcmp (text, "optional") == 0) 218: { 219: TtaFreeMemory(text); 220: return; 221: } 222: TtaFreeMemory(text); 1.1 vatton 223: } 1.18 kia 224: 1.1 vatton 225: // get the "name" and "default" attributes 226: nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc); 1.9 vatton 227: nameType.AttrTypeNum = Template_ATTR_ref_name; 1.1 vatton 228: defaultType.AttrTypeNum = Template_ATTR_defaultAt; 229: nameAttr = TtaGetAttribute (el, nameType); 230: defAttr = TtaGetAttribute (el, defaultType); 231: if (nameAttr) 232: { 1.10 kia 233: text = GetAttributeStringValue (el, nameAttr, NULL); 1.1 vatton 234: if (text) 235: { 236: elType = TtaGetElementType (parent); 237: elementName = TtaGetElementTypeName (elType); 238: level = TRUE; 239: MapHTMLAttribute (text, &attrType, elementName, &level, doc); 240: TtaFreeMemory(text); 241: attr = TtaNewAttribute (attrType); 242: if (attr) 243: { 244: TtaAttachAttribute (parent, attr, doc); 245: if (defAttr) 246: { 1.10 kia 247: text = GetAttributeStringValue (el, defAttr, NULL); 1.18 kia 248: if(text) 249: TtaSetAttributeText(attr, text, parent, doc); 1.1 vatton 250: TtaFreeMemory(text); 251: // if it's a src arttribute for an image, load the image 252: if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") && 253: elType.ElTypeNum == HTML_EL_IMG) 254: if (attrType.AttrTypeNum == HTML_ATTR_SRC && 255: attrType.AttrSSchema == elType.ElSSchema) 256: { 257: event.document = doc; 258: event.element = parent; 259: event.attribute = attr; 260: SRCattrModified (&event); 261: } 262: } 263: } 264: } 265: } 266: #endif /* TEMPLATES */ 267: } 268: 269: #ifdef TEMPLATES 270: /*---------------------------------------------------------------------- 271: ProcessAttr 1.9 vatton 272: Look for all "attribute" elements in the subtree and instantiate them 1.3 vatton 273: ----------------------------------------------------------------------*/ 1.1 vatton 274: static void ProcessAttr (XTigerTemplate t, Element el, Document doc) 275: { 276: Element child; 277: ElementType elType; 278: 279: for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child)) 280: { 281: elType = TtaGetElementType (child); 282: if (elType.ElTypeNum == Template_EL_attribute && 283: !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME)) 284: InstantiateAttribute (t, child, doc); 285: else 286: ProcessAttr (t, child, doc); 287: } 288: } 289: #endif /* TEMPLATES */ 290: 1.12 kia 291: 292: /*---------------------------------------------------------------------- 293: Template_GetNewSimpleTypeInstance 294: Create an new instance of xt:use/SimpleType 295: The decl attribute must embed SimpleType declaration (no validation). 296: @param decl Declaration of new element 297: @param parent Future parent element 298: @param doc Document 299: @return The new element 300: ----------------------------------------------------------------------*/ 301: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl) 302: { 303: Element newEl = NULL; 304: #ifdef TEMPLATES 305: ElementType elType; 306: char *empty = " "; 1.17 kia 307: elType.ElSSchema = TtaGetSSchema(TEMPLATE_SSHEMA_NAME, doc); 1.12 kia 308: elType.ElTypeNum = Template_EL_TEXT_UNIT; 309: newEl = TtaNewElement (doc, elType); 1.17 kia 310: 1.12 kia 311: TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc); 312: 313: #endif 314: return newEl; 315: } 316: 317: /*---------------------------------------------------------------------- 318: Template_GetNewXmlElementInstance 319: Create an new instance of xt:use/XmlElement 320: The decl attribute must embed XmlElement declaration (no validation). 321: @param decl Declaration of new element 322: @param parent Future parent element 323: @param doc Document 324: @return The new element 325: ----------------------------------------------------------------------*/ 326: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl) 327: { 328: Element newEl = NULL; 329: #ifdef TEMPLATES 330: ElementType elType; 331: 1.13 kia 332: GIType(decl->name, &elType, doc); 1.12 kia 333: if(elType.ElTypeNum!=0) 334: { 335: newEl = TtaNewElement (doc, elType); 336: } 337: #endif /* TEMPLATES */ 338: return newEl; 339: } 340: 341: /*---------------------------------------------------------------------- 1.16 kia 342: Template_InsertUseChildren 343: Insert children to a xt:use 344: The dec parameter must be valid and will not be verified. It must be a 345: direct child element (for union elements). 346: @param el element (xt:use) in which insert a new element 347: @param dec Template declaration of the element to insert 348: @return The inserted element (the xt:use element if insertion is multiple as component) 349: ----------------------------------------------------------------------*/ 350: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec) 351: { 1.19 ! vatton 352: Element newEl = NULL; 1.16 kia 353: #ifdef TEMPLATES 1.19 ! vatton 354: Element current = NULL; ! 355: Element child = NULL; ! 356: //char *attrCurrentTypeValue; ! 357: //ElementType elType; 1.17 kia 358: 1.16 kia 359: switch (dec->nature) 360: { 361: case SimpleTypeNat: 362: newEl = Template_GetNewSimpleTypeInstance(doc, el, dec); 1.19 ! vatton 363: TtaInsertFirstChild (&newEl, el, doc); 1.16 kia 364: break; 365: case XmlElementNat: 366: newEl = Template_GetNewXmlElementInstance(doc, el, dec); 1.19 ! vatton 367: TtaInsertFirstChild (&newEl, el, doc); 1.16 kia 368: break; 369: case ComponentNat: 1.19 ! vatton 370: newEl = TtaCopyTree(dec->componentType.content, doc, doc, el); ! 371: ProcessAttr (dec->declaredIn, newEl, doc); 1.16 kia 372: 373: /* Copy elements from new use to existing use. */ 374: while((child = TtaGetFirstChild(newEl))) 375: { 1.19 ! vatton 376: TtaRemoveTree (child, doc); 1.16 kia 377: if(current) 1.19 ! vatton 378: TtaInsertSibling (child, current, FALSE, doc); 1.16 kia 379: else 1.19 ! vatton 380: TtaInsertFirstChild (&child, el, doc); 1.16 kia 381: current = child; 382: } 383: 384: /* Copy currentType attribute. */ 1.19 ! vatton 385: //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL); ! 386: //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue); 1.16 kia 387: TtaDeleteTree(newEl, doc); 388: newEl = el; 389: break; 390: case UnionNat : 391: /* Nothing to do.*/ 392: // elType.ElTypeNum = Template_EL_useEl; 393: // cont = TtaNewElement (doc, elType); 394: // if (cont) 395: // { 396: // TtaSetAccessRight (cont, ReadWrite, doc); 397: // at = TtaNewAttribute (att); 398: // if (at) 399: // { 400: // TtaAttachAttribute (cont, at, doc); 401: // TtaSetAttributeText(at, types, cont, doc); 402: // } 403: // } 404: /* @@@@@ */ 405: break; 406: default : 407: //Impossible 408: break; 409: } 410: 411: #endif /* TEMPLATES */ 412: return newEl; 413: } 414: 415: /*---------------------------------------------------------------------- 1.1 vatton 416: InstantiateUse 1.3 vatton 417: ----------------------------------------------------------------------*/ 1.1 vatton 418: Element InstantiateUse (XTigerTemplate t, Element el, Document doc, 419: ThotBool insert) 420: { 421: #ifdef TEMPLATES 1.17 kia 422: Element cont; 1.1 vatton 423: ElementType elType; 424: Declaration dec; 425: int size, nbitems; 426: struct menuType *items; 427: char *types; 428: ThotBool oldStructureChecking; 429: 430: /* get the value of the "types" attribute */ 431: cont = NULL; 1.12 kia 432: elType = TtaGetElementType (el); 1.18 kia 433: types = GetAttributeStringValueFromNum(el, Template_ATTR_types, &size); 434: if(!types) 435: return NULL; 1.1 vatton 436: giveItems (types, size, &items, &nbitems); 437: // No structure checking 438: oldStructureChecking = TtaGetStructureChecking (doc); 439: TtaSetStructureChecking (FALSE, doc); 1.10 kia 440: 1.1 vatton 441: if (nbitems == 1) 442: /* only one type in the "types" attribute */ 443: { 1.17 kia 444: dec = Template_GetDeclaration (t, items[0].label); 1.1 vatton 445: if (dec) 1.16 kia 446: cont = Template_InsertUseChildren(doc, el, dec); 1.1 vatton 447: } 1.4 quint 448: TtaFreeMemory(types); 1.18 kia 449: TtaFreeMemory(items); 1.1 vatton 450: TtaSetStructureChecking (oldStructureChecking, doc); 451: return cont; 452: #endif /* TEMPLATES */ 453: } 454: 455: /*---------------------------------------------------------------------- 1.3 vatton 456: ----------------------------------------------------------------------*/ 1.1 vatton 457: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc) 458: { 459: #ifdef TEMPLATES 460: int curVal, minVal, maxVal; 461: Attribute curAtt, minAtt, maxAtt; 462: AttributeType curType, minType, maxType; 463: char *text; 464: 465: //Preparing types 466: curType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc); 467: minType.AttrSSchema = maxType.AttrSSchema = curType.AttrSSchema; 468: curType.AttrTypeNum = Template_ATTR_currentOccurs; 469: minType.AttrTypeNum = Template_ATTR_minOccurs; 470: maxType.AttrTypeNum = Template_ATTR_maxOccurs; 471: 472: //Get currentOccurs, minOccurs and maxOccurs attributes 473: curAtt = TtaGetAttribute (el, curType); 474: minAtt = TtaGetAttribute (el, minType); 475: maxAtt = TtaGetAttribute (el, maxType); 476: 477: //Get the values 478: if (minAtt) 479: { 1.10 kia 480: text = GetAttributeStringValue(el, minAtt, NULL); 1.1 vatton 481: if (text) 482: { 483: minVal = atoi(text); 484: TtaFreeMemory(text); 485: } 486: else 487: //Error : Attribute with no value 488: return; 489: } 490: else 491: minVal = 0; 492: 493: if (maxAtt) 494: { 1.10 kia 495: text = GetAttributeStringValue (el, maxAtt, NULL); 1.1 vatton 496: if (text) 497: { 498: if (!strcmp (text, "*")) 499: maxVal = INT_MAX; 500: else 501: maxVal = atoi (text); 502: TtaFreeMemory (text); 503: } 504: else 505: //Error : Attribute with no value 506: return; 507: } 508: else 509: maxVal = INT_MAX; 510: 511: if (curAtt) 512: { 1.10 kia 513: text = GetAttributeStringValue(el, curAtt, NULL); 1.1 vatton 514: if (text) 515: { 1.2 quint 516: curVal = atoi(text); 1.1 vatton 517: TtaFreeMemory(text); 518: } 519: else 520: //Error : Attribute with no value 521: return; 522: } 523: else 524: curVal = minVal; 525: 526: text = (char*)TtaGetMemory(MAX_LENGTH); 527: 528: //Create non existing attributes 529: if (!minAtt) 530: { 531: minAtt = TtaNewAttribute(minType); 532: sprintf(text,"%d",minVal); 533: TtaAttachAttribute(el, minAtt, doc); 534: TtaSetAttributeText(minAtt, text, el, doc); 535: } 536: 537: if (!maxAtt) 538: { 539: maxAtt = TtaNewAttribute(maxType); 540: if (maxVal<INT_MAX) 541: sprintf(text,"%d",maxVal); 542: else 543: sprintf(text,"*"); 544: TtaAttachAttribute(el, maxAtt, doc); 545: TtaSetAttributeText(maxAtt, text, el, doc); 546: } 547: 548: if (!curAtt) 1.2 quint 549: { 1.1 vatton 550: curAtt = TtaNewAttribute(curType); 551: sprintf(text,"%d",curVal); 552: TtaAttachAttribute(el, curAtt, doc); 553: TtaSetAttributeText(curAtt, text, el, doc); 554: } 555: 556: if (text) 557: TtaFreeMemory(text); 558: 559: //We must have currentOccurs children 560: Element child, newChild; 561: int childrenCount; 562: 563: child = TtaGetFirstChild(el); 564: if (!child) 565: //Error : a repeat must have at least one child which will be the model 566: return; 567: 568: for(childrenCount = 0; child; TtaNextSibling(&child)) 569: { 570: //TODO : Check that every child is valid 571: childrenCount ++; 572: } 573: 574: if (childrenCount > maxVal) 575: //Error : too many children! 576: return; 577: 578: child = TtaGetLastChild(el); 579: 580: while(childrenCount < curVal) 581: { 582: //Create a new child 583: newChild = TtaCopyTree(child, doc, doc, el); 584: TtaInsertSibling(newChild, child, FALSE, doc); 585: child = newChild; 586: childrenCount++; 587: } 588: #endif /* TEMPLATES */ 589: } 590: 591: /*---------------------------------------------------------------------- 592: ParseTemplate 1.3 vatton 593: ----------------------------------------------------------------------*/ 1.1 vatton 594: static void ParseTemplate (XTigerTemplate t, Element el, Document doc, 595: ThotBool loading) 596: { 597: #ifdef TEMPLATES 598: AttributeType attType; 1.5 vatton 599: Attribute att; 600: Element aux, child; //Needed when deleting trees 601: char *name; 602: ElementType elType = TtaGetElementType (el); 1.1 vatton 603: 1.5 vatton 604: name = TtaGetSSchemaName (elType.ElSSchema); 605: if (!strcmp (name, "Template")) 1.1 vatton 606: { 1.5 vatton 607: switch(elType.ElTypeNum) 1.1 vatton 608: { 609: case Template_EL_head : 610: //Remove it and all of its children 611: TtaDeleteTree(el, doc); 612: //We must stop searching into this tree 613: return; 614: break; 615: case Template_EL_component : 616: //Replace by a use 1.5 vatton 617: attType.AttrSSchema = elType.ElSSchema; 1.1 vatton 618: attType.AttrTypeNum = Template_ATTR_name; 619: 1.18 kia 620: name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL); 1.5 vatton 621: TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc); 1.1 vatton 622: if (NeedAMenu (el, doc)) 1.5 vatton 623: TtaChangeElementType (el, Template_EL_useEl); 1.1 vatton 624: else 1.5 vatton 625: TtaChangeElementType (el, Template_EL_useSimple); 1.1 vatton 626: 627: attType.AttrTypeNum = Template_ATTR_types; 1.5 vatton 628: att = TtaNewAttribute (attType); 629: TtaAttachAttribute (el, att, doc); 1.18 kia 630: if(name) 631: TtaSetAttributeText (att, name, el, doc); 1.1 vatton 632: 633: attType.AttrTypeNum = Template_ATTR_currentType; 1.5 vatton 634: att = TtaNewAttribute (attType); 1.18 kia 635: TtaAttachAttribute (el, att, doc); 636: if(name) 637: TtaSetAttributeText (att, name, el, doc); 1.1 vatton 638: 639: break; 640: case Template_EL_option : 641: aux = NULL; 642: break; 643: case Template_EL_bag : 644: //Link to types 645: //Allow editing the content 646: break; 647: case Template_EL_useEl : 648: case Template_EL_useSimple : 649: /* if this use element is not empty, don't do anything: it is 650: supposed to contain a valid instance. This should be 651: checked, though */ 652: if (!TtaGetFirstChild (el)) 653: InstantiateUse (t, el, doc, TRUE); 654: break; 655: case Template_EL_attribute : 656: if (!loading) 657: InstantiateAttribute (t, el, doc); 658: break; 659: case Template_EL_repeat : 660: InstantiateRepeat (t, el, doc); 661: break; 662: default : 663: break; 664: } 665: } 666: 1.5 vatton 667: child = TtaGetFirstChild (el); 668: while (child) 1.1 vatton 669: { 670: aux = child; 1.5 vatton 671: TtaNextSibling (&aux); 672: ParseTemplate (t, child, doc, loading); 1.1 vatton 673: child = aux; 674: } 675: #endif /* TEMPLATES */ 676: } 677: 678: /*---------------------------------------------------------------------- 1.3 vatton 679: ----------------------------------------------------------------------*/ 1.1 vatton 680: void DoInstanceTemplate (char *templatename) 681: { 682: #ifdef TEMPLATES 683: XTigerTemplate t; 684: ElementType elType; 685: Element root, piElem, doctype, elFound, text; 686: char *s, *charsetname = NULL, buffer[MAX_LENGTH]; 687: int pi_type; 688: Document doc; 689: 690: //Instantiate all elements 1.8 kia 691: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, templatename); 1.1 vatton 692: doc = GetTemplateDocument (t); 693: root = TtaGetMainRoot (doc); 694: ParseTemplate (t, root, doc, FALSE); 695: 696: //Look for PIs 697: /* check if the document has a DOCTYPE declaration */ 698: #ifdef ANNOTATIONS 699: if (DocumentTypes[doc] == docAnnot) 700: elType = TtaGetElementType (root); 701: else 702: #endif /* ANNOTATIONS */ 703: elType = TtaGetElementType (root); 704: s = TtaGetSSchemaName (elType.ElSSchema); 705: if (strcmp (s, "HTML") == 0) 706: { 707: elType.ElTypeNum = HTML_EL_DOCTYPE; 708: pi_type = HTML_EL_XMLPI; 709: } 710: #ifdef _SVG 711: else if (strcmp (s, "SVG") == 0) 712: { 713: elType.ElTypeNum = SVG_EL_DOCTYPE; 714: pi_type = SVG_EL_XMLPI; 715: } 716: #endif /* _SVG */ 717: else if (strcmp (s, "MathML") == 0) 718: { 719: elType.ElTypeNum = MathML_EL_DOCTYPE; 720: pi_type = MathML_EL_XMLPI; 721: } 722: else 723: { 724: elType.ElTypeNum = XML_EL_doctype; 725: pi_type = XML_EL_xmlpi; 726: } 727: doctype = TtaSearchTypedElement (elType, SearchInTree, root); 728: if (!doctype) 729: { 730: /* generate the XML declaration */ 731: /* Check the Thot abstract tree against the structure schema. */ 732: TtaSetStructureChecking (FALSE, doc); 733: elType.ElTypeNum = pi_type; 734: doctype = TtaNewTree (doc, elType, ""); 735: TtaInsertFirstChild (&doctype, root, doc); 736: elFound = TtaGetFirstChild (doctype); 737: text = TtaGetFirstChild (elFound); 738: strcpy (buffer, "xml version=\"1.0\" encoding=\""); 739: charsetname = UpdateDocumentCharset (doc); 740: strcat (buffer, charsetname); 741: strcat (buffer, "\""); 742: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc); 743: TtaSetStructureChecking (TRUE, doc); 744: } 745: 746: /* generate the XTiger PI */ 747: /* Check the Thot abstract tree against the structure schema. */ 748: TtaSetStructureChecking (FALSE, doc); 749: elType.ElTypeNum = pi_type; 750: piElem = TtaNewTree (doc, elType, ""); 751: TtaInsertSibling(piElem, doctype, FALSE, doc); 752: elFound = TtaGetFirstChild (piElem); 753: text = TtaGetFirstChild (elFound); 754: strcpy (buffer, "xtiger template=\""); 755: strcat (buffer, templatename); 1.17 kia 756: strcat (buffer, "\" version=\""); 757: strcat (buffer, t->version); 1.1 vatton 758: strcat (buffer, "\""); 1.17 kia 759: if(t->templateVersion) 760: { 761: strcat (buffer, " templateVersion=\""); 762: strcat (buffer, t->templateVersion); 763: strcat (buffer, "\""); 764: } 1.1 vatton 765: TtaSetTextContent (text, (unsigned char*)buffer, Latin_Script, doc); 766: TtaSetStructureChecking (TRUE, doc); 1.5 vatton 767: 768: // update the document title 769: if (!strcmp (s, "HTML")) 770: { 771: elType.ElTypeNum = HTML_EL_TITLE; 772: elFound = TtaSearchTypedElement (elType, SearchInTree, root); 773: if (elFound) 774: { 775: elFound = TtaGetFirstChild (elFound); 776: TtaSetTextContent (elFound, (unsigned char *)Answer_text, 777: TtaGetDefaultLanguage (), doc); 778: } 779: } 1.1 vatton 780: #endif /* TEMPLATES */ 781: } 782: 783: /*---------------------------------------------------------------------- 1.3 vatton 784: PreInstantiateComponents: Instantiates all components in order to improve 785: editing. 786: ----------------------------------------------------------------------*/ 1.1 vatton 787: void PreInstantiateComponents(XTigerTemplate t) 788: { 789: #ifdef TEMPLATES 790: DicDictionary components = GetComponents(t); 791: Declaration comp; 792: 1.8 kia 793: for(Dictionary_First(components);!Dictionary_IsDone(components);Dictionary_Next(components)) 1.1 vatton 794: { 1.8 kia 795: comp = (Declaration) Dictionary_CurrentElement(components); 1.1 vatton 796: ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE); 797: } 798: #endif /* TEMPLATES */ 799: }