SURF Reference Implementation

https://urf.io/surf/impl/ref
Source Code
https://bitbucket.org/account/user/globalmentor/projects/URF
Issues
https://globalmentor.atlassian.net/projects/URF
Maven
io.urf

Maven Dependency

pom.xml
<project>
  …
  <dependencies>
    …
    <dependency>
      <groupId>io.urf</groupId>
      <artifactId>surf</artifactId>
      <version>x.x.x</version>
    </dependency>
  </dependencies>
</project>

SURF Parser

Parse File user.surf

import io.urf.surf.*;SurfParser parser = new SurfParser();
Optional<Object> surf;
Path = Paths.get("user.surf");
try (final InputStream inputStream = newInputStream(path)) {
  surf = parser.parse(inputStream);
}

Parsing Results

The returned SURF data will be one of the following, based upon the root resource in the file:

  • Optional.empty() if the file was empty.
  • A value object (e.g. java.lang.Integer) if the root resource was a SURF literal.
  • A implementation of a collection type (e.g. java.util.Set<>) if it was a SURF collection.
  • An instance of SurfObject if it was a SURF object.

Access SURF Data

SurfObject user = (SurfObject)surf
    .orElseThrow(() -> new IOException("User file empty."));
URI userTag = user.getTag()
    .orElseThrow(() -> new IOException("Missing user IRI."));
String userName = (String)user.getPropertyValue("name")
    .orElseThrow(() -> new IOException("Missing user name."));
Optional<Object> userJoined = user.getPropertyValue("joined");
userJoined.ifPresent(joined -> {
  System.out.println(String.format("User joined %d days ago.",
      DAYS.between((LocalDate)joined, LocalDate.now()));
});

SURF Serializer

Create SurfObject

import io.urf.surf.*;

…

userTag = URI.fromString("urn:uuid:bb8e7dbe-f0b4-4d94"
    +"-a1cf-46ed0e920832");
SurfObject user = new SurfObject(userTag, "example-User");
user.setPropertyValue("name", "Jane Doe");
LocalDate joined = LocalDate.of(2016, Month.JANUARY, 23);
user.setPropertyValue("joined", joined);

Serialize File user.surf

SurfSerializer serializer = new SurfSerializer();
serializer.setFormatted(true);

Path = Paths.get("user.surf");
try (final OutputStream outputStream = newOutputStream(path)) {
  serializer.serialize(outputStream, user);
}