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

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

Webmaster