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

1.19      vatton      1: /*
                      2:  *
                      3:  *  COPYRIGHT INRIA and W3C, 2006-2007
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7: 
1.1       vatton      8: #include "templates.h"
                      9: 
                     10: #define THOT_EXPORT extern
                     11: #include "templateDeclarations.h"
                     12: 
1.8       kia        13: #include "Elemlist.h"
                     14: 
1.41      vatton     15: #include "AHTURLTools_f.h"
1.1       vatton     16: #include "EDITimage_f.h"
                     17: #include "HTMLactions_f.h"
                     18: #include "HTMLsave_f.h"
1.34      vatton     19: #include "HTMLtable_f.h"
1.1       vatton     20: #include "init_f.h"
                     21: #include "templates_f.h"
                     22: #include "templateDeclarations_f.h"
                     23: #include "templateInstantiate_f.h"
                     24: #include "Templatebuilder_f.h"
                     25: #include "templateUtils_f.h"
                     26: #include "fetchHTMLname_f.h"
                     27: #include "Template.h"
                     28: 
                     29: #ifdef TEMPLATES
                     30: #define TEMPLATE_SCHEMA_NAME "Template"
                     31: 
                     32: typedef struct _InstantiateCtxt
                     33: {
1.47    ! kia        34:   char         *templatePath;
        !            35:   char         *instancePath;
        !            36:   char         *schemaName;
1.41      vatton     37:   Document      doc;
1.47    ! kia        38:   DocumentType  docType;
        !            39:   ThotBool      dontReplace;
1.1       vatton     40: } InstantiateCtxt;
                     41: #endif /* TEMPLATES */
                     42: 
                     43: 
                     44: /*----------------------------------------------------------------------
                     45:   CreateInstance
1.41      vatton     46:   basedoc is the displayed doc that launchs the creation of instance
1.3       vatton     47:   ----------------------------------------------------------------------*/
1.41      vatton     48: void  CreateInstance(char *templatePath, char *instancePath, int basedoc)
1.3       vatton     49: {
1.1       vatton     50: #ifdef TEMPLATES
1.41      vatton     51:   Document          doc = 0, newdoc = 0;
                     52:   DocumentType      docType;
                     53:   ElementType       elType;
                     54:   Element           root, title, text;
                     55:   char             *localFile, *s;
1.1       vatton     56: 
1.35      kia        57:   XTigerTemplate t = GetXTigerTemplate(templatePath);
1.1       vatton     58:   if (t == NULL)
1.29      vatton     59:     {
                     60:       // the template cannot be loaded
                     61:       InitConfirm (doc, 1, TtaGetMessage (AMAYA, AM_BAD_TEMPLATE));
                     62:       return;
                     63:     }
1.41      vatton     64:   // the template document
1.5       vatton     65:   doc = GetTemplateDocument (t);
1.41      vatton     66:   // localize the new created document
                     67:   if (DontReplaceOldDoc)
                     68:     newdoc = TtaGetNextDocumentIndex ();
                     69:   else
                     70:     newdoc = basedoc;
1.43      vatton     71: #ifndef IV
1.41      vatton     72:   localFile = GetLocalPath (newdoc, instancePath);
                     73: #else
                     74:   localFile = TtaStrdup (instancePath);
                     75: #endif
1.1       vatton     76:   if (!TtaPrepareUndo (doc))
                     77:     {
                     78:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.14      vatton     79:       root = TtaGetRootElement(doc);
                     80:       elType = TtaGetElementType (root);
                     81:       // get the target document type
                     82:       s = TtaGetSSchemaName (elType.ElSSchema);
                     83:       if (strcmp (s, "HTML") == 0)
                     84:         docType = docHTML;
                     85:       else if (strcmp (s, "SVG") == 0)
                     86:         docType = docSVG;
                     87:       else if (strcmp (s, "MathML") == 0)
                     88:         docType = docMath;
                     89:       else
                     90:         docType = docXml;
1.30      vatton     91:       // update all links
1.33      vatton     92:       SetRelativeURLs (doc, instancePath, "", FALSE, FALSE, FALSE);
1.41      vatton     93: 
                     94:       // prepare the new document view
                     95:       TtaExtractName (instancePath, DirectoryName, DocumentName);
                     96:       // save in the local path
1.1       vatton     97:       switch (docType)
                     98:         {
1.14      vatton     99:         case docSVG:
1.41      vatton    100:           TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT");
1.1       vatton    101:           break;
1.14      vatton    102:         case docMath:
1.41      vatton    103:           TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT");
1.1       vatton    104:           break;
1.14      vatton    105:         case docHTML:
                    106:           // Initialize the document title
                    107:           elType.ElTypeNum = HTML_EL_TITLE;
                    108:           title = TtaSearchTypedElement (elType, SearchInTree, root);
                    109:           text = TtaGetFirstChild (title);
                    110:           while (text)
                    111:             {
                    112:               elType = TtaGetElementType (text);
                    113:               if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
                    114:                 {
                    115:                   TtaSetTextContent (text, (unsigned char*)Answer_text,
                    116:                                      TtaGetDefaultLanguage (), doc);
                    117:                   text = NULL;
                    118:                 }
                    119:               else if ((elType.ElTypeNum == Template_EL_useEl ||
                    120:                         elType.ElTypeNum == Template_EL_useSimple) &&
                    121:                        !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
                    122:                 // Ignore the template use element
                    123:                 text = TtaGetFirstChild (text);
                    124:               else
                    125:                 // Look for the first text child
                    126:                 TtaNextSibling (&text);
                    127:             }
1.41      vatton    128:           if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
                    129:               TtaGetDocumentProfile(doc) == L_Basic)
                    130:             TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11");
1.1       vatton    131:           else
1.41      vatton    132:             TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX");
1.1       vatton    133:           break;
1.14      vatton    134:         default:
1.41      vatton    135:           TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL);
1.1       vatton    136:           break;
                    137:         }
1.47    ! kia       138: 
1.1       vatton    139:       TtaCloseUndoSequence (doc);
                    140:       TtaUndoNoRedo (doc);
                    141:       TtaClearUndoHistory (doc);
1.41      vatton    142:       RemoveParsingErrors (doc);
1.1       vatton    143: 
1.43      vatton    144: #ifndef IV
1.41      vatton    145:       GetAmayaDoc (instancePath, NULL, basedoc, basedoc, CE_INSTANCE,
                    146:                    !DontReplaceOldDoc, NULL, NULL);
                    147:       TtaSetDocumentModified (newdoc);
                    148: #else
1.1       vatton    149:       CallbackDialogue (BaseDialog + OpenForm, INTEGER_DATA, (char *) 1);
1.41      vatton    150: #endif
1.1       vatton    151:     }
1.41      vatton    152:   TtaFreeMemory (localFile);
1.1       vatton    153: #endif /* TEMPLATES */
                    154: }
                    155: 
                    156: /*----------------------------------------------------------------------
1.3       vatton    157:   ----------------------------------------------------------------------*/
1.41      vatton    158: void InstantiateTemplate_callback (int newdoc, int status,  char *urlName,
                    159:                                    char *outputfile,
                    160:                                    char *proxyName, AHTHeaders *http_headers,
                    161:                                    void * context)
