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

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;
                    198:        size = TtaGetTextAttributeLength (attribute);
                    199:        aux = (char*) TtaGetMemory (size+1);
1.1       francesc  200:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    201:   aux[size] = EOS;
1.31      vatton    202:   if (sz)
1.4       kia       203:     *sz = size;
1.1       francesc  204:        return aux;
                    205: #else
1.8       kia       206:        return NULL;
1.1       francesc  207: #endif /* TEMPLATES */
                    208: }
1.2       francesc  209: 
                    210: /*----------------------------------------------------------------------
1.30      kia       211: Returns the value of an int attribute
                    212: ----------------------------------------------------------------------*/
                    213: int GetAttributeIntValueFromNum (Element el, int att)
                    214: {
                    215: #ifdef TEMPLATES
                    216:   AttributeType attType;
                    217:   Attribute     attribute;
                    218: 
1.45    ! vatton    219:   attType.AttrSSchema = TtaGetElementType (el).ElSSchema;
1.30      kia       220:   attType.AttrTypeNum = att;
1.45    ! vatton    221:   attribute = TtaGetAttribute (el, attType);
        !           222:   if (attribute)
        !           223:     return TtaGetAttributeValue (attribute);
        !           224:   else
        !           225:     return 0;
1.30      kia       226: #else
1.45    ! vatton    227:   return 0;
1.30      kia       228: #endif /* TEMPLATES */
                    229: }
                    230: 
                    231: /*----------------------------------------------------------------------
                    232: Set the value of a int attribute and registering it in undo sequence if wanted
                    233: ----------------------------------------------------------------------*/
                    234: void SetAttributeIntValue (Element el, int att, int value, ThotBool undo)
                    235: {
                    236: #ifdef TEMPLATES
                    237:   Document      doc = TtaGetDocument(el);
                    238:   AttributeType attType;
                    239:   Attribute     attribute;
                    240: 
                    241:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
                    242:     return;
                    243:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    244:   attType.AttrTypeNum = att;
                    245:   attribute = TtaGetAttribute(el, attType);
                    246:   if (attribute == NULL)
                    247:     {
                    248:       attribute = TtaNewAttribute (attType);
                    249:       TtaAttachAttribute(el, attribute, doc);
1.37      vatton    250:       if (undo)
1.30      kia       251:         TtaRegisterAttributeCreate(attribute, el, doc);
                    252:     }
                    253:   TtaSetAttributeValue(attribute, value, el, doc);
1.37      vatton    254:   if (undo)
1.30      kia       255:     TtaRegisterAttributeReplace(attribute, el, doc);
                    256: #endif /* TEMPLATES */
                    257: }
                    258: 
                    259: /*----------------------------------------------------------------------
                    260: Returns the value of a string attribute
1.2       francesc  261: ----------------------------------------------------------------------*/
1.4       kia       262: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz)
1.2       francesc  263: {
                    264: #ifdef TEMPLATES
                    265:        int size = TtaGetTextAttributeLength(attribute);
                    266:        char *aux = (char*) TtaGetMemory(size+1);
1.12      vatton    267: 
1.2       francesc  268:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    269:   aux[size] = EOS;
1.37      vatton    270:   if (sz)
1.4       kia       271:     *sz = size;
1.2       francesc  272:        return aux;
                    273: #else
1.8       kia       274:        return NULL;
1.2       francesc  275: #endif /* TEMPLATES */
                    276: }
1.6       kia       277: 
1.31      vatton    278: /*----------------------------------------------------------------------
                    279:   GetAncestorComponentName returns the name of the ancestor component
                    280:   or NULL;
                    281:   ----------------------------------------------------------------------*/
                    282: char *GetAncestorComponentName (Element *el)
                    283: {
                    284: #ifdef TEMPLATES
                    285:   ElementType   elType;
                    286:   Element       anc = NULL;
                    287:   char         *name;
                    288: 
                    289:   elType = TtaGetElementType (*el);
                    290:   elType.ElTypeNum = Template_EL_component;
                    291:   anc = TtaGetParent (*el);
                    292:   anc = TtaGetExactTypedAncestor (anc, elType);
                    293:   if (anc)
                    294:     {
1.32      kia       295:       name = GetAttributeStringValueFromNum (anc, Template_ATTR_name, NULL);
1.31      vatton    296:       *el = anc;
                    297:       return name;
                    298:     }
1.32      kia       299: #endif /* TEMPLATES */
1.31      vatton    300:   return NULL;
                    301: }
                    302: 
