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

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

Webmaster