1.1       vatton    162: {
                    163: #ifdef TEMPLATES
1.47    ! kia       164:   InstantiateCtxt *ctx = (InstantiateCtxt*)context;
1.1       vatton    165: 
1.47    ! kia       166:   DoInstanceTemplate (ctx->templatePath);
1.41      vatton    167:   CreateInstance (ctx->templatePath, ctx->instancePath, ctx->doc);
1.1       vatton    168:   TtaFreeMemory (ctx->templatePath);
                    169:   TtaFreeMemory (ctx->instancePath);
                    170:   TtaFreeMemory (ctx);
                    171: #endif /* TEMPLATES */
                    172: }
                    173: 
                    174: /*----------------------------------------------------------------------
1.3       vatton    175:   ----------------------------------------------------------------------*/
1.1       vatton    176: void InstantiateTemplate (Document doc, char *templatename, char *docname,
                    177:                           DocumentType docType, ThotBool loaded)
                    178: {
                    179: #ifdef TEMPLATES
1.47    ! kia       180:   if (!loaded)
1.3       vatton    181:     {
                    182:       // Create the callback context
                    183:       InstantiateCtxt *ctx = (InstantiateCtxt *)TtaGetMemory (sizeof (InstantiateCtxt));
                    184:       ctx->templatePath        = TtaStrdup (templatename);
                    185:       ctx->instancePath        = TtaStrdup (docname);
                    186:       ctx->schemaName = GetSchemaFromDocType(docType);
1.41      vatton    187:       ctx->doc = doc;
1.3       vatton    188:       ctx->docType = docType;
1.47    ! kia       189: 
1.3       vatton    190:       GetAmayaDoc (templatename, NULL, doc, doc, CE_MAKEBOOK, FALSE, 
1.41      vatton    191:                    (void (*)(int, int, char*, char*, char*,
                    192:                              const AHTHeaders*, void*)) InstantiateTemplate_callback,
1.3       vatton    193:                    (void *) ctx);
                    194:     }
1.47    ! kia       195:   else
1.1       vatton    196:     {
                    197:       DoInstanceTemplate (templatename);
1.41      vatton    198:       CreateInstance (templatename, docname, doc);
1.3       vatton    199:     }  
1.1       vatton    200: #endif /* TEMPLATES */
                    201: }
                    202: 
                    203: /*----------------------------------------------------------------------
                    204:   InstantiateAttribute
1.3       vatton    205:   ----------------------------------------------------------------------*/
1.1       vatton    206: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
                    207: {
                    208: #ifdef TEMPLATES
                    209:   AttributeType  useType, nameType, defaultType, attrType;
                    210:   Attribute      useAttr, nameAttr, defAttr, attr;
                    211:   ElementType    elType;
                    212:   Element        parent;
                    213:   char           *text, *elementName;
                    214:   ThotBool       level;
                    215:   NotifyAttribute event;
                    216: 
                    217:   parent = TtaGetParent (el);
                    218:   if (!parent)
                    219:     return;
                    220:   // if attribute "use" has value "optional", don't do anything
                    221:   useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    222:   useType.AttrTypeNum = Template_ATTR_useAt;
                    223:   useAttr = TtaGetAttribute (el, useType);
                    224:   if (useAttr)
                    225:     // there is a "use" attribute. Check its value
                    226:     {
1.10      kia       227:       text = GetAttributeStringValue (el, useAttr, NULL);
1.25      vatton    228:       if (!text)
1.1       vatton    229:         return;
1.25      vatton    230:       if (strcmp (text, "optional") == 0)
1.18      kia       231:       {
                    232:         TtaFreeMemory(text);
                    233:         return;
                    234:       }
                    235:       TtaFreeMemory(text);
1.1       vatton    236:     }
1.18      kia       237:     
1.1       vatton    238:   // get the "name" and "default" attributes
                    239:   nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.9       vatton    240:   nameType.AttrTypeNum = Template_ATTR_ref_name;
1.1       vatton    241:   defaultType.AttrTypeNum = Template_ATTR_defaultAt;
                    242:   nameAttr = TtaGetAttribute (el, nameType);
                    243:   defAttr = TtaGetAttribute (el, defaultType);
                    244:   if (nameAttr)
                    245:     {
1.10      kia       246:       text = GetAttributeStringValue (el, nameAttr, NULL);
1.1       vatton    247:       if (text)
                    248:         {
                    249:           elType = TtaGetElementType (parent);
                    250:           elementName = TtaGetElementTypeName (elType);
                    251:           level = TRUE;
                    252:           MapHTMLAttribute (text, &attrType, elementName, &level, doc);
                    253:           TtaFreeMemory(text);
                    254:           attr = TtaNewAttribute (attrType);
                    255:           if (attr)
                    256:             {
                    257:               TtaAttachAttribute (parent, attr, doc);
                    258:               if (defAttr)
                    259:                 {
1.10      kia       260:                   text = GetAttributeStringValue (el, defAttr, NULL);
1.25      vatton    261:                   if (text)
1.18      kia       262:                     TtaSetAttributeText(attr, text, parent, doc);
1.1       vatton    263:                   TtaFreeMemory(text);
                    264:                   // if it's a src arttribute for an image, load the image
                    265:                   if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
                    266:                       elType.ElTypeNum == HTML_EL_IMG)
                    267:                     if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
                    268:                         attrType.AttrSSchema == elType.ElSSchema)
                    269:                       {
                    270:                         event.document = doc;
                    271:                         event.element = parent;
                    272:                         event.attribute = attr;
                    273:                         SRCattrModified (&event);
                    274:                       }
                    275:                 }
                    276:             }
                    277:         }
                    278:     }
                    279: #endif /* TEMPLATES */
                    280: }
                    281: 
                    282: #ifdef TEMPLATES
                    283: /*----------------------------------------------------------------------
                    284:   ProcessAttr
1.9       vatton    285:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    286:   ----------------------------------------------------------------------*/
1.1       vatton    287: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    288: {
                    289:   Element      child;
                    290:   ElementType  elType;
                    291: 
                    292:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    293:     {
                    294:       elType = TtaGetElementType (child);
                    295:       if (elType.ElTypeNum == Template_EL_attribute &&
                    296:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    297:         InstantiateAttribute (t, child, doc);
                    298:       else
                    299:         ProcessAttr (t, child, doc);
                    300:     }
                    301: }
                    302: #endif /* TEMPLATES */
                    303: 
1.12      kia       304: 
                    305: /*----------------------------------------------------------------------
                    306:   Template_GetNewSimpleTypeInstance
                    307:   Create an new instance of xt:use/SimpleType
                    308:   The decl attribute must embed SimpleType declaration (no validation).
                    309:   @param decl Declaration of new element
                    310:   @param parent Future parent element
                    311:   @param doc Document
                    312:   @return The new element
                    313:   ----------------------------------------------------------------------*/
                    314: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl)
                    315: {
                    316:   Element           newEl = NULL;
                    317: #ifdef TEMPLATES
                    318:   ElementType       elType;
                    319:   char             *empty = " ";
1.24      vatton    320: 
1.38      vatton    321:   elType.ElSSchema = TtaGetSSchema("Template", doc);
1.12      kia       322:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    323:   newEl = TtaNewElement (doc, elType);
                    324:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
1.24      vatton    325: #endif /* TEMPLATES */
1.12      kia       326:   return newEl;
                    327: }
                    328: 
                    329: /*----------------------------------------------------------------------
                    330:   Template_GetNewXmlElementInstance
                    331:   Create an new instance of xt:use/XmlElement
                    332:   The decl attribute must embed XmlElement declaration (no validation).
                    333:   @param decl Declaration of new element
                    334:   @param parent Future parent element
                    335:   @param doc Document
                    336:   @return The new element
                    337:   ----------------------------------------------------------------------*/
                    338: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
                    339: {
                    340:   Element           newEl = NULL;
                    341: #ifdef TEMPLATES
                    342:   ElementType       elType;
                    343: 
1.24      vatton    344:   GIType (decl->name, &elType, doc);
                    345:   if (elType.ElTypeNum != 0)
1.12      kia       346:   {
1.23      kia       347:     newEl = TtaNewTree (doc, elType, "");
1.12      kia       348:   }
                    349: #endif /* TEMPLATES */
                    350:   return newEl;
                    351: }
                    352: 