1.45    ! vatton    303: /*----------------------------------------------------------------------
        !           304:   AllowAttributeEdit returns TRUE if the template allows the user to edit
        !           305:   the attribute
        !           306:   ----------------------------------------------------------------------*/
        !           307: ThotBool AllowAttributeEdit (Element el, Document doc, char *name)
        !           308: {
        !           309: #ifdef TEMPLATES
        !           310:   Element      child;
        !           311:   ElementType  childType;
        !           312:   char         *value;
        !           313:   int           val;
        !           314:   ThotBool      ok;
        !           315: 
        !           316:   if (name == NULL || *name == EOS)
        !           317:     return FALSE;
        !           318:   child = TtaGetFirstChild (el);
        !           319:   while (child)
        !           320:     {
        !           321:       childType = TtaGetElementType (child);
        !           322:       if (childType.ElTypeNum == Template_EL_attribute &&
        !           323:           !strcmp (TtaGetSSchemaName(childType.ElSSchema) , "Template"))
        !           324:         {
        !           325:           value = GetAttributeStringValueFromNum (child, Template_ATTR_ref_name, NULL);
        !           326:           ok = (value && !strcmp (value, name));
        !           327:           TtaFreeMemory (value);
        !           328:           if (ok)
        !           329:             {
        !           330:               val = GetAttributeIntValueFromNum (el, Template_ATTR_useAt);
        !           331:               if (val == Template_ATTR_useAt_VAL_prohibited)
        !           332:                 return FALSE;
        !           333:               value = GetAttributeStringValueFromNum (child, Template_ATTR_fixed, NULL);
        !           334:               if (value)
        !           335:                 {
        !           336:                   TtaFreeMemory (value);
        !           337:                   return FALSE;
        !           338:                 }
        !           339:               return ok;
        !           340:             }
        !           341:           else
        !           342:             TtaNextSibling(&child);
        !           343:         }
        !           344:       else
        !           345:         return FALSE;
        !           346:     }
        !           347: #endif /* TEMPLATES */
        !           348:   return FALSE;
        !           349: }
1.6       kia       350: 
                    351: /*----------------------------------------------------------------------
                    352: GetFirstEditableElement
                    353: Returns the first descendant element which is modifiable.
                    354: ----------------------------------------------------------------------*/
                    355: Element GetFirstEditableElement (Element el)
                    356: {
1.38      vatton    357:   ElementType     elType;
                    358:   Element         res = NULL, current;
1.30      kia       359: 
1.38      vatton    360:   current = TtaGetFirstChild(el);
1.37      vatton    361:   while (!res && current)
1.38      vatton    362:     {
                    363:       // skip col colgroup 
                    364:       elType = TtaGetElementType (current);
                    365:       if ((elType.ElSSchema &&
                    366:            strcmp (TtaGetSSchemaName(elType.ElSSchema), "HTML")) ||
                    367:           (elType.ElTypeNum != HTML_EL_ColStruct &&
                    368:            elType.ElTypeNum != HTML_EL_Table_head &&
                    369:            elType.ElTypeNum != HTML_EL_Comment_))
                    370:         res = GetFirstEditableElement (current);
                    371:       TtaNextSibling(&current);
                    372:     }
1.37      vatton    373:   if (!res && !TtaIsReadOnly(el))
1.6       kia       374:     res = el;
                    375:   return res;
                    376: }
1.14      kia       377: 
                    378: /*----------------------------------------------------------------------
1.15      kia       379:   TemplateCanInsertFirstChild
                    380:   Test if an element can be inserted as child of another, bypassing xt.
                    381: ----------------------------------------------------------------------*/
