You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/08/16 01:39:57 UTC
svn commit: r566411 - in
/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net:
URIEncoderDecoder.cpp URIEncoderDecoder.h
Author: tabish
Date: Wed Aug 15 16:39:56 2007
New Revision: 566411
URL: http://svn.apache.org/viewvc?view=rev&rev=566411
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Adding start of URI class
Modified:
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.cpp
activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.h
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.cpp?view=diff&rev=566411&r1=566410&r2=566411
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.cpp (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.cpp Wed Aug 15 16:39:56 2007
@@ -24,6 +24,9 @@
using namespace decaf::internal::net
////////////////////////////////////////////////////////////////////////////////
+const std::string URIEncoderDecoder::digits = "0123456789ABCDEF";
+
+////////////////////////////////////////////////////////////////////////////////
URIEncoderDecoder::URIEncoderDecoder() {
}
@@ -35,21 +38,23 @@
for( int i = 0; itr != s.end(); ++i, ++iter ) {
- if (ch == '%') {
- do {
- if (i + 2 >= s.length()) {
- throw new URISyntaxException(s, Msg.getString("K0313"), //$NON-NLS-1$
- i);
- }
- int d1 = Character.digit(s.charAt(i + 1), 16);
- int d2 = Character.digit(s.charAt(i + 2), 16);
- if (d1 == -1 || d2 == -1) {
- throw new URISyntaxException(s, Msg.getString("K0314", //$NON-NLS-1$
- s.substring(i, i + 3)), i);
- }
+ if( ch == '%' ) {
+ if( i + 2 >= s.length() ) {
+ throw URISyntaxException(
+ __FILE__, __LINE__, s,
+ "invalid Encoded data", i );
+ }
- i += 3;
- } while (i < s.length() && s.charAt(i) == '%');
+ int d1 = Character::digit( *(++iter), 16 );
+ int d2 = Character::digit( *(++iter), 16 );
+
+ if( d1 == -1 || d2 == -1 ) {
+ throw URISyntaxException(
+ __FILE__, __LINE__, s,
+ "Invalid Hex Digit in char", i );
+ }
+
+ i += 2;
continue;
}
@@ -83,127 +88,83 @@
}
}
-/**
- * All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
- * and legal characters are converted into their hexidecimal value prepended
- * by '%'.
- * <p>
- * For example: '#' -> %23
- * <p>
- * Other characters, which are Unicode chars that are not US-ASCII, and are
- * not ISO Control or are not ISO Space chars, are preserved.
- * <p>
- * Called from URI.quoteComponent() (for multiple argument constructors)
- * <p>
- *
- * @param s
- * java.lang.const std::string& the string to be converted
- * @param legal
- * java.lang.const std::string& the characters allowed to be preserved in the
- * string s
- * @return java.lang.const std::string& the converted string
- */
-static const std::string& quoteIllegal(const std::string& s, const std::string& legal)
- throws UnsupportedEncodingException {
- const std::string&Buffer buf = new const std::string&Buffer();
- for (int i = 0; i < s.length(); i++) {
- char ch = s.charAt(i);
- if ((ch >= 'a' && ch <= 'z')
- || (ch >= 'A' && ch <= 'Z')
- || (ch >= '0' && ch <= '9')
- || legal.indexOf(ch) > -1
- || (ch > 127 && !Character.isSpaceChar(ch) && !Character
- .isISOControl(ch))) {
+////////////////////////////////////////////////////////////////////////////////
+std::string URIEncoderDecoder::quoteIllegal( const std::string& s,
+ const std::string& legal );
+
+ std::string buf = "";
+ std::string::const_iterator iter = s.begin();
+
+ for( ; iter != s.end(); ++iter ) {
+
+ char ch = *iter;
+
+ if( Character::isLetterOrDigit( ch ) ||
+ legal.find( ch ) > string::npos ||
+ ( (unsigned char)ch > 127 &&
+ !Character.isSpaceChar(ch) &&
+ !Character.isISOControl(ch) ) ) {
+
buf.append(ch);
} else {
- byte[] bytes = new const std::string&(new char[] { ch }).getBytes(encoding);
- for (int j = 0; j < bytes.length; j++) {
- buf.append('%');
- buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
- buf.append(digits.charAt(bytes[j] & 0xf));
- }
+ buf.append('%');
+ buf.append( digits.at( ( ch & 0xf0 ) >> 4 ) );
+ buf.append( digits.at( bytes[j] & 0xf ) );
}
}
- return buf.toconst std::string&();
+
+ return buf;
}
-/**
- * Other characters, which are Unicode chars that are not US-ASCII, and are
- * not ISO Control or are not ISO Space chars are not preserved. They are
- * converted into their hexidecimal value prepended by '%'.
- * <p>
- * For example: Euro currency symbol -> "%E2%82%AC".
- * <p>
- * Called from URI.toASCIIconst std::string&()
- * <p>
- *
- * @param s
- * java.lang.const std::string& the string to be converted
- * @return java.lang.const std::string& the converted string
- */
-static std::string encodeOthers(const std::string& s) throws UnsupportedEncodingException {
+////////////////////////////////////////////////////////////////////////////////
+std::string URIEncoderDecoder::encodeOthers( const std::string& s ) {
std::string buf = "";
- for (int i = 0; i < s.length(); i++) {
- char ch = s.charAt(i);
- if (ch <= 127) {
- buf.append(ch);
+ for( int i = 0; i < s.length(); i++ ) {
+ char ch = s.at(i);
+ if( ch <= 127 ) {
+ buf.append( ch );
} else {
- byte[] bytes = new const std::string&(new char[] { ch }).getBytes(encoding);
- for (int j = 0; j < bytes.length; j++) {
- buf.append('%');
- buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
- buf.append(digits.charAt(bytes[j] & 0xf));
- }
+ buf.append('%');
+ buf.append( digits.at( ( ch & 0xf0 ) >> 4 ) );
+ buf.append( digits.at( ch & 0xf ) );
}
}
return buf;
}
-/**
- * Decodes the string argument which is assumed to be encoded in the
- * <code>x-www-form-urlencoded</code> MIME content type using the UTF-8
- * encoding scheme.
- * <p>
- * '%' and two following hex digit characters are converted to the
- * equivalent byte value. All other characters are passed through
- * unmodified.
- *
- * <p>
- * e.g. "A%20B%20C %24%25" -> "A B C $%"
- * <p>
- * Called from URI.getXYZ() methods
- * <p>
- *
- * @param s
- * java.lang.const std::string& The encoded string.
- * @return java.lang.const std::string& The decoded version.
- */
-static const std::string& decode(const std::string& s) throws UnsupportedEncodingException {
+////////////////////////////////////////////////////////////////////////////////
+std::string URIEncoderDecoder::decode( const std::string& s ) {
std::string result = "";
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- for (int i = 0; i < s.length();) {
+ for( int i = 0; i < s.length(); ) {
char c = s.charAt(i);
- if (c == '%') {
- out.reset();
+ if( c == '%' ) {
+
do {
- if (i + 2 >= s.length()) {
- throw new IllegalArgumentException(Msg.getconst std::string&(
- "K01fe", i)); //$NON-NLS-1$
+ if( i + 2 >= s.length() ) {
+ throw IllegalArgumentException(
+ __FILE__, __LINE__,
+ "String has invalid encoding: %s", s.c_str() );
}
- int d1 = Character.digit(s.charAt(i + 1), 16);
- int d2 = Character.digit(s.charAt(i + 2), 16);
- if (d1 == -1 || d2 == -1) {
- throw new IllegalArgumentException(Msg.getconst std::string&(
- "K01ff", s.substring(i, i + 3), //$NON-NLS-1$
- const std::string&.valueOf(i)));
+
+ int d1 = Character.digit( s.at(i + 1), 16);
+ int d2 = Character.digit( s.at(i + 2), 16);
+
+ if( d1 == -1 || d2 == -1 ) {
+ throw IllegalArgumentException(
+ __FILE__, __LINE__,
+ "URIEncoderDecoder::decode - Bad hex encoding in: ",
+ s.c_str() );
}
- out.write((byte) ((d1 << 4) + d2));
+
+ result.append( (unsigned char)( ( d1 << 4 ) + d2 ) );
i += 3;
- } while (i < s.length() && s.charAt(i) == '%');
- result.append(out.toconst std::string&(encoding));
+
+ } while( i < s.length() && s.at(i) == '%' );
+
continue;
}
+
result.append(c);
i++;
}
Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.h?view=diff&rev=566411&r1=566410&r2=566411
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.h (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/internal/net/URIEncoderDecoder.h Wed Aug 15 16:39:56 2007
@@ -27,6 +27,10 @@
namespace net{
class URIEncoderDecoder {
+ private:
+
+ static const std::string digits;
+
public:
URIEncoderDecoder();
@@ -58,6 +62,48 @@
*/
static void validateSimple( const std::string& s, const std::string& legal )
throw ( URISyntaxException );
+
+ /**
+ * All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
+ * and legal characters are converted into their hexidecimal value prepended
+ * by '%'.
+ * <p>
+ * For example: '#' -> %23
+ * <p>
+ * Other characters, which are chars that are not US-ASCII, and are
+ * not ISO Control or are not ISO Space chars, are preserved.
+ * @param s - the string to be converted
+ * @param legal - the characters allowed to be preserved in the string s
+ * @return converted string
+ */
+ static std::string quoteIllegal( const std::string& s,
+ const std::string& legal );
+
+ /**
+ * Other characters, which are chars that are not US-ASCII, and are
+ * not ISO Control or are not ISO Space chars are not preserved. They are
+ * converted into their hexidecimal value prepended by '%'.
+ * <p>
+ * For example: Euro currency symbol -> "%E2%82%AC".
+ * @param s - the string to be converted
+ * @return the converted string
+ */
+ static std::string encodeOthers( const std::string& s );
+
+ /**
+ * Decodes the string argument which is assumed to be encoded in the
+ * <code>x-www-form-urlencoded</code> MIME content type using the UTF-8
+ * encoding scheme.
+ * <p>
+ * '%' and two following hex digit characters are converted to the
+ * equivalent byte value. All other characters are passed through
+ * unmodified.
+ * <p>
+ * e.g. "A%20B%20C %24%25" -> "A B C $%"
+ * @param s - The encoded string.
+ * @return The decoded version.
+ */
+ static std::string decode( const std::string& s );
};