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

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

Webmaster