/** Determine the Lang, given the URI target, any content type header string and a hint */ public static Lang determineLang(String target, String ctStr, Lang hintLang) { ContentType ct = WebContent.determineCT(ctStr, hintLang, target) ; if ( ct == null ) return hintLang ; Lang lang = RDFLanguages.contentTypeToLang(ct) ; if (lang == null ) return hintLang ; return lang ; }
private Lang determinLang(TypedInputStream in, String url) { if ( in == null ) throw new RiotNotFoundException(url); Lang lang = forceLang; if ( lang == null ) { ContentType ct = WebContent.determineCT(in.getContentType(), hintLang, url); lang = RDFLanguages.contentTypeToLang(ct); } if ( lang == null ) throw new RiotException("Can't indentify the result set syntax from "+url); return lang; }
/** Parse when there is no URI to guide the choice of syntax */ private void parseNotUri(StreamRDF destination) { // parse from bytes or chars, no indication of the syntax from the source. Lang lang = hintLang; if ( forceLang != null ) lang = forceLang; ContentType ct = WebContent.determineCT(null, lang, baseUri); if ( ct == null ) throw new RiotException("Failed to determine the RDF syntax (.lang or .base required)"); ReaderRIOT readerRiot = createReader(ct); if ( readerRiot == null ) throw new RiotException("No parser registered for content type: " + ct.getContentType()); Reader jr = javaReader; if ( content != null ) jr = new StringReader(content); read(readerRiot, inputStream, jr, baseUri, context, ct, destination); }
static void test(String url, String ct, Lang hint, Lang expected) { ContentType x = WebContent.determineCT(ct, hint, url) ; Lang lang = RDFDataMgr.determineLang(url, ct, hint) ; assertEquals(expected, lang) ; } }
@Deprecated private static void processFromTypedInputStream(StreamRDF sink, TypedInputStream in, String baseUri, Lang hintLang, Context context) { // If the input stream comes with a content type, use that in preference to the hint (compatibility). // Except for text/plain. // Do here, which duplicates RDFParser, because "TypedInputStream" gets lost at RDFParser if ( in.getContentType() != null ) { // Special case of text/plain. ContentType ct = WebContent.determineCT(in.getContentType(), hintLang, null); Lang lang2 = RDFLanguages.contentTypeToLang(ct); hintLang = lang2 ; } RDFParser.create() .source(in) .base(baseUri) .lang(hintLang) // We made the decision above. .forceLang(hintLang) .context(context) .parse(sink); } // ---- Support for RDFDataMgr.parse only.
/** Parse when there is a URI to guide the choice of syntax */ private void parseURI(StreamRDF destination) { // Source by uri or path. try (TypedInputStream input = openTypedInputStream(uri, path)) { ReaderRIOT reader; ContentType ct; if ( forceLang != null ) { @SuppressWarnings("deprecation") ReaderRIOTFactory r = RDFParserRegistry.getFactory(forceLang); if ( r == null ) throw new RiotException("No parser registered for language: " + forceLang); ct = forceLang.getContentType(); reader = createReader(r, forceLang); } else { // No forced language. // Conneg and hint, ignoring text/plain. ct = WebContent.determineCT(input.getContentType(), hintLang, baseUri); if ( ct == null ) throw new RiotException("Failed to determine the content type: (URI=" + baseUri + " : stream=" + input.getContentType()+")"); reader = createReader(ct); if ( reader == null ) throw new RiotException("No parser registered for content type: " + ct.getContentType()); } read(reader, input, null, baseUri, context, ct, destination); } }
ContentType ct = WebContent.determineCT(in.getContentType(), null, url); Lang lang = RDFLanguages.contentTypeToLang(ct);