------ Apache Any23 - Data Conversion ------ The Apache Software Foundation ------ 2011-2012 ~~ Licensed to the Apache Software Foundation (ASF) under one or more ~~ contributor license agreements. See the NOTICE file distributed with ~~ this work for additional information regarding copyright ownership. ~~ The ASF licenses this file to You under the Apache License, Version 2.0 ~~ (the "License"); you may not use this file except in compliance with ~~ the License. You may obtain a copy of the License at ~~ ~~ http://www.apache.org/licenses/LICENSE-2.0 ~~ ~~ Unless required by applicable law or agreed to in writing, software ~~ distributed under the License is distributed on an "AS IS" BASIS, ~~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ~~ See the License for the specific language governing permissions and ~~ limitations under the License. Data Conversion +---------------------------------------------------------------------------------------------- /*1*/ Apache Any23 runner = new Apache 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); try { /*6*/ runner.extract(source, handler); } finally { /*7*/ handler.close(); } /*8*/ String nt = 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 <> and convert it in <> 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/apache/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/apache/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/apache/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/apache/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/apache/any23/writer/NTriplesWriter.html}NTriplesWriter}} here that writes into a <>. The main <> formats writers are available and it is possible also to store the triples directly into a <> repository to query them via <>. See {{{./xref/org/apache/any23/writer/RepositoryWriter.html}RepositoryWriter}} and the writer {{{./xref/org/apache/any23/writer/package-summary.html}package}}. The extractor method invoked at <> performs the metadata extraction. This method accepts as first argument a {{{./xref/org/apache/any23/source/DocumentSource.html}DocumentSource}} and as second argument a {{{./xref/org/apache/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 <>, the charset will be auto detected. The {{{./xref/org/apache/any23/writer/TripleHandler.html}TripleHandler}} needs to be explicitly closed, this is done safely in a <> block at <>. The expected output is encoded at <>: +---------------------------------------------------------------------------------------------- . . +----------------------------------------------------------------------------------------------