-
Notifications
You must be signed in to change notification settings - Fork 3
Added support for UTF-8 characters less 32 or above 126 codes (out of ASCII range) #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Vector; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Represents an XML node | ||
|
@@ -178,7 +177,7 @@ public void save(String dir, String fileName, boolean includeNamespace, String o | |
*/ | ||
public String toString(String tabs) | ||
{ | ||
StringBuffer out = new StringBuffer(); | ||
StringBuilder out = new StringBuilder(); | ||
|
||
out.append( tabs + "<" + _namespacePrefix + _type ); | ||
Iterator attrIterator = _attributes.keySet().iterator(); | ||
|
@@ -218,28 +217,88 @@ public String toString(String tabs) | |
return out.toString(); | ||
} | ||
|
||
/** | ||
* Encodes strings as XML. Check for <, >, ', ", &. | ||
// /** | ||
// * Encodes strings as XML. Check for <, >, ', ", &. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about removing the commented out block? :) |
||
// * | ||
// * @param in The input string | ||
// * @return The encoded string. | ||
// */ | ||
// static protected String encode( String in ) | ||
// { | ||
// Pattern ampPat = Pattern.compile( "&" ); | ||
// Pattern ltPat = Pattern.compile( "<" ); | ||
// Pattern gtPat = Pattern.compile( ">" ); | ||
// Pattern aposPat = Pattern.compile( "\'" ); | ||
// Pattern quotPat = Pattern.compile( "\"" ); | ||
// | ||
// String out = new String( in ); | ||
// | ||
// out = (ampPat.matcher(out)).replaceAll("&"); | ||
// out = (ltPat.matcher(out)).replaceAll("<"); | ||
// out = (gtPat.matcher(out)).replaceAll(">"); | ||
// out = (aposPat.matcher(out)).replaceAll("'"); | ||
// out = (quotPat.matcher(out)).replaceAll("""); | ||
// | ||
// return out; | ||
// } | ||
|
||
/** | ||
* Returns the string where all non-ascii and <, &, > are encoded as numeric entities. I.e. "<A & B >" | ||
* .... (insert result here). The result is safe to include anywhere in a text field in an XML-string. If there was | ||
* no characters to protect, the original string is returned. | ||
* | ||
* @param in The input string | ||
* @return The encoded string. | ||
* @param originalUnprotectedString | ||
* original string which may contain characters either reserved in XML or with different representation | ||
* in different encodings (like 8859-1 and UFT-8) | ||
* @see https://stackoverflow.com/questions/439298/best-way-to-encode-text-data-for-xml-in-java | ||
* @return | ||
*/ | ||
static protected String encode( String in ) | ||
{ | ||
Pattern ampPat = Pattern.compile( "&" ); | ||
Pattern ltPat = Pattern.compile( "<" ); | ||
Pattern gtPat = Pattern.compile( ">" ); | ||
Pattern aposPat = Pattern.compile( "\'" ); | ||
Pattern quotPat = Pattern.compile( "\"" ); | ||
static String encode(String originalUnprotectedString) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The broken indentations in this method make it really hard to follow the flow |
||
if (originalUnprotectedString == null) { | ||
return null; | ||
} | ||
boolean anyCharactersProtected = false; | ||
|
||
String out = new String( in ); | ||
StringBuilder stringBuffer = new StringBuilder(originalUnprotectedString.length()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. given that there are both |
||
for (int i = 0; i < originalUnprotectedString.length(); i++) { | ||
char ch = originalUnprotectedString.charAt(i); | ||
|
||
out = (ampPat.matcher(out)).replaceAll("&"); | ||
out = (ltPat.matcher(out)).replaceAll("<"); | ||
out = (gtPat.matcher(out)).replaceAll(">"); | ||
out = (aposPat.matcher(out)).replaceAll("'"); | ||
out = (quotPat.matcher(out)).replaceAll("""); | ||
if (ch<32 || ch>126) { | ||
// control characters or unicode but not Ascii | ||
stringBuffer.append("&#" + (int) ch + ";"); | ||
anyCharactersProtected = true; | ||
} else | ||
switch (ch) | ||
{ | ||
case '<': | ||
stringBuffer.append("<"); | ||
anyCharactersProtected = true; | ||
break; | ||
case '>': | ||
stringBuffer.append(">"); | ||
anyCharactersProtected = true; | ||
break; | ||
case '&': | ||
stringBuffer.append("&"); | ||
anyCharactersProtected = true; | ||
break; | ||
case '\'': | ||
stringBuffer.append("'"); | ||
anyCharactersProtected = true; | ||
break; | ||
case '"': | ||
stringBuffer.append("""); | ||
anyCharactersProtected = true; | ||
break; | ||
default: | ||
stringBuffer.append(ch); | ||
anyCharactersProtected = true; | ||
} | ||
} | ||
if (anyCharactersProtected == false) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd just get rid of this but if you want to keep it, then, probably, in the default case of the switch it should be:
|
||
return originalUnprotectedString; | ||
} | ||
|
||
return out; | ||
return stringBuffer.toString(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a changed indentation (with one space?). Can be reverted.