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

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.17    ! cvs       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.17    ! cvs       145:     ITERATOR_FOREACH(iter, HashMapNode, mapnode)
1.13      kia       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);
1.17    ! cvs       155:        ITERATOR_FOREACH(iter, DLListNode, listnode)
1.13      kia       156:       DLList_Append(list, listnode->elem);
                    157:     TtaFreeMemory(iter);
                    158: 
1.1       kia       159:     tempList->destroyElement = NULL;
                    160:     DLList_Destroy(tempList);
                    161:     
                    162:     TtaFreeMemory(newPath);
                    163:     
                    164:     /** todo Remove excluded elements.*/
                    165:   }
1.8       vatton    166:   else if (dec->nature==SimpleTypeNat)
1.1       kia       167:   {
1.12      vatton    168:     DLList_Append(list, ElemListElement_CreateBaseType(level, dec->name, resolvedPath,
                    169:                                                        elem));
1.1       kia       170:     /* Do nothing. */
                    171:   }
                    172:   else
                    173:   {
                    174:     /* Search in tgt std elements. */
                    175:     int xmlType; /* See parser.h */
                    176:     for(xmlType=XHTML_TYPE; xmlType<Template_TYPE; xmlType++)
                    177:     {
                    178:       ElementType elType = {0,0};
                    179:       char*       mappedName;
1.2       cvs       180:       char       content;
1.1       kia       181:       ThotBool    checkProfile;
1.6       vatton    182:       MapXMLElementType(xmlType, dec->name, &elType, &mappedName, &content,
                    183:                         &checkProfile, TtaGetDocument(elem));
1.8       vatton    184:       if (elType.ElTypeNum!=0)
1.1       kia       185:       {
1.12      vatton    186:         DLList_Append(list, ElemListElement_CreateLanguageElement(level, elType,
                    187:                                                                   resolvedPath, elem));
1.1       kia       188:         break;
                    189:       }
                    190:     }
                    191:   }
                    192: }
                    193: 
                    194: /*----------------------------------------------------------------------
                    195:   FillInsertableElementFromElemAttribute
                    196:   Fill an element list with all possible elements from an attribute list.
                    197:   ----------------------------------------------------------------------*/
1.16      vatton    198: static void FillInsertableElementFromElemAttribute (XTigerTemplate t,
                    199:                                                     Element elem, Element refelem,
                    200:                                                     int attrib, DLList list, int level)
1.1       kia       201: {
                    202:   ElementType     type = TtaGetElementType(elem);
                    203:   AttributeType   attributeType = {type.ElSSchema, attrib};
                    204:   Attribute       att = TtaGetAttribute (elem, attributeType);
                    205:   int             size = TtaGetTextAttributeLength (att);
                    206:   char*           types = (char *) TtaGetMemory (size+1); 
1.6       vatton    207: 
1.1       kia       208:   TtaGiveTextAttributeValue (att, types, &size);
1.14      kia       209: 
                    210:   HashMap         basemap = KeywordHashMap_CreateFromList(NULL, -1, types);
                    211:   HashMap         map     = Template_ExpandHashMapTypes(t, basemap);
                    212:   ForwardIterator iter;
                    213:   HashMapNode     node;
                    214:    
                    215:   iter = HashMap_GetForwardIterator(map);
                    216:   ITERATOR_FOREACH(iter, HashMapNode, node)
                    217:     {
                    218:       FillUnionResolvedPossibleElement(t, (const char*)node->key, refelem, NULL, list, level);
                    219:     }
                    220:   HashMap_Destroy (map);
                    221:   HashMap_Destroy (basemap);
                    222:   
1.6       vatton    223:   TtaFreeMemory (types);
1.1       kia       224: }
                    225: #endif/* TEMPLATES */
                    226: 
                    227: /*----------------------------------------------------------------------
                    228:   FillInsertableElemList
                    229:   Fill an element list with all insertable elements (base element or
                    230:   XTiger comonent).
                    231:   ----------------------------------------------------------------------*/
