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