Annotation of Amaya/amaya/templateUtils.c, revision 1.39

1.9       vatton      1: /*
                      2:  *
1.37      vatton      3:  *  COPYRIGHT INRIA and W3C, 2006-2009
1.9       vatton      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7: 
1.3       quint       8: #include "templates.h"
1.14      kia         9: #include "Templatename.h"
                     10: #include "templates_f.h"
1.1       francesc   11: 
1.21      kia        12: #include "AHTURLTools_f.h"
                     13: #include "HTMLsave_f.h"
                     14: 
                     15: 
1.19      kia        16: #include <stdarg.h>
                     17: 
1.1       francesc   18: /*----------------------------------------------------------------------
1.30      kia        19: GetSchemaFromDocType: Returns the name of the schema corresponding to
1.1       francesc   20: a doc type.
                     21: ----------------------------------------------------------------------*/
1.24      kia        22: const char *GetSchemaFromDocType (DocumentType docType)
1.1       francesc   23: {
                     24: #ifdef TEMPLATES
                     25:        switch (docType)
                     26:     {
                     27:     case docAnnot :
                     28:                return "Annot";
                     29:     case docBookmark :
                     30:                return "Topics";
                     31:     case docSVG :
                     32:                return "SVG";
                     33:     case docMath :
                     34:                return "MathML";
                     35:     case docXml :
                     36:                return "XML";
                     37:     default :
                     38:                return "HTML";
                     39:     }
                     40: #endif // TEMPLATES
                     41:        return "HTML";
                     42: }
                     43: 
1.5       kia        44: /*----------------------------------------------------------------------
1.30      kia        45: Set the value of a string attribute
1.5       kia        46: ----------------------------------------------------------------------*/
1.24      kia        47: void SetAttributeStringValue (Element el, int att, const char* value)
1.5       kia        48: {
                     49: #ifdef TEMPLATES
1.7       vatton     50:   Document      doc = TtaGetDocument(el);
                     51:   AttributeType attType;
                     52:   Attribute     attribute;
1.5       kia        53: 
1.10      kia        54:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
1.9       vatton     55:     return;
1.5       kia        56:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     57:   attType.AttrTypeNum = att;
1.7       vatton     58:   attribute = TtaGetAttribute(el, attType);
                     59:   if (attribute == NULL)
                     60:     {
                     61:       attribute = TtaNewAttribute (attType);
                     62:       TtaAttachAttribute(el, attribute, doc);
                     63:     }
1.5       kia        64:   TtaSetAttributeText(attribute, value, el, doc);
                     65: #endif /* TEMPLATES */
                     66: }
                     67: 
1.11      kia        68: /*----------------------------------------------------------------------
                     69: Set the value of a string attribute and registering it in undo sequence.
                     70: ----------------------------------------------------------------------*/
                     71: void SetAttributeStringValueWithUndo (Element el, int att, char* value)
                     72: {
                     73: #ifdef TEMPLATES
                     74:   Document      doc = TtaGetDocument(el);
                     75:   AttributeType attType;
                     76:   Attribute     attribute;
                     77: 
                     78:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
                     79:     return;
                     80:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     81:   attType.AttrTypeNum = att;
                     82:   attribute = TtaGetAttribute(el, attType);
                     83:   if (attribute == NULL)
                     84:     {
                     85:       attribute = TtaNewAttribute (attType);
                     86:       TtaAttachAttribute(el, attribute, doc);
                     87:       TtaRegisterAttributeCreate(attribute, el, doc);
                     88:     }
                     89:   TtaSetAttributeText(attribute, value, el, doc);
                     90:   TtaRegisterAttributeReplace(attribute, el, doc);
                     91: #endif /* TEMPLATES */
                     92: }
                     93: 
1.13      kia        94: /*----------------------------------------------------------------------
1.30      kia        95: Returns the value of a string attribute without copy it
1.13      kia        96: ----------------------------------------------------------------------*/
                     97: void GiveAttributeStringValueFromNum (Element el, int att, char* buff, int* sz)
                     98: {
                     99: #ifdef TEMPLATES
                    100:   AttributeType attType;
                    101:   Attribute     attribute;
                    102:   int           size;
                    103: 
                    104:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    105:   attType.AttrTypeNum = att;
                    106:   attribute = TtaGetAttribute(el, attType);
1.30      kia       107: 
1.13      kia       108:   size = TtaGetTextAttributeLength(attribute);
                    109:   TtaGiveTextAttributeValue (attribute, buff, &size);
                    110:   buff[size] = EOS;
1.37      vatton    111:   if (sz)
1.13      kia       112:     *sz = size;
                    113: #endif /* TEMPLATES */
                    114: }
                    115: 
                    116: 
