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

1.19      vatton      1: /*
                      2:  *
1.104     vatton      3:  *  COPYRIGHT INRIA and W3C, 2006-2009
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.94      vatton     31: #include "fetchXMLname_f.h"
1.86      vatton     32: #include "styleparser_f.h"
1.94      vatton     33: 
1.1       vatton     34: #ifdef TEMPLATES
                     35: #define TEMPLATE_SCHEMA_NAME "Template"
                     36: #endif /* TEMPLATES */
                     37: 
                     38: 
1.123     vatton     39: /*----------------------------------------------------------------------
                     40:   Template_FillEmpty removes current children and generates an empty
                     41:   child.
                     42:   The parameter el must be a use element.
                     43:   Returns a pointer to the empty child.
                     44:   ----------------------------------------------------------------------*/
                     45: Element Template_FillEmpty (Element el, Document doc, ThotBool withUndo)
                     46: {
                     47: #ifdef TEMPLATES
                     48:   Element         child, next, parent;
                     49:   ElementType     elType, childType, parentType;
                     50: 
                     51:   // generate a content
                     52:   child = TtaGetFirstChild (el);
                     53:   elType = TtaGetElementType (el);
                     54:   if (child)
                     55:     {
                     56:       childType = TtaGetElementType (child);
                     57:       if (childType.ElSSchema == elType.ElSSchema &&
                     58:           childType.ElTypeNum == Template_EL_TemplateObject)
                     59:         // already empty
                     60:         return child;
                     61:       // remove current children
                     62:       do
                     63:         {
                     64:           next = child;
                     65:           TtaNextSibling (&next);
                     66:           if (withUndo)
                     67:             TtaRegisterElementDelete(child, doc);
                     68:           TtaDeleteTree (child, doc);
                     69:           child = next;
                     70:         }
                     71:       while (next);
                     72:       child = NULL;
                     73:     }
                     74:   if (child == NULL)
                     75:     {
                     76:       childType.ElTypeNum = Template_EL_TemplateObject;
                     77:       childType.ElSSchema = elType.ElSSchema;
                     78:       child = TtaNewElement (doc, childType);
                     79:       TtaInsertFirstChild (&child, el, doc);
                     80:       TtaSetAccessRight (child, ReadOnly, doc);
                     81:       if (withUndo)
                     82:         TtaRegisterElementCreate (child, doc);
                     83:     }
                     84: 
                     85:   if (elType.ElTypeNum == Template_EL_useSimple)
                     86:     {
                     87:       parent = TtaGetParent (el);
                     88:       parentType = TtaGetElementType (parent);
                     89:       if (parentType.ElSSchema == elType.ElSSchema &&
                     90:           parentType.ElTypeNum == Template_EL_repeat)
                     91:         {
                     92:           // mark the element as un empty element
                     93:           TtaChangeTypeOfElement (el, doc, Template_EL_useEl);
                     94:           TtaRegisterElementTypeChange (el, Template_EL_useSimple, doc);
                     95:         }
                     96:     }
                     97:   return child;
                     98: #endif /* TEMPLATES */
                     99:   return NULL;
                    100: }
                    101: 
                    102: 
                    103: /*----------------------------------------------------------------------
                    104:   CleanUpRepeat cleans up a repeat
                    105:   ----------------------------------------------------------------------*/
                    106: void CleanUpRepeat (Element el, Document doc, ThotBool withUndo)
                    107: {
                    108: #ifdef TEMPLATES
                    109:   Element        child, next, parent;
                    110:   int            minVal, count = 0;
                    111:   XTigerTemplate t;
                    112: 
                    113:   minVal = GetMinOccurence (el, doc);
                    114:   child =  TtaGetFirstChild (el);
                    115:   if (minVal == 0)
                    116:     {
                    117:       // clean up but keep the first use
                    118:       Template_FillEmpty (child, doc, withUndo);
                    119:       TtaNextSibling (&child);
                    120:     }
                    121:   // remove other children
                    122:   while (child)
                    123:     {
                    124:       next = child;
                    125:       TtaNextSibling (&next);
                    126:       count++;
                    127:       if (count > minVal)
                    128:         {
                    129:           if (withUndo)
                    130:             TtaRegisterElementDelete(child, doc);
                    131:           TtaDeleteTree (child, doc);
                    132:         }
                    133:       child = next;
                    134:     }
                    135:   // regenerate the minimum of children
                    136:   parent = GetParentLine (el, TtaGetElementType (el).ElSSchema);
                    137:   t = GetXTigerDocTemplate(doc);
                    138:   InstantiateRepeat (t, el, doc, parent, withUndo, TRUE);
                    139: #endif /* TEMPLATES */
                    140: }
1.49      vatton    141: 
                    142: /*----------------------------------------------------------------------
                    143:   Template_InsertRepeatChildAfter
                    144:   Insert a child to a xt:repeat
                    145:   The decl parameter must be valid and will not be verified. It must be a
                    146:     direct child element or the "use in the use" for union elements.
                    147:   @param el element (xt:repeat) in which insert a new element
                    148:   @param decl Template declaration of the element to insert
                    149:   @param elPrev Element (xt:use) after which insert the new elem, NULL if first.
                    150:   @return The inserted element 
                    151:   ----------------------------------------------------------------------*/
                    152: Element Template_InsertRepeatChildAfter (Document doc, Element el,
                    153:                                          Declaration decl, Element elPrev)
                    154: {
                    155: #ifdef TEMPLATES
1.123     vatton    156:   Element     child;
                    157:   Element     use, parent;
                    158:   ElementType useType;
1.102     vatton    159:   char       *types = NULL;
1.118     vatton    160:   ThotBool    isInstance;
1.49      vatton    161: 
                    162:   if (!TtaGetDocumentAccessMode (doc))
                    163:     return NULL;
1.56      kia       164:   
1.49      vatton    165:   /* Copy xt:use with xt:types param */
1.104     vatton    166:   child = TtaGetFirstChild (el);
                    167:   useType = TtaGetElementType (child);
                    168:   use = TtaCopyElement (child, doc, doc, el);
1.123     vatton    169:   isInstance = IsTemplateInstanceDocument (doc);
                    170: 
1.104     vatton    171:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
1.118     vatton    172:   if (isInstance && useType.ElTypeNum != Template_EL_useSimple)
1.123     vatton    173:     // insert an instantiate use
1.104     vatton    174:     TtaChangeElementType (use, Template_EL_useSimple);
1.102     vatton    175:   if (types)
                    176:     {
                    177:       SetAttributeStringValueWithUndo (use, Template_ATTR_types, types);
                    178:       TtaFreeMemory (types);
                    179:     }
                    180:   else
                    181:     SetAttributeStringValueWithUndo (use, Template_ATTR_types, decl->name);
1.123     vatton    182: 
1.49      vatton    183:   /* insert it */
                    184:   if (elPrev)
1.104     vatton    185:     TtaInsertSibling (use, elPrev, FALSE, doc);
1.49      vatton    186:   else
1.104     vatton    187:     TtaInsertSibling (use, child, TRUE, doc);
                    188:   // look for the enclosing target element
                    189:   parent = GetParentLine (use, useType.ElSSchema);
1.118     vatton    190:   if (isInstance)
                    191:     Template_InsertUseChildren (doc, use, decl, parent, TRUE);
                    192:   else
                    193:     {
1.123     vatton    194:       child = Template_FillEmpty (use,doc, FALSE);
1.118     vatton    195:       TtaSelectElement (doc, child);
                    196:     }
1.102     vatton    197:   SetAttributeStringValueWithUndo (use, Template_ATTR_title, decl->name);
                    198:   SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, decl->name);
1.49      vatton    199:   TtaRegisterElementCreate (use, doc);
1.123     vatton    200: 
                    201:   if (isInstance && useType.ElTypeNum != Template_EL_useSimple)
                    202:     {
                    203:       // delete the first empty use element
                    204:       TtaRegisterElementDelete(child, doc);
                    205:       TtaDeleteTree (child, doc);
                    206:     }
                    207:   // add needed prompt attributes
                    208:   AddPromptIndicatorInSubtree (use, doc);
1.49      vatton    209:   return use;
                    210: #else /* TEMPLATES */
                    211:   return NULL;
                    212: #endif /* TEMPLATES */
                    213: }
                    214: 
                    215: /*----------------------------------------------------------------------
                    216:   Template_InsertBagChild
                    217:   Insert a child to a xt:bag at the current insertion point.
                    218:   The decl parameter must be valid and will not be verified.
1.102     vatton    219:   @param sel the refered element. If NULL use the selection
                    220:   @param bag element (xt:bag) in which insert a new element
1.49      vatton    221:   @param decl Template declaration of the element to insert
                    222:   @return The inserted element
                    223:   ----------------------------------------------------------------------*/
1.102     vatton    224: Element Template_InsertBagChild (Document doc, Element sel, Element bag,
                    225:                                  Declaration decl, ThotBool before)
