Data Conversion +---------------------------------------------------------------------------------------------- /*1*/ Any23 runner = new Any23(); /*2*/ final String content = "@prefix foo: . " + "@prefix : ." + "foo:bar foo: : . " + ":bar : foo:bar . "; // The second argument of StringDocumentSource() must be a valid URI. /*3*/ DocumentSource source = new StringDocumentSource(content, "http://host.com/service"); /*4*/ ByteArrayOutputStream out = new ByteArrayOutputStream(); /*5*/ TripleHandler handler = new NTriplesWriter(out); /*6*/ runner.extract(source, handler); /*7*/ String n3 = out.toString("UTF-8"); +---------------------------------------------------------------------------------------------- This example aims to demonstrate how to use <> to perform RDF data conversion. In this code we provide some input data expressed as Turtle and convert it in N3 format. At <> we define a new instance of the <> facade, that provides all the methods useful for the transformation. The facade constructor accepts a list of extractor names, if specified the extraction will be done only over this list, otherwise the data will detected and will be applied all the compatible extractors declared within the {{{./xref/org/deri/any23/extractor/ExtractorRegistry.html}ExtractorRegistry}}. The <> defines the input string containing some {{{http://www.w3.org/TeamSubmission/turtle/}Turtle}} data. At <> we instantiate a {{{./xref/org/deri/any23/source/StringDocumentSource.html}StringDocumentSource}}, specifying a content and a the source . The should be the source of the content data, and must be valid. Besides the {{{./xref/org/deri/any23/source/StringDocumentSource.html}StringDocumentSource}}, you can also provide input from other sources, such as requests and local files. See the classes in the sources {{{./xref/org/deri/any23/source/package-summary.html}package}}. The <> defines a buffered output stream that will be used to store the data produced by the writer declared at <>. A writer stores the extracted triples in some destination. We use an {{{./xref/org/deri/any23/writer/NTriplesWriter.html}NTriplesWriter}} here that writes into a ByteArrayOutputStream. There are writers for a number of formats, and you can also store the triples directly into a Sesame repository to query them with SPARQL; see {{{./xref/org/deri/any23/writer/RepositoryWriter.html}RepositoryWriter}} and the writer {{{./xref/org/deri/any23/writer/package-summary.html}package}}. The extractor method invoked at <> performs the metadata extraction. This method accepts as first argument a {{{./xref/org/deri/any23/source/DocumentSource.html}DocumentSource}} and as second argument a {{{./xref/org/deri/any23/writer/TripleHandler.html}TripleHandler}}, that will receive the sequence parsing events generated by the applied extractors. The extract method defines also another signature where it is possible to specify a charset encoding for the input data. If null, the charset will be auto detected. The expected output is encoded at <>: +---------------------------------------------------------------------------------------------- . . +----------------------------------------------------------------------------------------------