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

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: 
                    856:   elType = TtaGetElementType (el);
                    857:   attrType.AttrSSchema = elType.ElSSchema;
                    858:   attrType.AttrTypeNum = Template_ATTR_prompt;
                    859:   att = TtaNewAttribute (attrType);
                    860:   TtaAttachAttribute (el, att, doc);
                    861: #endif /* TEMPLATES */
                    862: }
                    863: 
                    864: /*----------------------------------------------------------------------
1.1       vatton    865:   InstantiateUse
1.3       vatton    866:   ----------------------------------------------------------------------*/
1.1       vatton    867: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.27      kia       868:                         ThotBool registerUndo)
1.1       vatton    869: {
                    870: #ifdef TEMPLATES
1.47      kia       871:   Element          cont = NULL;
1.1       vatton    872:   ElementType      elType;
                    873:   Declaration      dec;
1.28      kia       874:   int              size, nbitems, i;
1.1       vatton    875:   struct menuType  *items;
1.33      vatton    876:   char             *types, *text = NULL;
1.1       vatton    877:   ThotBool          oldStructureChecking;
                    878: 
1.25      vatton    879:   if (!t)
1.23      kia       880:     return NULL;
                    881: 
1.1       vatton    882:   /* get the value of the "types" attribute */
                    883:   cont = NULL;
1.12      kia       884:   elType = TtaGetElementType (el);
1.46      vatton    885:   AddPromptIndicator (el, doc);
1.32      vatton    886:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton    887:   if (!types || types[0] == EOS)
                    888:     {
                    889:       TtaFreeMemory (types);
                    890:       return NULL;
                    891:     }
1.1       vatton    892:   giveItems (types, size, &items, &nbitems);
                    893:   // No structure checking
                    894:   oldStructureChecking = TtaGetStructureChecking (doc);
                    895:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       896:   
1.1       vatton    897:   if (nbitems == 1)
                    898:     /* only one type in the "types" attribute */
                    899:     {
1.17      kia       900:       dec = Template_GetDeclaration (t, items[0].label);
1.1       vatton    901:       if (dec)
1.27      kia       902:       {
1.46      vatton    903:         cont = Template_InsertUseChildren (doc, el, dec);
1.32      vatton    904:         if (cont)
1.27      kia       905:         {
                    906:           TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1.32      vatton    907:           if (registerUndo)
                    908:             TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.27      kia       909:         }
                    910:       }
1.1       vatton    911:     }
1.33      vatton    912:   TtaFreeMemory (text);
1.32      vatton    913:   TtaFreeMemory (types);
1.28      kia       914:   
1.32      vatton    915:   for (i = 0; i < nbitems; i++)
1.28      kia       916:     TtaFreeMemory(items[i].label);
1.18      kia       917:   TtaFreeMemory(items);
1.1       vatton    918:   TtaSetStructureChecking (oldStructureChecking, doc);
1.40      kia       919:   
1.44      kia       920:   Template_FixAccessRight (t, el, doc);
                    921:   TtaUpdateAccessRightInViews (doc, el);
                    922:   
1.1       vatton    923:   return cont;
1.23      kia       924: #else /* TEMPLATES */
                    925:   return NULL;
1.1       vatton    926: #endif /* TEMPLATES */
                    927: }
                    928: 
                    929: /*----------------------------------------------------------------------
1.22      kia       930:   InstantiateRepeat
                    931:   Check for min and max param and validate xt:repeat element content.
                    932:   @param registerUndo True to register undo creation sequences.
1.3       vatton    933:   ----------------------------------------------------------------------*/
1.46      vatton    934: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
                    935:                         ThotBool registerUndo)
