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

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);
                    328:   DocumentMeta[doc]->isTemplate = TRUE;
                    329: //  DocumentTypes[doc] = docTemplate;
                    330:   t->isLoaded = TRUE;
1.63      kia       331: 
                    332: #ifdef AMAYA_DEBUG  
1.64      kia       333:     DumpAllDeclarations();
1.63      kia       334: #endif /* AMAYA_DEBUG */
1.64      kia       335:     
                    336:   /* Update the URL combo box */
                    337:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    338:   TtaSetTextZone (doc, 1, URL_list);
                    339:   /* Update template menus */
                    340:   UpdateTemplateMenus(doc);
                    341: 
1.63      kia       342: #endif /* TEMPLATES */  
                    343: }
                    344: 
1.1       vatton    345: /*----------------------------------------------------------------------
                    346:   CreateInstance
1.41      vatton    347:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    348:   ----------------------------------------------------------------------*/
1.63      kia       349: void CreateInstance(char *templatePath, char *instancePath, int basedoc)
1.3       vatton    350: {
1.1       vatton    351: #ifdef TEMPLATES
1.41      vatton    352:   Document          doc = 0, newdoc = 0;
                    353:   ElementType       elType;
                    354:   Element           root, title, text;
1.54      vatton    355:   CHARSET           charset;
                    356:   char             *localFile, *s, *charsetname;
1.63      kia       357:   ThotBool          changes = FALSE;
1.1       vatton    358: 
1.35      kia       359:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    360:   if (t == NULL)
1.29      vatton    361:     {
                    362:       // the template cannot be loaded
                    363:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    364:       return;
                    365:     }
1.41      vatton    366:   // the template document
1.5       vatton    367:   doc = GetTemplateDocument (t);
1.41      vatton    368:   // localize the new created document
                    369:   if (DontReplaceOldDoc)
                    370:     newdoc = TtaGetNextDocumentIndex ();
                    371:   else
                    372:     newdoc = basedoc;
1.54      vatton    373: 
1.58      vatton    374:   if (!TtaHasUndoSequence (doc))
1.1       vatton    375:     {
1.65      vatton    376:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.14      vatton    377:       root = TtaGetRootElement(doc);
                    378:       elType = TtaGetElementType (root);
                    379:       // get the target document type
                    380:       s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       381:       // Do special stuff for HTML documents
1.14      vatton    382:       if (strcmp (s, "HTML") == 0)
1.1       vatton    383:         {
1.14      vatton    384:           // Initialize the document title
                    385:           elType.ElTypeNum = HTML_EL_TITLE;
                    386:           title = TtaSearchTypedElement (elType, SearchInTree, root);
                    387:           text = TtaGetFirstChild (title);
                    388:           while (text)
                    389:             {
                    390:               elType = TtaGetElementType (text);
                    391:               if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                    392:                 {
                    393:                   TtaSetTextContent (text, (unsigned char*)Answer_text,
                    394:                                      TtaGetDefaultLanguage (), doc);
                    395:                   text = NULL;
                    396:                 }
                    397:               else if ((elType.ElTypeNum == Template_EL_useEl ||
                    398:                         elType.ElTypeNum == Template_EL_useSimple) &&
                    399:                        !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    400:                 // Ignore the template use element
                    401:                 text = TtaGetFirstChild (text);
                    402:               else
                    403:                 // Look for the first text child
                    404:                 TtaNextSibling (&text);
                    405:             }
1.63      kia       406:         }
1.54      vatton    407: 
1.65      vatton    408:       // update the charset if needed
                    409:       charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    410:       charset = TtaGetCharset (charsetname);
                    411:       if (charset != UNDEFINED_CHARSET &&
                    412:           DocumentMeta[doc]->charset &&
                    413:           strcmp (charsetname, DocumentMeta[doc]->charset))
                    414:         {
                    415:           TtaSetDocumentCharset (doc, charset, FALSE);
                    416:           DocumentMeta[doc]->charset = TtaStrdup (charsetname);
                    417:           SetNamespacesAndDTD (doc);
                    418:         }
                    419:       // Save HTML special changes
                    420:       TtaCloseUndoSequence (doc);
                    421:       changes = TRUE;
                    422: 
1.63      kia       423:       // Save document
                    424:       SaveDocumentToNewDoc(doc, newdoc, instancePath, &localFile);
                    425: 
                    426:       // Revert HTML special changes
                    427:       if(changes)
                    428:         TtaUndoNoRedo (doc);
                    429:       
                    430:       TtaFreeMemory(localFile);
1.47      kia       431: 
1.1       vatton    432:       TtaClearUndoHistory (doc);
1.41      vatton    433:       RemoveParsingErrors (doc);
                    434:       GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    435:                    !DontReplaceOldDoc, NULL, NULL);
                    436:       TtaSetDocumentModified (newdoc);
1.64      kia       437:       UpdateTemplateMenus(newdoc);
1.1       vatton    438:     }
                    439: #endif /* TEMPLATES */
                    440: }
                    441: 
                    442: /*----------------------------------------------------------------------
1.3       vatton    443:   ----------------------------------------------------------------------*/
1.41      vatton    444: void InstantiateTemplate_callback (int newdoc, int status,  char *urlName,
                    445:                                    char *outputfile,
                    446:                                    char *proxyName, AHTHeaders *http_headers,
                    447:                                    void * context)