1.34      vatton    353: 
                    354: /*----------------------------------------------------------------------
                    355:   InsertWithNotify applies pre and post functions when inserting the new
                    356:   element el after child (if not NULL) or as first child of parent.
                    357:   ----------------------------------------------------------------------*/
                    358: Element InsertWithNotify (Element el, Element child, Element parent, Document doc)
                    359: {
                    360:   ElementType      elType;
                    361:   NotifyElement    event;
                    362:   char            *name;
                    363:   ThotBool         isRow = FALSE, isCell = FALSE;
                    364: 
                    365:   elType = TtaGetElementType (el);
                    366:   name = TtaGetSSchemaName (elType.ElSSchema);
                    367:   isCell = ((!strcmp (name,"HTML") &&
                    368:              elType.ElTypeNum == HTML_EL_Data_cell ||
                    369:              elType.ElTypeNum == HTML_EL_Heading_cell) ||
                    370:             (!strcmp (name,"MathML") && elType.ElTypeNum == MathML_EL_MTD));
                    371:   isRow = ((!strcmp (name,"HTML") && elType.ElTypeNum == HTML_EL_Table_row) ||
                    372:            (!strcmp (name,"MathML") &&
                    373:             (elType.ElTypeNum == MathML_EL_MTR ||
                    374:              elType.ElTypeNum == MathML_EL_MLABELEDTR)));
                    375:   if (child)
                    376:     TtaInsertSibling (el, child, FALSE, doc);
                    377:   else
                    378:     TtaInsertFirstChild (&el, parent, doc);
                    379: 
                    380:   if (isCell)
                    381:     {
                    382:       // a cell is created
1.39      quint     383:       NewCell (el, doc, TRUE, TRUE, TRUE);
1.34      vatton    384:     }
                    385:   else if (isRow)
                    386:     {
                    387:       // a row is created
                    388:       event.element = el;
                    389:       event.document = doc;
                    390:       RowPasted (&event);
                    391:     }
                    392:   //else
                    393:   // TtaNotifySubTree (TteElemNew, doc, el, FALSE);
                    394:   return el;
                    395: }
                    396: 
                    397: 