1.1       francesc  117: 
                    118: /*----------------------------------------------------------------------
1.31      vatton    119:   Returns the value of a string attribute or NULL
1.1       francesc  120: ----------------------------------------------------------------------*/
1.8       kia       121: char *GetAttributeStringValueFromNum (Element el, int att, int* sz)
1.1       francesc  122: {
                    123: #ifdef TEMPLATES
                    124:        AttributeType attType;
1.12      vatton    125:   Attribute     attribute;
                    126:   char         *aux;
                    127:   int           size;
                    128: 
1.1       francesc  129:        attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    130:        attType.AttrTypeNum = att;
1.12      vatton    131:        attribute = TtaGetAttribute(el, attType);
1.31      vatton    132:        if (attribute == NULL)
                    133:     return NULL;
1.30      kia       134: 
1.31      vatton    135:        size = TtaGetTextAttributeLength (attribute);
                    136:        aux = (char*) TtaGetMemory (size+1);
1.1       francesc  137:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    138:   aux[size] = EOS;
1.31      vatton    139:   if (sz)
1.4       kia       140:     *sz = size;
1.1       francesc  141:        return aux;
                    142: #else
1.8       kia       143:        return NULL;
1.1       francesc  144: #endif /* TEMPLATES */
                    145: }
1.2       francesc  146: 
                    147: /*----------------------------------------------------------------------
1.30      kia       148: Returns the value of an int attribute
                    149: ----------------------------------------------------------------------*/
                    150: int GetAttributeIntValueFromNum (Element el, int att)
                    151: {
                    152: #ifdef TEMPLATES
                    153:   AttributeType attType;
                    154:   Attribute     attribute;
                    155: 
                    156:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    157:   attType.AttrTypeNum = att;
                    158:   attribute = TtaGetAttribute(el, attType);
                    159: 
                    160:   return TtaGetAttributeValue(attribute);
                    161: #else
                    162:   return NULL;
                    163: #endif /* TEMPLATES */
                    164: }
                    165: 
                    166: /*----------------------------------------------------------------------
                    167: Set the value of a int attribute and registering it in undo sequence if wanted
                    168: ----------------------------------------------------------------------*/
                    169: void SetAttributeIntValue (Element el, int att, int value, ThotBool undo)
                    170: {
                    171: #ifdef TEMPLATES
                    172:   Document      doc = TtaGetDocument(el);
                    173:   AttributeType attType;
                    174:   Attribute     attribute;
                    175: 
                    176:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
                    177:     return;
                    178:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    179:   attType.AttrTypeNum = att;
                    180:   attribute = TtaGetAttribute(el, attType);
                    181:   if (attribute == NULL)
                    182:     {
                    183:       attribute = TtaNewAttribute (attType);
                    184:       TtaAttachAttribute(el, attribute, doc);
1.37      vatton    185:       if (undo)
1.30      kia       186:         TtaRegisterAttributeCreate(attribute, el, doc);
                    187:     }
                    188:   TtaSetAttributeValue(attribute, value, el, doc);
1.37      vatton    189:   if (undo)
1.30      kia       190:     TtaRegisterAttributeReplace(attribute, el, doc);
                    191: #endif /* TEMPLATES */
                    192: }
                    193: 
                    194: 
                    195: 
                    196: 
                    197: /*----------------------------------------------------------------------
                    198: Returns the value of a string attribute
1.2       francesc  199: ----------------------------------------------------------------------*/
1.4       kia       200: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz)
1.2       francesc  201: {
                    202: #ifdef TEMPLATES
                    203:        int size = TtaGetTextAttributeLength(attribute);
                    204:        char *aux = (char*) TtaGetMemory(size+1);
1.12      vatton    205: 
1.2       francesc  206:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    207:   aux[size] = EOS;
1.37      vatton    208:   if (sz)
1.4       kia       209:     *sz = size;
1.2       francesc  210:        return aux;
                    211: #else
1.8       kia       212:        return NULL;
1.2       francesc  213: #endif /* TEMPLATES */
                    214: }
