ECode CKXmlSerializer::WriteCdSect( /* [in] */ const String& text) { FAIL_RETURN(Check(FALSE)); // BEGIN android-changed: ]]> is not allowed within a CDATA, // so break and start a new one when necessary. String data; StringUtils::ReplaceAll(text, String("]]>"), String("]]]]><![CDATA[>"), &data); // We also aren't allowed any invalid characters. AutoPtr<ArrayOf<Char32> > charArray = data.GetChars(); Char32 ch; Boolean valid; Int32 N = charArray->GetLength(); for (Int32 i = 0; i < N; i++) { ch = (*charArray)[i]; valid = (ch >= 0x20 && ch <= 0xd7ff) || (ch == '\t' || ch == '\n' || ch == '\r') || (ch >= 0xe000 && ch <= 0xfffd); if (!valid) { return ReportInvalidCharacter(ch); } } FAIL_RETURN(IWriter::Probe(mWriter)->Write(String("<![CDATA["))); FAIL_RETURN(IWriter::Probe(mWriter)->Write(data)); return IWriter::Probe(mWriter)->Write(String("]]>")); // END android-changed }
ECode Rfc822Token::QuoteNameIfNecessary( /* [in] */ const String& name, /* [out] */ String* str) { VALIDATE_NOT_NULL(str) AutoPtr<ArrayOf<Char32> > chars = name.GetChars(); Int32 len = chars->GetLength(); Char32 c; for (Int32 i = 0; i < len; i++) { c = (*chars)[i]; if (! ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == ' ') || (c >= '0' && c <= '9'))) { StringBuilder sb; sb += "\""; String qn; QuoteName(name, &qn); sb += qn; sb += "\""; *str = sb.ToString(); return NOERROR; } } *str = name; return NOERROR; }
ECode CFastXmlSerializer::EscapeAndAppendString( /* [in] */ const String& string) { AutoPtr<ArrayOf<Char32> > charArray = string.GetChars(); Int32 N = charArray->GetLength(); Char32 NE = (Char32)ESCAPE_TABLE->GetLength(); AutoPtr<ArrayOf<String> > escapes = ESCAPE_TABLE; Int32 lastPos = 0; Int32 pos; Char32 c; String escape; for (pos=0; pos < N; pos++) { c = (*charArray)[pos]; if (c >= NE) continue; escape = (*escapes)[c]; if (escape.IsNull()) continue; if (lastPos < pos) FAIL_RETURN(Append(string, lastPos, pos-lastPos)); lastPos = pos + 1; FAIL_RETURN(Append(escape)); } if (lastPos < pos) FAIL_RETURN(Append(string, lastPos, pos-lastPos)); return NOERROR; }
/** * @return True iff the url is correctly URL encoded */ Boolean URLUtil::VerifyURLEncoding( /* [in] */ const String& url) { AutoPtr<ArrayOf<Char32> > chars = url.GetChars(); Int32 count = chars->GetLength(); if (count == 0) { return FALSE; } Int32 index = url.IndexOf('%'); while (index >= 0 && index < count) { if (index < count - 2) { //try { Int32 byteOut; if (FAILED(ParseHex((Byte) (*chars)[++index], &byteOut))) { return FALSE; } if (FAILED(ParseHex((Byte) (*chars)[++index], &byteOut))) { return FALSE; } //} catch (IllegalArgumentException e) { // return false; //} } else { return FALSE; } index = url.IndexOf('%', index + 1); } return TRUE; }
String File::FixSlashes( /* [in] */ const String& origPath) { // Remove duplicate adjacent slashes. Boolean lastWasSlash = FALSE; AutoPtr< ArrayOf<Char32> > newPath = origPath.GetChars(); Int32 length = newPath->GetLength(); Int32 newLength = 0; Char32 ch; for (Int32 i = 0; i < length; ++i) { ch = (*newPath)[i]; if (ch == '/') { if (!lastWasSlash) { (*newPath)[newLength++] = sSeparatorChar; lastWasSlash = TRUE; } } else { (*newPath)[newLength++] = ch; lastWasSlash = FALSE; } } // Remove any trailing slash (unless this is the root of the file system). if (lastWasSlash && newLength > 1) { newLength--; } // Reuse the original string if possible. return (newLength != length) ? String(*newPath.Get(), 0, newLength) : origPath; }
ECode UriCodec::Decode( /* [in] */ const String& s, /* [in] */ Boolean convertPlus, /* [in] */ ICharset* charset, /* [in] */ Boolean throwOnFailure, /* [out] */ String* decodedString) { VALIDATE_NOT_NULL(decodedString); if (s.IndexOf('%') == -1 && (!convertPlus || s.IndexOf('+') == -1)) { *decodedString = s; return NOERROR; } StringBuilder result(s.GetByteLength()); AutoPtr<IByteArrayOutputStream> out; CByteArrayOutputStream::New((IByteArrayOutputStream**)&out); IOutputStream* os = IOutputStream::Probe(out); AutoPtr<ArrayOf<Char32> > char32Array = s.GetChars(); for (Int32 i = 0; i < s.GetLength();) { Char32 c = (*char32Array)[i]; if (c == '%') { do { Int32 d1, d2; if (i + 2 < s.GetLength() && (d1 = HexToInt((*char32Array)[i + 1])) != -1 && (d2 = HexToInt((*char32Array)[i + 2])) != -1) { os->Write((Byte) ((d1 << 4) + d2)); } else if (throwOnFailure) { return E_ILLEGAL_ARGUMENT_EXCEPTION; } else { // TODO: unicode escape const char* chars = "\ufffd"; AutoPtr<ArrayOf<Byte> > replacement = GetBytes(chars, charset); os->Write(replacement, 0, replacement->GetLength()); } i += 3; } while (i < s.GetLength() && (*char32Array)[i] == '%'); AutoPtr<ArrayOf<Byte> > bytes; out->ToByteArray((ArrayOf<Byte>**)&bytes); //result.append(new String(out.toByteArray(), charset); result.Append(String((char*)bytes->GetPayload())); out->Reset(); } else { if (convertPlus && c == '+') { c = ' '; } result.AppendChar(c); i++; } } *decodedString = result.ToString(); return NOERROR; }
Int32 UrlUtils::FindFirstOf( /* [in] */ const String& string, /* [in] */ const String& chars, /* [in] */ Int32 start, /* [in] */ Int32 end) { AutoPtr<ArrayOf<Char32> > char32Array = string.GetChars(); for (Int32 i = start; i < end; i++) { if (chars.IndexOf((*char32Array)[i]) != -1) { return i; } } return end; }
int CTest::testParseInputStream(int argc, char* argv[]) { String snippet = String("<dagny dad=\"bob\">hello</dagny>"); AutoPtr<IXmlPullParser> parser = newPullParser(); parser->SetFeature(FEATURE_PROCESS_NAMESPACES, false); AutoPtr<ArrayOf<Char32> > gchars = snippet.GetChars(); AutoPtr< ArrayOf<Byte> > buffer = ArrayOf<Byte>::Alloc(gchars->GetLength()); for (int i = 0; i < snippet.GetLength(); ++i) { (*buffer)[i] = (*gchars)[i] & 0xFF; } AutoPtr<IByteArrayInputStream> binput; CByteArrayInputStream::New(buffer, (IByteArrayInputStream**)&binput); parser->SetInputEx(binput, String("UTF-8")); validate(parser); return 0; }
ECode UriCodec::ValidateSimple( /* [in] */ const String& s, /* [in] */ const String& legal) { AutoPtr<ArrayOf<Char32> > char32Array = s.GetChars(); for(Int32 i = 0; i < char32Array->GetLength(); i++) { Char32 ch = (*char32Array)[i]; if(!((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || legal.IndexOf(ch) > -1)) { return E_URI_SYNTAX_EXCEPTION; } } return NOERROR; }
ECode CWifiInfo::RemoveDoubleQuotes( /* [in] */ const String& str, /* [out] */ String* value) { VALIDATE_NOT_NULL(value); if (str.IsNull()) return NOERROR; AutoPtr<ArrayOf<Char32> > charArray = str.GetChars(); const Int32 length = charArray->GetLength(); if ((length > 1) && ((*charArray)[0] == '"') && ((*charArray)[length - 1] == '"')) { *value = str.Substring(1, length - 1); return NOERROR; } *value = str; return NOERROR; }
ECode CURI::ValidateUserInfo( /* [in] */ const String& uri, /* [in] */ const String& userInfo, /* [in] */ Int32 index) { AutoPtr<ArrayOf<Char32> > char32Array = userInfo.GetChars(); if (char32Array) { for (Int32 i = 0; i < char32Array->GetLength(); i++) { if ((*char32Array)[i] == ']' || (*char32Array)[i] == '[') { // throw new URISyntaxException(uri, "Illegal character in userInfo", index + i); return E_URI_SYNTAX_EXCEPTION; } } } return NOERROR; }
ECode UriCodec::AppendEncoded( /* [in] */ IStringBuilder * builder, /* [in] */ const String& s, /* [in] */ ICharset* charset, /* [in] */ Boolean isPartiallyEncoded) { if (s.IsNull()) return E_NULL_POINTER_EXCEPTION; Int32 escapeStart = -1; AutoPtr<ArrayOf<Char32> > char32Array = s.GetChars(); for (Int32 i = 0; i < s.GetLength(); i++) { Char32 c = (*char32Array)[i]; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || IsRetained(c) || (c == '%' && isPartiallyEncoded)) { if (escapeStart != -1) { AppendHex(builder, s.Substring(escapeStart, i), charset); escapeStart = -1; } if (c =='%' && isPartiallyEncoded){ builder->Append(s.Substring(i, Elastos::Core::Math::Min(i + 3, s.GetLength()))); i += 2; } else if (c == ' '){ builder->AppendChar('+'); } else{ builder->AppendChar(c); } } else if(escapeStart == -1) { escapeStart = i; } } if (escapeStart != -1) { AppendHex(builder, s.Substring(escapeStart, s.GetLength()), charset); } return NOERROR; }
String UrlUtils::GetSchemePrefix( /* [in] */const String& spec) { Int32 colon = spec.IndexOf(':'); if (colon < 1) { return String(NULL); } AutoPtr<ArrayOf<Char32> > char32Array = spec.GetChars(); for (int i = 0; i < colon; i++) { if (!IsValidSchemeChar(i, (Byte)(*char32Array)[i])) { return String(NULL); } } String result = spec.Substring(0, colon).ToLowerCase(); return result; }
ECode XmlBlock::Parser::GetTextCharacters( /* [out] */ ArrayOf<Int32>* holderForStartAndLength, /* [out, callee] */ ArrayOf<Char32>** textChars) { VALIDATE_NOT_NULL(textChars) *textChars = NULL; VALIDATE_NOT_NULL(holderForStartAndLength) String txt; GetText(&txt); AutoPtr< ArrayOf<Char32> > chars; if (!txt.IsNull()) { (*holderForStartAndLength)[0] = 0; (*holderForStartAndLength)[1] = txt.GetLength(); chars = txt.GetChars(); } *textChars = chars; REFCOUNT_ADD(*textChars); return NOERROR; }
ECode FormatBase::UpToWithQuotes( /* [in] */ const String& string, /* [in] */ IParsePosition* position, /* [in] */ StringBuffer& buffer, /* [in] */ Char32 stop, /* [in] */ Char32 start, /* [out] */ Boolean* succeeded) { VALIDATE_NOT_NULL(succeeded); *succeeded = FALSE; VALIDATE_NOT_NULL(position); Int32 index, length, count = 1; position->GetIndex(&index); AutoPtr<ArrayOf<Char32> > charArray = string.GetChars(); length = charArray->GetLength(); Boolean quote = FALSE; Char32 ch; while (index < length) { ch = (*charArray)[index++]; if (ch == '\'') { quote = !quote; } if (!quote) { if (ch == stop) { count--; } if (count == 0) { position->SetIndex(index); *succeeded = TRUE; return NOERROR; } if (ch == start) { count++; } } buffer.AppendChar(ch); } return E_ILLEGAL_ARGUMENT_EXCEPTION; }
ECode CURI::ValidateScheme( /* [in] */ const String& uri, /* [in] */ Int32 end, /* [out] */ String* result) { VALIDATE_NOT_NULL(result) *result = String(NULL); if (end == 0) { return E_URI_SYNTAX_EXCEPTION; } AutoPtr<ArrayOf<Char32> > char32Array = uri.GetChars(); for (Int32 i = 0; i < end; i++) { if (!UrlUtils::IsValidSchemeChar(i, (*char32Array)[i])) { return E_URI_SYNTAX_EXCEPTION; } } *result = uri.Substring(0, end); return NOERROR; }
ECode FormatBase::UpTo( /* [in] */ const String& string, /* [in] */ IParsePosition* position, /* [in] */ StringBuffer& buffer, /* [in] */ Char32 stop, /* [out] */ Boolean* succeeded) { VALIDATE_NOT_NULL(succeeded); *succeeded = FALSE; VALIDATE_NOT_NULL(position); Int32 index, length; position->GetIndex(&index); AutoPtr<ArrayOf<Char32> > charArray = string.GetChars(); length = charArray->GetLength(); Boolean lastQuote = FALSE, quote = FALSE; Char32 ch; while (index < length) { ch = (*charArray)[index++]; if (ch == '\'') { if (lastQuote) { buffer.AppendChar('\''); } quote = !quote; lastQuote = TRUE; } else if (ch == stop && !quote) { position->SetIndex(index); *succeeded = TRUE; return NOERROR; } else { lastQuote = FALSE; buffer.AppendChar(ch); } } position->SetIndex(index); return NOERROR; }
ECode Rfc822Token::QuoteComment( /* [in] */ const String& comment, /* [out] */ String* str) { VALIDATE_NOT_NULL(str) StringBuilder sb; AutoPtr<ArrayOf<Char32> > chars = comment.GetChars(); Int32 len = chars->GetLength(); Char32 c; for (Int32 i = 0; i < len; i++) { c = (*chars)[i]; if (c == '(' || c == ')' || c == '\\') { sb.AppendChar('\\'); } sb.AppendChar(c); } *str = sb.ToString(); return NOERROR; }
String Patterns::DigitsAndPlusOnly( /* [in] */ IMatcher* matcher) { if (matcher == NULL) { return String(NULL); } StringBuilder buffer; String matchingRegion; matcher->Group(&matchingRegion); AutoPtr<ArrayOf<Char32> > charArray = matchingRegion.GetChars(); Char32 character; for (UInt32 i = 0, size = charArray->GetLength(); i < size; i++) { character = (*charArray)[i]; if (character == '+' || Character::IsDigit(character)) { buffer += character; } } return buffer.ToString(); }
ECode UriCodec::Validate( /* [in] */ const String& uri, /* [in] */ Int32 start, /* [in] */ Int32 end, /* [in] */ const String& name, /* [out] */ String* result) { VALIDATE_NOT_NULL(result); *result = NULL; AutoPtr<ArrayOf<Char32> > char32Array = uri.GetChars(); for(Int32 i = start; i < end;) { Char32 ch = (*char32Array)[i]; if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || IsRetained(ch)){ ++i; } else if (ch == '%'){ if(i + 2 >= end){ return E_URI_SYNTAX_EXCEPTION; } Int32 d1 = HexToInt((*char32Array)[i+1]); Int32 d2 = HexToInt((*char32Array)[i+2]); if (d1 == -1 || d2 == -1) { return E_URI_SYNTAX_EXCEPTION; } i += 3; } else{ return E_URI_SYNTAX_EXCEPTION; } } *result = uri.Substring(start, end); return NOERROR; }
ECode MessageFormat::ApplyPattern( /* [in] */ const String& tem) { if (tem.IsNull()) { return NOERROR; } AutoPtr<ArrayOf<Char32> > charArray = tem.GetChars(); Int32 length = charArray->GetLength(); StringBuffer buffer(""); AutoPtr<IParsePosition> position; CParsePosition::New(0, (IParsePosition**)&position); List<String> localStrings; Int32 argCount = 0; AutoPtr<ArrayOf<Int32> > args = ArrayOf<Int32>::Alloc(10); Int32 maxArg = -1; List< AutoPtr<IFormat> > localFormats; Int32 index, arg, offset; position->GetIndex(&index); Boolean succeeded; Char32 ch; while (index < length) { if (FormatBase::UpTo(tem, position, buffer, '{', &succeeded), succeeded) { arg = 0; position->GetIndex(&offset); if (offset >= length) { return E_ILLEGAL_ARGUMENT_EXCEPTION; } // Get argument number while ((ch = (*charArray)[offset++]) != '}' && ch != ',') { if (ch < '0' && ch > '9') { return E_ILLEGAL_ARGUMENT_EXCEPTION; } arg = arg * 10 + (ch - '0'); if (arg < 0 || offset >= length) { return E_ILLEGAL_ARGUMENT_EXCEPTION; } } offset--; position->SetIndex(offset); AutoPtr<IFormat> f; FAIL_RETURN(ParseVariable(tem, position, (IFormat**)&f)); localFormats.PushBack(f); if (argCount >= args->GetLength()) { AutoPtr<ArrayOf<Int32> > newArgs = ArrayOf<Int32>::Alloc(args->GetLength() * 2); newArgs->Copy(args); args = newArgs; } (*args)[argCount++] = arg; if (arg > maxArg) { maxArg = arg; } } String outstr; buffer.Substring(0, buffer.GetLength(), &outstr); localStrings.PushBack(outstr); buffer.Reset(); position->GetIndex(&index); } mStrings = ArrayOf<String>::Alloc(localStrings.GetSize()); List<String>::Iterator it = localStrings.Begin(); for (Int32 i = 0; i < (Int32)(localStrings.GetSize()); ++i, ++it) { (*mStrings)[i] = *it; } mArgumentNumbers = args; mFormats = ArrayOf<IFormat* >::Alloc(argCount); List< AutoPtr<IFormat> >::Iterator ite = localFormats.Begin(); for (Int32 i = 0; i < argCount; ++i, ++ite) { mFormats->Set(i, *ite); } mMaxOffset = argCount - 1; mMaxArgumentIndex = maxArg; return NOERROR; }