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

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

Webmaster