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

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

Webmaster