1.6       kia       215: 
1.31      vatton    216: /*----------------------------------------------------------------------
                    217:   GetAncestorComponentName returns the name of the ancestor component
                    218:   or NULL;
                    219:   ----------------------------------------------------------------------*/
                    220: char *GetAncestorComponentName (Element *el)
                    221: {
                    222: #ifdef TEMPLATES
                    223:   ElementType   elType;
                    224:   Element       anc = NULL;
                    225:   char         *name;
                    226: 
                    227:   elType = TtaGetElementType (*el);
                    228:   elType.ElTypeNum = Template_EL_component;
                    229:   anc = TtaGetParent (*el);
                    230:   anc = TtaGetExactTypedAncestor (anc, elType);
                    231:   if (anc)
                    232:     {
1.32      kia       233:       name = GetAttributeStringValueFromNum (anc, Template_ATTR_name, NULL);
1.31      vatton    234:       *el = anc;
                    235:       return name;
                    236:     }
1.32      kia       237: #endif /* TEMPLATES */
1.31      vatton    238:   return NULL;
                    239: }
                    240: 
1.6       kia       241: 
                    242: /*----------------------------------------------------------------------
                    243: GetFirstEditableElement
                    244: Returns the first descendant element which is modifiable.
                    245: ----------------------------------------------------------------------*/
                    246: Element GetFirstEditableElement (Element el)
                    247: {
1.38      vatton    248:   ElementType     elType;
                    249:   Element         res = NULL, current;
1.30      kia       250: 
1.38      vatton    251:   current = TtaGetFirstChild(el);
1.37      vatton    252:   while (!res && current)
1.38      vatton    253:     {
                    254:       // skip col colgroup 
                    255:       elType = TtaGetElementType (current);
                    256:       if ((elType.ElSSchema &&
                    257:            strcmp (TtaGetSSchemaName(elType.ElSSchema), "HTML")) ||
                    258:           (elType.ElTypeNum != HTML_EL_ColStruct &&
                    259:            elType.ElTypeNum != HTML_EL_Table_head &&
                    260:            elType.ElTypeNum != HTML_EL_Comment_))
                    261:         res = GetFirstEditableElement (current);
                    262:       TtaNextSibling(&current);
                    263:     }
1.37      vatton    264:   if (!res && !TtaIsReadOnly(el))
1.6       kia       265:     res = el;
                    266:   return res;
                    267: }
1.14      kia       268: 
                    269: /*----------------------------------------------------------------------
1.15      kia       270:   TemplateCanInsertFirstChild
                    271:   Test if an element can be inserted as child of another, bypassing xt.
                    272: ----------------------------------------------------------------------*/
1.37      vatton    273: ThotBool TemplateCanInsertFirstChild (ElementType elementType, Element parent,
                    274:                                       Document document)
