You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xang-cvs@xml.apache.org by md...@apache.org on 2001/08/07 08:15:31 UTC
cvs commit: xml-xang/java/src/org/apache/xang/util TextUtil.java
mdierken 01/08/06 23:15:31
Modified: java/src/org/apache/xang/util TextUtil.java
Log:
Add encodeToURL()
Revision Changes Path
1.3 +121 -88 xml-xang/java/src/org/apache/xang/util/TextUtil.java
Index: TextUtil.java
===================================================================
RCS file: /home/cvs/xml-xang/java/src/org/apache/xang/util/TextUtil.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TextUtil.java 2000/03/14 00:49:40 1.2
+++ TextUtil.java 2001/08/07 06:15:31 1.3
@@ -2,7 +2,7 @@
* The Apache Software License, Version 1.1
*
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution,
- * if any, must include the following acknowledgment:
+ * if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
*
* 4. The names "Xang" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
- * software without prior written permission. For written
+ * software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
@@ -74,7 +74,7 @@
if( unencodedText == null )
return null;
boolean bNeedsWork=false;
-
+
int length = unencodedText.length();
for( int i = 0; i < length; i++ )
{
@@ -90,19 +90,19 @@
switch( suspect )
{
case '\\' : // the backslash needs to be escaped for XQL strings
- case '\"' :
- case '\'' :
- case '&' :
- case '<' :
+ case '\"' :
+ case '\'' :
+ case '&' :
+ case '<' :
case '>' :
- bNeedsWork=true;
+ bNeedsWork=true;
break;
}
}
-
+
if (bNeedsWork)
break;
- }
+ }
if (!bNeedsWork)
return unencodedText;
@@ -128,7 +128,7 @@
case '&' : out.append( "&" ); break;
case '<' : out.append( "<" ); break;
case '>' : out.append( ">" ); break;
-
+
/*
* These characters are escaped with a backslash for XQL strings:
* [\, ", ' ]
@@ -136,11 +136,11 @@
case '\\' : out.append( "\\\\" ); break;
case '\"' : out.append( "\\"" ); break;
case '\'' : out.append( "\\'" ); break;
-
+
default : out.append( suspect ); break;
}
}
- }
+ }
return out.toString();
}
@@ -152,7 +152,7 @@
if( unencodedText == null )
return null;
boolean bNeedsWork=false;
-
+
int length = unencodedText.length();
for( int i = 0; i < length; i++ )
{
@@ -160,15 +160,15 @@
switch( suspect )
{
case '\\' : // the backslash needs to be escaped for XQL strings
- case '\"' :
- case '\'' :
- bNeedsWork=true;
+ case '\"' :
+ case '\'' :
+ bNeedsWork=true;
break;
}
-
+
if (bNeedsWork)
break;
- }
+ }
if (!bNeedsWork)
return unencodedText;
@@ -188,14 +188,14 @@
case '\\' : out.append( "\\\\" ); break;
case '\"' : out.append( "\\\"" ); break;
case '\'' : out.append( "\\'" ); break;
-
+
default : out.append( suspect ); break;
}
}
- }
+ }
return out.toString();
}
-
+
/**
* Encodes XML reserved characters [', ", &, <, >] and character values above 127
*/
@@ -204,7 +204,7 @@
if( unencodedText == null )
return null;
boolean bNeedsWork=false;
-
+
int length = unencodedText.length();
for( int i = 0; i < length; i++ )
{
@@ -219,18 +219,18 @@
{
switch( suspect )
{
- case '\"' :
- case '\'' :
- case '&' :
- case '<' :
- case '>' : bNeedsWork=true;
+ case '\"' :
+ case '\'' :
+ case '&' :
+ case '<' :
+ case '>' : bNeedsWork=true;
break;
}
}
-
+
if (bNeedsWork)
break;
- }
+ }
if (!bNeedsWork)
return unencodedText;
@@ -260,19 +260,19 @@
default : out.append( suspect ); break;
}
}
- }
+ }
return out.toString();
}
-
+
/**
* Encodes HTML reserved characters [", &, <, >] and character values above 127 to entities.
*/
- public static String encodeToHTML( String unencodedText )
+ public static String encodeToHTML( String unencodedText )
{
if( unencodedText == null )
return null;
boolean bNeedsWork=false;
-
+
int length = unencodedText.length();
for( int i = 0; i < length; i++ )
{
@@ -287,17 +287,17 @@
{
switch( suspect )
{
- case '\"' :
- case '&' :
- case '<' :
- case '>' : bNeedsWork=true;
+ case '\"' :
+ case '&' :
+ case '<' :
+ case '>' : bNeedsWork=true;
break;
}
}
-
+
if (bNeedsWork)
break;
- }
+ }
if (!bNeedsWork)
return unencodedText;
@@ -307,7 +307,7 @@
int b;
StringBuffer out = new StringBuffer(length+16);
- for( int i = 0; i < length; i++ )
+ for( int i = 0; i < length; i++ )
{
suspect = unencodedText.charAt( i );
b = (int)suspect;
@@ -317,7 +317,7 @@
}
else
{
- switch( suspect )
+ switch( suspect )
{
case '\"' : out.append( """ ); break;
case '&' : out.append( "&" ); break;
@@ -327,21 +327,21 @@
}
}
- }
+ }
return out.toString();
}
-
+
/**
* Encodes HTML reserved characters [", &, <, >] and character values above 127 to entities,
* replacing spaces adjoining those characters with  .
* This is because our xsl engine tends to strip whitespace adjoining to entity refs.
*/
- public static String encodeToPreTransformationHTML( String unencodedText )
+ public static String encodeToPreTransformationHTML( String unencodedText )
{
if( unencodedText == null )
return null;
boolean bNeedsWork=false;
-
+
int length = unencodedText.length();
for( int i = 0; i < length; i++ )
{
@@ -356,17 +356,17 @@
{
switch( suspect )
{
- case '\"' :
- case '&' :
- case '<' :
- case '>' : bNeedsWork=true;
+ case '\"' :
+ case '&' :
+ case '<' :
+ case '>' : bNeedsWork=true;
break;
}
}
-
+
if (bNeedsWork)
break;
- }
+ }
if (!bNeedsWork)
return unencodedText;
@@ -376,7 +376,7 @@
int b;
StringBuffer out = new StringBuffer(length+16);
- for( int i = 0; i < length; i++ )
+ for( int i = 0; i < length; i++ )
{
suspect = unencodedText.charAt( i );
b = (int)suspect;
@@ -386,7 +386,7 @@
}
else
{
- switch( suspect )
+ switch( suspect )
{
case '\"' : i = escapeSpacesAroundEscapedChar( unencodedText, out, i, """ ); break;
case '&' : i = escapeSpacesAroundEscapedChar( unencodedText, out, i, "&" ); break;
@@ -396,49 +396,49 @@
}
}
- }
+ }
return out.toString();
}
-
+
/**
* If a space adjoins the escaped char, we need to replace it with  
*/
protected static int escapeSpacesAroundEscapedChar(String unencodedText, StringBuffer encodedText, int pos, String escapedChar)
{
int returnPos = pos;
-
+
if (pos > 0 && unencodedText.charAt(pos - 1) == ' ')
{
encodedText.setCharAt(encodedText.length() - 1, '&');
encodedText.append("#32;");
}
-
+
encodedText.append(escapedChar);
-
+
if (pos < unencodedText.length() - 1 && unencodedText.charAt(pos + 1) == ' ')
{
encodedText.append(" ");
returnPos++;
}
-
+
return returnPos;
}
/**
* Encodes quotes, backslash, and whitespace to JS string format.
*/
- public static String encodeToJS (String str)
+ public static String encodeToJS (String str)
{
char[] in = str.toCharArray();
int len = in.length;
boolean noEscape = true;
-
+
// scan for characters that need encoding
- for (int i = 0; i < len; i++)
+ for (int i = 0; i < len; i++)
{
char c = in[i];
- if (c == '\"' || c == '\'' || c == '\\' ||
- c == '\r' || c == '\n' || c == '\t')
+ if (c == '\"' || c == '\'' || c == '\\' ||
+ c == '\r' || c == '\n' || c == '\t')
{
noEscape = false;
break;
@@ -449,10 +449,10 @@
// convert characters
StringBuffer b = new StringBuffer(len+8);
- for (int i = 0; i < len; i++)
+ for (int i = 0; i < len; i++)
{
char c = in[i];
- switch (c)
+ switch (c)
{
case '\'':
case '\"':
@@ -462,27 +462,27 @@
case '\t': b.append("\\t"); break;
default: b.append(c); break;
}
- }
- return b.toString();
+ }
+ return b.toString();
}
/**
* Encodes quotes, backslash, whitespace and [&, <, >] to entities.
* NOTE: this does not encode single quote as an entity.
*/
- public static String encodeToHTMLWithinJS (String str)
+ public static String encodeToHTMLWithinJS (String str)
{
char[] in = str.toCharArray();
int len = in.length;
boolean noEscape = true;
// scan for characters that need encoding
- for (int i = 0; i < len; i++)
+ for (int i = 0; i < len; i++)
{
char c = in[i];
- if (c == '\"' || c == '\'' || c == '\\' ||
+ if (c == '\"' || c == '\'' || c == '\\' ||
c == '\r' || c == '\n' || c == '\t' ||
- c == '&' || c =='<' || c == '>')
+ c == '&' || c =='<' || c == '>')
{
noEscape = false;
break;
@@ -493,10 +493,10 @@
// convert characters
StringBuffer b = new StringBuffer(len+8);
- for (int i = 0; i < len; i++)
+ for (int i = 0; i < len; i++)
{
char c = in[i];
- switch (c)
+ switch (c)
{
case '\'':
case '\"':
@@ -520,7 +520,7 @@
/**
* Simplistic entity reference replacment utility
*/
- public static String replaceEntityRefs(String text, Hashtable values)
+ public static String replaceEntityRefs(String text, Hashtable values)
{
String key;
String value;
@@ -530,10 +530,10 @@
{
Enumeration keys = values.keys();
- while(keys.hasMoreElements())
+ while(keys.hasMoreElements())
{
key = (String)keys.nextElement();
- value = (String)values.get(key);
+ value = (String)values.get(key);
newString = replaceAll(newString,"&" + key + ";",value);
}
}
@@ -543,7 +543,7 @@
/**
* Search and replace
*/
- public static String replaceAll(String text, String find, String replace)
+ public static String replaceAll(String text, String find, String replace)
{
int replaceLen = replace.length();
int index = text.indexOf(find);
@@ -554,19 +554,19 @@
}
return text;
}
-
+
/**
- * display formats XML
+ * display formats XML
*/
public static String formatXML( String xml )
{
StringBuffer fxml = new StringBuffer();
-
+
// <BLA> ... </BLA>
// <BLA />
-
+
boolean fwaitForEnd = false;
-
+
for ( int i = 0; i < xml.length(); i++ )
{
char chr = xml.charAt( i );
@@ -586,8 +586,41 @@
}
}
}
-
+
return fxml.toString();
}
-
+
+ public static String encodeToURL(String str)
+ {
+ char[] in = str.toCharArray();
+ int len = in.length;
+ boolean noEscape = true;
+
+ // scan for characters that need encoding
+ for (int i = 0; i < len; i++)
+ {
+ char c = in[i];
+ if (c == '%')
+ {
+ noEscape = false;
+ break;
+ }
+ }
+ if (noEscape)
+ return str;
+
+ // convert characters
+ StringBuffer b = new StringBuffer(len+8);
+ for (int i = 0; i < len; i++)
+ {
+ char c = in[i];
+ switch (c)
+ {
+ case '%': b.append("%25"); break;
+ default: b.append(c); break;
+ }
+ }
+ return b.toString();
+
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xang-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xang-cvs-help@xml.apache.org