1.12      kia       398: /*----------------------------------------------------------------------
1.16      kia       399:   Template_InsertUseChildren
                    400:   Insert children to a xt:use
                    401:   The dec parameter must be valid and will not be verified. It must be a
                    402:     direct child element (for union elements).
                    403:   @param el element (xt:use) in which insert a new element
                    404:   @param dec Template declaration of the element to insert
                    405:   @return The inserted element (the xt:use element if insertion is multiple as component)
                    406:   ----------------------------------------------------------------------*/
                    407: Element Template_InsertUseChildren(Document doc, Element el, Declaration dec)
                    408: {
1.34      vatton    409:   Element     newEl = NULL;
1.16      kia       410: #ifdef TEMPLATES
1.19      vatton    411:   Element     current = NULL;
1.34      vatton    412:   Element     child = NULL;
1.19      vatton    413:   //char       *attrCurrentTypeValue;
                    414:   //ElementType elType;
1.17      kia       415:   
1.25      vatton    416:   if (TtaGetDocumentAccessMode(doc))
1.16      kia       417:   {
1.23      kia       418:     switch (dec->nature)
                    419:     {
                    420:       case SimpleTypeNat:
                    421:         newEl = Template_GetNewSimpleTypeInstance(doc, el, dec);
1.34      vatton    422:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       423:         break;
                    424:       case XmlElementNat:
                    425:         newEl = Template_GetNewXmlElementInstance(doc, el, dec);
1.34      vatton    426:         newEl = InsertWithNotify (newEl, NULL, el, doc);
1.23      kia       427:         break;
                    428:       case ComponentNat:
                    429:         newEl = TtaCopyTree(dec->componentType.content, doc, doc, el);
1.35      kia       430:         ProcessAttr (dec->usedIn, newEl, doc);        
1.23      kia       431:         /* Copy elements from new use to existing use. */
1.34      vatton    432:         while ((child = TtaGetFirstChild(newEl)))
1.23      kia       433:         {
                    434:           TtaRemoveTree (child, doc);
1.34      vatton    435:           child = InsertWithNotify (child, current, el, doc);
1.23      kia       436:           current = child; 
                    437:         }
                    438:         
                    439:         /* Copy currentType attribute. */
                    440:         //attrCurrentTypeValue = GetAttributeStringValue (el, Template_ATTR_currentType, NULL);
                    441:         //SetAttributeStringValue (el, Template_ATTR_currentType, attrCurrentTypeValue);
                    442:         TtaDeleteTree(newEl, doc);
                    443:         newEl = el;
                    444:         break;
                    445:       case UnionNat :
                    446:         /* Nothing to do.*/
                    447:   //                elType.ElTypeNum = Template_EL_useEl;
                    448:   //                cont = TtaNewElement (doc, elType);
                    449:   //                if (cont)
                    450:   //                  {
                    451:   //                    TtaSetAccessRight (cont, ReadWrite, doc);
                    452:   //                    at = TtaNewAttribute (att);
                    453:   //                    if (at)
                    454:   //                      {
                    455:   //                        TtaAttachAttribute (cont, at, doc);
                    456:   //                        TtaSetAttributeText(at, types, cont, doc);
                    457:   //                      }
                    458:   //                  }
                    459:         /* @@@@@ */
                    460:         break;
                    461:       default :
                    462:         //Impossible
                    463:         break;   
                    464:     }
1.44      kia       465:     Template_FixAccessRight (dec->usedIn, el, doc);
                    466:     TtaUpdateAccessRightInViews (doc, el);    
1.23      kia       467:   }  
1.16      kia       468: #endif /* TEMPLATES */
                    469:   return newEl;
                    470: }
                    471: 
1.40      kia       472: 
                    473: /*----------------------------------------------------------------------
                    474:   Fix access rights.
                    475:   ----------------------------------------------------------------------*/