1.15      kia       275: {
                    276: #ifdef TEMPLATES
                    277:   SSchema         templateSSchema = TtaGetSSchema ("Template", document);
                    278:   ElementType     parType;
1.30      kia       279: 
1.37      vatton    280:   while (parent)
1.15      kia       281:     {
                    282:       parType = TtaGetElementType(parent);
1.37      vatton    283:       if (parType.ElSSchema != templateSSchema)
1.15      kia       284:         break;
                    285:       parent = TtaGetParent(parent);
                    286:     }
1.37      vatton    287:   if (!parent)
1.15      kia       288:     return FALSE;
                    289: #endif /* TEMPLATES */
                    290:   return TtaCanInsertFirstChild(elementType, parent, document);
                    291: }
                    292: 
                    293: /*----------------------------------------------------------------------
1.37      vatton    294:   CheckTemplateAttrInMenu
1.14      kia       295:   Validate the status of an attribute according to xt::atribute rules.
1.37      vatton    296:        Return TRUE if the attribute is not valid
1.14      kia       297:   ----------------------------------------------------------------------*/
1.37      vatton    298: ThotBool CheckTemplateAttrInMenu (NotifyAttribute *event)
1.14      kia       299: {
                    300: #ifdef TEMPLATES
1.37      vatton    301:   Document      doc = event->document;
1.14      kia       302:   Element       elem;
1.37      vatton    303:   Element       parent = event->element;
1.14      kia       304:   ElementType   elType;
                    305:   AttributeType attrType;
                    306:   Attribute     attr;
1.37      vatton    307:   char         *attrName;
1.14      kia       308:   char          buffer[MAX_LENGTH];
1.37      vatton    309:   int           sz, useAt, type;
1.30      kia       310: 
1.14      kia       311:   /* Prevent from showing attributes for template instance but not templates. */
1.37      vatton    312:   if (IsTemplateInstanceDocument(doc))
1.14      kia       313:     {
                    314:       /* Prevent if attribute's element is not a descendant of xt:use */
1.17      kia       315:       /* Dont prevent if descendant of xt:bag. */
1.14      kia       316:       elem = GetFirstTemplateParentElement(parent);
1.37      vatton    317:       if (!elem)
1.14      kia       318:         return TRUE;
1.37      vatton    319:       elType = TtaGetElementType(elem);
                    320:       if (elType.ElTypeNum == Template_EL_bag)
                    321:         return FALSE;  /* let Thot perform normal operation */
                    322:       if (elType.ElTypeNum != Template_EL_useSimple)
1.14      kia       323:         return TRUE;
1.37      vatton    324:       if (!TtaIsReadOnly (parent))
                    325:         return FALSE;  /* let Thot perform normal operation */
                    326:  
1.14      kia       327:       /* Search for the corresponding xt:attribute element*/
                    328:       attrName = TtaGetAttributeName(event->attributeType);
1.37      vatton    329:       attrType.AttrSSchema = TtaGetSSchema ("Template", doc);
                    330:       for (elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem))
1.14      kia       331:         {
                    332:           attrType.AttrTypeNum = Template_ATTR_ref_name;
                    333:           elType = TtaGetElementType(elem);
1.37      vatton    334:           if (elType.ElTypeNum == Template_EL_attribute &&
                    335:               elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.14      kia       336:             {
                    337:                attr = TtaGetAttribute(elem, attrType);
1.37      vatton    338:                if (attr)
1.14      kia       339:                  {
                    340:                    sz = MAX_LENGTH;
                    341:                    TtaGiveTextAttributeValue(attr, buffer, &sz);
1.37      vatton    342:                    if (!strcmp(buffer, attrName))
1.14      kia       343:                      {
                    344:                        /* Process the attribute filtering */
                    345:                        /* Get 'useAt' attr value. */
                    346:                        attrType.AttrTypeNum = Template_ATTR_useAt;
                    347:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    348:                        if (attr)
1.14      kia       349:                          useAt = TtaGetAttributeValue(attr);
                    350:                        else
                    351:                          useAt = Template_ATTR_useAt_VAL_required;
1.30      kia       352:                        /* Get 'type' attr value. */
1.14      kia       353:                        attrType.AttrTypeNum = Template_ATTR_type;
                    354:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    355:                        if (attr)
1.14      kia       356:                          type = TtaGetAttributeValue(attr);
                    357:                        else
                    358:                          type = Template_ATTR_type_VAL_string;
1.37      vatton    359: 
1.14      kia       360:                        event->restr.RestrType = (RestrictionContentType)type;
                    361:                        /* If attr is prohibited, dont show it.*/
1.37      vatton    362:                        if (useAt == Template_ATTR_useAt_VAL_prohibited)
1.16      kia       363:                            return TRUE;
1.37      vatton    364:                        if (useAt == Template_ATTR_useAt_VAL_required)
1.14      kia       365:                          {
                    366:                            /* Force the usage of this attribute.*/
                    367:                            event->restr.RestrFlags |= attr_mandatory;
                    368:                          }
                    369: 
                    370:                        /* Get 'fixed' attr value. */
                    371:                        attrType.AttrTypeNum = Template_ATTR_fixed;
                    372:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    373:                        if (attr)
1.14      kia       374:                          {
                    375:                            sz = MAX_LENGTH;
                    376:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    377:                            event->restr.RestrFlags |= attr_readonly;
                    378:                            event->restr.RestrDefVal = TtaStrdup(buffer);
1.37      vatton    379:                            return FALSE;       /* let Thot perform normal operation */
1.14      kia       380:                          }
                    381: 
                    382:                        /* Get 'default' attr value.*/
                    383:                        attrType.AttrTypeNum = Template_ATTR_defaultAt;
                    384:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    385:                        if (attr)
1.14      kia       386:                          {
                    387:                            sz = MAX_LENGTH;
                    388:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    389:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    390:                          }
                    391: 
                    392:                        /* Get 'values' attr value.*/
                    393:                        attrType.AttrTypeNum = Template_ATTR_values;
                    394:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    395:                        if (attr)
1.14      kia       396:                          {
                    397:                            sz = MAX_LENGTH;
                    398:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    399:                            event->restr.RestrEnumVal = TtaStrdup(buffer);
                    400:                            event->restr.RestrFlags |= attr_enum;
                    401:                          }
1.37      vatton    402:                        return FALSE;   /* let Thot perform normal operation */
1.14      kia       403:                      }
                    404:                  }
                    405:             }
                    406:         }
1.16      kia       407:       return TRUE;
1.14      kia       408:     }
                    409: #endif /* TEMPLATES */
1.37      vatton    410:   return FALSE;
1.14      kia       411: }
1.19      kia       412: 
                    413: /*----------------------------------------------------------------------
1.20      kia       414:  * Dump element path
                    415:   ----------------------------------------------------------------------*/
