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

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

Webmaster