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

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: {
                    248:   Element res = NULL;
                    249:   Element current = TtaGetFirstChild(el);
1.30      kia       250: 
1.37    ! vatton    251:   while (!res && current)
1.6       kia       252:   {
                    253:     res = GetFirstEditableElement(current);
                    254:     TtaNextSibling(&current);
                    255:   }
1.30      kia       256: 
1.37    ! vatton    257:   if (!res && !TtaIsReadOnly(el))
1.6       kia       258:     res = el;
1.30      kia       259: 
1.6       kia       260:   return res;
                    261: }
1.14      kia       262: 
                    263: /*----------------------------------------------------------------------
1.15      kia       264:   TemplateCanInsertFirstChild
                    265:   Test if an element can be inserted as child of another, bypassing xt.
                    266: ----------------------------------------------------------------------*/
1.37    ! vatton    267: ThotBool TemplateCanInsertFirstChild (ElementType elementType, Element parent,
        !           268:                                       Document document)
1.15      kia       269: {
                    270: #ifdef TEMPLATES
                    271:   SSchema         templateSSchema = TtaGetSSchema ("Template", document);
                    272:   ElementType     parType;
1.30      kia       273: 
1.37    ! vatton    274:   while (parent)
1.15      kia       275:     {
                    276:       parType = TtaGetElementType(parent);
1.37    ! vatton    277:       if (parType.ElSSchema != templateSSchema)
1.15      kia       278:         break;
                    279:       parent = TtaGetParent(parent);
                    280:     }
1.37    ! vatton    281:   if (!parent)
1.15      kia       282:     return FALSE;
                    283: #endif /* TEMPLATES */
                    284:   return TtaCanInsertFirstChild(elementType, parent, document);
                    285: }
                    286: 
                    287: /*----------------------------------------------------------------------
1.37    ! vatton    288:   CheckTemplateAttrInMenu
1.14      kia       289:   Validate the status of an attribute according to xt::atribute rules.
1.37    ! vatton    290:        Return TRUE if the attribute is not valid
1.14      kia       291:   ----------------------------------------------------------------------*/
1.37    ! vatton    292: ThotBool CheckTemplateAttrInMenu (NotifyAttribute *event)
1.14      kia       293: {
                    294: #ifdef TEMPLATES
1.37    ! vatton    295:   Document      doc = event->document;
1.14      kia       296:   Element       elem;
1.37    ! vatton    297:   Element       parent = event->element;
1.14      kia       298:   ElementType   elType;
                    299:   AttributeType attrType;
                    300:   Attribute     attr;
1.37    ! vatton    301:   char         *attrName;
1.14      kia       302:   char          buffer[MAX_LENGTH];
1.37    ! vatton    303:   int           sz, useAt, type;
1.30      kia       304: 
1.14      kia       305:   /* Prevent from showing attributes for template instance but not templates. */
1.37    ! vatton    306:   if (IsTemplateInstanceDocument(doc))
1.14      kia       307:     {
                    308:       /* Prevent if attribute's element is not a descendant of xt:use */
1.17      kia       309:       /* Dont prevent if descendant of xt:bag. */
1.14      kia       310:       elem = GetFirstTemplateParentElement(parent);
1.37    ! vatton    311:       if (!elem)
1.14      kia       312:         return TRUE;
1.37    ! vatton    313:       elType = TtaGetElementType(elem);
        !           314:       if (elType.ElTypeNum == Template_EL_bag)
        !           315:         return FALSE;  /* let Thot perform normal operation */
        !           316:       if (elType.ElTypeNum != Template_EL_useSimple)
1.14      kia       317:         return TRUE;
1.37    ! vatton    318:       if (!TtaIsReadOnly (parent))
        !           319:         return FALSE;  /* let Thot perform normal operation */
        !           320:  
1.14      kia       321:       /* Search for the corresponding xt:attribute element*/
                    322:       attrName = TtaGetAttributeName(event->attributeType);
1.37    ! vatton    323:       attrType.AttrSSchema = TtaGetSSchema ("Template", doc);
        !           324:       for (elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem))
1.14      kia       325:         {
                    326:           attrType.AttrTypeNum = Template_ATTR_ref_name;
                    327:           elType = TtaGetElementType(elem);
1.37    ! vatton    328:           if (elType.ElTypeNum == Template_EL_attribute &&
        !           329:               elType.ElSSchema == TtaGetSSchema ("Template", doc))
1.14      kia       330:             {
                    331:                attr = TtaGetAttribute(elem, attrType);
1.37    ! vatton    332:                if (attr)
1.14      kia       333:                  {
                    334:                    sz = MAX_LENGTH;
                    335:                    TtaGiveTextAttributeValue(attr, buffer, &sz);
1.37    ! vatton    336:                    if (!strcmp(buffer, attrName))
1.14      kia       337:                      {
                    338:                        /* Process the attribute filtering */
                    339:                        /* Get 'useAt' attr value. */
                    340:                        attrType.AttrTypeNum = Template_ATTR_useAt;
                    341:                        attr = TtaGetAttribute(elem, attrType);
1.37    ! vatton    342:                        if (attr)
1.14      kia       343:                          useAt = TtaGetAttributeValue(attr);
                    344:                        else
                    345:                          useAt = Template_ATTR_useAt_VAL_required;
1.30      kia       346:                        /* Get 'type' attr value. */
1.14      kia       347:                        attrType.AttrTypeNum = Template_ATTR_type;
                    348:                        attr = TtaGetAttribute(elem, attrType);
1.37    ! vatton    349:                        if (attr)
1.14      kia       350:                          type = TtaGetAttributeValue(attr);
                    351:                        else
                    352:                          type = Template_ATTR_type_VAL_string;
1.37    ! vatton    353: 
1.14      kia       354:                        event->restr.RestrType = (RestrictionContentType)type;
                    355:                        /* If attr is prohibited, dont show it.*/
1.37    ! vatton    356:                        if (useAt == Template_ATTR_useAt_VAL_prohibited)
1.16      kia       357:                            return TRUE;
1.37    ! vatton    358:                        if (useAt == Template_ATTR_useAt_VAL_required)
1.14      kia       359:                          {
                    360:                            /* Force the usage of this attribute.*/
                    361:                            event->restr.RestrFlags |= attr_mandatory;
                    362:                          }
                    363: 
                    364:                        /* Get 'fixed' attr value. */
                    365:                        attrType.AttrTypeNum = Template_ATTR_fixed;
                    366:                        attr = TtaGetAttribute(elem, attrType);
1.37    ! vatton    367:                        if (attr)
1.14      kia       368:                          {
                    369:                            sz = MAX_LENGTH;
                    370:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    371:                            event->restr.RestrFlags |= attr_readonly;
                    372:                            event->restr.RestrDefVal = TtaStrdup(buffer);
1.37    ! vatton    373:                            return FALSE;       /* let Thot perform normal operation */
1.14      kia       374:                          }
                    375: 
                    376:                        /* Get 'default' attr value.*/
                    377:                        attrType.AttrTypeNum = Template_ATTR_defaultAt;
                    378:                        attr = TtaGetAttribute(elem, attrType);
1.37    ! vatton    379:                        if (attr)
1.14      kia       380:                          {
                    381:                            sz = MAX_LENGTH;
                    382:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    383:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    384:                          }
                    385: 
                    386:                        /* Get 'values' attr value.*/
                    387:                        attrType.AttrTypeNum = Template_ATTR_values;
                    388:                        attr = TtaGetAttribute(elem, attrType);
1.37    ! vatton    389:                        if (attr)
1.14      kia       390:                          {
                    391:                            sz = MAX_LENGTH;
                    392:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    393:                            event->restr.RestrEnumVal = TtaStrdup(buffer);
                    394:                            event->restr.RestrFlags |= attr_enum;
                    395:                          }
1.37    ! vatton    396:                        return FALSE;   /* let Thot perform normal operation */
1.14      kia       397:                      }
                    398:                  }
                    399:             }
                    400:         }
1.16      kia       401:       return TRUE;
1.14      kia       402:     }
                    403: #endif /* TEMPLATES */
