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

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 :
1.4     ! quint     386:             if (!insert)
        !           387:               /* the user has clicked a "repeat" button and wants to create
        !           388:                  a new instance of the repeated element. Just create the
        !           389:                  use element */
        !           390:               {
        !           391:                 elType.ElTypeNum = Template_EL_useEl;
        !           392:                 cont = TtaNewElement (doc, elType);
        !           393:                 if (cont)
        !           394:                   {
        !           395:                     TtaSetAccessRight (cont, ReadWrite, doc);
        !           396:                     at = TtaNewAttribute (att);
        !           397:                     if (at)
        !           398:                       {
        !           399:                         TtaAttachAttribute (cont, at, doc);
        !           400:                         TtaSetAttributeText(at, types, cont, doc);
        !           401:                       }
        !           402:                   }
        !           403:               }
1.1       vatton    404:             break;
                    405:           default :
                    406:             //Impossible
                    407:             break;   
                    408:           }
                    409:     }
1.4     ! quint     410:   TtaFreeMemory(types);
1.1       vatton    411:   TtaSetStructureChecking (oldStructureChecking, doc);
                    412:   return cont;
                    413: #endif /* TEMPLATES */
                    414: }
                    415: 
                    416: /*----------------------------------------------------------------------
1.3       vatton    417:   ----------------------------------------------------------------------*/
1.1       vatton    418: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc)
                    419: {
                    420: #ifdef TEMPLATES
                    421:   int            curVal,  minVal,  maxVal;
                    422:   Attribute      curAtt,  minAtt,  maxAtt;
                    423:   AttributeType  curType, minType, maxType;
                    424:   char           *text;
                    425: 
                    426:   //Preparing types
                    427:   curType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    428:   minType.AttrSSchema = maxType.AttrSSchema = curType.AttrSSchema;
                    429:   curType.AttrTypeNum = Template_ATTR_currentOccurs; 
                    430:   minType.AttrTypeNum = Template_ATTR_minOccurs;
                    431:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
                    432: 
                    433:   //Get currentOccurs, minOccurs and maxOccurs attributes
                    434:   curAtt = TtaGetAttribute (el, curType);
                    435:   minAtt = TtaGetAttribute (el, minType);
                    436:   maxAtt = TtaGetAttribute (el, maxType);
                    437: 
                    438:   //Get the values
                    439:   if (minAtt)
                    440:     {
                    441:       text = GetAttributeStringValue(el, minAtt);
                    442:       if (text)
                    443:         {
                    444:           minVal = atoi(text);
                    445:           TtaFreeMemory(text);
                    446:         }
                    447:       else
                    448:         //Error : Attribute with no value
                    449:         return;
                    450:     }
                    451:   else
                    452:     minVal = 0;
                    453: 
                    454:   if (maxAtt)
                    455:     {
                    456:       text = GetAttributeStringValue (el, maxAtt);
                    457:       if (text)
                    458:         {
                    459:           if (!strcmp (text, "*"))
                    460:             maxVal = INT_MAX;
                    461:           else
                    462:             maxVal = atoi (text);
                    463:           TtaFreeMemory (text);
                    464:         }
                    465:       else
                    466:         //Error : Attribute with no value
                    467:         return;
                    468:     }
                    469:   else
                    470:     maxVal = INT_MAX;
                    471: 
                    472:   if (curAtt)
                    473:     {
                    474:       text = GetAttributeStringValue(el, curAtt);
                    475:       if (text)
                    476:         {
1.2       quint     477:           curVal = atoi(text);
1.1       vatton    478:           TtaFreeMemory(text);
                    479:         }
                    480:       else
                    481:         //Error : Attribute with no value
                    482:         return;
                    483:     }
                    484:   else
                    485:     curVal = minVal;
                    486: 
                    487:   text = (char*)TtaGetMemory(MAX_LENGTH);
                    488: 
                    489:   //Create non existing attributes
                    490:   if (!minAtt)
                    491:     {      
                    492:       minAtt = TtaNewAttribute(minType);
                    493:       sprintf(text,"%d",minVal);
                    494:       TtaAttachAttribute(el, minAtt, doc);
                    495:       TtaSetAttributeText(minAtt, text, el, doc);
                    496:     }
                    497: 
                    498:   if (!maxAtt)
                    499:     {  
                    500:       maxAtt = TtaNewAttribute(maxType);
                    501:       if (maxVal<INT_MAX)
                    502:         sprintf(text,"%d",maxVal);
                    503:       else
                    504:         sprintf(text,"*");
                    505:       TtaAttachAttribute(el, maxAtt, doc);      
                    506:       TtaSetAttributeText(maxAtt, text, el, doc);
                    507:     }
                    508: 
                    509:   if (!curAtt)
1.2       quint     510:     {
1.1       vatton    511:       curAtt = TtaNewAttribute(curType);
                    512:       sprintf(text,"%d",curVal);
                    513:       TtaAttachAttribute(el, curAtt, doc);
                    514:       TtaSetAttributeText(curAtt, text, el, doc);
                    515:     }
                    516: 
                    517:   if (text)
                    518:     TtaFreeMemory(text);
                    519: 
                    520:   //We must have currentOccurs children
                    521:   Element  child, newChild;
                    522:   int      childrenCount;
                    523: 
                    524:   child = TtaGetFirstChild(el);
                    525:   if (!child)
                    526:     //Error : a repeat must have at least one child which will be the model
                    527:     return;
                    528:   
                    529:   for(childrenCount = 0; child; TtaNextSibling(&child))
                    530:     {
                    531:       //TODO : Check that every child is valid
                    532:       childrenCount ++;
                    533:     }
                    534: 
                    535:   if (childrenCount > maxVal)
                    536:     //Error : too many children!
                    537:     return;  
                    538: 
                    539:   child = TtaGetLastChild(el);
                    540: 
                    541:   while(childrenCount < curVal)
                    542:     {
                    543:       //Create a new child
                    544:       newChild = TtaCopyTree(child, doc, doc, el);
                    545:       TtaInsertSibling(newChild, child, FALSE, doc);
                    546:       child = newChild;
                    547:       childrenCount++;
                    548:     }
                    549: #endif /* TEMPLATES */
                    550: }
                    551: 
                    552: /*----------------------------------------------------------------------
                    553:   ParseTemplate
1.3       vatton    554:   ----------------------------------------------------------------------*/
1.1       vatton    555: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                    556:                            ThotBool loading)
                    557: {
                    558: #ifdef TEMPLATES
                    559:        AttributeType attType;
                    560:        Attribute att;
                    561:        Element aux; //Needed when deleting trees
                    562:        char *name;
                    563:        ElementType type = TtaGetElementType(el);
                    564:        
                    565:        if (!strcmp (TtaGetSSchemaName(type.ElSSchema),"Template"))
                    566:     {
                    567:       switch(type.ElTypeNum)
                    568:         {
                    569:         case Template_EL_head :
                    570:           //Remove it and all of its children
                    571:           TtaDeleteTree(el, doc);
                    572:           //We must stop searching into this tree
                    573:           return;
                    574:           break;
                    575:         case Template_EL_component :
                    576:           //Replace by a use                           
                    577:           attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    578:           attType.AttrTypeNum = Template_ATTR_name;
                    579:           
                    580:           name = GetAttributeStringValue(el, Template_ATTR_name);                                
                    581:           TtaRemoveAttribute(el, TtaGetAttribute(el, attType), doc);
                    582:           if (NeedAMenu (el, doc))
                    583:             TtaChangeElementType(el, Template_EL_useEl);
                    584:           else
                    585:             TtaChangeElementType(el, Template_EL_useSimple);
                    586:           
                    587:           attType.AttrTypeNum = Template_ATTR_types;
                    588:           att = TtaNewAttribute(attType);
                    589:           TtaAttachAttribute(el, att, doc);
                    590:           TtaSetAttributeText(att, name, el, doc);
                    591:           
                    592:           attType.AttrTypeNum = Template_ATTR_currentType;
                    593:           att = TtaNewAttribute(attType);
                    594:           TtaAttachAttribute(el, att, doc);              
                    595:           TtaSetAttributeText(att, name, el, doc);
                    596:           
                    597:           break;
                    598:         case Template_EL_option :
                    599:           aux = NULL;
                    600:           break;
                    601:         case Template_EL_bag :
                    602:           //Link to types
                    603:           //Allow editing the content
                    604:           break;
                    605:         case Template_EL_useEl :
                    606:         case Template_EL_useSimple :
                    607:           /* if this use element is not empty, don't do anything: it is
                    608:              supposed to contain a valid instance. This should be
                    609:              checked, though */
                    610:           if (!TtaGetFirstChild (el))
                    611:             InstantiateUse (t, el, doc, TRUE);
                    612:           break;
                    613:         case Template_EL_attribute :
                    614:           if (!loading)
                    615:             InstantiateAttribute (t, el, doc);
                    616:           break;
                    617:         case Template_EL_repeat :
                    618:           InstantiateRepeat (t, el, doc);
                    619:           break;
                    620:         default :
                    621:           break;
                    622:         }
                    623:     }
                    624:        
                    625:        Element child = TtaGetFirstChild(el);
                    626:        while(child!=NULL)
                    627:     {
                    628:       aux = child;
                    629:       TtaNextSibling(&aux);
                    630:       ParseTemplate(t, child, doc, loading);
                    631:       child = aux;
                    632:     }
                    633: #endif /* TEMPLATES */
                    634: }
                    635: 
                    636: /*----------------------------------------------------------------------
1.3       vatton    637:   ----------------------------------------------------------------------*/
1.1       vatton    638: void DoInstanceTemplate (char *templatename)
                    639: {
                    640: #ifdef TEMPLATES
                    641:        XTigerTemplate  t;
                    642:        ElementType               elType;
                    643:        Element                     root, piElem, doctype, elFound, text;
                    644:        char                    *s, *charsetname = NULL, buffer[MAX_LENGTH];
                    645:   int             pi_type;
                    646:   Document        doc;
                    647: 
                    648:        //Instantiate all elements
                    649:        t = (XTigerTemplate) Get (Templates_Dic, templatename);
                    650:   doc = GetTemplateDocument (t);
                    651:        root =  TtaGetMainRoot (doc);
                    652:        ParseTemplate (t, root, doc, FALSE);
                    653: 
                    654:   //Look for PIs
                    655:   /* check if the document has a DOCTYPE declaration */
                    656: #ifdef ANNOTATIONS
                    657:   if (DocumentTypes[doc]  == docAnnot)
                    658:     elType = TtaGetElementType (root);
                    659:   else
                    660: #endif /* ANNOTATIONS */
                    661:     elType = TtaGetElementType (root);
                    662:   s = TtaGetSSchemaName (elType.ElSSchema);
                    663:   if (strcmp (s, "HTML") == 0)
                    664:     {
                    665:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                    666:       pi_type = HTML_EL_XMLPI;
                    667:     }
                    668: #ifdef _SVG
                    669:   else if (strcmp (s, "SVG") == 0)
                    670:     {
                    671:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                    672:       pi_type = SVG_EL_XMLPI;
                    673:     }
                    674: #endif /* _SVG */
                    675:   else if (strcmp (s, "MathML") == 0)
                    676:     {
                    677:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                    678:       pi_type = MathML_EL_XMLPI;
                    679:     }
                    680:   else
                    681:     {
                    682:       elType.ElTypeNum = XML_EL_doctype;
                    683:       pi_type = XML_EL_xmlpi;
                    684:     }
                    685:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
                    686:   if (!doctype)
                    687:     {
                    688:       /* generate the XML declaration */
                    689:       /* Check the Thot abstract tree against the structure schema. */
                    690:       TtaSetStructureChecking (FALSE, doc);
                    691:       elType.ElTypeNum = pi_type;
                    692:       doctype = TtaNewTree (doc, elType, "");
                    693:       TtaInsertFirstChild (&doctype, root, doc);
                    694:       elFound = TtaGetFirstChild (doctype);
                    695:       text = TtaGetFirstChild (elFound);
                    696:       strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                    697:       charsetname = UpdateDocumentCharset (doc);
                    698:       strcat (buffer, charsetname);
                    699:       strcat (buffer, "\"");
                    700:       TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    701:       TtaSetStructureChecking (TRUE, doc);
                    702:     }
                    703:   
                    704:   /* generate the XTiger PI */
                    705:   /* Check the Thot abstract tree against the structure schema. */
                    706:   TtaSetStructureChecking (FALSE, doc);
                    707:   elType.ElTypeNum = pi_type;
                    708:   piElem = TtaNewTree (doc, elType, "");
                    709:   TtaInsertSibling(piElem, doctype, FALSE, doc);
                    710:   elFound = TtaGetFirstChild (piElem);
                    711:   text = TtaGetFirstChild (elFound);
                    712:   strcpy (buffer, "xtiger template=\"");
                    713:   strcat (buffer, templatename);
                    714:   strcat (buffer, "\"");
                    715:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    716:   TtaSetStructureChecking (TRUE, doc);
                    717: #endif /* TEMPLATES */
                    718: }
                    719: 
                    720: /*----------------------------------------------------------------------
1.3       vatton    721:   PreInstantiateComponents: Instantiates all components in order to improve
                    722:   editing.
                    723:   ----------------------------------------------------------------------*/
1.1       vatton    724: void PreInstantiateComponents(XTigerTemplate t)
                    725: {
                    726: #ifdef TEMPLATES 
                    727:   DicDictionary components = GetComponents(t);
                    728:   Declaration comp;
                    729: 
                    730:   for(First(components);!IsDone(components);Next(components))
                    731:     {
                    732:       comp = (Declaration) CurrentElement(components);
                    733:       ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
                    734:     }
                    735: #endif /* TEMPLATES */
                    736: }

Webmaster