1.49      vatton    226: {
                    227: #ifdef TEMPLATES
                    228:   ElementType newElType, selType;
1.119     vatton    229:   Element     use = NULL, el;
1.102     vatton    230:   SSchema     sstempl;
                    231:   int         start, end;
                    232:   ThotBool    open;
1.49      vatton    233: 
                    234:   if (!TtaGetDocumentAccessMode (doc) || !decl)
                    235:     return NULL;
                    236: 
1.102     vatton    237:   TtaGiveFirstSelectedElement (doc, &el, &start, &end);
                    238:   if (sel == NULL)
                    239:     sel = el;
                    240:   if (sel == bag || TtaIsAncestor (sel, bag))
                    241:     {
                    242:       // opent the undo sequence if needed
                    243:       open = TtaHasUndoSequence (doc);
                    244:       if (!open)
                    245:         TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
                    246:       sstempl = TtaGetSSchema ("Template", doc);
                    247:       selType = TtaGetElementType (sel);
                    248:       if (decl->blockLevel == 2 && 
                    249:           (TtaIsLeaf (selType) || !IsTemplateElement (sel)))
                    250:         {
                    251:           // force the insertion of a block level element at the right position
                    252:           while (sel && IsCharacterLevelElement (sel))
                    253:             sel = TtaGetParent (sel);
                    254:           if (sel)
                    255:             TtaSelectElement (doc, sel);
                    256:         }
                    257: 
                    258:       if (decl->nature == XmlElementNat)
                    259:         {
                    260:           if (el == NULL && sel != bag)
                    261:             // force a selection
                    262:             TtaSelectElement (doc, sel);
1.119     vatton    263:           GIType (decl->name, &newElType, doc);
                    264:           el = TtaNewTree (doc, newElType, "");
1.102     vatton    265:           if (sel == bag)
                    266:             {
                    267:               // insert first an empty element
1.119     vatton    268:               TtaInsertFirstChild (&el, bag, doc);
                    269:               TtaRegisterElementCreate (el, doc);
                    270:               sel = TtaGetFirstChild (el);
                    271:               if (sel == NULL)
                    272:                 sel = el;
                    273:               TtaSelectElement (doc, sel);
1.102     vatton    274:             }
1.119     vatton    275:           else
1.102     vatton    276:             {
1.119     vatton    277:               // insert the new element before or after
                    278:               TtaInsertSibling (el, sel, before, doc);
                    279:               TtaRegisterElementCreate (el, doc);
                    280:               sel = TtaGetFirstChild (el);
                    281:               if (sel == NULL)
                    282:                 sel = el;
                    283:               TtaSelectElement (doc, sel);
1.102     vatton    284:             }
1.119     vatton    285:           //TtaGiveFirstSelectedElement (doc, &sel, &start, &end);
1.102     vatton    286:         }
                    287:       else if (decl->nature == ComponentNat)
                    288:         {
                    289:           // create a use element
                    290:           newElType.ElTypeNum = Template_EL_useSimple;
                    291:           newElType.ElSSchema = sstempl;
                    292:           use = TtaNewElement(doc, newElType);
                    293:           if (use)
                    294:             {
1.104     vatton    295:               Template_InsertUseChildren (doc, use, decl, NULL, TRUE);
1.102     vatton    296:               if (sel != bag)
                    297:                 TtaInsertSibling (use, sel, before, doc);
                    298:               else
                    299:                  TtaInsertFirstChild (&use, bag, doc);
1.110     vatton    300:               SetAttributeStringValueWithUndo (use, Template_ATTR_types, decl->name);
                    301:               SetAttributeStringValueWithUndo (use, Template_ATTR_title, decl->name);
                    302:               SetAttributeStringValueWithUndo (use, Template_ATTR_currentType, decl->name);
1.102     vatton    303:               TtaRegisterElementCreate (use, doc);
1.105     vatton    304:               
1.102     vatton    305:               sel = use;
                    306:             }
                    307:         }
                    308:       else if (decl->nature == UnionNat)
                    309:         {
                    310:           newElType.ElTypeNum = Template_EL_useEl;
                    311:           newElType.ElSSchema = sstempl;
                    312:         }
                    313:       // close the undo sequence
                    314:       if (!open)
                    315:         TtaCloseUndoSequence (doc);
                    316:       return sel;
1.56      kia       317:     }
1.49      vatton    318: #endif /* TEMPLATES */
                    319:   return NULL;
                    320: }
                    321: 
1.80      kia       322: /*----------------------------------------------------------------------
                    323:   InstantiateAttribute
                    324:   ----------------------------------------------------------------------*/
                    325: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
                    326: {
                    327: #ifdef TEMPLATES
                    328:   AttributeType  useType, nameType, defaultType, attrType;
                    329:   Attribute      useAttr, nameAttr, defAttr, attr;
                    330:   ElementType    elType;
                    331:   Element        parent;
                    332:   char           *text, *elementName;
                    333:   ThotBool       level;
                    334:   NotifyAttribute event;
                    335:   int             val;
                    336: 
                    337:   parent = TtaGetParent (el);
                    338:   if (!parent)
                    339:     return;
                    340:   // if attribute "use" has value "optional", don't do anything
                    341:   useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    342:   useType.AttrTypeNum = Template_ATTR_useAt;
                    343:   useAttr = TtaGetAttribute (el, useType);
                    344:   if (useAttr)
                    345:     // there is a "use" attribute. Check its value
                    346:     {
                    347:       val = TtaGetAttributeValue(useAttr);
                    348:       if (val == Template_ATTR_useAt_VAL_optional)
                    349:       {
                    350:         return;
                    351:       }
                    352:     }
                    353:     
                    354:   // get the "name" and "default" attributes
                    355:   nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    356:   nameType.AttrTypeNum = Template_ATTR_ref_name;
                    357:   defaultType.AttrTypeNum = Template_ATTR_defaultAt;
                    358:   nameAttr = TtaGetAttribute (el, nameType);
                    359:   defAttr = TtaGetAttribute (el, defaultType);
                    360:   if (nameAttr)
                    361:     {
                    362:       text = GetAttributeStringValue (el, nameAttr, NULL);
                    363:       if (text)
                    364:         {
                    365:           elType = TtaGetElementType (parent);
                    366:           elementName = TtaGetElementTypeName (elType);
                    367:           level = TRUE;
                    368:           MapHTMLAttribute (text, &attrType, elementName, &level, doc);
                    369:           TtaFreeMemory(text);
                    370:           attr = TtaNewAttribute (attrType);
                    371:           if (attr)
                    372:             {
                    373:               TtaAttachAttribute (parent, attr, doc);
                    374:               if (defAttr)
                    375:                 {
                    376:                   text = GetAttributeStringValue (el, defAttr, NULL);
                    377:                   if (text)
1.116     vatton    378:                     {
                    379:                       TtaSetAttributeText(attr, text, parent, doc);
                    380:                       TtaFreeMemory(text);
                    381:                     }
                    382:                   else if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
                    383:                            // if it's a src arttribute for an image, load the image
                    384:                            elType.ElTypeNum == HTML_EL_IMG)
1.80      kia       385:                     if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
                    386:                         attrType.AttrSSchema == elType.ElSSchema)
                    387:                       {
                    388:                         event.document = doc;
                    389:                         event.element = parent;
                    390:                         event.attribute = attr;
                    391:                         SRCattrModified (&event);
                    392:                       }
                    393:                 }
                    394:             }
                    395:         }
                    396:     }
                    397: #endif /* TEMPLATES */
                    398: }
                    399: 
                    400: /*----------------------------------------------------------------------
                    401:   ParseTemplate
1.92      vatton    402:   parentLine points to the enclosing pseudo paragraph or paragraph
1.99      vatton    403:   Parameter loading is TRUE when the document is not already loaded.
1.92      vatton    404:   Return the parentline to be considered for next elements
1.80      kia       405:   ----------------------------------------------------------------------*/
1.113     vatton    406: Element ParseTemplate (XTigerTemplate t, Element el, Document doc,
                    407:                        Element parentLine, ThotBool loading)
