XStreamUtils { public static String toFieldName(String xmlElementName) { if (xmlElementName == null) { return null; } if (xmlElementName.length() == 0) { return xmlElementName; } if (xmlElementName.length() == 1) { return xmlElementName.toLowerCase(); } if (Character.isUpperCase(xmlElementName.charAt(0)) && Character.isUpperCase(xmlElementName.charAt(1))) { return xmlElementName; } StringBuilder input = new StringBuilder(xmlElementName); StringBuilder output = new StringBuilder(); output.append(Character.toLowerCase(input.charAt(0))); boolean multiHyphens = false; for (int i = 1; i < input.length(); i++) { char ch = input.charAt(i); if (ch == '-') { if (input.charAt(++i) != '-') { output.append(Character.toUpperCase(input.charAt(i))); } else { multiHyphens = true; } } else { if (multiHyphens) { output.append(Character.toUpperCase(ch)); } else { output.append(ch); } multiHyphens = false; } } return output.toString(); }  static String toFieldName(String xmlElementName); static String toXmlElementName(String fieldName); static Collection<String> createParentFields(Class<?> clazz, String field, String separator); static boolean setContainsAnyOf(Set<String> toCheck, Collection<String> possibles); static List<String> readResource(InputStream in); static List<Class<?>> getClasses(InputStream in);  }
@Test public void testToFieldName() { assertNull(XStreamUtils.toFieldName(null)); assertEquals("", XStreamUtils.toFieldName("")); assertEquals(" ", XStreamUtils.toFieldName(" ")); assertEquals(" ", XStreamUtils.toFieldName(" ")); assertEquals("a", XStreamUtils.toFieldName("a")); assertEquals("1", XStreamUtils.toFieldName("1")); assertEquals("aDog", XStreamUtils.toFieldName("a-dog")); assertEquals("aSimpleXmlTagname", XStreamUtils.toFieldName("a-simple-xml-tagname")); assertEquals("a.simple.xml.tagname", XStreamUtils.toFieldName("a.simple.xml.tagname")); assertEquals("AB", XStreamUtils.toFieldName("AB")); assertEquals("ABCd", XStreamUtils.toFieldName("ABCd")); assertEquals("ABC-d", XStreamUtils.toFieldName("ABC-d")); assertEquals("abc", XStreamUtils.toFieldName("Abc")); assertEquals("aSimpleXmlTagname", XStreamUtils.toFieldName("a-simple-xml--tagname")); }