1.41      vatton    476: void Template_FixAccessRight (XTigerTemplate t, Element el, Document doc)
1.40      kia       477: {
                    478: #ifdef TEMPLATES
                    479:   ElementType elType;
                    480:   Element     child;
                    481:   char        currentType[MAX_LENGTH];
                    482:   Declaration decl;
                    483:   
                    484:   if (t && el && doc)
                    485:     {
                    486:       elType = TtaGetElementType(el);
1.41      vatton    487:       if (elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.40      kia       488:         {
1.41      vatton    489:           switch (elType.ElTypeNum)
1.40      kia       490:             {
                    491:             case Template_EL_TEXT_UNIT:
1.41      vatton    492:               //TtaSetAccessRight( el, ReadWrite, doc);
                    493:               return;
1.40      kia       494:             case Template_EL_useEl:
                    495:             case Template_EL_useSimple:
                    496:               GiveAttributeStringValueFromNum(el, Template_ATTR_currentType,
                    497:                                               (char*)currentType, NULL);
                    498:               decl = Template_GetDeclaration(t, currentType);
                    499:               if (decl)
                    500:                 {
                    501:                   switch (decl->nature)
                    502:                     {
                    503:                       case SimpleTypeNat:
                    504:                       case XmlElementNat:
1.41      vatton    505:                         TtaSetAccessRight (el, ReadWrite, doc);
                    506:                         return;
1.40      kia       507:                       default:
1.41      vatton    508:                         TtaSetAccessRight (el, ReadOnly, doc);
                    509:                          break;
1.40      kia       510:                     }
                    511:                 }
                    512:               break;
                    513:             case Template_EL_bag:
1.45      vatton    514:             case Template_EL_repeat:
1.40      kia       515:               TtaSetAccessRight(el, ReadWrite, doc);
                    516:               break;
                    517:             default:
                    518:               TtaSetAccessRight(el, ReadOnly, doc);
                    519:               break;
                    520:             }
                    521:         }
                    522: 
1.41      vatton    523:       child = TtaGetFirstChild (el);
                    524:       // fix access right to children
                    525:       while (child)
1.40      kia       526:         {
1.41      vatton    527:           Template_FixAccessRight (t, child, doc);
                    528:           TtaNextSibling (&child);
1.40      kia       529:         }
                    530:     }
                    531: #endif /* TEMPLATES */
                    532: }
                    533: 
1.16      kia       534: /*----------------------------------------------------------------------
1.46      vatton    535:   AddPromptIndicator
                    536:   ----------------------------------------------------------------------*/
                    537: void AddPromptIndicator (Element el, Document doc)
                    538: {
                    539: #ifdef TEMPLATES
                    540:   ElementType         elType;
                    541:   AttributeType       attrType;
                    542:   Attribute           att;
                    543: 
                    544:   elType = TtaGetElementType (el);
                    545:   attrType.AttrSSchema = elType.ElSSchema;
                    546:   attrType.AttrTypeNum = Template_ATTR_prompt;
                    547:   att = TtaNewAttribute (attrType);
                    548:   TtaAttachAttribute (el, att, doc);
                    549: #endif /* TEMPLATES */
                    550: }
                    551: 
                    552: /*----------------------------------------------------------------------
1.1       vatton    553:   InstantiateUse
1.3       vatton    554:   ----------------------------------------------------------------------*/
1.1       vatton    555: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
1.27      kia       556:                         ThotBool registerUndo)
1.1       vatton    557: {
                    558: #ifdef TEMPLATES
1.47    ! kia       559:   Element          cont = NULL;
1.1       vatton    560:   ElementType      elType;
                    561:   Declaration      dec;
1.28      kia       562:   int              size, nbitems, i;
1.1       vatton    563:   struct menuType  *items;
1.33      vatton    564:   char             *types, *text = NULL;
1.1       vatton    565:   ThotBool          oldStructureChecking;
                    566: 
1.25      vatton    567:   if (!t)
1.23      kia       568:     return NULL;
                    569: 
1.1       vatton    570:   /* get the value of the "types" attribute */
                    571:   cont = NULL;
1.12      kia       572:   elType = TtaGetElementType (el);
1.46      vatton    573:   AddPromptIndicator (el, doc);
1.32      vatton    574:   types = GetAttributeStringValueFromNum (el, Template_ATTR_types, &size);
1.36      vatton    575:   if (!types || types[0] == EOS)
                    576:     {
                    577:       TtaFreeMemory (types);
                    578:       return NULL;
                    579:     }
1.1       vatton    580:   giveItems (types, size, &items, &nbitems);
                    581:   // No structure checking
                    582:   oldStructureChecking = TtaGetStructureChecking (doc);
                    583:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       584:   
1.1       vatton    585:   if (nbitems == 1)
                    586:     /* only one type in the "types" attribute */
                    587:     {
1.17      kia       588:       dec = Template_GetDeclaration (t, items[0].label);
1.1       vatton    589:       if (dec)
1.27      kia       590:       {
1.46      vatton    591:         cont = Template_InsertUseChildren (doc, el, dec);
1.32      vatton    592:         if (cont)
1.27      kia       593:         {
                    594:           TtaChangeTypeOfElement (el, doc, Template_EL_useSimple);
1.32      vatton    595:           if (registerUndo)
                    596:             TtaRegisterElementTypeChange (el, Template_EL_useEl, doc);
1.27      kia       597:         }
                    598:       }
1.1       vatton    599:     }
1.33      vatton    600:   TtaFreeMemory (text);
1.32      vatton    601:   TtaFreeMemory (types);
1.28      kia       602:   
1.32      vatton    603:   for (i = 0; i < nbitems; i++)
1.28      kia       604:     TtaFreeMemory(items[i].label);
1.18      kia       605:   TtaFreeMemory(items);
1.1       vatton    606:   TtaSetStructureChecking (oldStructureChecking, doc);
1.40      kia       607:   
1.44      kia       608:   Template_FixAccessRight (t, el, doc);
                    609:   TtaUpdateAccessRightInViews (doc, el);
                    610:   
1.1       vatton    611:   return cont;
1.23      kia       612: #else /* TEMPLATES */
                    613:   return NULL;
1.1       vatton    614: #endif /* TEMPLATES */
                    615: }
                    616: 
                    617: /*----------------------------------------------------------------------
1.22      kia       618:   InstantiateRepeat
                    619:   Check for min and max param and validate xt:repeat element content.
                    620:   @param registerUndo True to register undo creation sequences.
1.3       vatton    621:   ----------------------------------------------------------------------*/
1.46      vatton    622: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc,
                    623:                         ThotBool registerUndo)
