Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef INCLUDED_XMLREADER_XMLREADER_HXX
21 : #define INCLUDED_XMLREADER_XMLREADER_HXX
22 :
23 : #include "sal/config.h"
24 :
25 : #include <stack>
26 : #include <vector>
27 :
28 : #include "boost/noncopyable.hpp"
29 : #include "com/sun/star/container/NoSuchElementException.hpp"
30 : #include "com/sun/star/uno/RuntimeException.hpp"
31 : #include "osl/file.h"
32 : #include "rtl/ustring.hxx"
33 : #include "sal/types.h"
34 : #include "xmlreader/detail/xmlreaderdllapi.hxx"
35 : #include "xmlreader/pad.hxx"
36 : #include "xmlreader/span.hxx"
37 :
38 : namespace xmlreader {
39 :
40 : class OOO_DLLPUBLIC_XMLREADER XmlReader: private boost::noncopyable {
41 : public:
42 : explicit XmlReader(OUString const & fileUrl)
43 : SAL_THROW((
44 : com::sun::star::container::NoSuchElementException,
45 : com::sun::star::uno::RuntimeException));
46 :
47 : ~XmlReader();
48 :
49 : enum { NAMESPACE_NONE = -2, NAMESPACE_UNKNOWN = -1, NAMESPACE_XML = 0 };
50 :
51 : enum Text { TEXT_NONE, TEXT_RAW, TEXT_NORMALIZED };
52 :
53 : enum Result { RESULT_BEGIN, RESULT_END, RESULT_TEXT, RESULT_DONE };
54 :
55 : int registerNamespaceIri(Span const & iri);
56 :
57 : // RESULT_BEGIN: data = localName, ns = ns
58 : // RESULT_END: data, ns unused
59 : // RESULT_TEXT: data = text, ns unused
60 : Result nextItem(Text reportText, Span * data, int * nsId);
61 :
62 : bool nextAttribute(int * nsId, Span * localName);
63 :
64 : // the span returned by getAttributeValue is only valid until the next call
65 : // to nextItem or getAttributeValue
66 : Span getAttributeValue(bool fullyNormalize);
67 :
68 : int getNamespaceId(Span const & prefix) const;
69 :
70 : OUString getUrl() const;
71 :
72 : private:
73 : typedef std::vector< Span > NamespaceIris;
74 :
75 : // If NamespaceData (and similarly ElementData and AttributeData) is made
76 : // SAL_DLLPRIVATE, at least gcc 4.2.3 erroneously warns about
77 : // "'xmlreader::XmlReader' declared with greater visibility than the type of
78 : // its field 'xmlreader::XmlReader::namespaces_'" (and similarly for
79 : // elements_ and attributes_):
80 :
81 : struct NamespaceData {
82 : Span prefix;
83 : int nsId;
84 :
85 0 : NamespaceData() {}
86 :
87 115190 : NamespaceData(Span const & thePrefix, int theNsId):
88 115190 : prefix(thePrefix), nsId(theNsId) {}
89 : };
90 :
91 : typedef std::vector< NamespaceData > NamespaceList;
92 :
93 : struct ElementData {
94 : Span name;
95 : NamespaceList::size_type inheritedNamespaces;
96 : int defaultNamespaceId;
97 :
98 8296229 : ElementData(
99 : Span const & theName,
100 : NamespaceList::size_type theInheritedNamespaces,
101 : int theDefaultNamespaceId):
102 : name(theName), inheritedNamespaces(theInheritedNamespaces),
103 8296229 : defaultNamespaceId(theDefaultNamespaceId)
104 8296229 : {}
105 : };
106 :
107 : typedef std::stack< ElementData > ElementStack;
108 :
109 : struct AttributeData {
110 : char const * nameBegin;
111 : char const * nameEnd;
112 : char const * nameColon;
113 : char const * valueBegin;
114 : char const * valueEnd;
115 :
116 8382178 : AttributeData(
117 : char const * theNameBegin, char const * theNameEnd,
118 : char const * theNameColon, char const * theValueBegin,
119 : char const * theValueEnd):
120 : nameBegin(theNameBegin), nameEnd(theNameEnd),
121 : nameColon(theNameColon), valueBegin(theValueBegin),
122 8382178 : valueEnd(theValueEnd)
123 8382178 : {}
124 : };
125 :
126 : typedef std::vector< AttributeData > Attributes;
127 :
128 : enum State {
129 : STATE_CONTENT, STATE_START_TAG, STATE_END_TAG, STATE_EMPTY_ELEMENT_TAG,
130 : STATE_DONE };
131 :
132 16939320 : SAL_DLLPRIVATE inline char read() { return pos_ == end_ ? '\0' : *pos_++; }
133 :
134 337796678 : SAL_DLLPRIVATE inline char peek() { return pos_ == end_ ? '\0' : *pos_; }
135 :
136 : SAL_DLLPRIVATE void normalizeLineEnds(Span const & text);
137 :
138 : SAL_DLLPRIVATE void skipSpace();
139 :
140 : SAL_DLLPRIVATE bool skipComment();
141 :
142 : SAL_DLLPRIVATE void skipProcessingInstruction();
143 :
144 : SAL_DLLPRIVATE void skipDocumentTypeDeclaration();
145 :
146 : SAL_DLLPRIVATE Span scanCdataSection();
147 :
148 : SAL_DLLPRIVATE bool scanName(char const ** nameColon);
149 :
150 : SAL_DLLPRIVATE int scanNamespaceIri(
151 : char const * begin, char const * end);
152 :
153 : SAL_DLLPRIVATE char const * handleReference(
154 : char const * position, char const * end);
155 :
156 : SAL_DLLPRIVATE Span handleAttributeValue(
157 : char const * begin, char const * end, bool fullyNormalize);
158 :
159 : SAL_DLLPRIVATE Result handleStartTag(int * nsId, Span * localName);
160 :
161 : SAL_DLLPRIVATE Result handleEndTag();
162 :
163 : SAL_DLLPRIVATE void handleElementEnd();
164 :
165 : SAL_DLLPRIVATE Result handleSkippedText(Span * data, int * nsId);
166 :
167 : SAL_DLLPRIVATE Result handleRawText(Span * text);
168 :
169 : SAL_DLLPRIVATE Result handleNormalizedText(Span * text);
170 :
171 : SAL_DLLPRIVATE int toNamespaceId(NamespaceIris::size_type pos);
172 :
173 : OUString fileUrl_;
174 : oslFileHandle fileHandle_;
175 : sal_uInt64 fileSize_;
176 : void * fileAddress_;
177 : NamespaceIris namespaceIris_;
178 : NamespaceList namespaces_;
179 : ElementStack elements_;
180 : char const * pos_;
181 : char const * end_;
182 : State state_;
183 : Attributes attributes_;
184 : Attributes::iterator currentAttribute_;
185 : bool firstAttribute_;
186 : Pad pad_;
187 : };
188 :
189 : }
190 :
191 : #endif
192 :
193 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|