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

1.19      vatton      1: /*
                      2:  *
1.60      vatton      3:  *  COPYRIGHT INRIA and W3C, 2006-2008
1.19      vatton      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7: 
1.1       vatton      8: #include "templates.h"
                      9: 
                     10: #define THOT_EXPORT extern
                     11: #include "templateDeclarations.h"
                     12: 
1.8       kia        13: #include "Elemlist.h"
                     14: 
1.63      kia        15: #include "wxdialogapi_f.h"
1.1       vatton     16: #include "EDITimage_f.h"
                     17: #include "HTMLsave_f.h"
1.34      vatton     18: #include "HTMLtable_f.h"
1.49      vatton     19: #include "html2thot_f.h"
1.1       vatton     20: #include "init_f.h"
                     21: #include "templates_f.h"
                     22: #include "templateDeclarations_f.h"
                     23: #include "templateInstantiate_f.h"
                     24: #include "Templatebuilder_f.h"
1.63      kia        25: #include "templateLoad_f.h"
1.1       vatton     26: #include "templateUtils_f.h"
                     27: #include "fetchHTMLname_f.h"
                     28: #include "Template.h"
                     29: 
                     30: #ifdef TEMPLATES
                     31: #define TEMPLATE_SCHEMA_NAME "Template"
                     32: 
                     33: typedef struct _InstantiateCtxt
                     34: {
1.47      kia        35:   char         *templatePath;
                     36:   char         *instancePath;
                     37:   char         *schemaName;
1.41      vatton     38:   Document      doc;
1.47      kia        39:   DocumentType  docType;
                     40:   ThotBool      dontReplace;
1.1       vatton     41: } InstantiateCtxt;
                     42: #endif /* TEMPLATES */
                     43: 
                     44: 
1.49      vatton     45: 
                     46: /*----------------------------------------------------------------------
                     47:   Template_InsertRepeatChildAfter
                     48:   Insert a child to a xt:repeat
                     49:   The decl parameter must be valid and will not be verified. It must be a
                     50:     direct child element or the "use in the use" for union elements.
                     51:   @param el element (xt:repeat) in which insert a new element
                     52:   @param decl Template declaration of the element to insert
                     53:   @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
                     54:   @return The inserted element 
                     55:   ----------------------------------------------------------------------*/
                     56: Element Template_InsertRepeatChildAfter (Document doc, Element el,
                     57:                                          Declaration decl, Element elPrev)
                     58: {
                     59: #ifdef TEMPLATES
                     60:   Element     useFirst; /* First xt:use of the repeat.*/
                     61:   Element     use;      /* xt:use to insert.*/
                     62:   ElementType useType;  /* type of xt:use.*/
                     63: 
                     64:   if (!TtaGetDocumentAccessMode (doc))
                     65:     return NULL;
1.56      kia        66:   
1.49      vatton     67:   /* Copy xt:use with xt:types param */
                     68:   useFirst = TtaGetFirstChild (el);
                     69:   useType = TtaGetElementType (useFirst);
                     70:   use = TtaCopyElement (useFirst, doc, doc, el);
                     71: 
1.51      kia        72:   TtaChangeElementType(use, Template_EL_useSimple);
                     73: 
1.49      vatton     74:   /* insert it */
                     75:   if (elPrev)
                     76:     TtaInsertSibling(use, elPrev, FALSE, doc);
                     77:   else
                     78:     TtaInsertSibling(use, useFirst, TRUE, doc);
                     79:   Template_InsertUseChildren(doc, use, decl);
                     80: 
                     81:   TtaRegisterElementCreate (use, doc);
                     82:   return use;
                     83: #else /* TEMPLATES */
                     84:   return NULL;
                     85: #endif /* TEMPLATES */
                     86: }
                     87: 
                     88: /*----------------------------------------------------------------------
                     89:   Template_InsertRepeatChild
                     90:   Insert a child to a xt:repeat
                     91:   The decl parameter must be valid and will not be verified. It must be a
                     92:     direct child element or the "use in the use" for union elements.
                     93:   @param el element (repeat) in which insert a new element
                     94:   @param decl Template declaration of the element to insert
                     95:   @param pos Position of insertion (0 before all, 1 after first ... -1 after all)
                     96:   @return The inserted element
                     97:   ----------------------------------------------------------------------*/
                     98: Element Template_InsertRepeatChild (Document doc, Element el, Declaration decl, int pos)
                     99: {
                    100: #ifdef TEMPLATES
                    101:   if (!TtaGetDocumentAccessMode(doc) || !decl)
                    102:     return NULL;
                    103: 
                    104:   if (pos == 0)
                    105:     return Template_InsertRepeatChildAfter (doc, el, decl, NULL);
                    106:   else if (pos == -1)
                    107:     return Template_InsertRepeatChildAfter (doc, el, decl, TtaGetLastChild(el));
                    108:   else
                    109:   {
                    110:     Element elem = TtaGetFirstChild(el);
                    111:     pos--;
                    112:     while (pos > 0)
                    113:       {
                    114:         TtaNextSibling(&elem);
                    115:         pos--;
                    116:       }
                    117:     return Template_InsertRepeatChildAfter (doc, el, decl, elem);
                    118:   }
                    119: #else /* TEMPLATES */
                    120:   return NULL;
                    121: #endif /* TEMPLATES */
                    122: }
                    123: 
                    124: 
                    125: /*----------------------------------------------------------------------
                    126:   Template_InsertBagChild
                    127:   Insert a child to a xt:bag at the current insertion point.
                    128:   The decl parameter must be valid and will not be verified.
                    129:   @param el element (xt:bag) in which insert a new element
                    130:   @param decl Template declaration of the element to insert
                    131:   @return The inserted element
                    132:   ----------------------------------------------------------------------*/
