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

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

Webmaster