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

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.103     vatton    413:   ElementType   elType, otherType;
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:                 {
                    495:                   if (!loading)
                    496:                     // don't apply to a loaded instance
                    497:                     AddPromptIndicator (el, doc);
                    498:                 }
1.92      vatton    499:               else
                    500:                 {
                    501:                   // avoid to have a block element within a pseudo paragraph
1.94      vatton    502:                   dec = Template_GetDeclaration (t, types);
1.92      vatton    503:                   if (dec && dec->blockLevel == 2 && parentLine)
                    504:                     {
                    505:                       // move the use element after the paragraph
                    506:                       child = TtaGetParent (el);
1.94      vatton    507:                       otherType = TtaGetElementType (child);
                    508:                       if (otherType.ElSSchema != elType.ElSSchema ||
                    509:                           otherType.ElTypeNum == Template_EL_repeat)
1.92      vatton    510:                         // not need to move the parent element
                    511:                           child = el;
                    512:                       next = child;
                    513:                       prev = parentLine;
                    514:                       while (child)
                    515:                         {
                    516:                           // move the element and next siblings after the pseudo paragraph
                    517:                           TtaNextSibling (&next);
                    518:                           TtaRemoveTree (child, doc);
                    519:                           TtaInsertSibling (child, prev, FALSE, doc);
                    520:                           prev = child;
                    521:                           child = next;
                    522:                         }
                    523:                       // elements are now out of the parent line
                    524:                       savedInline = NULL;
1.97      vatton    525:                       parentLine = NULL;
1.109     vatton    526:                       child = TtaGetFirstChild (el);
1.92      vatton    527:                     }
1.94      vatton    528: 
                    529:                   // generate the currentType attribute
                    530:                   otherType = TtaGetElementType (child);
1.109     vatton    531:                   if (otherType.ElSSchema == elType.ElSSchema &&
                    532:                       otherType.ElTypeNum == Template_EL_TemplateObject)
1.94      vatton    533:                     {
1.109     vatton    534:                       // not already instantiated
                    535:                       TtaDeleteTree (child, doc);
                    536:                       child = NULL;
1.94      vatton    537:                     }
                    538:                   else
                    539:                     {
1.109     vatton    540:                       attType.AttrTypeNum = Template_ATTR_currentType;
                    541:                       att = TtaGetAttribute (el, attType);
                    542:                       if (att == NULL)
                    543:                         {
                    544:                           att = TtaNewAttribute (attType);
                    545:                           TtaAttachAttribute (el, att, doc);
                    546:                         }
                    547:                       if (otherType.ElTypeNum == 1)
1.121     vatton    548:                         {
                    549:                           if (strstr (types, "string"))
                    550:                             TtaSetAttributeText (att, "string", el, doc);
                    551:                           else
                    552:                             TtaSetAttributeText (att, "number", el, doc);
                    553:                         }
1.109     vatton    554:                       else
                    555:                         {
                    556:                           name = (char *)GetXMLElementName (otherType, doc);
                    557:                           if (name && strcmp (name,"???"))
                    558:                             TtaSetAttributeText (att, name, el, doc);
                    559:                         }
1.94      vatton    560:                     }
1.92      vatton    561:                 }
1.87      vatton    562:             }
1.94      vatton    563:           if (child == NULL)
1.114     vatton    564:             {
1.123   ! vatton    565:               if (IsUseInstantiated (el, doc))
        !           566:                 // complete the use
        !           567:                 InstantiateUse (t, el, doc, parentLine, FALSE, loading);
        !           568:               else
        !           569:                 // keep the use empty
        !           570:                 Template_FillEmpty (el, doc, FALSE);
1.114     vatton    571:             }
1.94      vatton    572:           else
                    573:             {
                    574:               Template_FixAccessRight (t, el, doc);
                    575:               TtaUpdateAccessRightInViews (doc, el);
                    576:             }
1.109     vatton    577:           TtaFreeMemory (types);
1.80      kia       578:           break;
                    579:         case Template_EL_attribute :
                    580:           if (!loading)
                    581:             InstantiateAttribute (t, el, doc);
                    582:           break;
                    583:         case Template_EL_repeat :
1.123   ! vatton    584:           InstantiateRepeat (t, el, doc, parentLine, FALSE, loading);
1.80      kia       585:           break;
                    586:         default :
                    587:           break;
                    588:         }
                    589:     }
1.92      vatton    590:   else if (!strcmp (name, "HTML") &&
                    591:            (elType.ElTypeNum == HTML_EL_Pseudo_paragraph ||
                    592:             elType.ElTypeNum == HTML_EL_Paragraph))
                    593:     parentLine = el;
1.80      kia       594: 
                    595:   child = TtaGetFirstChild (el);
                    596:   while (child)
                    597:     {
1.92      vatton    598:       next = child;
                    599:       TtaNextSibling (&next);
                    600:       parentLine = ParseTemplate (t, child, doc, parentLine, loading);
                    601:       child = next;
1.80      kia       602:     }
1.92      vatton    603:   return savedInline;
1.80      kia       604: #endif /* TEMPLATES */
                    605: }
                    606: 
1.63      kia       607: /*----------------------------------------------------------------------
                    608:   CreateTemplate
                    609:   Create a template from any document.
                    610:   ----------------------------------------------------------------------*/
1.89      vatton    611: void CreateTemplate (Document doc, char *templatePath)
1.63      kia       612: {
                    613: #ifdef TEMPLATES
                    614:   Element           root, head, elem, xt, title, child, last;
                    615:   ElementType       elType, xtType;
1.64      kia       616:   char             *s;
1.63      kia       617:   SSchema           templSchema;
                    618:   XTigerTemplate    t;
                    619:   
1.89      vatton    620:   if (IsTemplateInstanceDocument(doc))
1.63      kia       621:     {
                    622:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_INSTANCE),
                    623:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    624:       return;
                    625:     }
                    626: 
1.89      vatton    627:   if (IsTemplateDocument(doc))
1.63      kia       628:     {
                    629:       ShowMessage(TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_TEMPLATE),
                    630:           TtaGetMessage (AMAYA, AM_TEMPLATE_ERR_CREATION));
                    631:       return;
                    632:     }
                    633:   
1.64      kia       634:   root = TtaGetRootElement(doc);
                    635:   elType = TtaGetElementType (root);
                    636:   // get the target document type
                    637:   s = TtaGetSSchemaName (elType.ElSSchema);
1.63      kia       638:   
1.64      kia       639:   TtaNewNature (doc, elType.ElSSchema,  NULL, "Template", "TemplateP");
1.96      vatton    640:   TtaSetANamespaceDeclaration (doc, root, "xt", Template_URI);
                    641:   templSchema = TtaGetSSchema ("Template", doc);
                    642:   TtaSetUriSSchema (templSchema, Template_URI);
1.64      kia       643: 
                    644:   // Insert xt:head and others
                    645:   TtaSetStructureChecking (FALSE, doc);
                    646:   if (strcmp (s, "HTML") == 0)
