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

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

Webmaster