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

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

Webmaster