Comment node. Extra root level for XML parsed data1
[yaz-moved-to-github.git] / retrieval / d1_write.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: d1_write.c,v 1.12 2002-05-21 07:43:16 adam Exp $
7  */
8
9 #include <string.h>
10
11 #include <yaz/data1.h>
12 #include <yaz/wrbuf.h>
13
14 #define IDSGML_MARGIN 75
15
16 static int wordlen(char *b, int max)
17 {
18     int l = 0;
19
20     while (l < max && !d1_isspace(*b))
21         l++, b++;
22     return l;
23 }
24
25 static int nodetoidsgml(data1_node *n, int select, WRBUF b, int col)
26 {
27     data1_node *c;
28     char line[1024];
29
30     for (c = n->child; c; c = c->next)
31     {
32         char *tag;
33
34         if (c->which == DATA1N_tag)
35         {
36             if (select && c->u.tag.node_selected)
37                 continue;
38             tag = c->u.tag.tag;
39             if (!data1_matchstr(tag, "wellknown")) /* skip wellknown */
40             {
41                 if (nodetoidsgml(c, select, b, col) < 0)
42                     return -1;
43             }
44             else
45             {
46                 data1_xattr *p;
47
48                 sprintf (line, "%*s<", col, "");
49                 wrbuf_puts (b, line);
50                 wrbuf_puts (b, tag);
51                 for (p = c->u.tag.attributes; p; p = p->next)
52                 {
53                     wrbuf_putc (b, ' ');
54                     wrbuf_puts (b, p->name);
55                     wrbuf_putc (b, '=');
56                     wrbuf_putc (b, '"');
57                     wrbuf_puts (b, p->value);
58                     wrbuf_putc (b, '"');
59                 }
60                 wrbuf_puts(b, ">\n");
61                 if (nodetoidsgml(c, select, b, (col > 40) ? 40 : col+2) < 0)
62                     return -1;
63                 sprintf (line, "%*s</%s>\n", col, "", tag);
64                 wrbuf_write(b, line, strlen(line));
65             }
66         }
67         else if (c->which == DATA1N_data || c->which == DATA1N_comment)
68         {
69             char *p = c->u.data.data;
70             int l = c->u.data.len;
71             int first = 1;
72             int lcol = col;
73
74             if (!c->u.data.formatted_text)
75             {
76                 sprintf(line, "%*s", col, "");
77                 wrbuf_write(b, line, strlen(line));
78             }
79             if (c->which == DATA1N_comment)
80             {
81                 wrbuf_write (b, "<!--", 4);
82             }
83             switch (c->u.data.what)
84             {
85             case DATA1I_text:
86                 if (c->u.data.formatted_text)
87                 {
88                     wrbuf_write (b, p, l);
89                 }
90                 else
91                 {
92                     while (l)
93                     {
94                         int wlen;
95                         
96                         while (l && d1_isspace(*p))
97                             p++, l--;
98                         if (!l)
99                             break;
100                         /* break if we cross margin and word is not too long */
101                         if (lcol + (wlen = wordlen(p, l)) > IDSGML_MARGIN &&
102                             wlen < IDSGML_MARGIN)
103                         {
104                             sprintf(line, "\n%*s", col, "");
105                             lcol = col;
106                             wrbuf_write(b, line, strlen(line));
107                             first = 1;
108                         }
109                         if (!first)
110                         {
111                             wrbuf_putc(b, ' ');
112                             lcol++;
113                         }
114                         while (l && !d1_isspace(*p))
115                         {
116                             wrbuf_putc(b, *p);
117                             p++;
118                             l--;
119                             lcol++;
120                         }
121                         first = 0;
122                     }
123                     wrbuf_write(b, "\n", 1);
124                 }
125                 break;
126             case DATA1I_num:
127                 wrbuf_write(b, c->u.data.data, c->u.data.len);
128                 break;
129             case DATA1I_oid:
130                 wrbuf_write(b, c->u.data.data, c->u.data.len);
131             }
132             if (c->which == DATA1N_comment)
133             {
134                 wrbuf_write (b, "-->", 3);
135             }
136         }
137     }
138     return 0;
139 }
140
141 char *data1_nodetoidsgml (data1_handle dh, data1_node *n, int select, int *len)
142 {
143     WRBUF b = data1_get_wrbuf (dh);
144     char line[1024];
145     
146     wrbuf_rewind(b);
147     
148     sprintf(line, "<%s>\n", n->u.root.type);
149     wrbuf_write(b, line, strlen(line));
150     if (nodetoidsgml(n, select, b, 0))
151         return 0;
152     sprintf(line, "</%s>\n", n->u.root.type);
153     wrbuf_write(b, line, strlen(line));
154     *len = wrbuf_len(b);
155     return wrbuf_buf(b);
156 }