The x11 protocol is described in several xml files in the xcb project. These files define structures used in the C client. Each struct contains types and variable names. The variable names have two problems in java.
- do not follow the lower camel case convention
- start with invalid characters (numbers)
When converting these names to java I wanted to find an existing solution that could do this quickly. One benefit of java is that so many problems have already been solved by one library or another. I came across two projects that helped me solve these issues.
implementation group: 'com.google.guava', name: 'guava', version: '29.0-jre'
implementation 'pl.allegro.finance:tradukisto:1.8.0'
Guava contains the class CaseFormat
which is able to convert between java and c style naming conventions. To convert from c to java:
String converted = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, x11Name)
This works for the majority of names but some of the enum items in the protocol start with a number.
<enum name="PropertyFormat">
<item name="8Bits"> <value>8</value> </item>
<item name="16Bits"> <value>16</value> </item>
<item name="32Bits"> <value>32</value> </item>
</enum>
To convert these names tradukisto can be used to first convert the number to words. Then the rest of the name can be appended.
String startNumbers = x11Name.find('^\\d+')
if(startNumbers) {
String remainingString = x11Name.substring(startNumbers.length())
String numberWords = ValueConverters.ENGLISH_INTEGER.asWords(startNumbers.toInteger())
.replace('-', ' ')
.split(' ').collect{ it.capitalize() }.join('')
return numberWords + remainingString
}
With Guava and tradukisto I am able to easily convert c variable names to java variable names.
Top comments (0)