1.37      vatton    416: void DumpElementSubPath (Element el, char* buffer)
1.20      kia       417: {
1.36      vatton    418: #ifdef TEMPLATE_DEBUG
1.20      kia       419:   Element parent = TtaGetParent(el);
1.37      vatton    420:   if (parent == NULL)
1.20      kia       421:     strcpy(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    422:   else
                    423:     {
                    424:       DumpElementSubPath(parent, buffer);
                    425:       strcat(buffer, "/");
                    426:       strcat(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    427:     }
1.36      vatton    428: #endif /* TEMPLATE_DEBUG */
1.20      kia       429: }
                    430: 
                    431: /*----------------------------------------------------------------------
                    432:  * Dump element path
                    433:   ----------------------------------------------------------------------*/
1.37      vatton    434: void DumpElementPath (Element el)
1.20      kia       435: {
1.36      vatton    436: #ifdef TEMPLATE_DEBUG
1.20      kia       437:   char buffer[MAX_LENGTH];
1.37      vatton    438: 
1.20      kia       439:   DumpElementSubPath(el, buffer);
                    440:   printf("%s\n", buffer);
1.36      vatton    441: #endif /* TEMPLATE_DEBUG */
1.20      kia       442: }
                    443: 
                    444: 
                    445: /*----------------------------------------------------------------------
1.19      kia       446:  * Dump template element
                    447:   ----------------------------------------------------------------------*/
1.37      vatton    448: void DumpTemplateElement (Element el, Document doc)
1.19      kia       449: {
1.36      vatton    450: #ifdef TEMPLATE_DEBUG
1.28      vatton    451:   ElementType    elType;
                    452:   AttributeType  attType;
                    453:   Attribute      att;
                    454:   SSchema        schema = TtaGetSSchema ("Template", doc);
                    455:   char*          str;
                    456:   char           buffer[MAX_LENGTH];
                    457:   int            len;
                    458:   Language       lang;
1.30      kia       459: 
1.37      vatton    460:   if (el && doc)
1.19      kia       461:     {
                    462:       elType = TtaGetElementType(el);
1.26      kia       463:       printf("- %p %d ", elType.ElSSchema, elType.ElTypeNum);
                    464:       printf(" %s", TtaGetSSchemaName(elType.ElSSchema));
                    465:       printf(":%s", TtaGetElementTypeName(elType));
1.37      vatton    466:       if (elType.ElTypeNum == 1)
1.26      kia       467:         {
                    468:           len = MAX_LENGTH-1;
                    469:           TtaGiveTextContent(el, (unsigned char*)buffer, &len, &lang);
                    470:           buffer[len] = EOS;
                    471:           printf(" \"%s\"", buffer);
                    472:         }
1.30      kia       473: 
1.37      vatton    474:       if (elType.ElSSchema == schema)
1.19      kia       475:         {
                    476:           switch(elType.ElTypeNum)
                    477:             {
                    478:               case Template_EL_head:
                    479:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_version, NULL);
                    480:                 printf(" version=%s", str);
                    481:                 TtaFreeMemory(str);
                    482:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_templateVersion, NULL);
                    483:                 printf(" templateVersion=%s", str);
1.30      kia       484:                 TtaFreeMemory(str);
1.19      kia       485:                 break;
                    486:               case Template_EL_component:
                    487:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    488:                 printf(" name=%s", str);
                    489:                 TtaFreeMemory(str);
                    490:                 break;
                    491:               case Template_EL_union:
                    492:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    493:                 printf(" name=%s", str);
                    494:                 TtaFreeMemory(str);
                    495:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_includeAt, NULL);
                    496:                 printf(" include=%s", str);
                    497:                 TtaFreeMemory(str);
                    498:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_exclude, NULL);
                    499:                 printf(" exclude=%s", str);
                    500:                 TtaFreeMemory(str);
                    501:                 break;
                    502:               case Template_EL_import:
                    503:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_src, NULL);
                    504:                 printf(" src=%s", str);
                    505:                 TtaFreeMemory(str);
                    506:                 break;
                    507:               case Template_EL_repeat:
                    508:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    509:                 printf(" label=%s", str);
                    510:                 TtaFreeMemory(str);
                    511:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_minOccurs, NULL);
                    512:                 printf(" minOccurs=%s", str);
                    513:                 TtaFreeMemory(str);
                    514:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
                    515:                 printf(" maxOccurs=%s", str);
                    516:                 TtaFreeMemory(str);
                    517:                 break;
                    518:               case Template_EL_useSimple:
                    519:               case Template_EL_useEl:
                    520:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    521:                 printf(" label=%s", str);
                    522:                 TtaFreeMemory(str);
                    523:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    524:                 printf(" types=%s", str);
                    525:                 TtaFreeMemory(str);
