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

1.1       vatton      1: #include "templates.h"
                      2: 
                      3: #define THOT_EXPORT extern
                      4: #include "templateDeclarations.h"
                      5: 
1.8       kia         6: #include "Elemlist.h"
                      7: 
1.1       vatton      8: #include "EDITimage_f.h"
                      9: #include "HTMLactions_f.h"
                     10: #include "HTMLsave_f.h"
                     11: #include "init_f.h"
                     12: #include "mydictionary_f.h"
                     13: #include "templates_f.h"
                     14: #include "templateDeclarations_f.h"
                     15: #include "templateInstantiate_f.h"
                     16: #include "Templatebuilder_f.h"
                     17: #include "templateUtils_f.h"
                     18: #include "fetchHTMLname_f.h"
                     19: #include "Template.h"
                     20: 
                     21: #ifdef TEMPLATES
                     22: #define TEMPLATE_SCHEMA_NAME "Template"
                     23: 
                     24: typedef struct _InstantiateCtxt
                     25: {
                     26:        char *                  templatePath;
                     27:        char *                  instancePath;
                     28:        char *                  schemaName;
                     29:        DocumentType    docType;
                     30:        ThotBool                dontReplace;
                     31: } InstantiateCtxt;
                     32: #endif /* TEMPLATES */
                     33: 
                     34: typedef struct _AttSearch
                     35: {
                     36:   int   att;
                     37:   int   type;
                     38: } AttSearch;
                     39: 
                     40: static AttSearch    URL_attr_tab[] =
                     41:   {
                     42:     {HTML_ATTR_HREF_, XHTML_TYPE},
                     43:     {HTML_ATTR_codebase, XHTML_TYPE},
                     44:     {HTML_ATTR_Script_URL, XHTML_TYPE},
                     45:     {HTML_ATTR_SRC, XHTML_TYPE},
                     46:     {HTML_ATTR_data, XHTML_TYPE},
                     47:     {HTML_ATTR_background_, XHTML_TYPE},
                     48:     {HTML_ATTR_Style_, XHTML_TYPE},
                     49:     {HTML_ATTR_cite, XHTML_TYPE},
                     50:     //{XLink_ATTR_href_, XLINK_TYPE},
                     51:     {MathML_ATTR_style_, MATH_TYPE},
                     52: #ifdef _SVG
                     53:     {SVG_ATTR_style_, SVG_TYPE},
                     54:     {SVG_ATTR_xlink_href, SVG_TYPE}
                     55: #endif
                     56:   };
                     57: 
                     58: /*----------------------------------------------------------------------
                     59:   RegisterURLs
1.3       vatton     60:   ----------------------------------------------------------------------*/
1.1       vatton     61: void RegisterURLs(Document doc, Element el)
                     62: {
                     63: #ifdef TEMPLATES
                     64:   SSchema             XHTMLSSchema, MathSSchema, SVGSSchema, XLinkSSchema;
                     65:   AttributeType       attrType;
                     66:   Attribute           attr;
                     67:   int                 max;
                     68: 
                     69:   XHTMLSSchema = TtaGetSSchema ("HTML", doc);
                     70:   MathSSchema = TtaGetSSchema ("MathML", doc);
                     71:   SVGSSchema = TtaGetSSchema ("SVG", doc);
                     72:   XLinkSSchema = TtaGetSSchema ("XLink", doc);
                     73: 
                     74:   max = sizeof (URL_attr_tab) / sizeof (AttSearch);
                     75: 
                     76:   for(int i=0; i<max; i++)
                     77:     {
                     78:       attrType.AttrTypeNum = URL_attr_tab[i].att;
                     79:       switch (URL_attr_tab[i].type)
                     80:         {
                     81:         case XHTML_TYPE:
                     82:           attrType.AttrSSchema = XHTMLSSchema;
                     83:           break;
                     84:         case MATH_TYPE:
                     85:           attrType.AttrSSchema = MathSSchema;
                     86:           break;
                     87:         case SVG_TYPE:
                     88:           attrType.AttrSSchema = SVGSSchema;
                     89:           break;
                     90:         case XLINK_TYPE:
                     91:           attrType.AttrSSchema = XLinkSSchema;
                     92:           break;
                     93:         default:
                     94:           attrType.AttrSSchema = NULL;
                     95:         }
                     96: 
                     97:       attr = TtaGetAttribute(el, attrType);
                     98:       if (attr!=NULL)      
                     99:         TtaRegisterAttributeReplace(attr, el, doc);
                    100:     }  
                    101: 
                    102:   for (Element child = TtaGetFirstChild (el); child; TtaNextSibling (&child))
                    103:     RegisterURLs (doc, child);
                    104: #endif /* TEMPLATES*/
                    105: }
                    106: 
                    107: /*----------------------------------------------------------------------
                    108:   CreateInstance
1.3       vatton    109:   ----------------------------------------------------------------------*/
1.1       vatton    110: void  CreateInstance(char *templatePath, char *instancePath)
1.3       vatton    111: {
1.1       vatton    112: #ifdef TEMPLATES
1.14    ! vatton    113:   Document     doc = 0;
1.5       vatton    114:   DocumentType docType;
1.14    ! vatton    115:   ElementType  elType;
        !           116:   Element      root, title, text;
        !           117:   char        *s;
        !           118:   int          alreadyOnDoc = 0;
        !           119:   ThotBool     alreadyViewing = FALSE;
1.1       vatton    120: 
1.8       kia       121:   XTigerTemplate t = (XTigerTemplate)Dictionary_Get (Templates_Dic, templatePath);
1.1       vatton    122:   if (t == NULL)
                    123:     //The template must be loaded before calling this function
                    124:     return;
                    125: 
1.5       vatton    126:   doc = GetTemplateDocument (t);
1.3       vatton    127:   while (alreadyOnDoc < DocumentTableLength-1 && !alreadyViewing)
1.1       vatton    128:     {
                    129:       alreadyOnDoc++;
                    130:       if (DocumentURLs[alreadyOnDoc])
                    131:         alreadyViewing = !strcmp (DocumentURLs[alreadyOnDoc],instancePath);
                    132:     }
                    133: 
                    134:   if (!TtaPrepareUndo (doc))
                    135:     {
                    136:       TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.14    ! vatton    137:       root = TtaGetRootElement(doc);
        !           138:       elType = TtaGetElementType (root);
        !           139:       // get the target document type
        !           140:       s = TtaGetSSchemaName (elType.ElSSchema);
        !           141:       if (strcmp (s, "HTML") == 0)
        !           142:         docType = docHTML;
        !           143:       else if (strcmp (s, "SVG") == 0)
        !           144:         docType = docSVG;
        !           145:       else if (strcmp (s, "MathML") == 0)
        !           146:         docType = docMath;
        !           147:       else
        !           148:         docType = docXml;
        !           149: 
        !           150:       RegisterURLs (doc, root);
1.1       vatton    151:       SetRelativeURLs (doc, instancePath);
                    152:       
                    153:       switch (docType)
                    154:         {
1.14    ! vatton    155:         case docSVG:
1.1       vatton    156:           TtaExportDocumentWithNewLineNumbers (doc, instancePath, "SVGT");
                    157:           break;
1.14    ! vatton    158:         case docMath:
1.1       vatton    159:           TtaExportDocumentWithNewLineNumbers (doc, instancePath, "MathMLT");
                    160:           break;
1.14    ! vatton    161:         case docHTML:
        !           162:           // Initialize the document title
        !           163:           elType.ElTypeNum = HTML_EL_TITLE;
        !           164:           title = TtaSearchTypedElement (elType, SearchInTree, root);
        !           165:           text = TtaGetFirstChild (title);
        !           166:           while (text)
        !           167:             {
        !           168:               elType = TtaGetElementType (text);
        !           169:               if (elType.ElTypeNum == HTML_EL_TEXT_UNIT && Answer_text[0] != EOS)
        !           170:                 {
        !           171:                   TtaSetTextContent (text, (unsigned char*)Answer_text,
        !           172:                                      TtaGetDefaultLanguage (), doc);
        !           173:                   text = NULL;
        !           174:                 }
        !           175:               else if ((elType.ElTypeNum == Template_EL_useEl ||
        !           176:                         elType.ElTypeNum == Template_EL_useSimple) &&
        !           177:                        !strcmp (TtaGetSSchemaName (elType.ElSSchema), "Template"))
        !           178:                 // Ignore the template use element
        !           179:                 text = TtaGetFirstChild (text);
        !           180:               else
        !           181:                 // Look for the first text child
        !           182:                 TtaNextSibling (&text);
        !           183:             }
1.1       vatton    184:           if (TtaGetDocumentProfile(doc)==L_Xhtml11 || TtaGetDocumentProfile(doc)==L_Basic)
                    185:             TtaExportDocumentWithNewLineNumbers (doc, instancePath, "HTMLT11");
                    186:           else
                    187:             TtaExportDocumentWithNewLineNumbers (doc, instancePath, "HTMLTX");
                    188:           break;
1.14    ! vatton    189:         default:
1.1       vatton    190:           TtaExportDocumentWithNewLineNumbers (doc, instancePath, NULL);
                    191:           break;
                    192:         }
                    193:       
                    194:       TtaCloseUndoSequence (doc);
                    195:       TtaUndoNoRedo (doc);
                    196:       TtaClearUndoHistory (doc);
                    197:     }
                    198: 
1.3       vatton    199:   if (!alreadyViewing)
                    200:     {
                    201:       // Open the instance
1.1       vatton    202:       TtaExtractName (instancePath, DirectoryName, DocumentName);
                    203:       CallbackDialogue (BaseDialog + OpenForm, INTEGER_DATA, (char *) 1);
                    204:     }
1.3       vatton    205:   else
                    206:     {
                    207:       // Reload on the existing view
                    208:       Reload (alreadyOnDoc, 0);
                    209:     }
1.1       vatton    210: #endif /* TEMPLATES */
                    211: }
                    212: 
                    213: /*----------------------------------------------------------------------
1.3       vatton    214:   ----------------------------------------------------------------------*/
1.7       cvs       215: void InstantiateTemplate_callback (int newdoc, int status,  char *urlName, char *outputfile,
                    216:                                   char *proxyName, AHTHeaders *http_headers, void * context)
1.1       vatton    217: {
                    218: #ifdef TEMPLATES
                    219:        InstantiateCtxt *ctx = (InstantiateCtxt*)context;
                    220: 
                    221:        DoInstanceTemplate (ctx->templatePath);
                    222:   CreateInstance (ctx->templatePath, ctx->instancePath);
                    223:   TtaFreeMemory (ctx->templatePath);
                    224:   TtaFreeMemory (ctx->instancePath);
                    225:   TtaFreeMemory (ctx);
                    226: #endif /* TEMPLATES */
                    227: }
                    228: 
                    229: /*----------------------------------------------------------------------
1.3       vatton    230:   ----------------------------------------------------------------------*/
1.1       vatton    231: void InstantiateTemplate (Document doc, char *templatename, char *docname,
                    232:                           DocumentType docType, ThotBool loaded)
                    233: {
                    234: #ifdef TEMPLATES
                    235:        if (!loaded)
1.3       vatton    236:     {
                    237:       // Create the callback context
                    238:       InstantiateCtxt *ctx = (InstantiateCtxt *)TtaGetMemory (sizeof (InstantiateCtxt));
                    239:       ctx->templatePath        = TtaStrdup (templatename);
                    240:       ctx->instancePath        = TtaStrdup (docname);
                    241:       ctx->schemaName = GetSchemaFromDocType(docType);
                    242:       ctx->docType = docType;
1.1       vatton    243:                
1.3       vatton    244:       GetAmayaDoc (templatename, NULL, doc, doc, CE_MAKEBOOK, FALSE, 
1.6       cvs       245:                    (void (*)(int, int, char*, char*, char*, const AHTHeaders*, void*)) InstantiateTemplate_callback,
1.3       vatton    246:                    (void *) ctx);
                    247:     }
1.1       vatton    248:        else
                    249:     {
                    250:       DoInstanceTemplate (templatename);
                    251:       CreateInstance (templatename, docname);
1.3       vatton    252:     }  
1.1       vatton    253: #endif /* TEMPLATES */
                    254: }
                    255: 
                    256: /*----------------------------------------------------------------------
                    257:   InstantiateAttribute
1.3       vatton    258:   ----------------------------------------------------------------------*/
1.1       vatton    259: static void InstantiateAttribute (XTigerTemplate t, Element el, Document doc)
                    260: {
                    261: #ifdef TEMPLATES
                    262:   AttributeType  useType, nameType, defaultType, attrType;
                    263:   Attribute      useAttr, nameAttr, defAttr, attr;
                    264:   ElementType    elType;
                    265:   Element        parent;
                    266:   char           *text, *elementName;
                    267:   ThotBool       level;
                    268:   NotifyAttribute event;
                    269: 
                    270:   parent = TtaGetParent (el);
                    271:   if (!parent)
                    272:     return;
                    273:   // if attribute "use" has value "optional", don't do anything
                    274:   useType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    275:   useType.AttrTypeNum = Template_ATTR_useAt;
                    276:   useAttr = TtaGetAttribute (el, useType);
                    277:   if (useAttr)
                    278:     // there is a "use" attribute. Check its value
                    279:     {
1.10      kia       280:       text = GetAttributeStringValue (el, useAttr, NULL);
1.1       vatton    281:       if (text && strcmp (text, "optional") == 0)
                    282:         return;
                    283:     }
                    284:   // get the "name" and "default" attributes
                    285:   nameType.AttrSSchema = defaultType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
1.9       vatton    286:   nameType.AttrTypeNum = Template_ATTR_ref_name;
1.1       vatton    287:   defaultType.AttrTypeNum = Template_ATTR_defaultAt;
                    288:   nameAttr = TtaGetAttribute (el, nameType);
                    289:   defAttr = TtaGetAttribute (el, defaultType);
                    290:   if (nameAttr)
                    291:     {
1.10      kia       292:       text = GetAttributeStringValue (el, nameAttr, NULL);
1.1       vatton    293:       if (text)
                    294:         {
                    295:           elType = TtaGetElementType (parent);
                    296:           elementName = TtaGetElementTypeName (elType);
                    297:           level = TRUE;
                    298:           MapHTMLAttribute (text, &attrType, elementName, &level, doc);
                    299:           TtaFreeMemory(text);
                    300:           attr = TtaNewAttribute (attrType);
                    301:           if (attr)
                    302:             {
                    303:               TtaAttachAttribute (parent, attr, doc);
                    304:               if (defAttr)
                    305:                 {
1.10      kia       306:                   text = GetAttributeStringValue (el, defAttr, NULL);
1.1       vatton    307:                   TtaSetAttributeText(attr, text, parent, doc);
                    308:                   TtaFreeMemory(text);
                    309:                   // if it's a src arttribute for an image, load the image
                    310:                   if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML") &&
                    311:                       elType.ElTypeNum == HTML_EL_IMG)
                    312:                     if (attrType.AttrTypeNum == HTML_ATTR_SRC &&
                    313:                         attrType.AttrSSchema == elType.ElSSchema)
                    314:                       {
                    315:                         event.document = doc;
                    316:                         event.element = parent;
                    317:                         event.attribute = attr;
                    318:                         SRCattrModified (&event);
                    319:                       }
                    320:                 }
                    321:             }
                    322:         }
                    323:     }
                    324: #endif /* TEMPLATES */
                    325: }
                    326: 
                    327: #ifdef TEMPLATES
                    328: /*----------------------------------------------------------------------
                    329:   ProcessAttr
1.9       vatton    330:   Look for all "attribute" elements in the subtree and instantiate them
1.3       vatton    331:   ----------------------------------------------------------------------*/
1.1       vatton    332: static void ProcessAttr (XTigerTemplate t, Element el, Document doc)
                    333: {
                    334:   Element      child;
                    335:   ElementType  elType;
                    336: 
                    337:   for (child = TtaGetFirstChild (el); child; TtaNextSibling(&child))
                    338:     {
                    339:       elType = TtaGetElementType (child);
                    340:       if (elType.ElTypeNum == Template_EL_attribute &&
                    341:           !strcmp (TtaGetSSchemaName (elType.ElSSchema), TEMPLATE_SCHEMA_NAME))
                    342:         InstantiateAttribute (t, child, doc);
                    343:       else
                    344:         ProcessAttr (t, child, doc);
                    345:     }
                    346: }
                    347: #endif /* TEMPLATES */
                    348: 
