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 : XmlReader(char const *sStr, size_t nLength);
43 :
44 : explicit XmlReader(OUString const & fileUrl)
45 : SAL_THROW((
46 : com::sun::star::container::NoSuchElementException,
47 : com::sun::star::uno::RuntimeException));
48 :
49 : ~XmlReader();
50 :
51 : enum { NAMESPACE_NONE = -2, NAMESPACE_UNKNOWN = -1, NAMESPACE_XML = 0 };
52 :
53 : enum Text { TEXT_NONE, TEXT_RAW, TEXT_NORMALIZED };
54 :
55 : enum Result { RESULT_BEGIN, RESULT_END, RESULT_TEXT, RESULT_DONE };
56 :
57 : int registerNamespaceIri(Span const & iri);
58 :
59 : // RESULT_BEGIN: data = localName, ns = ns
60 : // RESULT_END: data, ns unused
61 : // RESULT_TEXT: data = text, ns unused
62 : Result nextItem(Text reportText, Span * data, int * nsId);
63 :
64 : bool nextAttribute(int * nsId, Span * localName);
65 :
66 : // the span returned by getAttributeValue is only valid until the next call
67 : // to nextItem or getAttributeValue
68 : Span getAttributeValue(bool fullyNormalize);
69 :
70 : int getNamespaceId(Span const & prefix) const;
71 :
72 : OUString getUrl() const;
73 :
74 : private:
75 : typedef std::vector< Span > NamespaceIris;
76 :
77 : // If NamespaceData (and similarly ElementData and AttributeData) is made
78 : // SAL_DLLPRIVATE, at least gcc 4.2.3 erroneously warns about
79 : // "'xmlreader::XmlReader' declared with greater visibility than the type of
80 : // its field 'xmlreader::XmlReader::namespaces_'" (and similarly for
81 : // elements_ and attributes_):
82 :
83 : struct NamespaceData {
84 : Span prefix;
85 : int nsId;
86 :
87 0 : NamespaceData():
88 0 : nsId(-1) {}
89 :
90 188 : NamespaceData(Span const & thePrefix, int theNsId):
91 188 : prefix(thePrefix), nsId(theNsId) {}
92 : };
93 :
94 : typedef std::vector< NamespaceData > NamespaceList;
95 :
96 : struct ElementData {
97 : Span name;
98 : NamespaceList::size_type inheritedNamespaces;
99 : int defaultNamespaceId;
100 :
101 50370 : ElementData(
102 : Span const & theName,
103 : NamespaceList::size_type theInheritedNamespaces,
104 : int theDefaultNamespaceId):
105 : name(theName), inheritedNamespaces(theInheritedNamespaces),
106 50370 : defaultNamespaceId(theDefaultNamespaceId)
107 50370 : {}
108 : };
109 :
110 : typedef std::stack< ElementData > ElementStack;
111 :
112 : struct AttributeData {
113 : char const * nameBegin;
114 : char const * nameEnd;
115 : char const * nameColon;
116 : char const * valueBegin;
117 : char const * valueEnd;
118 :
119 55138 : AttributeData(
120 : char const * theNameBegin, char const * theNameEnd,
121 : char const * theNameColon, char const * theValueBegin,
122 : char const * theValueEnd):
123 : nameBegin(theNameBegin), nameEnd(theNameEnd),
124 : nameColon(theNameColon), valueBegin(theValueBegin),
125 55138 : valueEnd(theValueEnd)
126 55138 : {}
127 : };
128 :
129 : typedef std::vector< AttributeData > Attributes;
130 :
131 : enum State {
132 : STATE_CONTENT, STATE_START_TAG, STATE_END_TAG, STATE_EMPTY_ELEMENT_TAG,
133 : STATE_DONE };
134 :
135 110601 : SAL_DLLPRIVATE inline char read() { return pos_ == end_ ? '\0' : *pos_++; }
136 :
137 2123369 : SAL_DLLPRIVATE inline char peek() { return pos_ == end_ ? '\0' : *pos_; }
138 :
139 : SAL_DLLPRIVATE void normalizeLineEnds(Span const & text);
140 :
141 : SAL_DLLPRIVATE void skipSpace();
142 :
143 : SAL_DLLPRIVATE bool skipComment();
144 :
145 : SAL_DLLPRIVATE void skipProcessingInstruction();
146 :
147 : SAL_DLLPRIVATE void skipDocumentTypeDeclaration();
148 :
149 : SAL_DLLPRIVATE Span scanCdataSection();
150 :
151 : SAL_DLLPRIVATE bool scanName(char const ** nameColon);
152 :
153 : SAL_DLLPRIVATE int scanNamespaceIri(
154 : char const * begin, char const * end);
155 :
156 : SAL_DLLPRIVATE char const * handleReference(
157 : char const * position, char const * end);
158 :
159 : SAL_DLLPRIVATE Span handleAttributeValue(
160 : char const * begin, char const * end, bool fullyNormalize);
161 :
162 : SAL_DLLPRIVATE Result handleStartTag(int * nsId, Span * localName);
163 :
164 : SAL_DLLPRIVATE Result handleEndTag();
165 :
166 : SAL_DLLPRIVATE void handleElementEnd();
167 :
168 : SAL_DLLPRIVATE Result handleSkippedText(Span * data, int * nsId);
169 :
170 : SAL_DLLPRIVATE Result handleRawText(Span * text);
171 :
172 : SAL_DLLPRIVATE Result handleNormalizedText(Span * text);
173 :
174 : SAL_DLLPRIVATE int toNamespaceId(NamespaceIris::size_type pos);
175 :
176 : OUString fileUrl_;
177 : oslFileHandle fileHandle_;
178 : sal_uInt64 fileSize_;
179 : void * fileAddress_;
180 : NamespaceIris namespaceIris_;
181 : NamespaceList namespaces_;
182 : ElementStack elements_;
183 : char const * pos_;
184 : char const * end_;
185 : State state_;
186 : Attributes attributes_;
187 : Attributes::iterator currentAttribute_;
188 : bool firstAttribute_;
189 : Pad pad_;
190 : };
191 :
192 : }
193 :
194 : #endif
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|