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

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

Webmaster