1.37    ! vatton    404:   return FALSE;
1.14      kia       405: }
1.19      kia       406: 
                    407: /*----------------------------------------------------------------------
1.20      kia       408:  * Dump element path
                    409:   ----------------------------------------------------------------------*/
1.37    ! vatton    410: void DumpElementSubPath (Element el, char* buffer)
1.20      kia       411: {
1.36      vatton    412: #ifdef TEMPLATE_DEBUG
1.20      kia       413:   Element parent = TtaGetParent(el);
1.37    ! vatton    414:   if (parent == NULL)
1.20      kia       415:     strcpy(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    416:   else
                    417:     {
                    418:       DumpElementSubPath(parent, buffer);
                    419:       strcat(buffer, "/");
                    420:       strcat(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    421:     }
1.36      vatton    422: #endif /* TEMPLATE_DEBUG */
1.20      kia       423: }
                    424: 
                    425: /*----------------------------------------------------------------------
                    426:  * Dump element path
                    427:   ----------------------------------------------------------------------*/
1.37    ! vatton    428: void DumpElementPath (Element el)
1.20      kia       429: {
1.36      vatton    430: #ifdef TEMPLATE_DEBUG
1.20      kia       431:   char buffer[MAX_LENGTH];
1.37    ! vatton    432: 
1.20      kia       433:   DumpElementSubPath(el, buffer);
                    434:   printf("%s\n", buffer);
1.36      vatton    435: #endif /* TEMPLATE_DEBUG */
1.20      kia       436: }
                    437: 
                    438: 
                    439: /*----------------------------------------------------------------------
1.19      kia       440:  * Dump template element
                    441:   ----------------------------------------------------------------------*/
1.37    ! vatton    442: void DumpTemplateElement (Element el, Document doc)
1.19      kia       443: {
1.36      vatton    444: #ifdef TEMPLATE_DEBUG
1.28      vatton    445:   ElementType    elType;
                    446:   AttributeType  attType;
                    447:   Attribute      att;
                    448:   SSchema        schema = TtaGetSSchema ("Template", doc);
                    449:   char*          str;
                    450:   char           buffer[MAX_LENGTH];
                    451:   int            len;
                    452:   Language       lang;
1.30      kia       453: 
1.37    ! vatton    454:   if (el && doc)
1.19      kia       455:     {
                    456:       elType = TtaGetElementType(el);
1.26      kia       457:       printf("- %p %d ", elType.ElSSchema, elType.ElTypeNum);
                    458:       printf(" %s", TtaGetSSchemaName(elType.ElSSchema));
                    459:       printf(":%s", TtaGetElementTypeName(elType));
1.37    ! vatton    460:       if (elType.ElTypeNum == 1)
1.26      kia       461:         {
                    462:           len = MAX_LENGTH-1;
                    463:           TtaGiveTextContent(el, (unsigned char*)buffer, &len, &lang);
                    464:           buffer[len] = EOS;
                    465:           printf(" \"%s\"", buffer);
                    466:         }
1.30      kia       467: 
1.37    ! vatton    468:       if (elType.ElSSchema == schema)
1.19      kia       469:         {
                    470:           switch(elType.ElTypeNum)
                    471:             {
                    472:               case Template_EL_head:
                    473:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_version, NULL);
                    474:                 printf(" version=%s", str);
                    475:                 TtaFreeMemory(str);
                    476:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_templateVersion, NULL);
                    477:                 printf(" templateVersion=%s", str);
1.30      kia       478:                 TtaFreeMemory(str);
1.19      kia       479:                 break;
                    480:               case Template_EL_component:
                    481:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    482:                 printf(" name=%s", str);
                    483:                 TtaFreeMemory(str);
                    484:                 break;
                    485:               case Template_EL_union:
                    486:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    487:                 printf(" name=%s", str);
                    488:                 TtaFreeMemory(str);
                    489:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_includeAt, NULL);
                    490:                 printf(" include=%s", str);
                    491:                 TtaFreeMemory(str);
                    492:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_exclude, NULL);
                    493:                 printf(" exclude=%s", str);
                    494:                 TtaFreeMemory(str);
                    495:                 break;
                    496:               case Template_EL_import:
                    497:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_src, NULL);
                    498:                 printf(" src=%s", str);
                    499:                 TtaFreeMemory(str);
                    500:                 break;
                    501:               case Template_EL_repeat:
                    502:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    503:                 printf(" label=%s", str);
                    504:                 TtaFreeMemory(str);
                    505:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_minOccurs, NULL);
                    506:                 printf(" minOccurs=%s", str);
                    507:                 TtaFreeMemory(str);
                    508:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
                    509:                 printf(" maxOccurs=%s", str);
                    510:                 TtaFreeMemory(str);
                    511:                 break;
                    512:               case Template_EL_useSimple:
                    513:               case Template_EL_useEl:
                    514:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    515:                 printf(" label=%s", str);
                    516:                 TtaFreeMemory(str);
                    517:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    518:                 printf(" types=%s", str);
                    519:                 TtaFreeMemory(str);