1.63      kia       647:     {
1.64      kia       648:       // Initialize the xt:head
                    649:       elType.ElTypeNum = HTML_EL_HEAD;
1.101     vatton    650:       xtType.ElSSchema = templSchema;
1.64      kia       651:       head = TtaSearchTypedElement (elType, SearchInTree, root);
                    652:       if(head)
                    653:         {
                    654:           xtType.ElTypeNum = Template_EL_head;
                    655:           xt = TtaNewElement(doc, xtType);
                    656:           elem = TtaGetLastChild(head);
                    657:           if(elem)
                    658:               TtaInsertSibling(xt, elem, FALSE, doc);
                    659:           else
                    660:               TtaInsertFirstChild(&xt, head, doc);
                    661:           
                    662:           SetAttributeStringValue(xt, Template_ATTR_version, Template_Current_Version);
                    663:           SetAttributeStringValue(xt, Template_ATTR_templateVersion, "1.0");
                    664:         }
1.63      kia       665:       
1.64      kia       666:       // Initialize the document title
                    667:       elType.ElTypeNum = HTML_EL_TITLE;
                    668:       title = TtaSearchTypedElement (elType, SearchInTree, root);
1.96      vatton    669:       if (title)
1.63      kia       670:         {
1.64      kia       671:           // Create xt:use for title
                    672:           xtType.ElTypeNum = Template_EL_useSimple;
1.96      vatton    673:           xt = TtaNewElement (doc, xtType);
1.64      kia       674:           TtaInsertFirstChild(&xt, title, doc);
1.96      vatton    675:           SetAttributeStringValue (xt, Template_ATTR_types, "string");
                    676:           SetAttributeStringValue (xt, Template_ATTR_title, "title");
1.64      kia       677:           
                    678:           // Move current title content to xt:use
                    679:           last = NULL;
                    680:           while(child = TtaGetLastChild(title), child!=NULL)
1.63      kia       681:             {
1.96      vatton    682:               if (child == xt)
1.64      kia       683:                 break;
1.96      vatton    684:               TtaRemoveTree (child, doc);
                    685:               if (last)
                    686:                 TtaInsertSibling (child, last, FALSE, doc);
1.63      kia       687:               else
1.96      vatton    688:                 TtaInsertFirstChild (&child, xt, doc);
1.64      kia       689:               last = child;
1.63      kia       690:             }
                    691:         }
1.64      kia       692:     }
                    693:   else
                    694:     {
                    695:       xtType.ElSSchema = templSchema;
                    696:       xtType.ElTypeNum = Template_EL_head;
1.96      vatton    697:       xt = TtaNewElement (doc, xtType);
                    698:       TtaInsertFirstChild (&xt, root, doc);
                    699:       SetAttributeStringValue (xt, Template_ATTR_version, Template_Current_Version);
                    700:       SetAttributeStringValue (xt, Template_ATTR_templateVersion, "1.0");      
1.64      kia       701:     }
                    702:   // Save changes
                    703:   TtaSetStructureChecking (TRUE, doc);
1.86      vatton    704:   if (DocumentTypes[doc] == docHTML)
                    705:     // avoid positionned boxes to overlap the xt:head section
                    706:     SetBodyAbsolutePosition (doc);
                    707: 
1.64      kia       708:   TtaClearUndoHistory (doc);
                    709:   RemoveParsingErrors (doc);
1.63      kia       710: 
1.64      kia       711:   TtaFreeMemory(DocumentURLs[doc]);
                    712:   DocumentURLs[doc] = TtaStrdup(templatePath);
                    713:   
                    714:   if(DocumentMeta[doc]==NULL)
                    715:     DocumentMeta[doc] = DocumentMetaDataAlloc();
                    716:   
                    717:   DocumentMeta[doc]->method = CE_TEMPLATE;
                    718:   if(DocumentMeta[doc]->initial_url)
                    719:     {
                    720:       TtaFreeMemory(DocumentMeta[doc]->initial_url);
                    721:       DocumentMeta[doc]->initial_url = NULL;
                    722:     }
                    723:   TtaSetDocumentModified (doc);
1.63      kia       724: 
1.64      kia       725:   // Load template-related infos :
                    726:   // like LoadTemplate(..)
                    727:   t = LookForXTigerTemplate(templatePath);
                    728:   t->doc = doc;
1.89      vatton    729:   Template_PrepareTemplate(t, doc);
1.67      kia       730:   //  DocumentTypes[doc] = docTemplate;
                    731:   t->state |= templloaded|templTemplate;
1.63      kia       732: 
1.98      vatton    733: #ifdef TEMPLATE_DEBUG  
1.64      kia       734:     DumpAllDeclarations();
1.98      vatton    735: #endif /* TEMPLATE_DEBUG */
1.64      kia       736:     
                    737:   /* Update the URL combo box */
                    738:   AddURLInCombobox (DocumentURLs[doc], NULL, FALSE);
                    739:   TtaSetTextZone (doc, 1, URL_list);
                    740:   /* Update template menus */
                    741:   UpdateTemplateMenus(doc);
                    742: 
1.63      kia       743: #endif /* TEMPLATES */  
                    744: }
                    745: 
1.1       vatton    746: /*----------------------------------------------------------------------
                    747:   CreateInstance
1.41      vatton    748:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton    749:   ----------------------------------------------------------------------*/
1.99      vatton    750: void CreateInstance (char *templatePath, char *instancePath,
                    751:                      char *docname,  DocumentType docType, int basedoc)
1.3       vatton    752: {
1.1       vatton    753: #ifdef TEMPLATES
1.41      vatton    754:   Document          doc = 0, newdoc = 0;
1.90      vatton    755:   Element           root, title, text;
1.41      vatton    756:   ElementType       elType;
1.90      vatton    757:   CHARSET           charset, ocharset;
                    758:   char             *s, *charsetname, *ocharsetname, *localFile;
1.1       vatton    759: 
1.35      kia       760:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton    761:   if (t == NULL)
1.29      vatton    762:     {
                    763:       // the template cannot be loaded
                    764:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                    765:       return;
                    766:     }
1.41      vatton    767:   // the template document
1.5       vatton    768:   doc = GetTemplateDocument (t);
1.41      vatton    769:   // localize the new created document
                    770:   if (DontReplaceOldDoc)
                    771:     newdoc = TtaGetNextDocumentIndex ();
                    772:   else
                    773:     newdoc = basedoc;
1.90      vatton    774:   
                    775:   // close current undo sepquence in the template document
                    776:   if (TtaHasUndoSequence (doc))
                    777:     TtaCloseUndoSequence (doc);
                    778: 
                    779:   // update the charset if needed
                    780:   charsetname = TtaGetEnvString ("DOCUMENT_CHARSET");
                    781:   charset = TtaGetCharset (charsetname);
                    782:   ocharsetname = DocumentMeta[doc]->charset;
                    783:   ocharset =  TtaGetCharset (ocharsetname);
                    784:   if (charset != UNDEFINED_CHARSET &&
                    785:       DocumentMeta[doc]->charset &&
                    786:       strcmp (charsetname, DocumentMeta[doc]->charset))
                    787:     {
                    788:       TtaSetDocumentCharset (doc, charset, FALSE);
                    789:       DocumentMeta[doc]->charset = TtaStrdup (charsetname);
                    790:       SetNamespacesAndDTD (doc, FALSE);
                    791:     }
1.54      vatton    792: 
1.72      vatton    793:   // register the document type to open the right page model
                    794:   DocumentTypes[newdoc] = docType;
1.90      vatton    795:   // Generate the instance content as a copy of the template
                    796:   localFile = SaveDocumentToNewDoc(doc, newdoc, instancePath);
                    797:   Template_PrepareInstance (instancePath, newdoc, t->version, templatePath);
                    798:   Template_AddReference (t);
                    799: 
                    800:   // Revert template changes
                    801:   TtaSetDocumentCharset (doc, ocharset, FALSE);
1.111     vatton    802:   TtaFreeMemory (DocumentMeta[doc]->charset);
1.90      vatton    803:   DocumentMeta[doc]->charset = ocharsetname;
                    804:   // Now parse the instance
                    805:   // The xtiger PI will be added and components will be removed
                    806:   GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    807:                !DontReplaceOldDoc, NULL, NULL);
                    808:   if (DocumentMeta[newdoc])
                    809:     DocumentMeta[newdoc]->method = CE_ABSOLUTE;
                    810:   // Generate the HTML document title
                    811:   root = TtaGetRootElement(newdoc);
                    812:   elType = TtaGetElementType (root);
                    813:   // get the target document type
                    814:   s = TtaGetSSchemaName (elType.ElSSchema);
                    815:   if (strcmp (s, "HTML") == 0)
