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

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: 
                     12: /*----------------------------------------------------------------------
                     13: GetSchemaFromDocType: Returns the name of the schema corresponding to 
                     14: a doc type.
                     15: ----------------------------------------------------------------------*/
                     16: char *GetSchemaFromDocType (DocumentType docType)
                     17: {
                     18: #ifdef TEMPLATES
                     19:        switch (docType)
                     20:     {
                     21:     case docAnnot :
                     22:                return "Annot";
                     23:     case docBookmark :
                     24:                return "Topics";
                     25:     case docSVG :
                     26:                return "SVG";
                     27:     case docMath :
                     28:                return "MathML";
                     29:     case docXml :
                     30:                return "XML";
                     31:     default :
                     32:                return "HTML";
                     33:     }
                     34: #endif // TEMPLATES
                     35:        return "HTML";
                     36: }
                     37: 
1.5       kia        38: /*----------------------------------------------------------------------
                     39: Set the value of a string attribute 
                     40: ----------------------------------------------------------------------*/
                     41: void SetAttributeStringValue (Element el, int att, char* value)
                     42: {
                     43: #ifdef TEMPLATES
1.7       vatton     44:   Document      doc = TtaGetDocument(el);
                     45:   AttributeType attType;
                     46:   Attribute     attribute;
1.5       kia        47: 
1.10      kia        48:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
1.9       vatton     49:     return;
1.5       kia        50:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     51:   attType.AttrTypeNum = att;
1.7       vatton     52:   attribute = TtaGetAttribute(el, attType);
                     53:   if (attribute == NULL)
                     54:     {
                     55:       attribute = TtaNewAttribute (attType);
                     56:       TtaAttachAttribute(el, attribute, doc);
                     57:     }
1.5       kia        58:   TtaSetAttributeText(attribute, value, el, doc);
                     59: #endif /* TEMPLATES */
                     60: }
                     61: 
1.11      kia        62: /*----------------------------------------------------------------------
                     63: Set the value of a string attribute and registering it in undo sequence.
                     64: ----------------------------------------------------------------------*/
                     65: void SetAttributeStringValueWithUndo (Element el, int att, char* value)
                     66: {
                     67: #ifdef TEMPLATES
                     68:   Document      doc = TtaGetDocument(el);
                     69:   AttributeType attType;
                     70:   Attribute     attribute;
                     71: 
                     72:   if (doc == 0 || !TtaGetDocumentAccessMode(doc))
                     73:     return;
                     74:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     75:   attType.AttrTypeNum = att;
                     76:   attribute = TtaGetAttribute(el, attType);
                     77:   if (attribute == NULL)
                     78:     {
                     79:       attribute = TtaNewAttribute (attType);
                     80:       TtaAttachAttribute(el, attribute, doc);
                     81:       TtaRegisterAttributeCreate(attribute, el, doc);
                     82:     }
                     83:   TtaSetAttributeText(attribute, value, el, doc);
                     84:   TtaRegisterAttributeReplace(attribute, el, doc);
                     85: #endif /* TEMPLATES */
                     86: }
                     87: 
1.13      kia        88: /*----------------------------------------------------------------------
                     89: Returns the value of a string attribute without copy it 
                     90: ----------------------------------------------------------------------*/
                     91: void GiveAttributeStringValueFromNum (Element el, int att, char* buff, int* sz)
                     92: {
                     93: #ifdef TEMPLATES
                     94:   AttributeType attType;
                     95:   Attribute     attribute;
                     96:   int           size;
                     97: 
                     98:   attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                     99:   attType.AttrTypeNum = att;
                    100:   attribute = TtaGetAttribute(el, attType);
                    101:   
                    102:   size = TtaGetTextAttributeLength(attribute);
                    103:   TtaGiveTextAttributeValue (attribute, buff, &size);
                    104:   buff[size] = EOS;
                    105:   if(sz)
                    106:     *sz = size;
                    107: #endif /* TEMPLATES */
                    108: }
                    109: 
                    110: 
1.1       francesc  111: 
                    112: /*----------------------------------------------------------------------
                    113: Returns the value of a string attribute 
                    114: ----------------------------------------------------------------------*/
1.8       kia       115: char *GetAttributeStringValueFromNum (Element el, int att, int* sz)
1.1       francesc  116: {
                    117: #ifdef TEMPLATES
                    118:        AttributeType attType;
1.12      vatton    119:   Attribute     attribute;
                    120:   char         *aux;
                    121:   int           size;
                    122: 
1.1       francesc  123:        attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
                    124:        attType.AttrTypeNum = att;
1.12      vatton    125:        attribute = TtaGetAttribute(el, attType);
1.1       francesc  126:        
1.12      vatton    127:        size = TtaGetTextAttributeLength(attribute);
                    128:        aux = (char*) TtaGetMemory(size+1);
1.1       francesc  129:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    130:   aux[size] = EOS;
1.4       kia       131:   if(sz)
                    132:     *sz = size;
1.1       francesc  133:        return aux;
                    134: #else
1.8       kia       135:        return NULL;
1.1       francesc  136: #endif /* TEMPLATES */
                    137: }
