Annotation of Amaya/amaya/insertelem.c, revision 1.3

1.1       kia         1: /*
                      2:  *
                      3:  *  COPYRIGHT INRIA and W3C, 1996-2005
                      4:  *  Please first read the full copyright statement in file COPYRIGHT.
                      5:  *
                      6:  */
                      7:  
                      8: /*
                      9:  * Authors: Emilien Kia
                     10:  *
                     11:  */
                     12: 
                     13: #define THOT_EXPORT extern
                     14: #include "amaya.h"
                     15: #include "document.h"
                     16: 
                     17: #include "containers.h"
                     18: #include "Elemlist.h"
                     19: 
                     20: #ifdef TEMPLATES
                     21: #include "Template.h"
                     22: #include "templates.h"
                     23: #include "templates_f.h"
                     24: #include "templateDeclarations.h"
                     25: 
                     26: #include "mydictionary_f.h"
                     27: #include "templateLoad_f.h"
                     28: #include "templateDeclarations_f.h"
                     29: #include "templateInstantiate_f.h"
                     30: #include "appdialogue_wx.h"
                     31: #include "init_f.h"
                     32: #include "wxdialogapi_f.h"
                     33: #include "AHTURLTools_f.h"
                     34: 
                     35: #endif /* TEMPLATES */
                     36: 
                     37: 
                     38: #include "fetchXMLname_f.h"
                     39: #include "MENUconf.h"
                     40: #include "parser.h"
                     41: #include "fetchXMLname_f.h"
                     42: 
1.2       cvs        43: typedef struct _sInsertableElementList *InsertableElementList;
                     44: typedef struct _sInsertableElementList