1.37      vatton    382: ThotBool TemplateCanInsertFirstChild (ElementType elementType, Element parent,
                    383:                                       Document document)
1.15      kia       384: {
                    385: #ifdef TEMPLATES
                    386:   SSchema         templateSSchema = TtaGetSSchema ("Template", document);
                    387:   ElementType     parType;
1.30      kia       388: 
1.37      vatton    389:   while (parent)
1.15      kia       390:     {
                    391:       parType = TtaGetElementType(parent);
1.37      vatton    392:       if (parType.ElSSchema != templateSSchema)
1.15      kia       393:         break;
                    394:       parent = TtaGetParent(parent);
                    395:     }
1.37      vatton    396:   if (!parent)
1.15      kia       397:     return FALSE;
                    398: #endif /* TEMPLATES */
                    399:   return TtaCanInsertFirstChild(elementType, parent, document);
                    400: }
                    401: 
                    402: /*----------------------------------------------------------------------
1.37      vatton    403:   CheckTemplateAttrInMenu
1.14      kia       404:   Validate the status of an attribute according to xt::atribute rules.
1.37      vatton    405:        Return TRUE if the attribute is not valid
1.14      kia       406:   ----------------------------------------------------------------------*/
1.37      vatton    407: ThotBool CheckTemplateAttrInMenu (NotifyAttribute *event)
1.14      kia       408: {
                    409: #ifdef TEMPLATES
1.37      vatton    410:   Document      doc = event->document;
1.41      vatton    411:   Element       elem, parent = event->element;
1.14      kia       412:   ElementType   elType;
1.41      vatton    413:   SSchema       schema;
1.14      kia       414:   AttributeType attrType;
                    415:   Attribute     attr;
1.37      vatton    416:   char         *attrName;
1.14      kia       417:   char          buffer[MAX_LENGTH];
1.37      vatton    418:   int           sz, useAt, type;
1.30      kia       419: 
1.14      kia       420:   /* Prevent from showing attributes for template instance but not templates. */
1.37      vatton    421:   if (IsTemplateInstanceDocument(doc))
1.14      kia       422:     {
1.41      vatton    423:       schema = TtaGetSSchema ("Template", doc);
1.14      kia       424:       /* Prevent if attribute's element is not a descendant of xt:use */
1.17      kia       425:       /* Dont prevent if descendant of xt:bag. */
1.41      vatton    426:       elem = GetFirstTemplateParentElement (parent);
                    427:       if (elem)
                    428:         {
                    429:           elType = TtaGetElementType (elem);
                    430:           if (elType.ElTypeNum == Template_EL_bag)
                    431:             return FALSE;      /* let Thot perform normal operation */
                    432:           if (elType.ElTypeNum != Template_EL_useSimple)
                    433:             return TRUE;
                    434:           if (!TtaIsReadOnly (parent))
                    435:             return FALSE;      /* let Thot perform normal operation */
                    436:         }
1.14      kia       437:       /* Search for the corresponding xt:attribute element*/
1.41      vatton    438:       attrName = TtaGetAttributeName (event->attributeType);
                    439:       attrType.AttrSSchema = schema;
                    440:       for (elem = TtaGetFirstChild (parent); elem; TtaNextSibling (&elem))
1.14      kia       441:         {
                    442:           attrType.AttrTypeNum = Template_ATTR_ref_name;
                    443:           elType = TtaGetElementType(elem);
1.41      vatton    444:           if (elType.ElTypeNum == Template_EL_attribute && elType.ElSSchema == schema)
1.14      kia       445:             {
                    446:                attr = TtaGetAttribute(elem, attrType);
1.37      vatton    447:                if (attr)
1.14      kia       448:                  {
                    449:                    sz = MAX_LENGTH;
                    450:                    TtaGiveTextAttributeValue(attr, buffer, &sz);
1.37      vatton    451:                    if (!strcmp(buffer, attrName))
1.14      kia       452:                      {
                    453:                        /* Process the attribute filtering */
                    454:                        /* Get 'useAt' attr value. */
                    455:                        attrType.AttrTypeNum = Template_ATTR_useAt;
                    456:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    457:                        if (attr)
1.14      kia       458:                          useAt = TtaGetAttributeValue(attr);
                    459:                        else
                    460:                          useAt = Template_ATTR_useAt_VAL_required;
1.30      kia       461:                        /* Get 'type' attr value. */
1.14      kia       462:                        attrType.AttrTypeNum = Template_ATTR_type;
                    463:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    464:                        if (attr)
1.14      kia       465:                          type = TtaGetAttributeValue(attr);
                    466:                        else
                    467:                          type = Template_ATTR_type_VAL_string;
1.37      vatton    468: 
1.14      kia       469:                        event->restr.RestrType = (RestrictionContentType)type;
                    470:                        /* If attr is prohibited, dont show it.*/
1.37      vatton    471:                        if (useAt == Template_ATTR_useAt_VAL_prohibited)
1.16      kia       472:                            return TRUE;
1.37      vatton    473:                        if (useAt == Template_ATTR_useAt_VAL_required)
1.41      vatton    474:                          /* Force the usage of this attribute.*/
                    475:                          event->restr.RestrFlags |= attr_mandatory;
1.14      kia       476: 
                    477:                        /* Get 'fixed' attr value. */
                    478:                        attrType.AttrTypeNum = Template_ATTR_fixed;
                    479:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    480:                        if (attr)
1.14      kia       481:                          {
                    482:                            sz = MAX_LENGTH;
                    483:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    484:                            event->restr.RestrFlags |= attr_readonly;
                    485:                            event->restr.RestrDefVal = TtaStrdup(buffer);
1.37      vatton    486:                            return FALSE;       /* let Thot perform normal operation */
1.14      kia       487:                          }
                    488: 
                    489:                        /* Get 'default' attr value.*/
                    490:                        attrType.AttrTypeNum = Template_ATTR_defaultAt;
                    491:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    492:                        if (attr)
1.14      kia       493:                          {
                    494:                            sz = MAX_LENGTH;
                    495:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    496:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    497:                          }
                    498: 
                    499:                        /* Get 'values' attr value.*/
                    500:                        attrType.AttrTypeNum = Template_ATTR_values;
                    501:                        attr = TtaGetAttribute(elem, attrType);
1.37      vatton    502:                        if (attr)
1.14      kia       503:                          {
                    504:                            sz = MAX_LENGTH;
                    505:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    506:                            event->restr.RestrEnumVal = TtaStrdup(buffer);
                    507:                            event->restr.RestrFlags |= attr_enum;
                    508:                          }
1.37      vatton    509:                        return FALSE;   /* let Thot perform normal operation */
1.14      kia       510:                      }
                    511:                  }
                    512:             }
                    513:         }
1.16      kia       514:       return TRUE;
1.14      kia       515:     }
                    516: #endif /* TEMPLATES */
1.37      vatton    517:   return FALSE;
1.14      kia       518: }
1.19      kia       519: 
                    520: /*----------------------------------------------------------------------
1.20      kia       521:  * Dump element path
                    522:   ----------------------------------------------------------------------*/