1.28      vatton    526:                 attType.AttrSSchema = elType.ElSSchema;
                    527:                 attType.AttrTypeNum = Template_ATTR_option;
                    528:                 att = TtaGetAttribute (el, attType);
                    529:                 if (att)
                    530:                   printf(" option");
1.19      kia       531:                 break;
                    532:               case Template_EL_bag:
                    533:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    534:                 printf(" label=%s", str);
                    535:                 TtaFreeMemory(str);
                    536:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    537:                 printf(" types=%s", str);
                    538:                 TtaFreeMemory(str);
                    539:                 break;
                    540:               case Template_EL_attribute:
                    541:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_ref_name, NULL);
                    542:                 printf(" name=%s", str);
                    543:                 TtaFreeMemory(str);
                    544:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_type, NULL);
                    545:                 printf(" type=%s", str);
                    546:                 TtaFreeMemory(str);
                    547:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_useAt, NULL);
                    548:                 printf(" use=%s", str);
                    549:                 TtaFreeMemory(str);
                    550:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_defaultAt, NULL);
                    551:                 printf(" default=%s", str);
                    552:                 TtaFreeMemory(str);
                    553:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_fixed, NULL);
                    554:                 printf(" fixed=%s", str);
                    555:                 TtaFreeMemory(str);
                    556:                 break;
                    557:             }
                    558:         }
                    559:     }