1.1       kia        45: {
                     46:   /** Current selected element.*/
                     47:   Element elem;
                     48:   /** Insertable element list.*/
                     49:   DLList list;
1.2       cvs        50: } sInsertableElementList;
1.1       kia        51: 
                     52: 
                     53: static HashMap InsertableElementMap = NULL;
                     54: 
                     55: 
                     56: static InsertableElementList InsertableElementList_Create(Element elem, DLList list)
                     57: {
                     58:   InsertableElementList lst = (InsertableElementList)TtaGetMemory(sizeof(sInsertableElementList));
                     59:   lst->elem = elem;
                     60:   lst->list = list;
                     61:   return lst; 
                     62: }
                     63: 
                     64: static void InsertableElementList_Destroy(InsertableElementList list)
                     65: {
                     66:   if(list->list)
                     67:     DLList_Destroy(list->list);
                     68:   TtaFreeMemory(list);
                     69: }
                     70: 
                     71: /*----------------------------------------------------------------------
                     72:   InsertableElement_Init
                     73:   Initialize the module.
                     74:   ----------------------------------------------------------------------*/
                     75: 
                     76: void InsertableElement_Init()
                     77: {
                     78:   if(!InsertableElementMap)
                     79:     InsertableElementMap = PointerHashMap_Create((Container_DestroyElementFunction)InsertableElementList_Destroy, 32);
                     80: }
                     81: 
                     82: /*----------------------------------------------------------------------
                     83:   InsertableElement_Final
                     84:   Finalize the module.
                     85:   ----------------------------------------------------------------------*/
                     86: 
                     87: void InsertableElement_Final()
                     88: {
                     89:   if(InsertableElementMap)
                     90:   {
                     91:     HashMap_Destroy(InsertableElementMap);
                     92:     InsertableElementMap = NULL;
                     93:   }
                     94: }
                     95: 
                     96: 
                     97: 
                     98: 
                     99: #ifdef TEMPLATES
                    100: 
                    101: /*----------------------------------------------------------------------
                    102:   FillUnionResolvedPossibleElement
                    103:   Fill an element list with all possible element, resolving them if union.
                    104:   @param name Element name
                    105:   @param elem Document element to attach
                    106:   @param resolvedPath Path of different succesive union name.
                    107:   @param list List to fill.
                    108:   ----------------------------------------------------------------------*/
                    109: static void FillUnionResolvedPossibleElement(XTigerTemplate t, const char* name, Element elem, const char* resolvedPath,  DLList list, int level)
                    110: {
                    111:   Declaration dec = GetDeclaration (t, name);
                    112:   if(dec->declaredIn->isPredefined)
                    113:   {
                    114:     DLList_Append(list, ElemListElement_CreateComponent(level, dec->name, (void*)dec, resolvedPath, elem));
                    115:   }
                    116:   else if(dec->nature==ComponentNat)
                    117:   {
                    118:     DLList_Append(list, ElemListElement_CreateComponent(level, dec->name, (void*)dec, resolvedPath, elem));
                    119:   }
                    120:   else if(dec->nature==UnionNat)
                    121:   {
                    122:     DLList tempList = ElemList_Create();
                    123:     
                    124:     Record first = dec->unionType.include->first;
                    125:     Record rec = first;
                    126:     
                    127:     int len1 = 0 , len2 = strlen(dec->name);
                    128:     if(resolvedPath!=NULL)
                    129:       len1 = strlen(resolvedPath);
                    130:     char* newPath = (char*)TtaGetMemory(len1+len2+2);
                    131:     if(len1>0)
                    132:     {
                    133:       strcpy(newPath, resolvedPath);
                    134:       newPath[len1] = '/';
                    135:       strcpy(newPath+len1+1, dec->name);
                    136:     }
                    137:     else
                    138:     {
                    139:       strcpy(newPath, dec->name);
                    140:     }
                    141:     
                    142:     while(rec)
                    143:     {
                    144:       FillUnionResolvedPossibleElement(t, rec->key, elem, newPath, tempList, level);
                    145:       rec = rec->next;
                    146:     }
                    147:     
                    148:     ForwardIterator iter = DLList_GetForwardIterator(tempList);
                    149:     DLListNode node = (DLListNode) ForwardIterator_GetFirst(iter);
                    150:     while(node)
                    151:     {
                    152:       DLList_Append(list, node->elem);
                    153:       node = (DLListNode) ForwardIterator_GetNext(iter);
                    154:     }
                    155:     tempList->destroyElement = NULL;
                    156:     DLList_Destroy(tempList);
                    157:     
                    158:     TtaFreeMemory(newPath);
                    159:     
                    160:     /** todo Remove excluded elements.*/
                    161:   }
                    162:   else if(dec->nature==SimpleTypeNat)
                    163:   {
                    164:     DLList_Append(list, ElemListElement_CreateBaseType(level, dec->name, resolvedPath, elem));
                    165:     /* Do nothing. */
                    166:   }
                    167:   else
                    168:   {
                    169:     /* Search in tgt std elements. */
                    170:     int xmlType; /* See parser.h */
                    171:     for(xmlType=XHTML_TYPE; xmlType<Template_TYPE; xmlType++)
                    172:     {
                    173:       ElementType elType = {0,0};
                    174:       char*       mappedName;
1.2       cvs       175:       char       content;
1.1       kia       176:       ThotBool    checkProfile;
1.2       cvs       177:       MapXMLElementType(xmlType, dec->name, &elType, &mappedName, &content, &checkProfile, TtaGetDocument(elem));
1.1       kia       178:       if(elType.ElTypeNum!=0)
                    179:       {
                    180:         DLList_Append(list, ElemListElement_CreateLanguageElement(level, elType, resolvedPath, elem));
                    181:         break;
                    182:       }
                    183:     }
                    184:   }
                    185: }
                    186: 
                    187: /*----------------------------------------------------------------------
                    188:   FillInsertableTemplateElementFromStringList
                    189:   Fill an element list with all possible elements extracted from a stringlist.
                    190:     ----------------------------------------------------------------------*/
                    191: static void FillInsertableTemplateElementFromStringList(XTigerTemplate t, Element refelem, const char* strlist, DLList list, int level)
                    192: {
                    193:   int             pos = 0,
                    194:                   offset = 0;
                    195:   int size = strlen(strlist);
                    196:   char* types = strdup(strlist);
                    197:   while(pos<size)
                    198:   {
                    199:     if(isEOSorWhiteSpace(types[pos]))
                    200:     {
                    201:       if(pos>offset)
                    202:       {
                    203:         types[pos] = 0;
                    204:         FillUnionResolvedPossibleElement(t, types+offset, refelem, NULL, list, level);
                    205:       }
                    206:       offset = pos+1;
                    207:     }
                    208:     pos++;
                    209:   } 
                    210:   if(pos>offset)
                    211:   {
                    212:     types[pos] = 0;
                    213:     FillUnionResolvedPossibleElement(t, types+offset, refelem, NULL, list, level);
                    214:   }
                    215:   TtaFreeMemory(types);
                    216: }
                    217: 
                    218: 
                    219: /*----------------------------------------------------------------------
                    220:   FillInsertableElementFromElemAttribute
                    221:   Fill an element list with all possible elements from an attribute list.
                    222:   ----------------------------------------------------------------------*/