1.37      vatton    523: void DumpElementSubPath (Element el, char* buffer)
1.20      kia       524: {
1.36      vatton    525: #ifdef TEMPLATE_DEBUG
1.20      kia       526:   Element parent = TtaGetParent(el);
1.37      vatton    527:   if (parent == NULL)
1.20      kia       528:     strcpy(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    529:   else
                    530:     {
                    531:       DumpElementSubPath(parent, buffer);
                    532:       strcat(buffer, "/");
                    533:       strcat(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    534:     }
1.36      vatton    535: #endif /* TEMPLATE_DEBUG */
1.20      kia       536: }
                    537: 
                    538: /*----------------------------------------------------------------------
                    539:  * Dump element path
                    540:   ----------------------------------------------------------------------*/
1.37      vatton    541: void DumpElementPath (Element el)
1.20      kia       542: {
1.36      vatton    543: #ifdef TEMPLATE_DEBUG
1.20      kia       544:   char buffer[MAX_LENGTH];
1.37      vatton    545: 
1.20      kia       546:   DumpElementSubPath(el, buffer);
                    547:   printf("%s\n", buffer);
1.36      vatton    548: #endif /* TEMPLATE_DEBUG */
1.20      kia       549: }
                    550: 
                    551: 
                    552: /*----------------------------------------------------------------------
1.19      kia       553:  * Dump template element
                    554:   ----------------------------------------------------------------------*/
1.37      vatton    555: void DumpTemplateElement (Element el, Document doc)
1.19      kia       556: {
1.36      vatton    557: #ifdef TEMPLATE_DEBUG
1.28      vatton    558:   ElementType    elType;
                    559:   AttributeType  attType;
                    560:   Attribute      att;
                    561:   SSchema        schema = TtaGetSSchema ("Template", doc);
                    562:   char*          str;
                    563:   char           buffer[MAX_LENGTH];
                    564:   int            len;
                    565:   Language       lang;
1.30      kia       566: 
1.37      vatton    567:   if (el && doc)
1.19      kia       568:     {
                    569:       elType = TtaGetElementType(el);
1.26      kia       570:       printf("- %p %d ", elType.ElSSchema, elType.ElTypeNum);
                    571:       printf(" %s", TtaGetSSchemaName(elType.ElSSchema));
                    572:       printf(":%s", TtaGetElementTypeName(elType));
1.37      vatton    573:       if (elType.ElTypeNum == 1)
1.26      kia       574:         {
                    575:           len = MAX_LENGTH-1;
                    576:           TtaGiveTextContent(el, (unsigned char*)buffer, &len, &lang);
                    577:           buffer[len] = EOS;
                    578:           printf(" \"%s\"", buffer);
                    579:         }
1.30      kia       580: 
1.37      vatton    581:       if (elType.ElSSchema == schema)
1.19      kia       582:         {
                    583:           switch(elType.ElTypeNum)
                    584:             {
                    585:               case Template_EL_head:
                    586:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_version, NULL);
                    587:                 printf(" version=%s", str);
                    588:                 TtaFreeMemory(str);
                    589:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_templateVersion, NULL);
                    590:                 printf(" templateVersion=%s", str);
1.30      kia       591:                 TtaFreeMemory(str);
1.19      kia       592:                 break;
                    593:               case Template_EL_component:
                    594:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    595:                 printf(" name=%s", str);
                    596:                 TtaFreeMemory(str);
                    597:                 break;
                    598:               case Template_EL_union:
                    599:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    600:                 printf(" name=%s", str);
                    601:                 TtaFreeMemory(str);
                    602:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_includeAt, NULL);
                    603:                 printf(" include=%s", str);
                    604:                 TtaFreeMemory(str);
                    605:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_exclude, NULL);
                    606:                 printf(" exclude=%s", str);
                    607:                 TtaFreeMemory(str);
                    608:                 break;
                    609:               case Template_EL_import:
                    610:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_src, NULL);
                    611:                 printf(" src=%s", str);
                    612:                 TtaFreeMemory(str);
                    613:                 break;
                    614:               case Template_EL_repeat:
                    615:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    616:                 printf(" label=%s", str);
                    617:                 TtaFreeMemory(str);
                    618:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_minOccurs, NULL);
                    619:                 printf(" minOccurs=%s", str);
                    620:                 TtaFreeMemory(str);
                    621:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
                    622:                 printf(" maxOccurs=%s", str);
                    623:                 TtaFreeMemory(str);
                    624:                 break;
                    625:               case Template_EL_useSimple:
                    626:               case Template_EL_useEl:
                    627:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
