Class RdfCanon
- java.lang.Object
-
- com.apicatalog.rdf.canon.RdfCanon
-
- All Implemented Interfaces:
com.apicatalog.rdf.api.RdfQuadConsumer
public final class RdfCanon extends java.lang.Object implements com.apicatalog.rdf.api.RdfQuadConsumerAn implementation of the Standard RDF Dataset Canonicalization Algorithm as defined by the W3C.This class provides functionality to process and transform an RDF dataset into its canonical form, ensuring a stable and deterministic representation. Canonicalization is a key step in use cases such as digital signatures, data comparison, and RDF graph normalization.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classRdfCanon.HashNDegreeQuadsThe state information for the hash n-degree quads algorithm.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringBLANK_Aprivate static java.lang.StringBLANK_Zprivate java.util.Map<java.lang.String,java.util.Collection<Quad>>blankIdToQuadSetMap of blank IDs to all the quads that reference that specific blank ID.private java.util.Map<java.lang.String,Blank>blankNodesA map of blank node identifiers to their correspondingBlanknodes, used during RDF canonicalization for efficient relabeling of blank nodes.private IdentifierIssuercanonIssuerIssuer of canonical IDs to blank nodes.private java.security.MessageDigestdigestAn instance of a message digest algorithm (SHA-256, SHA-384, or custom).private java.util.Map<java.lang.String,java.util.Set<java.lang.String>>hashToBlankIdHash to associated IRIs.private static char[]HEXThe lower-case hexadecimal alphabet.private java.util.Set<java.lang.String>nonNormalizedA set of non-normalized values.private java.util.Set<Quad>quadsAll the n-quads in the dataset to be processed.private RdfCanonTickertickerAllows premature termination of the canonicalization process based on criteria defined by the associatedRdfCanonTickerinstance.
-
Constructor Summary
Constructors Constructor Description RdfCanon(java.util.Map<java.lang.String,java.util.Collection<Quad>> blankIdToQuadSet, java.util.Map<java.lang.String,Blank> resources, java.security.MessageDigest digest, java.util.Set<Quad> nquads, RdfCanonTicker ticker)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static RdfCanoncreate(java.lang.String hashAlgorithm)Creates a new instance ofRdfCanonusing the specified hash algorithm.static RdfCanoncreate(java.lang.String hashAlgorithm, RdfCanonTicker ticker)Creates a new instance ofRdfCanonusing the specified hash algorithm andRdfCanonTicker.static RdfCanoncreate(java.security.MessageDigest digest)Creates a new instance ofRdfCanonusing the specifiedMessageDigest.static RdfCanoncreate(java.security.MessageDigest digest, RdfCanonTicker ticker)(package private) java.lang.StringforBlank(Quad q0, java.lang.String blankNodeId)(package private) java.lang.StringhashFirstDegree(java.lang.String blankNodeId)(package private) NDegreeResulthashNDegreeQuads(java.lang.String id, IdentifierIssuer issuer)(package private) static java.lang.Stringhex(byte[] data)Convert bytes to hexadecimal.(package private) voidissueNDegreeIds()(package private) voidissueSimpleIds()(package private) voidmakeCanonQuads(com.apicatalog.rdf.api.RdfQuadConsumer consumer)java.util.Map<java.lang.String,java.lang.String>mapping()(package private) static RdfCanonnewInstance(java.util.Set<Quad> nquads, java.security.MessageDigest digest, RdfCanonTicker ticker)voidprovide(com.apicatalog.rdf.api.RdfQuadConsumer consumer)Emits canonical RDF quads to the given consumer.com.apicatalog.rdf.api.RdfQuadConsumerquad(java.lang.String subject, java.lang.String predicate, java.lang.String object, java.lang.String datatype, java.lang.String language, java.lang.String direction, java.lang.String graph)(package private) voidsetNonNormalized()(package private) voidsetResource(Position position, Quad quad, java.lang.String name)
-
-
-
Field Detail
-
HEX
private static final char[] HEX
The lower-case hexadecimal alphabet.
-
BLANK_A
private static final java.lang.String BLANK_A
- See Also:
- Constant Field Values
-
BLANK_Z
private static final java.lang.String BLANK_Z
- See Also:
- Constant Field Values
-
blankIdToQuadSet
private final java.util.Map<java.lang.String,java.util.Collection<Quad>> blankIdToQuadSet
Map of blank IDs to all the quads that reference that specific blank ID.
-
blankNodes
private final java.util.Map<java.lang.String,Blank> blankNodes
A map of blank node identifiers to their correspondingBlanknodes, used during RDF canonicalization for efficient relabeling of blank nodes.
-
canonIssuer
private final IdentifierIssuer canonIssuer
Issuer of canonical IDs to blank nodes.
-
ticker
private final RdfCanonTicker ticker
Allows premature termination of the canonicalization process based on criteria defined by the associatedRdfCanonTickerinstance.The
tickercan be used to monitor progress or enforce timeouts, limits, or custom stopping conditions during RDF dataset canonicalization.
-
hashToBlankId
private final java.util.Map<java.lang.String,java.util.Set<java.lang.String>> hashToBlankId
Hash to associated IRIs.
-
quads
private final java.util.Set<Quad> quads
All the n-quads in the dataset to be processed.
-
digest
private final java.security.MessageDigest digest
An instance of a message digest algorithm (SHA-256, SHA-384, or custom).
-
nonNormalized
private java.util.Set<java.lang.String> nonNormalized
A set of non-normalized values.
-
-
Constructor Detail
-
RdfCanon
RdfCanon(java.util.Map<java.lang.String,java.util.Collection<Quad>> blankIdToQuadSet, java.util.Map<java.lang.String,Blank> resources, java.security.MessageDigest digest, java.util.Set<Quad> nquads, RdfCanonTicker ticker)
-
-
Method Detail
-
create
public static RdfCanon create(java.lang.String hashAlgorithm)
Creates a new instance ofRdfCanonusing the specified hash algorithm.The provided algorithm determines the cryptographic hash function used for RDF canonicalization. Supported algorithms are:
SHA-256SHA-384
- Parameters:
hashAlgorithm- the name of the hash algorithm to use; must be either"SHA-256"or"SHA-384". Must not benull.- Returns:
- a new
RdfCanoninstance configured with the specified hash algorithm. - Throws:
java.lang.IllegalArgumentException- ifhashAlgorithmis not supported ornull.
-
create
public static RdfCanon create(java.lang.String hashAlgorithm, RdfCanonTicker ticker)
Creates a new instance ofRdfCanonusing the specified hash algorithm andRdfCanonTicker.The provided algorithm determines the cryptographic hash function used for RDF canonicalization. Supported algorithms are:
SHA-256SHA-384
A
RdfCanonTickermust also be provided to monitor the progress of the canonicalization process or enforce custom stopping conditions. The ticker'sRdfCanonTicker.tick()method will be invoked periodically during canonicalization. Iftick()throws anIllegalStateException, the process will be aborted.- Parameters:
hashAlgorithm- the name of the hash algorithm to use; must be either"SHA-256"or"SHA-384". Must not benull.ticker- a non-nullRdfCanonTickerused to monitor or control the canonicalization process.- Returns:
- a new
RdfCanoninstance configured with the specified hash algorithm and ticker. - Throws:
java.lang.IllegalArgumentException- ifhashAlgorithmis not supported ornull.java.lang.NullPointerException- iftickerisnull.
-
create
public static RdfCanon create(java.security.MessageDigest digest)
Creates a new instance ofRdfCanonusing the specifiedMessageDigest.This factory method allows the caller to provide a pre-configured
MessageDigestinstance, offering greater flexibility and control over the hashing behavior used during RDF canonicalization.The supplied
digestinstance will be used internally to compute hashes, and it should be properly initialized.- Parameters:
digest- a pre-configuredMessageDigestinstance; must not benull.- Returns:
- a new
RdfCanoninstance that uses the specifieddigest. - Throws:
java.lang.NullPointerException- ifdigestisnull.
-
create
public static RdfCanon create(java.security.MessageDigest digest, RdfCanonTicker ticker)
Creates a newRdfCanoninstance configured with the providedMessageDigestandRdfCanonTicker.This factory method gives callers fine-grained control over the hashing algorithm and behavior used during RDF dataset canonicalization by accepting a pre-configured
MessageDigestinstance.The supplied
digestwill be used internally to compute hashes for canonicalizing RDF datasets. It must be properly initialized and ready for use.A
RdfCanonTickermust also be provided to monitor the progress of canonicalization or enforce custom stopping conditions, such as timeouts, iteration limits, or external cancellation signals. The ticker’sRdfCanonTicker.tick()method will be invoked periodically during the canonicalization process. If it throws anIllegalStateException, processing will be interrupted.- Parameters:
digest- a pre-configured, non-nullMessageDigestinstance used for computing hashes.ticker- a non-nullRdfCanonTickerthat controls or monitors the canonicalization process.- Returns:
- a new
RdfCanoninstance configured with the specifieddigestandticker. - Throws:
java.lang.NullPointerException- ifdigestortickerisnull.
-
provide
public void provide(com.apicatalog.rdf.api.RdfQuadConsumer consumer) throws com.apicatalog.rdf.api.RdfConsumerExceptionEmits canonical RDF quads to the given consumer. This method generates RDF quads in a canonical form and supplies them to the providedRdfQuadConsumer.- Parameters:
consumer- theRdfQuadConsumerthat will receive the canonical RDF quads- Throws:
com.apicatalog.rdf.api.RdfConsumerException- if an error occurs while processing or consuming RDF quadsjava.lang.IllegalStateException- if the computation is terminated prematurely
-
mapping
public java.util.Map<java.lang.String,java.lang.String> mapping()
-
quad
public com.apicatalog.rdf.api.RdfQuadConsumer quad(java.lang.String subject, java.lang.String predicate, java.lang.String object, java.lang.String datatype, java.lang.String language, java.lang.String direction, java.lang.String graph)- Specified by:
quadin interfacecom.apicatalog.rdf.api.RdfQuadConsumer
-
newInstance
static RdfCanon newInstance(java.util.Set<Quad> nquads, java.security.MessageDigest digest, RdfCanonTicker ticker)
-
forBlank
java.lang.String forBlank(Quad q0, java.lang.String blankNodeId)
-
setNonNormalized
void setNonNormalized()
-
hashFirstDegree
java.lang.String hashFirstDegree(java.lang.String blankNodeId)
-
issueSimpleIds
void issueSimpleIds()
-
issueNDegreeIds
void issueNDegreeIds()
-
makeCanonQuads
void makeCanonQuads(com.apicatalog.rdf.api.RdfQuadConsumer consumer) throws com.apicatalog.rdf.api.RdfConsumerException- Throws:
com.apicatalog.rdf.api.RdfConsumerException
-
hex
static java.lang.String hex(byte[] data)
Convert bytes to hexadecimal.- Parameters:
data- the bytes- Returns:
- the data represented in hexadecimal.
-
hashNDegreeQuads
NDegreeResult hashNDegreeQuads(java.lang.String id, IdentifierIssuer issuer)
-
-