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

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

Webmaster