Rev 411 | Rev 418 | 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 | } |
||
150 | return true; |
||
151 | } |
||
152 | |||
153 | bool ContentReader::characters(const QString &ch) |
||
154 | { |
||
155 | QString tmp = ch; |
||
156 | tmp = tmp.remove("\n"); |
||
157 | tmp = tmp.remove(""); // Remove all OO.o hyphenation chars |
||
158 | if (append) |
||
159 | write(tmp); |
||
160 | return true; |
||
161 | } |
||
162 | |||
163 | bool ContentReader::endElement(const QString&, const QString&, const QString &name) |
||
164 | { |
||
165 | if ((name == "text:p") || (name == "text:h")) |
||
166 | { |
||
167 | write("\n"); |
||
168 | append = false; |
||
169 | if (styleNames.size() != 0) |
||
170 | styleNames.pop_back(); |
||
171 | } |
||
172 | else if (name == "text:span") |
||
173 | { |
||
174 | inSpan = false; |
||
175 | currentStyle = pstyle; |
||
176 | if (styleNames.size() != 0) |
||
177 | styleNames.pop_back(); |
||
178 | currentStyle = sreader->getStyle(getName()); |
||
179 | } |
||
180 | else if (name == "text:line-break") |
||
181 | write(QChar(28)); |
||
182 | else if (name == "text:tab-stop") |
||
183 | write("\t"); |
||
184 | else if ((name == "text:unordered-list") || (name == "text:ordered-list")) |
||
185 | { |
||
186 | --listLevel; |
||
187 | if (listLevel == 0) |
||
188 | { |
||
189 | inList = false; |
||
190 | listIndex2.clear(); |
||
191 | } |
||
192 | else |
||
193 | currentStyle = sreader->getStyle(QString(currentList + "_%1").arg(listLevel)); |
||
194 | } |
||
195 | else if ((name == "style:style") && (inT)) |
||
196 | { |
||
197 | inT = false; |
||
198 | tName = ""; |
||
199 | } |
||
200 | return true; |
||
201 | } |
||
202 | |||
203 | void ContentReader::write(const QString& text) |
||
204 | { |
||
205 | if (importTextOnly) |
||
206 | writer->append(text); |
||
207 | else if (inSpan) |
||
208 | writer->append(text, currentStyle, false); |
||
209 | else |
||
210 | writer->append(text, currentStyle); |
||
211 | lastStyle = currentStyle; |
||
212 | } |
||
213 | |||
214 | void ContentReader::parse(QString fileName) |
||
215 | { |
||
216 | sreader->parse(fileName); |
||
217 | xmlSAXParseFile(cSAXHandler, fileName.ascii(), XML_PARSE_RECOVER); |
||
218 | } |
||
219 | |||
220 | xmlSAXHandler cSAXHandlerStruct = { |
||
221 | NULL, // internalSubset, |
||
222 | NULL, // isStandalone, |
||
223 | NULL, // hasInternalSubset, |
||
224 | NULL, // hasExternalSubset, |
||
225 | NULL, // resolveEntity, |
||
226 | NULL, // getEntity, |
||
227 | NULL, // entityDecl, |
||
228 | NULL, // notationDecl, |
||
229 | NULL, // attributeDecl, |
||
230 | NULL, // elementDecl, |
||
231 | NULL, // unparsedEntityDecl, |
||
232 | NULL, // setDocumentLocator, |
||
233 | NULL, // startDocument, |
||
234 | NULL, // endDocument, |
||
235 | ContentReader::startElement, |
||
236 | ContentReader::endElement, |
||
237 | NULL, // reference, |
||
238 | ContentReader::characters, |
||
239 | NULL, // ignorableWhitespace, |
||
240 | NULL, // processingInstruction, |
||
241 | NULL, // comment, |
||
242 | NULL, // warning, |
||
243 | NULL, // error, |
||
244 | NULL, // fatalError, |
||
245 | NULL, // getParameterEntity, |
||
246 | NULL, // cdata, |
||
247 | NULL, |
||
248 | 1 |
||
249 | #ifdef HAVE_XML26 |
||
250 | , |
||
251 | NULL, |
||
252 | NULL, |
||
253 | NULL, |
||
254 | NULL |
||
255 | #endif |
||
256 | }; |
||
257 | |||
258 | xmlSAXHandlerPtr cSAXHandler = &cSAXHandlerStruct; |
||
259 | |||
260 | void ContentReader::startElement(void *user_data, const xmlChar *fullname, const xmlChar ** atts) |
||
261 | { |
||
262 | QString* name = new QString((const char*) fullname); |
||
263 | name = new QString(name->lower()); |
||
264 | QXmlAttributes* attrs = new QXmlAttributes(); |
||
265 | if (atts) |
||
266 | { |
||
267 | for(const xmlChar** cur = atts; cur && *cur; cur += 2) |
||
268 | attrs->append(QString((char*)*cur), NULL, QString((char*)*cur), QString((char*)*(cur + 1))); |
||
269 | } |
||
270 | creader->startElement(NULL, NULL, *name, *attrs); |
||
271 | } |
||
272 | |||
273 | void ContentReader::characters(void *user_data, const xmlChar *ch, int len) |
||
274 | { |
||
275 | QString chars = QString::fromUtf8((const char*) ch, len); |
||
276 | creader->characters(chars); |
||
277 | } |
||
278 | |||
279 | void ContentReader::endElement(void *user_data, const xmlChar *name) |
||
280 | { |
||
281 | QString *nname = new QString((const char*) name); |
||
282 | nname = new QString(nname->lower()); |
||
283 | creader->endElement(NULL, NULL, *nname); |
||
284 | } |
||
285 | |||
286 | QString ContentReader::getName() |
||
287 | { |
||
288 | QString s = ""; |
||
289 | for (uint i = 0; i < styleNames.size(); ++i) |
||
290 | s += styleNames[i]; |
||
291 | return s; |
||
292 | } |
||
293 | |||
294 | void ContentReader::getStyle() |
||
295 | { |
||
296 | gtParagraphStyle* par = dynamic_cast<gtParagraphStyle*>(sreader->getStyle(styleNames[0])); |
||
297 | gtParagraphStyle* tmp = new gtParagraphStyle(*par); |
||
298 | for (uint i = 1; i < styleNames.size(); ++i) |
||
299 | { |
||
300 | Properties& p = tmap[styleNames[i]]; |
||
301 | for (uint j = 0; j < p.size(); ++j) |
||
302 | sreader->updateStyle(tmp, sreader->getStyle(styleNames[i - 1]), p[j].first, p[j].second); |
||
303 | } |
||
304 | // gtParagraphStyle* s = new gtParagraphStyle(*dynamic_cast<gtParagraphStyle*>(lastStyle)); |
||
305 | // s->setAutoLineSpacing(true); |
||
306 | |||
307 | currentStyle = tmp; |
||
308 | sreader->setStyle(getName(), tmp); |
||
309 | } |
||
310 | |||
311 | ContentReader::~ContentReader() |
||
312 | { |
||
313 | creader = NULL; |
||
314 | delete defaultStyle; |
||
315 | } |
||
316 | |||
317 | #endif // HAVE_XML |