1.80      kia       408: {
                    409: #ifdef TEMPLATES
                    410:   AttributeType attType;
                    411:   Attribute     att;
1.101     vatton    412:   Element       next, child = NULL, savedInline, prev, parent = NULL;
1.126   ! vatton    413:   ElementType   elType, otherType, parentType;
1.92      vatton    414:   Declaration   dec;
1.94      vatton    415:   char         *name, *types;
1.80      kia       416: 
                    417:   if (!t || !el)
1.92      vatton    418:     return parentLine;
                    419: 
                    420:   savedInline = parentLine;
                    421:   elType = TtaGetElementType (el);
1.94      vatton    422:   attType.AttrSSchema = elType.ElSSchema;
1.80      kia       423:   name = TtaGetSSchemaName (elType.ElSSchema);
                    424:   if (!strcmp (name, "Template"))
                    425:     {
1.92      vatton    426:       switch (elType.ElTypeNum)
1.80      kia       427:         {
                    428:         case Template_EL_head :
                    429:           //Remove it and all of its children
                    430:           TtaDeleteTree(el, doc);
                    431:           //We must stop searching into this tree
1.92      vatton    432:           return parentLine;
1.80      kia       433:         case Template_EL_component :
                    434:           // remove the name attribute
                    435:           attType.AttrTypeNum = Template_ATTR_name;
1.87      vatton    436:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);
1.80      kia       437:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
                    438:           // replace the component by a use
1.93      vatton    439:           prev = el;
                    440:           TtaPreviousSibling (&prev);
                    441:           if (prev == NULL)
                    442:             {
                    443:               next = el;
                    444:               TtaNextSibling (&next);
                    445:               if (next == NULL)
                    446:                 parent = TtaGetParent (el);
                    447:             }
                    448:           TtaRemoveTree (el, doc);
                    449:           TtaChangeElementType (el, Template_EL_useSimple);
1.80      kia       450:           // generate the types attribute
                    451:           attType.AttrTypeNum = Template_ATTR_types;
                    452:           att = TtaNewAttribute (attType);
                    453:           TtaAttachAttribute (el, att, doc);
                    454:           if (name)
                    455:             TtaSetAttributeText (att, name, el, doc);
                    456:           // generate the title attribute
                    457:           attType.AttrTypeNum = Template_ATTR_title;
                    458:           att = TtaNewAttribute (attType);
                    459:           TtaAttachAttribute (el, att, doc);
                    460:           if (name)
                    461:             TtaSetAttributeText (att, name, el, doc);
                    462:           // generate the currentType attribute
                    463:           attType.AttrTypeNum = Template_ATTR_currentType;
                    464:           att = TtaNewAttribute (attType);
                    465:           TtaAttachAttribute (el, att, doc);
                    466:           if (name)
                    467:             TtaSetAttributeText (att, name, el, doc);
1.93      vatton    468:           /* now reinsert the element new map */
                    469:           if (prev != NULL)
                    470:             TtaInsertSibling (el, prev, FALSE, doc);
                    471:           else if (next != NULL)
                    472:             TtaInsertSibling (el, next, TRUE, doc);
                    473:           else
                    474:             TtaInsertFirstChild (&el, parent, doc);
1.80      kia       475:           TtaFreeMemory(name);
1.94      vatton    476:           Template_FixAccessRight (t, el, doc);
                    477:           TtaUpdateAccessRightInViews (doc, el);
1.80      kia       478:           break;
                    479:         case Template_EL_bag :
1.105     vatton    480:           Template_FixAccessRight (t, el, doc);
                    481:           TtaUpdateAccessRightInViews (doc, el);
1.80      kia       482:           break;
                    483:         case Template_EL_useEl :
                    484:         case Template_EL_useSimple :
                    485:           /* if this use element is not empty, don't do anything: it is
                    486:              supposed to contain a valid instance. This should be
                    487:              checked, though */
                    488:             // add the initial indicator
1.94      vatton    489:           types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                    490:           if (types)
1.87      vatton    491:             {
1.94      vatton    492:               child = TtaGetFirstChild (el);
                    493:               if (!strcmp (types, "string") || !strcmp (types, "number"))
1.120     vatton    494:                 {
1.124     vatton    495:                   if (child == NULL)
                    496:                     {
                    497:                       elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    498:                       child = TtaNewElement (doc, elType);
                    499:                       TtaInsertFirstChild (&child, el, doc);
                    500:                     }
1.120     vatton    501:                   if (!loading)
                    502:                     // don't apply to a loaded instance
                    503:                     AddPromptIndicator (el, doc);
                    504:                 }
1.92      vatton    505:               else
                    506:                 {
                    507:                   // avoid to have a block element within a pseudo paragraph
1.94      vatton    508:                   dec = Template_GetDeclaration (t, types);
1.92      vatton    509:                   if (dec && dec->blockLevel == 2 && parentLine)
                    510:                     {
                    511:                       // move the use element after the paragraph
                    512:                       child = TtaGetParent (el);
1.94      vatton    513:                       otherType = TtaGetElementType (child);
                    514:                       if (otherType.ElSSchema != elType.ElSSchema ||
                    515:                           otherType.ElTypeNum == Template_EL_repeat)
1.92      vatton    516:                         // not need to move the parent element
                    517:                           child = el;
                    518:                       next = child;
                    519:                       prev = parentLine;
                    520:                       while (child)
                    521:                         {
                    522:                           // move the element and next siblings after the pseudo paragraph
                    523:                           TtaNextSibling (&next);
                    524:                           TtaRemoveTree (child, doc);
                    525:                           TtaInsertSibling (child, prev, FALSE, doc);
                    526:                           prev = child;
                    527:                           child = next;
                    528:                         }
                    529:                       // elements are now out of the parent line
                    530:                       savedInline = NULL;
1.97      vatton    531:                       parentLine = NULL;
1.109     vatton    532:                       child = TtaGetFirstChild (el);
1.92      vatton    533:                     }
1.94      vatton    534: 
                    535:                   // generate the currentType attribute
                    536:                   otherType = TtaGetElementType (child);
1.109     vatton    537:                   if (otherType.ElSSchema == elType.ElSSchema &&
                    538:                       otherType.ElTypeNum == Template_EL_TemplateObject)
1.94      vatton    539:                     {
1.109     vatton    540:                       // not already instantiated
                    541:                       TtaDeleteTree (child, doc);
                    542:                       child = NULL;
1.94      vatton    543:                     }
                    544:                   else
                    545:                     {
1.126   ! vatton    546:       if (IsTemplateInstanceDocument (doc))
        !           547:         {
        !           548:           // give a riority to the repeat button
        !           549:           parent = TtaGetParent (el);
        !           550:           parentType = TtaGetElementType (parent);
        !           551:           if (elType.ElTypeNum == Template_EL_useEl &&
        !           552:               parentType.ElSSchema == elType.ElSSchema &&
        !           553:               parentType.ElTypeNum == Template_EL_repeat)
        !           554:             TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
        !           555:         }
        !           556:                       // there is already a contents
1.109     vatton    557:                       attType.AttrTypeNum = Template_ATTR_currentType;
                    558:                       att = TtaGetAttribute (el, attType);
                    559:                       if (att == NULL)
                    560:                         {
                    561:                           att = TtaNewAttribute (attType);
                    562:                           TtaAttachAttribute (el, att, doc);
                    563:                         }
                    564:                       if (otherType.ElTypeNum == 1)
1.121     vatton    565:                         {
                    566:                           if (strstr (types, "string"))
                    567:                             TtaSetAttributeText (att, "string", el, doc);
                    568:                           else
                    569:                             TtaSetAttributeText (att, "number", el, doc);
                    570:                         }
1.109     vatton    571:                       else
                    572:                         {
                    573:                           name = (char *)GetXMLElementName (otherType, doc);
                    574:                           if (name && strcmp (name,"???"))
                    575:                             TtaSetAttributeText (att, name, el, doc);
                    576:                         }
1.94      vatton    577:                     }
1.92      vatton    578:                 }
1.87      vatton    579:             }
1.94      vatton    580:           if (child == NULL)
1.114     vatton    581:             {
1.123     vatton    582:               if (IsUseInstantiated (el, doc))
                    583:                 // complete the use
                    584:                 InstantiateUse (t, el, doc, parentLine, FALSE, loading);
                    585:               else
                    586:                 // keep the use empty
                    587:                 Template_FillEmpty (el, doc, FALSE);
1.114     vatton    588:             }
1.94      vatton    589:           else
                    590:             {
                    591:               Template_FixAccessRight (t, el, doc);
                    592:               TtaUpdateAccessRightInViews (doc, el);
                    593:             }
1.109     vatton    594:           TtaFreeMemory (types);
1.80      kia       595:           break;
                    596:         case Template_EL_attribute :
                    597:           if (!loading)
                    598:             InstantiateAttribute (t, el, doc);
                    599:           break;
                    600:         case Template_EL_repeat :
1.123     vatton    601:           InstantiateRepeat (t, el, doc, parentLine, FALSE, loading);
1.80      kia       602:           break;
                    603:         default :
                    604:           break;
                    605:         }
                    606:     }
1.92      vatton    607:   else if (!strcmp (name, "HTML") &&
                    608:            (elType.ElTypeNum == HTML_EL_Pseudo_paragraph ||
                    609:             elType.ElTypeNum == HTML_EL_Paragraph))
                    610:     parentLine = el;
1.80      kia       611: 
                    612:   child = TtaGetFirstChild (el);
                    613:   while (child)
                    614:     {
1.92      vatton    615:       next = child;
                    616:       TtaNextSibling (&next);
                    617:       parentLine = ParseTemplate (t, child, doc, parentLine, loading);
                    618:       child = next;
1.80      kia       619:     }
1.92      vatton    620:   return savedInline;
1.80      kia       621: #endif /* TEMPLATES */
                    622: }
                    623: 
1.63      kia       624: /*----------------------------------------------------------------------
                    625:   CreateTemplate
                    626:   Create a template from any document.
                    627:   ----------------------------------------------------------------------*/