1.2       francesc  138: 
                    139: /*----------------------------------------------------------------------
                    140: Returns the value of a string attribute 
                    141: ----------------------------------------------------------------------*/
1.4       kia       142: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz)
1.2       francesc  143: {
                    144: #ifdef TEMPLATES
                    145:        int size = TtaGetTextAttributeLength(attribute);
                    146:        char *aux = (char*) TtaGetMemory(size+1);
1.12      vatton    147: 
1.2       francesc  148:        TtaGiveTextAttributeValue (attribute, aux, &size);
1.12      vatton    149:   aux[size] = EOS;
1.4       kia       150:   if(sz)
                    151:     *sz = size;
1.2       francesc  152:        return aux;
                    153: #else
1.8       kia       154:        return NULL;
1.2       francesc  155: #endif /* TEMPLATES */
                    156: }
1.6       kia       157: 
                    158: 
                    159: /*----------------------------------------------------------------------
                    160: GetFirstEditableElement
                    161: Returns the first descendant element which is modifiable.
                    162: ----------------------------------------------------------------------*/
                    163: Element GetFirstEditableElement (Element el)
                    164: {
                    165:   Element res = NULL;
                    166:   Element current = TtaGetFirstChild(el);
                    167:   
                    168:   while(!res && current)
                    169:   {
                    170:     res = GetFirstEditableElement(current);
                    171:     TtaNextSibling(&current);
                    172:   }
                    173:   
                    174:   if(!res && !TtaIsReadOnly(el))
                    175:     res = el;
                    176:   
                    177:   return res;
                    178: }
