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

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

Webmaster