X-Git-Url: https://git.saurik.com/android/aapt.git/blobdiff_plain/1fcf29a0fb4443ce145dfc9dd1c2872312ec9a72..dd1acdc6f0c3523886ecaf7a881676257da9cf69:/XMLNode.cpp diff --git a/XMLNode.cpp b/XMLNode.cpp index 2a85bc7..95a68d1 100644 --- a/XMLNode.cpp +++ b/XMLNode.cpp @@ -45,6 +45,7 @@ bool isWhitespace(const char16_t* str) static const String16 RESOURCES_PREFIX(RESOURCES_ROOT_NAMESPACE); static const String16 RESOURCES_PRV_PREFIX(RESOURCES_ROOT_PRV_NAMESPACE); +static const String16 RESOURCES_TOOLS_NAMESPACE("http://schemas.android.com/tools"); String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic) { @@ -68,12 +69,118 @@ String16 getNamespaceResourcePackage(String16 namespaceUri, bool* outIsPublic) return String16(namespaceUri, namespaceUri.size()-prefixSize, prefixSize); } +status_t hasSubstitutionErrors(const char* fileName, + ResXMLTree* inXml, + String16 str16) +{ + const char16_t* str = str16.string(); + const char16_t* p = str; + const char16_t* end = str + str16.size(); + + bool nonpositional = false; + int argCount = 0; + + while (p < end) { + /* + * Look for the start of a Java-style substitution sequence. + */ + if (*p == '%' && p + 1 < end) { + p++; + + // A literal percent sign represented by %% + if (*p == '%') { + p++; + continue; + } + + argCount++; + + if (*p >= '0' && *p <= '9') { + do { + p++; + } while (*p >= '0' && *p <= '9'); + if (*p != '$') { + // This must be a size specification instead of position. + nonpositional = true; + } + } else if (*p == '<') { + // Reusing last argument; bad idea since it can be re-arranged. + nonpositional = true; + p++; + + // Optionally '$' can be specified at the end. + if (p < end && *p == '$') { + p++; + } + } else { + nonpositional = true; + } + + // Ignore flags and widths + while (p < end && (*p == '-' || + *p == '#' || + *p == '+' || + *p == ' ' || + *p == ',' || + *p == '(' || + (*p >= '0' && *p <= '9'))) { + p++; + } + + /* + * This is a shortcut to detect strings that are going to Time.format() + * instead of String.format() + * + * Comparison of String.format() and Time.format() args: + * + * String: ABC E GH ST X abcdefgh nost x + * Time: DEFGHKMS W Za d hkm s w yz + * + * Therefore we know it's definitely Time if we have: + * DFKMWZkmwyz + */ + if (p < end) { + switch (*p) { + case 'D': + case 'F': + case 'K': + case 'M': + case 'W': + case 'Z': + case 'k': + case 'm': + case 'w': + case 'y': + case 'z': + return NO_ERROR; + } + } + } + + p++; + } + + /* + * If we have more than one substitution in this string and any of them + * are not in positional form, give the user an error. + */ + if (argCount > 1 && nonpositional) { + SourcePos(String8(fileName), inXml->getLineNumber()).error( + "Multiple substitutions specified in non-positional format; " + "did you mean to add the formatted=\"false\" attribute?\n"); + return NOT_ENOUGH_DATA; + } + + return NO_ERROR; +} + status_t parseStyledString(Bundle* bundle, const char* fileName, ResXMLTree* inXml, const String16& endTag, String16* outString, Vector* outSpans, + bool isFormatted, bool pseudolocalize) { Vector spanStack; @@ -101,7 +208,11 @@ status_t parseStyledString(Bundle* bundle, std::string pseudo = pseudolocalize_string(orig); curString.append(String16(String8(pseudo.c_str()))); } else { - curString.append(text); + if (isFormatted && hasSubstitutionErrors(fileName, inXml, text) != NO_ERROR) { + return UNKNOWN_ERROR; + } else { + curString.append(text); + } } } else if (code == ResXMLTree::START_TAG) { const String16 element16(inXml->getElementName(&len)); @@ -219,8 +330,13 @@ moveon: } spanStack.pop(); - if (empty) { - fprintf(stderr, "%s:%d: WARNING: empty '%s' span found in text '%s'\n", + /* + * This warning seems to be just an irritation to most people, + * since it is typically introduced by translators who then never + * see the warning. + */ + if (0 && empty) { + fprintf(stderr, "%s:%d: warning: empty '%s' span found in text '%s'\n", fileName, inXml->getLineNumber(), String8(spanTag).string(), String8(*outString).string()); @@ -336,13 +452,15 @@ void printXMLBlock(ResXMLTree* block) printf("=?0x%x", (int)value.data); } else if (value.dataType == Res_value::TYPE_STRING) { printf("=\"%s\"", - String8(block->getAttributeStringValue(i, &len)).string()); + ResTable::normalizeForOutput(String8(block->getAttributeStringValue(i, + &len)).string()).string()); } else { printf("=(type 0x%x)0x%x", (int)value.dataType, (int)value.data); } const char16_t* val = block->getAttributeStringValue(i, &len); if (val != NULL) { - printf(" (Raw: \"%s\")", String8(val).string()); + printf(" (Raw: \"%s\")", ResTable::normalizeForOutput(String8(val).string()). + string()); } printf("\n"); } @@ -473,6 +591,7 @@ XMLNode::XMLNode(const String8& filename, const String16& s1, const String16& s2 , mFilename(filename) , mStartLineNumber(0) , mEndLineNumber(0) + , mUTF8(false) { if (isNamespace) { mNamespacePrefix = s1; @@ -486,6 +605,7 @@ XMLNode::XMLNode(const String8& filename, const String16& s1, const String16& s2 XMLNode::XMLNode(const String8& filename) : mFilename(filename) { + memset(&mCharsValue, 0, sizeof(mCharsValue)); } XMLNode::type XMLNode::getType() const @@ -548,6 +668,19 @@ const XMLNode::attribute_entry* XMLNode::getAttribute(const String16& ns, return NULL; } +XMLNode::attribute_entry* XMLNode::editAttribute(const String16& ns, + const String16& name) +{ + for (size_t i=0; ins == ns && ae->name == name) { + return ae; + } + } + + return NULL; +} + const String16& XMLNode::getCData() const { return mChars; @@ -629,13 +762,16 @@ status_t XMLNode::addAttribute(const String16& ns, const String16& name, SourcePos(mFilename, getStartLineNumber()).error("Child to CDATA node."); return UNKNOWN_ERROR; } - attribute_entry e; - e.index = mNextAttributeIndex++; - e.ns = ns; - e.name = name; - e.string = value; - mAttributes.add(e); - mAttributeOrder.add(e.index, mAttributes.size()-1); + + if (ns != RESOURCES_TOOLS_NAMESPACE) { + attribute_entry e; + e.index = mNextAttributeIndex++; + e.ns = ns; + e.name = name; + e.string = value; + mAttributes.add(e); + mAttributeOrder.add(e.index, mAttributes.size()-1); + } return NO_ERROR; } @@ -831,7 +967,7 @@ status_t XMLNode::assignResourceIds(const sp& assets, status_t XMLNode::flatten(const sp& dest, bool stripComments, bool stripRawValues) const { - StringPool strings; + StringPool strings = StringPool(false, mUTF8); Vector resids; // First collect just the strings for attribute names that have a @@ -1083,11 +1219,13 @@ status_t XMLNode::collect_strings(StringPool* dest, Vector* outResIds, collect_attr_strings(dest, outResIds, true); int i; - if (mNamespacePrefix.size() > 0) { - dest->add(mNamespacePrefix, true); - } - if (mNamespaceUri.size() > 0) { - dest->add(mNamespaceUri, true); + if (RESOURCES_TOOLS_NAMESPACE != mNamespaceUri) { + if (mNamespacePrefix.size() > 0) { + dest->add(mNamespacePrefix, true); + } + if (mNamespaceUri.size() > 0) { + dest->add(mNamespaceUri, true); + } } if (mElementName.size() > 0) { dest->add(mElementName, true); @@ -1206,6 +1344,7 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp& de const void* extData = NULL; size_t extSize = 0; ResXMLTree_attribute attr; + bool writeCurrentNode = true; const size_t NA = mAttributes.size(); const size_t NC = mChildren.size(); @@ -1218,7 +1357,7 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp& de const String16 style16("style"); const type type = getType(); - + memset(&node, 0, sizeof(node)); memset(&attr, 0, sizeof(attr)); node.header.headerSize = htods(sizeof(node)); @@ -1263,17 +1402,21 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp& de } } } else if (type == TYPE_NAMESPACE) { - node.header.type = htods(RES_XML_START_NAMESPACE_TYPE); - extData = &namespaceExt; - extSize = sizeof(namespaceExt); - memset(&namespaceExt, 0, sizeof(namespaceExt)); - if (mNamespacePrefix.size() > 0) { - namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix)); + if (mNamespaceUri == RESOURCES_TOOLS_NAMESPACE) { + writeCurrentNode = false; } else { - namespaceExt.prefix.index = htodl((uint32_t)-1); + node.header.type = htods(RES_XML_START_NAMESPACE_TYPE); + extData = &namespaceExt; + extSize = sizeof(namespaceExt); + memset(&namespaceExt, 0, sizeof(namespaceExt)); + if (mNamespacePrefix.size() > 0) { + namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix)); + } else { + namespaceExt.prefix.index = htodl((uint32_t)-1); + } + namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix)); + namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri)); } - namespaceExt.prefix.index = htodl(strings.offsetForString(mNamespacePrefix)); - namespaceExt.uri.index = htodl(strings.offsetForString(mNamespaceUri)); LOG_ALWAYS_FATAL_IF(NA != 0, "Namespace nodes can't have attributes!"); } else if (type == TYPE_CDATA) { node.header.type = htods(RES_XML_CDATA_TYPE); @@ -1290,9 +1433,11 @@ status_t XMLNode::flatten_node(const StringPool& strings, const sp& de node.header.size = htodl(sizeof(node) + extSize + (sizeof(attr)*NA)); - dest->writeData(&node, sizeof(node)); - if (extSize > 0) { - dest->writeData(extData, extSize); + if (writeCurrentNode) { + dest->writeData(&node, sizeof(node)); + if (extSize > 0) { + dest->writeData(extData, extSize); + } } for (i=0; i& de dest->writeData(&node, sizeof(node)); dest->writeData(&endElementExt, sizeof(endElementExt)); } else if (type == TYPE_NAMESPACE) { - node.header.type = htods(RES_XML_END_NAMESPACE_TYPE); - node.lineNumber = htodl(getEndLineNumber()); - node.comment.index = htodl((uint32_t)-1); - node.header.size = htodl(sizeof(node)+extSize); - dest->writeData(&node, sizeof(node)); - dest->writeData(extData, extSize); + if (writeCurrentNode) { + node.header.type = htods(RES_XML_END_NAMESPACE_TYPE); + node.lineNumber = htodl(getEndLineNumber()); + node.comment.index = htodl((uint32_t)-1); + node.header.size = htodl(sizeof(node)+extSize); + dest->writeData(&node, sizeof(node)); + dest->writeData(extData, extSize); + } } return NO_ERROR;