1.1       vatton    936: {
                    937: #ifdef TEMPLATES
1.32      vatton    938:   Element        child, newChild;
1.52      vatton    939:   ElementType    newElType;
                    940:   Attribute      minAtt,  maxAtt;
                    941:   AttributeType  minType, maxType;
                    942:   char          *text, *types = NULL, *title = NULL;
                    943:   int            curVal, minVal,  maxVal;
1.32      vatton    944:   int            childrenCount;
                    945: 
1.1       vatton    946: 
1.25      vatton    947:   if (!t)
1.23      kia       948:     return;
                    949: 
1.1       vatton    950:   //Preparing types
1.52      vatton    951:   minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1       vatton    952:   minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52      vatton    953:   maxType.AttrSSchema =  minType.AttrSSchema;
1.1       vatton    954:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52      vatton    955:   newElType.ElSSchema = minType.AttrSSchema;
                    956:   //Get minOccurs and maxOccurs attributes
1.1       vatton    957:   minAtt = TtaGetAttribute (el, minType);
                    958:   maxAtt = TtaGetAttribute (el, maxType);
                    959:   //Get the values
                    960:   if (minAtt)
                    961:     {
1.10      kia       962:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    963:       if (text)
                    964:         {
                    965:           minVal = atoi(text);
                    966:           TtaFreeMemory(text);
1.52      vatton    967:           curVal = minVal;
1.1       vatton    968:         }
                    969:       else
                    970:         //Error : Attribute with no value
                    971:         return;
                    972:     }
                    973:   else
1.52      vatton    974:     {
                    975:       minVal = 0;
                    976:       curVal = 1;
                    977:     }
1.1       vatton    978: 
                    979:   if (maxAtt)
                    980:     {
1.10      kia       981:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton    982:       if (text)
                    983:         {
                    984:           if (!strcmp (text, "*"))
                    985:             maxVal = INT_MAX;
                    986:           else
                    987:             maxVal = atoi (text);
                    988:           TtaFreeMemory (text);
                    989:         }
                    990:       else
                    991:         //Error : Attribute with no value
                    992:         return;
                    993:     }
                    994:   else
                    995:     maxVal = INT_MAX;
                    996: 
                    997:   text = (char*)TtaGetMemory(MAX_LENGTH);
1.52      vatton    998:   //Create non existing min max attributes
                    999:   if (minAtt == NULL)
1.1       vatton   1000:     {      
1.32      vatton   1001:       minAtt = TtaNewAttribute (minType);
                   1002:       sprintf (text, "%d", minVal);
                   1003:       TtaAttachAttribute (el, minAtt, doc);
                   1004:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton   1005:       if (registerUndo)
1.32      vatton   1006:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton   1007:     }
                   1008: 
1.52      vatton   1009:   if (maxAtt == NULL)
1.1       vatton   1010:     {  
1.32      vatton   1011:       maxAtt = TtaNewAttribute (maxType);
                   1012:       if (maxVal < INT_MAX)
                   1013:         sprintf(text, "%d", maxVal);
1.1       vatton   1014:       else
1.32      vatton   1015:         sprintf (text, "*");
                   1016:       TtaAttachAttribute (el, maxAtt, doc);      
                   1017:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1018:       if (registerUndo)
1.32      vatton   1019:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton   1020:     }
1.52      vatton   1021:   TtaFreeMemory(text);
1.1       vatton   1022: 
1.52      vatton   1023:   //We must have minOccurs children
1.1       vatton   1024:   child = TtaGetFirstChild(el);
                   1025:   if (!child)
                   1026:     //Error : a repeat must have at least one child which will be the model
                   1027:     return;
                   1028:   
                   1029:   for(childrenCount = 0; child; TtaNextSibling(&child))
                   1030:     {
                   1031:       //TODO : Check that every child is valid
                   1032:       childrenCount ++;
                   1033:     }
                   1034: 
                   1035:   if (childrenCount > maxVal)
                   1036:     //Error : too many children!
                   1037:     return;  
                   1038: 
                   1039:   child = TtaGetLastChild(el);
1.32      vatton   1040:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1041:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1042:   newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton   1043:   while (childrenCount < curVal)
1.1       vatton   1044:     {
1.32      vatton   1045:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1046:       // Insert it
1.32      vatton   1047:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1048:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1049:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.34      vatton   1050:       InstantiateUse (t, newChild, doc, TRUE);
1.27      kia      1051:       
1.25      vatton   1052:       if (registerUndo)
1.34      vatton   1053:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1054:       child = newChild;
                   1055:       childrenCount++;
                   1056:     }
1.27      kia      1057:     
1.44      kia      1058:   Template_FixAccessRight (t, el, doc);
                   1059:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1060:   TtaFreeMemory (types);
                   1061:   TtaFreeMemory (title);
1.1       vatton   1062: #endif /* TEMPLATES */
                   1063: }
                   1064: 
                   1065: /*----------------------------------------------------------------------
                   1066:   ParseTemplate
1.3       vatton   1067:   ----------------------------------------------------------------------*/
1.1       vatton   1068: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                   1069:                            ThotBool loading)
                   1070: {
                   1071: #ifdef TEMPLATES
1.47      kia      1072:   AttributeType attType;
                   1073:   Attribute     att;
                   1074:   Element       aux, child; //Needed when deleting trees
                   1075:   char         *name;
                   1076:   ElementType   elType = TtaGetElementType (el);
                   1077: 
1.37      kia      1078:   if (!t || !el)
1.23      kia      1079:     return;
                   1080:   
1.61      kia      1081: //  static int off = 0;
                   1082: //  int i;
                   1083: //  off++;
                   1084: //  printf("ParseTemplate ");
                   1085: //  for(i=0; i<off; i++)
                   1086: //    printf(" ");
                   1087: //  DumpTemplateElement(el, doc);
                   1088: //  printf("\n");
                   1089:   
1.5       vatton   1090:   name = TtaGetSSchemaName (elType.ElSSchema);
1.47      kia      1091:   if (!strcmp (name, "Template"))
1.1       vatton   1092:     {
1.5       vatton   1093:       switch(elType.ElTypeNum)
1.1       vatton   1094:         {
                   1095:         case Template_EL_head :
                   1096:           //Remove it and all of its children
                   1097:           TtaDeleteTree(el, doc);
                   1098:           //We must stop searching into this tree
1.61      kia      1099: //          off--;
1.1       vatton   1100:           return;
                   1101:           break;
                   1102:         case Template_EL_component :
1.25      vatton   1103:           // remove the name attribute
1.5       vatton   1104:           attType.AttrSSchema = elType.ElSSchema;
1.1       vatton   1105:           attType.AttrTypeNum = Template_ATTR_name;
1.18      kia      1106:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);                                  
1.5       vatton   1107:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.25      vatton   1108:           // replace the component by a use
1.1       vatton   1109:           if (NeedAMenu (el, doc))
1.5       vatton   1110:             TtaChangeElementType (el, Template_EL_useEl);
1.1       vatton   1111:           else
1.5       vatton   1112:             TtaChangeElementType (el, Template_EL_useSimple);
1.25      vatton   1113:           // generate the types attribute
1.1       vatton   1114:           attType.AttrTypeNum = Template_ATTR_types;
1.5       vatton   1115:           att = TtaNewAttribute (attType);
                   1116:           TtaAttachAttribute (el, att, doc);
1.25      vatton   1117:           if (name)
1.18      kia      1118:             TtaSetAttributeText (att, name, el, doc);
1.25      vatton   1119:           // generate the title attribute
                   1120:           attType.AttrTypeNum = Template_ATTR_title;
                   1121:           att = TtaNewAttribute (attType);
                   1122:           TtaAttachAttribute (el, att, doc);
                   1123:           if (name)
                   1124:             TtaSetAttributeText (att, name, el, doc);
                   1125:           // generate the currentType attribute
1.1       vatton   1126:           attType.AttrTypeNum = Template_ATTR_currentType;
1.5       vatton   1127:           att = TtaNewAttribute (attType);
1.18      kia      1128:           TtaAttachAttribute (el, att, doc);
1.25      vatton   1129:           if (name)
1.18      kia      1130:             TtaSetAttributeText (att, name, el, doc);
1.28      kia      1131:           TtaFreeMemory(name);
1.1       vatton   1132:           break;
                   1133:         case Template_EL_option :
                   1134:           aux = NULL;
                   1135:           break;
                   1136:         case Template_EL_bag :
                   1137:           //Link to types
                   1138:           //Allow editing the content
                   1139:           break;
                   1140:         case Template_EL_useEl :
                   1141:         case Template_EL_useSimple :
                   1142:           /* if this use element is not empty, don't do anything: it is
                   1143:              supposed to contain a valid instance. This should be
                   1144:              checked, though */
1.46      vatton   1145:           if (DocumentMeta[doc] && DocumentMeta[doc]->isTemplate)
                   1146:             // add the initial indicator
                   1147:             AddPromptIndicator (el, doc);
                   1148:             
1.1       vatton   1149:           if (!TtaGetFirstChild (el))
1.27      kia      1150:             InstantiateUse (t, el, doc, FALSE);
1.1       vatton   1151:           break;
                   1152:         case Template_EL_attribute :
                   1153:           if (!loading)
                   1154:             InstantiateAttribute (t, el, doc);
                   1155:           break;
                   1156:         case Template_EL_repeat :
1.22      kia      1157:           InstantiateRepeat (t, el, doc, FALSE);
1.1       vatton   1158:           break;
                   1159:         default :
                   1160:           break;
                   1161:         }
                   1162:     }