1.1       vatton    448: {
                    449: #ifdef TEMPLATES
1.47      kia       450:   InstantiateCtxt *ctx = (InstantiateCtxt*)context;
1.1       vatton    451: 
1.47      kia       452:   DoInstanceTemplate (ctx->templatePath);
1.41      vatton    453:   CreateInstance (ctx->templatePath, ctx->instancePath, ctx->doc);
1.1       vatton    454:   TtaFreeMemory (ctx->templatePath);
                    455:   TtaFreeMemory (ctx->instancePath);
                    456:   TtaFreeMemory (ctx);
                    457: #endif /* TEMPLATES */
                    458: }
                    459: 
                    460: /*----------------------------------------------------------------------
1.3       vatton    461:   ----------------------------------------------------------------------*/
1.1       vatton    462: void InstantiateTemplate (Document doc, char *templatename, char *docname,
                    463:                           DocumentType docType, ThotBool loaded)
                    464: {
                    465: #ifdef TEMPLATES
1.47      kia       466:   if (!loaded)
1.3       vatton    467:     {
                    468:       // Create the callback context
                    469:       InstantiateCtxt *ctx = (InstantiateCtxt *)TtaGetMemory (sizeof (InstantiateCtxt));
                    470:       ctx->templatePath        = TtaStrdup (templatename);
                    471:       ctx->instancePath        = TtaStrdup (docname);
                    472:       ctx->schemaName = GetSchemaFromDocType(docType);
1.41      vatton    473:       ctx->doc = doc;
1.3       vatton    474:       ctx->docType = docType;
1.47      kia       475: 
1.3       vatton    476:       GetAmayaDoc (templatename, NULL, doc, doc, CE_MAKEBOOK, FALSE, 
1.41      vatton    477:                    (void (*)(int, int, char*, char*, char*,
                    478:                              const AHTHeaders*, void*)) InstantiateTemplate_callback,
1.3       vatton    479:                    (void *) ctx);
                    480:     }
1.47      kia       481:   else
1.1       vatton    482:     {
                    483:       DoInstanceTemplate (templatename);
1.41      vatton    484:       CreateInstance (templatename, docname, doc);
1.3       vatton    485:     }  
1.1       vatton    486: #endif /* TEMPLATES */
                    487: }
                    488: 
                    489: /*----------------------------------------------------------------------
                    490:   InstantiateAttribute
1.3       vatton    491:   ----------------------------------------------------------------------*/
1.1       vatton    492: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
                    493: {
                    494: #ifdef TEMPLATES
                    495:   AttributeType  useType, nameType, defaultType, attrType;
                    496:   Attribute      useAttr, nameAttr, defAttr, attr;
                    497:   ElementType    elType;
                    498:   Element        parent;
                    499:   char           *text, *elementName;
                    500:   ThotBool       level;
                    501:   NotifyAttribute event;
1.48      kia       502:   int             val;
1.1       vatton    503: 
                    504:   parent = TtaGetParent (el);
                    505:   if (!parent)
                    506:     return;
                    507:   // if attribute "use" has value "optional", don't do anything
                    508:   useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    509:   useType.AttrTypeNum = Template_ATTR_useAt;
                    510:   useAttr = TtaGetAttribute (el, useType);
                    511:   if (useAttr)
                    512:     // there is a "use" attribute. Check its value
                    513:     {
1.48      kia       514:       val = TtaGetAttributeValue(useAttr);
                    515:       if (val == Template_ATTR_useAt_VAL_optional)
1.18      kia       516:       {
                    517:         return;
                    518:       }
1.1       vatton    519:     }
1.18      kia       520:     
1.1       vatton    521:   // get the "name" and "default" attributes
                    522:   nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.9       vatton    523:   nameType.AttrTypeNum = Template_ATTR_ref_name;
1.1       vatton    524:   defaultType.AttrTypeNum = Template_ATTR_defaultAt;
                    525:   nameAttr = TtaGetAttribute (el, nameType);
                    526:   defAttr = TtaGetAttribute (el, defaultType);
                    527:   if (nameAttr)
                    528:     {
1.10      kia       529:       text = GetAttributeStringValue (el, nameAttr, NULL);
1.1       vatton    530:       if (text)
                    531:         {
                    532:           elType = TtaGetElementType (parent);
                    533:           elementName = TtaGetElementTypeName (elType);
                    534:           level = TRUE;
                    535:           MapHTMLAttribute (text, &attrType, elementName, &level, doc);
                    536:           TtaFreeMemory(text);
                    537:           attr = TtaNewAttribute (attrType);
                    538:           if (attr)
                    539:             {
                    540:               TtaAttachAttribute (parent, attr, doc);
                    541:               if (defAttr)
                    542:                 {
1.10      kia       543:                   text = GetAttributeStringValue (el, defAttr, NULL);
1.25      vatton    544:                   if (text)
1.18      kia       545:                     TtaSetAttributeText(attr, text, parent, doc);
1.1       vatton    546:                   TtaFreeMemory(text);
                    547:                   // if it's a src arttribute for an image, load the image
                    548:                   if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
                    549:                       elType.ElTypeNum == HTML_EL_IMG)
                    550:                     if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
                    551:                         attrType.AttrSSchema == elType.ElSSchema)
                    552:                       {
                    553:                         event.document = doc;
                    554:                         event.element = parent;
                    555:                         event.attribute = attr;
                    556:                         SRCattrModified (&event);
                    557:                       }
                    558:                 }
                    559:             }
                    560:         }
                    561:     }
                    562: #endif /* TEMPLATES */
                    563: }
                    564: 
                    565: #ifdef TEMPLATES
                    566: /*----------------------------------------------------------------------
                    567:   ProcessAttr
1.9       vatton    568:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    569:   ----------------------------------------------------------------------*/
1.1       vatton    570: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    571: {
                    572:   Element      child;
                    573:   ElementType  elType;
                    574: 
                    575:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    576:     {
                    577:       elType = TtaGetElementType (child);
                    578:       if (elType.ElTypeNum == Template_EL_attribute &&
                    579:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    580:         InstantiateAttribute (t, child, doc);
                    581:       else
                    582:         ProcessAttr (t, child, doc);
                    583:     }
                    584: }
                    585: #endif /* TEMPLATES */
                    586: 
1.12      kia       587: 
                    588: /*----------------------------------------------------------------------
                    589:   Template_GetNewSimpleTypeInstance
                    590:   Create an new instance of xt:use/SimpleType
                    591:   The decl attribute must embed SimpleType declaration (no validation).
                    592:   @param decl Declaration of new element
                    593:   @param parent Future parent element
                    594:   @param doc Document
                    595:   @return The new element
                    596:   ----------------------------------------------------------------------*/
                    597: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl)
                    598: {
                    599:   Element           newEl = NULL;
                    600: #ifdef TEMPLATES
                    601:   ElementType       elType;
                    602:   char             *empty = " ";
1.24      vatton    603: 
1.38      vatton    604:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       605:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    606:   newEl = TtaNewElement (doc, elType);
                    607:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    608: #endif /* TEMPLATES */
1.12      kia       609:   return newEl;
                    610: }
                    611: 
                    612: /*----------------------------------------------------------------------
                    613:   Template_GetNewXmlElementInstance
                    614:   Create an new instance of xt:use/XmlElement
                    615:   The decl attribute must embed XmlElement declaration (no validation).
                    616:   @param decl Declaration of new element
                    617:   @param parent Future parent element
                    618:   @param doc Document
                    619:   @return The new element
                    620:   ----------------------------------------------------------------------*/
                    621: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
                    622: {
                    623:   Element           newEl = NULL;
                    624: #ifdef TEMPLATES
                    625:   ElementType       elType;
                    626: 
1.24      vatton    627:   GIType (decl->name, &elType, doc);
                    628:   if (elType.ElTypeNum != 0)
1.12      kia       629:   {
1.23      kia       630:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       631:   }
                    632: #endif /* TEMPLATES */
                    633:   return newEl;
                    634: }
                    635: 