1.41      vatton    628:                 printf (" label=%s", str);
1.19      kia       629:                 TtaFreeMemory(str);
                    630:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.41      vatton    631:                 printf (" types=%s", str);
1.19      kia       632:                 TtaFreeMemory(str);
1.28      vatton    633:                 attType.AttrSSchema = elType.ElSSchema;
                    634:                 attType.AttrTypeNum = Template_ATTR_option;
                    635:                 att = TtaGetAttribute (el, attType);
                    636:                 if (att)
1.41      vatton    637:                   printf (" option");
1.19      kia       638:                 break;
                    639:               case Template_EL_bag:
                    640:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    641:                 printf(" label=%s", str);
                    642:                 TtaFreeMemory(str);
                    643:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    644:                 printf(" types=%s", str);
                    645:                 TtaFreeMemory(str);
                    646:                 break;
                    647:               case Template_EL_attribute:
                    648:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_ref_name, NULL);
1.41      vatton    649:                 printf (" name=%s", str);
1.19      kia       650:                 TtaFreeMemory(str);
                    651:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_type, NULL);
1.41      vatton    652:                 printf (" type=%s", str);
1.19      kia       653:                 TtaFreeMemory(str);
                    654:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_useAt, NULL);
1.41      vatton    655:                 printf (" use=%s", str);
1.19      kia       656:                 TtaFreeMemory(str);
                    657:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_defaultAt, NULL);