1.28      vatton    520:                 attType.AttrSSchema = elType.ElSSchema;
                    521:                 attType.AttrTypeNum = Template_ATTR_option;
                    522:                 att = TtaGetAttribute (el, attType);
                    523:                 if (att)
                    524:                   printf(" option");
1.19      kia       525:                 break;
                    526:               case Template_EL_bag:
                    527:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    528:                 printf(" label=%s", str);
                    529:                 TtaFreeMemory(str);
                    530:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    531:                 printf(" types=%s", str);
                    532:                 TtaFreeMemory(str);
                    533:                 break;
                    534:               case Template_EL_attribute:
                    535:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_ref_name, NULL);
                    536:                 printf(" name=%s", str);
                    537:                 TtaFreeMemory(str);
                    538:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_type, NULL);
                    539:                 printf(" type=%s", str);
                    540:                 TtaFreeMemory(str);
                    541:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_useAt, NULL);
                    542:                 printf(" use=%s", str);
                    543:                 TtaFreeMemory(str);
                    544:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_defaultAt, NULL);
                    545:                 printf(" default=%s", str);
                    546:                 TtaFreeMemory(str);
                    547:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_fixed, NULL);
                    548:                 printf(" fixed=%s", str);
                    549:                 TtaFreeMemory(str);
                    550:                 break;
                    551:             }
                    552:         }
                    553:     }