1.34      vatton    636: 
                    637: /*----------------------------------------------------------------------
                    638:   InsertWithNotify applies pre and post functions when inserting the new
                    639:   element el after child (if not NULL) or as first child of parent.
                    640:   ----------------------------------------------------------------------*/
                    641: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    642: {
                    643:   ElementType      elType;
                    644:   NotifyElement    event;
                    645:   char            *name;
                    646:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    647:   ThotBool         isImage = FALSE;
                    648:   ThotBool         oldStructureChecking;
                    649: 
                    650:   // avoid to check attributes now
                    651:   oldStructureChecking = TtaGetStructureChecking (doc);
                    652:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    653: 
                    654:   elType = TtaGetElementType (el);
                    655:   name = TtaGetSSchemaName (elType.ElSSchema);
                    656:   isCell = ((!strcmp (name,"HTML") &&
                    657:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    658:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    659:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    660:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    661:            (!strcmp (name,"MathML") &&
                    662:             (elType.ElTypeNum == MathML_EL_MTR ||
                    663:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    664:   isImage = (!strcmp (name,"HTML") && 
                    665:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    666:   if (child)
                    667:     TtaInsertSibling (el, child, FALSE, doc);
                    668:   else
                    669:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    670:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    671: 
1.50      vatton    672:   if (isImage)
                    673:     InsertImageOrObject (el, doc);
                    674:   else if (isCell)
1.34      vatton    675:     {
                    676:       // a cell is created
1.39      quint     677:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    678:     }
                    679:   else if (isRow)
                    680:     {
                    681:       // a row is created
                    682:       event.element = el;
                    683:       event.document = doc;
                    684:       RowPasted (&event);
                    685:     }
1.50      vatton    686:   
                    687:   if (!strcmp (name,"HTML"))
                    688:     {
                    689:       elType.ElTypeNum = HTML_EL_IMG;
                    690:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    691:       while (child)
                    692:         {
                    693:           InsertImageOrObject (child, doc);
                    694:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    695:         }
                    696:       elType.ElTypeNum = HTML_EL_Object;
                    697:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    698:       while (child)
                    699:         {
                    700:           InsertImageOrObject (child, doc);
                    701:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    702:         }
                    703:     }
1.34      vatton    704:   return el;
                    705: }
                    706: 
                    707: 
1.12      kia       708: /*----------------------------------------------------------------------
1.16      kia       709:   Template_InsertUseChildren
                    710:   Insert children to a xt:use
                    711:   The dec parameter must be valid and will not be verified. It must be a
                    712:     direct child element (for union elements).
                    713:   @param el element (xt:use) in which insert a new element
                    714:   @param dec Template declaration of the element to insert
                    715:   @return The inserted element (the xt:use element if insertion is multiple as component)
                    716:   ----------------------------------------------------------------------*/
                    717: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    718: {
1.34      vatton    719:   Element     newEl = NULL;
1.16      kia       720: #ifdef TEMPLATES
1.19      vatton    721:   Element     current = NULL;
1.34      vatton    722:   Element     child = NULL;
1.19      vatton    723:   //char       *attrCurrentTypeValue;
                    724:   //ElementType elType;
1.17      kia       725:   
1.25      vatton    726:   if (TtaGetDocumentAccessMode(doc))
1.16      kia       727:   {
1.23      kia       728:     switch (dec->nature)
                    729:     {
                    730:       case SimpleTypeNat:
                    731:         newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
1.34      vatton    732:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       733:         break;
                    734:       case XmlElementNat:
                    735:         newEl = Template_GetNewXmlElementInstance(doc, el, dec);
1.34      vatton    736:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       737:         break;
                    738:       case ComponentNat:
                    739:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.35      kia       740:         ProcessAttr (dec->usedIn, newEl, doc);        
1.23      kia       741:         /* Copy elements from new use to existing use. */
1.34      vatton    742:         while ((child = TtaGetFirstChild(newEl)))
1.23      kia       743:         {
                    744:           TtaRemoveTree (child, doc);
1.34      vatton    745:           child = InsertWithNotify (child, current, el, doc);
1.23      kia       746:           current = child; 
                    747:         }
                    748:         
                    749:         /* Copy currentType attribute. */
                    750:         //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL);
                    751:         //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue);
                    752:         TtaDeleteTree(newEl, doc);
                    753:         newEl = el;
                    754:         break;
                    755:       case UnionNat :
                    756:         /* Nothing to do.*/
                    757:   //                elType.ElTypeNum = Template_EL_useEl;
                    758:   //                cont = TtaNewElement (doc, elType);
                    759:   //                if (cont)
                    760:   //                  {
                    761:   //                    TtaSetAccessRight (cont, ReadWrite, doc);
                    762:   //                    at = TtaNewAttribute (att);
                    763:   //                    if (at)
                    764:   //                      {
                    765:   //                        TtaAttachAttribute (cont, at, doc);
                    766:   //                        TtaSetAttributeText(at, types, cont, doc);
                    767:   //                      }
                    768:   //                  }
                    769:         /* @@@@@ */
                    770:         break;
                    771:       default :
                    772:         //Impossible
                    773:         break;   
                    774:     }
1.44      kia       775:     Template_FixAccessRight (dec->usedIn, el, doc);
                    776:     TtaUpdateAccessRightInViews (doc, el);    
1.23      kia       777:   }  
1.16      kia       778: #endif /* TEMPLATES */
                    779:   return newEl;
                    780: }
                    781: 
1.40      kia       782: 
                    783: /*----------------------------------------------------------------------
                    784:   Fix access rights.
                    785:   ----------------------------------------------------------------------*/
1.41      vatton    786: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia       787: {
                    788: #ifdef TEMPLATES
                    789:   ElementType elType;
                    790:   Element     child;
                    791:   char        currentType[MAX_LENGTH];
                    792:   Declaration decl;
                    793:   
1.62      kia       794: //  DumpElementPath(el);
1.61      kia       795:   
1.40      kia       796:   if (t && el && doc)
                    797:     {
                    798:       elType = TtaGetElementType(el);
1.41      vatton    799:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia       800:         {
1.41      vatton    801:           switch (elType.ElTypeNum)
1.40      kia       802:             {
                    803:             case Template_EL_TEXT_UNIT:
1.41      vatton    804:               //TtaSetAccessRight( el, ReadWrite, doc);
                    805:               return;
1.40      kia       806:             case Template_EL_useEl:
                    807:             case Template_EL_useSimple:
                    808:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                    809:                                               (char*)currentType, NULL);
                    810:               decl = Template_GetDeclaration(t, currentType);
                    811:               if (decl)
                    812:                 {
                    813:                   switch (decl->nature)
                    814:                     {
                    815:                       case SimpleTypeNat:
                    816:                       case XmlElementNat:
1.41      vatton    817:                         TtaSetAccessRight (el, ReadWrite, doc);
                    818:                         return;
1.40      kia       819:                       default:
1.41      vatton    820:                         TtaSetAccessRight (el, ReadOnly, doc);
                    821:                          break;
1.40      kia       822:                     }
                    823:                 }
                    824:               break;
                    825:             case Template_EL_bag:
1.45      vatton    826:             case Template_EL_repeat:
1.40      kia       827:               TtaSetAccessRight(el, ReadWrite, doc);
                    828:               break;
                    829:             default:
                    830:               TtaSetAccessRight(el, ReadOnly, doc);
                    831:               break;
                    832:             }
                    833:         }
                    834: 
1.41      vatton    835:       child = TtaGetFirstChild (el);
                    836:       // fix access right to children
                    837:       while (child)
1.40      kia       838:         {
1.41      vatton    839:           Template_FixAccessRight (t, child, doc);
                    840:           TtaNextSibling (&child);
1.40      kia       841:         }
                    842:     }
                    843: #endif /* TEMPLATES */
                    844: }
                    845: 
1.16      kia       846: /*----------------------------------------------------------------------
1.46      vatton    847:   AddPromptIndicator
                    848:   ----------------------------------------------------------------------*/
                    849: void AddPromptIndicator (Element el, Document doc)
                    850: {
                    851: #ifdef TEMPLATES
                    852:   ElementType         elType;
                    853:   AttributeType       attrType;
                    854:   Attribute           att;
                    855: 
1.66    ! vatton    856:   if (el)
        !           857:     {
        !           858:       elType = TtaGetElementType (el);
        !           859:       attrType.AttrSSchema = elType.ElSSchema;
        !           860:       attrType.AttrTypeNum = Template_ATTR_prompt;
        !           861:       att = TtaNewAttribute (attrType);
        !           862:       TtaAttachAttribute (el, att, doc);
        !           863:     }
1.46      vatton    864: #endif /* TEMPLATES */
                    865: }
                    866: 
                    867: /*----------------------------------------------------------------------
1.1       vatton    868:   InstantiateUse
1.3       vatton    869:   ----------------------------------------------------------------------*/
1.1       vatton    870: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.27      kia       871:                         ThotBool registerUndo)
1.1       vatton    872: {
                    873: #ifdef TEMPLATES
1.47      kia       874:   Element          cont = NULL;
1.1       vatton    875:   ElementType      elType;
                    876:   Declaration      dec;
1.28      kia       877:   int              size, nbitems, i;
1.1       vatton    878:   struct menuType  *items;
1.33      vatton    879:   char             *types, *text = NULL;
1.1       vatton    880:   ThotBool          oldStructureChecking;
                    881: 
1.25      vatton    882:   if (!t)
1.23      kia       883:     return NULL;
                    884: 
1.1       vatton    885:   /* get the value of the "types" attribute */
                    886:   cont = NULL;
1.12      kia       887:   elType = TtaGetElementType (el);
1.46      vatton    888:   AddPromptIndicator (el, doc);
1.32      vatton    889:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton    890:   if (!types || types[0] == EOS)
                    891:     {
                    892:       TtaFreeMemory (types);
                    893:       return NULL;
                    894:     }
1.1       vatton    895:   giveItems (types, size, &items, &nbitems);
                    896:   // No structure checking
                    897:   oldStructureChecking = TtaGetStructureChecking (doc);
                    898:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       899:   
1.1       vatton    900:   if (nbitems == 1)
                    901:     /* only one type in the "types" attribute */
                    902:     {
1.17      kia       903:       dec = Template_GetDeclaration (t, items[0].label);
1.1       vatton    904:       if (dec)
1.27      kia       905:       {
1.46      vatton    906:         cont = Template_InsertUseChildren (doc, el, dec);
1.32      vatton    907:         if (cont)
1.27      kia       908:         {
                    909:           TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1.32      vatton    910:           if (registerUndo)
                    911:             TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.27      kia       912:         }
                    913:       }
1.1       vatton    914:     }
1.33      vatton    915:   TtaFreeMemory (text);
1.32      vatton    916:   TtaFreeMemory (types);
1.28      kia       917:   
1.32      vatton    918:   for (i = 0; i < nbitems; i++)
1.28      kia       919:     TtaFreeMemory(items[i].label);
1.18      kia       920:   TtaFreeMemory(items);
1.1       vatton    921:   TtaSetStructureChecking (oldStructureChecking, doc);
1.40      kia       922:   
1.44      kia       923:   Template_FixAccessRight (t, el, doc);
                    924:   TtaUpdateAccessRightInViews (doc, el);
                    925:   
1.1       vatton    926:   return cont;
1.23      kia       927: #else /* TEMPLATES */
                    928:   return NULL;
1.1       vatton    929: #endif /* TEMPLATES */
                    930: }
                    931: 
                    932: /*----------------------------------------------------------------------
1.22      kia       933:   InstantiateRepeat
                    934:   Check for min and max param and validate xt:repeat element content.
                    935:   @param registerUndo True to register undo creation sequences.
1.3       vatton    936:   ----------------------------------------------------------------------*/
1.46      vatton    937: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
                    938:                         ThotBool registerUndo)
1.1       vatton    939: {
                    940: #ifdef TEMPLATES
1.32      vatton    941:   Element        child, newChild;
1.52      vatton    942:   ElementType    newElType;
                    943:   Attribute      minAtt,  maxAtt;
                    944:   AttributeType  minType, maxType;
                    945:   char          *text, *types = NULL, *title = NULL;
                    946:   int            curVal, minVal,  maxVal;
1.32      vatton    947:   int            childrenCount;
                    948: 
1.1       vatton    949: 
1.25      vatton    950:   if (!t)
1.23      kia       951:     return;
                    952: 
1.1       vatton    953:   //Preparing types
1.52      vatton    954:   minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1       vatton    955:   minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52      vatton    956:   maxType.AttrSSchema =  minType.AttrSSchema;
1.1       vatton    957:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52      vatton    958:   newElType.ElSSchema = minType.AttrSSchema;
                    959:   //Get minOccurs and maxOccurs attributes
1.1       vatton    960:   minAtt = TtaGetAttribute (el, minType);
                    961:   maxAtt = TtaGetAttribute (el, maxType);
                    962:   //Get the values
                    963:   if (minAtt)
                    964:     {
1.10      kia       965:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    966:       if (text)
                    967:         {
                    968:           minVal = atoi(text);
                    969:           TtaFreeMemory(text);
1.52      vatton    970:           curVal = minVal;
1.1       vatton    971:         }
                    972:       else
                    973:         //Error : Attribute with no value
                    974:         return;
                    975:     }
                    976:   else
1.52      vatton    977:     {
                    978:       minVal = 0;
                    979:       curVal = 1;
                    980:     }
1.1       vatton    981: 
                    982:   if (maxAtt)
                    983:     {
1.10      kia       984:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton    985:       if (text)
                    986:         {
                    987:           if (!strcmp (text, "*"))
                    988:             maxVal = INT_MAX;
                    989:           else
                    990:             maxVal = atoi (text);
                    991:           TtaFreeMemory (text);
                    992:         }
                    993:       else
                    994:         //Error : Attribute with no value
                    995:         return;
                    996:     }
                    997:   else
                    998:     maxVal = INT_MAX;
                    999: 
                   1000:   text = (char*)TtaGetMemory(MAX_LENGTH);
1.52      vatton   1001:   //Create non existing min max attributes
                   1002:   if (minAtt == NULL)
1.1       vatton   1003:     {      
1.32      vatton   1004:       minAtt = TtaNewAttribute (minType);
                   1005:       sprintf (text, "%d", minVal);
                   1006:       TtaAttachAttribute (el, minAtt, doc);
                   1007:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton   1008:       if (registerUndo)
1.32      vatton   1009:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton   1010:     }
                   1011: 
1.52      vatton   1012:   if (maxAtt == NULL)
1.1       vatton   1013:     {  
1.32      vatton   1014:       maxAtt = TtaNewAttribute (maxType);
                   1015:       if (maxVal < INT_MAX)
                   1016:         sprintf(text, "%d", maxVal);
1.1       vatton   1017:       else
1.32      vatton   1018:         sprintf (text, "*");
                   1019:       TtaAttachAttribute (el, maxAtt, doc);      
                   1020:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1021:       if (registerUndo)
1.32      vatton   1022:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton   1023:     }
1.52      vatton   1024:   TtaFreeMemory(text);
1.1       vatton   1025: 
1.52      vatton   1026:   //We must have minOccurs children
1.1       vatton   1027:   child = TtaGetFirstChild(el);
                   1028:   if (!child)
                   1029:     //Error : a repeat must have at least one child which will be the model
                   1030:     return;
                   1031:   
                   1032:   for(childrenCount = 0; child; TtaNextSibling(&child))
                   1033:     {
                   1034:       //TODO : Check that every child is valid
                   1035:       childrenCount ++;
                   1036:     }
                   1037: 
                   1038:   if (childrenCount > maxVal)
                   1039:     //Error : too many children!
                   1040:     return;  
                   1041: 
                   1042:   child = TtaGetLastChild(el);
1.32      vatton   1043:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1044:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1045:   newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton   1046:   while (childrenCount < curVal)
1.1       vatton   1047:     {
1.32      vatton   1048:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1049:       // Insert it
1.32      vatton   1050:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1051:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1052:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.34      vatton   1053:       InstantiateUse (t, newChild, doc, TRUE);
1.27      kia      1054:       
1.25      vatton   1055:       if (registerUndo)
1.34      vatton   1056:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1057:       child = newChild;
                   1058:       childrenCount++;
                   1059:     }
1.27      kia      1060:     
1.44      kia      1061:   Template_FixAccessRight (t, el, doc);
                   1062:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1063:   TtaFreeMemory (types);
                   1064:   TtaFreeMemory (title);
1.1       vatton   1065: #endif /* TEMPLATES */
                   1066: }
                   1067: 
                   1068: /*----------------------------------------------------------------------
                   1069:   ParseTemplate
1.3       vatton   1070:   ----------------------------------------------------------------------*/
1.1       vatton   1071: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                   1072:                            ThotBool loading)
                   1073: {
                   1074: #ifdef TEMPLATES
1.47      kia      1075:   AttributeType attType;
                   1076:   Attribute     att;
                   1077:   Element       aux, child; //Needed when deleting trees
                   1078:   char         *name;
                   1079:   ElementType   elType = TtaGetElementType (el);
                   1080: 
1.37      kia      1081:   if (!t || !el)
1.23      kia      1082:     return;
                   1083:   
1.61      kia      1084: //  static int off = 0;
                   1085: //  int i;
                   1086: //  off++;
                   1087: //  printf("ParseTemplate ");
                   1088: //  for(i=0; i<off; i++)
                   1089: //    printf(" ");
                   1090: //  DumpTemplateElement(el, doc);
                   1091: //  printf("\n");
                   1092:   
1.5       vatton   1093:   name = TtaGetSSchemaName (elType.ElSSchema);
1.47      kia      1094:   if (!strcmp (name, "Template"))
1.1       vatton   1095:     {
1.5       vatton   1096:       switch(elType.ElTypeNum)
1.1       vatton   1097:         {
                   1098:         case Template_EL_head :
                   1099:           //Remove it and all of its children
                   1100:           TtaDeleteTree(el, doc);
                   1101:           //We must stop searching into this tree
1.61      kia      1102: //          off--;
1.1       vatton   1103:           return;
                   1104:           break;
                   1105:         case Template_EL_component :
1.25      vatton   1106:           // remove the name attribute
1.5       vatton   1107:           attType.AttrSSchema = elType.ElSSchema;
1.1       vatton   1108:           attType.AttrTypeNum = Template_ATTR_name;
1.18      kia      1109:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);                                  
1.5       vatton   1110:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.25      vatton   1111:           // replace the component by a use
1.1       vatton   1112:           if (NeedAMenu (el, doc))
1.5       vatton   1113:             TtaChangeElementType (el, Template_EL_useEl);
1.1       vatton   1114:           else
1.5       vatton   1115:             TtaChangeElementType (el, Template_EL_useSimple);
1.25      vatton   1116:           // generate the types attribute
1.1       vatton   1117:           attType.AttrTypeNum = Template_ATTR_types;
1.5       vatton   1118:           att = TtaNewAttribute (attType);
                   1119:           TtaAttachAttribute (el, att, doc);
1.25      vatton   1120:           if (name)
1.18      kia      1121:             TtaSetAttributeText (att, name, el, doc);
1.25      vatton   1122:           // generate the title attribute
                   1123:           attType.AttrTypeNum = Template_ATTR_title;
                   1124:           att = TtaNewAttribute (attType);
                   1125:           TtaAttachAttribute (el, att, doc);
                   1126:           if (name)
                   1127:             TtaSetAttributeText (att, name, el, doc);
                   1128:           // generate the currentType attribute
1.1       vatton   1129:           attType.AttrTypeNum = Template_ATTR_currentType;
1.5       vatton   1130:           att = TtaNewAttribute (attType);
1.18      kia      1131:           TtaAttachAttribute (el, att, doc);
1.25      vatton   1132:           if (name)
1.18      kia      1133:             TtaSetAttributeText (att, name, el, doc);
1.28      kia      1134:           TtaFreeMemory(name);
1.1       vatton   1135:           break;
                   1136:         case Template_EL_option :
                   1137:           aux = NULL;
                   1138:           break;
                   1139:         case Template_EL_bag :
                   1140:           //Link to types
                   1141:           //Allow editing the content
                   1142:           break;
                   1143:         case Template_EL_useEl :
                   1144:         case Template_EL_useSimple :
                   1145:           /* if this use element is not empty, don't do anything: it is
                   1146:              supposed to contain a valid instance. This should be
                   1147:              checked, though */
1.46      vatton   1148:           if (DocumentMeta[doc] && DocumentMeta[doc]->isTemplate)
                   1149:             // add the initial indicator
                   1150:             AddPromptIndicator (el, doc);
                   1151:             
1.1       vatton   1152:           if (!TtaGetFirstChild (el))
1.27      kia      1153:             InstantiateUse (t, el, doc, FALSE);
1.1       vatton   1154:           break;
                   1155:         case Template_EL_attribute :
                   1156:           if (!loading)
                   1157:             InstantiateAttribute (t, el, doc);
                   1158:           break;
                   1159:         case Template_EL_repeat :
1.22      kia      1160:           InstantiateRepeat (t, el, doc, FALSE);
1.1       vatton   1161:           break;
                   1162:         default :
                   1163:           break;
                   1164:         }
                   1165:     }
1.47      kia      1166: 
                   1167:   child = TtaGetFirstChild (el);
                   1168:   while (child)
1.1       vatton   1169:     {
                   1170:       aux = child;
1.5       vatton   1171:       TtaNextSibling (&aux);
                   1172:       ParseTemplate (t, child, doc, loading);
1.1       vatton   1173:       child = aux;
                   1174:     }
1.61      kia      1175: //  off--;
1.1       vatton   1176: #endif /* TEMPLATES */
                   1177: }
                   1178: 
                   1179: /*----------------------------------------------------------------------
1.3       vatton   1180:   ----------------------------------------------------------------------*/
1.1       vatton   1181: void DoInstanceTemplate (char *templatename)
                   1182: {
                   1183: #ifdef TEMPLATES
1.47      kia      1184:   XTigerTemplate  t;
                   1185:   ElementType     elType;
1.65      vatton   1186:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.54      vatton   1187:   Document        doc;
1.47      kia      1188:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1189:   int             pi_type;
                   1190: 
1.47      kia      1191:   //Instantiate all elements
                   1192:   t = GetXTigerTemplate(templatename);
1.25      vatton   1193:   if (!t)
1.23      kia      1194:     return;
1.47      kia      1195: 
1.1       vatton   1196:   doc = GetTemplateDocument (t);
1.47      kia      1197:   root =       TtaGetMainRoot (doc);
                   1198:   ParseTemplate (t, root, doc, FALSE);
1.1       vatton   1199: 
                   1200:   //Look for PIs
                   1201:   /* check if the document has a DOCTYPE declaration */
                   1202: #ifdef ANNOTATIONS
                   1203:   if (DocumentTypes[doc]  == docAnnot)
                   1204:     elType = TtaGetElementType (root);
                   1205:   else
                   1206: #endif /* ANNOTATIONS */
                   1207:     elType = TtaGetElementType (root);
                   1208:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1209:   if (strcmp (s, "HTML") == 0)
                   1210:     {
                   1211:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1212:       pi_type = HTML_EL_XMLPI;
                   1213:     }
                   1214: #ifdef _SVG
                   1215:   else if (strcmp (s, "SVG") == 0)
                   1216:     {
                   1217:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1218:       pi_type = SVG_EL_XMLPI;
                   1219:     }
                   1220: #endif /* _SVG */
                   1221:   else if (strcmp (s, "MathML") == 0)
                   1222:     {
                   1223:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1224:       pi_type = MathML_EL_XMLPI;
                   1225:     }
                   1226:   else
                   1227:     {
                   1228:       elType.ElTypeNum = XML_EL_doctype;
                   1229:       pi_type = XML_EL_xmlpi;
                   1230:     }
1.54      vatton   1231: 
1.1       vatton   1232:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1233:   if (doctype == NULL)
1.1       vatton   1234:     {
1.65      vatton   1235:       elType.ElTypeNum = pi_type;      
                   1236:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1237:       if (piElem == NULL)
                   1238:         {
                   1239:           /* generate the XML declaration */
                   1240:           /* Check the Thot abstract tree against the structure schema. */
                   1241:           TtaSetStructureChecking (FALSE, doc);
                   1242:           piElem = TtaNewTree (doc, elType, "");
                   1243:           TtaInsertFirstChild (&piElem, root, doc);
                   1244:           line = TtaGetFirstChild (piElem);
                   1245:           text = TtaGetFirstChild (line);
                   1246:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1247:           charsetname = UpdateDocumentCharset (doc);
                   1248:           strcat (buffer, charsetname);
                   1249:           strcat (buffer, "\"");
                   1250:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1251:           TtaSetStructureChecking (TRUE, doc);
                   1252:           TtaFreeMemory(charsetname);
                   1253:         }
1.1       vatton   1254:     }
                   1255:   
                   1256:   /* generate the XTiger PI */
                   1257:   /* Check the Thot abstract tree against the structure schema. */
                   1258:   TtaSetStructureChecking (FALSE, doc);
                   1259:   elType.ElTypeNum = pi_type;
1.65      vatton   1260:   elNew = TtaNewTree (doc, elType, "");
                   1261:   if (doctype)
                   1262:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1263:   else
                   1264:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1265:   line = TtaGetFirstChild (elNew);
                   1266:   text = TtaGetFirstChild (line);
1.1       vatton   1267:   strcpy (buffer, "xtiger template=\"");
                   1268:   strcat (buffer, templatename);
1.17      kia      1269:   strcat (buffer, "\" version=\"");
1.20      vatton   1270:   if (t->version)
                   1271:     strcat (buffer, t->version);
                   1272:   else
                   1273:     strcat (buffer, "0.8");
1.1       vatton   1274:   strcat (buffer, "\"");
1.25      vatton   1275:   if (t->templateVersion)
1.20      vatton   1276:     {
                   1277:       strcat (buffer, " templateVersion=\"");
                   1278:       strcat (buffer, t->templateVersion);
                   1279:       strcat (buffer, "\"");
                   1280:     }
1.1       vatton   1281:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1282:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1283: 
                   1284:   // update the document title
1.47      kia      1285:   if (!strcmp (s, "HTML"))
1.5       vatton   1286:     {
                   1287:       elType.ElTypeNum = HTML_EL_TITLE;
                   1288:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                   1289:       if (elFound)
                   1290:         {
                   1291:           elFound = TtaGetFirstChild (elFound);
                   1292:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                   1293:                              TtaGetDefaultLanguage (), doc);
                   1294:         }
                   1295:     }
1.1       vatton   1296: #endif /* TEMPLATES */
                   1297: }
                   1298: 
                   1299: /*----------------------------------------------------------------------
1.46      vatton   1300:   Template_PreInstantiateComponents: Instantiates all components in 
                   1301:   order to improve editing.
1.3       vatton   1302:   ----------------------------------------------------------------------*/
1.35      kia      1303: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1304: {
                   1305: #ifdef TEMPLATES 
1.25      vatton   1306:   if (!t)
1.23      kia      1307:     return;
                   1308: 
1.35      kia      1309:   HashMap         components = GetComponents(t);
                   1310:   ForwardIterator iter = HashMap_GetForwardIterator(components);
                   1311:   Declaration     comp;
                   1312:   HashMapNode     node;
1.47      kia      1313: 
1.35      kia      1314:   ITERATOR_FOREACH(iter, HashMapNode, node)
1.1       vatton   1315:     {
1.35      kia      1316:       comp = (Declaration) node->elem;
1.1       vatton   1317:       ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
                   1318:     }
1.35      kia      1319:   TtaFreeMemory(iter);
1.1       vatton   1320: #endif /* TEMPLATES */
                   1321: }

Webmaster