1.1       vatton    624: {
                    625: #ifdef TEMPLATES
1.32      vatton    626:   Element        child, newChild;
1.1       vatton    627:   Attribute      curAtt,  minAtt,  maxAtt;
                    628:   AttributeType  curType, minType, maxType;
1.33      vatton    629:   char           *text, *types = NULL, *title = NULL;
1.32      vatton    630:   int            curVal,  minVal,  maxVal;
                    631:   int            childrenCount;
                    632: 
1.1       vatton    633: 
1.25      vatton    634:   if (!t)
1.23      kia       635:     return;
                    636: 
1.1       vatton    637:   //Preparing types
                    638:   curType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    639:   minType.AttrSSchema = maxType.AttrSSchema = curType.AttrSSchema;
                    640:   curType.AttrTypeNum = Template_ATTR_currentOccurs; 
                    641:   minType.AttrTypeNum = Template_ATTR_minOccurs;
                    642:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
                    643: 
                    644:   //Get currentOccurs, minOccurs and maxOccurs attributes
                    645:   curAtt = TtaGetAttribute (el, curType);
                    646:   minAtt = TtaGetAttribute (el, minType);
                    647:   maxAtt = TtaGetAttribute (el, maxType);
                    648: 
                    649:   //Get the values
                    650:   if (minAtt)
                    651:     {
1.10      kia       652:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    653:       if (text)
                    654:         {
                    655:           minVal = atoi(text);
                    656:           TtaFreeMemory(text);
                    657:         }
                    658:       else
                    659:         //Error : Attribute with no value
                    660:         return;
                    661:     }
                    662:   else
                    663:     minVal = 0;
                    664: 
                    665:   if (maxAtt)
                    666:     {
1.10      kia       667:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton    668:       if (text)
                    669:         {
                    670:           if (!strcmp (text, "*"))
                    671:             maxVal = INT_MAX;
                    672:           else
                    673:             maxVal = atoi (text);
                    674:           TtaFreeMemory (text);
                    675:         }
                    676:       else
                    677:         //Error : Attribute with no value
                    678:         return;
                    679:     }
                    680:   else
                    681:     maxVal = INT_MAX;
                    682: 
                    683:   if (curAtt)
                    684:     {
1.10      kia       685:       text = GetAttributeStringValue(el, curAtt, NULL);
1.1       vatton    686:       if (text)
                    687:         {
1.2       quint     688:           curVal = atoi(text);
1.1       vatton    689:           TtaFreeMemory(text);
                    690:         }
                    691:       else
                    692:         //Error : Attribute with no value
                    693:         return;
                    694:     }
                    695:   else
                    696:     curVal = minVal;
                    697: 
                    698:   text = (char*)TtaGetMemory(MAX_LENGTH);
                    699:   //Create non existing attributes
                    700:   if (!minAtt)
                    701:     {      
1.32      vatton    702:       minAtt = TtaNewAttribute (minType);
                    703:       sprintf (text, "%d", minVal);
                    704:       TtaAttachAttribute (el, minAtt, doc);
                    705:       TtaSetAttributeText (minAtt, text, el, doc);
1.25      vatton    706:       if (registerUndo)
1.32      vatton    707:         TtaRegisterAttributeCreate (minAtt, el, doc);
1.1       vatton    708:     }
                    709: 
                    710:   if (!maxAtt)
                    711:     {  
1.32      vatton    712:       maxAtt = TtaNewAttribute (maxType);
                    713:       if (maxVal < INT_MAX)
                    714:         sprintf(text, "%d", maxVal);
1.1       vatton    715:       else
1.32      vatton    716:         sprintf (text, "*");
                    717:       TtaAttachAttribute (el, maxAtt, doc);      
                    718:       TtaSetAttributeText (maxAtt, text, el, doc);
1.25      vatton    719:       if (registerUndo)
1.32      vatton    720:         TtaRegisterAttributeCreate (maxAtt, el, doc);
1.1       vatton    721:     }
                    722: 
                    723:   if (!curAtt)
1.2       quint     724:     {
1.32      vatton    725:       curAtt = TtaNewAttribute (curType);
                    726:       sprintf (text,"%d",curVal);
                    727:       TtaAttachAttribute (el, curAtt, doc);
                    728:       TtaSetAttributeText (curAtt, text, el, doc);
1.25      vatton    729:       if (registerUndo)
1.32      vatton    730:         TtaRegisterAttributeCreate (curAtt, el, doc);
1.1       vatton    731:     }
                    732:   if (text)
                    733:     TtaFreeMemory(text);
                    734: 
                    735:   //We must have currentOccurs children
                    736:   child = TtaGetFirstChild(el);
                    737:   if (!child)
                    738:     //Error : a repeat must have at least one child which will be the model
                    739:     return;
                    740:   
                    741:   for(childrenCount = 0; child; TtaNextSibling(&child))
                    742:     {
                    743:       //TODO : Check that every child is valid
                    744:       childrenCount ++;
                    745:     }
                    746: 
                    747:   if (childrenCount > maxVal)
                    748:     //Error : too many children!
                    749:     return;  
                    750: 
                    751:   child = TtaGetLastChild(el);
1.32      vatton    752:   types = GetAttributeStringValueFromNum (child, Template_ATTR_types, NULL);
                    753:   title = GetAttributeStringValueFromNum (child, Template_ATTR_title, NULL);
                    754:   while (childrenCount < curVal)
1.1       vatton    755:     {
1.27      kia       756:       ElementType newElType;
                    757:       newElType.ElSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    758:       newElType.ElTypeNum = Template_EL_useEl;
1.32      vatton    759:       newChild = TtaNewElement (doc, newElType);
1.27      kia       760:       
                    761:       // Insert it
1.32      vatton    762:       TtaInsertSibling (newChild, child, FALSE, doc);
                    763:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_types, types);
                    764:       SetAttributeStringValueWithUndo (newChild, Template_ATTR_title, title);
1.34      vatton    765:       InstantiateUse (t, newChild, doc, TRUE);
1.27      kia       766:       
1.25      vatton    767:       if (registerUndo)
1.34      vatton    768:         TtaRegisterElementCreate (newChild, doc);
1.1       vatton    769:       child = newChild;
                    770:       childrenCount++;
                    771:     }