1.89      vatton    628: void CreateTemplate (Document doc, char *templatePath)
1.63      kia       629: {
                    630: #ifdef TEMPLATES
                    631:   Element           root, head, elem, xt, title, child, last;
                    632:   ElementType       elType, xtType;
1.64      kia       633:   char             *s;
1.63      kia       634:   SSchema           templSchema;
                    635:   XTigerTemplate    t;
                    636:   
1.89      vatton    637:   if (IsTemplateInstanceDocument(doc))
1.63      kia       638:     {
                    639:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
                    640:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    641:       return;
                    642:     }
                    643: 
1.89      vatton    644:   if (IsTemplateDocument(doc))
1.63      kia       645:     {
                    646:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
                    647:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    648:       return;
                    649:     }
                    650:   
1.64      kia       651:   root = TtaGetRootElement(doc);
                    652:   elType = TtaGetElementType (root);
                    653:   // get the target document type
                    654:   s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       655:   
1.64      kia       656:   TtaNewNature (doc, elType.ElSSchema,  NULL, "Template", "TemplateP");
1.96      vatton    657:   TtaSetANamespaceDeclaration (doc, root, "xt", Template_URI);
                    658:   templSchema = TtaGetSSchema ("Template", doc);
                    659:   TtaSetUriSSchema (templSchema, Template_URI);
1.64      kia       660: 
                    661:   // Insert xt:head and others
                    662:   TtaSetStructureChecking (FALSE, doc);
                    663:   if (strcmp (s, "HTML") == 0)
1.63      kia       664:     {
1.64      kia       665:       // Initialize the xt:head
                    666:       elType.ElTypeNum = HTML_EL_HEAD;
1.101     vatton    667:       xtType.ElSSchema = templSchema;
1.64      kia       668:       head = TtaSearchTypedElement (elType, SearchInTree, root);
                    669:       if(head)
                    670:         {
                    671:           xtType.ElTypeNum = Template_EL_head;
                    672:           xt = TtaNewElement(doc, xtType);
                    673:           elem = TtaGetLastChild(head);
                    674:           if(elem)
                    675:               TtaInsertSibling(xt, elem, FALSE, doc);
                    676:           else
                    677:               TtaInsertFirstChild(&xt, head, doc);
                    678:           
                    679:           SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    680:           SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
                    681:         }
1.63      kia       682:       
1.64      kia       683:       // Initialize the document title
                    684:       elType.ElTypeNum = HTML_EL_TITLE;
                    685:       title = TtaSearchTypedElement (elType, SearchInTree, root);
1.96      vatton    686:       if (title)
1.63      kia       687:         {
1.64      kia       688:           // Create xt:use for title
                    689:           xtType.ElTypeNum = Template_EL_useSimple;
1.96      vatton    690:           xt = TtaNewElement (doc, xtType);
1.64      kia       691:           TtaInsertFirstChild(&xt, title, doc);
1.96      vatton    692:           SetAttributeStringValue (xt, Template_ATTR_types, "string");
                    693:           SetAttributeStringValue (xt, Template_ATTR_title, "title");
1.64      kia       694:           
                    695:           // Move current title content to xt:use
                    696:           last = NULL;
                    697:           while(child = TtaGetLastChild(title), child!=NULL)
1.63      kia       698:             {
1.96      vatton    699:               if (child == xt)
1.64      kia       700:                 break;
1.96      vatton    701:               TtaRemoveTree (child, doc);
                    702:               if (last)
                    703:                 TtaInsertSibling (child, last, FALSE, doc);
1.63      kia       704:               else
1.96      vatton    705:                 TtaInsertFirstChild (&child, xt, doc);
1.64      kia       706:               last = child;
1.63      kia       707:             }
                    708:         }
1.64      kia       709:     }
                    710:   else
                    711:     {
                    712:       xtType.ElSSchema = templSchema;
                    713:       xtType.ElTypeNum = Template_EL_head;
1.96      vatton    714:       xt = TtaNewElement (doc, xtType);
                    715:       TtaInsertFirstChild (&xt, root, doc);
                    716:       SetAttributeStringValue (xt, Template_ATTR_version, Template_Current_Version);
                    717:       SetAttributeStringValue (xt, Template_ATTR_templateVersion, "1.0");      
1.64      kia       718:     }
                    719:   // Save changes
                    720:   TtaSetStructureChecking (TRUE, doc);
1.86      vatton    721:   if (DocumentTypes[doc] == docHTML)
                    722:     // avoid positionned boxes to overlap the xt:head section
                    723:     SetBodyAbsolutePosition (doc);
                    724: 
1.64      kia       725:   TtaClearUndoHistory (doc);
                    726:   RemoveParsingErrors (doc);
1.63      kia       727: 
1.64      kia       728:   TtaFreeMemory(DocumentURLs[doc]);
                    729:   DocumentURLs[doc] = TtaStrdup(templatePath);
                    730:   
                    731:   if(DocumentMeta[doc]==NULL)
                    732:     DocumentMeta[doc] = DocumentMetaDataAlloc();
                    733:   
                    734:   DocumentMeta[doc]->method = CE_TEMPLATE;
                    735:   if(DocumentMeta[doc]->initial_url)
                    736:     {
                    737:       TtaFreeMemory(DocumentMeta[doc]->initial_url);
                    738:       DocumentMeta[doc]->initial_url = NULL;
                    739:     }
                    740:   TtaSetDocumentModified (doc);
1.63      kia       741: 
1.64      kia       742:   // Load template-related infos :
                    743:   // like LoadTemplate(..)
                    744:   t = LookForXTigerTemplate(templatePath);
                    745:   t->doc = doc;
1.89      vatton    746:   Template_PrepareTemplate(t, doc);
1.67      kia       747:   //  DocumentTypes[doc] = docTemplate;
                    748:   t->state |= templloaded|templTemplate;
1.63      kia       749: 
1.98      vatton    750: #ifdef TEMPLATE_DEBUG  
1.64      kia       751:     DumpAllDeclarations();
1.98      vatton    752: #endif /* TEMPLATE_DEBUG */
1.64      kia       753:     
                    754:   /* Update the URL combo box */
                    755:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    756:   TtaSetTextZone (doc, 1, URL_list);
                    757:   /* Update template menus */
                    758:   UpdateTemplateMenus(doc);
                    759: 
1.63      kia       760: #endif /* TEMPLATES */  
                    761: }
                    762: 
1.1       vatton    763: /*----------------------------------------------------------------------
                    764:   CreateInstance
1.41      vatton    765:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    766:   ----------------------------------------------------------------------*/
1.99      vatton    767: void CreateInstance (char *templatePath, char *instancePath,
                    768:                      char *docname,  DocumentType docType, int basedoc)
1.3       vatton    769: {
1.1       vatton    770: #ifdef TEMPLATES
1.41      vatton    771:   Document          doc = 0, newdoc = 0;
1.90      vatton    772:   Element           root, title, text;
1.41      vatton    773:   ElementType       elType;
1.90      vatton    774:   CHARSET           charset, ocharset;
                    775:   char             *s, *charsetname, *ocharsetname, *localFile;
1.1       vatton    776: 
1.35      kia       777:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    778:   if (t == NULL)
1.29      vatton    779:     {
                    780:       // the template cannot be loaded
                    781:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    782:       return;
                    783:     }
1.41      vatton    784:   // the template document
1.5       vatton    785:   doc = GetTemplateDocument (t);
1.41      vatton    786:   // localize the new created document
                    787:   if (DontReplaceOldDoc)
                    788:     newdoc = TtaGetNextDocumentIndex ();
                    789:   else
                    790:     newdoc = basedoc;
1.90      vatton    791:   
                    792:   // close current undo sepquence in the template document
                    793:   if (TtaHasUndoSequence (doc))
                    794:     TtaCloseUndoSequence (doc);
                    795: 
                    796:   // update the charset if needed
                    797:   charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    798:   charset = TtaGetCharset (charsetname);
                    799:   ocharsetname = DocumentMeta[doc]->charset;
                    800:   ocharset =  TtaGetCharset (ocharsetname);
                    801:   if (charset != UNDEFINED_CHARSET &&
                    802:       DocumentMeta[doc]->charset &&
                    803:       strcmp (charsetname, DocumentMeta[doc]->charset))
                    804:     {
                    805:       TtaSetDocumentCharset (doc, charset, FALSE);
                    806:       DocumentMeta[doc]->charset = TtaStrdup (charsetname);
                    807:       SetNamespacesAndDTD (doc, FALSE);
                    808:     }
1.54      vatton    809: 
1.72      vatton    810:   // register the document type to open the right page model
                    811:   DocumentTypes[newdoc] = docType;
1.90      vatton    812:   // Generate the instance content as a copy of the template
                    813:   localFile = SaveDocumentToNewDoc(doc, newdoc, instancePath);
                    814:   Template_PrepareInstance (instancePath, newdoc, t->version, templatePath);
                    815:   Template_AddReference (t);
                    816: 
                    817:   // Revert template changes
                    818:   TtaSetDocumentCharset (doc, ocharset, FALSE);
1.111     vatton    819:   TtaFreeMemory (DocumentMeta[doc]->charset);
1.90      vatton    820:   DocumentMeta[doc]->charset = ocharsetname;
                    821:   // Now parse the instance
                    822:   // The xtiger PI will be added and components will be removed
                    823:   GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    824:                !DontReplaceOldDoc, NULL, NULL);
                    825:   if (DocumentMeta[newdoc])
                    826:     DocumentMeta[newdoc]->method = CE_ABSOLUTE;
                    827:   // Generate the HTML document title
                    828:   root = TtaGetRootElement(newdoc);
                    829:   elType = TtaGetElementType (root);
                    830:   // get the target document type
                    831:   s = TtaGetSSchemaName (elType.ElSSchema);
                    832:   if (strcmp (s, "HTML") == 0)
1.1       vatton    833:     {
1.90      vatton    834:       // Initialize the document title
                    835:       elType.ElTypeNum = HTML_EL_TITLE;
                    836:       title = TtaSearchTypedElement (elType, SearchInTree, root);
                    837:       text = TtaGetFirstChild (title);
                    838:       while (text)
1.1       vatton    839:         {
1.90      vatton    840:           elType = TtaGetElementType (text);
                    841:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1.14      vatton    842:             {
1.90      vatton    843:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                    844:                                  TtaGetDefaultLanguage (), newdoc);
                    845:               text = NULL;
                    846:               SetNewTitle (newdoc);
1.14      vatton    847:             }
1.90      vatton    848:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                    849:                     elType.ElTypeNum == Template_EL_useSimple) &&
                    850:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    851:             // Ignore the template use element
                    852:             text = TtaGetFirstChild (text);
                    853:           else
                    854:             // Look for the first text child
                    855:             TtaNextSibling (&text);
1.63      kia       856:         }
1.90      vatton    857:     }
1.54      vatton    858: 
1.90      vatton    859:   // Insert XTiger PI
                    860:   Template_InsertXTigerPI(newdoc, t);   
                    861:   // Parse template to fill structure and remove extra data
1.92      vatton    862:   ParseTemplate (t, root, newdoc, NULL, FALSE);
1.90      vatton    863:   TtaFreeMemory (localFile);
                    864:   TtaClearUndoHistory (newdoc);
                    865:   RemoveParsingErrors (newdoc);
                    866:   TtaSetDocumentModified (newdoc);
                    867:   UpdateTemplateMenus(newdoc);