1.47      kia      1163: 
                   1164:   child = TtaGetFirstChild (el);
                   1165:   while (child)
1.1       vatton   1166:     {
                   1167:       aux = child;
1.5       vatton   1168:       TtaNextSibling (&aux);
                   1169:       ParseTemplate (t, child, doc, loading);
1.1       vatton   1170:       child = aux;
                   1171:     }
1.61      kia      1172: //  off--;
1.1       vatton   1173: #endif /* TEMPLATES */
                   1174: }
                   1175: 
                   1176: /*----------------------------------------------------------------------
1.3       vatton   1177:   ----------------------------------------------------------------------*/
1.1       vatton   1178: void DoInstanceTemplate (char *templatename)
                   1179: {
                   1180: #ifdef TEMPLATES
1.47      kia      1181:   XTigerTemplate  t;
                   1182:   ElementType     elType;
1.65    ! vatton   1183:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.54      vatton   1184:   Document        doc;
1.47      kia      1185:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1186:   int             pi_type;
                   1187: 
1.47      kia      1188:   //Instantiate all elements
                   1189:   t = GetXTigerTemplate(templatename);
1.25      vatton   1190:   if (!t)
1.23      kia      1191:     return;
1.47      kia      1192: 
1.1       vatton   1193:   doc = GetTemplateDocument (t);
1.47      kia      1194:   root =       TtaGetMainRoot (doc);
                   1195:   ParseTemplate (t, root, doc, FALSE);
1.1       vatton   1196: 
                   1197:   //Look for PIs
                   1198:   /* check if the document has a DOCTYPE declaration */
                   1199: #ifdef ANNOTATIONS
                   1200:   if (DocumentTypes[doc]  == docAnnot)
                   1201:     elType = TtaGetElementType (root);
                   1202:   else
                   1203: #endif /* ANNOTATIONS */
                   1204:     elType = TtaGetElementType (root);
                   1205:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1206:   if (strcmp (s, "HTML") == 0)
                   1207:     {
                   1208:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1209:       pi_type = HTML_EL_XMLPI;
                   1210:     }
                   1211: #ifdef _SVG
                   1212:   else if (strcmp (s, "SVG") == 0)
                   1213:     {
                   1214:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1215:       pi_type = SVG_EL_XMLPI;
                   1216:     }
                   1217: #endif /* _SVG */
                   1218:   else if (strcmp (s, "MathML") == 0)
                   1219:     {
                   1220:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1221:       pi_type = MathML_EL_XMLPI;
                   1222:     }
                   1223:   else
                   1224:     {
                   1225:       elType.ElTypeNum = XML_EL_doctype;
                   1226:       pi_type = XML_EL_xmlpi;
                   1227:     }
1.54      vatton   1228: 
1.1       vatton   1229:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65    ! vatton   1230:   if (doctype == NULL)
1.1       vatton   1231:     {
1.65    ! vatton   1232:       elType.ElTypeNum = pi_type;      
        !          1233:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
        !          1234:       if (piElem == NULL)
        !          1235:         {
        !          1236:           /* generate the XML declaration */
        !          1237:           /* Check the Thot abstract tree against the structure schema. */
        !          1238:           TtaSetStructureChecking (FALSE, doc);
        !          1239:           piElem = TtaNewTree (doc, elType, "");
        !          1240:           TtaInsertFirstChild (&piElem, root, doc);
        !          1241:           line = TtaGetFirstChild (piElem);
        !          1242:           text = TtaGetFirstChild (line);
        !          1243:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
        !          1244:           charsetname = UpdateDocumentCharset (doc);
        !          1245:           strcat (buffer, charsetname);
        !          1246:           strcat (buffer, "\"");
        !          1247:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
        !          1248:           TtaSetStructureChecking (TRUE, doc);
        !          1249:           TtaFreeMemory(charsetname);
        !          1250:         }
1.1       vatton   1251:     }
                   1252:   
                   1253:   /* generate the XTiger PI */
                   1254:   /* Check the Thot abstract tree against the structure schema. */
                   1255:   TtaSetStructureChecking (FALSE, doc);
                   1256:   elType.ElTypeNum = pi_type;
1.65    ! vatton   1257:   elNew = TtaNewTree (doc, elType, "");
        !          1258:   if (doctype)
        !          1259:     TtaInsertSibling (elNew, doctype, FALSE, doc);
        !          1260:   else
        !          1261:     TtaInsertSibling (elNew, piElem, FALSE, doc);
        !          1262:   line = TtaGetFirstChild (elNew);
        !          1263:   text = TtaGetFirstChild (line);
1.1       vatton   1264:   strcpy (buffer, "xtiger template=\"");
                   1265:   strcat (buffer, templatename);
1.17      kia      1266:   strcat (buffer, "\" version=\"");
1.20      vatton   1267:   if (t->version)
                   1268:     strcat (buffer, t->version);
                   1269:   else
                   1270:     strcat (buffer, "0.8");
1.1       vatton   1271:   strcat (buffer, "\"");
1.25      vatton   1272:   if (t->templateVersion)
1.20      vatton   1273:     {
                   1274:       strcat (buffer, " templateVersion=\"");
                   1275:       strcat (buffer, t->templateVersion);
                   1276:       strcat (buffer, "\"");
                   1277:     }
1.1       vatton   1278:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1279:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1280: 
                   1281:   // update the document title
1.47      kia      1282:   if (!strcmp (s, "HTML"))
1.5       vatton   1283:     {
                   1284:       elType.ElTypeNum = HTML_EL_TITLE;
                   1285:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                   1286:       if (elFound)
                   1287:         {
                   1288:           elFound = TtaGetFirstChild (elFound);
                   1289:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                   1290:                              TtaGetDefaultLanguage (), doc);
                   1291:         }
                   1292:     }
1.1       vatton   1293: #endif /* TEMPLATES */
                   1294: }
                   1295: 
                   1296: /*----------------------------------------------------------------------
1.46      vatton   1297:   Template_PreInstantiateComponents: Instantiates all components in 
                   1298:   order to improve editing.
1.3       vatton   1299:   ----------------------------------------------------------------------*/
1.35      kia      1300: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1301: {
                   1302: #ifdef TEMPLATES 
1.25      vatton   1303:   if (!t)
1.23      kia      1304:     return;
                   1305: 
1.35      kia      1306:   HashMap         components = GetComponents(t);
                   1307:   ForwardIterator iter = HashMap_GetForwardIterator(components);
                   1308:   Declaration     comp;
                   1309:   HashMapNode     node;
1.47      kia      1310: 
1.35      kia      1311:   ITERATOR_FOREACH(iter, HashMapNode, node)
1.1       vatton   1312:     {
1.35      kia      1313:       comp = (Declaration) node->elem;
1.1       vatton   1314:       ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
                   1315:     }
1.35      kia      1316:   TtaFreeMemory(iter);
1.1       vatton   1317: #endif /* TEMPLATES */
                   1318: }

Webmaster