Java Code Examples for org.jdom.Verifier#isXMLCharacter()

The following examples show how to use org.jdom.Verifier#isXMLCharacter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JDOMUtils.java    From geowave with Apache License 2.0 6 votes vote down vote up
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());
  }
}
 
Example 2
Source File: DefaultJDOMExternalizer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example 3
Source File: XmlStringUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 4
Source File: Samdas.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
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();
}
 
Example 5
Source File: XMLTool.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
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();
}