OpenXRI:SyntaxTutorial

From I-names Development Wiki

Jump to: navigation, search

This tutorial is intended for developers who are interested in using the OpenXRI syntax package (an XRI parser library).

Essential Classes

This is a summary of the few prominent classes within the library.

  • org.openxri.XRI

This represents a type-safe parsed XRI. An XRI object can be instantiated by using the XRI(String) constructor if the client is certain that the string is in XRI normal form. XRI normal form is the preferred form for XRI processing but it is not suitable for contexts where an IRI or URI is expected. For those, the the fromIRINormalForm and fromIRINormalForm static methods should be used. URI normal form is by far the most prevalent and compatible way to transport URI in applications that are not aware of XRIs.

Calling any of the three methods above causes the string to be parsed, and the various parts of the XRI may be accessed using a variety of methods. This is similar in spirit to Java's java.net.URI class.

Example Code

import org.openxri.GCSAuthority;
import org.openxri.IRIAuthority;
import org.openxri.XRI;
import org.openxri.XRefAuthority;

public class Test {

	public static void main(String[] args) throws Exception {
		
		XRI xri = new XRI("xri://@mycompany*europe/documentation/xri?page=overview#introduction");
		
		if (xri.isAbsolute()) System.out.println("This is an absolute XRI.");
		else System.out.println("This is a relative XRI.");
			
		if (xri.getAuthorityPath() instanceof GCSAuthority) System.out.println("This XRI has a GCS Authority.");
		else if (xri.getAuthorityPath() instanceof XRefAuthority) System.out.println("This XRI has a XRef Authority.");
		else if (xri.getAuthorityPath() instanceof IRIAuthority) System.out.println("This XRI has a IRI Authority.");
			
		System.out.println("Authority: " + xri.getAuthorityPath());
		System.out.println("Path: " + xri.getXRIPath());
			
		if (xri.getXRIPath() != null) {
			
			for (int i=0; i<xri.getXRIPath().getNumSegments(); i++) {
			
				System.out.println("Segment " + (i+1) + ": " + xri.getXRIPath().getSegmentAt(i));
			}
		}
			
		System.out.println("Query: " + xri.getQuery());
		System.out.println("Fragment: " + xri.getFragment());

		System.out.println("IRI normal form: " + xri.toIRINormalForm());
		System.out.println("URI normal form: " + xri.toURINormalForm());
	}
}