1.41      vatton    658:                 printf (" default=%s", str);
1.19      kia       659:                 TtaFreeMemory(str);
                    660:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_fixed, NULL);
1.41      vatton    661:                 printf (" fixed=%s", str);
1.19      kia       662:                 TtaFreeMemory(str);
                    663:                 break;
                    664:             }
                    665:         }
                    666:     }
1.36      vatton    667: #endif /* TEMPLATE_DEBUG */
1.19      kia       668: }
1.21      kia       669: 
                    670: /*----------------------------------------------------------------------
1.25      kia       671:  * Dump subtree
                    672:   ----------------------------------------------------------------------*/
                    673: void DumpSubtree(Element el, Document doc, int off)
                    674: {
1.36      vatton    675: #ifdef TEMPLATE_DEBUG
1.25      kia       676:   Element child = TtaGetFirstChild(el);
                    677:   int i;
1.30      kia       678: 
1.37      vatton    679:   for (i=0; i<off; i++)
1.25      kia       680:     printf("  ");
                    681:   DumpTemplateElement(el, doc);
                    682:   printf("\n");
                    683: 
1.37      vatton    684:   while (child)
1.25      kia       685:     {
                    686:       DumpSubtree(child, doc, off+1);
                    687:       TtaNextSibling(&child);
                    688:     }
1.36      vatton    689: #endif /* TEMPLATE_DEBUG */
1.25      kia       690: }
                    691: 
                    692: /*----------------------------------------------------------------------
1.34      vatton    693:   Save an opened document to a specified path in order to open.
                    694:   The parameter doc is the original doc to be saved
                    695:   The parameter newdoc is the new document
                    696:   The parameter newpath is the newdoc URI
                    697:   Return the saved localFile (to be freed) or NULL
1.21      kia       698:   ----------------------------------------------------------------------*/
1.34      vatton    699: char *SaveDocumentToNewDoc(Document doc, Document newdoc, char* newpath)
1.21      kia       700: {
                    701:   ElementType   elType;
                    702:   Element       root;
1.34      vatton    703:   char         *localFile = NULL, *s;
1.21      kia       704:   ThotBool      res = FALSE;
1.30      kia       705: 
1.21      kia       706:   localFile = GetLocalPath (newdoc, newpath);
1.40      vatton    707: 
                    708:   // apply link changes to the template
                    709:     TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.39      vatton    710:   SetRelativeURLs (doc, newpath, NULL, FALSE, FALSE, FALSE, FALSE);
1.22      kia       711:   // prepare the new document view
                    712:   TtaExtractName (newpath, DirectoryName, DocumentName);
1.21      kia       713: 
1.22      kia       714:   root = TtaGetRootElement(doc);
                    715:   elType = TtaGetElementType (root);
                    716:   // get the target document type
                    717:   s = TtaGetSSchemaName (elType.ElSSchema);
                    718:   if (strcmp (s, "HTML") == 0)
                    719:     {
                    720:       /* docType = docHTML; */
                    721:       if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
                    722:           TtaGetDocumentProfile(doc) == L_Basic)
                    723:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11", FALSE);
1.21      kia       724:       else
1.22      kia       725:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX", FALSE);
                    726:     }
                    727:   else if (strcmp (s, "SVG") == 0)
                    728:     /* docType = docSVG; */
                    729:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT", FALSE);
                    730:   else if (strcmp (s, "MathML") == 0)
                    731:     /* docType = docMath; */
                    732:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT", FALSE);
                    733:   else
                    734:     /* docType = docXml; */
                    735:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL, FALSE);
