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

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

Webmaster