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

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

Webmaster