1.12      kia       349: 
                    350: /*----------------------------------------------------------------------
                    351:   Template_GetNewSimpleTypeInstance
                    352:   Create an new instance of xt:use/SimpleType
                    353:   The decl attribute must embed SimpleType declaration (no validation).
                    354:   @param decl Declaration of new element
                    355:   @param parent Future parent element
                    356:   @param doc Document
                    357:   @return The new element
                    358:   ----------------------------------------------------------------------*/
                    359: Element Template_GetNewSimpleTypeInstance(Document doc, Element parent, Declaration decl)
                    360: {
                    361:   Element           newEl = NULL;
                    362: #ifdef TEMPLATES
                    363:   ElementType       elType;
                    364:   char             *empty = " ";
                    365:   
                    366:   elType = TtaGetElementType(parent);
                    367:   elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    368:   newEl = TtaNewElement (doc, elType);
                    369:   TtaSetTextContent (newEl, (unsigned char*) empty, 0, doc);
                    370: 
                    371: #endif 
                    372:   return newEl;
                    373: }
                    374: 
                    375: /*----------------------------------------------------------------------
                    376:   Template_GetNewXmlElementInstance
                    377:   Create an new instance of xt:use/XmlElement
                    378:   The decl attribute must embed XmlElement declaration (no validation).
                    379:   @param decl Declaration of new element
                    380:   @param parent Future parent element
                    381:   @param doc Document
                    382:   @return The new element
                    383:   ----------------------------------------------------------------------*/
                    384: Element Template_GetNewXmlElementInstance(Document doc, Element parent, Declaration decl)
                    385: {
                    386:   Element           newEl = NULL;
                    387: #ifdef TEMPLATES
                    388:   ElementType       elType;
                    389: 
1.13      kia       390:   GIType(decl->name, &elType, doc);
1.12      kia       391:   if(elType.ElTypeNum!=0)
                    392:   {
                    393:     newEl = TtaNewElement (doc, elType);
                    394:   }
                    395: #endif /* TEMPLATES */
                    396:   return newEl;
                    397: }
                    398: 
                    399: /*----------------------------------------------------------------------
                    400:   Template_GetNewComponentInstance
                    401:   Create an new instance of xt:use/Component
                    402:   The decl attribute must embed Component declaration (no validation).
                    403:   The returned element is the xt:use of the element.
                    404:   @param decl Declaration of new element
                    405:   @param parent Future parent element
                    406:   @param doc Document
                    407:   @return The new element
                    408:   ----------------------------------------------------------------------*/
                    409: Element Template_GetNewComponentInstance(Document doc, Element parent, Declaration decl)
                    410: {
                    411:   Element           newEl = NULL;
                    412: #ifdef TEMPLATES
                    413:   newEl = TtaCopyTree(decl->componentType.content, doc, doc, parent);
                    414:   ProcessAttr(decl->declaredIn, newEl, doc);
                    415: #endif /* TEMPLATES */
                    416:   return newEl;
                    417: }
                    418: 
                    419: 
