
org.wiztools.commons.XmlEntityEncode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-lib Show documentation
Show all versions of commons-lib Show documentation
Commons library used across WizTools.org projects
The newest version!
/*
* Copyright WizTools.org
* Licensed under the Apache License, Version 2.0:
* http://www.apache.org/licenses/LICENSE-2.0
*/
package org.wiztools.commons;
/**
* Convenience method to encode XML pre-defined entity characters.
* @author subwiz
*/
public final class XmlEntityEncode {
private XmlEntityEncode(){}
/**
* Converts XML special characters like < to corresponding encoded value <.
* @param input
* @return String which can be embeded safely inside XML document.
*/
public static String encode(final String input){
final StringBuilder sb = new StringBuilder();
for(final char c: input.toCharArray()) {
switch(c) {
case '&':
sb.append("&");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '\'':
sb.append("'");
break;
case '"':
sb.append(""");
break;
default:
sb.append(c);
}
}
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy