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

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:   Element           root, head, elem, xt, title, child, last;
                    356:   ElementType       elType, xtType;
1.64      kia       357:   char             *s;
1.63      kia       358:   SSchema           templSchema;
                    359:   XTigerTemplate    t;
                    360:   
                    361:   if(IsTemplateInstanceDocument(doc))
                    362:     {
                    363:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
                    364:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    365:       return;
                    366:     }
                    367: 
                    368:   if(IsTemplateDocument(doc))
                    369:     {
                    370:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
                    371:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    372:       return;
                    373:     }
                    374:   
1.64      kia       375:   root = TtaGetRootElement(doc);
                    376:   elType = TtaGetElementType (root);
                    377:   // get the target document type
                    378:   s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       379:   
1.64      kia       380:   TtaNewNature (doc, elType.ElSSchema,  NULL, "Template", "TemplateP");
                    381:   TtaSetANamespaceDeclaration(doc, root, "xt", Template_URI);
                    382:   templSchema = TtaGetSSchema("Template", doc);
                    383:   TtaSetUriSSchema(templSchema, Template_URI);
                    384: 
                    385:   // Insert xt:head and others
                    386:   TtaSetStructureChecking (FALSE, doc);
                    387:   if (strcmp (s, "HTML") == 0)
1.63      kia       388:     {
1.64      kia       389:       // Initialize the xt:head
                    390:       elType.ElTypeNum = HTML_EL_HEAD;
                    391:       head = TtaSearchTypedElement (elType, SearchInTree, root);
                    392:       if(head)
                    393:         {
                    394:           xtType.ElSSchema = templSchema;
                    395:           xtType.ElTypeNum = Template_EL_head;
                    396:           xt = TtaNewElement(doc, xtType);
                    397:           
                    398:           elem = TtaGetLastChild(head);
                    399:           if(elem)
                    400:               TtaInsertSibling(xt, elem, FALSE, doc);
                    401:           else
                    402:               TtaInsertFirstChild(&xt, head, doc);
                    403:           
                    404:           SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    405:           SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
                    406:         }
1.63      kia       407:       
1.64      kia       408:       // Initialize the document title
                    409:       elType.ElTypeNum = HTML_EL_TITLE;
                    410:       title = TtaSearchTypedElement (elType, SearchInTree, root);
                    411:       if(title)
1.63      kia       412:         {
1.64      kia       413:           // Create xt:use for title
                    414:           xtType.ElTypeNum = Template_EL_useSimple;
                    415:           xt = TtaNewElement(doc, xtType);
                    416:           TtaInsertFirstChild(&xt, title, doc);
                    417:           SetAttributeStringValue(xt, Template_ATTR_types, "string");
                    418:           SetAttributeStringValue(xt, Template_ATTR_title, "title");
                    419:           
                    420:           // Move current title content to xt:use
                    421:           last = NULL;
                    422:           while(child = TtaGetLastChild(title), child!=NULL)
1.63      kia       423:             {
1.64      kia       424:               if(child==xt)
                    425:                 break;
                    426:               TtaRemoveTree(child, doc);
                    427:               if(last)
                    428:                 TtaInsertSibling(child, last, FALSE, doc);
1.63      kia       429:               else
1.64      kia       430:                 TtaInsertFirstChild(&child, xt, doc);
                    431:               last = child;
1.63      kia       432:             }
                    433:         }
1.64      kia       434:     }
                    435:   else
                    436:     {
                    437:       xtType.ElSSchema = templSchema;
                    438:       xtType.ElTypeNum = Template_EL_head;
                    439:       xt = TtaNewElement(doc, xtType);
                    440:       TtaInsertFirstChild(&xt, root, doc);
                    441:       SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    442:       SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");      
                    443:     }
                    444:   // Save changes
                    445:   TtaSetStructureChecking (TRUE, doc);
                    446:   
                    447:   TtaClearUndoHistory (doc);
                    448:   RemoveParsingErrors (doc);
1.63      kia       449: 
1.64      kia       450:   TtaFreeMemory(DocumentURLs[doc]);
                    451:   DocumentURLs[doc] = TtaStrdup(templatePath);
                    452:   
                    453:   if(DocumentMeta[doc]==NULL)
                    454:     DocumentMeta[doc] = DocumentMetaDataAlloc();
                    455:   
                    456:   DocumentMeta[doc]->method = CE_TEMPLATE;
                    457:   if(DocumentMeta[doc]->initial_url)
                    458:     {
                    459:       TtaFreeMemory(DocumentMeta[doc]->initial_url);
                    460:       DocumentMeta[doc]->initial_url = NULL;
                    461:     }
                    462:   TtaSetDocumentModified (doc);
1.63      kia       463: 
1.64      kia       464:   // Load template-related infos :
                    465:   // like LoadTemplate(..)
                    466:   t = LookForXTigerTemplate(templatePath);
                    467:   t->doc = doc;
                    468:   Template_PrepareTemplate(t);
1.67      kia       469:   //  DocumentTypes[doc] = docTemplate;
                    470:   t->state |= templloaded|templTemplate;
1.63      kia       471: 
                    472: #ifdef AMAYA_DEBUG  
1.64      kia       473:     DumpAllDeclarations();
1.63      kia       474: #endif /* AMAYA_DEBUG */
1.64      kia       475:     
                    476:   /* Update the URL combo box */
                    477:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    478:   TtaSetTextZone (doc, 1, URL_list);
                    479:   /* Update template menus */
                    480:   UpdateTemplateMenus(doc);
                    481: 
1.63      kia       482: #endif /* TEMPLATES */  
                    483: }
                    484: 
1.1       vatton    485: /*----------------------------------------------------------------------
                    486:   CreateInstance
1.41      vatton    487:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    488:   ----------------------------------------------------------------------*/
1.71      vatton    489: void CreateInstance(char *templatePath, char *instancePath,
1.72      vatton    490:                     char *docname,  DocumentType docType, int basedoc)
1.3       vatton    491: {
1.1       vatton    492: #ifdef TEMPLATES
1.41      vatton    493:   Document          doc = 0, newdoc = 0;
                    494:   ElementType       elType;
                    495:   Element           root, title, text;
1.54      vatton    496:   CHARSET           charset;
1.71      vatton    497:   char             *s, *charsetname;
1.63      kia       498:   ThotBool          changes = FALSE;
1.1       vatton    499: 
1.35      kia       500:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    501:   if (t == NULL)
1.29      vatton    502:     {
                    503:       // the template cannot be loaded
                    504:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    505:       return;
                    506:     }
1.41      vatton    507:   // the template document
1.5       vatton    508:   doc = GetTemplateDocument (t);
1.41      vatton    509:   // localize the new created document
                    510:   if (DontReplaceOldDoc)
                    511:     newdoc = TtaGetNextDocumentIndex ();
                    512:   else
                    513:     newdoc = basedoc;
1.54      vatton    514: 
1.72      vatton    515:   // register the document type to open the right page model
                    516:   DocumentTypes[newdoc] = docType;
1.58      vatton    517:   if (!TtaHasUndoSequence (doc))
1.1       vatton    518:     {
1.65      vatton    519:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.14      vatton    520:       root = TtaGetRootElement(doc);
                    521:       elType = TtaGetElementType (root);
                    522:       // get the target document type
                    523:       s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       524:       // Do special stuff for HTML documents
1.14      vatton    525:       if (strcmp (s, "HTML") == 0)
1.1       vatton    526:         {
1.14      vatton    527:           // Initialize the document title
                    528:           elType.ElTypeNum = HTML_EL_TITLE;
                    529:           title = TtaSearchTypedElement (elType, SearchInTree, root);
                    530:           text = TtaGetFirstChild (title);
                    531:           while (text)
                    532:             {
                    533:               elType = TtaGetElementType (text);
                    534:               if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                    535:                 {
                    536:                   TtaSetTextContent (text, (unsigned char*)Answer_text,
                    537:                                      TtaGetDefaultLanguage (), doc);
                    538:                   text = NULL;
                    539:                 }
                    540:               else if ((elType.ElTypeNum == Template_EL_useEl ||
                    541:                         elType.ElTypeNum == Template_EL_useSimple) &&
                    542:                        !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    543:                 // Ignore the template use element
                    544:                 text = TtaGetFirstChild (text);
                    545:               else
                    546:                 // Look for the first text child
                    547:                 TtaNextSibling (&text);
                    548:             }
1.63      kia       549:         }
1.54      vatton    550: 
1.65      vatton    551:       // update the charset if needed
                    552:       charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    553:       charset = TtaGetCharset (charsetname);
                    554:       if (charset != UNDEFINED_CHARSET &&
                    555:           DocumentMeta[doc]->charset &&
                    556:           strcmp (charsetname, DocumentMeta[doc]->charset))
                    557:         {
                    558:           TtaSetDocumentCharset (doc, charset, FALSE);
                    559:           DocumentMeta[doc]->charset = TtaStrdup (charsetname);
1.83    ! vatton    560:           SetNamespacesAndDTD (doc, FALSE);
1.65      vatton    561:         }
1.80      kia       562: 
                    563:       // Parse template to fill structure and remove extra data
                    564:       ParseTemplate (t, root, doc, FALSE);
                    565:       
                    566:       // Insert XTiger PI
                    567:       Template_InsertXTigerPI(doc, t);
                    568:       
1.65      vatton    569:       // Save HTML special changes
                    570:       TtaCloseUndoSequence (doc);
                    571:       changes = TRUE;
                    572: 
1.63      kia       573:       // Save document
1.71      vatton    574:       SaveDocumentToNewDoc(doc, newdoc, instancePath);
1.63      kia       575: 
                    576:       // Revert HTML special changes
1.71      vatton    577:       if (changes)
1.63      kia       578:         TtaUndoNoRedo (doc);
                    579:       
1.1       vatton    580:       TtaClearUndoHistory (doc);
1.41      vatton    581:       RemoveParsingErrors (doc);
1.71      vatton    582:       // now load the document as an instance
1.41      vatton    583:       GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    584:                    !DontReplaceOldDoc, NULL, NULL);
                    585:       TtaSetDocumentModified (newdoc);
1.64      kia       586:       UpdateTemplateMenus(newdoc);
1.1       vatton    587:     }
                    588: #endif /* TEMPLATES */
                    589: }
                    590: 
                    591: 
                    592: #ifdef TEMPLATES
                    593: /*----------------------------------------------------------------------
                    594:   ProcessAttr
1.9       vatton    595:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    596:   ----------------------------------------------------------------------*/
1.1       vatton    597: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    598: {
                    599:   Element      child;
                    600:   ElementType  elType;
                    601: 
                    602:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    603:     {
                    604:       elType = TtaGetElementType (child);
                    605:       if (elType.ElTypeNum == Template_EL_attribute &&
                    606:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    607:         InstantiateAttribute (t, child, doc);
                    608:       else
                    609:         ProcessAttr (t, child, doc);
                    610:     }
                    611: }
                    612: #endif /* TEMPLATES */
                    613: 
1.12      kia       614: 
                    615: /*----------------------------------------------------------------------
                    616:   Template_GetNewSimpleTypeInstance
                    617:   Create an new instance of xt:use/SimpleType
                    618:   The decl attribute must embed SimpleType declaration (no validation).
                    619:   @param decl Declaration of new element
                    620:   @param parent Future parent element
                    621:   @param doc Document
                    622:   @return The new element
                    623:   ----------------------------------------------------------------------*/
                    624: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl)
                    625: {
                    626:   Element           newEl = NULL;
                    627: #ifdef TEMPLATES
                    628:   ElementType       elType;
1.73      kia       629:   const char       *empty = " ";
1.24      vatton    630: 
1.38      vatton    631:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       632:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    633:   newEl = TtaNewElement (doc, elType);
                    634:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    635: #endif /* TEMPLATES */
1.12      kia       636:   return newEl;
                    637: }
                    638: 
                    639: /*----------------------------------------------------------------------
                    640:   Template_GetNewXmlElementInstance
                    641:   Create an new instance of xt:use/XmlElement
                    642:   The decl attribute must embed XmlElement declaration (no validation).
                    643:   @param decl Declaration of new element
                    644:   @param parent Future parent element
                    645:   @param doc Document
                    646:   @return The new element
                    647:   ----------------------------------------------------------------------*/
                    648: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
                    649: {
                    650:   Element           newEl = NULL;
                    651: #ifdef TEMPLATES
                    652:   ElementType       elType;
                    653: 
1.24      vatton    654:   GIType (decl->name, &elType, doc);
                    655:   if (elType.ElTypeNum != 0)
1.12      kia       656:   {
1.23      kia       657:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       658:   }
                    659: #endif /* TEMPLATES */
                    660:   return newEl;
                    661: }
                    662: 
