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

1.9       vatton      1: /*
                      2:  *
                      3:  *  COPYRIGHT INRIA and W3C, 2006-2007
                      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 */
                    224:       parent = event->element;
                    225:       elem = GetFirstTemplateParentElement(parent);
                    226:       if(!elem)
                    227:         return TRUE;
                    228:       elType     = TtaGetElementType(elem);
                    229:       if(elType.ElTypeNum!=Template_EL_useSimple)
                    230:         return TRUE;
                    231: 
                    232:       /* Search for the corresponding xt:attribute element*/
                    233:       attrName = TtaGetAttributeName(event->attributeType);
                    234:       attrType.AttrSSchema = TtaGetSSchema ("Template", event->document);
                    235:       for(elem = TtaGetFirstChild(parent); elem; TtaNextSibling(&elem))
                    236:         {
                    237:           attrType.AttrTypeNum = Template_ATTR_ref_name;
                    238:           elType = TtaGetElementType(elem);
                    239:           if(elType.ElTypeNum==Template_EL_attribute &&
                    240:                   elType.ElSSchema==TtaGetSSchema ("Template", event->document))
                    241:             {
                    242:                attr = TtaGetAttribute(elem, attrType);
                    243:                if(attr)
                    244:                  {
                    245:                    sz = MAX_LENGTH;
                    246:                    TtaGiveTextAttributeValue(attr, buffer, &sz);
                    247:                    if(!strcmp(buffer, attrName))
                    248:                      {
                    249:                        /* Process the attribute filtering */
                    250:                        /* Get 'useAt' attr value. */
                    251:                        attrType.AttrTypeNum = Template_ATTR_useAt;
                    252:                        attr = TtaGetAttribute(elem, attrType);
                    253:                        if(attr)
                    254:                          useAt = TtaGetAttributeValue(attr);
                    255:                        else
                    256:                          useAt = Template_ATTR_useAt_VAL_required;
                    257:                        /* Get 'type' attr value. */                       
                    258:                        attrType.AttrTypeNum = Template_ATTR_type;
                    259:                        attr = TtaGetAttribute(elem, attrType);
                    260:                        if(attr)
                    261:                          type = TtaGetAttributeValue(attr);
                    262:                        else
                    263:                          type = Template_ATTR_type_VAL_string;
                    264: 
                    265: /******************************************************************************/
                    266:                        printf("Attribute : %s \n", attrName);
                    267:                        switch(useAt)
                    268:                        {
                    269:                          case Template_ATTR_useAt_VAL_required:
                    270:                            printf("    required");
                    271:                            break;
                    272:                          case Template_ATTR_useAt_VAL_optional:
                    273:                            printf("    optional");
                    274:                            break;
                    275:                          case Template_ATTR_useAt_VAL_prohibited:
                    276:                            printf("    prohibited");
                    277:                            break;
                    278:                          default:
                    279:                            printf("    error");
                    280:                            break;
                    281:                        }
                    282:                        switch(type)
                    283:                        {
                    284:                          case Template_ATTR_type_VAL_string:
                    285:                            printf(" string\n");
                    286:                            break;
                    287:                          case Template_ATTR_type_VAL_number:
                    288:                            printf(" number\n");
                    289:                            break;
                    290:                          case Template_ATTR_type_VAL_listVal:
                    291:                            printf(" list\n");
                    292:                            break;
                    293:                          default:
                    294:                            printf(" error\n");
                    295:                            break;
                    296:                        }
                    297: /******************************************************************************/
                    298: 
                    299:                        event->restr.RestrType = (RestrictionContentType)type;
                    300:                        /* If attr is prohibited, dont show it.*/
                    301:                        if(useAt==Template_ATTR_useAt_VAL_prohibited)
                    302:                          return TRUE;
                    303:                        if(useAt==Template_ATTR_useAt_VAL_required)
                    304:                          {
                    305:                            /* Force the usage of this attribute.*/
                    306:                            event->restr.RestrFlags |= attr_mandatory;
                    307:                          }
                    308: 
                    309:                        /* Get 'fixed' attr value. */
                    310:                        attrType.AttrTypeNum = Template_ATTR_fixed;
                    311:                        attr = TtaGetAttribute(elem, attrType);
                    312:                        if(attr)
                    313:                          {
                    314:                            sz = MAX_LENGTH;
                    315:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    316:                            event->restr.RestrFlags |= attr_readonly;
                    317:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    318:                            return FALSE;
                    319:                          }
                    320: 
                    321:                        /* Get 'default' attr value.*/
                    322:                        attrType.AttrTypeNum = Template_ATTR_defaultAt;
                    323:                        attr = TtaGetAttribute(elem, attrType);
                    324:                        if(attr)
                    325:                          {
                    326:                            sz = MAX_LENGTH;
                    327:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    328:                            event->restr.RestrDefVal = TtaStrdup(buffer);
                    329:                          }
                    330: 
                    331:                        /* Get 'values' attr value.*/
                    332:                        attrType.AttrTypeNum = Template_ATTR_values;
                    333:                        attr = TtaGetAttribute(elem, attrType);
                    334:                        if(attr)
                    335:                          {
                    336:                            sz = MAX_LENGTH;
                    337:                            TtaGiveTextAttributeValue(attr, buffer, &sz);
                    338:                            event->restr.RestrEnumVal = TtaStrdup(buffer);
                    339:                            event->restr.RestrFlags |= attr_enum;
                    340:                          }
                    341:                        return FALSE;
                    342:                      }
                    343:                  }
                    344:             }
                    345:         }
                    346:       
                    347:       return FALSE;
                    348:     }
                    349:   else
                    350: #endif /* TEMPLATES */
                    351:     return FALSE;
                    352: }

Webmaster