1.27      kia       772:     
1.44      kia       773:   Template_FixAccessRight (t, el, doc);
                    774:   TtaUpdateAccessRightInViews (doc, el);
                    775:   
1.32      vatton    776:   TtaFreeMemory (types);
                    777:   TtaFreeMemory (title);
1.1       vatton    778: #endif /* TEMPLATES */
                    779: }
                    780: 
                    781: /*----------------------------------------------------------------------
                    782:   ParseTemplate
1.3       vatton    783:   ----------------------------------------------------------------------*/
1.1       vatton    784: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                    785:                            ThotBool loading)
                    786: {
                    787: #ifdef TEMPLATES
1.47    ! kia       788:   AttributeType attType;
        !           789:   Attribute     att;
        !           790:   Element       aux, child; //Needed when deleting trees
        !           791:   char         *name;
        !           792:   ElementType   elType = TtaGetElementType (el);
        !           793: 
1.37      kia       794:   if (!t || !el)
1.23      kia       795:     return;
                    796:   
1.5       vatton    797:   name = TtaGetSSchemaName (elType.ElSSchema);
1.47    ! kia       798:   if (!strcmp (name, "Template"))
1.1       vatton    799:     {
1.5       vatton    800:       switch(elType.ElTypeNum)
1.1       vatton    801:         {
                    802:         case Template_EL_head :
                    803:           //Remove it and all of its children
                    804:           TtaDeleteTree(el, doc);
                    805:           //We must stop searching into this tree
                    806:           return;
                    807:           break;
                    808:         case Template_EL_component :
1.25      vatton    809:           // remove the name attribute
1.5       vatton    810:           attType.AttrSSchema = elType.ElSSchema;
1.1       vatton    811:           attType.AttrTypeNum = Template_ATTR_name;
1.18      kia       812:           name = GetAttributeStringValueFromNum (el, Template_ATTR_name, NULL);                                  
1.5       vatton    813:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.25      vatton    814:           // replace the component by a use
1.1       vatton    815:           if (NeedAMenu (el, doc))
1.5       vatton    816:             TtaChangeElementType (el, Template_EL_useEl);
1.1       vatton    817:           else
1.5       vatton    818:             TtaChangeElementType (el, Template_EL_useSimple);
1.25      vatton    819:           // generate the types attribute
1.1       vatton    820:           attType.AttrTypeNum = Template_ATTR_types;
1.5       vatton    821:           att = TtaNewAttribute (attType);
                    822:           TtaAttachAttribute (el, att, doc);
1.25      vatton    823:           if (name)
1.18      kia       824:             TtaSetAttributeText (att, name, el, doc);
1.25      vatton    825:           // generate the title attribute
                    826:           attType.AttrTypeNum = Template_ATTR_title;
                    827:           att = TtaNewAttribute (attType);
                    828:           TtaAttachAttribute (el, att, doc);
                    829:           if (name)
                    830:             TtaSetAttributeText (att, name, el, doc);
                    831:           // generate the currentType attribute
1.1       vatton    832:           attType.AttrTypeNum = Template_ATTR_currentType;
1.5       vatton    833:           att = TtaNewAttribute (attType);
1.18      kia       834:           TtaAttachAttribute (el, att, doc);
1.25      vatton    835:           if (name)
1.18      kia       836:             TtaSetAttributeText (att, name, el, doc);
1.28      kia       837:           TtaFreeMemory(name);
1.1       vatton    838:           break;
                    839:         case Template_EL_option :
                    840:           aux = NULL;
                    841:           break;
                    842:         case Template_EL_bag :
                    843:           //Link to types
                    844:           //Allow editing the content
                    845:           break;
                    846:         case Template_EL_useEl :
                    847:         case Template_EL_useSimple :
                    848:           /* if this use element is not empty, don't do anything: it is
                    849:              supposed to contain a valid instance. This should be
                    850:              checked, though */
1.46      vatton    851:           if (DocumentMeta[doc] && DocumentMeta[doc]->isTemplate)
                    852:             // add the initial indicator
                    853:             AddPromptIndicator (el, doc);
                    854:             
1.1       vatton    855:           if (!TtaGetFirstChild (el))
1.27      kia       856:             InstantiateUse (t, el, doc, FALSE);
1.1       vatton    857:           break;
                    858:         case Template_EL_attribute :
                    859:           if (!loading)
                    860:             InstantiateAttribute (t, el, doc);
                    861:           break;
                    862:         case Template_EL_repeat :
1.22      kia       863:           InstantiateRepeat (t, el, doc, FALSE);
1.1       vatton    864:           break;
                    865:         default :
                    866:           break;
                    867:         }
                    868:     }
1.47    ! kia       869: 
        !           870:   child = TtaGetFirstChild (el);
        !           871:   while (child)
1.1       vatton    872:     {
                    873:       aux = child;
1.5       vatton    874:       TtaNextSibling (&aux);
                    875:       ParseTemplate (t, child, doc, loading);
1.1       vatton    876:       child = aux;
                    877:     }
                    878: #endif /* TEMPLATES */
                    879: }
                    880: 
                    881: /*----------------------------------------------------------------------
1.3       vatton    882:   ----------------------------------------------------------------------*/
1.1       vatton    883: void DoInstanceTemplate (char *templatename)
                    884: {
                    885: #ifdef TEMPLATES
1.47    ! kia       886:   XTigerTemplate  t;
        !           887:   ElementType     elType;
        !           888:   Element         root, piElem, doctype, elFound, text;
        !           889:   char           *s, *charsetname = NULL, buffer[MAX_LENGTH];
1.1       vatton    890:   int             pi_type;
                    891:   Document        doc;
                    892: 
1.47    ! kia       893:   //Instantiate all elements
        !           894:   t = GetXTigerTemplate(templatename);
1.25      vatton    895:   if (!t)
1.23      kia       896:     return;
1.47    ! kia       897: 
1.1       vatton    898:   doc = GetTemplateDocument (t);
1.47    ! kia       899:   root =       TtaGetMainRoot (doc);
        !           900:   ParseTemplate (t, root, doc, FALSE);
1.1       vatton    901: 
                    902:   //Look for PIs
                    903:   /* check if the document has a DOCTYPE declaration */
                    904: #ifdef ANNOTATIONS
                    905:   if (DocumentTypes[doc]  == docAnnot)
                    906:     elType = TtaGetElementType (root);
                    907:   else
                    908: #endif /* ANNOTATIONS */
                    909:     elType = TtaGetElementType (root);
                    910:   s = TtaGetSSchemaName (elType.ElSSchema);
                    911:   if (strcmp (s, "HTML") == 0)
                    912:     {
                    913:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                    914:       pi_type = HTML_EL_XMLPI;
                    915:     }
                    916: #ifdef _SVG
                    917:   else if (strcmp (s, "SVG") == 0)
                    918:     {
                    919:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                    920:       pi_type = SVG_EL_XMLPI;
                    921:     }
                    922: #endif /* _SVG */
                    923:   else if (strcmp (s, "MathML") == 0)
                    924:     {
                    925:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                    926:       pi_type = MathML_EL_XMLPI;
                    927:     }
                    928:   else
                    929:     {
                    930:       elType.ElTypeNum = XML_EL_doctype;
                    931:       pi_type = XML_EL_xmlpi;
                    932:     }
                    933:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
                    934:   if (!doctype)
                    935:     {
                    936:       /* generate the XML declaration */
                    937:       /* Check the Thot abstract tree against the structure schema. */
                    938:       TtaSetStructureChecking (FALSE, doc);
                    939:       elType.ElTypeNum = pi_type;
                    940:       doctype = TtaNewTree (doc, elType, "");
                    941:       TtaInsertFirstChild (&doctype, root, doc);
                    942:       elFound = TtaGetFirstChild (doctype);
                    943:       text = TtaGetFirstChild (elFound);
                    944:       strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                    945:       charsetname = UpdateDocumentCharset (doc);
                    946:       strcat (buffer, charsetname);
                    947:       strcat (buffer, "\"");
                    948:       TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    949:       TtaSetStructureChecking (TRUE, doc);
1.28      kia       950:       TtaFreeMemory(charsetname);
1.1       vatton    951:     }
                    952:   
                    953:   /* generate the XTiger PI */
                    954:   /* Check the Thot abstract tree against the structure schema. */
                    955:   TtaSetStructureChecking (FALSE, doc);
                    956:   elType.ElTypeNum = pi_type;
                    957:   piElem = TtaNewTree (doc, elType, "");
1.34      vatton    958:   TtaInsertSibling (piElem, doctype, FALSE, doc);
1.1       vatton    959:   elFound = TtaGetFirstChild (piElem);
                    960:   text = TtaGetFirstChild (elFound);
                    961:   strcpy (buffer, "xtiger template=\"");
                    962:   strcat (buffer, templatename);
1.17      kia       963:   strcat (buffer, "\" version=\"");
1.20      vatton    964:   if (t->version)
                    965:     strcat (buffer, t->version);
                    966:   else
                    967:     strcat (buffer, "0.8");
1.1       vatton    968:   strcat (buffer, "\"");
1.25      vatton    969:   if (t->templateVersion)
1.20      vatton    970:     {
                    971:       strcat (buffer, " templateVersion=\"");
                    972:       strcat (buffer, t->templateVersion);
                    973:       strcat (buffer, "\"");
                    974:     }
1.1       vatton    975:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    976:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton    977: 
                    978:   // update the document title
1.47    ! kia       979:   if (!strcmp (s, "HTML"))
1.5       vatton    980:     {
                    981:       elType.ElTypeNum = HTML_EL_TITLE;
                    982:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                    983:       if (elFound)
                    984:         {
                    985:           elFound = TtaGetFirstChild (elFound);
                    986:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                    987:                              TtaGetDefaultLanguage (), doc);
                    988:         }
                    989:     }
1.1       vatton    990: #endif /* TEMPLATES */
                    991: }
                    992: 
                    993: /*----------------------------------------------------------------------
1.46      vatton    994:   Template_PreInstantiateComponents: Instantiates all components in 
                    995:   order to improve editing.
1.3       vatton    996:   ----------------------------------------------------------------------*/
1.35      kia       997: void Template_PreInstantiateComponents (XTigerTemplate t)
1.1       vatton    998: {
                    999: #ifdef TEMPLATES 
1.25      vatton   1000:   if (!t)
1.23      kia      1001:     return;
                   1002: 
1.35      kia      1003:   HashMap         components = GetComponents(t);
                   1004:   ForwardIterator iter = HashMap_GetForwardIterator(components);
                   1005:   Declaration     comp;
                   1006:   HashMapNode     node;
1.47    ! kia      1007: 
1.35      kia      1008:   ITERATOR_FOREACH(iter, HashMapNode, node)
1.1       vatton   1009:     {
1.35      kia      1010:       comp = (Declaration) node->elem;
1.1       vatton   1011:       ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
                   1012:     }
1.35      kia      1013:   TtaFreeMemory(iter);
1.1       vatton   1014: #endif /* TEMPLATES */
                   1015: }

Webmaster