Annotation of Amaya/amaya/templateInstantiate.c, revision 1.3

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

Webmaster