1.34      vatton    663: 
                    664: /*----------------------------------------------------------------------
                    665:   InsertWithNotify applies pre and post functions when inserting the new
                    666:   element el after child (if not NULL) or as first child of parent.
                    667:   ----------------------------------------------------------------------*/
                    668: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    669: {
                    670:   ElementType      elType;
                    671:   NotifyElement    event;
                    672:   char            *name;
                    673:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    674:   ThotBool         isImage = FALSE;
                    675:   ThotBool         oldStructureChecking;
                    676: 
                    677:   // avoid to check attributes now
                    678:   oldStructureChecking = TtaGetStructureChecking (doc);
                    679:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    680: 
                    681:   elType = TtaGetElementType (el);
                    682:   name = TtaGetSSchemaName (elType.ElSSchema);
                    683:   isCell = ((!strcmp (name,"HTML") &&
                    684:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    685:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    686:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    687:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    688:            (!strcmp (name,"MathML") &&
                    689:             (elType.ElTypeNum == MathML_EL_MTR ||
                    690:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    691:   isImage = (!strcmp (name,"HTML") && 
                    692:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    693:   if (child)
                    694:     TtaInsertSibling (el, child, FALSE, doc);
                    695:   else
                    696:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    697:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    698: 
1.50      vatton    699:   if (isImage)
                    700:     InsertImageOrObject (el, doc);
                    701:   else if (isCell)
1.34      vatton    702:     {
                    703:       // a cell is created
1.39      quint     704:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    705:     }
                    706:   else if (isRow)
                    707:     {
                    708:       // a row is created
                    709:       event.element = el;
                    710:       event.document = doc;
                    711:       RowPasted (&event);
                    712:     }
1.50      vatton    713:   
                    714:   if (!strcmp (name,"HTML"))
                    715:     {
                    716:       elType.ElTypeNum = HTML_EL_IMG;
                    717:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    718:       while (child)
                    719:         {
                    720:           InsertImageOrObject (child, doc);
                    721:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    722:         }
                    723:       elType.ElTypeNum = HTML_EL_Object;
                    724:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    725:       while (child)
                    726:         {
                    727:           InsertImageOrObject (child, doc);
                    728:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    729:         }
                    730:     }
1.34      vatton    731:   return el;
                    732: }
                    733: 
                    734: 
1.12      kia       735: /*----------------------------------------------------------------------
1.16      kia       736:   Template_InsertUseChildren
                    737:   Insert children to a xt:use
                    738:   The dec parameter must be valid and will not be verified. It must be a
                    739:     direct child element (for union elements).
                    740:   @param el element (xt:use) in which insert a new element
                    741:   @param dec Template declaration of the element to insert
                    742:   @return The inserted element (the xt:use element if insertion is multiple as component)
                    743:   ----------------------------------------------------------------------*/
                    744: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    745: {
1.34      vatton    746:   Element     newEl = NULL;
1.16      kia       747: #ifdef TEMPLATES
1.19      vatton    748:   Element     current = NULL;
1.34      vatton    749:   Element     child = NULL;
1.19      vatton    750:   //char       *attrCurrentTypeValue;
                    751:   //ElementType elType;
1.17      kia       752:   
1.25      vatton    753:   if (TtaGetDocumentAccessMode(doc))
1.16      kia       754:   {
1.23      kia       755:     switch (dec->nature)
                    756:     {
                    757:       case SimpleTypeNat:
                    758:         newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
1.34      vatton    759:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       760:         break;
                    761:       case XmlElementNat:
                    762:         newEl = Template_GetNewXmlElementInstance(doc, el, dec);
1.34      vatton    763:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       764:         break;
                    765:       case ComponentNat:
                    766:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74      kia       767:         ProcessAttr (dec->usedIn, newEl, doc);
                    768: 
1.23      kia       769:         /* Copy elements from new use to existing use. */
1.76      vatton    770: #ifdef AMAYA_DEBUG
1.74      kia       771:         DumpSubtree(newEl, doc, 0);
1.76      vatton    772: #endif /* AMAYA_DEBUG */
                    773:         child = TtaGetFirstChild(newEl);
                    774:         while (child)
                    775:           {
                    776:             // move the new subtree to the document
                    777:             TtaRemoveTree (child, doc);
                    778:             current = InsertWithNotify (child, current, el, doc);
                    779:             child = TtaGetFirstChild(newEl);
                    780:           }
1.23      kia       781:         
                    782:         /* Copy currentType attribute. */
                    783:         //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL);
                    784:         //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue);
                    785:         TtaDeleteTree(newEl, doc);
                    786:         newEl = el;
                    787:         break;
                    788:       default :
                    789:         //Impossible
                    790:         break;   
                    791:     }
1.44      kia       792:     Template_FixAccessRight (dec->usedIn, el, doc);
                    793:     TtaUpdateAccessRightInViews (doc, el);    
1.23      kia       794:   }  
1.16      kia       795: #endif /* TEMPLATES */
                    796:   return newEl;
                    797: }
                    798: 
1.40      kia       799: 
                    800: /*----------------------------------------------------------------------
                    801:   Fix access rights.
                    802:   ----------------------------------------------------------------------*/
1.41      vatton    803: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia       804: {
                    805: #ifdef TEMPLATES
                    806:   ElementType elType;
                    807:   Element     child;
                    808:   char        currentType[MAX_LENGTH];
                    809:   Declaration decl;
                    810:   
                    811:   if (t && el && doc)
                    812:     {
                    813:       elType = TtaGetElementType(el);
1.41      vatton    814:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia       815:         {
1.41      vatton    816:           switch (elType.ElTypeNum)
1.40      kia       817:             {
                    818:             case Template_EL_TEXT_UNIT:
1.41      vatton    819:               //TtaSetAccessRight( el, ReadWrite, doc);
                    820:               return;
1.40      kia       821:             case Template_EL_useEl:
                    822:             case Template_EL_useSimple:
                    823:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                    824:                                               (char*)currentType, NULL);
                    825:               decl = Template_GetDeclaration(t, currentType);
                    826:               if (decl)
                    827:                 {
                    828:                   switch (decl->nature)
                    829:                     {
                    830:                       case SimpleTypeNat:
                    831:                       case XmlElementNat:
1.41      vatton    832:                         TtaSetAccessRight (el, ReadWrite, doc);
                    833:                         return;
1.40      kia       834:                       default:
1.41      vatton    835:                         TtaSetAccessRight (el, ReadOnly, doc);
                    836:                          break;
1.40      kia       837:                     }
                    838:                 }
                    839:               break;
                    840:             case Template_EL_bag:
1.45      vatton    841:             case Template_EL_repeat:
1.40      kia       842:               TtaSetAccessRight(el, ReadWrite, doc);
                    843:               break;
                    844:             default:
                    845:               TtaSetAccessRight(el, ReadOnly, doc);
                    846:               break;
                    847:             }
                    848:         }
                    849: 
1.41      vatton    850:       child = TtaGetFirstChild (el);
                    851:       // fix access right to children
                    852:       while (child)
1.40      kia       853:         {
1.41      vatton    854:           Template_FixAccessRight (t, child, doc);
                    855:           TtaNextSibling (&child);
1.40      kia       856:         }
                    857:     }
                    858: #endif /* TEMPLATES */
                    859: }
                    860: 
1.16      kia       861: /*----------------------------------------------------------------------
1.46      vatton    862:   AddPromptIndicator
                    863:   ----------------------------------------------------------------------*/
                    864: void AddPromptIndicator (Element el, Document doc)
                    865: {
                    866: #ifdef TEMPLATES
                    867:   ElementType         elType;
                    868:   AttributeType       attrType;
                    869:   Attribute           att;
                    870: 
1.66      vatton    871:   if (el)
                    872:     {
                    873:       elType = TtaGetElementType (el);
                    874:       attrType.AttrSSchema = elType.ElSSchema;
                    875:       attrType.AttrTypeNum = Template_ATTR_prompt;
1.69      vatton    876:       att = TtaGetAttribute (el, attrType);
                    877:       if (att == NULL)
                    878:         {
                    879:           att = TtaNewAttribute (attrType);
                    880:           TtaAttachAttribute (el, att, doc);
                    881:         }
1.66      vatton    882:     }
1.46      vatton    883: #endif /* TEMPLATES */
                    884: }
                    885: 
                    886: /*----------------------------------------------------------------------
1.1       vatton    887:   InstantiateUse
1.3       vatton    888:   ----------------------------------------------------------------------*/
1.1       vatton    889: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.27      kia       890:                         ThotBool registerUndo)
1.1       vatton    891: {
                    892: #ifdef TEMPLATES
1.47      kia       893:   Element          cont = NULL;
1.1       vatton    894:   ElementType      elType;
                    895:   Declaration      dec;
1.28      kia       896:   int              size, nbitems, i;
1.1       vatton    897:   struct menuType  *items;
1.33      vatton    898:   char             *types, *text = NULL;
1.1       vatton    899:   ThotBool          oldStructureChecking;
                    900: 
1.25      vatton    901:   if (!t)
1.23      kia       902:     return NULL;
                    903: 
1.1       vatton    904:   /* get the value of the "types" attribute */
                    905:   cont = NULL;
1.12      kia       906:   elType = TtaGetElementType (el);
1.32      vatton    907:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton    908:   if (!types || types[0] == EOS)
                    909:     {
                    910:       TtaFreeMemory (types);
                    911:       return NULL;
                    912:     }
1.78      vatton    913:   if (!strcmp (types, "string"))
                    914:     AddPromptIndicator (el, doc);
                    915: 
1.1       vatton    916:   giveItems (types, size, &items, &nbitems);
                    917:   // No structure checking
                    918:   oldStructureChecking = TtaGetStructureChecking (doc);
                    919:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       920:   
1.1       vatton    921:   if (nbitems == 1)
                    922:     /* only one type in the "types" attribute */
                    923:     {
1.17      kia       924:       dec = Template_GetDeclaration (t, items[0].label);
1.1       vatton    925:       if (dec)
1.27      kia       926:       {
1.46      vatton    927:         cont = Template_InsertUseChildren (doc, el, dec);
1.32      vatton    928:         if (cont)
1.27      kia       929:         {
                    930:           TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1.32      vatton    931:           if (registerUndo)
                    932:             TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.27      kia       933:         }
                    934:       }
1.1       vatton    935:     }
1.33      vatton    936:   TtaFreeMemory (text);
1.32      vatton    937:   TtaFreeMemory (types);
1.28      kia       938:   
1.32      vatton    939:   for (i = 0; i < nbitems; i++)
1.28      kia       940:     TtaFreeMemory(items[i].label);
1.18      kia       941:   TtaFreeMemory(items);
1.1       vatton    942:   TtaSetStructureChecking (oldStructureChecking, doc);
1.40      kia       943:   
1.44      kia       944:   Template_FixAccessRight (t, el, doc);
                    945:   TtaUpdateAccessRightInViews (doc, el);
                    946:   
1.1       vatton    947:   return cont;
1.23      kia       948: #else /* TEMPLATES */
                    949:   return NULL;
1.1       vatton    950: #endif /* TEMPLATES */
                    951: }
                    952: 
                    953: /*----------------------------------------------------------------------
1.22      kia       954:   InstantiateRepeat
                    955:   Check for min and max param and validate xt:repeat element content.
                    956:   @param registerUndo True to register undo creation sequences.
1.3       vatton    957:   ----------------------------------------------------------------------*/
1.46      vatton    958: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
                    959:                         ThotBool registerUndo)
1.1       vatton    960: {
                    961: #ifdef TEMPLATES
1.32      vatton    962:   Element        child, newChild;
1.52      vatton    963:   ElementType    newElType;
                    964:   Attribute      minAtt,  maxAtt;
                    965:   AttributeType  minType, maxType;
                    966:   char          *text, *types = NULL, *title = NULL;
                    967:   int            curVal, minVal,  maxVal;
1.32      vatton    968:   int            childrenCount;
                    969: 
1.1       vatton    970: 
1.25      vatton    971:   if (!t)
1.23      kia       972:     return;
                    973: 
1.1       vatton    974:   //Preparing types
1.52      vatton    975:   minType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.1       vatton    976:   minType.AttrTypeNum = Template_ATTR_minOccurs;
1.52      vatton    977:   maxType.AttrSSchema =  minType.AttrSSchema;
1.1       vatton    978:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.52      vatton    979:   newElType.ElSSchema = minType.AttrSSchema;
                    980:   //Get minOccurs and maxOccurs attributes
1.1       vatton    981:   minAtt = TtaGetAttribute (el, minType);
                    982:   maxAtt = TtaGetAttribute (el, maxType);
                    983:   //Get the values
                    984:   if (minAtt)
                    985:     {
1.10      kia       986:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    987:       if (text)
                    988:         {
                    989:           minVal = atoi(text);
                    990:           TtaFreeMemory(text);
1.52      vatton    991:           curVal = minVal;
1.1       vatton    992:         }
                    993:       else
                    994:         //Error : Attribute with no value
                    995:         return;
                    996:     }
                    997:   else
1.52      vatton    998:     {
                    999:       minVal = 0;
                   1000:       curVal = 1;
                   1001:     }
1.1       vatton   1002: 
                   1003:   if (maxAtt)
                   1004:     {
1.10      kia      1005:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton   1006:       if (text)
                   1007:         {
                   1008:           if (!strcmp (text, "*"))
                   1009:             maxVal = INT_MAX;
                   1010:           else
                   1011:             maxVal = atoi (text);
                   1012:           TtaFreeMemory (text);
                   1013:         }
                   1014:       else
                   1015:         //Error : Attribute with no value
                   1016:         return;
                   1017:     }
                   1018:   else
                   1019:     maxVal = INT_MAX;
                   1020: 
                   1021:   text = (char*)TtaGetMemory(MAX_LENGTH);
1.52      vatton   1022:   //Create non existing min max attributes
                   1023:   if (minAtt == NULL)
1.1       vatton   1024:     {      
1.32      vatton   1025:       minAtt = TtaNewAttribute (minType);
                   1026:       sprintf (text, "%d", minVal);
                   1027:       TtaAttachAttribute (el, minAtt, doc);
                   1028:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton   1029:       if (registerUndo)
1.32      vatton   1030:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton   1031:     }
                   1032: 
1.52      vatton   1033:   if (maxAtt == NULL)
1.1       vatton   1034:     {  
1.32      vatton   1035:       maxAtt = TtaNewAttribute (maxType);
                   1036:       if (maxVal < INT_MAX)
                   1037:         sprintf(text, "%d", maxVal);
1.1       vatton   1038:       else
1.32      vatton   1039:         sprintf (text, "*");
                   1040:       TtaAttachAttribute (el, maxAtt, doc);      
                   1041:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1042:       if (registerUndo)
1.32      vatton   1043:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton   1044:     }
1.52      vatton   1045:   TtaFreeMemory(text);
1.1       vatton   1046: 
1.52      vatton   1047:   //We must have minOccurs children
1.1       vatton   1048:   child = TtaGetFirstChild(el);
                   1049:   if (!child)
                   1050:     //Error : a repeat must have at least one child which will be the model
                   1051:     return;
                   1052:   
                   1053:   for(childrenCount = 0; child; TtaNextSibling(&child))
                   1054:     {
                   1055:       //TODO : Check that every child is valid
                   1056:       childrenCount ++;
                   1057:     }
                   1058: 
                   1059:   if (childrenCount > maxVal)
                   1060:     //Error : too many children!
                   1061:     return;  
                   1062: 
                   1063:   child = TtaGetLastChild(el);
1.32      vatton   1064:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1065:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1066:   newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton   1067:   while (childrenCount < curVal)
1.1       vatton   1068:     {
1.32      vatton   1069:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1070:       // Insert it
1.32      vatton   1071:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1072:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1073:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.34      vatton   1074:       InstantiateUse (t, newChild, doc, TRUE);
1.27      kia      1075:       
1.25      vatton   1076:       if (registerUndo)
1.34      vatton   1077:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1078:       child = newChild;
                   1079:       childrenCount++;
                   1080:     }
1.27      kia      1081:     
1.44      kia      1082:   Template_FixAccessRight (t, el, doc);
                   1083:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1084:   TtaFreeMemory (types);
                   1085:   TtaFreeMemory (title);
1.1       vatton   1086: #endif /* TEMPLATES */
                   1087: }
                   1088: 
                   1089: /*----------------------------------------------------------------------
1.80      kia      1090:   Template_InsertXTigerPI
                   1091:   Insert the XTiger PI element in template instance.
                   1092:   Param t is the XTigerTemplate structure of the template,
                   1093:   not the template instance one.
1.3       vatton   1094:   ----------------------------------------------------------------------*/
1.80      kia      1095: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1       vatton   1096: {
                   1097: #ifdef TEMPLATES
1.47      kia      1098:   ElementType     elType;
1.65      vatton   1099:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.47      kia      1100:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1101:   int             pi_type;
                   1102: 
1.80      kia      1103:   if (!t || !doc)
1.23      kia      1104:     return;
1.47      kia      1105: 
1.80      kia      1106:   root =  TtaGetMainRoot (doc);
1.1       vatton   1107: 
                   1108:   //Look for PIs
                   1109:   /* check if the document has a DOCTYPE declaration */
                   1110: #ifdef ANNOTATIONS
                   1111:   if (DocumentTypes[doc]  == docAnnot)
                   1112:     elType = TtaGetElementType (root);
                   1113:   else
                   1114: #endif /* ANNOTATIONS */
                   1115:     elType = TtaGetElementType (root);
                   1116:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1117:   if (strcmp (s, "HTML") == 0)
                   1118:     {
                   1119:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1120:       pi_type = HTML_EL_XMLPI;
                   1121:     }
                   1122: #ifdef _SVG
                   1123:   else if (strcmp (s, "SVG") == 0)
                   1124:     {
                   1125:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1126:       pi_type = SVG_EL_XMLPI;
                   1127:     }
                   1128: #endif /* _SVG */
                   1129:   else if (strcmp (s, "MathML") == 0)
                   1130:     {
                   1131:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1132:       pi_type = MathML_EL_XMLPI;
                   1133:     }
                   1134:   else
                   1135:     {
                   1136:       elType.ElTypeNum = XML_EL_doctype;
                   1137:       pi_type = XML_EL_xmlpi;
                   1138:     }
1.54      vatton   1139: 
1.1       vatton   1140:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1141:   if (doctype == NULL)
1.1       vatton   1142:     {
1.65      vatton   1143:       elType.ElTypeNum = pi_type;      
                   1144:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1145:       if (piElem == NULL)
                   1146:         {
                   1147:           /* generate the XML declaration */
                   1148:           /* Check the Thot abstract tree against the structure schema. */
                   1149:           TtaSetStructureChecking (FALSE, doc);
                   1150:           piElem = TtaNewTree (doc, elType, "");
                   1151:           TtaInsertFirstChild (&piElem, root, doc);
                   1152:           line = TtaGetFirstChild (piElem);
                   1153:           text = TtaGetFirstChild (line);
                   1154:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1155:           charsetname = UpdateDocumentCharset (doc);
                   1156:           strcat (buffer, charsetname);
                   1157:           strcat (buffer, "\"");
                   1158:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1159:           TtaSetStructureChecking (TRUE, doc);
                   1160:           TtaFreeMemory(charsetname);
                   1161:         }
1.1       vatton   1162:     }
                   1163:   
                   1164:   /* generate the XTiger PI */
                   1165:   /* Check the Thot abstract tree against the structure schema. */
                   1166:   TtaSetStructureChecking (FALSE, doc);
                   1167:   elType.ElTypeNum = pi_type;
1.65      vatton   1168:   elNew = TtaNewTree (doc, elType, "");
                   1169:   if (doctype)
                   1170:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1171:   else
                   1172:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1173:   line = TtaGetFirstChild (elNew);
                   1174:   text = TtaGetFirstChild (line);
1.1       vatton   1175:   strcpy (buffer, "xtiger template=\"");
1.80      kia      1176:   strcat (buffer, DocumentURLs[doc]);
1.17      kia      1177:   strcat (buffer, "\" version=\"");
1.20      vatton   1178:   if (t->version)
                   1179:     strcat (buffer, t->version);
                   1180:   else
                   1181:     strcat (buffer, "0.8");
1.1       vatton   1182:   strcat (buffer, "\"");
1.25      vatton   1183:   if (t->templateVersion)
1.20      vatton   1184:     {
                   1185:       strcat (buffer, " templateVersion=\"");
                   1186:       strcat (buffer, t->templateVersion);
                   1187:       strcat (buffer, "\"");
                   1188:     }
1.1       vatton   1189:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1190:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1191: 
                   1192:   // update the document title
1.47      kia      1193:   if (!strcmp (s, "HTML"))
1.5       vatton   1194:     {
                   1195:       elType.ElTypeNum = HTML_EL_TITLE;
                   1196:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                   1197:       if (elFound)
                   1198:         {
                   1199:           elFound = TtaGetFirstChild (elFound);
                   1200:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                   1201:                              TtaGetDefaultLanguage (), doc);
                   1202:         }
                   1203:     }
1.1       vatton   1204: #endif /* TEMPLATES */
                   1205: }
                   1206: 
1.80      kia      1207: 
1.1       vatton   1208: /*----------------------------------------------------------------------
1.68      kia      1209:   Template_PreInstantiateComponents
                   1210:   Instantiates all components in order to improve editing.
1.3       vatton   1211:   ----------------------------------------------------------------------*/
1.35      kia      1212: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1213: {
                   1214: #ifdef TEMPLATES 
1.25      vatton   1215:   if (!t)
1.23      kia      1216:     return;
                   1217: 
1.76      vatton   1218: #ifdef AMAYA_DEBUG
1.74      kia      1219:   DumpAllDeclarations();
1.76      vatton   1220: #endif /* AMAYA_DEBUG */  
1.68      kia      1221:   ForwardIterator iter = SearchSet_GetForwardIterator(GetComponents(t));
                   1222:   Declaration     dec;
                   1223:   SearchSetNode   node;
1.47      kia      1224: 
1.76      vatton   1225: #ifdef AMAYA_DEBUG
1.74      kia      1226:   printf("Template_PreInstantiateComponents %s\n", t->uri);
1.76      vatton   1227: #endif /* AMAYA_DEBUG */  
1.68      kia      1228:   ITERATOR_FOREACH(iter, SearchSetNode, node)
1.1       vatton   1229:     {
1.68      kia      1230:       dec = (Declaration) node->elem;
                   1231:       ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), TRUE);
1.1       vatton   1232:     }
1.35      kia      1233:   TtaFreeMemory(iter);
1.1       vatton   1234: #endif /* TEMPLATES */
                   1235: }

Webmaster