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

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

Webmaster