1.16      vatton    232: static void FillInsertableElemList (Document doc, Element elem, DLList list)
1.1       kia       233: {
1.6       vatton    234:   ElementType      type;
1.15      kia       235:   Element          parent;
1.4       cvs       236: #ifdef TEMPLATES
1.6       vatton    237:   Element          child;
                    238:   ElementType      childType;
                    239:   XTigerTemplate   t;
1.15      kia       240:   ThotBool         haveAncestorBag = FALSE;
1.4       cvs       241: #endif/* TEMPLATES */
1.2       cvs       242:   int level;
1.17    ! cvs       243:   ThotBool cont = TRUE;
1.2       cvs       244: 
1.17    ! cvs       245:   if (elem)
        !           246:   {
1.8       vatton    247:     if (doc==0)
1.1       kia       248:       doc = TtaGetDocument(elem);
                    249: 
                    250: #ifdef TEMPLATES
1.13      kia       251:     t = GetXTigerTemplate(DocumentMeta[doc]->template_url);
1.15      kia       252: 
                    253: 
1.17    ! cvs       254:     if (!IsTemplateElement(elem))
1.15      kia       255:       elem = GetFirstTemplateParentElement(elem);
                    256: 
                    257:     // Search for first xt:bag ancestor.
                    258:     parent = elem;
1.17    ! cvs       259:     while (parent!= NULL && cont)
1.15      kia       260:       {
                    261:         type = TtaGetElementType(parent);
1.17    ! cvs       262:         if (type.ElTypeNum == Template_EL_bag)
1.15      kia       263:           {
                    264:             haveAncestorBag = TRUE;
                    265:             cont = FALSE;
                    266:           }
                    267:         parent = GetFirstTemplateParentElement(parent);
                    268:       }
1.1       kia       269: 
1.2       cvs       270:     level = 0;
                    271:     cont = TRUE;
1.15      kia       272: 
                    273:     // Process for each ancestor.
1.17    ! cvs       274:     while (elem!=NULL && cont)
1.1       kia       275:     {
1.4       cvs       276:       type = TtaGetElementType(elem);
1.1       kia       277:       switch(type.ElTypeNum)
1.15      kia       278:         {
1.1       kia       279:         case Template_EL_repeat:
                    280:           child = TtaGetFirstChild(elem);
                    281:           childType = TtaGetElementType(child);
                    282:           switch(childType.ElTypeNum)
                    283:           {
                    284:             case Template_EL_useEl:
1.6       vatton    285:               FillInsertableElementFromElemAttribute(t, child, elem,
                    286:                                                      Template_ATTR_types, list, level);
1.3       kia       287:               break;
1.1       kia       288:             case Template_EL_useSimple:
1.6       vatton    289:               FillInsertableElementFromElemAttribute(t, child, elem,
                    290:                                                      Template_ATTR_types, list, level);
1.1       kia       291:               break;
                    292:             case Template_EL_bag:
1.6       vatton    293:               FillInsertableElementFromElemAttribute(t, child, elem,
                    294:                                                      Template_ATTR_types, list, level);
1.1       kia       295:               break;
                    296:             default:
                    297:               break;
                    298:           }
1.15      kia       299:           cont = haveAncestorBag;
1.1       kia       300:           break;
                    301:         case Template_EL_useEl:
                    302:           // Fill for xt:use only if have no child.
1.8       vatton    303:           if (TtaGetFirstChild(elem)==NULL){
1.6       vatton    304:             FillInsertableElementFromElemAttribute(t, elem, elem,
                    305:                                                    Template_ATTR_types, list, level);
1.15      kia       306:             cont = haveAncestorBag;
1.1       kia       307:           }
                    308:           break;
                    309:         case Template_EL_bag:
1.6       vatton    310:           FillInsertableElementFromElemAttribute(t, elem, elem,
                    311:                                                  Template_ATTR_types, list, level);
1.15      kia       312:           cont = FALSE;
1.1       kia       313:           break;
1.15      kia       314:         }
                    315:       elem = GetFirstTemplateParentElement(elem);
1.1       kia       316:       level ++;
1.15      kia       317:     }
                    318: #endif/* TEMPLATES */
                    319:   }
1.1       kia       320: }
                    321: 
                    322: /*----------------------------------------------------------------------
                    323:   InsertableElement_GetList
                    324:   Get the insertable element list for a document.
                    325:   @param doc Document
                    326:   @return The insertable element list or NULL.
                    327:   ----------------------------------------------------------------------*/
                    328: DLList InsertableElement_GetList(Document doc)
                    329: {
1.16      vatton    330:   InsertableElementList list;
                    331: 
                    332:   list = (InsertableElementList) HashMap_Get(InsertableElementMap, (void*)doc);
1.6       vatton    333:   if (list)
1.1       kia       334:     return list->list;
                    335:   else
                    336:     return NULL;
                    337: }
                    338: 
                    339: /*----------------------------------------------------------------------
                    340:   InsertableElement_Update
                    341:   Update the insertable element list for a document.
                    342:   @param el Selected element, cant be NULL.
                    343:   @param document Document, can be NULL.
                    344:   @param force No dont force the refresh of the list if the element is already selected.
                    345:   @return List of insertable elements.
                    346:   ----------------------------------------------------------------------*/
1.7       kia       347: DLList InsertableElement_Update(Document doc, Element el)
1.1       kia       348: {
1.2       cvs       349:   InsertableElementList list;
1.16      vatton    350: 
                    351:   if (doc == 0)
                    352:     doc= TtaGetDocument (el);
                    353:   list = (InsertableElementList) HashMap_Get (InsertableElementMap, (void*)doc);
                    354:   if (list == NULL)
1.1       kia       355:   {
1.16      vatton    356:     list = InsertableElementList_Create (0, DLList_Create());
                    357:     HashMap_Set (InsertableElementMap, (void*)doc, list);
1.1       kia       358:   }
1.7       kia       359:   
1.16      vatton    360:   DLList_Empty (list->list);
                    361:   FillInsertableElemList (doc, el, list->list);
1.7       kia       362:   list->elem = el;
                    363: 
1.1       kia       364:   return list->list;
                    365: }
                    366: 
1.3       kia       367: /*----------------------------------------------------------------------
                    368:   InsertableElement_DoInsertElement
                    369:   Insert the specified element.
                    370:   @param el Element to insert (ElemListElement)
                    371:   ----------------------------------------------------------------------*/
1.16      vatton    372: void InsertableElement_DoInsertElement (void* el)
1.3       kia       373: {
1.16      vatton    374:   ElemListElement elem = (ElemListElement) el;
                    375:   Element         ref = elem->refElem;
                    376:   ElementType     refType = TtaGetElementType (ref);
                    377:   Document        doc = TtaGetDocument (ref);
                    378:   Element         newEl = NULL;
                    379:   SSchema         templateSSchema;
1.10      kia       380: 
1.6       vatton    381: #ifdef AMAYA_DEBUG
1.12      vatton    382:   printf("insert %s into %s\n", ElemListElement_GetName(elem),
1.16      vatton    383:          TtaGetElementTypeName (refType));
1.6       vatton    384: #endif /* AMAYA_DEBUG */
1.11      kia       385: 
                    386: #ifdef TEMPLATES
1.16      vatton    387:   templateSSchema = TtaGetSSchema ("Template", doc);
                    388:   if (templateSSchema && refType.ElSSchema == templateSSchema)
1.3       kia       389:   {
1.11      kia       390:     switch(refType.ElTypeNum)
                    391:     {
                    392:       case Template_EL_repeat:
                    393:         if (elem->typeClass==DefinedComponent)
1.16      vatton    394:           newEl = Template_InsertRepeatChild (doc, ref,
                    395:                                               (Declaration)elem->elem.component.declaration,
                    396:                                               -1);
1.11      kia       397:         break;
                    398:       case Template_EL_bag:
1.16      vatton    399:         newEl = Template_InsertBagChild (doc, ref,
                    400:                                          (Declaration)elem->elem.component.declaration);
1.11      kia       401:         break;
                    402:       default:
                    403:         break;
                    404:     }
                    405:   }
1.3       kia       406: #endif /* TEMPLATES */
1.11      kia       407: 
1.12      vatton    408:   if (newEl)
1.16      vatton    409:     TtaSelectElement (doc, newEl);
1.3       kia       410: }

Webmaster