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

1.9       vatton      1: /*
                      2:  *
1.18      vatton      3:  *  COPYRIGHT INRIA and W3C, 2006-2008
1.9       vatton      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7: 
1.3       quint       8: #include "templates.h"
1.14      kia         9: #include "Templatename.h"
                     10: #include "templates_f.h"
1.1       francesc   11: 
1.21      kia        12: #include "AHTURLTools_f.h"
                     13: #include "HTMLsave_f.h"
                     14: 
                     15: 
1.19      kia        16: #include <stdarg.h>
                     17: 
1.1       francesc   18: /*----------------------------------------------------------------------
                     19: GetSchemaFromDocType: Returns the name of the schema corresponding to 
                     20: a doc type.
                     21: ----------------------------------------------------------------------*/
                     22: char *GetSchemaFromDocType (DocumentType docType)
                     23: {
                     24: #ifdef TEMPLATES
                     25:        switch (docType)
                     26:     {
                     27:     case docAnnot :
                     28:                return "Annot";
                     29:     case docBookmark :
                     30:                return "Topics";
                     31:     case docSVG :
                     32:                return "SVG";
                     33:     case docMath :
                     34:                return "MathML";
                     35:     case docXml :
                     36:                return "XML";
                     37:     default :
                     38:                return "HTML";
                     39:     }
                     40: #endif // TEMPLATES
                     41:        return "HTML";
                     42: }
                     43: 
1.5       kia        44: /*----------------------------------------------------------------------
                     45: Set the value of a string attribute 
                     46: ----------------------------------------------------------------------*/
                     47: void SetAttributeStringValue (Element el, int att, char* value)
                     48: {
                     49: #ifdef TEMPLATES
1.7       vatton     50:   Document      doc = TtaGetDocument(el);
                     51:   AttributeType attType;
                     52:   Attribute     attribute;
1.5       kia        53: 
1.10      kia        54:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
1.9       vatton     55:     return;
1.5       kia        56:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     57:   attType.AttrTypeNum = att;
1.7       vatton     58:   attribute = TtaGetAttribute(el, attType);
                     59:   if (attribute == NULL)
                     60:     {
                     61:       attribute = TtaNewAttribute (attType);
                     62:       TtaAttachAttribute(el, attribute, doc);
                     63:     }
1.5       kia        64:   TtaSetAttributeText(attribute, value, el, doc);
                     65: #endif /* TEMPLATES */
                     66: }
                     67: 
1.11      kia        68: /*----------------------------------------------------------------------
                     69: Set the value of a string attribute and registering it in undo sequence.
                     70: ----------------------------------------------------------------------*/
                     71: void SetAttributeStringValueWithUndo (Element el, int att, char* value)
                     72: {
                     73: #ifdef TEMPLATES
                     74:   Document      doc = TtaGetDocument(el);
                     75:   AttributeType attType;
                     76:   Attribute     attribute;
                     77: 
                     78:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
                     79:     return;
                     80:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     81:   attType.AttrTypeNum = att;
                     82:   attribute = TtaGetAttribute(el, attType);
                     83:   if (attribute == NULL)
                     84:     {
                     85:       attribute = TtaNewAttribute (attType);
                     86:       TtaAttachAttribute(el, attribute, doc);
                     87:       TtaRegisterAttributeCreate(attribute, el, doc);
                     88:     }
                     89:   TtaSetAttributeText(attribute, value, el, doc);
                     90:   TtaRegisterAttributeReplace(attribute, el, doc);
                     91: #endif /* TEMPLATES */
                     92: }
                     93: 
1.13      kia        94: /*----------------------------------------------------------------------
                     95: Returns the value of a string attribute without copy it 
                     96: ----------------------------------------------------------------------*/
                     97: void GiveAttributeStringValueFromNum (Element el, int att, char* buff, int* sz)
                     98: {
                     99: #ifdef TEMPLATES
                    100:   AttributeType attType;
                    101:   Attribute     attribute;
                    102:   int           size;
                    103: 
                    104:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    105:   attType.AttrTypeNum = att;
                    106:   attribute = TtaGetAttribute(el, attType);
                    107:   
                    108:   size = TtaGetTextAttributeLength(attribute);
                    109:   TtaGiveTextAttributeValue (attribute, buff, &size);
                    110:   buff[size] = EOS;
                    111:   if(sz)
                    112:     *sz = size;
                    113: #endif /* TEMPLATES */
                    114: }
                    115: 
                    116: 
