Annotation of Amaya/amaya/templateUtils.c, revision 1.43
1.9 vatton 1: /*
2: *
1.37 vatton 3: * COPYRIGHT INRIA and W3C, 2006-2009
1.9 vatton 4: * Please first read the full copyright statement in file COPYRIGHT.
5: *
6: */
7:
1.3 quint 8: #include "templates.h"
1.14 kia 9: #include "Templatename.h"
10: #include "templates_f.h"
1.1 francesc 11:
1.21 kia 12: #include "AHTURLTools_f.h"
13: #include "HTMLsave_f.h"
14:
15:
1.19 kia 16: #include <stdarg.h>
17:
1.1 francesc 18: /*----------------------------------------------------------------------
1.30 kia 19: GetSchemaFromDocType: Returns the name of the schema corresponding to
1.1 francesc 20: a doc type.
21: ----------------------------------------------------------------------*/
1.24 kia 22: const char *GetSchemaFromDocType (DocumentType docType)
1.1 francesc 23: {
24: #ifdef TEMPLATES
25: switch (docType)
26: {
27: case docAnnot :
28: return "Annot";
29: case docBookmark :
30: return "Topics";
31: case docSVG :
32: return "SVG";
33: case docMath :
34: return "MathML";
35: case docXml :
36: return "XML";
37: default :
38: return "HTML";
39: }
40: #endif // TEMPLATES
41: return "HTML";
42: }
43:
1.5 kia 44: /*----------------------------------------------------------------------
1.30 kia 45: Set the value of a string attribute
1.5 kia 46: ----------------------------------------------------------------------*/
1.24 kia 47: void SetAttributeStringValue (Element el, int att, const char* value)
1.5 kia 48: {
49: #ifdef TEMPLATES
1.7 vatton 50: Document doc = TtaGetDocument(el);
51: AttributeType attType;
52: Attribute attribute;
1.5 kia 53:
1.10 kia 54: if (doc == 0 || !TtaGetDocumentAccessMode(doc))
1.9 vatton 55: return;
1.5 kia 56: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
57: attType.AttrTypeNum = att;
1.7 vatton 58: attribute = TtaGetAttribute(el, attType);
59: if (attribute == NULL)
60: {
61: attribute = TtaNewAttribute (attType);
62: TtaAttachAttribute(el, attribute, doc);
63: }
1.5 kia 64: TtaSetAttributeText(attribute, value, el, doc);
65: #endif /* TEMPLATES */
66: }
67:
1.11 kia 68: /*----------------------------------------------------------------------
69: Set the value of a string attribute and registering it in undo sequence.
70: ----------------------------------------------------------------------*/
71: void SetAttributeStringValueWithUndo (Element el, int att, char* value)
72: {
73: #ifdef TEMPLATES
74: Document doc = TtaGetDocument(el);
75: AttributeType attType;
76: Attribute attribute;
77:
78: if (doc == 0 || !TtaGetDocumentAccessMode(doc))
79: return;
80: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
81: attType.AttrTypeNum = att;
82: attribute = TtaGetAttribute(el, attType);
83: if (attribute == NULL)
84: {
85: attribute = TtaNewAttribute (attType);
86: TtaAttachAttribute(el, attribute, doc);
87: TtaRegisterAttributeCreate(attribute, el, doc);
88: }
89: TtaSetAttributeText(attribute, value, el, doc);
90: TtaRegisterAttributeReplace(attribute, el, doc);
91: #endif /* TEMPLATES */
92: }
93:
1.13 kia 94: /*----------------------------------------------------------------------
1.30 kia 95: Returns the value of a string attribute without copy it
1.13 kia 96: ----------------------------------------------------------------------*/
97: void GiveAttributeStringValueFromNum (Element el, int att, char* buff, int* sz)
98: {
99: #ifdef TEMPLATES
100: AttributeType attType;
101: Attribute attribute;
102: int size;
103:
104: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
105: attType.AttrTypeNum = att;
106: attribute = TtaGetAttribute(el, attType);
1.30 kia 107:
1.13 kia 108: size = TtaGetTextAttributeLength(attribute);
109: TtaGiveTextAttributeValue (attribute, buff, &size);
110: buff[size] = EOS;
1.37 vatton 111: if (sz)
1.13 kia 112: *sz = size;
113: #endif /* TEMPLATES */
114: }
115:
116:
1.1 francesc 117:
118: /*----------------------------------------------------------------------
1.31 vatton 119: Returns the value of a string attribute or NULL
1.1 francesc 120: ----------------------------------------------------------------------*/
1.8 kia 121: char *GetAttributeStringValueFromNum (Element el, int att, int* sz)
1.1 francesc 122: {
123: #ifdef TEMPLATES
124: AttributeType attType;
1.12 vatton 125: Attribute attribute;
126: char *aux;
127: int size;
128:
1.1 francesc 129: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
130: attType.AttrTypeNum = att;
1.12 vatton 131: attribute = TtaGetAttribute(el, attType);
1.31 vatton 132: if (attribute == NULL)
133: return NULL;
1.30 kia 134:
1.31 vatton 135: size = TtaGetTextAttributeLength (attribute);
136: aux = (char*) TtaGetMemory (size+1);
1.1 francesc 137: TtaGiveTextAttributeValue (attribute, aux, &size);
1.12 vatton 138: aux[size] = EOS;
1.31 vatton 139: if (sz)
1.4 kia 140: *sz = size;
1.1 francesc 141: return aux;
142: #else
1.8 kia 143: return NULL;
1.1 francesc 144: #endif /* TEMPLATES */
145: }
1.2 francesc 146:
147: /*----------------------------------------------------------------------
1.30 kia 148: Returns the value of an int attribute
149: ----------------------------------------------------------------------*/
150: int GetAttributeIntValueFromNum (Element el, int att)
151: {
152: #ifdef TEMPLATES
153: AttributeType attType;
154: Attribute attribute;
155:
156: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
157: attType.AttrTypeNum = att;
158: attribute = TtaGetAttribute(el, attType);
159:
160: return TtaGetAttributeValue(attribute);
161: #else
162: return NULL;
163: #endif /* TEMPLATES */
164: }
165:
166: /*----------------------------------------------------------------------
167: Set the value of a int attribute and registering it in undo sequence if wanted
168: ----------------------------------------------------------------------*/
169: void SetAttributeIntValue (Element el, int att, int value, ThotBool undo)
170: {
171: #ifdef TEMPLATES
172: Document doc = TtaGetDocument(el);
173: AttributeType attType;
174: Attribute attribute;
175:
176: if (doc == 0 || !TtaGetDocumentAccessMode(doc))
177: return;
178: attType.AttrSSchema = TtaGetElementType(el).ElSSchema;
179: attType.AttrTypeNum = att;
180: attribute = TtaGetAttribute(el, attType);
181: if (attribute == NULL)
182: {
183: attribute = TtaNewAttribute (attType);
184: TtaAttachAttribute(el, attribute, doc);
1.37 vatton 185: if (undo)
1.30 kia 186: TtaRegisterAttributeCreate(attribute, el, doc);
187: }
188: TtaSetAttributeValue(attribute, value, el, doc);
1.37 vatton 189: if (undo)
1.30 kia 190: TtaRegisterAttributeReplace(attribute, el, doc);
191: #endif /* TEMPLATES */
192: }
193:
194:
195:
196:
197: /*----------------------------------------------------------------------
198: Returns the value of a string attribute
1.2 francesc 199: ----------------------------------------------------------------------*/
1.4 kia 200: char *GetAttributeStringValue (Element el, Attribute attribute, int* sz)
1.2 francesc 201: {
202: #ifdef TEMPLATES
203: int size = TtaGetTextAttributeLength(attribute);
204: char *aux = (char*) TtaGetMemory(size+1);
1.12 vatton 205:
1.2 francesc 206: TtaGiveTextAttributeValue (attribute, aux, &size);
1.12 vatton 207: aux[size] = EOS;
1.37 vatton 208: if (sz)
1.4 kia 209: *sz = size;
1.2 francesc 210: return aux;
211: #else
1.8 kia 212: return NULL;
1.2 francesc 213: #endif /* TEMPLATES */
214: }
1.6 kia 215:
1.31 vatton 216: /*----------------------------------------------------------------------
217: GetAncestorComponentName returns the name of the ancestor component
218: or NULL;
219: ----------------------------------------------------------------------*/
220: char *GetAncestorComponentName (Element *el)
221: {
222: #ifdef TEMPLATES
223: ElementType elType;
224: Element anc = NULL;
225: char *name;
226:
227: elType = TtaGetElementType (*el);
228: elType.ElTypeNum = Template_EL_component;
229: anc = TtaGetParent (*el);
230: anc = TtaGetExactTypedAncestor (anc, elType);
231: if (anc)
232: {
1.32 kia 233: name = GetAttributeStringValueFromNum (anc, Template_ATTR_name, NULL);
1.31 vatton 234: *el = anc;
235: return name;
236: }
1.32 kia 237: #endif /* TEMPLATES */
1.31 vatton 238: return NULL;
239: }
240:
1.6 kia 241:
242: /*----------------------------------------------------------------------
243: GetFirstEditableElement
244: Returns the first descendant element which is modifiable.
245: ----------------------------------------------------------------------*/
246: Element GetFirstEditableElement (Element el)
247: {
1.38 vatton 248: ElementType elType;
249: Element res = NULL, current;
1.30 kia 250:
1.38 vatton 251: current = TtaGetFirstChild(el);
1.37 vatton 252: while (!res && current)
1.38 vatton 253: {
254: // skip col colgroup
255: elType = TtaGetElementType (current);
256: if ((elType.ElSSchema &&
257: strcmp (TtaGetSSchemaName(elType.ElSSchema), "HTML")) ||
258: (elType.ElTypeNum != HTML_EL_ColStruct &&
259: elType.ElTypeNum != HTML_EL_Table_head &&
260: elType.ElTypeNum != HTML_EL_Comment_))
261: res = GetFirstEditableElement (current);
262: TtaNextSibling(¤t);
263: }
1.37 vatton 264: if (!res && !TtaIsReadOnly(el))
1.6 kia 265: res = el;
266: return res;
267: }
1.14 kia 268:
269: /*----------------------------------------------------------------------
1.15 kia 270: TemplateCanInsertFirstChild
271: Test if an element can be inserted as child of another, bypassing xt.
272: ----------------------------------------------------------------------*/
1.37 vatton 273: ThotBool TemplateCanInsertFirstChild (ElementType elementType, Element parent,
274: Document document)
1.15 kia 275: {
276: #ifdef TEMPLATES
277: SSchema templateSSchema = TtaGetSSchema ("Template", document);
278: ElementType parType;
1.30 kia 279:
1.37 vatton 280: while (parent)
1.15 kia 281: {
282: parType = TtaGetElementType(parent);
1.37 vatton 283: if (parType.ElSSchema != templateSSchema)
1.15 kia 284: break;
285: parent = TtaGetParent(parent);
286: }
1.37 vatton 287: if (!parent)
1.15 kia 288: return FALSE;
289: #endif /* TEMPLATES */
290: return TtaCanInsertFirstChild(elementType, parent, document);
291: }
292:
293: /*----------------------------------------------------------------------
1.37 vatton 294: CheckTemplateAttrInMenu
1.14 kia 295: Validate the status of an attribute according to xt::atribute rules.
1.37 vatton 296: Return TRUE if the attribute is not valid
1.14 kia 297: ----------------------------------------------------------------------*/
1.37 vatton 298: ThotBool CheckTemplateAttrInMenu (NotifyAttribute *event)
1.14 kia 299: {
300: #ifdef TEMPLATES
1.37 vatton 301: Document doc = event->document;
1.41 vatton 302: Element elem, parent = event->element;
1.14 kia 303: ElementType elType;
1.41 vatton 304: SSchema schema;
1.14 kia 305: AttributeType attrType;
306: Attribute attr;
1.37 vatton 307: char *attrName;
1.14 kia 308: char buffer[MAX_LENGTH];
1.37 vatton 309: int sz, useAt, type;
1.30 kia 310:
1.14 kia 311: /* Prevent from showing attributes for template instance but not templates. */
1.37 vatton 312: if (IsTemplateInstanceDocument(doc))
1.14 kia 313: {
1.41 vatton 314: schema = TtaGetSSchema ("Template", doc);
1.14 kia 315: /* Prevent if attribute's element is not a descendant of xt:use */
1.17 kia 316: /* Dont prevent if descendant of xt:bag. */
1.41 vatton 317: elem = GetFirstTemplateParentElement (parent);
318: if (elem)
319: {
320: elType = TtaGetElementType (elem);
321: if (elType.ElTypeNum == Template_EL_bag)
322: return FALSE; /* let Thot perform normal operation */
323: if (elType.ElTypeNum != Template_EL_useSimple)
324: return TRUE;
325: if (!TtaIsReadOnly (parent))
326: return FALSE; /* let Thot perform normal operation */
327: }
1.14 kia 328: /* Search for the corresponding xt:attribute element*/
1.41 vatton 329: attrName = TtaGetAttributeName (event->attributeType);
330: attrType.AttrSSchema = schema;
331: for (elem = TtaGetFirstChild (parent); elem; TtaNextSibling (&elem))
1.14 kia 332: {
333: attrType.AttrTypeNum = Template_ATTR_ref_name;
334: elType = TtaGetElementType(elem);
1.41 vatton 335: if (elType.ElTypeNum == Template_EL_attribute && elType.ElSSchema == schema)
1.14 kia 336: {
337: attr = TtaGetAttribute(elem, attrType);
1.37 vatton 338: if (attr)
1.14 kia 339: {
340: sz = MAX_LENGTH;
341: TtaGiveTextAttributeValue(attr, buffer, &sz);
1.37 vatton 342: if (!strcmp(buffer, attrName))
1.14 kia 343: {
344: /* Process the attribute filtering */
345: /* Get 'useAt' attr value. */
346: attrType.AttrTypeNum = Template_ATTR_useAt;
347: attr = TtaGetAttribute(elem, attrType);
1.37 vatton 348: if (attr)
1.14 kia 349: useAt = TtaGetAttributeValue(attr);
350: else
351: useAt = Template_ATTR_useAt_VAL_required;
1.30 kia 352: /* Get 'type' attr value. */
1.14 kia 353: attrType.AttrTypeNum = Template_ATTR_type;
354: attr = TtaGetAttribute(elem, attrType);
1.37 vatton 355: if (attr)
1.14 kia 356: type = TtaGetAttributeValue(attr);
357: else
358: type = Template_ATTR_type_VAL_string;
1.37 vatton 359:
1.14 kia 360: event->restr.RestrType = (RestrictionContentType)type;
361: /* If attr is prohibited, dont show it.*/
1.37 vatton 362: if (useAt == Template_ATTR_useAt_VAL_prohibited)
1.16 kia 363: return TRUE;
1.37 vatton 364: if (useAt == Template_ATTR_useAt_VAL_required)
1.41 vatton 365: /* Force the usage of this attribute.*/
366: event->restr.RestrFlags |= attr_mandatory;
1.14 kia 367:
368: /* Get 'fixed' attr value. */
369: attrType.AttrTypeNum = Template_ATTR_fixed;
370: attr = TtaGetAttribute(elem, attrType);
1.37 vatton 371: if (attr)
1.14 kia 372: {
373: sz = MAX_LENGTH;
374: TtaGiveTextAttributeValue(attr, buffer, &sz);
375: event->restr.RestrFlags |= attr_readonly;
376: event->restr.RestrDefVal = TtaStrdup(buffer);
1.37 vatton 377: return FALSE; /* let Thot perform normal operation */
1.14 kia 378: }
379:
380: /* Get 'default' attr value.*/
381: attrType.AttrTypeNum = Template_ATTR_defaultAt;
382: attr = TtaGetAttribute(elem, attrType);
1.37 vatton 383: if (attr)
1.14 kia 384: {
385: sz = MAX_LENGTH;
386: TtaGiveTextAttributeValue(attr, buffer, &sz);
387: event->restr.RestrDefVal = TtaStrdup(buffer);
388: }
389:
390: /* Get 'values' attr value.*/
391: attrType.AttrTypeNum = Template_ATTR_values;
392: attr = TtaGetAttribute(elem, attrType);
1.37 vatton 393: if (attr)
1.14 kia 394: {
395: sz = MAX_LENGTH;
396: TtaGiveTextAttributeValue(attr, buffer, &sz);
397: event->restr.RestrEnumVal = TtaStrdup(buffer);
398: event->restr.RestrFlags |= attr_enum;
399: }
1.37 vatton 400: return FALSE; /* let Thot perform normal operation */
1.14 kia 401: }
402: }
403: }
404: }
1.16 kia 405: return TRUE;
1.14 kia 406: }
407: #endif /* TEMPLATES */
1.37 vatton 408: return FALSE;
1.14 kia 409: }
1.19 kia 410:
411: /*----------------------------------------------------------------------
1.20 kia 412: * Dump element path
413: ----------------------------------------------------------------------*/
1.37 vatton 414: void DumpElementSubPath (Element el, char* buffer)
1.20 kia 415: {
1.36 vatton 416: #ifdef TEMPLATE_DEBUG
1.20 kia 417: Element parent = TtaGetParent(el);
1.37 vatton 418: if (parent == NULL)
1.20 kia 419: strcpy(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
420: else
421: {
422: DumpElementSubPath(parent, buffer);
423: strcat(buffer, "/");
424: strcat(buffer, TtaGetElementTypeName(TtaGetElementType(el)));
425: }
1.36 vatton 426: #endif /* TEMPLATE_DEBUG */
1.20 kia 427: }
428:
429: /*----------------------------------------------------------------------
430: * Dump element path
431: ----------------------------------------------------------------------*/
1.37 vatton 432: void DumpElementPath (Element el)
1.20 kia 433: {
1.36 vatton 434: #ifdef TEMPLATE_DEBUG
1.20 kia 435: char buffer[MAX_LENGTH];
1.37 vatton 436:
1.20 kia 437: DumpElementSubPath(el, buffer);
438: printf("%s\n", buffer);
1.36 vatton 439: #endif /* TEMPLATE_DEBUG */
1.20 kia 440: }
441:
442:
443: /*----------------------------------------------------------------------
1.19 kia 444: * Dump template element
445: ----------------------------------------------------------------------*/
1.37 vatton 446: void DumpTemplateElement (Element el, Document doc)
1.19 kia 447: {
1.36 vatton 448: #ifdef TEMPLATE_DEBUG
1.28 vatton 449: ElementType elType;
450: AttributeType attType;
451: Attribute att;
452: SSchema schema = TtaGetSSchema ("Template", doc);
453: char* str;
454: char buffer[MAX_LENGTH];
455: int len;
456: Language lang;
1.30 kia 457:
1.37 vatton 458: if (el && doc)
1.19 kia 459: {
460: elType = TtaGetElementType(el);
1.26 kia 461: printf("- %p %d ", elType.ElSSchema, elType.ElTypeNum);
462: printf(" %s", TtaGetSSchemaName(elType.ElSSchema));
463: printf(":%s", TtaGetElementTypeName(elType));
1.37 vatton 464: if (elType.ElTypeNum == 1)
1.26 kia 465: {
466: len = MAX_LENGTH-1;
467: TtaGiveTextContent(el, (unsigned char*)buffer, &len, &lang);
468: buffer[len] = EOS;
469: printf(" \"%s\"", buffer);
470: }
1.30 kia 471:
1.37 vatton 472: if (elType.ElSSchema == schema)
1.19 kia 473: {
474: switch(elType.ElTypeNum)
475: {
476: case Template_EL_head:
477: str = GetAttributeStringValueFromNum(el, Template_ATTR_version, NULL);
478: printf(" version=%s", str);
479: TtaFreeMemory(str);
480: str = GetAttributeStringValueFromNum(el, Template_ATTR_templateVersion, NULL);
481: printf(" templateVersion=%s", str);
1.30 kia 482: TtaFreeMemory(str);
1.19 kia 483: break;
484: case Template_EL_component:
485: str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
486: printf(" name=%s", str);
487: TtaFreeMemory(str);
488: break;
489: case Template_EL_union:
490: str = GetAttributeStringValueFromNum(el, Template_ATTR_name, NULL);
491: printf(" name=%s", str);
492: TtaFreeMemory(str);
493: str = GetAttributeStringValueFromNum(el, Template_ATTR_includeAt, NULL);
494: printf(" include=%s", str);
495: TtaFreeMemory(str);
496: str = GetAttributeStringValueFromNum(el, Template_ATTR_exclude, NULL);
497: printf(" exclude=%s", str);
498: TtaFreeMemory(str);
499: break;
500: case Template_EL_import:
501: str = GetAttributeStringValueFromNum(el, Template_ATTR_src, NULL);
502: printf(" src=%s", str);
503: TtaFreeMemory(str);
504: break;
505: case Template_EL_repeat:
506: str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
507: printf(" label=%s", str);
508: TtaFreeMemory(str);
509: str = GetAttributeStringValueFromNum(el, Template_ATTR_minOccurs, NULL);
510: printf(" minOccurs=%s", str);
511: TtaFreeMemory(str);
512: str = GetAttributeStringValueFromNum(el, Template_ATTR_maxOccurs, NULL);
513: printf(" maxOccurs=%s", str);
514: TtaFreeMemory(str);
515: break;
516: case Template_EL_useSimple:
517: case Template_EL_useEl:
518: str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
1.41 vatton 519: printf (" label=%s", str);
1.19 kia 520: TtaFreeMemory(str);
521: str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
1.41 vatton 522: printf (" types=%s", str);
1.19 kia 523: TtaFreeMemory(str);
1.28 vatton 524: attType.AttrSSchema = elType.ElSSchema;
525: attType.AttrTypeNum = Template_ATTR_option;
526: att = TtaGetAttribute (el, attType);
527: if (att)
1.41 vatton 528: printf (" option");
1.19 kia 529: break;
530: case Template_EL_bag:
531: str = GetAttributeStringValueFromNum(el, Template_ATTR_title, NULL);
532: printf(" label=%s", str);
533: TtaFreeMemory(str);
534: str = GetAttributeStringValueFromNum(el, Template_ATTR_types, NULL);
535: printf(" types=%s", str);
536: TtaFreeMemory(str);
537: break;
538: case Template_EL_attribute:
539: str = GetAttributeStringValueFromNum(el, Template_ATTR_ref_name, NULL);
1.41 vatton 540: printf (" name=%s", str);
1.19 kia 541: TtaFreeMemory(str);
542: str = GetAttributeStringValueFromNum(el, Template_ATTR_type, NULL);
1.41 vatton 543: printf (" type=%s", str);
1.19 kia 544: TtaFreeMemory(str);
545: str = GetAttributeStringValueFromNum(el, Template_ATTR_useAt, NULL);
1.41 vatton 546: printf (" use=%s", str);
1.19 kia 547: TtaFreeMemory(str);
548: str = GetAttributeStringValueFromNum(el, Template_ATTR_defaultAt, NULL);
1.41 vatton 549: printf (" default=%s", str);
1.19 kia 550: TtaFreeMemory(str);
551: str = GetAttributeStringValueFromNum(el, Template_ATTR_fixed, NULL);
1.41 vatton 552: printf (" fixed=%s", str);
1.19 kia 553: TtaFreeMemory(str);
554: break;
555: }
556: }
557: }
1.36 vatton 558: #endif /* TEMPLATE_DEBUG */
1.19 kia 559: }
1.21 kia 560:
561: /*----------------------------------------------------------------------
1.25 kia 562: * Dump subtree
563: ----------------------------------------------------------------------*/
564: void DumpSubtree(Element el, Document doc, int off)
565: {
1.36 vatton 566: #ifdef TEMPLATE_DEBUG
1.25 kia 567: Element child = TtaGetFirstChild(el);
568: int i;
1.30 kia 569:
1.37 vatton 570: for (i=0; i<off; i++)
1.25 kia 571: printf(" ");
572: DumpTemplateElement(el, doc);
573: printf("\n");
574:
1.37 vatton 575: while (child)
1.25 kia 576: {
577: DumpSubtree(child, doc, off+1);
578: TtaNextSibling(&child);
579: }
1.36 vatton 580: #endif /* TEMPLATE_DEBUG */
1.25 kia 581: }
582:
583: /*----------------------------------------------------------------------
1.34 vatton 584: Save an opened document to a specified path in order to open.
585: The parameter doc is the original doc to be saved
586: The parameter newdoc is the new document
587: The parameter newpath is the newdoc URI
588: Return the saved localFile (to be freed) or NULL
1.21 kia 589: ----------------------------------------------------------------------*/
1.34 vatton 590: char *SaveDocumentToNewDoc(Document doc, Document newdoc, char* newpath)
1.21 kia 591: {
592: ElementType elType;
593: Element root;
1.34 vatton 594: char *localFile = NULL, *s;
1.21 kia 595: ThotBool res = FALSE;
1.30 kia 596:
1.21 kia 597: localFile = GetLocalPath (newdoc, newpath);
1.40 vatton 598:
599: // apply link changes to the template
600: TtaOpenUndoSequence (doc, NULL, NULL, 0, 0);
1.39 vatton 601: SetRelativeURLs (doc, newpath, NULL, FALSE, FALSE, FALSE, FALSE);
1.22 kia 602: // prepare the new document view
603: TtaExtractName (newpath, DirectoryName, DocumentName);
1.21 kia 604:
1.22 kia 605: root = TtaGetRootElement(doc);
606: elType = TtaGetElementType (root);
607: // get the target document type
608: s = TtaGetSSchemaName (elType.ElSSchema);
609: if (strcmp (s, "HTML") == 0)
610: {
611: /* docType = docHTML; */
612: if (TtaGetDocumentProfile(doc) == L_Xhtml11 ||
613: TtaGetDocumentProfile(doc) == L_Basic)
614: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLT11", FALSE);
1.21 kia 615: else
1.22 kia 616: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "HTMLTX", FALSE);
617: }
618: else if (strcmp (s, "SVG") == 0)
619: /* docType = docSVG; */
620: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "SVGT", FALSE);
621: else if (strcmp (s, "MathML") == 0)
622: /* docType = docMath; */
623: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, "MathMLT", FALSE);
624: else
625: /* docType = docXml; */
626: res = TtaExportDocumentWithNewLineNumbers (doc, localFile, NULL, FALSE);
1.40 vatton 627:
628: // restore the previous state of the template
629: TtaCloseUndoSequence (doc);
630: TtaUndoNoRedo (doc);
1.34 vatton 631: if (res)
632: return localFile;
633: else
634: {
635: TtaFreeMemory (localFile);
636: return NULL;
637: }
1.21 kia 638: }
1.29 kia 639:
640: /*----------------------------------------------------------------------
1.43 ! vatton 641: TemplateGetParentHead looks for the parent xt:head element
! 642: ----------------------------------------------------------------------*/
! 643: Element TemplateGetParentHead (Element el, Document doc)
! 644: {
! 645: #ifdef TEMPLATES
! 646: ElementType headType;
! 647: SSchema schema;
! 648:
! 649: schema = TtaGetSSchema ("Template", doc);
! 650: if (schema == TtaGetDocumentSSchema (doc))
! 651: return TtaGetMainRoot (doc);
! 652: if (schema == NULL)
! 653: // no template element in that document
! 654: return NULL;
! 655:
! 656: headType.ElTypeNum = Template_EL_head;
! 657: return TtaGetExactTypedAncestor (el, headType);
! 658: #endif /* TEMPLATES */
! 659: }
! 660:
! 661: /*----------------------------------------------------------------------
! 662: TemplateFindHead looks for the xt:head element and creates it
! 663: if it doesn't exist.
1.29 kia 664: ----------------------------------------------------------------------*/
1.35 vatton 665: Element TemplateFindHead (Document doc)
1.29 kia 666: {
667: #ifdef TEMPLATES
1.35 vatton 668: ElementType headType, elType;
669: Element head, root;
670:
1.29 kia 671: headType.ElSSchema = TtaGetSSchema ("Template", doc);
1.35 vatton 672: if (headType.ElSSchema == NULL)
673: return NULL;
674:
1.42 vatton 675: root = TtaGetMainRoot (doc);
676: elType = TtaGetElementType (root);
1.29 kia 677: headType.ElTypeNum = Template_EL_head;
1.42 vatton 678: if (elType.ElSSchema == headType.ElSSchema)
679: head = root;
680: else
681: head = TtaSearchTypedElement (headType, SearchInTree, root);
1.35 vatton 682: if (head == NULL)
683: {
684: // create the template head
685: head = TtaNewElement (doc, headType);
686: if (!strcmp (TtaGetSSchemaName (elType.ElSSchema), "HTML"))
687: {
688: elType.ElTypeNum = HTML_EL_HEAD;
689: root = TtaSearchTypedElement (elType, SearchInTree, root);
690: }
691: TtaInsertFirstChild (&head, root, doc);
692: SetAttributeStringValue (head, Template_ATTR_version, Template_Current_Version);
693: SetAttributeStringValue (head, Template_ATTR_templateVersion, "1.0");
694: }
695: return head;
1.29 kia 696: #else /* TEMPLATES */
697: return NULL;
698: #endif /* TEMPLATES */
699: }
700:
701:
Webmaster