Rev 418 | Rev 439 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
411 | Franz | 1 | /*************************************************************************** |
2 | * Copyright (C) 2004 by Riku Leino * |
||
3 | * tsoots@welho.com * |
||
4 | * * |
||
5 | * This program is free software; you can redistribute it and/or modify * |
||
6 | * it under the terms of the GNU General Public License as published by * |
||
7 | * the Free Software Foundation; either version 2 of the License, or * |
||
8 | * (at your option) any later version. * |
||
9 | * * |
||
10 | * This program is distributed in the hope that it will be useful, * |
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
13 | * GNU General Public License for more details. * |
||
14 | * * |
||
15 | * You should have received a copy of the GNU General Public License * |
||
16 | * along with this program; if not, write to the * |
||
17 | * Free Software Foundation, Inc., * |
||
18 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
19 | ***************************************************************************/ |
||
20 | |||
21 | #include "contentreader.h" |
||
22 | |||
23 | #ifdef HAVE_XML |
||
24 | |||
25 | ContentReader* ContentReader::creader = NULL; |
||
26 | |||
27 | extern xmlSAXHandlerPtr cSAXHandler; |
||
28 | |||
29 | ContentReader::ContentReader(QString documentName, StyleReader *s, gtWriter *w, bool textOnly) |
||
30 | { |
||
31 | creader = this; |
||
32 | docname = documentName; |
||
33 | sreader = s; |
||
34 | writer = w; |
||
35 | importTextOnly = textOnly; |
||
36 | defaultStyle = NULL; |
||
37 | currentStyle = NULL; |
||
38 | append = false; |
||
39 | inList = false; |
||
40 | isOrdered = false; |
||
41 | inSpan = false; |
||
42 | listIndex = 0; |
||
43 | listLevel = 0; |
||
44 | currentList = ""; |
||
45 | inT = false; |
||
46 | tName = ""; |
||
47 | } |
||
48 | |||
49 | bool ContentReader::startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs) |
||
50 | { |
||
51 | if ((name == "text:p") || (name == "text:h")) |
||
52 | { |
||
53 | append = true; |
||
54 | if (!inList) |
||
55 | { |
||
56 | for (int i = 0; i < attrs.count(); ++i) |
||
57 | { |
||
58 | if (attrs.localName(i) == "text:style-name") |
||
59 | { |
||
60 | pstyle = sreader->getStyle(attrs.value(i)); |
||
61 | currentStyle = pstyle; |
||
62 | styleNames.push_back(attrs.value(i)); |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | else if (name == "text:span") |
||
68 | { |
||
69 | inSpan = true; |
||
70 | QString styleName = ""; |
||
71 | for (int i = 0; i < attrs.count(); ++i) |
||
72 | { |
||
73 | if (attrs.localName(i) == "text:style-name") |
||
74 | { |
||
75 | currentStyle = sreader->getStyle(attrs.value(i)); |
||
76 | styleName = attrs.value(i); |
||
77 | styleNames.push_back(styleName); |
||
78 | } |
||
79 | } |
||
80 | gtStyle *tmp = sreader->getStyle(getName()); |
||
81 | if ((tmp->getName()).find("default-style") != -1) |
||
82 | getStyle(); |
||
83 | else |
||
84 | currentStyle = tmp; |
||
85 | } |
||
86 | else if ((name == "text:unordered-list") || (name == "text:ordered-list")) |
||
87 | { |
||
88 | inList = true; |
||
89 | ++listLevel; |
||
90 | if (static_cast<int>(listIndex2.size()) < listLevel) |
||
91 | listIndex2.push_back(0); |
||
92 | for (int i = 0; i < attrs.count(); ++i) |
||
93 | { |
||
94 | if (attrs.localName(i) == "text:style-name") |
||
95 | currentList = attrs.value(i); |
||
96 | } |
||
97 | currentStyle = sreader->getStyle(QString(currentList + "_%1").arg(listLevel)); |
||
98 | if (name == "text:ordered-list") |
||
99 | { |
||
100 | isOrdered = true; |
||
101 | isOrdered2.push_back(true); |
||
102 | } |
||
103 | else |
||
104 | { |
||
105 | isOrdered = false; |
||
106 | isOrdered2.push_back(false); |
||
107 | } |
||
108 | } |
||
109 | else if (name == "text:list-item") |
||
110 | { |
||
111 | if (isOrdered2[listLevel - 1]) |
||
112 | { |
||
113 | ++listIndex; |
||
114 | ++listIndex2[listLevel - 1]; |
||
115 | if (listLevel == 1) |
||
116 | write(QString("%1. ").arg(listIndex2[listLevel - 1])); |
||
117 | else |
||
118 | write(QString("%1. ").arg(listIndex2[listLevel - 1])); |
||
119 | } |
||
120 | else |
||
121 | write("- "); |
||
122 | } |
||
123 | else if (name == "style:style") |
||
124 | { |
||
125 | QString sname = ""; |
||
126 | bool isTextStyle = false; |
||
127 | for (int i = 0; i < attrs.count(); ++i) |
||
128 | { |
||
129 | if (attrs.localName(i) == "style:name") |
||
130 | sname = attrs.value(i); |
||
131 | else if ((attrs.localName(i) == "style:family") && (attrs.value(i) == "text")); |
||
132 | isTextStyle = true; |
||
133 | } |
||
134 | if (isTextStyle) |
||
135 | { |
||
136 | tName = sname; |
||
137 | inT = true; |
||
138 | } |
||
139 | } |
||
140 | else if ((name == "style:properties") && (inT)) |
||
141 | { |
||
142 | Properties p; |
||
143 | for (int i = 0; i < attrs.count(); ++i) |
||
144 | { |
||
145 | std::pair<QString, QString> pair(attrs.localName(i), attrs.value(i)); |
||
146 | p.push_back(pair); |
||
147 | } |
||
148 | tmap[tName] = p; |
||
149 | } |
||
420 | Franz | 150 | else if (name == "text:s") |
151 | { |
||
152 | int count = 1; |
||
153 | for (int i = 0; i < attrs.count(); ++i) |
||
154 | { |
||
155 | if (attrs.localName(i) == "text:c") |
||
156 | { |
||
157 | bool ok = false; |
||
158 | int tmpcount = (attrs.value(i)).toInt(&ok); |
||
159 | if (ok) |
||
160 | count = tmpcount; |
||
161 | } |
||
162 | } |
||
163 | for (int i = 0; i < count; ++i) |
||
164 | write(" "); |
||
165 | } |
||
411 | Franz | 166 | return true; |
167 | } |
||
168 | |||
169 | bool ContentReader::characters(const QString &ch) |
||
170 | { |
||
171 | QString tmp = ch; |
||
172 | tmp = tmp.remove("\n"); |
||
173 | tmp = tmp.remove(""); // Remove all OO.o hyphenation chars |
||
174 | if (append) |
||
175 | write(tmp); |
||
176 | return true; |
||
177 | } |
||
178 | |||
179 | bool ContentReader::endElement(const QString&, const QString&, const QString &name) |
||
180 | { |
||
181 | if ((name == "text:p") || (name == "text:h")) |
||
182 | { |
||
183 | write("\n"); |
||
184 | append = false; |
||
185 | if (styleNames.size() != 0) |
||
186 | styleNames.pop_back(); |
||
187 | } |
||
188 | else if (name == "text:span") |
||
189 | { |
||
190 | inSpan = false; |
||
191 | currentStyle = pstyle; |
||
192 | if (styleNames.size() != 0) |
||
193 | styleNames.pop_back(); |
||
194 | currentStyle = sreader->getStyle(getName()); |
||
195 | } |
||
196 | else if (name == "text:line-break") |
||
197 | write(QChar(28)); |
||
198 | else if (name == "text:tab-stop") |
||
199 | write("\t"); |
||
200 | else if ((name == "text:unordered-list") || (name == "text:ordered-list")) |
||
201 | { |
||
202 | --listLevel; |
||
203 | if (listLevel == 0) |
||
204 | { |
||
205 | inList = false; |
||
206 | listIndex2.clear(); |
||
207 | } |
||
208 | else |
||
209 | currentStyle = sreader->getStyle(QString(currentList + "_%1").arg(listLevel)); |
||
210 | } |
||
211 | else if ((name == "style:style") && (inT)) |
||
212 | { |
||
213 | inT = false; |
||
214 | tName = ""; |
||
215 | } |
||
216 | return true; |
||
217 | } |
||
218 | |||
219 | void ContentReader::write(const QString& text) |
||
220 | { |
||
221 | if (importTextOnly) |
||
222 | writer->append(text); |
||
223 | else if (inSpan) |
||
224 | writer->append(text, currentStyle, false); |
||
225 | else |
||
226 | writer->append(text, currentStyle); |
||
227 | lastStyle = currentStyle; |
||
228 | } |
||
229 | |||
230 | void ContentReader::parse(QString fileName) |
||
231 | { |
||
232 | sreader->parse(fileName); |
||
233 | xmlSAXParseFile(cSAXHandler, fileName.ascii(), XML_PARSE_RECOVER); |
||
234 | } |
||
235 | |||
236 | xmlSAXHandler cSAXHandlerStruct = { |
||
237 | NULL, // internalSubset, |
||
238 | NULL, // isStandalone, |
||
239 | NULL, // hasInternalSubset, |
||
240 | NULL, // hasExternalSubset, |
||
241 | NULL, // resolveEntity, |
||
242 | NULL, // getEntity, |
||
243 | NULL, // entityDecl, |
||
244 | NULL, // notationDecl, |
||
245 | NULL, // attributeDecl, |
||
246 | NULL, // elementDecl, |
||
247 | NULL, // unparsedEntityDecl, |
||
248 | NULL, // setDocumentLocator, |
||
249 | NULL, // startDocument, |
||
250 | NULL, // endDocument, |
||
251 | ContentReader::startElement, |
||
252 | ContentReader::endElement, |
||
253 | NULL, // reference, |
||
254 | ContentReader::characters, |
||
255 | NULL, // ignorableWhitespace, |
||
256 | NULL, // processingInstruction, |
||
257 | NULL, // comment, |
||
258 | NULL, // warning, |
||
259 | NULL, // error, |
||
260 | NULL, // fatalError, |
||
261 | NULL, // getParameterEntity, |
||
262 | NULL, // cdata, |
||
263 | NULL, |
||
264 | 1 |
||
265 | #ifdef HAVE_XML26 |
||
266 | , |
||
267 | NULL, |
||
268 | NULL, |
||
269 | NULL, |
||
270 | NULL |
||
271 | #endif |
||
272 | }; |
||
273 | |||
274 | xmlSAXHandlerPtr cSAXHandler = &cSAXHandlerStruct; |
||
275 | |||
276 | void ContentReader::startElement(void *user_data, const xmlChar *fullname, const xmlChar ** atts) |
||
277 | { |
||
278 | QString* name = new QString((const char*) fullname); |
||
279 | name = new QString(name->lower()); |
||
280 | QXmlAttributes* attrs = new QXmlAttributes(); |
||
281 | if (atts) |
||
282 | { |
||
283 | for(const xmlChar** cur = atts; cur && *cur; cur += 2) |
||
284 | attrs->append(QString((char*)*cur), NULL, QString((char*)*cur), QString((char*)*(cur + 1))); |
||
285 | } |
||
286 | creader->startElement(NULL, NULL, *name, *attrs); |
||
287 | } |
||
288 | |||
289 | void ContentReader::characters(void *user_data, const xmlChar *ch, int len) |
||
290 | { |
||
291 | QString chars = QString::fromUtf8((const char*) ch, len); |
||
292 | creader->characters(chars); |
||
293 | } |
||
294 | |||
295 | void ContentReader::endElement(void *user_data, const xmlChar *name) |
||
296 | { |
||
297 | QString *nname = new QString((const char*) name); |
||
298 | nname = new QString(nname->lower()); |
||
299 | creader->endElement(NULL, NULL, *nname); |
||
300 | } |
||
301 | |||
302 | QString ContentReader::getName() |
||
303 | { |
||
304 | QString s = ""; |
||
305 | for (uint i = 0; i < styleNames.size(); ++i) |
||
306 | s += styleNames[i]; |
||
307 | return s; |
||
308 | } |
||
309 | |||
310 | void ContentReader::getStyle() |
||
311 | { |
||
312 | gtParagraphStyle* par = dynamic_cast<gtParagraphStyle*>(sreader->getStyle(styleNames[0])); |
||
313 | gtParagraphStyle* tmp = new gtParagraphStyle(*par); |
||
314 | for (uint i = 1; i < styleNames.size(); ++i) |
||
315 | { |
||
316 | Properties& p = tmap[styleNames[i]]; |
||
317 | for (uint j = 0; j < p.size(); ++j) |
||
318 | sreader->updateStyle(tmp, sreader->getStyle(styleNames[i - 1]), p[j].first, p[j].second); |
||
319 | } |
||
320 | // gtParagraphStyle* s = new gtParagraphStyle(*dynamic_cast<gtParagraphStyle*>(lastStyle)); |
||
321 | // s->setAutoLineSpacing(true); |
||
322 | |||
323 | currentStyle = tmp; |
||
324 | sreader->setStyle(getName(), tmp); |
||
325 | } |
||
326 | |||
327 | ContentReader::~ContentReader() |
||
328 | { |
||
329 | creader = NULL; |
||
330 | delete defaultStyle; |
||
331 | } |
||
332 | |||
333 | #endif // HAVE_XML |