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

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

Webmaster