1.1       francesc  117: 
                    118: /*----------------------------------------------------------------------
                    119: Returns the value of a string attribute 
                    120: ----------------------------------------------------------------------*/
1.8       kia       121: char *GetAttributeStringValueFromNum (Element el, int att, int* sz)
1.1       francesc  122: {
                    123: #ifdef TEMPLATES
                    124:        AttributeType attType;
1.12      vatton    125:   Attribute     attribute;
                    126:   char         *aux;
                    127:   int           size;
                    128: 
1.1       francesc  129:        attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    130:        attType.AttrTypeNum = att;
1.12      vatton    131:        attribute = TtaGetAttribute(el, attType);
1.1       francesc  132:        
1.12      vatton    133:        size = TtaGetTextAttributeLength(attribute);
                    134:        aux = (char*) TtaGetMemory(size+1);
1.1       francesc  135:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    136:   aux[size] = EOS;
1.4       kia       137:   if(sz)
                    138:     *sz = size;
1.1       francesc  139:        return aux;
                    140: #else
1.8       kia       141:        return NULL;
1.1       francesc  142: #endif /* TEMPLATES */
                    143: }
1.2       francesc  144: 
                    145: /*----------------------------------------------------------------------
                    146: Returns the value of a string attribute 
                    147: ----------------------------------------------------------------------*/
1.4       kia       148: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz)
1.2       francesc  149: {
                    150: #ifdef TEMPLATES
                    151:        int size = TtaGetTextAttributeLength(attribute);
                    152:        char *aux = (char*) TtaGetMemory(size+1);
1.12      vatton    153: 
1.2       francesc  154:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    155:   aux[size] = EOS;
1.4       kia       156:   if(sz)
                    157:     *sz = size;
1.2       francesc  158:        return aux;
                    159: #else
1.8       kia       160:        return NULL;
1.2       francesc  161: #endif /* TEMPLATES */
                    162: }
1.6       kia       163: 
                    164: 
                    165: /*----------------------------------------------------------------------
                    166: GetFirstEditableElement
                    167: Returns the first descendant element which is modifiable.
                    168: ----------------------------------------------------------------------*/
                    169: Element GetFirstEditableElement (Element el)
                    170: {
                    171:   Element res = NULL;
                    172:   Element current = TtaGetFirstChild(el);
                    173:   
                    174:   while(!res && current)
                    175:   {
                    176:     res = GetFirstEditableElement(current);
                    177:     TtaNextSibling(&current);
                    178:   }
                    179:   
                    180:   if(!res && !TtaIsReadOnly(el))
                    181:     res = el;
                    182:   
                    183:   return res;
                    184: }
