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

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

Webmaster