1.36      vatton    554: #endif /* TEMPLATE_DEBUG */
1.19      kia       555: }
1.21      kia       556: 
                    557: /*----------------------------------------------------------------------
1.25      kia       558:  * Dump subtree
                    559:   ----------------------------------------------------------------------*/
                    560: void DumpSubtree(Element el, Document doc, int off)
                    561: {
1.36      vatton    562: #ifdef TEMPLATE_DEBUG
1.25      kia       563:   Element child = TtaGetFirstChild(el);
                    564:   int i;
1.30      kia       565: 
1.37    ! vatton    566:   for (i=0; i<off; i++)
1.25      kia       567:     printf("  ");
                    568:   DumpTemplateElement(el, doc);
                    569:   printf("\n");
                    570: 
1.37    ! vatton    571:   while (child)
1.25      kia       572:     {
                    573:       DumpSubtree(child, doc, off+1);
                    574:       TtaNextSibling(&child);
                    575:     }
1.36      vatton    576: #endif /* TEMPLATE_DEBUG */
1.25      kia       577: }
                    578: 
                    579: /*----------------------------------------------------------------------
1.34      vatton    580:   Save an opened document to a specified path in order to open.
                    581:   The parameter doc is the original doc to be saved
                    582:   The parameter newdoc is the new document
                    583:   The parameter newpath is the newdoc URI
                    584:   Return the saved localFile (to be freed) or NULL
1.21      kia       585:   ----------------------------------------------------------------------*/
1.34      vatton    586: char *SaveDocumentToNewDoc(Document doc, Document newdoc, char* newpath)
1.21      kia       587: {
                    588:   ElementType   elType;
                    589:   Element       root;
1.34      vatton    590:   char         *localFile = NULL, *s;
1.21      kia       591:   ThotBool      res = FALSE;
1.30      kia       592: 
1.21      kia       593:   localFile = GetLocalPath (newdoc, newpath);
1.22      kia       594:   // update all links
1.24      kia       595:   SetRelativeURLs (doc, newpath, NULL, FALSE, FALSE, FALSE);
1.22      kia       596:   // prepare the new document view
                    597:   TtaExtractName (newpath, DirectoryName, DocumentName);
1.21      kia       598: 
1.22      kia       599:   root = TtaGetRootElement(doc);
                    600:   elType = TtaGetElementType (root);
                    601:   // get the target document type
                    602:   s = TtaGetSSchemaName (elType.ElSSchema);
                    603:   if (strcmp (s, "HTML") == 0)
                    604:     {
                    605:       /* docType = docHTML; */
                    606:       if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
                    607:           TtaGetDocumentProfile(doc) == L_Basic)
                    608:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11", FALSE);
1.21      kia       609:       else
1.22      kia       610:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX", FALSE);
                    611:     }
                    612:   else if (strcmp (s, "SVG") == 0)
                    613:     /* docType = docSVG; */
                    614:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT", FALSE);
                    615:   else if (strcmp (s, "MathML") == 0)
                    616:     /* docType = docMath; */
                    617:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT", FALSE);
                    618:   else
                    619:     /* docType = docXml; */
                    620:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL, FALSE);
