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

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

Webmaster