Namespaces
You'll notice in the previous example that Juneau namespaces are used to represent bean property names.
These are used by default when namespaces are not explicitly specified.
The juneau namespace is used for generic names for objects that don't have namespaces
associated with them.
The juneaubp namespace is used on bean properties that don't have namespaces associated with
them.
The easiest way to specify namespaces is through annotations.
In this example, we're going to associate the prefix 'per' to our bean class and all properties
of this class.
We do this by adding the following annotation to our class:
@Rdf(prefix="per")
public class Person {
In general, the best approach is to define the namespace URIs at the package level using a
package-info.java class, like so:
// RDF namespaces used in this package
@RdfSchema(
prefix="ab",
rdfNs={
@RdfNs(prefix="ab", namespaceURI="http://www.apache.org/addressBook/"),
@RdfNs(prefix="per", namespaceURI="http://www.apache.org/person/"),
@RdfNs(prefix="addr", namespaceURI="http://www.apache.org/address/"),
@RdfNs(prefix="mail", namespaceURI="http://www.apache.org/mail/")
}
)
package org.apache.juneau.sample.addressbook;
import org.apache.juneau.xml.annotation.*;
This assigns a default prefix of "ab" for all classes and properties within the project, and
specifies various other prefixes used within this project.
Now when we rerun the sample code, we'll get the following:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j="http://www.apache.org/juneau/"
xmlns:jp="http://www.apache.org/juneaubp/"
xmlns:per="http://www.apache.org/person/">
<rdf:Description>
<per:id>1</per:id>
<per:name>John Smith</per:name>
</rdf:Description>
</rdf:RDF>
Namespace auto-detection ({@link oaj.xml.XmlSerializer#XML_autoDetectNamespaces}) is
enabled on serializers by default.
This causes the serializer to make a first-pass over the data structure to look for namespaces.
In high-performance environments, you may want to consider disabling auto-detection and providing an
explicit list of namespaces to the serializer to avoid this scanning step.
// Create a new serializer, but manually specify the namespaces.
RdfSerializer s = RdfSerializer.create()
.xmlabbrev()
.set(RdfProperties.RDF_rdfxml_tab, 3)
.autoDetectNamespaces(false)
.namespaces("{per:'http://www.apache.org/person/'}")
.build();
This code change will produce the same output as before, but will perform slightly better since it doesn't
have to crawl the POJO tree before serializing the result.