1.3     ! kia       223: static void FillInsertableElementFromElemAttribute(XTigerTemplate t, Element elem, Element refelem, int attrib, DLList list, int level)
1.1       kia       224: {
                    225:   ElementType     type = TtaGetElementType(elem);
                    226:   AttributeType   attributeType = {type.ElSSchema, attrib};
                    227:   Attribute       att = TtaGetAttribute (elem, attributeType);
                    228:   int             size = TtaGetTextAttributeLength (att);
                    229:   char*           types = (char *) TtaGetMemory (size+1); 
                    230:   TtaGiveTextAttributeValue (att, types, &size);
1.3     ! kia       231:   FillInsertableTemplateElementFromStringList(t, refelem, types, list, level);
1.1       kia       232: }
                    233: #endif/* TEMPLATES */
                    234: 
                    235: /*----------------------------------------------------------------------
                    236:   FillInsertableElemList
                    237:   Fill an element list with all insertable elements (base element or
                    238:   XTiger comonent).
                    239:   ----------------------------------------------------------------------*/
                    240: static void FillInsertableElemList(Document doc, Element elem, DLList list)
                    241: {
1.2       cvs       242:   int level;
                    243:   ThotBool cont;
                    244: 
1.1       kia       245:   if(elem){
                    246:     if(doc==0)
                    247:       doc = TtaGetDocument(elem);
                    248: 
                    249: #ifdef TEMPLATES
                    250:     XTigerTemplate   t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url);
                    251: #endif/* TEMPLATES */
                    252: 
1.2       cvs       253:     level = 0;
                    254:     cont = TRUE;
1.1       kia       255:     
                    256:     while(elem!=NULL && cont)
                    257:     {
                    258:       ElementType   type = TtaGetElementType(elem);
                    259:       Element       child;
                    260:       ElementType   childType;
                    261:       switch(type.ElTypeNum)
                    262:       {
                    263: #ifdef TEMPLATES
                    264:         case Template_EL_repeat:
                    265:           child = TtaGetFirstChild(elem);
                    266:           childType = TtaGetElementType(child);
                    267:           switch(childType.ElTypeNum)
                    268:           {
                    269:             case Template_EL_useEl:
1.3     ! kia       270:               FillInsertableElementFromElemAttribute(t, child, elem, Template_ATTR_types, list, level);
        !           271:               break;
1.1       kia       272:             case Template_EL_useSimple:
1.3     ! kia       273:               FillInsertableElementFromElemAttribute(t, child, elem, Template_ATTR_types, list, level);
1.1       kia       274:               break;
                    275:             case Template_EL_bag:
1.3     ! kia       276:               FillInsertableElementFromElemAttribute(t, child, elem, Template_ATTR_types, list, level);
1.1       kia       277:               break;
                    278:             default:
                    279:               break;
                    280:           }
                    281:           cont = FALSE;
                    282:           break;
                    283:         case Template_EL_useEl:
                    284:           // Fill for xt:use only if have no child.
                    285:           if(TtaGetFirstChild(elem)==NULL){
1.3     ! kia       286:             FillInsertableElementFromElemAttribute(t, elem, elem, Template_ATTR_types, list, level);
1.1       kia       287:             cont = FALSE;
                    288:           }
                    289:           break;
                    290:         case Template_EL_bag:
1.3     ! kia       291:           FillInsertableElementFromElemAttribute(t, elem, elem, Template_ATTR_types, list, level);
1.1       kia       292:           cont = FALSE;
                    293:           break;
                    294: #endif /*TEMPLATES */
                    295:         default:
                    296:           break;
                    297:       }
                    298:       
                    299:       elem = TtaGetParent(elem);
                    300:       level ++;
                    301:     }    
                    302:   }  
                    303: }
                    304: 
                    305: /*----------------------------------------------------------------------
                    306:   InsertableElement_GetList
                    307:   Get the insertable element list for a document.
                    308:   @param doc Document
                    309:   @return The insertable element list or NULL.
                    310:   ----------------------------------------------------------------------*/
                    311: DLList InsertableElement_GetList(Document doc)
                    312: {
                    313:   InsertableElementList list = (InsertableElementList) HashMap_Get(InsertableElementMap, (void*)doc);
                    314:   if(list!=NULL)
                    315:     return list->list;
                    316:   else
                    317:     return NULL;
                    318: }
                    319: 
                    320: /*----------------------------------------------------------------------
                    321:   InsertableElement_Update
                    322:   Update the insertable element list for a document.
                    323:   @param el Selected element, cant be NULL.
                    324:   @param document Document, can be NULL.
                    325:   @param force No dont force the refresh of the list if the element is already selected.
                    326:   @return List of insertable elements.
                    327:   ----------------------------------------------------------------------*/
                    328: DLList InsertableElement_Update(Document doc, Element el, ThotBool force)
                    329: {
1.2       cvs       330:   InsertableElementList list;
1.1       kia       331:   if(doc==0)
                    332:     doc= TtaGetDocument(el);
1.2       cvs       333:   list = (InsertableElementList) HashMap_Get(InsertableElementMap, (void*)doc);
1.1       kia       334:   if(list==NULL)
                    335:   {
                    336:     list = InsertableElementList_Create(0, DLList_Create());
                    337:     HashMap_Set(InsertableElementMap, (void*)doc, list);
                    338:   }
                    339:   if(force || list->elem!=el){
                    340:     DLList_Empty(list->list);
                    341:     FillInsertableElemList(doc, el, list->list);
                    342:     list->elem = el;
                    343:   }
                    344:   return list->list;
                    345: }
                    346: 
