Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 79 additions & 20 deletions src/main/java/com/jeldoclet/XMLNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Copy link
Member

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.


out.append( tabs + "<" + _namespacePrefix + _type );
Iterator attrIterator = _attributes.keySet().iterator();
Expand Down Expand Up @@ -218,28 +217,88 @@ public String toString(String tabs)
return out.toString();
}

/**
* Encodes strings as XML. Check for <, >, ', ", &.
// /**
// * Encodes strings as XML. Check for <, >, ', ", &.
Copy link
Member

Choose a reason for hiding this comment

The 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("&amp;");
// out = (ltPat.matcher(out)).replaceAll("&lt;");
// out = (gtPat.matcher(out)).replaceAll("&gt;");
// out = (aposPat.matcher(out)).replaceAll("&apos;");
// out = (quotPat.matcher(out)).replaceAll("&quot;");
//
// return out;
// }

/**
* Returns the string where all non-ascii and <, &, > are encoded as numeric entities. I.e. "&lt;A &amp; B &gt;"
* .... (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) {
Copy link
Member

Choose a reason for hiding this comment

The 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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that there are both StringBuffer and StringBuilder classes in java, I wouldn't call a variable of type StringBuilder like that. :) It's really misleading.

for (int i = 0; i < originalUnprotectedString.length(); i++) {
char ch = originalUnprotectedString.charAt(i);

out = (ampPat.matcher(out)).replaceAll("&amp;");
out = (ltPat.matcher(out)).replaceAll("&lt;");
out = (gtPat.matcher(out)).replaceAll("&gt;");
out = (aposPat.matcher(out)).replaceAll("&apos;");
out = (quotPat.matcher(out)).replaceAll("&quot;");
if (ch<32 || ch>126) {
// control characters or unicode but not Ascii
stringBuffer.append("&#" + (int) ch + ";");
anyCharactersProtected = true;
} else
switch (ch)
{
case '<':
stringBuffer.append("&lt;");
anyCharactersProtected = true;
break;
case '>':
stringBuffer.append("&gt;");
anyCharactersProtected = true;
break;
case '&':
stringBuffer.append("&amp;");
anyCharactersProtected = true;
break;
case '\'':
stringBuffer.append("&apos;");
anyCharactersProtected = true;
break;
case '"':
stringBuffer.append("&quot;");
anyCharactersProtected = true;
break;
default:
stringBuffer.append(ch);
anyCharactersProtected = true;
}
}
if (anyCharactersProtected == false) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

anyCharactersProtected is always true, as far as I can see.

I'd just get rid of this if block (and the whole anyCharactersProtected flag altogether) and always return stringBuffer.toString() as this seems like a really minor optimisation to me (if at all).

but if you want to keep it, then, probably, in the default case of the switch it should be:

default:
      stringBuffer.append(ch);
      anyCharactersProtected = false;
}

return originalUnprotectedString;
}

return out;
return stringBuffer.toString();
}
}