1.53      kia       133: Element Template_InsertBagChild (Document doc, Element el, Declaration decl, ThotBool before)
1.49      vatton    134: {
                    135: #ifdef TEMPLATES
                    136:   Element     sel;
                    137:   ElementType newElType, selType;
                    138:   int start, end;
1.56      kia       139:   SSchema sstempl = TtaGetSSchema ("Template", doc);
1.49      vatton    140: 
                    141:   if (!TtaGetDocumentAccessMode (doc) || !decl)
                    142:     return NULL;
                    143: 
                    144:   TtaGiveFirstSelectedElement (doc, &sel, &start, &end);
                    145:   if (TtaIsAncestor (sel, el))
                    146:   {
1.56      kia       147:     
                    148:     switch(decl->nature)
                    149:     {
                    150:       case UnionNat:
                    151:         newElType.ElTypeNum = Template_EL_useEl;
                    152:         newElType.ElSSchema = sstempl;
                    153:         break;
                    154:       case ComponentNat:
                    155:         newElType.ElTypeNum = Template_EL_useSimple;
                    156:         newElType.ElSSchema = sstempl;
                    157:         break;
                    158:       case XmlElementNat:
                    159:         GIType (decl->name, &newElType, doc);
                    160:         break;
                    161:       default:
                    162:         break;
                    163:     }
1.49      vatton    164: 
                    165:     selType = TtaGetElementType (sel);
                    166:     if (decl->blockLevel && 
                    167:         (TtaIsLeaf (selType) || !IsTemplateElement (sel)))
                    168:       {
                    169:         // force the insertion of a block level element at the right position
                    170:         while (sel && IsCharacterLevelElement (sel))
                    171:           sel = TtaGetParent (sel);
                    172:         if (sel)
                    173:           TtaSelectElement (doc, sel);
1.53      kia       174:         TtaInsertAnyElement (doc, before);
1.57      vatton    175:         TtaExtendUndoSequence (doc);
1.49      vatton    176:       }
                    177:     TtaInsertElement (newElType, doc);
                    178:     TtaGiveFirstSelectedElement (doc, &sel, &start, &end);
1.56      kia       179:     if (sel && newElType.ElSSchema == sstempl)
1.49      vatton    180:       {
1.57      vatton    181:         selType = TtaGetElementType (sel);
                    182:         TtaUnselect (doc);
                    183:         
                    184:         if (selType.ElSSchema == newElType.ElSSchema &&
                    185:             selType.ElTypeNum == Template_EL_useSimple)
                    186:           {
                    187:             SetAttributeStringValueWithUndo (sel, Template_ATTR_types, decl->name);
                    188:             SetAttributeStringValueWithUndo (sel, Template_ATTR_title, decl->name);
                    189:             Template_InsertUseChildren (doc, sel, decl);
                    190:           }
                    191:       }   
                    192:     return sel;
1.49      vatton    193:   }
                    194: #endif /* TEMPLATES */
                    195:   return NULL;
                    196: }
                    197: 
1.63      kia       198: 
                    199: /*----------------------------------------------------------------------
                    200:   CreateTemplate
                    201:   Create a template from any document.
                    202:   ----------------------------------------------------------------------*/
                    203: void CreateTemplate(Document doc, char *templatePath)
                    204: {
                    205: #ifdef TEMPLATES
                    206:   Document          newdoc = 0;
                    207:   Element           root, head, elem, xt, title, child, last;
                    208:   ElementType       elType, xtType;
1.64      kia       209:   char             *s;
                    210:   ThotBool          mathPI;
1.63      kia       211:   SSchema           templSchema;
                    212:   XTigerTemplate    t;
                    213:   
                    214:   if(IsTemplateInstanceDocument(doc))
                    215:     {
                    216:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
                    217:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    218:       return;
                    219:     }
                    220: 
                    221:   if(IsTemplateDocument(doc))
                    222:     {
                    223:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
                    224:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    225:       return;
                    226:     }
                    227:   
1.64      kia       228:   root = TtaGetRootElement(doc);
                    229:   elType = TtaGetElementType (root);
                    230:   // get the target document type
                    231:   s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       232:   
1.64      kia       233:   TtaNewNature (doc, elType.ElSSchema,  NULL, "Template", "TemplateP");
                    234:   TtaSetANamespaceDeclaration(doc, root, "xt", Template_URI);
                    235:   templSchema = TtaGetSSchema("Template", doc);
                    236:   TtaSetUriSSchema(templSchema, Template_URI);
                    237: 
                    238:   // Insert xt:head and others
                    239:   TtaSetStructureChecking (FALSE, doc);
                    240:   if (strcmp (s, "HTML") == 0)
1.63      kia       241:     {
1.64      kia       242:       // Initialize the xt:head
                    243:       elType.ElTypeNum = HTML_EL_HEAD;
                    244:       head = TtaSearchTypedElement (elType, SearchInTree, root);
                    245:       if(head)
                    246:         {
                    247:           xtType.ElSSchema = templSchema;
                    248:           xtType.ElTypeNum = Template_EL_head;
                    249:           xt = TtaNewElement(doc, xtType);
                    250:           
                    251:           elem = TtaGetLastChild(head);
                    252:           if(elem)
                    253:               TtaInsertSibling(xt, elem, FALSE, doc);
                    254:           else
                    255:               TtaInsertFirstChild(&xt, head, doc);
                    256:           
                    257:           SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    258:           SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
                    259:         }
1.63      kia       260:       
1.64      kia       261:       // Initialize the document title
                    262:       elType.ElTypeNum = HTML_EL_TITLE;
                    263:       title = TtaSearchTypedElement (elType, SearchInTree, root);
                    264:       if(title)
1.63      kia       265:         {
1.64      kia       266:           // Create xt:use for title
                    267:           xtType.ElTypeNum = Template_EL_useSimple;
                    268:           xt = TtaNewElement(doc, xtType);
                    269:           TtaInsertFirstChild(&xt, title, doc);
                    270:           SetAttributeStringValue(xt, Template_ATTR_types, "string");
                    271:           SetAttributeStringValue(xt, Template_ATTR_title, "title");
                    272:           
                    273:           // Move current title content to xt:use
                    274:           last = NULL;
                    275:           while(child = TtaGetLastChild(title), child!=NULL)
1.63      kia       276:             {
1.64      kia       277:               if(child==xt)
                    278:                 break;
                    279:               TtaRemoveTree(child, doc);
                    280:               if(last)
                    281:                 TtaInsertSibling(child, last, FALSE, doc);
1.63      kia       282:               else
1.64      kia       283:                 TtaInsertFirstChild(&child, xt, doc);
                    284:               last = child;
1.63      kia       285:             }
                    286:         }
1.64      kia       287:     }
                    288:   else
                    289:     {
                    290:       xtType.ElSSchema = templSchema;
                    291:       xtType.ElTypeNum = Template_EL_head;
                    292:       xt = TtaNewElement(doc, xtType);
                    293:       TtaInsertFirstChild(&xt, root, doc);
                    294:       SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    295:       SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");      
                    296:     }
                    297:   // Save changes
                    298:   TtaSetStructureChecking (TRUE, doc);
                    299:   
                    300:   // Save document
                    301:   TtaGetEnvBoolean ("GENERATE_MATHPI", &mathPI);
                    302:   TtaSetEnvBoolean("GENERATE_MATHPI", TRUE, TRUE);
                    303:   SaveDocumentToNewDoc(doc, newdoc, templatePath, NULL);
                    304:   TtaSetEnvBoolean("GENERATE_MATHPI", mathPI, TRUE);
                    305:   
                    306:   TtaClearUndoHistory (doc);
                    307:   RemoveParsingErrors (doc);
1.63      kia       308: 
1.64      kia       309:   TtaFreeMemory(DocumentURLs[doc]);
                    310:   DocumentURLs[doc] = TtaStrdup(templatePath);
                    311:   
                    312:   if(DocumentMeta[doc]==NULL)
                    313:     DocumentMeta[doc] = DocumentMetaDataAlloc();
                    314:   
                    315:   DocumentMeta[doc]->method = CE_TEMPLATE;
                    316:   if(DocumentMeta[doc]->initial_url)
                    317:     {
                    318:       TtaFreeMemory(DocumentMeta[doc]->initial_url);
                    319:       DocumentMeta[doc]->initial_url = NULL;
                    320:     }
                    321:   TtaSetDocumentModified (doc);
1.63      kia       322: 
1.64      kia       323:   // Load template-related infos :
                    324:   // like LoadTemplate(..)
                    325:   t = LookForXTigerTemplate(templatePath);
                    326:   t->doc = doc;
                    327:   Template_PrepareTemplate(t);
1.67      kia       328:   //  DocumentTypes[doc] = docTemplate;
                    329:   t->state |= templloaded|templTemplate;
1.63      kia       330: 
                    331: #ifdef AMAYA_DEBUG  
1.64      kia       332:     DumpAllDeclarations();
1.63      kia       333: #endif /* AMAYA_DEBUG */
1.64      kia       334:     
                    335:   /* Update the URL combo box */
                    336:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    337:   TtaSetTextZone (doc, 1, URL_list);
                    338:   /* Update template menus */
                    339:   UpdateTemplateMenus(doc);
                    340: 
1.63      kia       341: #endif /* TEMPLATES */  
                    342: }
                    343: 