1.14      kia       179: 
                    180: /*----------------------------------------------------------------------
1.15      kia       181:   TemplateCanInsertFirstChild
                    182:   Test if an element can be inserted as child of another, bypassing xt.
                    183: ----------------------------------------------------------------------*/
                    184: ThotBool TemplateCanInsertFirstChild(ElementType elementType, Element parent, Document document)
                    185: {
                    186: #ifdef TEMPLATES
                    187:   SSchema         templateSSchema = TtaGetSSchema ("Template", document);
                    188:   ElementType     parType;
                    189:   
                    190:   while(parent)
                    191:     {
                    192:       parType = TtaGetElementType(parent);
                    193:       if(parType.ElSSchema != templateSSchema)
                    194:         break;
                    195:       parent = TtaGetParent(parent);
                    196:     }
                    197:   if(!parent)
                    198:     return FALSE;
                    199: #endif /* TEMPLATES */
                    200:   return TtaCanInsertFirstChild(elementType, parent, document);
                    201: }
                    202: 
                    203: /*----------------------------------------------------------------------
1.14      kia       204:   ValidateTemplateAttrInMenu
                    205:   Validate the status of an attribute according to xt::atribute rules.
                    206:   ----------------------------------------------------------------------*/
                    207: ThotBool ValidateTemplateAttrInMenu (NotifyAttribute * event)
                    208: {
                    209: #ifdef TEMPLATES
                    210:   Element       elem;
                    211:   Element       parent;
                    212:   ElementType   elType;
                    213:   AttributeType attrType;
                    214:   Attribute     attr;
                    215:   char*         attrName;
                    216:   char          buffer[MAX_LENGTH];
                    217:   int           sz;
                    218:   int           useAt, type;
                    219:   
                    220:   /* Prevent from showing attributes for template instance but not templates. */
                    221:   if(IsTemplateInstanceDocument(event->document))
                    222:     {
                    223:       /* Prevent if attribute's element is not a descendant of xt:use */
1.17      kia       224:       /* Dont prevent if descendant of xt:bag. */
1.14      kia       225:       parent = event->element;
                    226:       elem = GetFirstTemplateParentElement(parent);
                    227:       if(!elem)
                    228:         return TRUE;
                    229:       elType     = TtaGetElementType(elem);
1.17      kia       230:       if(elType.ElTypeNum==Template_EL_bag)
                    231:         return FALSE;
1.14      kia       232:       if(elType.ElTypeNum!=Template_EL_useSimple)
                    233:         return TRUE;
                    234: 
                    235:       /* Search for the corresponding xt:attribute element*/
                    236:       attrName = TtaGetAttributeName(event->attributeType);
                    237:       attrType.AttrSSchema = TtaGetSSchema ("Template", event->document);
                    238:       for(elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem))
                    239:         {
                    240:           attrType.AttrTypeNum = Template_ATTR_ref_name;
                    241:           elType = TtaGetElementType(elem);
                    242:           if(elType.ElTypeNum==Template_EL_attribute &&
                    243:                   elType.ElSSchema==TtaGetSSchema ("Template", event->document))
                    244:             {
                    245:                attr = TtaGetAttribute(elem, attrType);
                    246:                if(attr)
                    247:                  {
                    248:                    sz = MAX_LENGTH;
                    249:                    TtaGiveTextAttributeValue(attr, buffer, &sz);
                    250:                    if(!strcmp(buffer, attrName))
                    251:                      {
                    252:                        /* Process the attribute filtering */
                    253:                        /* Get 'useAt' attr value. */
                    254:                        attrType.AttrTypeNum = Template_ATTR_useAt;
                    255:                        attr = TtaGetAttribute(elem, attrType);
                    256:                        if(attr)
                    257:                          useAt = TtaGetAttributeValue(attr);
                    258:                        else
                    259:                          useAt = Template_ATTR_useAt_VAL_required;
                    260:                        /* Get 'type' attr value. */                       
                    261:                        attrType.AttrTypeNum = Template_ATTR_type;
                    262:                        attr = TtaGetAttribute(elem, attrType);
                    263:                        if(attr)
                    264:                          type = TtaGetAttributeValue(attr);
                    265:                        else
                    266:                          type = Template_ATTR_type_VAL_string;
1.16      kia       267: #ifdef AMAYA_DEBUG
                    268: #ifdef EK
1.14      kia       269: /******************************************************************************/
                    270:                        printf("Attribute : %s \n", attrName);
                    271:                        switch(useAt)
                    272:                        {
                    273:                          case Template_ATTR_useAt_VAL_required:
                    274:                            printf("    required");
                    275:                            break;
                    276:                          case Template_ATTR_useAt_VAL_optional:
                    277:                            printf("    optional");
                    278:                            break;
                    279:                          case Template_ATTR_useAt_VAL_prohibited:
                    280:                            printf("    prohibited");
                    281:                            break;
                    282:                          default:
                    283:                            printf("    error");
                    284:                            break;
                    285:                        }
                    286:                        switch(type)
                    287:                        {
                    288:                          case Template_ATTR_type_VAL_string:
                    289:                            printf(" string\n");
                    290:                            break;
                    291:                          case Template_ATTR_type_VAL_number:
                    292:                            printf(" number\n");
                    293:                            break;
                    294:                          case Template_ATTR_type_VAL_listVal:
                    295:                            printf(" list\n");
                    296:                            break;
                    297:                          default:
                    298:                            printf(" error\n");
                    299:                            break;
                    300:                        }
                    301: /******************************************************************************/
1.16      kia       302: #endif  /* EK */
                    303: #endif /* AMAYA_DEBUG */
1.14      kia       304:                        event->restr.RestrType = (RestrictionContentType)type;
                    305:                        /* If attr is prohibited, dont show it.*/
                    306:                        if(useAt==Template_ATTR_useAt_VAL_prohibited)
1.16      kia       307:                            return TRUE;
1.14      kia       308:                        if(useAt==Template_ATTR_useAt_VAL_required)
                    309:                          {
                    310:                            /* Force the usage of this attribute.*/
                    311:                            event->restr.RestrFlags |= attr_mandatory;
                    312:                          }
                    313: 
                    314:                        /* Get 'fixed' attr value. */
                    315:                        attrType.AttrTypeNum = Template_ATTR_fixed;
                    316:                        attr = TtaGetAttribute(elem, attrType);
                    317:                        if(attr)
                    318:                          {
                    319:                            sz = MAX_LENGTH;
                    320:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    321:                            event->restr.RestrFlags |= attr_readonly;
                    322:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    323:                            return FALSE;
                    324:                          }
                    325: 
                    326:                        /* Get 'default' attr value.*/
                    327:                        attrType.AttrTypeNum = Template_ATTR_defaultAt;
                    328:                        attr = TtaGetAttribute(elem, attrType);
                    329:                        if(attr)
                    330:                          {
                    331:                            sz = MAX_LENGTH;
                    332:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    333:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    334:                          }
                    335: 
                    336:                        /* Get 'values' attr value.*/
                    337:                        attrType.AttrTypeNum = Template_ATTR_values;
                    338:                        attr = TtaGetAttribute(elem, attrType);
                    339:                        if(attr)
                    340:                          {
                    341:                            sz = MAX_LENGTH;
                    342:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    343:                            event->restr.RestrEnumVal = TtaStrdup(buffer);
                    344:                            event->restr.RestrFlags |= attr_enum;
                    345:                          }
                    346:                        return FALSE;
                    347:                      }
                    348:                  }
                    349:             }
                    350:         }
                    351:       
1.16      kia       352:       return TRUE;
1.14      kia       353:     }
                    354:   else
                    355: #endif /* TEMPLATES */
                    356:     return FALSE;
                    357: }

Webmaster