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

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

Webmaster