1.1       vatton    344: /*----------------------------------------------------------------------
                    345:   CreateInstance
1.41      vatton    346:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    347:   ----------------------------------------------------------------------*/
1.63      kia       348: void CreateInstance(char *templatePath, char *instancePath, int basedoc)
1.3       vatton    349: {
1.1       vatton    350: #ifdef TEMPLATES
1.41      vatton    351:   Document          doc = 0, newdoc = 0;
                    352:   ElementType       elType;
                    353:   Element           root, title, text;
1.54      vatton    354:   CHARSET           charset;
                    355:   char             *localFile, *s, *charsetname;
1.63      kia       356:   ThotBool          changes = FALSE;
1.1       vatton    357: 
1.35      kia       358:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    359:   if (t == NULL)
1.29      vatton    360:     {
                    361:       // the template cannot be loaded
                    362:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    363:       return;
                    364:     }
1.41      vatton    365:   // the template document
1.5       vatton    366:   doc = GetTemplateDocument (t);
1.41      vatton    367:   // localize the new created document
                    368:   if (DontReplaceOldDoc)
                    369:     newdoc = TtaGetNextDocumentIndex ();
                    370:   else
                    371:     newdoc = basedoc;
1.54      vatton    372: 
1.58      vatton    373:   if (!TtaHasUndoSequence (doc))
1.1       vatton    374:     {
1.65      vatton    375:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.14      vatton    376:       root = TtaGetRootElement(doc);
                    377:       elType = TtaGetElementType (root);
                    378:       // get the target document type
                    379:       s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       380:       // Do special stuff for HTML documents
1.14      vatton    381:       if (strcmp (s, "HTML") == 0)
1.1       vatton    382:         {
1.14      vatton    383:           // Initialize the document title
                    384:           elType.ElTypeNum = HTML_EL_TITLE;
                    385:           title = TtaSearchTypedElement (elType, SearchInTree, root);
                    386:           text = TtaGetFirstChild (title);
                    387:           while (text)
                    388:             {
                    389:               elType = TtaGetElementType (text);
                    390:               if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                    391:                 {
                    392:                   TtaSetTextContent (text, (unsigned char*)Answer_text,
                    393:                                      TtaGetDefaultLanguage (), doc);
                    394:                   text = NULL;
                    395:                 }
                    396:               else if ((elType.ElTypeNum == Template_EL_useEl ||
                    397:                         elType.ElTypeNum == Template_EL_useSimple) &&
                    398:                        !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    399:                 // Ignore the template use element
                    400:                 text = TtaGetFirstChild (text);
                    401:               else
                    402:                 // Look for the first text child
                    403:                 TtaNextSibling (&text);
                    404:             }
1.63      kia       405:         }
1.54      vatton    406: 
1.65      vatton    407:       // update the charset if needed
                    408:       charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    409:       charset = TtaGetCharset (charsetname);
                    410:       if (charset != UNDEFINED_CHARSET &&
                    411:           DocumentMeta[doc]->charset &&
                    412:           strcmp (charsetname, DocumentMeta[doc]->charset))
                    413:         {
                    414:           TtaSetDocumentCharset (doc, charset, FALSE);
                    415:           DocumentMeta[doc]->charset = TtaStrdup (charsetname);
                    416:           SetNamespacesAndDTD (doc);
                    417:         }
                    418:       // Save HTML special changes
                    419:       TtaCloseUndoSequence (doc);
                    420:       changes = TRUE;
                    421: 
1.63      kia       422:       // Save document
                    423:       SaveDocumentToNewDoc(doc, newdoc, instancePath, &localFile);
                    424: 
                    425:       // Revert HTML special changes
                    426:       if(changes)
                    427:         TtaUndoNoRedo (doc);
                    428:       
                    429:       TtaFreeMemory(localFile);
1.47      kia       430: 
1.1       vatton    431:       TtaClearUndoHistory (doc);
1.41      vatton    432:       RemoveParsingErrors (doc);
                    433:       GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    434:                    !DontReplaceOldDoc, NULL, NULL);
                    435:       TtaSetDocumentModified (newdoc);
1.64      kia       436:       UpdateTemplateMenus(newdoc);
1.1       vatton    437:     }
                    438: #endif /* TEMPLATES */
                    439: }
                    440: 
                    441: /*----------------------------------------------------------------------
1.3       vatton    442:   ----------------------------------------------------------------------*/
1.41      vatton    443: void InstantiateTemplate_callback (int newdoc, int status,  char *urlName,
                    444:                                    char *outputfile,
                    445:                                    char *proxyName, AHTHeaders *http_headers,
                    446:                                    void * context)
1.1       vatton    447: {
                    448: #ifdef TEMPLATES
1.47      kia       449:   InstantiateCtxt *ctx = (InstantiateCtxt*)context;
1.1       vatton    450: 
1.47      kia       451:   DoInstanceTemplate (ctx->templatePath);
1.41      vatton    452:   CreateInstance (ctx->templatePath, ctx->instancePath, ctx->doc);
1.1       vatton    453:   TtaFreeMemory (ctx->templatePath);
                    454:   TtaFreeMemory (ctx->instancePath);
                    455:   TtaFreeMemory (ctx);
                    456: #endif /* TEMPLATES */
                    457: }
                    458: 
