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

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

Webmaster