1.34      vatton    621:   if (res)
                    622:     return localFile;
                    623:   else
                    624:     {
                    625:       TtaFreeMemory (localFile);
                    626:       return NULL;
                    627:     }
1.21      kia       628: }
1.29      kia       629: 
                    630: /*----------------------------------------------------------------------
                    631:  * Retrieve the xt:head element.
                    632:   ----------------------------------------------------------------------*/
1.35      vatton    633: Element TemplateFindHead (Document doc)
1.29      kia       634: {
                    635: #ifdef TEMPLATES
1.35      vatton    636:   ElementType headType, elType;
                    637:   Element     head, root;
                    638: 
1.29      kia       639:   headType.ElSSchema = TtaGetSSchema ("Template", doc);
1.35      vatton    640:   if (headType.ElSSchema == NULL)
                    641:     return NULL;
                    642: 
1.29      kia       643:   headType.ElTypeNum = Template_EL_head;
1.35      vatton    644:   root = TtaGetMainRoot (doc);
                    645:   head = TtaSearchTypedElement (headType, SearchInTree, root);
                    646:   if (head == NULL)
                    647:     {
                    648:       // create the template head
                    649:       head = TtaNewElement (doc, headType);
                    650:       elType = TtaGetElementType (root);
                    651:       if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
                    652:         {
                    653:           elType.ElTypeNum = HTML_EL_HEAD;
                    654:           root = TtaSearchTypedElement (elType, SearchInTree, root);
                    655:         }
                    656:       TtaInsertFirstChild (&head, root, doc);
                    657:       SetAttributeStringValue (head, Template_ATTR_version, Template_Current_Version);
                    658:       SetAttributeStringValue (head, Template_ATTR_templateVersion, "1.0");      
                    659:     }
                    660:   return head;
1.29      kia       661: #else /* TEMPLATES */
                    662:   return NULL;
                    663: #endif /* TEMPLATES */
                    664: }
                    665: 
                    666: 

Webmaster