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

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

Webmaster