• About Us
  • Blog
  • Basket
  • Account
  • Sign In
  •  

Java API

What's Covered

This tutorial covers:

  1. How to iterate through all available properties in the data file.
  2. How to iterate through all possible values for each property.
  3. How to access the description for each property.
  4. How to access the description for each value.

Code and Explanation

Example of listing properties and possible values from a Dataset

The example illustrates:

  1. Loading a memory-resident Pattern Dataset
          dataset = MemoryFactory.create(Shared.getLitePatternV32(), true);
      
  2. Enumerating properties contained in the dataset loaded
          for(Property property : dataset.getProperties()) {
      
  3. Accessing description for each property.
          property.getDescription();
      
  4. Enumerating values that the property may have
          for (Value value : property.getValues().getAll()) {
      
  5. Accessing description of each value
          if (value.getDescription() != null) {
      


Full Source File
    // Dataset created from 51Degrees data file.
    private final Dataset dataset;

    /**
     * Creates a new Dataset using memory factory which creates a 
     * memory-resident representation of data.
     * 
     * @throws IOException if there was a problem reading from the data file.
     */
    public MetadataExample() throws IOException {
        dataset = MemoryFactory.create(Shared.getLitePatternV32(), true);
    }

    /**
     * Lists all properties available in provided data file and all possible 
     * values for each property. 
     * 
     * @throws IOException if there was a problem reading from the data file.
     */
    public void listProperties () throws IOException {
        // iterate over all properties in the dataset
        for(Property property : dataset.getProperties()) {
            //Get individual property, print name and description.
            System.out.format("%s (%s) - %s%n", property.getName(),
                    property.valueType.name(),
                    property.getDescription());

            // collects name, values and their descriptions
            StringBuilder propertyOutput = new StringBuilder();

            // loop over all values for this property and list
            // add a description for this property value if there is one
            for (Value value : property.getValues().getAll()) {
                // add name of property
                propertyOutput.append("\t")
                        .append(truncateToNl(value.getName()));
                // add description if exists
                if (value.getDescription() != null) {
                    propertyOutput.append(" - ")
                            .append(value.getDescription());
                }
                propertyOutput.append("\n");
            }
            System.out.println(propertyOutput);
            propertyOutput.setLength(0);
        }
    }

    // truncate value if it contains newline (esp for the JavaScript property)
    private String truncateToNl(String s) {
        int i = s.indexOf('\n', 3);
        if (i == -1) {
            return s;
        }
        return s.substring(0, i+2) + " ...";
    }

    /**
     * Don't forget to close datasets when you are done with them.
     * 
     * @throws IOException if there was a problem accessing the data file.
     */
    @Override
    public void close() throws IOException {
        dataset.close();
    }


    public static void main(String[] args) throws IOException {
        System.out.println("Starting Metadata Example");
        System.out.print("Loading dataset ...\r");
        MetadataExample example = new MetadataExample();
        try {
            example.listProperties();
        } finally {
            // close example so that dataset gets closed
            example.close();
        }
    }

Full Source File

Summary

Metadata is essentially a short description for an entity in the 51Degrees device data model. Metadata is used to provide a meaningful description for an entity such as property or value and can be useful in a variety of situations:

  • When generating reports using 51Degrees' data it can aid the reader to have a short description alongside each property listed.
  • When exposing part of the API to the end user it can help to know what the property definition is, or what the value definitions are before they make a choice. E.G. An advertising agency that lets their clients' target specific devices can benefit if every choice is provided with a description.

The Property Dictionary page is an example that uses our metadata. The page is generated dynamically using the metadata and entities of the 51Degrees' Enterprise data file. The following table shows just a few of the available properties.

Property Description Values
IsMobile Indicates if the device's primary data connection is wireless and is designed to operate mostly from battery power (ie a mobile phone, smart phone or tablet). True, False
PlatformName The name of the software platform (operating system) the device is using. Android, Windows, PS4 Software, ...
BrowserName The name of the browser. Many mobile browsers come default with the OS. Unless specifically named, these browsers are named after the accompanying OS Firefox, Safari, Chrome Mobile, ...

 

Enterprise and Premium Only

Property Description Values
HardwareVendor The company that manufactures the device or primarily sells it. May return 'Unknown'. Samsumg, Asus, Panasonic, ...
ScreenInchesDiagonal The diagonal size of the device's screen in inches. May return 'N/A' or 'Unknown'. 5, 5.1, 7, ...
IsTv Indicates if the device is a smart TV. True, False