Я нашел метод extend библиотеки Underscore.js очень полезен, когда я хочу передать объект в качестве параметра, который может быть либо изменен, либо полностью заменен .
function replaceOrModify(aObj) {
if (modify) {
aObj.setNewValue('foo');
} else {
var newObj = new MyObject();
// _.extend(destination, *sources)
_.extend(newObj, aObj);
}
}
StringEscapeUtils.unescapeXml(xml)
( commons-lang , загрузить )
Вот один, который я написал через десять минут. Он не использует регулярные выражения, а только простые итерации. Я не думаю, что это можно улучшить, чтобы быть намного быстрее.
public static String unescape(final String text) {
StringBuilder result = new StringBuilder(text.length());
int i = 0;
int n = text.length();
while (i < n) {
char charAt = text.charAt(i);
if (charAt != '&') {
result.append(charAt);
i++;
} else {
if (text.startsWith("&", i)) {
result.append('&');
i += 5;
} else if (text.startsWith("'", i)) {
result.append('\'');
i += 6;
} else if (text.startsWith(""", i)) {
result.append('"');
i += 6;
} else if (text.startsWith("<", i)) {
result.append('<');
i += 4;
} else if (text.startsWith(">", i)) {
result.append('>');
i += 4;
} else i++;
}
}
return result.toString();
}
Если вы работаете с JSP, используйте su: unescapeXml из openutils-elfnctions
Вот простой способ unescape XML. Он обрабатывает предопределенные XML-сущности и десятичные числовые объекты (& amp; #nnnn;). Изменение его для обработки шестнадцатеричных объектов (& amp; #xhhhh;) должно быть простым.
public static String unescapeXML( final String xml )
{
Pattern xmlEntityRegex = Pattern.compile( "&(#?)([^;]+);" );
//Unfortunately, Matcher requires a StringBuffer instead of a StringBuilder
StringBuffer unescapedOutput = new StringBuffer( xml.length() );
Matcher m = xmlEntityRegex.matcher( xml );
Map<String,String> builtinEntities = null;
String entity;
String hashmark;
String ent;
int code;
while ( m.find() ) {
ent = m.group(2);
hashmark = m.group(1);
if ( (hashmark != null) && (hashmark.length() > 0) ) {
code = Integer.parseInt( ent );
entity = Character.toString( (char) code );
} else {
//must be a non-numerical entity
if ( builtinEntities == null ) {
builtinEntities = buildBuiltinXMLEntityMap();
}
entity = builtinEntities.get( ent );
if ( entity == null ) {
//not a known entity - ignore it
entity = "&" + ent + ';';
}
}
m.appendReplacement( unescapedOutput, entity );
}
m.appendTail( unescapedOutput );
return unescapedOutput.toString();
}
private static Map<String,String> buildBuiltinXMLEntityMap()
{
Map<String,String> entities = new HashMap<String,String>(10);
entities.put( "lt", "<" );
entities.put( "gt", ">" );
entities.put( "amp", "&" );
entities.put( "apos", "'" );
entities.put( "quot", "\"" );
return entities;
}