1.1       vatton    816:     {
1.90      vatton    817:       // Initialize the document title
                    818:       elType.ElTypeNum = HTML_EL_TITLE;
                    819:       title = TtaSearchTypedElement (elType, SearchInTree, root);
                    820:       text = TtaGetFirstChild (title);
                    821:       while (text)
1.1       vatton    822:         {
1.90      vatton    823:           elType = TtaGetElementType (text);
                    824:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
1.14      vatton    825:             {
1.90      vatton    826:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                    827:                                  TtaGetDefaultLanguage (), newdoc);
                    828:               text = NULL;
                    829:               SetNewTitle (newdoc);
1.14      vatton    830:             }
1.90      vatton    831:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                    832:                     elType.ElTypeNum == Template_EL_useSimple) &&
                    833:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    834:             // Ignore the template use element
                    835:             text = TtaGetFirstChild (text);
                    836:           else
                    837:             // Look for the first text child
                    838:             TtaNextSibling (&text);
1.63      kia       839:         }
1.90      vatton    840:     }
1.54      vatton    841: 
1.90      vatton    842:   // Insert XTiger PI
                    843:   Template_InsertXTigerPI(newdoc, t);   
                    844:   // Parse template to fill structure and remove extra data
1.92      vatton    845:   ParseTemplate (t, root, newdoc, NULL, FALSE);
1.90      vatton    846:   TtaFreeMemory (localFile);
                    847:   TtaClearUndoHistory (newdoc);
                    848:   RemoveParsingErrors (newdoc);
                    849:   TtaSetDocumentModified (newdoc);
                    850:   UpdateTemplateMenus(newdoc);
