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

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

Webmaster