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 "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: 1.6 vatton 56: /*---------------------------------------------------------------------- 57: ----------------------------------------------------------------------*/ 1.1 kia 58: static InsertableElementList InsertableElementList_Create(Element elem, DLList list) 59: { 60: InsertableElementList lst = (InsertableElementList)TtaGetMemory(sizeof(sInsertableElementList)); 61: lst->elem = elem; 62: lst->list = list; 63: return lst; 64: } 65: 66: static void InsertableElementList_Destroy(InsertableElementList list) 67: { 1.8 vatton 68: if (list->list) 1.1 kia 69: DLList_Destroy(list->list); 70: TtaFreeMemory(list); 71: } 72: 73: /*---------------------------------------------------------------------- 74: InsertableElement_Init 75: Initialize the module. 76: ----------------------------------------------------------------------*/ 77: void InsertableElement_Init() 78: { 1.8 vatton 79: if (!InsertableElementMap) 1.1 kia 80: InsertableElementMap = PointerHashMap_Create((Container_DestroyElementFunction)InsertableElementList_Destroy, 32); 81: } 82: 83: /*---------------------------------------------------------------------- 84: InsertableElement_Final 85: Finalize the module. 86: ----------------------------------------------------------------------*/ 87: void InsertableElement_Final() 88: { 1.8 vatton 89: if (InsertableElementMap) 1.1 kia 90: { 91: HashMap_Destroy(InsertableElementMap); 92: InsertableElementMap = NULL; 93: } 94: } 95: 96: 97: #ifdef TEMPLATES 98: /*---------------------------------------------------------------------- 99: FillUnionResolvedPossibleElement 100: Fill an element list with all possible element, resolving them if union. 101: @param name Element name 102: @param elem Document element to attach 103: @param resolvedPath Path of different succesive union name. 104: @param list List to fill. 105: ----------------------------------------------------------------------*/ 1.6 vatton 106: static void FillUnionResolvedPossibleElement(XTigerTemplate t, const char* name, 107: Element elem, const char* resolvedPath, 108: DLList list, int level) 1.1 kia 109: { 1.5 kia 110: Declaration dec = Template_GetDeclaration (t, name); 1.8 vatton 111: if (dec == NULL) 112: return; 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: { 125: DLList tempList = ElemList_Create(); 126: 127: Record first = dec->unionType.include->first; 128: Record rec = first; 129: 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: 145: while(rec) 146: { 147: FillUnionResolvedPossibleElement(t, rec->key, elem, newPath, tempList, level); 148: rec = rec->next; 149: } 150: 151: ForwardIterator iter = DLList_GetForwardIterator(tempList); 152: DLListNode node = (DLListNode) ForwardIterator_GetFirst(iter); 153: while(node) 154: { 155: DLList_Append(list, node->elem); 156: node = (DLListNode) ForwardIterator_GetNext(iter); 157: } 158: tempList->destroyElement = NULL; 159: DLList_Destroy(tempList); 160: 161: TtaFreeMemory(newPath); 162: 163: /** todo Remove excluded elements.*/ 164: } 1.8 vatton 165: else if (dec->nature==SimpleTypeNat) 1.1 kia 166: { 167: DLList_Append(list, ElemListElement_CreateBaseType(level, dec->name, resolvedPath, elem)); 168: /* Do nothing. */ 169: } 170: else 171: { 172: /* Search in tgt std elements. */ 173: int xmlType; /* See parser.h */ 174: for(xmlType=XHTML_TYPE; xmlType<Template_TYPE; xmlType++) 175: { 176: ElementType elType = {0,0}; 177: char* mappedName; 1.2 cvs 178: char content; 1.1 kia 179: ThotBool checkProfile; 1.6 vatton 180: MapXMLElementType(xmlType, dec->name, &elType, &mappedName, &content, 181: &checkProfile, TtaGetDocument(elem)); 1.8 vatton 182: if (elType.ElTypeNum!=0) 1.1 kia 183: { 184: DLList_Append(list, ElemListElement_CreateLanguageElement(level, elType, resolvedPath, elem)); 185: break; 186: } 187: } 188: } 189: } 190: 191: /*---------------------------------------------------------------------- 192: FillInsertableTemplateElementFromStringList 193: Fill an element list with all possible elements extracted from a stringlist. 194: ----------------------------------------------------------------------*/ 1.6 vatton 195: static void FillInsertableTemplateElementFromStringList(XTigerTemplate t, 196: Element refelem, const char* strlist, 197: DLList list, int level) 1.1 kia 198: { 199: int pos = 0, 200: offset = 0; 201: int size = strlen(strlist); 202: char* types = strdup(strlist); 203: while(pos<size) 204: { 1.8 vatton 205: if (isEOSorWhiteSpace(types[pos])) 1.1 kia 206: { 1.8 vatton 207: if (pos>offset) 1.1 kia 208: { 209: types[pos] = 0; 210: FillUnionResolvedPossibleElement(t, types+offset, refelem, NULL, list, level); 211: } 212: offset = pos+1; 213: } 214: pos++; 215: } 1.8 vatton 216: if (pos>offset) 1.1 kia 217: { 218: types[pos] = 0; 219: FillUnionResolvedPossibleElement(t, types+offset, refelem, NULL, list, level); 220: } 221: TtaFreeMemory(types); 222: } 223: 224: 225: /*---------------------------------------------------------------------- 226: FillInsertableElementFromElemAttribute 227: Fill an element list with all possible elements from an attribute list. 228: ----------------------------------------------------------------------*/ 1.6 vatton 229: static void FillInsertableElementFromElemAttribute(XTigerTemplate t, 230: Element elem, Element refelem, 231: int attrib, DLList list, int level) 1.1 kia 232: { 233: ElementType type = TtaGetElementType(elem); 234: AttributeType attributeType = {type.ElSSchema, attrib}; 235: Attribute att = TtaGetAttribute (elem, attributeType); 236: int size = TtaGetTextAttributeLength (att); 237: char* types = (char *) TtaGetMemory (size+1); 1.6 vatton 238: 1.1 kia 239: TtaGiveTextAttributeValue (att, types, &size); 1.6 vatton 240: FillInsertableTemplateElementFromStringList (t, refelem, types, list, level); 241: TtaFreeMemory (types); 1.1 kia 242: } 243: #endif/* TEMPLATES */ 244: 245: /*---------------------------------------------------------------------- 246: FillInsertableElemList 247: Fill an element list with all insertable elements (base element or 248: XTiger comonent). 249: ----------------------------------------------------------------------*/ 250: static void FillInsertableElemList(Document doc, Element elem, DLList list) 251: { 1.6 vatton 252: ElementType type; 1.4 cvs 253: #ifdef TEMPLATES 1.6 vatton 254: Element child; 255: ElementType childType; 256: XTigerTemplate t; 1.4 cvs 257: #endif/* TEMPLATES */ 1.2 cvs 258: int level; 259: ThotBool cont; 260: 1.8 vatton 261: if (elem){ 262: if (doc==0) 1.1 kia 263: doc = TtaGetDocument(elem); 264: 265: #ifdef TEMPLATES 1.6 vatton 266: t = (XTigerTemplate) Dictionary_Get (Templates_Dic, DocumentMeta[doc]->template_url); 1.1 kia 267: #endif/* TEMPLATES */ 268: 1.2 cvs 269: level = 0; 270: cont = TRUE; 1.1 kia 271: 272: while(elem!=NULL && cont) 273: { 1.4 cvs 274: type = TtaGetElementType(elem); 1.1 kia 275: switch(type.ElTypeNum) 276: { 277: #ifdef TEMPLATES 278: case Template_EL_repeat: 279: child = TtaGetFirstChild(elem); 280: childType = TtaGetElementType(child); 281: switch(childType.ElTypeNum) 282: { 283: case Template_EL_useEl: 1.6 vatton 284: FillInsertableElementFromElemAttribute(t, child, elem, 285: Template_ATTR_types, list, level); 1.3 kia 286: break; 1.1 kia 287: case Template_EL_useSimple: 1.6 vatton 288: FillInsertableElementFromElemAttribute(t, child, elem, 289: Template_ATTR_types, list, level); 1.1 kia 290: break; 291: case Template_EL_bag: 1.6 vatton 292: FillInsertableElementFromElemAttribute(t, child, elem, 293: Template_ATTR_types, list, level); 1.1 kia 294: break; 295: default: 296: break; 297: } 1.7 kia 298: cont = TRUE; 1.1 kia 299: break; 300: case Template_EL_useEl: 301: // Fill for xt:use only if have no child. 1.8 vatton 302: if (TtaGetFirstChild(elem)==NULL){ 1.6 vatton 303: FillInsertableElementFromElemAttribute(t, elem, elem, 304: Template_ATTR_types, list, level); 1.7 kia 305: cont = TRUE; 1.1 kia 306: } 307: break; 308: case Template_EL_bag: 1.6 vatton 309: FillInsertableElementFromElemAttribute(t, elem, elem, 310: Template_ATTR_types, list, level); 1.7 kia 311: cont = TRUE; 1.1 kia 312: break; 313: #endif /*TEMPLATES */ 314: default: 315: break; 316: } 317: 318: elem = TtaGetParent(elem); 319: level ++; 320: } 321: } 322: } 323: 324: /*---------------------------------------------------------------------- 325: InsertableElement_GetList 326: Get the insertable element list for a document. 327: @param doc Document 328: @return The insertable element list or NULL. 329: ----------------------------------------------------------------------*/ 330: DLList InsertableElement_GetList(Document doc) 331: { 332: InsertableElementList 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.8 vatton 350: if (doc==0) 1.1 kia 351: doc= TtaGetDocument(el); 1.2 cvs 352: list = (InsertableElementList) HashMap_Get(InsertableElementMap, (void*)doc); 1.8 vatton 353: if (list==NULL) 1.1 kia 354: { 355: list = InsertableElementList_Create(0, DLList_Create()); 356: HashMap_Set(InsertableElementMap, (void*)doc, list); 357: } 1.7 kia 358: 359: DLList_Empty(list->list); 360: FillInsertableElemList(doc, el, list->list); 361: list->elem = el; 362: 1.1 kia 363: return list->list; 364: } 365: 1.3 kia 366: /*---------------------------------------------------------------------- 367: InsertableElement_DoInsertElement 368: Insert the specified element. 369: @param el Element to insert (ElemListElement) 370: ----------------------------------------------------------------------*/ 371: void InsertableElement_DoInsertElement(void* el) 372: { 373: ElemListElement elem = (ElemListElement)el; 374: Element ref = elem->refElem; 375: ElementType refType = TtaGetElementType(ref); 376: Document doc = TtaGetDocument(ref); 1.10 kia 377: Element newEl = NULL; 1.11 ! kia 378: SSchema templateSSchema; 1.10 kia 379: 1.6 vatton 380: #ifdef AMAYA_DEBUG 1.3 kia 381: printf("insert %s into %s\n", ElemListElement_GetName(elem), TtaGetElementTypeName(refType)); 1.6 vatton 382: #endif /* AMAYA_DEBUG */ 1.11 ! kia 383: ! 384: #ifdef TEMPLATES ! 385: templateSSchema = TtaGetSSchema("Template", doc); ! 386: if(templateSSchema && refType.ElSSchema==templateSSchema) 1.3 kia 387: { 1.11 ! kia 388: switch(refType.ElTypeNum) ! 389: { ! 390: case Template_EL_repeat: ! 391: if (elem->typeClass==DefinedComponent) ! 392: newEl = Template_InsertRepeatChild(doc, ref, (Declaration)elem->elem.component.declaration, -1); ! 393: break; ! 394: case Template_EL_bag: ! 395: newEl = Template_InsertBagChild(doc, ref, (Declaration)elem->elem.component.declaration); ! 396: break; ! 397: default: ! 398: break; ! 399: } ! 400: } 1.3 kia 401: #endif /* TEMPLATES */ 1.11 ! kia 402: 1.10 kia 403: if(newEl) 404: { 405: TtaSelectElement(doc, newEl); 406: TtaSetStatusSelectedElement(doc, 1, newEl); 407: } 1.3 kia 408: }