1.67      kia       459: ///*----------------------------------------------------------------------
                    460: //  ----------------------------------------------------------------------*/
                    461: //void InstantiateTemplate (Document doc, char *templatename, char *docname,
                    462: //                          DocumentType docType, ThotBool loaded)
                    463: //{
                    464: //#ifdef TEMPLATES
                    465: //  if (!loaded)
                    466: //    {
                    467: //      // Create the callback context
                    468: //      InstantiateCtxt *ctx = (InstantiateCtxt *)TtaGetMemory (sizeof (InstantiateCtxt));
                    469: //      ctx->templatePath      = TtaStrdup (templatename);
                    470: //      ctx->instancePath      = TtaStrdup (docname);
                    471: //      ctx->schemaName = GetSchemaFromDocType(docType);
                    472: //      ctx->doc = doc;
                    473: //      ctx->docType = docType;
                    474: //
                    475: //      GetAmayaDoc (templatename, NULL, doc, doc, CE_MAKEBOOK, FALSE, 
                    476: //                   (void (*)(int, int, char*, char*, char*,
                    477: //                             const AHTHeaders*, void*)) InstantiateTemplate_callback,
                    478: //                   (void *) ctx);
                    479: //    }
                    480: //  else
                    481: //    {
                    482: //      DoInstanceTemplate (templatename);
                    483: //      CreateInstance (templatename, docname, doc);
                    484: //    }  
                    485: //#endif /* TEMPLATES */
                    486: //}
1.1       vatton    487: 
                    488: /*----------------------------------------------------------------------
                    489:   InstantiateAttribute
1.3       vatton    490:   ----------------------------------------------------------------------*/
1.1       vatton    491: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
                    492: {
                    493: #ifdef TEMPLATES
                    494:   AttributeType  useType, nameType, defaultType, attrType;
                    495:   Attribute      useAttr, nameAttr, defAttr, attr;
                    496:   ElementType    elType;
                    497:   Element        parent;
                    498:   char           *text, *elementName;
                    499:   ThotBool       level;
                    500:   NotifyAttribute event;
1.48      kia       501:   int             val;
1.1       vatton    502: 
                    503:   parent = TtaGetParent (el);
                    504:   if (!parent)
                    505:     return;
                    506:   // if attribute "use" has value "optional", don't do anything
                    507:   useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    508:   useType.AttrTypeNum = Template_ATTR_useAt;
                    509:   useAttr = TtaGetAttribute (el, useType);
                    510:   if (useAttr)
                    511:     // there is a "use" attribute. Check its value
                    512:     {
1.48      kia       513:       val = TtaGetAttributeValue(useAttr);
                    514:       if (val == Template_ATTR_useAt_VAL_optional)
1.18      kia       515:       {
                    516:         return;
                    517:       }
1.1       vatton    518:     }
1.18      kia       519:     
1.1       vatton    520:   // get the "name" and "default" attributes
                    521:   nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.9       vatton    522:   nameType.AttrTypeNum = Template_ATTR_ref_name;
1.1       vatton    523:   defaultType.AttrTypeNum = Template_ATTR_defaultAt;
                    524:   nameAttr = TtaGetAttribute (el, nameType);
                    525:   defAttr = TtaGetAttribute (el, defaultType);
                    526:   if (nameAttr)
                    527:     {
1.10      kia       528:       text = GetAttributeStringValue (el, nameAttr, NULL);
1.1       vatton    529:       if (text)
                    530:         {
                    531:           elType = TtaGetElementType (parent);
                    532:           elementName = TtaGetElementTypeName (elType);
                    533:           level = TRUE;
                    534:           MapHTMLAttribute (text, &attrType, elementName, &level, doc);
                    535:           TtaFreeMemory(text);
                    536:           attr = TtaNewAttribute (attrType);
                    537:           if (attr)
                    538:             {
                    539:               TtaAttachAttribute (parent, attr, doc);
                    540:               if (defAttr)
                    541:                 {
1.10      kia       542:                   text = GetAttributeStringValue (el, defAttr, NULL);
1.25      vatton    543:                   if (text)
1.18      kia       544:                     TtaSetAttributeText(attr, text, parent, doc);
1.1       vatton    545:                   TtaFreeMemory(text);
                    546:                   // if it's a src arttribute for an image, load the image
                    547:                   if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
                    548:                       elType.ElTypeNum == HTML_EL_IMG)
                    549:                     if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
                    550:                         attrType.AttrSSchema == elType.ElSSchema)
                    551:                       {
                    552:                         event.document = doc;
                    553:                         event.element = parent;
                    554:                         event.attribute = attr;
                    555:                         SRCattrModified (&event);
                    556:                       }
                    557:                 }
                    558:             }
                    559:         }
                    560:     }
                    561: #endif /* TEMPLATES */
                    562: }
                    563: 
                    564: #ifdef TEMPLATES
                    565: /*----------------------------------------------------------------------
                    566:   ProcessAttr
1.9       vatton    567:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    568:   ----------------------------------------------------------------------*/
1.1       vatton    569: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    570: {
                    571:   Element      child;
                    572:   ElementType  elType;
                    573: 
                    574:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    575:     {
                    576:       elType = TtaGetElementType (child);
                    577:       if (elType.ElTypeNum == Template_EL_attribute &&
                    578:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    579:         InstantiateAttribute (t, child, doc);
                    580:       else
                    581:         ProcessAttr (t, child, doc);
                    582:     }
                    583: }
                    584: #endif /* TEMPLATES */
                    585: 
1.12      kia       586: 
                    587: /*----------------------------------------------------------------------
                    588:   Template_GetNewSimpleTypeInstance
                    589:   Create an new instance of xt:use/SimpleType
                    590:   The decl attribute must embed SimpleType declaration (no validation).
                    591:   @param decl Declaration of new element
                    592:   @param parent Future parent element
                    593:   @param doc Document
                    594:   @return The new element
                    595:   ----------------------------------------------------------------------*/
                    596: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl)
                    597: {
                    598:   Element           newEl = NULL;
                    599: #ifdef TEMPLATES
                    600:   ElementType       elType;
                    601:   char             *empty = " ";
1.24      vatton    602: 
1.38      vatton    603:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       604:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    605:   newEl = TtaNewElement (doc, elType);
                    606:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    607: #endif /* TEMPLATES */
1.12      kia       608:   return newEl;
                    609: }
                    610: 
                    611: /*----------------------------------------------------------------------
                    612:   Template_GetNewXmlElementInstance
                    613:   Create an new instance of xt:use/XmlElement
                    614:   The decl attribute must embed XmlElement declaration (no validation).
                    615:   @param decl Declaration of new element
                    616:   @param parent Future parent element
                    617:   @param doc Document
                    618:   @return The new element
                    619:   ----------------------------------------------------------------------*/
                    620: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
                    621: {
                    622:   Element           newEl = NULL;
                    623: #ifdef TEMPLATES
                    624:   ElementType       elType;
                    625: 
1.24      vatton    626:   GIType (decl->name, &elType, doc);
                    627:   if (elType.ElTypeNum != 0)
1.12      kia       628:   {
1.23      kia       629:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       630:   }
                    631: #endif /* TEMPLATES */
                    632:   return newEl;
                    633: }
                    634: 
