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

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.18      kia       215:       if(!text)
1.1       vatton    216:         return;
1.18      kia       217:       if(strcmp (text, "optional") == 0)
                    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.18      kia       248:                   if(text)
                    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.17      kia       307:   elType.ElSSchema = TtaGetSSchema(TEMPLATE_SSHEMA_NAME, doc);
1.12      kia       308:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    309:   newEl = TtaNewElement (doc, elType);
1.17      kia       310:   
1.12      kia       311:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
                    312: 
                    313: #endif 
                    314:   return newEl;
                    315: }
                    316: 
                    317: /*----------------------------------------------------------------------
                    318:   Template_GetNewXmlElementInstance
                    319:   Create an new instance of xt:use/XmlElement
                    320:   The decl attribute must embed XmlElement declaration (no validation).
                    321:   @param decl Declaration of new element
                    322:   @param parent Future parent element
                    323:   @param doc Document
                    324:   @return The new element
                    325:   ----------------------------------------------------------------------*/
                    326: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
                    327: {
                    328:   Element           newEl = NULL;
                    329: #ifdef TEMPLATES
                    330:   ElementType       elType;
                    331: 
1.13      kia       332:   GIType(decl->name, &elType, doc);
1.12      kia       333:   if(elType.ElTypeNum!=0)
                    334:   {
1.23    ! kia       335:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       336:   }
                    337: #endif /* TEMPLATES */
                    338:   return newEl;
                    339: }
                    340: 
                    341: /*----------------------------------------------------------------------
1.16      kia       342:   Template_InsertUseChildren
                    343:   Insert children to a xt:use
                    344:   The dec parameter must be valid and will not be verified. It must be a
                    345:     direct child element (for union elements).
                    346:   @param el element (xt:use) in which insert a new element
                    347:   @param dec Template declaration of the element to insert
                    348:   @return The inserted element (the xt:use element if insertion is multiple as component)
                    349:   ----------------------------------------------------------------------*/
                    350: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    351: {
1.19      vatton    352:   Element     newEl   = NULL;
1.16      kia       353: #ifdef TEMPLATES
1.19      vatton    354:   Element     current = NULL;
                    355:   Element     child   = NULL;
                    356:   //char       *attrCurrentTypeValue;
                    357:   //ElementType elType;
1.17      kia       358:   
1.23    ! kia       359:   if(TtaGetDocumentAccessMode(doc))
1.16      kia       360:   {
1.23    ! kia       361:     switch (dec->nature)
        !           362:     {
        !           363:       case SimpleTypeNat:
        !           364:         newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
        !           365:         TtaInsertFirstChild (&newEl, el, doc);
        !           366:         break;
        !           367:       case XmlElementNat:
        !           368:         newEl = Template_GetNewXmlElementInstance(doc, el, dec);
        !           369:         TtaInsertFirstChild (&newEl, el, doc);
        !           370:         break;
        !           371:       case ComponentNat:
        !           372:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
        !           373:         ProcessAttr (dec->declaredIn, newEl, doc);
        !           374:         
        !           375:         /* Copy elements from new use to existing use. */
        !           376:         while((child = TtaGetFirstChild(newEl)))
        !           377:         {
        !           378:           TtaRemoveTree (child, doc);
        !           379:           if(current)
        !           380:             TtaInsertSibling (child, current, FALSE, doc);
        !           381:           else
        !           382:             TtaInsertFirstChild (&child, el, doc);      
        !           383:           current = child; 
        !           384:         }
        !           385:         
        !           386:         /* Copy currentType attribute. */
        !           387:         //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL);
        !           388:         //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue);
        !           389:         TtaDeleteTree(newEl, doc);
        !           390:         newEl = el;
        !           391:         break;
        !           392:       case UnionNat :
        !           393:         /* Nothing to do.*/
        !           394:   //                elType.ElTypeNum = Template_EL_useEl;
        !           395:   //                cont = TtaNewElement (doc, elType);
        !           396:   //                if (cont)
        !           397:   //                  {
        !           398:   //                    TtaSetAccessRight (cont, ReadWrite, doc);
        !           399:   //                    at = TtaNewAttribute (att);
        !           400:   //                    if (at)
        !           401:   //                      {
        !           402:   //                        TtaAttachAttribute (cont, at, doc);
        !           403:   //                        TtaSetAttributeText(at, types, cont, doc);
        !           404:   //                      }
        !           405:   //                  }
        !           406:         /* @@@@@ */
        !           407:         break;
        !           408:       default :
        !           409:         //Impossible
        !           410:         break;   
        !           411:     }
        !           412:   }  
1.16      kia       413: #endif /* TEMPLATES */
                    414:   return newEl;
                    415: }
                    416: 
                    417: /*----------------------------------------------------------------------
1.1       vatton    418:   InstantiateUse
1.3       vatton    419:   ----------------------------------------------------------------------*/
1.1       vatton    420: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
                    421:                         ThotBool insert)
                    422: {
                    423: #ifdef TEMPLATES
1.23    ! kia       424:        Element          cont = NULL;
1.1       vatton    425:   ElementType      elType;
                    426:   Declaration      dec;
                    427:   int              size, nbitems;
                    428:   struct menuType  *items;
                    429:   char             *types;
                    430:   ThotBool          oldStructureChecking;
                    431: 
1.23    ! kia       432:   if(!t)
        !           433:     return NULL;
        !           434: 
1.1       vatton    435:   /* get the value of the "types" attribute */
                    436:   cont = NULL;
1.12      kia       437:   elType = TtaGetElementType (el);
1.18      kia       438:   types = GetAttributeStringValueFromNum(el, Template_ATTR_types, &size);
                    439:   if(!types)
                    440:     return NULL;
1.1       vatton    441:   giveItems (types, size, &items, &nbitems);
                    442:   // No structure checking
                    443:   oldStructureChecking = TtaGetStructureChecking (doc);
                    444:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       445:   
1.1       vatton    446:   if (nbitems == 1)
                    447:     /* only one type in the "types" attribute */
                    448:     {
1.17      kia       449:       dec = Template_GetDeclaration (t, items[0].label);
1.1       vatton    450:       if (dec)
1.16      kia       451:         cont = Template_InsertUseChildren(doc, el, dec);
1.1       vatton    452:     }
1.4       quint     453:   TtaFreeMemory(types);
1.18      kia       454:   TtaFreeMemory(items);
1.1       vatton    455:   TtaSetStructureChecking (oldStructureChecking, doc);
                    456:   return cont;
1.23    ! kia       457: #else /* TEMPLATES */
        !           458:   return NULL;
1.1       vatton    459: #endif /* TEMPLATES */
                    460: }
                    461: 
                    462: /*----------------------------------------------------------------------
1.22      kia       463:   InstantiateRepeat
                    464:   Check for min and max param and validate xt:repeat element content.
                    465:   @param registerUndo True to register undo creation sequences.
1.3       vatton    466:   ----------------------------------------------------------------------*/
1.22      kia       467: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc, ThotBool registerUndo)
1.1       vatton    468: {
                    469: #ifdef TEMPLATES
                    470:   int            curVal,  minVal,  maxVal;
                    471:   Attribute      curAtt,  minAtt,  maxAtt;
                    472:   AttributeType  curType, minType, maxType;
                    473:   char           *text;
                    474: 
1.23    ! kia       475:   if(!t)
        !           476:     return;
        !           477: 
1.1       vatton    478:   //Preparing types
                    479:   curType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    480:   minType.AttrSSchema = maxType.AttrSSchema = curType.AttrSSchema;
                    481:   curType.AttrTypeNum = Template_ATTR_currentOccurs; 
                    482:   minType.AttrTypeNum = Template_ATTR_minOccurs;
                    483:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
                    484: 
                    485:   //Get currentOccurs, minOccurs and maxOccurs attributes
                    486:   curAtt = TtaGetAttribute (el, curType);
                    487:   minAtt = TtaGetAttribute (el, minType);
                    488:   maxAtt = TtaGetAttribute (el, maxType);
                    489: 
                    490:   //Get the values
                    491:   if (minAtt)
                    492:     {
1.10      kia       493:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    494:       if (text)
                    495:         {
                    496:           minVal = atoi(text);
                    497:           TtaFreeMemory(text);
                    498:         }
                    499:       else
                    500:         //Error : Attribute with no value
                    501:         return;
                    502:     }
                    503:   else
                    504:     minVal = 0;
                    505: 
                    506:   if (maxAtt)
                    507:     {
1.10      kia       508:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton    509:       if (text)
                    510:         {
                    511:           if (!strcmp (text, "*"))
                    512:             maxVal = INT_MAX;
                    513:           else
                    514:             maxVal = atoi (text);
                    515:           TtaFreeMemory (text);
                    516:         }
                    517:       else
                    518:         //Error : Attribute with no value
                    519:         return;
                    520:     }
                    521:   else
                    522:     maxVal = INT_MAX;
                    523: 
                    524:   if (curAtt)
                    525:     {
1.10      kia       526:       text = GetAttributeStringValue(el, curAtt, NULL);
1.1       vatton    527:       if (text)
                    528:         {
1.2       quint     529:           curVal = atoi(text);
1.1       vatton    530:           TtaFreeMemory(text);
                    531:         }
                    532:       else
                    533:         //Error : Attribute with no value
                    534:         return;
                    535:     }
                    536:   else
                    537:     curVal = minVal;
                    538: 
                    539:   text = (char*)TtaGetMemory(MAX_LENGTH);
                    540: 
                    541:   //Create non existing attributes
                    542:   if (!minAtt)
                    543:     {      
                    544:       minAtt = TtaNewAttribute(minType);
                    545:       sprintf(text,"%d",minVal);
                    546:       TtaAttachAttribute(el, minAtt, doc);
                    547:       TtaSetAttributeText(minAtt, text, el, doc);
1.22      kia       548:       if(registerUndo)
                    549:         TtaRegisterAttributeCreate(minAtt, el, doc);
1.1       vatton    550:     }
                    551: 
                    552:   if (!maxAtt)
                    553:     {  
                    554:       maxAtt = TtaNewAttribute(maxType);
                    555:       if (maxVal<INT_MAX)
                    556:         sprintf(text,"%d",maxVal);
                    557:       else
                    558:         sprintf(text,"*");
                    559:       TtaAttachAttribute(el, maxAtt, doc);      
                    560:       TtaSetAttributeText(maxAtt, text, el, doc);
1.22      kia       561:       if(registerUndo)
                    562:         TtaRegisterAttributeCreate(maxAtt, el, doc);
1.1       vatton    563:     }
                    564: 
                    565:   if (!curAtt)
1.2       quint     566:     {
1.1       vatton    567:       curAtt = TtaNewAttribute(curType);
                    568:       sprintf(text,"%d",curVal);
                    569:       TtaAttachAttribute(el, curAtt, doc);
                    570:       TtaSetAttributeText(curAtt, text, el, doc);
1.22      kia       571:       if(registerUndo)
                    572:         TtaRegisterAttributeCreate(curAtt, el, doc);
1.1       vatton    573:     }
                    574: 
                    575:   if (text)
                    576:     TtaFreeMemory(text);
                    577: 
                    578:   //We must have currentOccurs children
                    579:   Element  child, newChild;
                    580:   int      childrenCount;
                    581: 
                    582:   child = TtaGetFirstChild(el);
                    583:   if (!child)
                    584:     //Error : a repeat must have at least one child which will be the model
                    585:     return;
                    586:   
                    587:   for(childrenCount = 0; child; TtaNextSibling(&child))
                    588:     {
                    589:       //TODO : Check that every child is valid
                    590:       childrenCount ++;
                    591:     }
                    592: 
                    593:   if (childrenCount > maxVal)
                    594:     //Error : too many children!
                    595:     return;  
                    596: 
                    597:   child = TtaGetLastChild(el);
                    598: 
                    599:   while(childrenCount < curVal)
                    600:     {
                    601:       //Create a new child
                    602:       newChild = TtaCopyTree(child, doc, doc, el);
                    603:       TtaInsertSibling(newChild, child, FALSE, doc);
1.22      kia       604:       if(registerUndo)
                    605:         TtaRegisterElementCreate(newChild, doc);
1.1       vatton    606:       child = newChild;
                    607:       childrenCount++;
                    608:     }
                    609: #endif /* TEMPLATES */
                    610: }
                    611: 
                    612: /*----------------------------------------------------------------------
                    613:   ParseTemplate
1.3       vatton    614:   ----------------------------------------------------------------------*/
1.1       vatton    615: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                    616:                            ThotBool loading)
                    617: {
                    618: #ifdef TEMPLATES
                    619:        AttributeType attType;
1.5       vatton    620:        Attribute     att;
                    621:        Element       aux, child; //Needed when deleting trees
                    622:        char         *name;
                    623:        ElementType   elType = TtaGetElementType (el);
1.1       vatton    624:        
1.23    ! kia       625:   if(!t)
        !           626:     return;
        !           627:   
1.5       vatton    628:   name = TtaGetSSchemaName (elType.ElSSchema);
                    629:        if (!strcmp (name, "Template"))
1.1       vatton    630:     {
1.5       vatton    631:       switch(elType.ElTypeNum)
1.1       vatton    632:         {
                    633:         case Template_EL_head :
                    634:           //Remove it and all of its children
                    635:           TtaDeleteTree(el, doc);
                    636:           //We must stop searching into this tree
                    637:           return;
                    638:           break;
                    639:         case Template_EL_component :
                    640:           //Replace by a use                           
1.5       vatton    641:           attType.AttrSSchema = elType.ElSSchema;
1.1       vatton    642:           attType.AttrTypeNum = Template_ATTR_name;
                    643:           
1.18      kia       644:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);                                  
1.5       vatton    645:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.1       vatton    646:           if (NeedAMenu (el, doc))
1.5       vatton    647:             TtaChangeElementType (el, Template_EL_useEl);
1.1       vatton    648:           else
1.5       vatton    649:             TtaChangeElementType (el, Template_EL_useSimple);
1.1       vatton    650:           
                    651:           attType.AttrTypeNum = Template_ATTR_types;
1.5       vatton    652:           att = TtaNewAttribute (attType);
                    653:           TtaAttachAttribute (el, att, doc);
1.18      kia       654:           if(name)
                    655:             TtaSetAttributeText (att, name, el, doc);
1.1       vatton    656:           
                    657:           attType.AttrTypeNum = Template_ATTR_currentType;
1.5       vatton    658:           att = TtaNewAttribute (attType);
1.18      kia       659:           TtaAttachAttribute (el, att, doc);
                    660:           if(name)
                    661:             TtaSetAttributeText (att, name, el, doc);
1.1       vatton    662:           
                    663:           break;
                    664:         case Template_EL_option :
                    665:           aux = NULL;
                    666:           break;
                    667:         case Template_EL_bag :
                    668:           //Link to types
                    669:           //Allow editing the content
                    670:           break;
                    671:         case Template_EL_useEl :
                    672:         case Template_EL_useSimple :
                    673:           /* if this use element is not empty, don't do anything: it is
                    674:              supposed to contain a valid instance. This should be
                    675:              checked, though */
                    676:           if (!TtaGetFirstChild (el))
                    677:             InstantiateUse (t, el, doc, TRUE);
                    678:           break;
                    679:         case Template_EL_attribute :
                    680:           if (!loading)
                    681:             InstantiateAttribute (t, el, doc);
                    682:           break;
                    683:         case Template_EL_repeat :
1.22      kia       684:           InstantiateRepeat (t, el, doc, FALSE);
1.1       vatton    685:           break;
                    686:         default :
                    687:           break;
                    688:         }
                    689:     }
                    690:        