1.1       vatton    868: #endif /* TEMPLATES */
                    869: }
                    870: 
                    871: 
                    872: #ifdef TEMPLATES
                    873: /*----------------------------------------------------------------------
                    874:   ProcessAttr
1.9       vatton    875:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    876:   ----------------------------------------------------------------------*/
1.1       vatton    877: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    878: {
                    879:   Element      child;
                    880:   ElementType  elType;
                    881: 
                    882:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    883:     {
                    884:       elType = TtaGetElementType (child);
                    885:       if (elType.ElTypeNum == Template_EL_attribute &&
                    886:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    887:         InstantiateAttribute (t, child, doc);
                    888:       else
                    889:         ProcessAttr (t, child, doc);
                    890:     }
                    891: }
                    892: #endif /* TEMPLATES */
                    893: 
1.12      kia       894: 
                    895: /*----------------------------------------------------------------------
                    896:   Template_GetNewSimpleTypeInstance
1.109     vatton    897:   Create an new instance of xt:use/SimpleType for the document doc.
                    898:   Return the new element
1.12      kia       899:   ----------------------------------------------------------------------*/
1.109     vatton    900: Element Template_GetNewSimpleTypeInstance(Document doc)
1.12      kia       901: {
                    902:   Element           newEl = NULL;
                    903: #ifdef TEMPLATES
                    904:   ElementType       elType;
1.73      kia       905:   const char       *empty = " ";
1.24      vatton    906: 
1.38      vatton    907:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       908:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    909:   newEl = TtaNewElement (doc, elType);
                    910:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    911: #endif /* TEMPLATES */
1.12      kia       912:   return newEl;
                    913: }
                    914: 
                    915: /*----------------------------------------------------------------------
                    916:   Template_GetNewXmlElementInstance
1.109     vatton    917:   Create an new instance of xt:use/XmlElement for the document doc.
                    918:   The parameter decl gives the type of the element of new element.
                    919:   Return the new element
1.12      kia       920:   ----------------------------------------------------------------------*/
1.109     vatton    921: Element Template_GetNewXmlElementInstance(Document doc, Declaration decl)
1.12      kia       922: {
                    923:   Element           newEl = NULL;
                    924: #ifdef TEMPLATES
                    925:   ElementType       elType;
                    926: 
1.24      vatton    927:   GIType (decl->name, &elType, doc);
                    928:   if (elType.ElTypeNum != 0)
1.23      kia       929:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       930: #endif /* TEMPLATES */
                    931:   return newEl;
                    932: }
                    933: 
1.34      vatton    934: 
                    935: /*----------------------------------------------------------------------
                    936:   InsertWithNotify applies pre and post functions when inserting the new
                    937:   element el after child (if not NULL) or as first child of parent.
                    938:   ----------------------------------------------------------------------*/
                    939: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    940: {
                    941:   ElementType      elType;
1.116     vatton    942:   AttributeType    attrType;
                    943:   Attribute        attr;
1.34      vatton    944:   NotifyElement    event;
                    945:   char            *name;
                    946:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    947:   ThotBool         isImage = FALSE;
                    948:   ThotBool         oldStructureChecking;
                    949: 
                    950:   // avoid to check attributes now
                    951:   oldStructureChecking = TtaGetStructureChecking (doc);
                    952:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    953: 
                    954:   elType = TtaGetElementType (el);
                    955:   name = TtaGetSSchemaName (elType.ElSSchema);
                    956:   isCell = ((!strcmp (name,"HTML") &&
                    957:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    958:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    959:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    960:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    961:            (!strcmp (name,"MathML") &&
                    962:             (elType.ElTypeNum == MathML_EL_MTR ||
                    963:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    964:   isImage = (!strcmp (name,"HTML") && 
                    965:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    966:   if (child)
                    967:     TtaInsertSibling (el, child, FALSE, doc);
                    968:   else
                    969:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    970:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    971: 
1.50      vatton    972:   if (isImage)
1.116     vatton    973:     {
                    974:       // check if the src attribute is there
                    975:       attrType.AttrSSchema = elType.ElSSchema;
                    976:       if (elType.ElTypeNum == HTML_EL_IMG)
                    977:         attrType.AttrTypeNum = HTML_ATTR_SRC;
                    978:       else
                    979:          attrType.AttrTypeNum = HTML_ATTR_data;
                    980:       attr = TtaGetAttribute (el, attrType);
                    981:       if (attr == NULL)
                    982:         InsertImageOrObject (el, doc);
                    983:     }
1.50      vatton    984:   else if (isCell)
1.34      vatton    985:     {
                    986:       // a cell is created
1.39      quint     987:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    988:     }
                    989:   else if (isRow)
                    990:     {
                    991:       // a row is created
                    992:       event.element = el;
                    993:       event.document = doc;
                    994:       RowPasted (&event);
                    995:     }
1.50      vatton    996:   
                    997:   if (!strcmp (name,"HTML"))
                    998:     {
1.104     vatton    999:       // special management for images and objets
1.50      vatton   1000:       elType.ElTypeNum = HTML_EL_IMG;
                   1001:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                   1002:       while (child)
                   1003:         {
                   1004:           InsertImageOrObject (child, doc);
                   1005:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                   1006:         }
                   1007:       elType.ElTypeNum = HTML_EL_Object;
                   1008:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                   1009:       while (child)
                   1010:         {
                   1011:           InsertImageOrObject (child, doc);
                   1012:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                   1013:         }
                   1014:     }
1.34      vatton   1015:   return el;
                   1016: }
                   1017: 
1.118     vatton   1018: /*----------------------------------------------------------------------
1.16      kia      1019:   Template_InsertUseChildren
                   1020:   Insert children to a xt:use
                   1021:   The dec parameter must be valid and will not be verified. It must be a
                   1022:     direct child element (for union elements).
                   1023:   @param el element (xt:use) in which insert a new element
                   1024:   @param dec Template declaration of the element to insert
                   1025:   @return The inserted element (the xt:use element if insertion is multiple as component)
1.104     vatton   1026:   The parentLine parameter points to the enclosing line if any.
1.16      kia      1027:   ----------------------------------------------------------------------*/
1.104     vatton   1028: Element Template_InsertUseChildren (Document doc, Element el, Declaration dec,
                   1029:                                     Element parentLine, ThotBool registerUndo)
1.16      kia      1030: {
1.99      vatton   1031:   Element         newEl = NULL;
1.16      kia      1032: #ifdef TEMPLATES
1.99      vatton   1033:   Element         current = NULL;
1.104     vatton   1034:   Element         child = NULL, prev, next;
                   1035:   ElementType     childType, elType; 
                   1036:   SSchema         sshtml;
1.99      vatton   1037:   XTigerTemplate  t;
1.17      kia      1038:   
1.25      vatton   1039:   if (TtaGetDocumentAccessMode(doc))
1.16      kia      1040:   {
1.23      kia      1041:     switch (dec->nature)
                   1042:     {
                   1043:       case SimpleTypeNat:
1.109     vatton   1044:         newEl = Template_GetNewSimpleTypeInstance(doc);
1.34      vatton   1045:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia      1046:         break;
                   1047:       case XmlElementNat:
1.109     vatton   1048:         newEl = Template_GetNewXmlElementInstance(doc, dec);
1.34      vatton   1049:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia      1050:         break;
                   1051:       case ComponentNat:
                   1052:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74      kia      1053:         ProcessAttr (dec->usedIn, newEl, doc);
1.104     vatton   1054:         elType = TtaGetElementType (el);
1.23      kia      1055:         /* Copy elements from new use to existing use. */
1.98      vatton   1056: #ifdef TEMPLATE_DEBUG
1.74      kia      1057:         DumpSubtree(newEl, doc, 0);
1.98      vatton   1058: #endif /* TEMPLATE_DEBUG */
1.104     vatton   1059:         sshtml = TtaGetSSchema ("HTML", doc);
1.102     vatton   1060:         t = GetXTigerDocTemplate( doc);
                   1061:         child = TtaGetFirstChild  (newEl);
1.76      vatton   1062:         while (child)
                   1063:           {
                   1064:             // move the new subtree to the document
                   1065:             TtaRemoveTree (child, doc);
1.104     vatton   1066:             childType = TtaGetElementType (child);
                   1067:             if (parentLine)
                   1068:               {
                   1069:                 if (childType.ElSSchema == sshtml &&
                   1070:                     childType.ElTypeNum == HTML_EL_Pseudo_paragraph)
                   1071:                   {
                   1072:                     prev = TtaGetFirstChild  (child);
                   1073:                     while (prev)
                   1074:                       {
                   1075:                         next = prev;
                   1076:                         TtaNextSibling (&next);
                   1077:                         TtaRemoveTree (prev, doc);
                   1078:                         current = InsertWithNotify (prev, current, el, doc);
                   1079:                         prev = next;
                   1080:                       }
1.108     vatton   1081:                     TtaDeleteTree (child, doc);
1.104     vatton   1082:                   }
1.108     vatton   1083:                 else
                   1084:                   current = InsertWithNotify (child, current, el, doc);
1.104     vatton   1085:               }
                   1086:             else
                   1087:               {
                   1088:                 current = InsertWithNotify (child, current, el, doc);
                   1089:                 // check if a new paragraph is inserted
                   1090:                 if (childType.ElSSchema == sshtml &&
                   1091:                     childType.ElTypeNum == HTML_EL_Paragraph)
                   1092:                   Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                   1093:                 else
                   1094:                   {
                   1095:                     childType.ElSSchema = sshtml;
                   1096:                     childType.ElTypeNum = HTML_EL_Paragraph;
                   1097:                     child = TtaSearchTypedElement (childType, SearchInTree, current);
                   1098:                     while (child)
                   1099:                       {
                   1100:                         Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                   1101:                         child = TtaSearchTypedElement (childType, SearchInTree, child);
                   1102:                       }
                   1103:                   }
                   1104:               }
1.99      vatton   1105:             child = TtaGetFirstChild (newEl);
1.76      vatton   1106:           }
1.99      vatton   1107: 
1.102     vatton   1108:         TtaDeleteTree (newEl, doc);
1.23      kia      1109:         newEl = el;
                   1110:         break;
                   1111:       default :
                   1112:         //Impossible
                   1113:         break;   
                   1114:     }
1.44      kia      1115:     Template_FixAccessRight (dec->usedIn, el, doc);
1.105     vatton   1116:     if (dec->nature == ComponentNat)
                   1117:       Component_FixAccessRight (el, doc);
1.102     vatton   1118:     TtaUpdateAccessRightInViews (doc, el);
1.23      kia      1119:   }  
1.16      kia      1120: #endif /* TEMPLATES */
                   1121:   return newEl;
                   1122: }
                   1123: 
1.40      kia      1124: 
                   1125: /*----------------------------------------------------------------------
1.105     vatton   1126:   Component_FixAccessRight locks children of the component
                   1127:   ----------------------------------------------------------------------*/
                   1128: void Component_FixAccessRight (Element el, Document doc)
                   1129: {
                   1130: #ifdef TEMPLATES
                   1131:   Element     child;
                   1132:   
                   1133:   if (el && doc)
                   1134:     {
                   1135:       TtaSetAccessRight (el, ReadOnly, doc);
                   1136:       // fix access right to children
                   1137:       child = TtaGetFirstChild (el);
                   1138:       while (child)
                   1139:         {
                   1140:           TtaSetAccessRight (child, ReadOnly, doc);
                   1141:           TtaNextSibling (&child);
                   1142:         }
                   1143:     }
                   1144: #endif /* TEMPLATES */
                   1145: }
                   1146: 
                   1147: /*----------------------------------------------------------------------
1.97      vatton   1148:   Template_FixAccessRight fixes access rights of the el element
1.40      kia      1149:   ----------------------------------------------------------------------*/
1.41      vatton   1150: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia      1151: {
                   1152: #ifdef TEMPLATES
                   1153:   ElementType elType;
                   1154:   Element     child;
1.97      vatton   1155:   Declaration decl;
1.88      vatton   1156:   char        currentType[MAX_LENGTH], *ptr;
1.40      kia      1157:   
                   1158:   if (t && el && doc)
                   1159:     {
                   1160:       elType = TtaGetElementType(el);
1.41      vatton   1161:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia      1162:         {
1.41      vatton   1163:           switch (elType.ElTypeNum)
1.40      kia      1164:             {
                   1165:             case Template_EL_TEXT_UNIT:
1.41      vatton   1166:               //TtaSetAccessRight( el, ReadWrite, doc);
                   1167:               return;
1.105     vatton   1168:             case Template_EL_component:
                   1169:               Component_FixAccessRight (el, doc);
                   1170:               break;
1.40      kia      1171:             case Template_EL_useEl:
                   1172:             case Template_EL_useSimple:
                   1173:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                   1174:                                               (char*)currentType, NULL);
1.88      vatton   1175:               if (currentType[0] == EOS)
                   1176:                 {
                   1177:                   GiveAttributeStringValueFromNum(el, Template_ATTR_types,
                   1178:                                                   (char*)currentType, NULL);
                   1179:                   ptr = strstr (currentType, " ");
                   1180:                   if (ptr)
                   1181:                     *ptr = EOS;
                   1182:                 }
1.40      kia      1183:               decl = Template_GetDeclaration(t, currentType);
                   1184:               if (decl)
                   1185:                 {
                   1186:                   switch (decl->nature)
                   1187:                     {
                   1188:                       case SimpleTypeNat:
1.41      vatton   1189:                         TtaSetAccessRight (el, ReadWrite, doc);
                   1190:                         return;
1.105     vatton   1191:                       case ComponentNat:
1.107     vatton   1192:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1193:                          break;
                   1194:                         //Component_FixAccessRight (el, doc);
                   1195:                         //return;
1.97      vatton   1196:                       case XmlElementNat:
1.105     vatton   1197:                         if (TtaIsSetReadOnly (el))
                   1198:                           break;
1.97      vatton   1199:                         child = TtaGetFirstChild (el);
                   1200:                         if (child)
                   1201:                           TtaSetAccessRight (child, ReadWrite, doc);
1.40      kia      1202:                       default:
1.41      vatton   1203:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1204:                          break;
1.40      kia      1205:                     }
                   1206:                 }
                   1207:               break;
                   1208:             case Template_EL_bag:
1.45      vatton   1209:             case Template_EL_repeat:
1.40      kia      1210:               TtaSetAccessRight(el, ReadWrite, doc);
                   1211:               break;
                   1212:             default:
                   1213:               TtaSetAccessRight(el, ReadOnly, doc);
                   1214:               break;
                   1215:             }
                   1216:         }
                   1217: 
1.92      vatton   1218:       // fix access right to children
1.41      vatton   1219:       child = TtaGetFirstChild (el);
                   1220:       while (child)
1.40      kia      1221:         {
1.41      vatton   1222:           Template_FixAccessRight (t, child, doc);
                   1223:           TtaNextSibling (&child);
1.40      kia      1224:         }
                   1225:     }
                   1226: #endif /* TEMPLATES */
                   1227: }
                   1228: 
1.16      kia      1229: /*----------------------------------------------------------------------
1.46      vatton   1230:   AddPromptIndicator
                   1231:   ----------------------------------------------------------------------*/
                   1232: void AddPromptIndicator (Element el, Document doc)
                   1233: {
                   1234: #ifdef TEMPLATES
                   1235:   ElementType         elType;
                   1236:   AttributeType       attrType;
                   1237:   Attribute           att;
                   1238: 
1.66      vatton   1239:   if (el)
                   1240:     {
                   1241:       elType = TtaGetElementType (el);
                   1242:       attrType.AttrSSchema = elType.ElSSchema;
                   1243:       attrType.AttrTypeNum = Template_ATTR_prompt;
1.69      vatton   1244:       att = TtaGetAttribute (el, attrType);
                   1245:       if (att == NULL)
                   1246:         {
                   1247:           att = TtaNewAttribute (attrType);
                   1248:           TtaAttachAttribute (el, att, doc);
                   1249:         }
1.66      vatton   1250:     }
1.46      vatton   1251: #endif /* TEMPLATES */
                   1252: }
                   1253: 
                   1254: /*----------------------------------------------------------------------
1.123     vatton   1255:   AddPromptIndicatorInSubtree
                   1256:   ----------------------------------------------------------------------*/
                   1257: void AddPromptIndicatorInSubtree (Element el, Document doc)
                   1258: {
                   1259: #ifdef TEMPLATES
                   1260:   Element             child;
                   1261:   ElementType         elType;
                   1262:   char               *types;
                   1263: 
                   1264:   if (el)
                   1265:     {
                   1266:       elType = TtaGetElementType (el);
                   1267:       types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
                   1268:       if (types &&
                   1269:           (!strcmp (types, "string") || !strcmp (types, "number")))
                   1270:         AddPromptIndicator (el, doc);
                   1271:       else
                   1272:         {
                   1273:           child = el;
                   1274:           while (child)
                   1275:             {
                   1276:               child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                   1277:               TtaFreeMemory (types);
                   1278:               types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1279:               if (types &&
                   1280:                   (!strcmp (types, "string") || !strcmp (types, "number")))
                   1281:                 AddPromptIndicator (child, doc);
                   1282:             }
                   1283:         }
                   1284:       TtaFreeMemory (types);
                   1285:     }
                   1286: #endif /* TEMPLATES */
                   1287: }
                   1288: 
                   1289: /*----------------------------------------------------------------------
1.104     vatton   1290:   Template_SetInline manages inline elements
                   1291:   registerUndo says if changes must be registered
                   1292:   ----------------------------------------------------------------------*/
                   1293: void Template_SetInline (Element el, SSchema sstempl, Document doc, ThotBool registerUndo)
                   1294: {
                   1295: #ifdef TEMPLATES
                   1296:   Element         child = NULL;
                   1297:   ElementType     elType1, elType2, elType3;
                   1298: 
                   1299:   if (el)
                   1300:     {
                   1301:       elType1 = TtaGetElementType (el);
                   1302:       if (elType1.ElSSchema == sstempl)
                   1303:         // apply to hte current template element
                   1304:         SetAttributeIntValue (el, Template_ATTR_SetInLine,
                   1305:                               Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1306:       else
                   1307:         elType1.ElSSchema = sstempl;
                   1308:       elType1.ElTypeNum = Template_EL_useSimple;
                   1309:       elType2.ElTypeNum = Template_EL_useEl;
                   1310:       elType2.ElSSchema = elType1.ElSSchema;
                   1311:       elType3.ElTypeNum = Template_EL_repeat;
                   1312:       elType3.ElSSchema = elType1.ElSSchema;
                   1313:       child = TtaSearchElementAmong5Types (elType1, elType2, elType3, elType3, elType3,
                   1314:                                            SearchForward, el);
                   1315:       while (child && TtaIsAncestor (child, el))
                   1316:         {
                   1317:           SetAttributeIntValue (child, Template_ATTR_SetInLine,
                   1318:                                 Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1319:           child = TtaSearchElementAmong5Types (elType1, elType2,
                   1320:                                                elType3, elType3, elType3,
                   1321:                                                SearchForward, child);
                   1322:         }
                   1323:     }
                   1324: #endif /* TEMPLATES */
                   1325: }
                   1326: 
                   1327: /*----------------------------------------------------------------------
                   1328:   InstantiateUse intantiate the use element el.
1.123     vatton   1329:   Parameter loading is TRUE when the document is not already loaded.
1.104     vatton   1330:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1331:   ----------------------------------------------------------------------*/
1.1       vatton   1332: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.123     vatton   1333:                         Element parentLine, ThotBool registerUndo, ThotBool loading)
1.1       vatton   1334: {
                   1335: #ifdef TEMPLATES
1.109     vatton   1336:   Element          child = NULL;
1.1       vatton   1337:   ElementType      elType;
                   1338:   Declaration      dec;
1.123     vatton   1339:   int              size, nbitems, i;
1.1       vatton   1340:   struct menuType  *items;
1.104     vatton   1341:   char             *types;
1.123     vatton   1342:   ThotBool          oldStructureChecking;
1.1       vatton   1343: 
1.25      vatton   1344:   if (!t)
1.23      kia      1345:     return NULL;
                   1346: 
1.1       vatton   1347:   /* get the value of the "types" attribute */
1.12      kia      1348:   elType = TtaGetElementType (el);
1.32      vatton   1349:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton   1350:   if (!types || types[0] == EOS)
                   1351:     {
                   1352:       TtaFreeMemory (types);
                   1353:       return NULL;
                   1354:     }
1.123     vatton   1355: 
1.104     vatton   1356:   if (!strcmp (types, "string") || !strcmp (types, "number"))
1.109     vatton   1357:     {
                   1358:       child = TtaGetFirstChild (el);
                   1359:       if (child == NULL)
                   1360:         {
                   1361:           child = Template_GetNewSimpleTypeInstance(doc);
                   1362:           child = InsertWithNotify (child, NULL, el, doc);
                   1363:         }
1.123     vatton   1364:       if (!loading)
                   1365:         AddPromptIndicator (el, doc);
1.109     vatton   1366:     }
1.104     vatton   1367:   else
1.1       vatton   1368:     {
1.104     vatton   1369:       giveItems (types, size, &items, &nbitems);
                   1370:       // No structure checking
                   1371:       oldStructureChecking = TtaGetStructureChecking (doc);
                   1372:       TtaSetStructureChecking (FALSE, doc);
1.123     vatton   1373:       if (nbitems == 1 || IsUseInstantiated (el, doc))
1.104     vatton   1374:         /* only one type in the "types" attribute */
                   1375:         {
                   1376:           dec = Template_GetDeclaration (t, items[0].label);
                   1377:           if (dec)
1.110     vatton   1378:             child = Template_InsertUseChildren (doc, el, dec, parentLine, registerUndo);
1.112     vatton   1379:           if (nbitems == 1 && elType.ElTypeNum != Template_EL_useSimple)
                   1380:             {
                   1381:               TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
                   1382:               if (registerUndo)
                   1383:                 TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
                   1384:             }
1.123     vatton   1385:           if (!loading)
                   1386:             AddPromptIndicatorInSubtree (el, doc);
1.104     vatton   1387:         }
                   1388:       else
1.123     vatton   1389:         Template_FillEmpty (el, doc, FALSE);
1.104     vatton   1390: 
                   1391:       for (i = 0; i < nbitems; i++)
                   1392:         TtaFreeMemory(items[i].label);
                   1393:       TtaFreeMemory(items);
                   1394: 
                   1395:       if (parentLine)
                   1396:         // display the element in line
                   1397:         Template_SetInline (el, elType.ElSSchema, doc, registerUndo);
                   1398:       TtaSetStructureChecking (oldStructureChecking, doc);
1.1       vatton   1399:     }
1.32      vatton   1400:   TtaFreeMemory (types);
1.44      kia      1401:   Template_FixAccessRight (t, el, doc);
                   1402:   TtaUpdateAccessRightInViews (doc, el);
1.109     vatton   1403:   return child;
1.23      kia      1404: #else /* TEMPLATES */
                   1405:   return NULL;
1.1       vatton   1406: #endif /* TEMPLATES */
                   1407: }
                   1408: 
                   1409: /*----------------------------------------------------------------------
1.22      kia      1410:   InstantiateRepeat
                   1411:   Check for min and max param and validate xt:repeat element content.
                   1412:   @param registerUndo True to register undo creation sequences.
1.123     vatton   1413:   Parameter loading is TRUE when the document is not already loaded.
1.104     vatton   1414:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1415:   ----------------------------------------------------------------------*/
1.46      vatton   1416: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
1.123     vatton   1417:                         Element parentLine, ThotBool registerUndo, ThotBool loading)
1.1       vatton   1418: {
                   1419: #ifdef TEMPLATES
1.32      vatton   1420:   Element        child, newChild;
1.52      vatton   1421:   ElementType    newElType;
1.123     vatton   1422:   Attribute      maxAtt;
                   1423:   AttributeType  maxType;
1.52      vatton   1424:   char          *text, *types = NULL, *title = NULL;
1.123     vatton   1425:   int            minVal, maxVal;
1.32      vatton   1426:   int            childrenCount;
                   1427: 
1.25      vatton   1428:   if (!t)
1.23      kia      1429:     return;
                   1430: 
1.123     vatton   1431:   // Preparing types
                   1432:   newElType = TtaGetElementType(el);
                   1433:   maxType.AttrSSchema =  newElType.ElSSchema;
1.1       vatton   1434:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.123     vatton   1435:   // Get minOccurs and maxOccurs attributes
1.1       vatton   1436:   maxAtt = TtaGetAttribute (el, maxType);
1.123     vatton   1437:   minVal = GetMinOccurence (el, doc);;
1.1       vatton   1438: 
                   1439:   if (maxAtt)
                   1440:     {
1.10      kia      1441:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton   1442:       if (text)
                   1443:         {
                   1444:           if (!strcmp (text, "*"))
                   1445:             maxVal = INT_MAX;
                   1446:           else
                   1447:             maxVal = atoi (text);
                   1448:           TtaFreeMemory (text);
                   1449:         }
                   1450:       else
                   1451:         //Error : Attribute with no value
                   1452:         return;
                   1453:     }
                   1454:   else
                   1455:     maxVal = INT_MAX;
                   1456: 
1.52      vatton   1457:   if (maxAtt == NULL)
1.1       vatton   1458:     {  
1.32      vatton   1459:       maxAtt = TtaNewAttribute (maxType);
1.123     vatton   1460:       text = (char*)TtaGetMemory(MAX_LENGTH);
                   1461:        if (maxVal < INT_MAX)
1.32      vatton   1462:         sprintf(text, "%d", maxVal);
1.1       vatton   1463:       else
1.32      vatton   1464:         sprintf (text, "*");
                   1465:       TtaAttachAttribute (el, maxAtt, doc);      
                   1466:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1467:       if (registerUndo)
1.32      vatton   1468:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.123     vatton   1469:       TtaFreeMemory (text);
1.1       vatton   1470:     }
                   1471: 
1.52      vatton   1472:   //We must have minOccurs children
1.104     vatton   1473:   child = TtaGetFirstChild (el);
1.122     vatton   1474:   if (!child)
                   1475:     //Error : a repeat must have at least one child which will be the model
                   1476:     return;
                   1477:   
1.104     vatton   1478:   for (childrenCount = 0; child; TtaNextSibling(&child))
                   1479:     //TODO : Check that every child is valid
                   1480:     childrenCount ++;
1.1       vatton   1481: 
                   1482:   if (childrenCount > maxVal)
                   1483:     //Error : too many children!
                   1484:     return;  
                   1485: 
1.122     vatton   1486: 
1.104     vatton   1487:   if (parentLine)
                   1488:     // display the element in line
1.123     vatton   1489:     Template_SetInline (el, newElType.ElSSchema, doc, registerUndo);
1.104     vatton   1490: 
1.1       vatton   1491:   child = TtaGetLastChild(el);
1.32      vatton   1492:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1493:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1494:   newElType.ElTypeNum = Template_EL_useEl;
1.123     vatton   1495:   while (childrenCount < minVal)
1.1       vatton   1496:     {
1.32      vatton   1497:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1498:       // Insert it
1.32      vatton   1499:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1500:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1501:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.123     vatton   1502:       InstantiateUse (t, newChild, doc, parentLine, TRUE, loading);
1.27      kia      1503:       
1.25      vatton   1504:       if (registerUndo)
1.34      vatton   1505:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1506:       child = newChild;
                   1507:       childrenCount++;
                   1508:     }
1.27      kia      1509:     
1.44      kia      1510:   Template_FixAccessRight (t, el, doc);
                   1511:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1512:   TtaFreeMemory (types);
                   1513:   TtaFreeMemory (title);
1.1       vatton   1514: #endif /* TEMPLATES */
                   1515: }
                   1516: 
                   1517: /*----------------------------------------------------------------------
1.80      kia      1518:   Template_InsertXTigerPI
                   1519:   Insert the XTiger PI element in template instance.
                   1520:   Param t is the XTigerTemplate structure of the template,
                   1521:   not the template instance one.
1.3       vatton   1522:   ----------------------------------------------------------------------*/
1.80      kia      1523: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1       vatton   1524: {
                   1525: #ifdef TEMPLATES
1.47      kia      1526:   ElementType     elType;
1.65      vatton   1527:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.47      kia      1528:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1529:   int             pi_type;
                   1530: 
1.80      kia      1531:   if (!t || !doc)
1.23      kia      1532:     return;
1.47      kia      1533: 
1.80      kia      1534:   root =  TtaGetMainRoot (doc);
1.90      vatton   1535:   if (root == NULL)
                   1536:     return;
1.1       vatton   1537:   //Look for PIs
                   1538:   /* check if the document has a DOCTYPE declaration */
                   1539: #ifdef ANNOTATIONS
                   1540:   if (DocumentTypes[doc]  == docAnnot)
                   1541:     elType = TtaGetElementType (root);
                   1542:   else
                   1543: #endif /* ANNOTATIONS */
                   1544:     elType = TtaGetElementType (root);
                   1545:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1546:   if (strcmp (s, "HTML") == 0)
                   1547:     {
                   1548:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1549:       pi_type = HTML_EL_XMLPI;
                   1550:     }
                   1551: #ifdef _SVG
                   1552:   else if (strcmp (s, "SVG") == 0)
                   1553:     {
                   1554:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1555:       pi_type = SVG_EL_XMLPI;
                   1556:     }
                   1557: #endif /* _SVG */
                   1558:   else if (strcmp (s, "MathML") == 0)
                   1559:     {
                   1560:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1561:       pi_type = MathML_EL_XMLPI;
                   1562:     }
                   1563:   else
                   1564:     {
                   1565:       elType.ElTypeNum = XML_EL_doctype;
                   1566:       pi_type = XML_EL_xmlpi;
                   1567:     }
1.54      vatton   1568: 
1.1       vatton   1569:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1570:   if (doctype == NULL)
1.1       vatton   1571:     {
1.65      vatton   1572:       elType.ElTypeNum = pi_type;      
                   1573:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1574:       if (piElem == NULL)
                   1575:         {
                   1576:           /* generate the XML declaration */
                   1577:           /* Check the Thot abstract tree against the structure schema. */
                   1578:           TtaSetStructureChecking (FALSE, doc);
                   1579:           piElem = TtaNewTree (doc, elType, "");
                   1580:           TtaInsertFirstChild (&piElem, root, doc);
                   1581:           line = TtaGetFirstChild (piElem);
                   1582:           text = TtaGetFirstChild (line);
                   1583:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1584:           charsetname = UpdateDocumentCharset (doc);
                   1585:           strcat (buffer, charsetname);
                   1586:           strcat (buffer, "\"");
                   1587:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1588:           TtaSetStructureChecking (TRUE, doc);
1.90      vatton   1589:           TtaFreeMemory (charsetname);
                   1590:           TtaRegisterElementCreate (piElem, doc);
1.65      vatton   1591:         }
1.1       vatton   1592:     }
                   1593:   
                   1594:   /* generate the XTiger PI */
                   1595:   /* Check the Thot abstract tree against the structure schema. */
                   1596:   TtaSetStructureChecking (FALSE, doc);
                   1597:   elType.ElTypeNum = pi_type;
1.65      vatton   1598:   elNew = TtaNewTree (doc, elType, "");
                   1599:   if (doctype)
                   1600:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1601:   else
                   1602:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1603:   line = TtaGetFirstChild (elNew);
                   1604:   text = TtaGetFirstChild (line);
1.1       vatton   1605:   strcpy (buffer, "xtiger template=\"");
1.100     vatton   1606:   if (t->uri)
                   1607:     strcat (buffer, t->uri);
                   1608:   else if (t->base_uri)
                   1609:     strcat (buffer, t->uri);
1.17      kia      1610:   strcat (buffer, "\" version=\"");
1.20      vatton   1611:   if (t->version)
                   1612:     strcat (buffer, t->version);
                   1613:   else
                   1614:     strcat (buffer, "0.8");
1.1       vatton   1615:   strcat (buffer, "\"");
1.25      vatton   1616:   if (t->templateVersion)
1.20      vatton   1617:     {
                   1618:       strcat (buffer, " templateVersion=\"");
                   1619:       strcat (buffer, t->templateVersion);
                   1620:       strcat (buffer, "\"");
                   1621:     }
1.1       vatton   1622:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
1.90      vatton   1623:   TtaRegisterElementCreate (elNew, doc);
1.1       vatton   1624:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1625: 
                   1626:   // update the document title
1.47      kia      1627:   if (!strcmp (s, "HTML"))
1.5       vatton   1628:     {
                   1629:       elType.ElTypeNum = HTML_EL_TITLE;
                   1630:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1.90      vatton   1631:       text = TtaGetFirstChild (elFound);
                   1632:       while (text)
1.5       vatton   1633:         {
1.90      vatton   1634:           elType = TtaGetElementType (text);
                   1635:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                   1636:             {
                   1637:               TtaRegisterElementReplace (text, doc);
                   1638:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                   1639:                                  TtaGetDefaultLanguage (), doc);
                   1640:               text = NULL;
                   1641:             }
                   1642:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                   1643:                     elType.ElTypeNum == Template_EL_useSimple) &&
                   1644:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                   1645:             // Ignore the template use element
                   1646:             text = TtaGetFirstChild (text);
                   1647:           else
                   1648:             // Look for the first text child
                   1649:             TtaNextSibling (&text);
1.5       vatton   1650:         }
                   1651:     }
1.1       vatton   1652: #endif /* TEMPLATES */
                   1653: }
                   1654: 
1.80      kia      1655: 
1.1       vatton   1656: /*----------------------------------------------------------------------
1.68      kia      1657:   Template_PreInstantiateComponents
                   1658:   Instantiates all components in order to improve editing.
1.3       vatton   1659:   ----------------------------------------------------------------------*/
1.35      kia      1660: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1661: {
                   1662: #ifdef TEMPLATES 
1.90      vatton   1663:   ForwardIterator iter;
                   1664:   Declaration     dec;
                   1665:   SearchSetNode   node;
                   1666: 
1.25      vatton   1667:   if (!t)
1.23      kia      1668:     return;
                   1669: 
1.90      vatton   1670:   if (Template_IsInstance (t))
                   1671:     {
1.98      vatton   1672: #ifdef TEMPLATE_DEBUG
1.90      vatton   1673:       DumpAllDeclarations();
1.98      vatton   1674: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1675:       iter = SearchSet_GetForwardIterator(GetComponents(t));
1.98      vatton   1676: #ifdef TEMPLATE_DEBUG
1.90      vatton   1677:       printf("Template_PreInstantiateComponents %s\n", t->uri);
1.98      vatton   1678: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1679:       ITERATOR_FOREACH(iter, SearchSetNode, node)
                   1680:         {
                   1681:           dec = (Declaration) node->elem;
1.92      vatton   1682:           ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE);
1.90      vatton   1683:         }
                   1684:       TtaFreeMemory(iter);
1.1       vatton   1685:     }
                   1686: #endif /* TEMPLATES */
                   1687: }
1.84      kia      1688: 
                   1689: /*----------------------------------------------------------------------
                   1690:   Template_SetName
                   1691:   Set the xt:component or xt:union element xt:name attribute.
                   1692:   Make it unique.
                   1693:   Return TRUE if the name is not modified.
                   1694:   ----------------------------------------------------------------------*/
1.96      vatton   1695: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo)
1.84      kia      1696: {
                   1697: #ifdef TEMPLATES 
1.95      vatton   1698:   AttributeType attType;
                   1699:   Attribute     attr;
1.96      vatton   1700:   ThotBool      res, res2;
1.95      vatton   1701: 
                   1702:   if (doc && el && name)
1.84      kia      1703:     {
1.95      vatton   1704:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1705:       attType.AttrTypeNum = Template_ATTR_name;
                   1706:       attr = TtaGetAttribute(el, attType);
                   1707:       if (attr == NULL)
                   1708:         {
                   1709:           attr = TtaNewAttribute (attType);
                   1710:           TtaAttachAttribute (el, attr, doc);
1.96      vatton   1711:           if (withUndo)
                   1712:             TtaRegisterAttributeCreate (attr, el, doc);
1.95      vatton   1713:         }
1.96      vatton   1714:       if (withUndo)
                   1715:          TtaRegisterAttributeReplace(attr, el, doc);
1.95      vatton   1716:       TtaSetAttributeText (attr, name, el, doc);
1.96      vatton   1717:       res = TtaIsValidID (attr, TRUE);
                   1718:       res2 = !MakeUniqueName(el, doc, TRUE, FALSE);
                   1719:       return (res || res2);
                   1720:     }
                   1721: #endif /* TEMPLATES */
                   1722:   return FALSE;
                   1723: }
                   1724: 
                   1725: /*----------------------------------------------------------------------
                   1726:   Template_SetName
                   1727:   Set the xt:component or xt:union element xt:name attribute.
                   1728:   Make it unique.
                   1729:   Return TRUE if the name is not modified.
                   1730:   ----------------------------------------------------------------------*/
                   1731: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo)
                   1732: {
                   1733: #ifdef TEMPLATES 
                   1734:   AttributeType attType;
                   1735:   Attribute     attr;
                   1736: 
                   1737:   if (doc && el && label)
                   1738:     {
                   1739:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1740:       attType.AttrTypeNum = Template_ATTR_title;
                   1741:       attr = TtaGetAttribute(el, attType);
                   1742:       if (attr == NULL)
                   1743:         {
                   1744:           attr = TtaNewAttribute (attType);
                   1745:           TtaAttachAttribute (el, attr, doc);
                   1746:           if (withUndo)
                   1747:             TtaRegisterAttributeCreate (attr, el, doc);
                   1748:         }
                   1749:       if (withUndo)
                   1750:          TtaRegisterAttributeReplace(attr, el, doc);
                   1751:       TtaSetAttributeText (attr, label, el, doc);
1.95      vatton   1752:       return TtaIsValidID (attr, TRUE);
1.84      kia      1753:     }
                   1754: #endif /* TEMPLATES */
                   1755:   return FALSE;
                   1756: }
                   1757: 

Webmaster