1.1       vatton    420: /*----------------------------------------------------------------------
                    421:   InstantiateUse
1.3       vatton    422:   ----------------------------------------------------------------------*/
1.1       vatton    423: Element InstantiateUse (XTigerTemplate t, Element el, Document doc,
                    424:                         ThotBool insert)
                    425: {
                    426: #ifdef TEMPLATES
                    427:        Element          cont, child, prev, next;
                    428:   ElementType      elType;
                    429:        Attribute        at;
                    430:        AttributeType    att;
                    431:   Declaration      dec;
                    432:   int              size, nbitems;
                    433:   struct menuType  *items;
                    434:   char             *types;
                    435:   char             *empty = " ";
                    436:   ThotBool          oldStructureChecking;
                    437: 
                    438:   /* get the value of the "types" attribute */
                    439:   cont = NULL;
1.12      kia       440:   elType = TtaGetElementType (el);
1.10      kia       441:   types = GetAttributeStringValue(el, Template_ATTR_types, &size);
1.1       vatton    442:   giveItems (types, size, &items, &nbitems);
                    443:   // No structure checking
                    444:   oldStructureChecking = TtaGetStructureChecking (doc);
                    445:   TtaSetStructureChecking (FALSE, doc);
1.10      kia       446:   
1.1       vatton    447:   if (nbitems == 1)
                    448:     /* only one type in the "types" attribute */
                    449:     {
                    450:       dec = GetDeclaration (t, items[0].label);
                    451:       if (dec)
                    452:         switch(dec->nature)
                    453:           {
                    454:           case SimpleTypeNat :
                    455:             elType.ElTypeNum = Template_EL_TEXT_UNIT;
                    456:             cont = TtaNewElement (doc, elType);
                    457:             TtaInsertFirstChild (&cont, el, doc);
                    458:             TtaSetTextContent (cont, (unsigned char*) empty, 0, doc);
                    459:             cont = NULL;
                    460:             break;
                    461:           case XmlElementNat :
                    462:             GIType (dec->name, &elType, doc);
                    463:             cont = TtaNewElement (doc, elType);
                    464:             if (insert)
                    465:               TtaInsertFirstChild (&cont, el, doc);
                    466:             break;
                    467:           case ComponentNat :
                    468:             cont = TtaCopyTree (dec->componentType.content, doc, doc, el);
                    469:             ProcessAttr (t, cont, doc);
                    470:             if (insert)
                    471:               {
                    472:                 prev = NULL;
                    473:                 child = TtaGetFirstChild (cont);
                    474:                 while (child)
                    475:                   {
                    476:                     next = child;
                    477:                     TtaNextSibling (&next);
                    478:                     TtaRemoveTree (child, doc);
                    479:                     if (prev)
                    480:                       TtaInsertSibling (child, prev, FALSE, doc);
                    481:                     else
                    482:                       TtaInsertFirstChild (&child, el, doc);
                    483:                     prev = child;
                    484:                     child = next;
                    485:                   }
                    486:                 TtaDeleteTree (cont, doc);
                    487:                 cont = el;
                    488:               }
                    489:             break;
                    490:           case UnionNat :
1.4       quint     491:             if (!insert)
                    492:               /* the user has clicked a "repeat" button and wants to create
                    493:                  a new instance of the repeated element. Just create the
                    494:                  use element */
                    495:               {
                    496:                 elType.ElTypeNum = Template_EL_useEl;
                    497:                 cont = TtaNewElement (doc, elType);
                    498:                 if (cont)
                    499:                   {
                    500:                     TtaSetAccessRight (cont, ReadWrite, doc);
                    501:                     at = TtaNewAttribute (att);
                    502:                     if (at)
                    503:                       {
                    504:                         TtaAttachAttribute (cont, at, doc);
                    505:                         TtaSetAttributeText(at, types, cont, doc);
                    506:                       }
                    507:                   }
                    508:               }
1.1       vatton    509:             break;
                    510:           default :
                    511:             //Impossible
                    512:             break;   
                    513:           }
                    514:     }
1.4       quint     515:   TtaFreeMemory(types);
1.1       vatton    516:   TtaSetStructureChecking (oldStructureChecking, doc);
                    517:   return cont;
                    518: #endif /* TEMPLATES */
                    519: }
                    520: 
                    521: /*----------------------------------------------------------------------
1.3       vatton    522:   ----------------------------------------------------------------------*/
1.1       vatton    523: void InstantiateRepeat (XTigerTemplate t, Element el, Document doc)
                    524: {
                    525: #ifdef TEMPLATES
                    526:   int            curVal,  minVal,  maxVal;
                    527:   Attribute      curAtt,  minAtt,  maxAtt;
                    528:   AttributeType  curType, minType, maxType;
                    529:   char           *text;
                    530: 
                    531:   //Preparing types
                    532:   curType.AttrSSchema = TtaGetSSchema (TEMPLATE_SCHEMA_NAME, doc);
                    533:   minType.AttrSSchema = maxType.AttrSSchema = curType.AttrSSchema;
                    534:   curType.AttrTypeNum = Template_ATTR_currentOccurs; 
                    535:   minType.AttrTypeNum = Template_ATTR_minOccurs;
                    536:   maxType.AttrTypeNum = Template_ATTR_maxOccurs;
                    537: 
                    538:   //Get currentOccurs, minOccurs and maxOccurs attributes
                    539:   curAtt = TtaGetAttribute (el, curType);
                    540:   minAtt = TtaGetAttribute (el, minType);
                    541:   maxAtt = TtaGetAttribute (el, maxType);
                    542: 
                    543:   //Get the values
                    544:   if (minAtt)
                    545:     {
1.10      kia       546:       text = GetAttributeStringValue(el, minAtt, NULL);
1.1       vatton    547:       if (text)
                    548:         {
                    549:           minVal = atoi(text);
                    550:           TtaFreeMemory(text);
                    551:         }
                    552:       else
                    553:         //Error : Attribute with no value
                    554:         return;
                    555:     }
                    556:   else
                    557:     minVal = 0;
                    558: 
                    559:   if (maxAtt)
                    560:     {
1.10      kia       561:       text = GetAttributeStringValue (el, maxAtt, NULL);
1.1       vatton    562:       if (text)
                    563:         {
                    564:           if (!strcmp (text, "*"))
                    565:             maxVal = INT_MAX;
                    566:           else
                    567:             maxVal = atoi (text);
                    568:           TtaFreeMemory (text);
                    569:         }
                    570:       else
                    571:         //Error : Attribute with no value
                    572:         return;
                    573:     }
                    574:   else
                    575:     maxVal = INT_MAX;
                    576: 
                    577:   if (curAtt)
                    578:     {
1.10      kia       579:       text = GetAttributeStringValue(el, curAtt, NULL);
1.1       vatton    580:       if (text)
                    581:         {
1.2       quint     582:           curVal = atoi(text);
1.1       vatton    583:           TtaFreeMemory(text);
                    584:         }
                    585:       else
                    586:         //Error : Attribute with no value
                    587:         return;
                    588:     }
                    589:   else
                    590:     curVal = minVal;
                    591: 
                    592:   text = (char*)TtaGetMemory(MAX_LENGTH);
                    593: 
                    594:   //Create non existing attributes
                    595:   if (!minAtt)
                    596:     {      
                    597:       minAtt = TtaNewAttribute(minType);
                    598:       sprintf(text,"%d",minVal);
                    599:       TtaAttachAttribute(el, minAtt, doc);
                    600:       TtaSetAttributeText(minAtt, text, el, doc);
                    601:     }
                    602: 
                    603:   if (!maxAtt)
                    604:     {  
                    605:       maxAtt = TtaNewAttribute(maxType);
                    606:       if (maxVal<INT_MAX)
                    607:         sprintf(text,"%d",maxVal);
                    608:       else
                    609:         sprintf(text,"*");
                    610:       TtaAttachAttribute(el, maxAtt, doc);      
                    611:       TtaSetAttributeText(maxAtt, text, el, doc);
                    612:     }
                    613: 
                    614:   if (!curAtt)
1.2       quint     615:     {
1.1       vatton    616:       curAtt = TtaNewAttribute(curType);
                    617:       sprintf(text,"%d",curVal);
                    618:       TtaAttachAttribute(el, curAtt, doc);
                    619:       TtaSetAttributeText(curAtt, text, el, doc);
                    620:     }
                    621: 
                    622:   if (text)
                    623:     TtaFreeMemory(text);
                    624: 
                    625:   //We must have currentOccurs children
                    626:   Element  child, newChild;
                    627:   int      childrenCount;
                    628: 
                    629:   child = TtaGetFirstChild(el);
                    630:   if (!child)
                    631:     //Error : a repeat must have at least one child which will be the model
                    632:     return;
                    633:   
                    634:   for(childrenCount = 0; child; TtaNextSibling(&child))
                    635:     {
                    636:       //TODO : Check that every child is valid
                    637:       childrenCount ++;
                    638:     }
                    639: 
                    640:   if (childrenCount > maxVal)
                    641:     //Error : too many children!
                    642:     return;  
                    643: 
                    644:   child = TtaGetLastChild(el);
                    645: 
                    646:   while(childrenCount < curVal)
                    647:     {
                    648:       //Create a new child
                    649:       newChild = TtaCopyTree(child, doc, doc, el);
                    650:       TtaInsertSibling(newChild, child, FALSE, doc);
                    651:       child = newChild;
                    652:       childrenCount++;
                    653:     }
                    654: #endif /* TEMPLATES */
                    655: }
                    656: 
                    657: /*----------------------------------------------------------------------
                    658:   ParseTemplate
1.3       vatton    659:   ----------------------------------------------------------------------*/
1.1       vatton    660: static void ParseTemplate (XTigerTemplate t, Element el, Document doc,
                    661:                            ThotBool loading)
                    662: {
                    663: #ifdef TEMPLATES
                    664:        AttributeType attType;
1.5       vatton    665:        Attribute     att;
                    666:        Element       aux, child; //Needed when deleting trees
                    667:        char         *name;
                    668:        ElementType   elType = TtaGetElementType (el);
1.1       vatton    669:        
1.5       vatton    670:   name = TtaGetSSchemaName (elType.ElSSchema);
                    671:        if (!strcmp (name, "Template"))
1.1       vatton    672:     {
1.5       vatton    673:       switch(elType.ElTypeNum)
1.1       vatton    674:         {
                    675:         case Template_EL_head :
                    676:           //Remove it and all of its children
                    677:           TtaDeleteTree(el, doc);
                    678:           //We must stop searching into this tree
                    679:           return;
                    680:           break;
                    681:         case Template_EL_component :
                    682:           //Replace by a use                           
1.5       vatton    683:           attType.AttrSSchema = elType.ElSSchema;
1.1       vatton    684:           attType.AttrTypeNum = Template_ATTR_name;
                    685:           
1.10      kia       686:           name = GetAttributeStringValue (el, Template_ATTR_name, NULL);                                 
1.5       vatton    687:           TtaRemoveAttribute (el, TtaGetAttribute (el, attType), doc);
1.1       vatton    688:           if (NeedAMenu (el, doc))
1.5       vatton    689:             TtaChangeElementType (el, Template_EL_useEl);
1.1       vatton    690:           else
1.5       vatton    691:             TtaChangeElementType (el, Template_EL_useSimple);
1.1       vatton    692:           
                    693:           attType.AttrTypeNum = Template_ATTR_types;
1.5       vatton    694:           att = TtaNewAttribute (attType);
                    695:           TtaAttachAttribute (el, att, doc);
                    696:           TtaSetAttributeText (att, name, el, doc);
1.1       vatton    697:           
                    698:           attType.AttrTypeNum = Template_ATTR_currentType;
1.5       vatton    699:           att = TtaNewAttribute (attType);
                    700:           TtaAttachAttribute (el, att, doc);             
                    701:           TtaSetAttributeText (att, name, el, doc);
1.1       vatton    702:           
                    703:           break;
                    704:         case Template_EL_option :
                    705:           aux = NULL;
                    706:           break;
                    707:         case Template_EL_bag :
                    708:           //Link to types
                    709:           //Allow editing the content
                    710:           break;
                    711:         case Template_EL_useEl :
                    712:         case Template_EL_useSimple :
                    713:           /* if this use element is not empty, don't do anything: it is
                    714:              supposed to contain a valid instance. This should be
                    715:              checked, though */
                    716:           if (!TtaGetFirstChild (el))
                    717:             InstantiateUse (t, el, doc, TRUE);
                    718:           break;
                    719:         case Template_EL_attribute :
                    720:           if (!loading)
                    721:             InstantiateAttribute (t, el, doc);
                    722:           break;
                    723:         case Template_EL_repeat :
                    724:           InstantiateRepeat (t, el, doc);
                    725:           break;
                    726:         default :
                    727:           break;
                    728:         }
                    729:     }
                    730:        
1.5       vatton    731:        child = TtaGetFirstChild (el);
                    732:        while (child)
1.1       vatton    733:     {
                    734:       aux = child;
1.5       vatton    735:       TtaNextSibling (&aux);
                    736:       ParseTemplate (t, child, doc, loading);
1.1       vatton    737:       child = aux;
                    738:     }
                    739: #endif /* TEMPLATES */
                    740: }
                    741: 
                    742: /*----------------------------------------------------------------------
1.3       vatton    743:   ----------------------------------------------------------------------*/
1.1       vatton    744: void DoInstanceTemplate (char *templatename)
                    745: {
                    746: #ifdef TEMPLATES
                    747:        XTigerTemplate  t;
                    748:        ElementType               elType;
                    749:        Element                     root, piElem, doctype, elFound, text;
                    750:        char                    *s, *charsetname = NULL, buffer[MAX_LENGTH];
                    751:   int             pi_type;
                    752:   Document        doc;
                    753: 
                    754:        //Instantiate all elements
1.8       kia       755:        t = (XTigerTemplate) Dictionary_Get (Templates_Dic, templatename);
1.1       vatton    756:   doc = GetTemplateDocument (t);
                    757:        root =  TtaGetMainRoot (doc);
                    758:        ParseTemplate (t, root, doc, FALSE);
                    759: 
                    760:   //Look for PIs
                    761:   /* check if the document has a DOCTYPE declaration */
                    762: #ifdef ANNOTATIONS
                    763:   if (DocumentTypes[doc]  == docAnnot)
                    764:     elType = TtaGetElementType (root);
                    765:   else
                    766: #endif /* ANNOTATIONS */
                    767:     elType = TtaGetElementType (root);
                    768:   s = TtaGetSSchemaName (elType.ElSSchema);
                    769:   if (strcmp (s, "HTML") == 0)
                    770:     {
                    771:       elType.ElTypeNum = HTML_EL_DOCTYPE;
                    772:       pi_type = HTML_EL_XMLPI;
                    773:     }
                    774: #ifdef _SVG
                    775:   else if (strcmp (s, "SVG") == 0)
                    776:     {
                    777:       elType.ElTypeNum = SVG_EL_DOCTYPE;
                    778:       pi_type = SVG_EL_XMLPI;
                    779:     }
                    780: #endif /* _SVG */
                    781:   else if (strcmp (s, "MathML") == 0)
                    782:     {
                    783:       elType.ElTypeNum = MathML_EL_DOCTYPE;
                    784:       pi_type = MathML_EL_XMLPI;
                    785:     }
                    786:   else
                    787:     {
                    788:       elType.ElTypeNum = XML_EL_doctype;
                    789:       pi_type = XML_EL_xmlpi;
                    790:     }
                    791:   doctype = TtaSearchTypedElement (elType, SearchInTree, root);
                    792:   if (!doctype)
                    793:     {
                    794:       /* generate the XML declaration */
                    795:       /* Check the Thot abstract tree against the structure schema. */
                    796:       TtaSetStructureChecking (FALSE, doc);
                    797:       elType.ElTypeNum = pi_type;
                    798:       doctype = TtaNewTree (doc, elType, "");
                    799:       TtaInsertFirstChild (&doctype, root, doc);
                    800:       elFound = TtaGetFirstChild (doctype);
                    801:       text = TtaGetFirstChild (elFound);
                    802:       strcpy (buffer, "xml version=\"1.0\" encoding=\"");
                    803:       charsetname = UpdateDocumentCharset (doc);
                    804:       strcat (buffer, charsetname);
                    805:       strcat (buffer, "\"");
                    806:       TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    807:       TtaSetStructureChecking (TRUE, doc);
                    808:     }
                    809:   
                    810:   /* generate the XTiger PI */
                    811:   /* Check the Thot abstract tree against the structure schema. */
                    812:   TtaSetStructureChecking (FALSE, doc);
                    813:   elType.ElTypeNum = pi_type;
                    814:   piElem = TtaNewTree (doc, elType, "");
                    815:   TtaInsertSibling(piElem, doctype, FALSE, doc);
                    816:   elFound = TtaGetFirstChild (piElem);
                    817:   text = TtaGetFirstChild (elFound);
                    818:   strcpy (buffer, "xtiger template=\"");
                    819:   strcat (buffer, templatename);
                    820:   strcat (buffer, "\"");
                    821:   TtaSetTextContent (text, (unsigned char*)buffer,  Latin_Script, doc);
                    822:   TtaSetStructureChecking (TRUE, doc);
1.5       vatton    823: 
                    824:   // update the document title
                    825:        if (!strcmp (s, "HTML"))
                    826:     {
                    827:       elType.ElTypeNum = HTML_EL_TITLE;
                    828:       elFound = TtaSearchTypedElement (elType, SearchInTree, root);
                    829:       if (elFound)
                    830:         {
                    831:           elFound = TtaGetFirstChild (elFound);
                    832:           TtaSetTextContent (elFound, (unsigned char *)Answer_text,
                    833:                              TtaGetDefaultLanguage (), doc);
                    834:         }
                    835:     }
1.1       vatton    836: #endif /* TEMPLATES */
                    837: }
                    838: 
                    839: /*----------------------------------------------------------------------
1.3       vatton    840:   PreInstantiateComponents: Instantiates all components in order to improve
                    841:   editing.
                    842:   ----------------------------------------------------------------------*/
1.1       vatton    843: void PreInstantiateComponents(XTigerTemplate t)
                    844: {
                    845: #ifdef TEMPLATES 
                    846:   DicDictionary components = GetComponents(t);
                    847:   Declaration comp;
                    848: 
1.8       kia       849:   for(Dictionary_First(components);!Dictionary_IsDone(components);Dictionary_Next(components))
1.1       vatton    850:     {
1.8       kia       851:       comp = (Declaration) Dictionary_CurrentElement(components);
1.1       vatton    852:       ParseTemplate(t, GetComponentContent(comp), GetTemplateDocument(t), TRUE);
                    853:     }
                    854: #endif /* TEMPLATES */
                    855: }

Webmaster