1.14      kia       185: 
                    186: /*----------------------------------------------------------------------
1.15      kia       187:   TemplateCanInsertFirstChild
                    188:   Test if an element can be inserted as child of another, bypassing xt.
                    189: ----------------------------------------------------------------------*/
                    190: ThotBool TemplateCanInsertFirstChild(ElementType elementType, Element parent, Document document)
                    191: {
                    192: #ifdef TEMPLATES
                    193:   SSchema         templateSSchema = TtaGetSSchema ("Template", document);
                    194:   ElementType     parType;
                    195:   
                    196:   while(parent)
                    197:     {
                    198:       parType = TtaGetElementType(parent);
                    199:       if(parType.ElSSchema != templateSSchema)
                    200:         break;
                    201:       parent = TtaGetParent(parent);
                    202:     }
                    203:   if(!parent)
                    204:     return FALSE;
                    205: #endif /* TEMPLATES */
                    206:   return TtaCanInsertFirstChild(elementType, parent, document);
                    207: }
                    208: 
                    209: /*----------------------------------------------------------------------
1.14      kia       210:   ValidateTemplateAttrInMenu
                    211:   Validate the status of an attribute according to xt::atribute rules.
                    212:   ----------------------------------------------------------------------*/
                    213: ThotBool ValidateTemplateAttrInMenu (NotifyAttribute * event)
                    214: {
                    215: #ifdef TEMPLATES
                    216:   Element       elem;
                    217:   Element       parent;
                    218:   ElementType   elType;
                    219:   AttributeType attrType;
                    220:   Attribute     attr;
                    221:   char*         attrName;
                    222:   char          buffer[MAX_LENGTH];
                    223:   int           sz;
                    224:   int           useAt, type;
                    225:   
                    226:   /* Prevent from showing attributes for template instance but not templates. */
                    227:   if(IsTemplateInstanceDocument(event->document))
                    228:     {
                    229:       /* Prevent if attribute's element is not a descendant of xt:use */
1.17      kia       230:       /* Dont prevent if descendant of xt:bag. */
1.14      kia       231:       parent = event->element;
                    232:       elem = GetFirstTemplateParentElement(parent);
                    233:       if(!elem)
                    234:         return TRUE;
                    235:       elType     = TtaGetElementType(elem);
1.17      kia       236:       if(elType.ElTypeNum==Template_EL_bag)
                    237:         return FALSE;
1.14      kia       238:       if(elType.ElTypeNum!=Template_EL_useSimple)
                    239:         return TRUE;
                    240: 
                    241:       /* Search for the corresponding xt:attribute element*/
                    242:       attrName = TtaGetAttributeName(event->attributeType);
                    243:       attrType.AttrSSchema = TtaGetSSchema ("Template", event->document);
                    244:       for(elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem))
                    245:         {
                    246:           attrType.AttrTypeNum = Template_ATTR_ref_name;
                    247:           elType = TtaGetElementType(elem);
                    248:           if(elType.ElTypeNum==Template_EL_attribute &&
                    249:                   elType.ElSSchema==TtaGetSSchema ("Template", event->document))
                    250:             {
                    251:                attr = TtaGetAttribute(elem, attrType);
                    252:                if(attr)
                    253:                  {
                    254:                    sz = MAX_LENGTH;
                    255:                    TtaGiveTextAttributeValue(attr, buffer, &sz);
                    256:                    if(!strcmp(buffer, attrName))
                    257:                      {
                    258:                        /* Process the attribute filtering */
                    259:                        /* Get 'useAt' attr value. */
                    260:                        attrType.AttrTypeNum = Template_ATTR_useAt;
                    261:                        attr = TtaGetAttribute(elem, attrType);
                    262:                        if(attr)
                    263:                          useAt = TtaGetAttributeValue(attr);
                    264:                        else
                    265:                          useAt = Template_ATTR_useAt_VAL_required;
                    266:                        /* Get 'type' attr value. */                       
                    267:                        attrType.AttrTypeNum = Template_ATTR_type;
                    268:                        attr = TtaGetAttribute(elem, attrType);
                    269:                        if(attr)
                    270:                          type = TtaGetAttributeValue(attr);
                    271:                        else
                    272:                          type = Template_ATTR_type_VAL_string;
1.16      kia       273: #ifdef AMAYA_DEBUG
                    274: #ifdef EK
1.14      kia       275: /******************************************************************************/
                    276:                        printf("Attribute : %s \n", attrName);
                    277:                        switch(useAt)
                    278:                        {
                    279:                          case Template_ATTR_useAt_VAL_required:
                    280:                            printf("    required");
                    281:                            break;
                    282:                          case Template_ATTR_useAt_VAL_optional:
                    283:                            printf("    optional");
                    284:                            break;
                    285:                          case Template_ATTR_useAt_VAL_prohibited:
                    286:                            printf("    prohibited");
                    287:                            break;
                    288:                          default:
                    289:                            printf("    error");
                    290:                            break;
                    291:                        }
                    292:                        switch(type)
                    293:                        {
                    294:                          case Template_ATTR_type_VAL_string:
                    295:                            printf(" string\n");
                    296:                            break;
                    297:                          case Template_ATTR_type_VAL_number:
                    298:                            printf(" number\n");
                    299:                            break;
                    300:                          case Template_ATTR_type_VAL_listVal:
                    301:                            printf(" list\n");
                    302:                            break;
                    303:                          default:
                    304:                            printf(" error\n");
                    305:                            break;
                    306:                        }
                    307: /******************************************************************************/
1.16      kia       308: #endif  /* EK */
                    309: #endif /* AMAYA_DEBUG */
1.14      kia       310:                        event->restr.RestrType = (RestrictionContentType)type;
                    311:                        /* If attr is prohibited, dont show it.*/
                    312:                        if(useAt==Template_ATTR_useAt_VAL_prohibited)
1.16      kia       313:                            return TRUE;
1.14      kia       314:                        if(useAt==Template_ATTR_useAt_VAL_required)
                    315:                          {
                    316:                            /* Force the usage of this attribute.*/
                    317:                            event->restr.RestrFlags |= attr_mandatory;
                    318:                          }
                    319: 
                    320:                        /* Get 'fixed' attr value. */
                    321:                        attrType.AttrTypeNum = Template_ATTR_fixed;
                    322:                        attr = TtaGetAttribute(elem, attrType);
                    323:                        if(attr)
                    324:                          {
                    325:                            sz = MAX_LENGTH;
                    326:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    327:                            event->restr.RestrFlags |= attr_readonly;
                    328:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    329:                            return FALSE;
                    330:                          }
                    331: 
                    332:                        /* Get 'default' attr value.*/
                    333:                        attrType.AttrTypeNum = Template_ATTR_defaultAt;
                    334:                        attr = TtaGetAttribute(elem, attrType);
                    335:                        if(attr)
                    336:                          {
                    337:                            sz = MAX_LENGTH;
                    338:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    339:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    340:                          }
                    341: 
                    342:                        /* Get 'values' attr value.*/
                    343:                        attrType.AttrTypeNum = Template_ATTR_values;
                    344:                        attr = TtaGetAttribute(elem, attrType);
                    345:                        if(attr)
                    346:                          {
                    347:                            sz = MAX_LENGTH;
                    348:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    349:                            event->restr.RestrEnumVal = TtaStrdup(buffer);
                    350:                            event->restr.RestrFlags |= attr_enum;
                    351:                          }
                    352:                        return FALSE;
                    353:                      }
                    354:                  }
                    355:             }
                    356:         }
                    357:       
1.16      kia       358:       return TRUE;
1.14      kia       359:     }
                    360:   else
                    361: #endif /* TEMPLATES */
                    362:     return FALSE;
                    363: }
