org.jdom.Verifier Java Examples

The following examples show how to use org.jdom.Verifier. 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: ActionMappings.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
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 #3
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 #4
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 #5
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 #6
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();
}