1.1       vatton    851: #endif /* TEMPLATES */
                    852: }
                    853: 
                    854: 
                    855: #ifdef TEMPLATES
                    856: /*----------------------------------------------------------------------
                    857:   ProcessAttr
1.9       vatton    858:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    859:   ----------------------------------------------------------------------*/
1.1       vatton    860: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    861: {
                    862:   Element      child;
                    863:   ElementType  elType;
                    864: 
                    865:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    866:     {
                    867:       elType = TtaGetElementType (child);
                    868:       if (elType.ElTypeNum == Template_EL_attribute &&
                    869:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    870:         InstantiateAttribute (t, child, doc);
                    871:       else
                    872:         ProcessAttr (t, child, doc);
                    873:     }
                    874: }
                    875: #endif /* TEMPLATES */
                    876: 
1.12      kia       877: 
                    878: /*----------------------------------------------------------------------
                    879:   Template_GetNewSimpleTypeInstance
1.109     vatton    880:   Create an new instance of xt:use/SimpleType for the document doc.
                    881:   Return the new element
1.12      kia       882:   ----------------------------------------------------------------------*/
1.109     vatton    883: Element Template_GetNewSimpleTypeInstance(Document doc)
1.12      kia       884: {
                    885:   Element           newEl = NULL;
                    886: #ifdef TEMPLATES
                    887:   ElementType       elType;
1.73      kia       888:   const char       *empty = " ";
1.24      vatton    889: 
1.38      vatton    890:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       891:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    892:   newEl = TtaNewElement (doc, elType);
                    893:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    894: #endif /* TEMPLATES */
1.12      kia       895:   return newEl;
                    896: }
                    897: 
                    898: /*----------------------------------------------------------------------
                    899:   Template_GetNewXmlElementInstance
1.109     vatton    900:   Create an new instance of xt:use/XmlElement for the document doc.
                    901:   The parameter decl gives the type of the element of new element.
                    902:   Return the new element
1.12      kia       903:   ----------------------------------------------------------------------*/
1.109     vatton    904: Element Template_GetNewXmlElementInstance(Document doc, Declaration decl)
1.12      kia       905: {
                    906:   Element           newEl = NULL;
                    907: #ifdef TEMPLATES
                    908:   ElementType       elType;
                    909: 
1.24      vatton    910:   GIType (decl->name, &elType, doc);
                    911:   if (elType.ElTypeNum != 0)
1.23      kia       912:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       913: #endif /* TEMPLATES */
                    914:   return newEl;
                    915: }
                    916: 
1.34      vatton    917: 
                    918: /*----------------------------------------------------------------------
                    919:   InsertWithNotify applies pre and post functions when inserting the new
                    920:   element el after child (if not NULL) or as first child of parent.
                    921:   ----------------------------------------------------------------------*/
                    922: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    923: {
                    924:   ElementType      elType;
1.116     vatton    925:   AttributeType    attrType;
                    926:   Attribute        attr;
1.34      vatton    927:   NotifyElement    event;
                    928:   char            *name;
                    929:   ThotBool         isRow = FALSE, isCell = FALSE;
1.50      vatton    930:   ThotBool         isImage = FALSE;
                    931:   ThotBool         oldStructureChecking;
                    932: 
                    933:   // avoid to check attributes now
                    934:   oldStructureChecking = TtaGetStructureChecking (doc);
                    935:   TtaSetStructureChecking (FALSE, doc);
1.34      vatton    936: 
                    937:   elType = TtaGetElementType (el);
                    938:   name = TtaGetSSchemaName (elType.ElSSchema);
                    939:   isCell = ((!strcmp (name,"HTML") &&
                    940:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    941:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    942:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    943:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    944:            (!strcmp (name,"MathML") &&
                    945:             (elType.ElTypeNum == MathML_EL_MTR ||
                    946:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
1.50      vatton    947:   isImage = (!strcmp (name,"HTML") && 
                    948:               (elType.ElTypeNum == HTML_EL_IMG || elType.ElTypeNum == HTML_EL_Object));
1.34      vatton    949:   if (child)
                    950:     TtaInsertSibling (el, child, FALSE, doc);
                    951:   else
                    952:     TtaInsertFirstChild (&el, parent, doc);
1.50      vatton    953:   TtaSetStructureChecking (oldStructureChecking, doc);
1.34      vatton    954: 
1.50      vatton    955:   if (isImage)
1.116     vatton    956:     {
                    957:       // check if the src attribute is there
                    958:       attrType.AttrSSchema = elType.ElSSchema;
                    959:       if (elType.ElTypeNum == HTML_EL_IMG)
                    960:         attrType.AttrTypeNum = HTML_ATTR_SRC;
                    961:       else
                    962:          attrType.AttrTypeNum = HTML_ATTR_data;
                    963:       attr = TtaGetAttribute (el, attrType);
                    964:       if (attr == NULL)
                    965:         InsertImageOrObject (el, doc);
                    966:     }
1.50      vatton    967:   else if (isCell)
1.34      vatton    968:     {
                    969:       // a cell is created
1.39      quint     970:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    971:     }
                    972:   else if (isRow)
                    973:     {
                    974:       // a row is created
                    975:       event.element = el;
                    976:       event.document = doc;
                    977:       RowPasted (&event);
                    978:     }
1.50      vatton    979:   
                    980:   if (!strcmp (name,"HTML"))
                    981:     {
1.104     vatton    982:       // special management for images and objets
1.50      vatton    983:       elType.ElTypeNum = HTML_EL_IMG;
                    984:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    985:       while (child)
                    986:         {
                    987:           InsertImageOrObject (child, doc);
                    988:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    989:         }
                    990:       elType.ElTypeNum = HTML_EL_Object;
                    991:       child = TtaSearchTypedElement (elType, SearchInTree, el);
                    992:       while (child)
                    993:         {
                    994:           InsertImageOrObject (child, doc);
                    995:           child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
                    996:         }
                    997:     }
1.34      vatton    998:   return el;
                    999: }
                   1000: 
1.118     vatton   1001: /*----------------------------------------------------------------------
1.16      kia      1002:   Template_InsertUseChildren
                   1003:   Insert children to a xt:use
                   1004:   The dec parameter must be valid and will not be verified. It must be a
                   1005:     direct child element (for union elements).
                   1006:   @param el element (xt:use) in which insert a new element
                   1007:   @param dec Template declaration of the element to insert
                   1008:   @return The inserted element (the xt:use element if insertion is multiple as component)
1.104     vatton   1009:   The parentLine parameter points to the enclosing line if any.
1.16      kia      1010:   ----------------------------------------------------------------------*/
1.104     vatton   1011: Element Template_InsertUseChildren (Document doc, Element el, Declaration dec,
                   1012:                                     Element parentLine, ThotBool registerUndo)
1.16      kia      1013: {
1.99      vatton   1014:   Element         newEl = NULL;
1.16      kia      1015: #ifdef TEMPLATES
1.99      vatton   1016:   Element         current = NULL;
1.104     vatton   1017:   Element         child = NULL, prev, next;
                   1018:   ElementType     childType, elType; 
                   1019:   SSchema         sshtml;
1.99      vatton   1020:   XTigerTemplate  t;
1.17      kia      1021:   
1.25      vatton   1022:   if (TtaGetDocumentAccessMode(doc))
1.16      kia      1023:   {
1.23      kia      1024:     switch (dec->nature)
                   1025:     {
                   1026:       case SimpleTypeNat:
1.109     vatton   1027:         newEl = Template_GetNewSimpleTypeInstance(doc);
1.34      vatton   1028:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia      1029:         break;
                   1030:       case XmlElementNat:
1.109     vatton   1031:         newEl = Template_GetNewXmlElementInstance(doc, dec);
1.34      vatton   1032:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia      1033:         break;
                   1034:       case ComponentNat:
                   1035:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.74      kia      1036:         ProcessAttr (dec->usedIn, newEl, doc);
1.104     vatton   1037:         elType = TtaGetElementType (el);
1.23      kia      1038:         /* Copy elements from new use to existing use. */
1.98      vatton   1039: #ifdef TEMPLATE_DEBUG
1.74      kia      1040:         DumpSubtree(newEl, doc, 0);
1.98      vatton   1041: #endif /* TEMPLATE_DEBUG */
1.104     vatton   1042:         sshtml = TtaGetSSchema ("HTML", doc);
1.102     vatton   1043:         t = GetXTigerDocTemplate( doc);
                   1044:         child = TtaGetFirstChild  (newEl);
1.76      vatton   1045:         while (child)
                   1046:           {
                   1047:             // move the new subtree to the document
                   1048:             TtaRemoveTree (child, doc);
1.104     vatton   1049:             childType = TtaGetElementType (child);
                   1050:             if (parentLine)
                   1051:               {
                   1052:                 if (childType.ElSSchema == sshtml &&
                   1053:                     childType.ElTypeNum == HTML_EL_Pseudo_paragraph)
                   1054:                   {
                   1055:                     prev = TtaGetFirstChild  (child);
                   1056:                     while (prev)
                   1057:                       {
                   1058:                         next = prev;
                   1059:                         TtaNextSibling (&next);
                   1060:                         TtaRemoveTree (prev, doc);
                   1061:                         current = InsertWithNotify (prev, current, el, doc);
                   1062:                         prev = next;
                   1063:                       }
1.108     vatton   1064:                     TtaDeleteTree (child, doc);
1.104     vatton   1065:                   }
1.108     vatton   1066:                 else
                   1067:                   current = InsertWithNotify (child, current, el, doc);
1.104     vatton   1068:               }
                   1069:             else
                   1070:               {
                   1071:                 current = InsertWithNotify (child, current, el, doc);
                   1072:                 // check if a new paragraph is inserted
                   1073:                 if (childType.ElSSchema == sshtml &&
                   1074:                     childType.ElTypeNum == HTML_EL_Paragraph)
                   1075:                   Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                   1076:                 else
                   1077:                   {
                   1078:                     childType.ElSSchema = sshtml;
                   1079:                     childType.ElTypeNum = HTML_EL_Paragraph;
                   1080:                     child = TtaSearchTypedElement (childType, SearchInTree, current);
                   1081:                     while (child)
                   1082:                       {
                   1083:                         Template_SetInline (child, elType.ElSSchema, doc, registerUndo);
                   1084:                         child = TtaSearchTypedElement (childType, SearchInTree, child);
                   1085:                       }
                   1086:                   }
                   1087:               }
1.99      vatton   1088:             child = TtaGetFirstChild (newEl);
1.76      vatton   1089:           }
1.99      vatton   1090: 
1.102     vatton   1091:         TtaDeleteTree (newEl, doc);
1.23      kia      1092:         newEl = el;
                   1093:         break;
                   1094:       default :
                   1095:         //Impossible
                   1096:         break;   
                   1097:     }
1.44      kia      1098:     Template_FixAccessRight (dec->usedIn, el, doc);
1.105     vatton   1099:     if (dec->nature == ComponentNat)
                   1100:       Component_FixAccessRight (el, doc);
1.102     vatton   1101:     TtaUpdateAccessRightInViews (doc, el);
1.23      kia      1102:   }  
1.16      kia      1103: #endif /* TEMPLATES */
                   1104:   return newEl;
                   1105: }
                   1106: 
1.40      kia      1107: 
                   1108: /*----------------------------------------------------------------------
1.105     vatton   1109:   Component_FixAccessRight locks children of the component
                   1110:   ----------------------------------------------------------------------*/
                   1111: void Component_FixAccessRight (Element el, Document doc)
                   1112: {
                   1113: #ifdef TEMPLATES
                   1114:   Element     child;
                   1115:   
                   1116:   if (el && doc)
                   1117:     {
                   1118:       TtaSetAccessRight (el, ReadOnly, doc);
                   1119:       // fix access right to children
                   1120:       child = TtaGetFirstChild (el);
                   1121:       while (child)
                   1122:         {
                   1123:           TtaSetAccessRight (child, ReadOnly, doc);
                   1124:           TtaNextSibling (&child);
                   1125:         }
                   1126:     }
                   1127: #endif /* TEMPLATES */
                   1128: }
                   1129: 
                   1130: /*----------------------------------------------------------------------
1.97      vatton   1131:   Template_FixAccessRight fixes access rights of the el element
1.40      kia      1132:   ----------------------------------------------------------------------*/
1.41      vatton   1133: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia      1134: {
                   1135: #ifdef TEMPLATES
                   1136:   ElementType elType;
                   1137:   Element     child;
1.97      vatton   1138:   Declaration decl;
1.88      vatton   1139:   char        currentType[MAX_LENGTH], *ptr;
1.40      kia      1140:   
                   1141:   if (t && el && doc)
                   1142:     {
                   1143:       elType = TtaGetElementType(el);
1.41      vatton   1144:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia      1145:         {
1.41      vatton   1146:           switch (elType.ElTypeNum)
1.40      kia      1147:             {
                   1148:             case Template_EL_TEXT_UNIT:
1.41      vatton   1149:               //TtaSetAccessRight( el, ReadWrite, doc);
                   1150:               return;
1.105     vatton   1151:             case Template_EL_component:
                   1152:               Component_FixAccessRight (el, doc);
                   1153:               break;
1.40      kia      1154:             case Template_EL_useEl:
                   1155:             case Template_EL_useSimple:
                   1156:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                   1157:                                               (char*)currentType, NULL);
1.88      vatton   1158:               if (currentType[0] == EOS)
                   1159:                 {
                   1160:                   GiveAttributeStringValueFromNum(el, Template_ATTR_types,
                   1161:                                                   (char*)currentType, NULL);
                   1162:                   ptr = strstr (currentType, " ");
                   1163:                   if (ptr)
                   1164:                     *ptr = EOS;
                   1165:                 }
1.40      kia      1166:               decl = Template_GetDeclaration(t, currentType);
                   1167:               if (decl)
                   1168:                 {
                   1169:                   switch (decl->nature)
                   1170:                     {
                   1171:                       case SimpleTypeNat:
1.41      vatton   1172:                         TtaSetAccessRight (el, ReadWrite, doc);
                   1173:                         return;
1.105     vatton   1174:                       case ComponentNat:
1.107     vatton   1175:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1176:                          break;
                   1177:                         //Component_FixAccessRight (el, doc);
                   1178:                         //return;
1.97      vatton   1179:                       case XmlElementNat:
1.105     vatton   1180:                         if (TtaIsSetReadOnly (el))
                   1181:                           break;
1.97      vatton   1182:                         child = TtaGetFirstChild (el);
                   1183:                         if (child)
                   1184:                           TtaSetAccessRight (child, ReadWrite, doc);
1.40      kia      1185:                       default:
1.41      vatton   1186:                         TtaSetAccessRight (el, ReadOnly, doc);
                   1187:                          break;
1.40      kia      1188:                     }
                   1189:                 }
                   1190:               break;
                   1191:             case Template_EL_bag:
1.45      vatton   1192:             case Template_EL_repeat:
1.40      kia      1193:               TtaSetAccessRight(el, ReadWrite, doc);
                   1194:               break;
                   1195:             default:
                   1196:               TtaSetAccessRight(el, ReadOnly, doc);
                   1197:               break;
                   1198:             }
                   1199:         }
                   1200: 
1.92      vatton   1201:       // fix access right to children
1.41      vatton   1202:       child = TtaGetFirstChild (el);
                   1203:       while (child)
1.40      kia      1204:         {
1.41      vatton   1205:           Template_FixAccessRight (t, child, doc);
                   1206:           TtaNextSibling (&child);
1.40      kia      1207:         }
                   1208:     }
                   1209: #endif /* TEMPLATES */
                   1210: }
                   1211: 
1.16      kia      1212: /*----------------------------------------------------------------------
1.46      vatton   1213:   AddPromptIndicator
                   1214:   ----------------------------------------------------------------------*/
                   1215: void AddPromptIndicator (Element el, Document doc)
                   1216: {
                   1217: #ifdef TEMPLATES
                   1218:   ElementType         elType;
                   1219:   AttributeType       attrType;
                   1220:   Attribute           att;
                   1221: 
1.66      vatton   1222:   if (el)
                   1223:     {
                   1224:       elType = TtaGetElementType (el);
                   1225:       attrType.AttrSSchema = elType.ElSSchema;
                   1226:       attrType.AttrTypeNum = Template_ATTR_prompt;
1.69      vatton   1227:       att = TtaGetAttribute (el, attrType);
                   1228:       if (att == NULL)
                   1229:         {
                   1230:           att = TtaNewAttribute (attrType);
                   1231:           TtaAttachAttribute (el, att, doc);
                   1232:         }
1.66      vatton   1233:     }
1.46      vatton   1234: #endif /* TEMPLATES */
                   1235: }
                   1236: 
                   1237: /*----------------------------------------------------------------------
1.123   ! vatton   1238:   AddPromptIndicatorInSubtree
        !          1239:   ----------------------------------------------------------------------*/
        !          1240: void AddPromptIndicatorInSubtree (Element el, Document doc)
        !          1241: {
        !          1242: #ifdef TEMPLATES
        !          1243:   Element             child;
        !          1244:   ElementType         elType;
        !          1245:   char               *types;
        !          1246: 
        !          1247:   if (el)
        !          1248:     {
        !          1249:       elType = TtaGetElementType (el);
        !          1250:       types = GetAttributeStringValueFromNum (el, Template_ATTR_types, NULL);
        !          1251:       if (types &&
        !          1252:           (!strcmp (types, "string") || !strcmp (types, "number")))
        !          1253:         AddPromptIndicator (el, doc);
        !          1254:       else
        !          1255:         {
        !          1256:           child = el;
        !          1257:           while (child)
        !          1258:             {
        !          1259:               child = TtaSearchTypedElementInTree (elType, SearchForward, el, child);
        !          1260:               TtaFreeMemory (types);
        !          1261:               types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
        !          1262:               if (types &&
        !          1263:                   (!strcmp (types, "string") || !strcmp (types, "number")))
        !          1264:                 AddPromptIndicator (child, doc);
        !          1265:             }
        !          1266:         }
        !          1267:       TtaFreeMemory (types);
        !          1268:     }
        !          1269: #endif /* TEMPLATES */
        !          1270: }
        !          1271: 
        !          1272: /*----------------------------------------------------------------------
1.104     vatton   1273:   Template_SetInline manages inline elements
                   1274:   registerUndo says if changes must be registered
                   1275:   ----------------------------------------------------------------------*/
                   1276: void Template_SetInline (Element el, SSchema sstempl, Document doc, ThotBool registerUndo)
                   1277: {
                   1278: #ifdef TEMPLATES
                   1279:   Element         child = NULL;
                   1280:   ElementType     elType1, elType2, elType3;
                   1281: 
                   1282:   if (el)
                   1283:     {
                   1284:       elType1 = TtaGetElementType (el);
                   1285:       if (elType1.ElSSchema == sstempl)
                   1286:         // apply to hte current template element
                   1287:         SetAttributeIntValue (el, Template_ATTR_SetInLine,
                   1288:                               Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1289:       else
                   1290:         elType1.ElSSchema = sstempl;
                   1291:       elType1.ElTypeNum = Template_EL_useSimple;
                   1292:       elType2.ElTypeNum = Template_EL_useEl;
                   1293:       elType2.ElSSchema = elType1.ElSSchema;
                   1294:       elType3.ElTypeNum = Template_EL_repeat;
                   1295:       elType3.ElSSchema = elType1.ElSSchema;
                   1296:       child = TtaSearchElementAmong5Types (elType1, elType2, elType3, elType3, elType3,
                   1297:                                            SearchForward, el);
                   1298:       while (child && TtaIsAncestor (child, el))
                   1299:         {
                   1300:           SetAttributeIntValue (child, Template_ATTR_SetInLine,
                   1301:                                 Template_ATTR_SetInLine_VAL_Yes_, registerUndo);
                   1302:           child = TtaSearchElementAmong5Types (elType1, elType2,
                   1303:                                                elType3, elType3, elType3,
                   1304:                                                SearchForward, child);
                   1305:         }
                   1306:     }
                   1307: #endif /* TEMPLATES */
                   1308: }
                   1309: 
                   1310: /*----------------------------------------------------------------------
                   1311:   InstantiateUse intantiate the use element el.
1.123   ! vatton   1312:   Parameter loading is TRUE when the document is not already loaded.
1.104     vatton   1313:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1314:   ----------------------------------------------------------------------*/
1.1       vatton   1315: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.123   ! vatton   1316:                         Element parentLine, ThotBool registerUndo, ThotBool loading)
1.1       vatton   1317: {
                   1318: #ifdef TEMPLATES
1.109     vatton   1319:   Element          child = NULL;
1.1       vatton   1320:   ElementType      elType;
                   1321:   Declaration      dec;
1.123   ! vatton   1322:   int              size, nbitems, i;
1.1       vatton   1323:   struct menuType  *items;
1.104     vatton   1324:   char             *types;
1.123   ! vatton   1325:   ThotBool          oldStructureChecking;
1.1       vatton   1326: 
1.25      vatton   1327:   if (!t)
1.23      kia      1328:     return NULL;
                   1329: 
1.1       vatton   1330:   /* get the value of the "types" attribute */
1.12      kia      1331:   elType = TtaGetElementType (el);
1.32      vatton   1332:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton   1333:   if (!types || types[0] == EOS)
                   1334:     {
                   1335:       TtaFreeMemory (types);
                   1336:       return NULL;
                   1337:     }
1.123   ! vatton   1338: 
1.104     vatton   1339:   if (!strcmp (types, "string") || !strcmp (types, "number"))
1.109     vatton   1340:     {
                   1341:       child = TtaGetFirstChild (el);
                   1342:       if (child == NULL)
                   1343:         {
                   1344:           child = Template_GetNewSimpleTypeInstance(doc);
                   1345:           child = InsertWithNotify (child, NULL, el, doc);
                   1346:         }
1.123   ! vatton   1347:       if (!loading)
        !          1348:         AddPromptIndicator (el, doc);
1.109     vatton   1349:     }
1.104     vatton   1350:   else
1.1       vatton   1351:     {
1.104     vatton   1352:       giveItems (types, size, &items, &nbitems);
                   1353:       // No structure checking
                   1354:       oldStructureChecking = TtaGetStructureChecking (doc);
                   1355:       TtaSetStructureChecking (FALSE, doc);
1.123   ! vatton   1356:       if (nbitems == 1 || IsUseInstantiated (el, doc))
1.104     vatton   1357:         /* only one type in the "types" attribute */
                   1358:         {
                   1359:           dec = Template_GetDeclaration (t, items[0].label);
                   1360:           if (dec)
1.110     vatton   1361:             child = Template_InsertUseChildren (doc, el, dec, parentLine, registerUndo);
1.112     vatton   1362:           if (nbitems == 1 && elType.ElTypeNum != Template_EL_useSimple)
                   1363:             {
                   1364:               TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
                   1365:               if (registerUndo)
                   1366:                 TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
                   1367:             }
1.123   ! vatton   1368:           if (!loading)
        !          1369:             AddPromptIndicatorInSubtree (el, doc);
1.104     vatton   1370:         }
                   1371:       else
1.123   ! vatton   1372:         Template_FillEmpty (el, doc, FALSE);
1.104     vatton   1373: 
                   1374:       for (i = 0; i < nbitems; i++)
                   1375:         TtaFreeMemory(items[i].label);
                   1376:       TtaFreeMemory(items);
                   1377: 
                   1378:       if (parentLine)
                   1379:         // display the element in line
                   1380:         Template_SetInline (el, elType.ElSSchema, doc, registerUndo);
                   1381:       TtaSetStructureChecking (oldStructureChecking, doc);
1.1       vatton   1382:     }
1.32      vatton   1383:   TtaFreeMemory (types);
1.44      kia      1384:   Template_FixAccessRight (t, el, doc);
                   1385:   TtaUpdateAccessRightInViews (doc, el);
1.109     vatton   1386:   return child;
1.23      kia      1387: #else /* TEMPLATES */
                   1388:   return NULL;
1.1       vatton   1389: #endif /* TEMPLATES */
                   1390: }
                   1391: 
                   1392: /*----------------------------------------------------------------------
1.22      kia      1393:   InstantiateRepeat
                   1394:   Check for min and max param and validate xt:repeat element content.
                   1395:   @param registerUndo True to register undo creation sequences.
1.123   ! vatton   1396:   Parameter loading is TRUE when the document is not already loaded.
1.104     vatton   1397:   The parentLine parameter points to the enclosing line if any.
1.3       vatton   1398:   ----------------------------------------------------------------------*/
1.46      vatton   1399: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
1.123   ! vatton   1400:                         Element parentLine, ThotBool registerUndo, ThotBool loading)
1.1       vatton   1401: {
                   1402: #ifdef TEMPLATES
1.32      vatton   1403:   Element        child, newChild;
1.52      vatton   1404:   ElementType    newElType;
1.123   ! vatton   1405:   Attribute      maxAtt;
        !          1406:   AttributeType  maxType;
1.52      vatton   1407:   char          *text, *types = NULL, *title = NULL;
1.123   ! vatton   1408:   int            minVal, maxVal;
1.32      vatton   1409:   int            childrenCount;
                   1410: 
1.25      vatton   1411:   if (!t)
1.23      kia      1412:     return;
                   1413: 
1.123   ! vatton   1414:   // Preparing types
        !          1415:   newElType = TtaGetElementType(el);
        !          1416:   maxType.AttrSSchema =  newElType.ElSSchema;
1.1       vatton   1417:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
1.123   ! vatton   1418:   // Get minOccurs and maxOccurs attributes
1.1       vatton   1419:   maxAtt = TtaGetAttribute (el, maxType);
1.123   ! vatton   1420:   minVal = GetMinOccurence (el, doc);;
1.1       vatton   1421: 
                   1422:   if (maxAtt)
                   1423:     {
1.10      kia      1424:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton   1425:       if (text)
                   1426:         {
                   1427:           if (!strcmp (text, "*"))
                   1428:             maxVal = INT_MAX;
                   1429:           else
                   1430:             maxVal = atoi (text);
                   1431:           TtaFreeMemory (text);
                   1432:         }
                   1433:       else
                   1434:         //Error : Attribute with no value
                   1435:         return;
                   1436:     }
                   1437:   else
                   1438:     maxVal = INT_MAX;
                   1439: 
1.52      vatton   1440:   if (maxAtt == NULL)
1.1       vatton   1441:     {  
1.32      vatton   1442:       maxAtt = TtaNewAttribute (maxType);
1.123   ! vatton   1443:       text = (char*)TtaGetMemory(MAX_LENGTH);
        !          1444:        if (maxVal < INT_MAX)
1.32      vatton   1445:         sprintf(text, "%d", maxVal);
1.1       vatton   1446:       else
1.32      vatton   1447:         sprintf (text, "*");
                   1448:       TtaAttachAttribute (el, maxAtt, doc);      
                   1449:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton   1450:       if (registerUndo)
1.32      vatton   1451:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.123   ! vatton   1452:       TtaFreeMemory (text);
1.1       vatton   1453:     }
                   1454: 
1.52      vatton   1455:   //We must have minOccurs children
1.104     vatton   1456:   child = TtaGetFirstChild (el);
1.122     vatton   1457:   if (!child)
                   1458:     //Error : a repeat must have at least one child which will be the model
                   1459:     return;
                   1460:   
1.104     vatton   1461:   for (childrenCount = 0; child; TtaNextSibling(&child))
                   1462:     //TODO : Check that every child is valid
                   1463:     childrenCount ++;
1.1       vatton   1464: 
                   1465:   if (childrenCount > maxVal)
                   1466:     //Error : too many children!
                   1467:     return;  
                   1468: 
1.122     vatton   1469: 
1.104     vatton   1470:   if (parentLine)
                   1471:     // display the element in line
1.123   ! vatton   1472:     Template_SetInline (el, newElType.ElSSchema, doc, registerUndo);
1.104     vatton   1473: 
1.1       vatton   1474:   child = TtaGetLastChild(el);
1.32      vatton   1475:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                   1476:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
1.52      vatton   1477:   newElType.ElTypeNum = Template_EL_useEl;
1.123   ! vatton   1478:   while (childrenCount < minVal)
1.1       vatton   1479:     {
1.32      vatton   1480:       newChild = TtaNewElement (doc, newElType);
1.27      kia      1481:       // Insert it
1.32      vatton   1482:       TtaInsertSibling (newChild, child, FALSE, doc);
                   1483:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                   1484:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.123   ! vatton   1485:       InstantiateUse (t, newChild, doc, parentLine, TRUE, loading);
1.27      kia      1486:       
1.25      vatton   1487:       if (registerUndo)
1.34      vatton   1488:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton   1489:       child = newChild;
                   1490:       childrenCount++;
                   1491:     }
1.27      kia      1492:     
1.44      kia      1493:   Template_FixAccessRight (t, el, doc);
                   1494:   TtaUpdateAccessRightInViews (doc, el);
1.32      vatton   1495:   TtaFreeMemory (types);
                   1496:   TtaFreeMemory (title);
1.1       vatton   1497: #endif /* TEMPLATES */
                   1498: }
                   1499: 
                   1500: /*----------------------------------------------------------------------
1.80      kia      1501:   Template_InsertXTigerPI
                   1502:   Insert the XTiger PI element in template instance.
                   1503:   Param t is the XTigerTemplate structure of the template,
                   1504:   not the template instance one.
1.3       vatton   1505:   ----------------------------------------------------------------------*/
1.80      kia      1506: void Template_InsertXTigerPI(Document doc, XTigerTemplate t)
1.1       vatton   1507: {
                   1508: #ifdef TEMPLATES
1.47      kia      1509:   ElementType     elType;
1.65      vatton   1510:   Element         root, piElem, doctype, line, text, elNew, elFound;
1.47      kia      1511:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton   1512:   int             pi_type;
                   1513: 
1.80      kia      1514:   if (!t || !doc)
1.23      kia      1515:     return;
1.47      kia      1516: 
1.80      kia      1517:   root =  TtaGetMainRoot (doc);
1.90      vatton   1518:   if (root == NULL)
                   1519:     return;
1.1       vatton   1520:   //Look for PIs
                   1521:   /* check if the document has a DOCTYPE declaration */
                   1522: #ifdef ANNOTATIONS
                   1523:   if (DocumentTypes[doc]  == docAnnot)
                   1524:     elType = TtaGetElementType (root);
                   1525:   else
                   1526: #endif /* ANNOTATIONS */
                   1527:     elType = TtaGetElementType (root);
                   1528:   s = TtaGetSSchemaName (elType.ElSSchema);
                   1529:   if (strcmp (s, "HTML") == 0)
                   1530:     {
                   1531:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                   1532:       pi_type = HTML_EL_XMLPI;
                   1533:     }
                   1534: #ifdef _SVG
                   1535:   else if (strcmp (s, "SVG") == 0)
                   1536:     {
                   1537:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                   1538:       pi_type = SVG_EL_XMLPI;
                   1539:     }
                   1540: #endif /* _SVG */
                   1541:   else if (strcmp (s, "MathML") == 0)
                   1542:     {
                   1543:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                   1544:       pi_type = MathML_EL_XMLPI;
                   1545:     }
                   1546:   else
                   1547:     {
                   1548:       elType.ElTypeNum = XML_EL_doctype;
                   1549:       pi_type = XML_EL_xmlpi;
                   1550:     }
1.54      vatton   1551: 
1.1       vatton   1552:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
1.65      vatton   1553:   if (doctype == NULL)
1.1       vatton   1554:     {
1.65      vatton   1555:       elType.ElTypeNum = pi_type;      
                   1556:       piElem = TtaSearchTypedElement (elType, SearchInTree, root);
                   1557:       if (piElem == NULL)
                   1558:         {
                   1559:           /* generate the XML declaration */
                   1560:           /* Check the Thot abstract tree against the structure schema. */
                   1561:           TtaSetStructureChecking (FALSE, doc);
                   1562:           piElem = TtaNewTree (doc, elType, "");
                   1563:           TtaInsertFirstChild (&piElem, root, doc);
                   1564:           line = TtaGetFirstChild (piElem);
                   1565:           text = TtaGetFirstChild (line);
                   1566:           strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                   1567:           charsetname = UpdateDocumentCharset (doc);
                   1568:           strcat (buffer, charsetname);
                   1569:           strcat (buffer, "\"");
                   1570:           TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                   1571:           TtaSetStructureChecking (TRUE, doc);
1.90      vatton   1572:           TtaFreeMemory (charsetname);
                   1573:           TtaRegisterElementCreate (piElem, doc);
1.65      vatton   1574:         }
1.1       vatton   1575:     }
                   1576:   
                   1577:   /* generate the XTiger PI */
                   1578:   /* Check the Thot abstract tree against the structure schema. */
                   1579:   TtaSetStructureChecking (FALSE, doc);
                   1580:   elType.ElTypeNum = pi_type;
1.65      vatton   1581:   elNew = TtaNewTree (doc, elType, "");
                   1582:   if (doctype)
                   1583:     TtaInsertSibling (elNew, doctype, FALSE, doc);
                   1584:   else
                   1585:     TtaInsertSibling (elNew, piElem, FALSE, doc);
                   1586:   line = TtaGetFirstChild (elNew);
                   1587:   text = TtaGetFirstChild (line);
1.1       vatton   1588:   strcpy (buffer, "xtiger template=\"");
1.100     vatton   1589:   if (t->uri)
                   1590:     strcat (buffer, t->uri);
                   1591:   else if (t->base_uri)
                   1592:     strcat (buffer, t->uri);
1.17      kia      1593:   strcat (buffer, "\" version=\"");
1.20      vatton   1594:   if (t->version)
                   1595:     strcat (buffer, t->version);
                   1596:   else
                   1597:     strcat (buffer, "0.8");
1.1       vatton   1598:   strcat (buffer, "\"");
1.25      vatton   1599:   if (t->templateVersion)
1.20      vatton   1600:     {
                   1601:       strcat (buffer, " templateVersion=\"");
                   1602:       strcat (buffer, t->templateVersion);
                   1603:       strcat (buffer, "\"");
                   1604:     }
1.1       vatton   1605:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
1.90      vatton   1606:   TtaRegisterElementCreate (elNew, doc);
1.1       vatton   1607:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton   1608: 
                   1609:   // update the document title
1.47      kia      1610:   if (!strcmp (s, "HTML"))
1.5       vatton   1611:     {
                   1612:       elType.ElTypeNum = HTML_EL_TITLE;
                   1613:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
1.90      vatton   1614:       text = TtaGetFirstChild (elFound);
                   1615:       while (text)
1.5       vatton   1616:         {
1.90      vatton   1617:           elType = TtaGetElementType (text);
                   1618:           if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                   1619:             {
                   1620:               TtaRegisterElementReplace (text, doc);
                   1621:               TtaSetTextContent (text, (unsigned char*)Answer_text,
                   1622:                                  TtaGetDefaultLanguage (), doc);
                   1623:               text = NULL;
                   1624:             }
                   1625:           else if ((elType.ElTypeNum == Template_EL_useEl ||
                   1626:                     elType.ElTypeNum == Template_EL_useSimple) &&
                   1627:                    !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                   1628:             // Ignore the template use element
                   1629:             text = TtaGetFirstChild (text);
                   1630:           else
                   1631:             // Look for the first text child
                   1632:             TtaNextSibling (&text);
1.5       vatton   1633:         }
                   1634:     }
1.1       vatton   1635: #endif /* TEMPLATES */
                   1636: }
                   1637: 
1.80      kia      1638: 
1.1       vatton   1639: /*----------------------------------------------------------------------
1.68      kia      1640:   Template_PreInstantiateComponents
                   1641:   Instantiates all components in order to improve editing.
1.3       vatton   1642:   ----------------------------------------------------------------------*/
1.35      kia      1643: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton   1644: {
                   1645: #ifdef TEMPLATES 
1.90      vatton   1646:   ForwardIterator iter;
                   1647:   Declaration     dec;
                   1648:   SearchSetNode   node;
                   1649: 
1.25      vatton   1650:   if (!t)
1.23      kia      1651:     return;
                   1652: 
1.90      vatton   1653:   if (Template_IsInstance (t))
                   1654:     {
1.98      vatton   1655: #ifdef TEMPLATE_DEBUG
1.90      vatton   1656:       DumpAllDeclarations();
1.98      vatton   1657: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1658:       iter = SearchSet_GetForwardIterator(GetComponents(t));
1.98      vatton   1659: #ifdef TEMPLATE_DEBUG
1.90      vatton   1660:       printf("Template_PreInstantiateComponents %s\n", t->uri);
1.98      vatton   1661: #endif /* TEMPLATE_DEBUG */  
1.90      vatton   1662:       ITERATOR_FOREACH(iter, SearchSetNode, node)
                   1663:         {
                   1664:           dec = (Declaration) node->elem;
1.92      vatton   1665:           ParseTemplate(t, GetComponentContent(dec), GetTemplateDocument(t), NULL, TRUE);
1.90      vatton   1666:         }
                   1667:       TtaFreeMemory(iter);
1.1       vatton   1668:     }
                   1669: #endif /* TEMPLATES */
                   1670: }
1.84      kia      1671: 
                   1672: /*----------------------------------------------------------------------
                   1673:   Template_SetName
                   1674:   Set the xt:component or xt:union element xt:name attribute.
                   1675:   Make it unique.
                   1676:   Return TRUE if the name is not modified.
                   1677:   ----------------------------------------------------------------------*/
1.96      vatton   1678: ThotBool Template_SetName (Document doc, Element el, const char *name, ThotBool withUndo)
1.84      kia      1679: {
                   1680: #ifdef TEMPLATES 
1.95      vatton   1681:   AttributeType attType;
                   1682:   Attribute     attr;
1.96      vatton   1683:   ThotBool      res, res2;
1.95      vatton   1684: 
                   1685:   if (doc && el && name)
1.84      kia      1686:     {
1.95      vatton   1687:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1688:       attType.AttrTypeNum = Template_ATTR_name;
                   1689:       attr = TtaGetAttribute(el, attType);
                   1690:       if (attr == NULL)
                   1691:         {
                   1692:           attr = TtaNewAttribute (attType);
                   1693:           TtaAttachAttribute (el, attr, doc);
1.96      vatton   1694:           if (withUndo)
                   1695:             TtaRegisterAttributeCreate (attr, el, doc);
1.95      vatton   1696:         }
1.96      vatton   1697:       if (withUndo)
                   1698:          TtaRegisterAttributeReplace(attr, el, doc);
1.95      vatton   1699:       TtaSetAttributeText (attr, name, el, doc);
1.96      vatton   1700:       res = TtaIsValidID (attr, TRUE);
                   1701:       res2 = !MakeUniqueName(el, doc, TRUE, FALSE);
                   1702:       return (res || res2);
                   1703:     }
                   1704: #endif /* TEMPLATES */
                   1705:   return FALSE;
                   1706: }
                   1707: 
                   1708: /*----------------------------------------------------------------------
                   1709:   Template_SetName
                   1710:   Set the xt:component or xt:union element xt:name attribute.
                   1711:   Make it unique.
                   1712:   Return TRUE if the name is not modified.
                   1713:   ----------------------------------------------------------------------*/
                   1714: ThotBool Template_SetLabel (Document doc, Element el, const char *label, ThotBool withUndo)
                   1715: {
                   1716: #ifdef TEMPLATES 
                   1717:   AttributeType attType;
                   1718:   Attribute     attr;
                   1719: 
                   1720:   if (doc && el && label)
                   1721:     {
                   1722:       attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                   1723:       attType.AttrTypeNum = Template_ATTR_title;
                   1724:       attr = TtaGetAttribute(el, attType);
                   1725:       if (attr == NULL)
                   1726:         {
                   1727:           attr = TtaNewAttribute (attType);
                   1728:           TtaAttachAttribute (el, attr, doc);
                   1729:           if (withUndo)
                   1730:             TtaRegisterAttributeCreate (attr, el, doc);
                   1731:         }
                   1732:       if (withUndo)
                   1733:          TtaRegisterAttributeReplace(attr, el, doc);
                   1734:       TtaSetAttributeText (attr, label, el, doc);
1.95      vatton   1735:       return TtaIsValidID (attr, TRUE);
1.84      kia      1736:     }
                   1737: #endif /* TEMPLATES */
                   1738:   return FALSE;
                   1739: }
                   1740: 

Webmaster