1.36      vatton    560: #endif /* TEMPLATE_DEBUG */
1.19      kia       561: }
1.21      kia       562: 
                    563: /*----------------------------------------------------------------------
1.25      kia       564:  * Dump subtree
                    565:   ----------------------------------------------------------------------*/
                    566: void DumpSubtree(Element el, Document doc, int off)
                    567: {
1.36      vatton    568: #ifdef TEMPLATE_DEBUG
1.25      kia       569:   Element child = TtaGetFirstChild(el);
                    570:   int i;
1.30      kia       571: 
1.37      vatton    572:   for (i=0; i<off; i++)
1.25      kia       573:     printf("  ");
                    574:   DumpTemplateElement(el, doc);
                    575:   printf("\n");
                    576: 
1.37      vatton    577:   while (child)
1.25      kia       578:     {
                    579:       DumpSubtree(child, doc, off+1);
                    580:       TtaNextSibling(&child);
                    581:     }
1.36      vatton    582: #endif /* TEMPLATE_DEBUG */
1.25      kia       583: }
                    584: 
                    585: /*----------------------------------------------------------------------
1.34      vatton    586:   Save an opened document to a specified path in order to open.
                    587:   The parameter doc is the original doc to be saved
                    588:   The parameter newdoc is the new document
                    589:   The parameter newpath is the newdoc URI
                    590:   Return the saved localFile (to be freed) or NULL
1.21      kia       591:   ----------------------------------------------------------------------*/
1.34      vatton    592: char *SaveDocumentToNewDoc(Document doc, Document newdoc, char* newpath)
1.21      kia       593: {
                    594:   ElementType   elType;
                    595:   Element       root;
1.34      vatton    596:   char         *localFile = NULL, *s;
1.21      kia       597:   ThotBool      res = FALSE;
1.30      kia       598: 
1.21      kia       599:   localFile = GetLocalPath (newdoc, newpath);
1.22      kia       600:   // update all links
1.39    ! vatton    601:   SetRelativeURLs (doc, newpath, NULL, FALSE, FALSE, FALSE, FALSE);
1.22      kia       602:   // prepare the new document view
                    603:   TtaExtractName (newpath, DirectoryName, DocumentName);
1.21      kia       604: 
1.22      kia       605:   root = TtaGetRootElement(doc);
                    606:   elType = TtaGetElementType (root);
                    607:   // get the target document type
                    608:   s = TtaGetSSchemaName (elType.ElSSchema);
                    609:   if (strcmp (s, "HTML") == 0)
                    610:     {
                    611:       /* docType = docHTML; */
                    612:       if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
                    613:           TtaGetDocumentProfile(doc) == L_Basic)
                    614:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11", FALSE);
1.21      kia       615:       else
1.22      kia       616:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX", FALSE);
                    617:     }
                    618:   else if (strcmp (s, "SVG") == 0)
                    619:     /* docType = docSVG; */
                    620:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT", FALSE);
                    621:   else if (strcmp (s, "MathML") == 0)
                    622:     /* docType = docMath; */
                    623:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT", FALSE);
                    624:   else
                    625:     /* docType = docXml; */
                    626:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL, FALSE);
1.34      vatton    627:   if (res)
                    628:     return localFile;
                    629:   else
                    630:     {
                    631:       TtaFreeMemory (localFile);
                    632:       return NULL;
                    633:     }
1.21      kia       634: }
1.29      kia       635: 
                    636: /*----------------------------------------------------------------------
                    637:  * Retrieve the xt:head element.
                    638:   ----------------------------------------------------------------------*/
1.35      vatton    639: Element TemplateFindHead (Document doc)
1.29      kia       640: {
                    641: #ifdef TEMPLATES
1.35      vatton    642:   ElementType headType, elType;
                    643:   Element     head, root;
                    644: 
1.29      kia       645:   headType.ElSSchema = TtaGetSSchema ("Template", doc);
1.35      vatton    646:   if (headType.ElSSchema == NULL)
                    647:     return NULL;
                    648: 
1.29      kia       649:   headType.ElTypeNum = Template_EL_head;
1.35      vatton    650:   root = TtaGetMainRoot (doc);
                    651:   head = TtaSearchTypedElement (headType, SearchInTree, root);
                    652:   if (head == NULL)
                    653:     {
                    654:       // create the template head
                    655:       head = TtaNewElement (doc, headType);
                    656:       elType = TtaGetElementType (root);
                    657:       if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
                    658:         {
                    659:           elType.ElTypeNum = HTML_EL_HEAD;
                    660:           root = TtaSearchTypedElement (elType, SearchInTree, root);
                    661:         }
                    662:       TtaInsertFirstChild (&head, root, doc);
                    663:       SetAttributeStringValue (head, Template_ATTR_version, Template_Current_Version);
                    664:       SetAttributeStringValue (head, Template_ATTR_templateVersion, "1.0");      
                    665:     }
                    666:   return head;
1.29      kia       667: #else /* TEMPLATES */
                    668:   return NULL;
                    669: #endif /* TEMPLATES */
                    670: }
                    671: 
                    672: 

Webmaster