1.40      vatton    736: 
                    737:   // restore the previous state of the template
                    738:   TtaCloseUndoSequence (doc);
                    739:   TtaUndoNoRedo (doc);
1.34      vatton    740:   if (res)
                    741:     return localFile;
                    742:   else
                    743:     {
                    744:       TtaFreeMemory (localFile);
                    745:       return NULL;
                    746:     }
1.21      kia       747: }
1.29      kia       748: 
                    749: /*----------------------------------------------------------------------
1.43      vatton    750:   TemplateGetParentHead looks for the parent xt:head element
                    751:   ----------------------------------------------------------------------*/
                    752: Element TemplateGetParentHead (Element el, Document doc)
                    753: {
                    754: #ifdef TEMPLATES
                    755:   ElementType headType;
                    756:   SSchema     schema;
                    757: 
                    758:   schema = TtaGetSSchema ("Template", doc);
                    759:   if (schema == TtaGetDocumentSSchema (doc))
                    760:     return TtaGetMainRoot (doc);
                    761:   if (schema == NULL)
                    762:     // no template element in that document
                    763:     return NULL;
                    764: 
                    765:   headType.ElTypeNum = Template_EL_head;
                    766:   return TtaGetExactTypedAncestor (el, headType);
                    767: #endif /* TEMPLATES */
                    768: }
                    769: 
                    770: /*----------------------------------------------------------------------
                    771:   TemplateFindHead looks for the xt:head element and creates it
                    772:   if it doesn't exist.
1.29      kia       773:   ----------------------------------------------------------------------*/
1.35      vatton    774: Element TemplateFindHead (Document doc)
1.29      kia       775: {
                    776: #ifdef TEMPLATES
1.35      vatton    777:   ElementType headType, elType;
                    778:   Element     head, root;
                    779: 
1.29      kia       780:   headType.ElSSchema = TtaGetSSchema ("Template", doc);
1.35      vatton    781:   if (headType.ElSSchema == NULL)
                    782:     return NULL;
                    783: 
1.42      vatton    784:   root = TtaGetMainRoot (doc);
                    785:   elType = TtaGetElementType (root);
1.29      kia       786:   headType.ElTypeNum = Template_EL_head;
1.42      vatton    787:   if (elType.ElSSchema == headType.ElSSchema)
                    788:     head = root;
                    789:   else
                    790:     head = TtaSearchTypedElement (headType, SearchInTree, root);
1.35      vatton    791:   if (head == NULL)
                    792:     {
                    793:       // create the template head
                    794:       head = TtaNewElement (doc, headType);
                    795:       if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
                    796:         {
                    797:           elType.ElTypeNum = HTML_EL_HEAD;
                    798:           root = TtaSearchTypedElement (elType, SearchInTree, root);
                    799:         }
                    800:       TtaInsertFirstChild (&head, root, doc);
                    801:       SetAttributeStringValue (head, Template_ATTR_version, Template_Current_Version);
                    802:       SetAttributeStringValue (head, Template_ATTR_templateVersion, "1.0");      
                    803:     }
                    804:   return head;
1.29      kia       805: #else /* TEMPLATES */
                    806:   return NULL;
                    807: #endif /* TEMPLATES */
                    808: }
                    809: 
                    810: 

Webmaster