1.34      vatton    635: 
                    636: /*----------------------------------------------------------------------
                    637:   InsertWithNotify applies pre and post functions when inserting the new
                    638:   element el after child (if not NULL) or as first child of parent.
                    639:   ----------------------------------------------------------------------*/
                    640: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    641: {
                    642:   ElementType      elType;
                    643:   NotifyElement    event;
                    644:   char            *name;
                    645:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    646:   ThotBool         isImage = FALSE;
                    647:   ThotBool         oldStructureChecking;
                    648: 
                    649:   // avoid to check attributes now
                    650:   oldStructureChecking = TtaGetStructureChecking (doc);
                    651:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    652: 
                    653:   elType = TtaGetElementType (el);
                    654:   name = TtaGetSSchemaName (elType.ElSSchema);
                    655:   isCell = ((!strcmp (name,"HTML") &&
                    656:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    657:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    658:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    659:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    660:            (!strcmp (name,"MathML") &&
                    661:             (elType.ElTypeNum == MathML_EL_MTR ||
                    662:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    663:   isImage = (!strcmp (name,"HTML") && 
                    664:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    665:   if (child)
                    666:     TtaInsertSibling (el, child, FALSE, doc);
                    667:   else
                    668:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    669:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    670: 
1.50      vatton    671:   if (isImage)
                    672:     InsertImageOrObject (el, doc);
                    673:   else if (isCell)
1.34      vatton    674:     {
                    675:       // a cell is created
1.39      quint     676:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    677:     }
                    678:   else if (isRow)
                    679:     {
                    680:       // a row is created
                    681:       event.element = el;
                    682:       event.document = doc;
                    683:       RowPasted (&event);
                    684:     }
1.50      vatton    685:   
                    686:   if (!strcmp (name,"HTML"))
                    687:     {
                    688:       elType.ElTypeNum = HTML_EL_IMG;
                    689:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    690:       while (child)
                    691:         {
                    692:           InsertImageOrObject (child, doc);
                    693:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    694:         }
                    695:       elType.ElTypeNum = HTML_EL_Object;
                    696:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    697:       while (child)
                    698:         {
                    699:           InsertImageOrObject (child, doc);
                    700:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    701:         }
                    702:     }
1.34      vatton    703:   return el;
                    704: }
                    705: 
                    706: 
1.12      kia       707: /*----------------------------------------------------------------------
1.16      kia       708:   Template_InsertUseChildren
                    709:   Insert children to a xt:use
                    710:   The dec parameter must be valid and will not be verified. It must be a
                    711:     direct child element (for union elements).
                    712:   @param el element (xt:use) in which insert a new element
                    713:   @param dec Template declaration of the element to insert
                    714:   @return The inserted element (the xt:use element if insertion is multiple as component)
                    715:   ----------------------------------------------------------------------*/
                    716: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    717: {
1.34      vatton    718:   Element     newEl = NULL;
1.16      kia       719: #ifdef TEMPLATES
1.19      vatton    720:   Element     current = NULL;
1.34      vatton    721:   Element     child = NULL;
1.19      vatton    722:   //char       *attrCurrentTypeValue;
                    723:   //ElementType elType;
1.17      kia       724:   
1.25      vatton    725:   if (TtaGetDocumentAccessMode(doc))
1.16      kia       726:   {
1.23      kia       727:     switch (dec->nature)
                    728:     {
                    729:       case SimpleTypeNat:
                    730:         newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
1.34      vatton    731:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       732:         break;
                    733:       case XmlElementNat:
                    734:         newEl = Template_GetNewXmlElementInstance(doc, el, dec);
1.34      vatton    735:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       736:         break;
                    737:       case ComponentNat:
                    738:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.35      kia       739:         ProcessAttr (dec->usedIn, newEl, doc);        
1.23      kia       740:         /* Copy elements from new use to existing use. */
1.34      vatton    741:         while ((child = TtaGetFirstChild(newEl)))
1.23      kia       742:         {
                    743:           TtaRemoveTree (child, doc);
1.34      vatton    744:           child = InsertWithNotify (child, current, el, doc);
1.23      kia       745:           current = child; 
                    746:         }
                    747:         
                    748:         /* Copy currentType attribute. */
                    749:         //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL);
                    750:         //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue);
                    751:         TtaDeleteTree(newEl, doc);
                    752:         newEl = el;
                    753:         break;
                    754:       default :
                    755:         //Impossible
                    756:         break;   
                    757:     }
1.44      kia       758:     Template_FixAccessRight (dec->usedIn, el, doc);
                    759:     TtaUpdateAccessRightInViews (doc, el);    
1.23      kia       760:   }  
1.16      kia       761: #endif /* TEMPLATES */
                    762:   return newEl;
                    763: }
                    764: 
1.40      kia       765: 
                    766: /*----------------------------------------------------------------------
                    767:   Fix access rights.
                    768:   ----------------------------------------------------------------------*/
1.41      vatton    769: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia       770: {
                    771: #ifdef TEMPLATES
                    772:   ElementType elType;
                    773:   Element     child;
                    774:   char        currentType[MAX_LENGTH];
                    775:   Declaration decl;
                    776:   
                    777:   if (t && el && doc)
                    778:     {
                    779:       elType = TtaGetElementType(el);
1.41      vatton    780:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia       781:         {
1.41      vatton    782:           switch (elType.ElTypeNum)
1.40      kia       783:             {
                    784:             case Template_EL_TEXT_UNIT:
1.41      vatton    785:               //TtaSetAccessRight( el, ReadWrite, doc);
                    786:               return;
1.40      kia       787:             case Template_EL_useEl:
                    788:             case Template_EL_useSimple:
                    789:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                    790:                                               (char*)currentType, NULL);
                    791:               decl = Template_GetDeclaration(t, currentType);
                    792:               if (decl)
                    793:                 {
                    794:                   switch (decl->nature)
                    795:                     {
                    796:                       case SimpleTypeNat:
                    797:                       case XmlElementNat:
1.41      vatton    798:                         TtaSetAccessRight (el, ReadWrite, doc);
                    799:                         return;
1.40      kia       800:                       default:
1.41      vatton    801:                         TtaSetAccessRight (el, ReadOnly, doc);
                    802:                          break;
1.40      kia       803:                     }
                    804:                 }
                    805:               break;
                    806:             case Template_EL_bag:
1.45      vatton    807:             case Template_EL_repeat:
1.40      kia       808:               TtaSetAccessRight(el, ReadWrite, doc);
                    809:               break;
                    810:             default:
                    811:               TtaSetAccessRight(el, ReadOnly, doc);
                    812:               break;
                    813:             }
                    814:         }
                    815: 
1.41      vatton    816:       child = TtaGetFirstChild (el);
                    817:       // fix access right to children
                    818:       while (child)
1.40      kia       819:         {
1.41      vatton    820:           Template_FixAccessRight (t, child, doc);
                    821:           TtaNextSibling (&child);
1.40      kia       822:         }
                    823:     }
                    824: #endif /* TEMPLATES */
                    825: }
                    826: 
1.16      kia       827: /*----------------------------------------------------------------------
1.46      vatton    828:   AddPromptIndicator
                    829:   ----------------------------------------------------------------------*/
                    830: void AddPromptIndicator (Element el, Document doc)
                    831: {
                    832: #ifdef TEMPLATES
                    833:   ElementType         elType;
                    834:   AttributeType       attrType;
                    835:   Attribute           att;
                    836: 
1.66      vatton    837:   if (el)
                    838:     {
                    839:       elType = TtaGetElementType (el);
                    840:       attrType.AttrSSchema = elType.ElSSchema;
                    841:       attrType.AttrTypeNum = Template_ATTR_prompt;
1.69    ! vatton    842:       att = TtaGetAttribute (el, attrType);
        !           843:       if (att == NULL)
        !           844:         {
        !           845:           att = TtaNewAttribute (attrType);
        !           846:           TtaAttachAttribute (el, att, doc);
        !           847:         }
1.66      vatton    848:     }
1.46      vatton    849: #endif /* TEMPLATES */
                    850: }
                    851: 
                    852: /*----------------------------------------------------------------------
1.1       vatton    853:   InstantiateUse
1.3       vatton    854:   ----------------------------------------------------------------------*/
1.1       vatton    855: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.27      kia       856:                         ThotBool registerUndo)
1.1       vatton    857: {
                    858: #ifdef TEMPLATES
1.47      kia       859:   Element          cont = NULL;
1.1       vatton    860:   ElementType      elType;
                    861:   Declaration      dec;
1.28      kia       862:   int              size, nbitems, i;
1.1       vatton    863:   struct menuType  *items;
1.33      vatton    864:   char             *types, *text = NULL;
1.1       vatton    865:   ThotBool          oldStructureChecking;
                    866: 
1.25      vatton    867:   if (!t)
1.23      kia       868:     return NULL;
                    869: 
1.1       vatton    870:   /* get the value of the "types" attribute */
                    871:   cont = NULL;
1.12      kia       872:   elType = TtaGetElementType (el);
1.46      vatton    873:   AddPromptIndicator (el, doc);
1.32      vatton    874:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton    875:   if (!types || types[0] == EOS)
                    876:     {
                    877:       TtaFreeMemory (types);
                    878:       return NULL;
                    879:     }
1.1       vatton    880:   giveItems (types, size, &items, &nbitems);
                    881:   // No structure checking
                    882:   oldStructureChecking = TtaGetStructureChecking (doc);
                    883:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       884:   
1.1       vatton    885:   if (nbitems == 1)
                    886:     /* only one type in the "types" attribute */
                    887:     {
1.17      kia       888:       dec = Template_GetDeclaration (t, items[0].label);
1.1       vatton    889:       if (dec)
1.27      kia       890:       {
1.46      vatton    891:         cont = Template_InsertUseChildren (doc, el, dec);
1.32      vatton    892:         if (cont)
1.27      kia       893:         {
                    894:           TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1.32      vatton    895:           if (registerUndo)
                    896:             TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.27      kia       897:         }
                    898:       }
1.1       vatton    899:     }
1.33      vatton    900:   TtaFreeMemory (text);
1.32      vatton    901:   TtaFreeMemory (types);
1.28      kia       902:   
1.32      vatton    903:   for (i = 0; i < nbitems; i++)
1.28      kia       904:     TtaFreeMemory(items[i].label);
1.18      kia       905:   TtaFreeMemory(items);
1.1       vatton    906:   TtaSetStructureChecking (oldStructureChecking, doc);
1.40      kia       907:   
1.44      kia       908:   Template_FixAccessRight (t, el, doc);
                    909:   TtaUpdateAccessRightInViews (doc, el);
                    910:   
1.1       vatton    911:   return cont;
1.23      kia       912: #else /* TEMPLATES */
                    913:   return NULL;
1.1       vatton    914: #endif /* TEMPLATES */
                    915: }
                    916: 
                    917: /*----------------------------------------------------------------------
1.22      kia       918:   InstantiateRepeat
                    919:   Check for min and max param and validate xt:repeat element content.
                    920:   @param registerUndo True to register undo creation sequences.
1.3       vatton    921:   ----------------------------------------------------------------------*/
1.46      vatton    922: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
                    923:                         ThotBool registerUndo)
1.1       vatton    924: {
                    925: #ifdef TEMPLATES
1.32      vatton    926:   Element        child, newChild;
1.52      vatton    927:   ElementType    newElType;
                    928:   Attribute      minAtt,  maxAtt;
                    929:   AttributeType  minType, maxType;
                    930:   char          *text, *types = NULL, *title = NULL;
                    931:   int            curVal, minVal,  maxVal;
1.32      vatton    932:   int            childrenCount;
                    933: 
1.1       vatton    934: 
1.25      vatton    935:   if (!t)
1.23      kia       936:     return;
                    937: 
1.1       vatton    938:   //Preparing types
1.52      vatton    939:   minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1       vatton    940:   minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52      vatton    941:   maxType.AttrSSchema =  minType.AttrSSchema;
1.1       vatton    942:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52      vatton    943:   newElType.ElSSchema = minType.AttrSSchema;
                    944:   //Get minOccurs and maxOccurs attributes
1.1       vatton    945:   minAtt = TtaGetAttribute (el, minType);
                    946:   maxAtt = TtaGetAttribute (el, maxType);
                    947:   //Get the values
                    948:   if (minAtt)
                    949:     {
1.10      kia       950:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    951:       if (text)
                    952:         {
                    953:           minVal = atoi(text);
                    954:           TtaFreeMemory(text);
1.52      vatton    955:           curVal = minVal;
1.1       vatton    956:         }
                    957:       else
                    958:         //Error : Attribute with no value
                    959:         return;
                    960:     }
                    961:   else
1.52      vatton    962:     {
                    963:       minVal = 0;
                    964:       curVal = 1;
                    965:     }
1.1       vatton    966: 
                    967:   if (maxAtt)
                    968:     {
1.10      kia       969:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton    970:       if (text)
                    971:         {
                    972:           if (!strcmp (text, "*"))
                    973:             maxVal = INT_MAX;
                    974:           else
                    975:             maxVal = atoi (text);
                    976:           TtaFreeMemory (text);
                    977:         }
                    978:       else
                    979:         //Error : Attribute with no value
                    980:         return;
                    981:     }
                    982:   else
                    983:     maxVal = INT_MAX;
                    984: 
                    985:   text = (char*)TtaGetMemory(MAX_LENGTH);
1.52      vatton    986:   //Create non existing min max attributes
                    987:   if (minAtt == NULL)
1.1       vatton    988:     {      
1.32      vatton    989:       minAtt = TtaNewAttribute (minType);
                    990:       sprintf (text, "%d", minVal);
                    991:       TtaAttachAttribute (el, minAtt, doc);
                    992:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton    993:       if (registerUndo)
1.32      vatton    994:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton    995:     }
                    996: 
1.52      vatton    997:   if (maxAtt == NULL)
1.1       vatton    998:     {  
1.32      vatton    999:       maxAtt = TtaNewAttribute (maxType);
                   1000:       if (maxVal < INT_MAX)
                   1001:         sprintf(text, "%d", maxVal);
1.1       vatton   1002:       else
1.32      vatton   1003:         sprintf (text, "*");
                   1004:       TtaAttachAttribute (el, maxAtt, doc);      
                   1005:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1006:       if (registerUndo)
1.32      vatton   1007:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton   1008:     }
1.52      vatton   1009:   TtaFreeMemory(text);
1.1       vatton   1010: 
1.52      vatton   1011:   //We must have minOccurs children
1.1       vatton   1012:   child = TtaGetFirstChild(el);
                   1013:   if (!child)
                   1014:     //Error : a repeat must have at least one child which will be the model
                   1015:     return;
                   1016:   
                   1017:   for(childrenCount = 0; child; TtaNextSibling(&child))
                   1018:     {
                   1019:       //TODO : Check that every child is valid
                   1020:       childrenCount ++;
                   1021:     }
                   1022: 
                   1023:   if (childrenCount > maxVal)
                   1024:     //Error : too many children!
                   1025:     return;  
                   1026: 
                   1027:   child = TtaGetLastChild(el);
1.32      vatton   1028:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1029:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1030:   newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton   1031:   while (childrenCount < curVal)
1.1       vatton   1032:     {
1.32      vatton   1033:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1034:       // Insert it
1.32      vatton   1035:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1036:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1037:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.34      vatton   1038:       InstantiateUse (t, newChild, doc, TRUE);
1.27      kia      1039:       
1.25      vatton   1040:       if (registerUndo)
1.34      vatton   1041:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1042:       child = newChild;
                   1043:       childrenCount++;
                   1044:     }
1.27      kia      1045:     
1.44      kia      1046:   Template_FixAccessRight (t, el, doc);
                   1047:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1048:   TtaFreeMemory (types);
                   1049:   TtaFreeMemory (title);
1.1       vatton   1050: #endif /* TEMPLATES */
                   1051: }
                   1052: 
                   1053: /*----------------------------------------------------------------------
                   1054:   ParseTemplate
1.3       vatton   1055:   ----------------------------------------------------------------------*/
1.1       vatton   1056: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                   1057:                            ThotBool loading)
                   1058: {
                   1059: #ifdef TEMPLATES
1.47      kia      1060:   AttributeType attType;
                   1061:   Attribute     att;
                   1062:   Element       aux, child; //Needed when deleting trees
                   1063:   char         *name;
                   1064:   ElementType   elType = TtaGetElementType (el);
                   1065: 
1.37      kia      1066:   if (!t || !el)
1.23      kia      1067:     return;
                   1068:   
1.61      kia      1069: //  static int off = 0;
                   1070: //  int i;
                   1071: //  off++;
                   1072: //  printf("ParseTemplate ");
                   1073: //  for(i=0; i<off; i++)
                   1074: //    printf(" ");
                   1075: //  DumpTemplateElement(el, doc);
                   1076: //  printf("\n");
                   1077:   
1.5       vatton   1078:   name = TtaGetSSchemaName (elType.ElSSchema);
1.47      kia      1079:   if (!strcmp (name, "Template"))
1.1       vatton   1080:     {
1.5       vatton   1081:       switch(elType.ElTypeNum)
1.1       vatton   1082:         {
                   1083:         case Template_EL_head :
                   1084:           //Remove it and all of its children
                   1085:           TtaDeleteTree(el, doc);
                   1086:           //We must stop searching into this tree
1.61      kia      1087: //          off--;
1.1       vatton   1088:           return;
                   1089:           break;
                   1090:         case Template_EL_component :
1.25      vatton   1091:           // remove the name attribute
1.5       vatton   1092:           attType.AttrSSchema = elType.ElSSchema;
1.1       vatton   1093:           attType.AttrTypeNum = Template_ATTR_name;
1.18      kia      1094:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);                                  
1.5       vatton   1095:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.25      vatton   1096:           // replace the component by a use
1.1       vatton   1097:           if (NeedAMenu (el, doc))
1.5       vatton   1098:             TtaChangeElementType (el, Template_EL_useEl);
1.1       vatton   1099:           else
1.5       vatton   1100:             TtaChangeElementType (el, Template_EL_useSimple);
1.25      vatton   1101:           // generate the types attribute
1.1       vatton   1102:           attType.AttrTypeNum = Template_ATTR_types;
1.5       vatton   1103:           att = TtaNewAttribute (attType);
                   1104:           TtaAttachAttribute (el, att, doc);
1.25      vatton   1105:           if (name)
1.18      kia      1106:             TtaSetAttributeText (att, name, el, doc);
1.25      vatton   1107:           // generate the title attribute
                   1108:           attType.AttrTypeNum = Template_ATTR_title;
                   1109:           att = TtaNewAttribute (attType);
                   1110:           TtaAttachAttribute (el, att, doc);
                   1111:           if (name)
                   1112:             TtaSetAttributeText (att, name, el, doc);
                   1113:           // generate the currentType attribute
1.1       vatton   1114:           attType.AttrTypeNum = Template_ATTR_currentType;
1.5       vatton   1115:           att = TtaNewAttribute (attType);
1.18      kia      1116:           TtaAttachAttribute (el, att, doc);
1.25      vatton   1117:           if (name)
1.18      kia      1118:             TtaSetAttributeText (att, name, el, doc);
1.28      kia      1119:           TtaFreeMemory(name);
1.1       vatton   1120:           break;
                   1121:         case Template_EL_option :
                   1122:           aux = NULL;
                   1123:           break;
                   1124:         case Template_EL_bag :
                   1125:           //Link to types
                   1126:           //Allow editing the content
                   1127:           break;
                   1128:         case Template_EL_useEl :
                   1129:         case Template_EL_useSimple :
                   1130:           /* if this use element is not empty, don't do anything: it is
                   1131:              supposed to contain a valid instance. This should be
                   1132:              checked, though */
1.46      vatton   1133:             // add the initial indicator
1.69    ! vatton   1134:           AddPromptIndicator (el, doc);
1.46      vatton   1135:             
1.1       vatton   1136:           if (!TtaGetFirstChild (el))
1.27      kia      1137:             InstantiateUse (t, el, doc, FALSE);
1.1       vatton   1138:           break;
                   1139:         case Template_EL_attribute :
                   1140:           if (!loading)
                   1141:             InstantiateAttribute (t, el, doc);
                   1142:           break;
                   1143:         case Template_EL_repeat :
1.22      kia      1144:           InstantiateRepeat (t, el, doc, FALSE);
1.1       vatton   1145:           break;
                   1146:         default :
                   1147:           break;
                   1148:         }
                   1149:     }
1.47      kia      1150: 
                   1151:   child = TtaGetFirstChild (el);
                   1152:   while (child)
1.1       vatton   1153:     {
                   1154:       aux = child;
1.5       vatton   1155:       TtaNextSibling (&aux);
                   1156:       ParseTemplate (t, child, doc, loading);
1.1       vatton   1157:       child = aux;
                   1158:     }
1.61      kia      1159: //  off--;
1.1       vatton   1160: #endif /* TEMPLATES */
                   1161: }
                   1162: 
                   1163: /*----------------------------------------------------------------------
1.3       vatton   1164:   ----------------------------------------------------------------------*/
1.1       vatton   1165: void DoInstanceTemplate (char *templatename)
                   1166: {
                   1167: #ifdef TEMPLATES
1.47      kia      1168:   XTigerTemplate  t;
                   1169:   ElementType     elType;
1.65      vatton   1170:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.54      vatton   1171:   Document        doc;
1.47      kia      1172:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1173:   int             pi_type;
                   1174: 
1.47      kia      1175:   //Instantiate all elements
                   1176:   t = GetXTigerTemplate(templatename);
1.25      vatton   1177:   if (!t)
1.23      kia      1178:     return;
1.47      kia      1179: 
1.1       vatton   1180:   doc = GetTemplateDocument (t);
1.47      kia      1181:   root =       TtaGetMainRoot (doc);
                   1182:   ParseTemplate (t, root, doc, FALSE);
1.1       vatton   1183: 
                   1184:   //Look for PIs
                   1185:   /* check if the document has a DOCTYPE declaration */
                   1186: #ifdef ANNOTATIONS
                   1187:   if (DocumentTypes[doc]  == docAnnot)
                   1188:     elType = TtaGetElementType (root);
                   1189:   else
                   1190: #endif /* ANNOTATIONS */
                   1191:     elType = TtaGetElementType (root);
                   1192:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1193:   if (strcmp (s, "HTML") == 0)
                   1194:     {
                   1195:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1196:       pi_type = HTML_EL_XMLPI;
                   1197:     }
                   1198: #ifdef _SVG
                   1199:   else if (strcmp (s, "SVG") == 0)
                   1200:     {
                   1201:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1202:       pi_type = SVG_EL_XMLPI;
                   1203:     }
                   1204: #endif /* _SVG */
                   1205:   else if (strcmp (s, "MathML") == 0)
                   1206:     {
                   1207:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1208:       pi_type = MathML_EL_XMLPI;
                   1209:     }
                   1210:   else
                   1211:     {
                   1212:       elType.ElTypeNum = XML_EL_doctype;
                   1213:       pi_type = XML_EL_xmlpi;
                   1214:     }
1.54      vatton   1215: 
1.1       vatton   1216:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1217:   if (doctype == NULL)
1.1       vatton   1218:     {
1.65      vatton   1219:       elType.ElTypeNum = pi_type;      
                   1220:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1221:       if (piElem == NULL)
                   1222:         {
                   1223:           /* generate the XML declaration */
                   1224:           /* Check the Thot abstract tree against the structure schema. */
                   1225:           TtaSetStructureChecking (FALSE, doc);
                   1226:           piElem = TtaNewTree (doc, elType, "");
                   1227:           TtaInsertFirstChild (&piElem, root, doc);
                   1228:           line = TtaGetFirstChild (piElem);
                   1229:           text = TtaGetFirstChild (line);
                   1230:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1231:           charsetname = UpdateDocumentCharset (doc);
                   1232:           strcat (buffer, charsetname);
                   1233:           strcat (buffer, "\"");
                   1234:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1235:           TtaSetStructureChecking (TRUE, doc);
                   1236:           TtaFreeMemory(charsetname);
                   1237:         }
1.1       vatton   1238:     }
                   1239:   
                   1240:   /* generate the XTiger PI */
                   1241:   /* Check the Thot abstract tree against the structure schema. */
                   1242:   TtaSetStructureChecking (FALSE, doc);
                   1243:   elType.ElTypeNum = pi_type;
1.65      vatton   1244:   elNew = TtaNewTree (doc, elType, "");
                   1245:   if (doctype)
                   1246:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1247:   else
                   1248:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1249:   line = TtaGetFirstChild (elNew);
                   1250:   text = TtaGetFirstChild (line);
1.1       vatton   1251:   strcpy (buffer, "xtiger template=\"");
                   1252:   strcat (buffer, templatename);
1.17      kia      1253:   strcat (buffer, "\" version=\"");
1.20      vatton   1254:   if (t->version)
                   1255:     strcat (buffer, t->version);
                   1256:   else
                   1257:     strcat (buffer, "0.8");
1.1       vatton   1258:   strcat (buffer, "\"");
1.25      vatton   1259:   if (t->templateVersion)
1.20      vatton   1260:     {
                   1261:       strcat (buffer, " templateVersion=\"");
                   1262:       strcat (buffer, t->templateVersion);
                   1263:       strcat (buffer, "\"");
                   1264:     }
1.1       vatton   1265:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1266:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1267: 
                   1268:   // update the document title
1.47      kia      1269:   if (!strcmp (s, "HTML"))
1.5       vatton   1270:     {
                   1271:       elType.ElTypeNum = HTML_EL_TITLE;
                   1272:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                   1273:       if (elFound)
                   1274:         {
                   1275:           elFound = TtaGetFirstChild (elFound);
                   1276:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                   1277:                              TtaGetDefaultLanguage (), doc);
                   1278:         }
                   1279:     }
1.1       vatton   1280: #endif /* TEMPLATES */
                   1281: }
                   1282: 
                   1283: /*----------------------------------------------------------------------
1.68      kia      1284:   Template_PreInstantiateComponents
                   1285:   Instantiates all components in order to improve editing.
1.3       vatton   1286:   ----------------------------------------------------------------------*/
1.35      kia      1287: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1288: {
                   1289: #ifdef TEMPLATES 
1.25      vatton   1290:   if (!t)
1.23      kia      1291:     return;
                   1292: 
1.68      kia      1293:   ForwardIterator iter = SearchSet_GetForwardIterator(GetComponents(t));
                   1294:   Declaration     dec;
                   1295:   SearchSetNode   node;
1.47      kia      1296: 
1.68      kia      1297:   ITERATOR_FOREACH(iter, SearchSetNode, node)
1.1       vatton   1298:     {
1.68      kia      1299:       dec = (Declaration) node->elem;
                   1300:       ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), TRUE);
1.1       vatton   1301:     }
1.35      kia      1302:   TtaFreeMemory(iter);
1.1       vatton   1303: #endif /* TEMPLATES */
                   1304: }

Webmaster