1.19      kia       364: 
                    365: /*----------------------------------------------------------------------
1.20      kia       366:  * Dump element path
                    367:   ----------------------------------------------------------------------*/
                    368: void DumpElementSubPath(Element el, char* buffer)
                    369: {
                    370:   Element parent = TtaGetParent(el);
                    371:   if(parent==NULL)
                    372:     strcpy(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    373:   else
                    374:     {
                    375:       DumpElementSubPath(parent, buffer);
                    376:       strcat(buffer, "/");
                    377:       strcat(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
                    378:     }
                    379: }
                    380: 
                    381: /*----------------------------------------------------------------------
                    382:  * Dump element path
                    383:   ----------------------------------------------------------------------*/
                    384: void DumpElementPath(Element el)
                    385: {
                    386:   char buffer[MAX_LENGTH];
                    387:   DumpElementSubPath(el, buffer);
                    388:   printf("%s\n", buffer);
                    389: }
                    390: 
                    391: 
                    392: /*----------------------------------------------------------------------
1.19      kia       393:  * Dump template element
                    394:   ----------------------------------------------------------------------*/
                    395: void DumpTemplateElement(Element el, Document doc)
                    396: {
                    397:   ElementType elType;
                    398:   SSchema     schema = TtaGetSSchema ("Template", doc);
                    399:   char*       str;
                    400:   if(el && doc)
                    401:     {
                    402:       elType = TtaGetElementType(el);
                    403:       printf("%s", TtaGetElementTypeName(elType));
                    404:       if(elType.ElSSchema==schema)
                    405:         {
                    406:           switch(elType.ElTypeNum)
                    407:             {
                    408:               case Template_EL_head:
                    409:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_version, NULL);
                    410:                 printf(" version=%s", str);
                    411:                 TtaFreeMemory(str);
                    412:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_templateVersion, NULL);
                    413:                 printf(" templateVersion=%s", str);
                    414:                 TtaFreeMemory(str);                
                    415:                 break;
                    416:               case Template_EL_component:
                    417:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    418:                 printf(" name=%s", str);
                    419:                 TtaFreeMemory(str);
                    420:                 break;
                    421:               case Template_EL_union:
                    422:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
                    423:                 printf(" name=%s", str);
                    424:                 TtaFreeMemory(str);
                    425:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_includeAt, NULL);
                    426:                 printf(" include=%s", str);
                    427:                 TtaFreeMemory(str);
                    428:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_exclude, NULL);
                    429:                 printf(" exclude=%s", str);
                    430:                 TtaFreeMemory(str);
                    431:                 break;
                    432:               case Template_EL_import:
                    433:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_src, NULL);
                    434:                 printf(" src=%s", str);
                    435:                 TtaFreeMemory(str);
                    436:                 break;
                    437:               case Template_EL_repeat:
                    438:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    439:                 printf(" label=%s", str);
                    440:                 TtaFreeMemory(str);
                    441:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_minOccurs, NULL);
                    442:                 printf(" minOccurs=%s", str);
                    443:                 TtaFreeMemory(str);
                    444:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
                    445:                 printf(" maxOccurs=%s", str);
                    446:                 TtaFreeMemory(str);
                    447:                 break;
                    448:               case Template_EL_option:
                    449:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    450:                 printf(" label=%s", str);
                    451:                 TtaFreeMemory(str);
                    452:                 break;
                    453:               case Template_EL_useSimple:
                    454:               case Template_EL_useEl:
                    455:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    456:                 printf(" label=%s", str);
                    457:                 TtaFreeMemory(str);
                    458:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    459:                 printf(" types=%s", str);
                    460:                 TtaFreeMemory(str);
                    461:                 break;
                    462:               case Template_EL_bag:
                    463:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
                    464:                 printf(" label=%s", str);
                    465:                 TtaFreeMemory(str);
                    466:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
                    467:                 printf(" types=%s", str);
                    468:                 TtaFreeMemory(str);
                    469:                 break;
                    470:               case Template_EL_attribute:
                    471:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_ref_name, NULL);
                    472:                 printf(" name=%s", str);
                    473:                 TtaFreeMemory(str);
                    474:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_type, NULL);
                    475:                 printf(" type=%s", str);
                    476:                 TtaFreeMemory(str);
                    477:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_useAt, NULL);
                    478:                 printf(" use=%s", str);
                    479:                 TtaFreeMemory(str);
                    480:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_defaultAt, NULL);
                    481:                 printf(" default=%s", str);
                    482:                 TtaFreeMemory(str);
                    483:                 str = GetAttributeStringValueFromNum(el, Template_ATTR_fixed, NULL);
                    484:                 printf(" fixed=%s", str);
                    485:                 TtaFreeMemory(str);
                    486:                 break;
                    487:             }
                    488:         }
                    489:     }
                    490: }
