Fork me on GitHub

Tutorial 3

Eclipse code formatter settings are stored in profiles. In this example we like to access the data of a certain profile. Features shown in this example:

  • Dynamic projections Getter methods may be declared with parameters. There parameters will fill placeholders in the XPath declaration to build up the final XPath expression. Syntax of this is the MessageFormat format syntax.

XML Content

<profiles version="8">
    <profile name="Some Profile" version="8">
        <setting
            id="org.eclipse.jdt.core.formatter.align_type_members_on_columns"
            value="false" />
        <setting
            id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression"
            value="16" />
... 
    </profile>
    <profile name="Another Profile" version="8">
        <setting
            id="org.eclipse.jdt.core.formatter.align_type_members_on_columns"
            value="true" />
        <setting
            id="org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression"
            value="16" />
 ...
    </profile>
</profiles>

Projection API

public interface EclipseFormatterConfigFile {
 
    interface Setting {
 
        @XBRead("@id")
        String getName();
 
        @XBRead("@value")
        String getValue();
         
    }
 
    @XBRead("//profile/@name")
    List<String> getProfileNames();
 
    @XBRead("//profiles/profile[@name=\"{0}\"]/setting")
    List<Setting> getAllSettingsForProfile(String profileName);
     
}

Example Code

EclipseFormatterConfigFile configFile = new XBProjector().io().fromURLAnnotation(EclipseFormatterConfigFile.class);
System.out.println("Profile names:" + configFile.getProfileNames());       
for (Setting setting:configFile.getAllSettingsForProfile("Some Profile")) {
    System.out.println(setting.getName()+" -> "+setting.getValue());   
}