/**
 * 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.
 */

package org.apache.jena.riot.adapters;

import java.io.InputStream ;
import java.io.Reader ;
import java.util.Locale ;

import org.apache.jena.riot.RDFDataMgr ;
import org.apache.jena.riot.RDFLanguages ;

import com.hp.hpl.jena.graph.GraphEvents ;
import com.hp.hpl.jena.rdf.model.Model ;
import com.hp.hpl.jena.rdf.model.RDFErrorHandler ;
import com.hp.hpl.jena.rdf.model.RDFReader ;
import com.hp.hpl.jena.rdf.model.impl.RDFDefaultErrorHandler ;
import com.hp.hpl.jena.sparql.util.Context ;
import com.hp.hpl.jena.sparql.util.Symbol ;

/** Adapter from Jena2 original style adapter to RIOT reader. */ 
public class RDFReaderRIOT implements RDFReader
{
    private final String basename ; // This will be per reader instance.
    private final String hintlang ;
    private Context context = new Context() ;
    private RDFErrorHandler errorHandler = new RDFDefaultErrorHandler();
    
    public RDFReaderRIOT()
    {
        basename = "org.apache.jena.riot.reader.generic" ;
        hintlang = null ;
    }
    
    public RDFReaderRIOT(String lang)
    {
        basename = "org.apache.jena.riot.reader."+lang.toLowerCase(Locale.ROOT) ;
        hintlang = lang ;
    }

    @SuppressWarnings("deprecation")
    @Override
    public void read(Model model, Reader r, String base)
    { 
        startRead(model) ; 
        RDFDataMgr.read(model, r, base, RDFLanguages.nameToLang(hintlang)) ;
        finishRead(model) ;
    }

    @Override
    public void read(Model model, InputStream r, String base)
    { 
        startRead(model) ; 
        RDFDataMgr.read(model, r, base, RDFLanguages.nameToLang(hintlang)) ;
        finishRead(model) ;
    }
    
    @Override
    public void read(Model model, String url)
    { 
        startRead(model) ;
        RDFDataMgr.read(model, url, RDFLanguages.nameToLang(hintlang)) ;
        finishRead(model) ;
    }

    @Override
    public Object setProperty(String propName, Object propValue)
    {
        Symbol sym = Symbol.create(basename+propName) ;
        Object oldObj = context.get(sym) ;
        return oldObj ;
    }
    
    protected void startRead(Model model)
    { model.notifyEvent( GraphEvents.startRead ); } 
    
    protected void finishRead(Model model)
    { model.notifyEvent( GraphEvents.finishRead ) ; }
    
    @Override
    public RDFErrorHandler setErrorHandler(RDFErrorHandler errHandler)
    {
        RDFErrorHandler old = errorHandler ;
        errorHandler = errHandler ;
        return old ;
    }
}


@@ -917,24 +917,59 @@
 l.Locale ;%0A%0A
+import org.apache.jena.riot.Lang ;%0A
 import org.a
@@ -993,24 +993,24 @@
 DFDataMgr ;%0A
-
 import org.a
@@ -1569,32 +1569,32 @@
 .%0A    pr
-iva
+otec
 te
+d
  final 
-Stri
+La
 ng hintl
@@ -1605,21 +1605,23 @@
 ;%0A    pr
-iva
+otec
 te
+d
  Context
@@ -1653,21 +1653,23 @@
 ;%0A    pr
-ivate
+otected
  RDFErro
@@ -2001,20 +2001,45 @@
 tlang = 
+RDFLanguages.nameToLang(
 lang
+)
  ;%0A    %7D
@@ -2227,32 +2227,8 @@
 se, 
-RDFLanguages.nameToLang(
 hint
@@ -2224,33 +2224,32 @@
  base, hintlang)
-)
  ;%0A        finis
@@ -2425,32 +2425,8 @@
 se, 
-RDFLanguages.nameToLang(
 hint
@@ -2422,33 +2422,32 @@
  base, hintlang)
-)
  ;%0A        finis
@@ -2606,32 +2606,8 @@
 rl, 
-RDFLanguages.nameToLang(
 hint
@@ -2611,17 +2611,16 @@
 intlang)
-)
  ;%0A     

Convert to Lang from string once only