Return to insertelem.c CVS log | Up to [Public] / Amaya / amaya |
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; 112: if (dec->declaredIn->isPredefined) 1.1 kia 113: { 1.11 kia 114: // DLList_Append(list, ElemListElement_CreateComponent(level, dec->name, 115: // (void*)dec, resolvedPath, elem)); 1.1 kia 116: } 1.8 vatton 117: else if (dec->nature==ComponentNat) 1.1 kia 118: { 1.6 vatton 119: DLList_Append(list, ElemListElement_CreateComponent(level, dec->name, (void*)dec, 120: resolvedPath, elem)); 1.1 kia 121: } 1.8 vatton 122: else if (dec->nature==UnionNat) 1.1 kia 123: { 1.13 ! kia 124: DLList tempList = ElemList_Create(); ! 125: ForwardIterator iter = HashMap_GetForwardIterator(dec->unionType.include); ! 126: HashMapNode mapnode; ! 127: DLListNode listnode; ! 128: 1.1 kia 129: int len1 = 0 , len2 = strlen(dec->name); 1.8 vatton 130: if (resolvedPath!=NULL) 1.1 kia 131: len1 = strlen(resolvedPath); 132: char* newPath = (char*)TtaGetMemory(len1+len2+2); 1.8 vatton 133: if (len1>0) 1.1 kia 134: { 135: strcpy(newPath, resolvedPath); 136: newPath[len1] = '/'; 137: strcpy(newPath+len1+1, dec->name); 138: } 139: else 140: { 141: strcpy(newPath, dec->name); 142: } 143: 1.13 ! kia 144: ITERATOR_FOREACH(iter, DLListNode, listnode) ! 145: { ! 146: FillUnionResolvedPossibleElement(t, (char*)mapnode->key, elem, newPath, tempList, level); ! 147: } ! 148: TtaFreeMemory(iter); ! 149: ! 150: iter = DLList_GetForwardIterator(tempList); ! 151: 1.1 kia 152: 1.13 ! kia 153: listnode = (DLListNode) ForwardIterator_GetFirst(iter); ! 154: for(listnode = (DLListNode) ForwardIterator_GetFirst(iter); listnode; ! 155: listnode = (DLListNode) ForwardIterator_GetNext(iter)) ! 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: FillInsertableTemplateElementFromStringList 196: Fill an element list with all possible elements extracted from a stringlist. 197: ----------------------------------------------------------------------*/ 1.6 vatton 198: static void FillInsertableTemplateElementFromStringList(XTigerTemplate t, 1.12 vatton 199: Element refelem, 200: const char* strlist, 1.6 vatton 201: DLList list, int level) 1.1 kia 202: { 203: int pos = 0, 204: offset = 0; 205: int size = strlen(strlist); 206: char* types = strdup(strlist); 207: while(pos<size) 208: { 1.8 vatton 209: if (isEOSorWhiteSpace(types[pos])) 1.1 kia 210: { 1.8 vatton 211: if (pos>offset) 1.1 kia 212: { 213: types[pos] = 0; 214: FillUnionResolvedPossibleElement(t, types+offset, refelem, NULL, list, level); 215: } 216: offset = pos+1; 217: } 218: pos++; 219: } 1.8 vatton 220: if (pos>offset) 1.1 kia 221: { 222: types[pos] = 0; 223: FillUnionResolvedPossibleElement(t, types+offset, refelem, NULL, list, level); 224: } 225: TtaFreeMemory(types); 226: } 227: 228: 229: /*---------------------------------------------------------------------- 230: FillInsertableElementFromElemAttribute 231: Fill an element list with all possible elements from an attribute list. 232: ----------------------------------------------------------------------*/ 1.6 vatton 233: static void FillInsertableElementFromElemAttribute(XTigerTemplate t, 234: Element elem, Element refelem, 235: int attrib, DLList list, int level) 1.1 kia 236: { 237: ElementType type = TtaGetElementType(elem); 238: AttributeType attributeType = {type.ElSSchema, attrib}; 239: Attribute att = TtaGetAttribute (elem, attributeType); 240: int size = TtaGetTextAttributeLength (att); 241: char* types = (char *) TtaGetMemory (size+1); 1.6 vatton 242: 1.1 kia 243: TtaGiveTextAttributeValue (att, types, &size); 1.6 vatton 244: FillInsertableTemplateElementFromStringList (t, refelem, types, list, level); 245: TtaFreeMemory (types); 1.1 kia 246: } 247: #endif/* TEMPLATES */ 248: 249: /*---------------------------------------------------------------------- 250: FillInsertableElemList 251: Fill an element list with all insertable elements (base element or 252: XTiger comonent). 253: ----------------------------------------------------------------------*/ 254: static void FillInsertableElemList(Document doc, Element elem, DLList list) 255: { 1.6 vatton 256: ElementType type; 1.4 cvs 257: #ifdef TEMPLATES 1.6 vatton 258: Element child; 259: ElementType childType; 260: XTigerTemplate t; 1.4 cvs 261: #endif/* TEMPLATES */ 1.2 cvs 262: int level; 263: ThotBool cont; 264: 1.8 vatton 265: if (elem){ 266: if (doc==0) 1.1 kia 267: doc = TtaGetDocument(elem); 268: 269: #ifdef TEMPLATES 1.13 ! kia 270: t = GetXTigerTemplate(DocumentMeta[doc]->template_url); 1.1 kia 271: #endif/* TEMPLATES */ 272: 1.2 cvs 273: level = 0; 274: cont = TRUE; 1.1 kia 275: 276: while(elem!=NULL && cont) 277: { 1.4 cvs 278: type = TtaGetElementType(elem); 1.1 kia 279: switch(type.ElTypeNum) 280: { 281: #ifdef TEMPLATES 282: case Template_EL_repeat: 283: child = TtaGetFirstChild(elem); 284: childType = TtaGetElementType(child); 285: switch(childType.ElTypeNum) 286: { 287: case Template_EL_useEl: 1.6 vatton 288: FillInsertableElementFromElemAttribute(t, child, elem, 289: Template_ATTR_types, list, level); 1.3 kia 290: break; 1.1 kia 291: case Template_EL_useSimple: 1.6 vatton 292: FillInsertableElementFromElemAttribute(t, child, elem, 293: Template_ATTR_types, list, level); 1.1 kia 294: break; 295: case Template_EL_bag: 1.6 vatton 296: FillInsertableElementFromElemAttribute(t, child, elem, 297: Template_ATTR_types, list, level); 1.1 kia 298: break; 299: default: 300: break; 301: } 1.7 kia 302: cont = TRUE; 1.1 kia 303: break; 304: case Template_EL_useEl: 305: // Fill for xt:use only if have no child. 1.8 vatton 306: if (TtaGetFirstChild(elem)==NULL){ 1.6 vatton 307: FillInsertableElementFromElemAttribute(t, elem, elem, 308: Template_ATTR_types, list, level); 1.7 kia 309: cont = TRUE; 1.1 kia 310: } 311: break; 312: case Template_EL_bag: 1.6 vatton 313: FillInsertableElementFromElemAttribute(t, elem, elem, 314: Template_ATTR_types, list, level); 1.7 kia 315: cont = TRUE; 1.1 kia 316: break; 317: #endif /*TEMPLATES */ 318: default: 319: break; 320: } 321: 322: elem = TtaGetParent(elem); 323: level ++; 324: } 325: } 326: } 327: 328: /*---------------------------------------------------------------------- 329: InsertableElement_GetList 330: Get the insertable element list for a document. 331: @param doc Document 332: @return The insertable element list or NULL. 333: ----------------------------------------------------------------------*/ 334: DLList InsertableElement_GetList(Document doc) 335: { 1.12 vatton 336: InsertableElementList list = (InsertableElementList) HashMap_Get(InsertableElementMap, 337: (void*)doc); 1.6 vatton 338: if (list) 1.1 kia 339: return list->list; 340: else 341: return NULL; 342: } 343: 344: /*---------------------------------------------------------------------- 345: InsertableElement_Update 346: Update the insertable element list for a document. 347: @param el Selected element, cant be NULL. 348: @param document Document, can be NULL. 349: @param force No dont force the refresh of the list if the element is already selected. 350: @return List of insertable elements. 351: ----------------------------------------------------------------------*/ 1.7 kia 352: DLList InsertableElement_Update(Document doc, Element el) 1.1 kia 353: { 1.2 cvs 354: InsertableElementList list; 1.8 vatton 355: if (doc==0) 1.1 kia 356: doc= TtaGetDocument(el); 1.2 cvs 357: list = (InsertableElementList) HashMap_Get(InsertableElementMap, (void*)doc); 1.8 vatton 358: if (list==NULL) 1.1 kia 359: { 360: list = InsertableElementList_Create(0, DLList_Create()); 361: HashMap_Set(InsertableElementMap, (void*)doc, list); 362: } 1.7 kia 363: 364: DLList_Empty(list->list); 365: FillInsertableElemList(doc, el, list->list); 366: list->elem = el; 367: 1.1 kia 368: return list->list; 369: } 370: 1.3 kia 371: /*---------------------------------------------------------------------- 372: InsertableElement_DoInsertElement 373: Insert the specified element. 374: @param el Element to insert (ElemListElement) 375: ----------------------------------------------------------------------*/ 376: void InsertableElement_DoInsertElement(void* el) 377: { 378: ElemListElement elem = (ElemListElement)el; 379: Element ref = elem->refElem; 380: ElementType refType = TtaGetElementType(ref); 381: Document doc = TtaGetDocument(ref); 1.10 kia 382: Element newEl = NULL; 1.11 kia 383: SSchema templateSSchema; 1.10 kia 384: 1.6 vatton 385: #ifdef AMAYA_DEBUG 1.12 vatton 386: printf("insert %s into %s\n", ElemListElement_GetName(elem), 387: TtaGetElementTypeName(refType)); 1.6 vatton 388: #endif /* AMAYA_DEBUG */ 1.11 kia 389: 390: #ifdef TEMPLATES 391: templateSSchema = TtaGetSSchema("Template", doc); 392: if(templateSSchema && refType.ElSSchema==templateSSchema) 1.3 kia 393: { 1.11 kia 394: switch(refType.ElTypeNum) 395: { 396: case Template_EL_repeat: 397: if (elem->typeClass==DefinedComponent) 1.12 vatton 398: newEl = Template_InsertRepeatChild(doc, ref, 399: (Declaration)elem->elem.component.declaration, 400: -1); 1.11 kia 401: break; 402: case Template_EL_bag: 1.12 vatton 403: newEl = Template_InsertBagChild(doc, ref, 404: (Declaration)elem->elem.component.declaration); 1.11 kia 405: break; 406: default: 407: break; 408: } 409: } 1.3 kia 410: #endif /* TEMPLATES */ 1.11 kia 411: 1.12 vatton 412: if (newEl) 1.10 kia 413: TtaSelectElement(doc, newEl); 1.3 kia 414: }