1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AAPT_XML_PULL_PARSER_H
#define AAPT_XML_PULL_PARSER_H
#include "Resource.h"
#include "process/IResourceTableConsumer.h"
#include "util/Maybe.h"
#include "util/StringPiece.h"
#include "xml/XmlUtil.h"
#include <algorithm>
#include <expat.h>
#include <istream>
#include <ostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
namespace aapt {
namespace xml {
class XmlPullParser : public IPackageDeclStack {
public:
enum class Event {
kBadDocument,
kStartDocument,
kEndDocument,
kStartNamespace,
kEndNamespace,
kStartElement,
kEndElement,
kText,
kComment,
};
/**
* Skips to the next direct descendant node of the given startDepth,
* skipping namespace nodes.
*
* When nextChildNode returns true, you can expect Comments, Text, and StartElement events.
*/
static bool nextChildNode(XmlPullParser* parser, size_t startDepth);
static bool skipCurrentElement(XmlPullParser* parser);
static bool isGoodEvent(Event event);
XmlPullParser(std::istream& in);
~XmlPullParser();
/**
* Returns the current event that is being processed.
*/
Event getEvent() const;
const std::string& getLastError() const;
/**
* Note, unlike XmlPullParser, the first call to next() will return
* StartElement of the first element.
*/
Event next();
//
// These are available for all nodes.
//
const std::u16string& getComment() const;
size_t getLineNumber() const;
size_t getDepth() const;
/**
* Returns the character data for a Text event.
*/
const std::u16string& getText() const;
//
// Namespace prefix and URI are available for StartNamespace and EndNamespace.
//
const std::u16string& getNamespacePrefix() const;
const std::u16string& getNamespaceUri() const;
//
// These are available for StartElement and EndElement.
//
const std::u16string& getElementNamespace() const;
const std::u16string& getElementName() const;
/*
* Uses the current stack of namespaces to resolve the package. Eg:
* xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
* ...
* android:text="@app:string/message"
*
* In this case, 'app' will be converted to 'com.android.app'.
*
* If xmlns:app="http://schemas.android.com/apk/res-auto", then
* 'package' will be set to 'defaultPackage'.
*/
Maybe<ExtractedPackage> transformPackageAlias(
const StringPiece16& alias, const StringPiece16& localPackage) const override;
//
// Remaining methods are for retrieving information about attributes
// associated with a StartElement.
//
// Attributes must be in sorted order (according to the less than operator
// of struct Attribute).
//
struct Attribute {
std::u16string namespaceUri;
std::u16string name;
std::u16string value;
int compare(const Attribute& rhs) const;
bool operator<(const Attribute& rhs) const;
bool operator==(const Attribute& rhs) const;
bool operator!=(const Attribute& rhs) const;
};
using const_iterator = std::vector<Attribute>::const_iterator;
const_iterator beginAttributes() const;
const_iterator endAttributes() const;
size_t getAttributeCount() const;
const_iterator findAttribute(StringPiece16 namespaceUri, StringPiece16 name) const;
private:
static void XMLCALL startNamespaceHandler(void* userData, const char* prefix, const char* uri);
static void XMLCALL startElementHandler(void* userData, const char* name, const char** attrs);
static void XMLCALL characterDataHandler(void* userData, const char* s, int len);
static void XMLCALL endElementHandler(void* userData, const char* name);
static void XMLCALL endNamespaceHandler(void* userData, const char* prefix);
static void XMLCALL commentDataHandler(void* userData, const char* comment);
struct EventData {
Event event;
size_t lineNumber;
size_t depth;
std::u16string data1;
std::u16string data2;
std::vector<Attribute> attributes;
};
std::istream& mIn;
XML_Parser mParser;
char mBuffer[16384];
std::queue<EventData> mEventQueue;
std::string mLastError;
const std::u16string mEmpty;
size_t mDepth;
std::stack<std::u16string> mNamespaceUris;
struct PackageDecl {
std::u16string prefix;
ExtractedPackage package;
};
std::vector<PackageDecl> mPackageAliases;
};
/**
* Finds the attribute in the current element within the global namespace.
*/
Maybe<StringPiece16> findAttribute(const XmlPullParser* parser, const StringPiece16& name);
/**
* Finds the attribute in the current element within the global namespace. The attribute's value
* must not be the empty string.
*/
Maybe<StringPiece16> findNonEmptyAttribute(const XmlPullParser* parser, const StringPiece16& name);
//
// Implementation
//
inline ::std::ostream& operator<<(::std::ostream& out, XmlPullParser::Event event) {
switch (event) {
case XmlPullParser::Event::kBadDocument: return out << "BadDocument";
case XmlPullParser::Event::kStartDocument: return out << "StartDocument";
case XmlPullParser::Event::kEndDocument: return out << "EndDocument";
case XmlPullParser::Event::kStartNamespace: return out << "StartNamespace";
case XmlPullParser::Event::kEndNamespace: return out << "EndNamespace";
case XmlPullParser::Event::kStartElement: return out << "StartElement";
case XmlPullParser::Event::kEndElement: return out << "EndElement";
case XmlPullParser::Event::kText: return out << "Text";
case XmlPullParser::Event::kComment: return out << "Comment";
}
return out;
}
inline bool XmlPullParser::nextChildNode(XmlPullParser* parser, size_t startDepth) {
Event event;
// First get back to the start depth.
while (isGoodEvent(event = parser->next()) && parser->getDepth() > startDepth + 1) {}
// Now look for the first good node.
while ((event != Event::kEndElement || parser->getDepth() > startDepth) && isGoodEvent(event)) {
switch (event) {
case Event::kText:
case Event::kComment:
case Event::kStartElement:
return true;
default:
break;
}
event = parser->next();
}
return false;
}
inline bool XmlPullParser::skipCurrentElement(XmlPullParser* parser) {
int depth = 1;
while (depth > 0) {
switch (parser->next()) {
case Event::kEndDocument:
return true;
case Event::kBadDocument:
return false;
case Event::kStartElement:
depth++;
break;
case Event::kEndElement:
depth--;
break;
default:
break;
}
}
return true;
}
inline bool XmlPullParser::isGoodEvent(XmlPullParser::Event event) {
return event != Event::kBadDocument && event != Event::kEndDocument;
}
inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
int cmp = namespaceUri.compare(rhs.namespaceUri);
if (cmp != 0) return cmp;
return name.compare(rhs.name);
}
inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
return compare(rhs) < 0;
}
inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
return compare(rhs) == 0;
}
inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
return compare(rhs) != 0;
}
inline XmlPullParser::const_iterator XmlPullParser::findAttribute(StringPiece16 namespaceUri,
StringPiece16 name) const {
const auto endIter = endAttributes();
const auto iter = std::lower_bound(beginAttributes(), endIter,
std::pair<StringPiece16, StringPiece16>(namespaceUri, name),
[](const Attribute& attr, const std::pair<StringPiece16, StringPiece16>& rhs) -> bool {
int cmp = attr.namespaceUri.compare(0, attr.namespaceUri.size(),
rhs.first.data(), rhs.first.size());
if (cmp < 0) return true;
if (cmp > 0) return false;
cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(), rhs.second.size());
if (cmp < 0) return true;
return false;
}
);
if (iter != endIter && namespaceUri == iter->namespaceUri && name == iter->name) {
return iter;
}
return endIter;
}
} // namespace xml
} // namespace aapt
#endif // AAPT_XML_PULL_PARSER_H
|