1.3     ! kia       347: /*----------------------------------------------------------------------
        !           348:   InsertableElement_DoInsertElement
        !           349:   Insert the specified element.
        !           350:   @param el Element to insert (ElemListElement)
        !           351:   ----------------------------------------------------------------------*/
        !           352: void InsertableElement_DoInsertElement(void* el)
        !           353: {
        !           354:   ElemListElement elem = (ElemListElement)el;
        !           355:   
        !           356:   Element ref = elem->refElem;
        !           357:   ElementType refType = TtaGetElementType(ref);
        !           358:   
        !           359:   Document doc = TtaGetDocument(ref);
        !           360:   
        !           361:   printf("insert %s into %s\n", ElemListElement_GetName(elem), TtaGetElementTypeName(refType));
        !           362:   switch(refType.ElTypeNum)
        !           363:   {
        !           364: #ifdef TEMPLATES
        !           365:     case Template_EL_repeat:
        !           366:       if(elem->typeClass==DefinedComponent)
        !           367:         Template_InsertRepeatChild(doc, ref, (Declaration)elem->elem.component.declaration, -1);
        !           368:       break;
        !           369: #endif /* TEMPLATES */
        !           370:     default:
        !           371:       break;
        !           372:   }
        !           373:   
        !           374: }

Webmaster