1.5       vatton    691:        child = TtaGetFirstChild (el);
                    692:        while (child)
1.1       vatton    693:     {
                    694:       aux = child;
1.5       vatton    695:       TtaNextSibling (&aux);
                    696:       ParseTemplate (t, child, doc, loading);
1.1       vatton    697:       child = aux;
                    698:     }
                    699: #endif /* TEMPLATES */
                    700: }
                    701: 
                    702: /*----------------------------------------------------------------------
1.3       vatton    703:   ----------------------------------------------------------------------*/
1.1       vatton    704: void DoInstanceTemplate (char *templatename)
                    705: {
                    706: #ifdef TEMPLATES
                    707:        XTigerTemplate  t;
                    708:        ElementType               elType;
                    709:        Element                     root, piElem, doctype, elFound, text;
                    710:        char                    *s, *charsetname = NULL, buffer[MAX_LENGTH];
                    711:   int             pi_type;
                    712:   Document        doc;
                    713: 
                    714:        //Instantiate all elements
1.8       kia       715:        t = (XTigerTemplate) Dictionary_Get (Templates_Dic, templatename);
1.23    ! kia       716:   if(!t)
        !           717:     return;
        !           718:   
1.1       vatton    719:   doc = GetTemplateDocument (t);
                    720:        root =  TtaGetMainRoot (doc);
                    721:        ParseTemplate (t, root, doc, FALSE);
                    722: 
                    723:   //Look for PIs
                    724:   /* check if the document has a DOCTYPE declaration */
                    725: #ifdef ANNOTATIONS
                    726:   if (DocumentTypes[doc]  == docAnnot)
                    727:     elType = TtaGetElementType (root);
                    728:   else
                    729: #endif /* ANNOTATIONS */
                    730:     elType = TtaGetElementType (root);
                    731:   s = TtaGetSSchemaName (elType.ElSSchema);
                    732:   if (strcmp (s, "HTML") == 0)
                    733:     {
                    734:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                    735:       pi_type = HTML_EL_XMLPI;
                    736:     }
                    737: #ifdef _SVG
                    738:   else if (strcmp (s, "SVG") == 0)
                    739:     {
                    740:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                    741:       pi_type = SVG_EL_XMLPI;
                    742:     }
                    743: #endif /* _SVG */
                    744:   else if (strcmp (s, "MathML") == 0)
                    745:     {
                    746:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                    747:       pi_type = MathML_EL_XMLPI;
                    748:     }
                    749:   else
                    750:     {
                    751:       elType.ElTypeNum = XML_EL_doctype;
                    752:       pi_type = XML_EL_xmlpi;
                    753:     }
                    754:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
                    755:   if (!doctype)
                    756:     {
                    757:       /* generate the XML declaration */
                    758:       /* Check the Thot abstract tree against the structure schema. */
                    759:       TtaSetStructureChecking (FALSE, doc);
                    760:       elType.ElTypeNum = pi_type;
                    761:       doctype = TtaNewTree (doc, elType, "");
                    762:       TtaInsertFirstChild (&doctype, root, doc);
                    763:       elFound = TtaGetFirstChild (doctype);
                    764:       text = TtaGetFirstChild (elFound);
                    765:       strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                    766:       charsetname = UpdateDocumentCharset (doc);
                    767:       strcat (buffer, charsetname);
                    768:       strcat (buffer, "\"");
                    769:       TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    770:       TtaSetStructureChecking (TRUE, doc);
                    771:     }
                    772:   
                    773:   /* generate the XTiger PI */
                    774:   /* Check the Thot abstract tree against the structure schema. */
                    775:   TtaSetStructureChecking (FALSE, doc);
                    776:   elType.ElTypeNum = pi_type;
                    777:   piElem = TtaNewTree (doc, elType, "");
                    778:   TtaInsertSibling(piElem, doctype, FALSE, doc);
                    779:   elFound = TtaGetFirstChild (piElem);
                    780:   text = TtaGetFirstChild (elFound);
                    781:   strcpy (buffer, "xtiger template=\"");
                    782:   strcat (buffer, templatename);
1.17      kia       783:   strcat (buffer, "\" version=\"");
1.20      vatton    784:   if (t->version)
                    785:     strcat (buffer, t->version);
                    786:   else
                    787:     strcat (buffer, "0.8");
1.1       vatton    788:   strcat (buffer, "\"");
1.17      kia       789:   if(t->templateVersion)
1.20      vatton    790:     {
                    791:       strcat (buffer, " templateVersion=\"");
                    792:       strcat (buffer, t->templateVersion);
                    793:       strcat (buffer, "\"");
                    794:     }
1.1       vatton    795:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    796:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton    797: 
                    798:   // update the document title
                    799:        if (!strcmp (s, "HTML"))
                    800:     {
                    801:       elType.ElTypeNum = HTML_EL_TITLE;
                    802:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                    803:       if (elFound)
                    804:         {
                    805:           elFound = TtaGetFirstChild (elFound);
                    806:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                    807:                              TtaGetDefaultLanguage (), doc);
                    808:         }
                    809:     }
1.1       vatton    810: #endif /* TEMPLATES */
                    811: }
                    812: 
                    813: /*----------------------------------------------------------------------
1.3       vatton    814:   PreInstantiateComponents: Instantiates all components in order to improve
                    815:   editing.
                    816:   ----------------------------------------------------------------------*/
1.1       vatton    817: void PreInstantiateComponents(XTigerTemplate t)
                    818: {
                    819: #ifdef TEMPLATES 
1.23    ! kia       820:   if(!t)
        !           821:     return;
        !           822: 
1.1       vatton    823:   DicDictionary components = GetComponents(t);
                    824:   Declaration comp;
                    825: 
1.8       kia       826:   for(Dictionary_First(components);!Dictionary_IsDone(components);Dictionary_Next(components))
1.1       vatton    827:     {
1.8       kia       828:       comp = (Declaration) Dictionary_CurrentElement(components);
1.1       vatton    829:       ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
                    830:     }
                    831: #endif /* TEMPLATES */
                    832: }

Webmaster