1.21      kia       491: 
                    492: /*----------------------------------------------------------------------
                    493:  * Save an opened document to a specified path in order to open.
1.23    ! vatton    494:  * param doc Original doc to save
        !           495:  * param newdoc Document where reopen it
        !           496:  * param newpath URI where save the doc
1.21      kia       497:   ----------------------------------------------------------------------*/
1.23    ! vatton    498: ThotBool SaveDocumentToNewDoc(Document doc, Document newdoc, char* newpath)
1.21      kia       499: {
                    500:   ElementType   elType;
                    501:   Element       root;
                    502:   char         *localFile, *s;
                    503:   ThotBool      res = FALSE;
                    504:   
                    505:   localFile = GetLocalPath (newdoc, newpath);
1.22      kia       506:   // update all links
                    507:   SetRelativeURLs (doc, newpath, "", FALSE, FALSE, FALSE);
                    508:   // prepare the new document view
                    509:   TtaExtractName (newpath, DirectoryName, DocumentName);
1.21      kia       510: 
1.22      kia       511:   root = TtaGetRootElement(doc);
                    512:   elType = TtaGetElementType (root);
                    513:   // get the target document type
                    514:   s = TtaGetSSchemaName (elType.ElSSchema);
                    515:   if (strcmp (s, "HTML") == 0)
                    516:     {
                    517:       /* docType = docHTML; */
                    518:       if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
                    519:           TtaGetDocumentProfile(doc) == L_Basic)
                    520:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11", FALSE);
1.21      kia       521:       else
1.22      kia       522:         res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX", FALSE);
                    523:     }
                    524:   else if (strcmp (s, "SVG") == 0)
                    525:     /* docType = docSVG; */
                    526:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT", FALSE);
                    527:   else if (strcmp (s, "MathML") == 0)
                    528:     /* docType = docMath; */
                    529:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT", FALSE);
                    530:   else
                    531:     /* docType = docXml; */
                    532:     res = TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL, FALSE);
1.21      kia       533:   return res;
                    534: }

Webmaster