Rev 1156 | 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 * |
||
898 | tsoots | 3 | * riku.leino@gmail.com * |
411 | Franz | 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; |
||
439 | tsoots | 54 | QString name = ""; |
55 | for (int i = 0; i < attrs.count(); ++i) |
||
411 | Franz | 56 | { |
439 | tsoots | 57 | if (attrs.localName(i) == "text:style-name") |
411 | Franz | 58 | { |
439 | tsoots | 59 | name = attrs.value(i); |
60 | styleNames.push_back(attrs.value(i)); |
||
411 | Franz | 61 | } |
62 | } |
||
439 | tsoots | 63 | if (!inList) |
64 | { |
||
65 | pstyle = sreader->getStyle(name); |
||
66 | currentStyle = pstyle; |
||
67 | } |
||
68 | else |
||
69 | { |
||
70 | gtStyle *tmp = sreader->getStyle(getName()); |
||
71 | if ((tmp->getName()).find("default-style") != -1) |
||
72 | getStyle(); |
||
73 | else |
||
74 | currentStyle = tmp; |
||
75 | } |
||
411 | Franz | 76 | } |
77 | else if (name == "text:span") |
||
78 | { |
||
79 | inSpan = true; |
||
80 | QString styleName = ""; |
||
81 | for (int i = 0; i < attrs.count(); ++i) |
||
82 | { |
||
83 | if (attrs.localName(i) == "text:style-name") |
||
84 | { |
||
85 | currentStyle = sreader->getStyle(attrs.value(i)); |
||
86 | styleName = attrs.value(i); |
||
87 | styleNames.push_back(styleName); |
||
88 | } |
||
89 | } |
||
90 | gtStyle *tmp = sreader->getStyle(getName()); |
||
91 | if ((tmp->getName()).find("default-style") != -1) |
||
92 | getStyle(); |
||
93 | else |
||
94 | currentStyle = tmp; |
||
95 | } |
||
96 | else if ((name == "text:unordered-list") || (name == "text:ordered-list")) |
||
97 | { |
||
98 | inList = true; |
||
99 | ++listLevel; |
||
100 | if (static_cast<int>(listIndex2.size()) < listLevel) |
||
101 | listIndex2.push_back(0); |
||
102 | for (int i = 0; i < attrs.count(); ++i) |
||
103 | { |
||
104 | if (attrs.localName(i) == "text:style-name") |
||
105 | currentList = attrs.value(i); |
||
106 | } |
||
107 | currentStyle = sreader->getStyle(QString(currentList + "_%1").arg(listLevel)); |
||
439 | tsoots | 108 | styleNames.clear(); |
109 | styleNames.push_back(QString(currentList + "_%1").arg(listLevel)); |
||
411 | Franz | 110 | if (name == "text:ordered-list") |
111 | { |
||
112 | isOrdered = true; |
||
113 | isOrdered2.push_back(true); |
||
114 | } |
||
115 | else |
||
116 | { |
||
117 | isOrdered = false; |
||
118 | isOrdered2.push_back(false); |
||
119 | } |
||
120 | } |
||
121 | else if (name == "text:list-item") |
||
122 | { |
||
123 | if (isOrdered2[listLevel - 1]) |
||
124 | { |
||
125 | ++listIndex; |
||
126 | ++listIndex2[listLevel - 1]; |
||
127 | if (listLevel == 1) |
||
128 | write(QString("%1. ").arg(listIndex2[listLevel - 1])); |
||
129 | else |
||
130 | write(QString("%1. ").arg(listIndex2[listLevel - 1])); |
||
131 | } |
||
132 | else |
||
133 | write("- "); |
||
134 | } |
||
135 | else if (name == "style:style") |
||
136 | { |
||
137 | QString sname = ""; |
||
138 | bool isTextStyle = false; |
||
139 | for (int i = 0; i < attrs.count(); ++i) |
||
140 | { |
||
141 | if (attrs.localName(i) == "style:name") |
||
142 | sname = attrs.value(i); |
||
143 | else if ((attrs.localName(i) == "style:family") && (attrs.value(i) == "text")); |
||
144 | isTextStyle = true; |
||
145 | } |
||
146 | if (isTextStyle) |
||
147 | { |
||
148 | tName = sname; |
||
149 | inT = true; |
||
150 | } |
||
151 | } |
||
152 | else if ((name == "style:properties") && (inT)) |
||
153 | { |
||
154 | Properties p; |
||
155 | for (int i = 0; i < attrs.count(); ++i) |
||
156 | { |
||
157 | std::pair<QString, QString> pair(attrs.localName(i), attrs.value(i)); |
||
158 | p.push_back(pair); |
||
159 | } |
||
160 | tmap[tName] = p; |
||
161 | } |
||
420 | Franz | 162 | else if (name == "text:s") |
163 | { |
||
164 | int count = 1; |
||
165 | for (int i = 0; i < attrs.count(); ++i) |
||
166 | { |
||
167 | if (attrs.localName(i) == "text:c") |
||
168 | { |
||
169 | bool ok = false; |
||
170 | int tmpcount = (attrs.value(i)).toInt(&ok); |
||
171 | if (ok) |
||
172 | count = tmpcount; |
||
173 | } |
||
174 | } |
||
175 | for (int i = 0; i < count; ++i) |
||
176 | write(" "); |
||
177 | } |
||
411 | Franz | 178 | return true; |
179 | } |
||
180 | |||
181 | bool ContentReader::characters(const QString &ch) |
||
182 | { |
||
183 | QString tmp = ch; |
||
184 | tmp = tmp.remove("\n"); |
||
185 | tmp = tmp.remove(""); // Remove all OO.o hyphenation chars |
||
1319 | tsoots | 186 | tmp = tmp.replace(QChar(160), QChar(29)); // replace OO.o nbsp with Scribus nbsp |
411 | Franz | 187 | if (append) |
188 | write(tmp); |
||
189 | return true; |
||
190 | } |
||
191 | |||
192 | bool ContentReader::endElement(const QString&, const QString&, const QString &name) |
||
193 | { |
||
194 | if ((name == "text:p") || (name == "text:h")) |
||
195 | { |
||
196 | write("\n"); |
||
197 | append = false; |
||
439 | tsoots | 198 | if (inList) |
411 | Franz | 199 | styleNames.pop_back(); |
439 | tsoots | 200 | else |
201 | styleNames.clear(); |
||
411 | Franz | 202 | } |
203 | else if (name == "text:span") |
||
204 | { |
||
205 | inSpan = false; |
||
206 | currentStyle = pstyle; |
||
207 | if (styleNames.size() != 0) |
||
439 | tsoots | 208 | styleNames.pop_back(); |
411 | Franz | 209 | currentStyle = sreader->getStyle(getName()); |
210 | } |
||
211 | else if (name == "text:line-break") |
||
212 | write(QChar(28)); |
||
213 | else if (name == "text:tab-stop") |
||
214 | write("\t"); |
||
215 | else if ((name == "text:unordered-list") || (name == "text:ordered-list")) |
||
216 | { |
||
217 | --listLevel; |
||
439 | tsoots | 218 | styleNames.clear(); |
411 | Franz | 219 | if (listLevel == 0) |
220 | { |
||
221 | inList = false; |
||
222 | listIndex2.clear(); |
||
223 | } |
||
224 | else |
||
439 | tsoots | 225 | { |
411 | Franz | 226 | currentStyle = sreader->getStyle(QString(currentList + "_%1").arg(listLevel)); |
439 | tsoots | 227 | styleNames.push_back(QString(currentList + "_%1").arg(listLevel)); |
228 | } |
||
411 | Franz | 229 | } |
230 | else if ((name == "style:style") && (inT)) |
||
231 | { |
||
232 | inT = false; |
||
233 | tName = ""; |
||
234 | } |
||
235 | return true; |
||
236 | } |
||
237 | |||
238 | void ContentReader::write(const QString& text) |
||
239 | { |
||
240 | if (importTextOnly) |
||
241 | writer->append(text); |
||
242 | else if (inSpan) |
||
243 | writer->append(text, currentStyle, false); |
||
244 | else |
||
245 | writer->append(text, currentStyle); |
||
246 | lastStyle = currentStyle; |
||
247 | } |
||
248 | |||
249 | void ContentReader::parse(QString fileName) |
||
250 | { |
||
251 | sreader->parse(fileName); |
||
940 | tsoots | 252 | xmlSAXParseFile(cSAXHandler, fileName.ascii(), 1); |
411 | Franz | 253 | } |
254 | |||
255 | xmlSAXHandler cSAXHandlerStruct = { |
||
256 | NULL, // internalSubset, |
||
257 | NULL, // isStandalone, |
||
258 | NULL, // hasInternalSubset, |
||
259 | NULL, // hasExternalSubset, |
||
260 | NULL, // resolveEntity, |
||
261 | NULL, // getEntity, |
||
262 | NULL, // entityDecl, |
||
263 | NULL, // notationDecl, |
||
264 | NULL, // attributeDecl, |
||
265 | NULL, // elementDecl, |
||
266 | NULL, // unparsedEntityDecl, |
||
267 | NULL, // setDocumentLocator, |
||
268 | NULL, // startDocument, |
||
269 | NULL, // endDocument, |
||
270 | ContentReader::startElement, |
||
271 | ContentReader::endElement, |
||
272 | NULL, // reference, |
||
273 | ContentReader::characters, |
||
274 | NULL, // ignorableWhitespace, |
||
275 | NULL, // processingInstruction, |
||
276 | NULL, // comment, |
||
277 | NULL, // warning, |
||
278 | NULL, // error, |
||
279 | NULL, // fatalError, |
||
280 | NULL, // getParameterEntity, |
||
281 | NULL, // cdata, |
||
282 | NULL, |
||
283 | 1 |
||
284 | #ifdef HAVE_XML26 |
||
285 | , |
||
286 | NULL, |
||
287 | NULL, |
||
288 | NULL, |
||
289 | NULL |
||
290 | #endif |
||
291 | }; |
||
292 | |||
293 | xmlSAXHandlerPtr cSAXHandler = &cSAXHandlerStruct; |
||
294 | |||
1156 | tsoots | 295 | void ContentReader::startElement(void*, const xmlChar *fullname, const xmlChar ** atts) |
411 | Franz | 296 | { |
297 | QString* name = new QString((const char*) fullname); |
||
298 | name = new QString(name->lower()); |
||
299 | QXmlAttributes* attrs = new QXmlAttributes(); |
||
300 | if (atts) |
||
301 | { |
||
302 | for(const xmlChar** cur = atts; cur && *cur; cur += 2) |
||
303 | attrs->append(QString((char*)*cur), NULL, QString((char*)*cur), QString((char*)*(cur + 1))); |
||
304 | } |
||
305 | creader->startElement(NULL, NULL, *name, *attrs); |
||
306 | } |
||
307 | |||
1156 | tsoots | 308 | void ContentReader::characters(void*, const xmlChar *ch, int len) |
411 | Franz | 309 | { |
310 | QString chars = QString::fromUtf8((const char*) ch, len); |
||
311 | creader->characters(chars); |
||
312 | } |
||
313 | |||
1156 | tsoots | 314 | void ContentReader::endElement(void*, const xmlChar *name) |
411 | Franz | 315 | { |
316 | QString *nname = new QString((const char*) name); |
||
317 | nname = new QString(nname->lower()); |
||
318 | creader->endElement(NULL, NULL, *nname); |
||
319 | } |
||
320 | |||
321 | QString ContentReader::getName() |
||
322 | { |
||
323 | QString s = ""; |
||
324 | for (uint i = 0; i < styleNames.size(); ++i) |
||
325 | s += styleNames[i]; |
||
326 | return s; |
||
327 | } |
||
328 | |||
329 | void ContentReader::getStyle() |
||
330 | { |
||
439 | tsoots | 331 | gtParagraphStyle* par = NULL; |
332 | if (styleNames.size() == 0) |
||
333 | par = dynamic_cast<gtParagraphStyle*>(sreader->getStyle("default-style")); |
||
334 | else |
||
335 | par = dynamic_cast<gtParagraphStyle*>(sreader->getStyle(styleNames[0])); |
||
411 | Franz | 336 | gtParagraphStyle* tmp = new gtParagraphStyle(*par); |
337 | for (uint i = 1; i < styleNames.size(); ++i) |
||
338 | { |
||
339 | Properties& p = tmap[styleNames[i]]; |
||
340 | for (uint j = 0; j < p.size(); ++j) |
||
341 | sreader->updateStyle(tmp, sreader->getStyle(styleNames[i - 1]), p[j].first, p[j].second); |
||
342 | } |
||
343 | |||
344 | currentStyle = tmp; |
||
345 | sreader->setStyle(getName(), tmp); |
||
346 | } |
||
347 | |||
348 | ContentReader::~ContentReader() |
||
349 | { |
||
350 | creader = NULL; |
||
351 | delete defaultStyle; |
||
352 | } |
||
353 | |||
354 | #endif // HAVE_XML |