Java源码示例:org.jdom.Verifier
示例1
public static Map<String, String> convertStringToActionProperties(String text) {
PropertySplitter split = new PropertySplitter(text);
String tok = split.nextPair();
Map<String,String> props = new LinkedHashMap<String,String>();
while (tok != null) {
String[] prp = StringUtils.split(tok, "=", 2); //NOI18N
if (prp.length >= 1 ) {
String key = prp[0];
//in case the user adds -D by mistake, remove it to get a parsable xml file.
if (key.startsWith("-D")) { //NOI18N
key = key.substring("-D".length()); //NOI18N
}
if (key.startsWith("-")) { //NOI18N
key = key.substring(1);
}
if (key.endsWith("=")) {
key = key.substring(0, key.length() - 1);
}
if (key.trim().length() > 0 && Verifier.checkElementName(key.trim()) == null) {
props.put(key.trim(), prp.length > 1 ? prp[1] : "");
}
}
tok = split.nextPair();
}
return props;
}
示例2
private static void addSanitizedContent(final Element e, final String val) {
try {
e.addContent(val);
} catch (final IllegalDataException ide) {
LOGGER.warn("Unable to add content", ide);
// Unless a better idea can be found, we need to replace all
// unparseable characters with a space as a placeholder
final StringBuffer newVal = new StringBuffer();
for (int i = 0, len = val.length(); i < len; i++) {
if (Verifier.isXMLCharacter(val.charAt(i))) {
newVal.append(val.charAt(i));
} else {
newVal.append(' ');
}
}
e.addContent(newVal.toString());
}
}
示例3
@Nullable
public static String filterXMLCharacters(String value) {
if (value != null) {
StringBuilder builder = null;
for (int i=0; i<value.length();i++) {
char c = value.charAt(i);
if (Verifier.isXMLCharacter(c)) {
if (builder != null) {
builder.append(c);
}
}
else {
if (builder == null) {
builder = new StringBuilder(value.length()+5);
builder.append(value, 0, i);
}
}
}
if (builder != null) {
value = builder.toString();
}
}
return value;
}
示例4
/**
* Some characters are illegal in XML even as numerical character references. This method performs escaping of them
* in a custom format, which is supposed to be unescaped on retrieving from XML using {@link #unescapeIllegalXmlChars(String)}.
* Resulting text can be part of XML version 1.0 document.
*
* @see <a href="https://www.w3.org/International/questions/qa-controls">https://www.w3.org/International/questions/qa-controls</a>
* @see Verifier#isXMLCharacter(int)
*/
@Nonnull
public static String escapeIllegalXmlChars(@Nonnull String text) {
StringBuilder b = null;
int lastPos = 0;
for (int i = 0; i < text.length(); i++) {
int c = text.codePointAt(i);
if (Character.isSupplementaryCodePoint(c)) {
//noinspection AssignmentToForLoopParameter
i++;
}
if (c == '#' || !Verifier.isXMLCharacter(c)) {
if (b == null) b = new StringBuilder(text.length() + 5); // assuming there's one 'large' char (e.g. 0xFFFF) to escape numerically
b.append(text, lastPos, i).append('#');
if (c != '#') b.append(Integer.toHexString(c));
b.append('#');
lastPos = i + 1;
}
}
return b == null ? text : b.append(text, lastPos, text.length()).toString();
}
示例5
private String getValidXMLString(String source){
StringBuilder ret = new StringBuilder();
for (int i = 0, len = source.length(); i < len; i++) {
// skip non valid XML characters
if (Verifier.isXMLCharacter(source.charAt(i))) {
ret.append(source.charAt(i));
}
}
return ret.toString();
}
示例6
public static String getValidXMLString(String source){
StringBuilder ret = new StringBuilder();
for (int i = 0, len = source.length(); i < len; i++) {
// skip non valid XML characters
if (Verifier.isXMLCharacter(source.charAt(i))) {
ret.append(source.charAt(i));
}
}
return ret.toString();
}