This is an automated email from the ASF dual-hosted git repository. andy pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/jena.git
commit 79367095f632acc79a27d25b684da777feafd66b Author: Andy Seaborne <[email protected]> AuthorDate: Sat Dec 20 16:25:45 2025 +0000 GH-3664: Unwire RDF/XML from jena-core main --- .../jena/riot/system/TestIO_JenaReaders.java | 40 +++++++---- .../jena/riot/system/TestIO_JenaWriters.java | 47 ++++++++----- .../main/java/org/apache/jena/rdf/model/Model.java | 4 +- .../apache/jena/rdf/model/impl/RDFReaderFImpl.java | 78 ++++++++++---------- .../apache/jena/rdf/model/impl/RDFWriterFImpl.java | 82 +++++++++++----------- .../apache/jena/rdfxml/xmlinput1/RDFXMLReader.java | 7 +- .../java/org/apache/jena/test/JenaCoreTestAll.java | 10 ++- .../java/org/apache/jena/test/X_RDFReaderF.java | 47 ++++++------- .../java/org/apache/jena/test/X_RDFWriterF.java | 39 +++++----- 9 files changed, 187 insertions(+), 167 deletions(-) diff --git a/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaReaders.java b/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaReaders.java index e475d5b989..4f5faad013 100644 --- a/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaReaders.java +++ b/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaReaders.java @@ -19,17 +19,22 @@ package org.apache.jena.riot.system; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.apache.jena.atlas.logging.LogCtl; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.RDFReaderF; import org.apache.jena.rdf.model.impl.RDFReaderFImpl; import org.apache.jena.riot.IO_Jena; import org.apache.jena.riot.adapters.RDFReaderRIOT; import org.apache.jena.shared.NoReaderForLangException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TestIO_JenaReaders { @BeforeAll public static void beforeClass() { } @@ -59,20 +64,31 @@ public class TestIO_JenaReaders { public void resetJena() { IO_Jena.wireIntoJena(); IO_Jena.resetJena(); - RDFReaderF readerF = new RDFReaderFImpl(); + try { + RDFReaderF readerF = new RDFReaderFImpl(); + Model model = ModelFactory.createDefaultModel(); + Logger logger = LoggerFactory.getLogger("RDFReader"); - assertNotEquals(RDFReaderRIOT.class, readerF.getReader(null).getClass()); - assertNotEquals(RDFReaderRIOT.class, readerF.getReader("RDF/XML").getClass()); - assertNotEquals(RDFReaderRIOT.class, readerF.getReader("RDF/XML-ABBREV").getClass()); - assertNotEquals(RDFReaderRIOT.class, readerF.getReader("N-TRIPLES").getClass()); - assertNotEquals(RDFReaderRIOT.class, readerF.getReader("N-Triples").getClass()); - assertNotEquals(RDFReaderRIOT.class, readerF.getReader("N-TRIPLE").getClass()); + LogCtl.withLevel(logger, "off", ()->{ + assertThrows(NoReaderForLangException.class, ()->readerF.getReader("RDF")); + assertThrows(NoReaderForLangException.class, ()->readerF.getReader("RDF/XML")); + assertThrows(NoReaderForLangException.class, ()->readerF.getReader("RDF/XML-ABBREV")); - try { readerF.getReader("NT") ; fail("Exception expected"); } catch (NoReaderForLangException e) {} - try { readerF.getReader("JSON_LD"); fail("Exception expected"); } catch (NoReaderForLangException e) {} - try { readerF.getReader("RDF/JSON"); fail("Exception expected"); } catch (NoReaderForLangException e) {} + assertThrows(NoReaderForLangException.class, ()->model.read("http://example/")); + assertThrows(NoReaderForLangException.class, ()->model.read("http://example/", "RDF/XML")); + }); - IO_Jena.wireIntoJena(); + assertNotEquals(RDFReaderRIOT.class, readerF.getReader("N-TRIPLES").getClass()); + assertNotEquals(RDFReaderRIOT.class, readerF.getReader("N-Triples").getClass()); + assertNotEquals(RDFReaderRIOT.class, readerF.getReader("N-TRIPLE").getClass()); + + // It's not called "NT" in jena-core on it's own. + assertThrows(NoReaderForLangException.class, ()->readerF.getReader("NT")); + assertThrows(NoReaderForLangException.class, ()->readerF.getReader("TURTLE")); + assertThrows(NoReaderForLangException.class, ()->readerF.getReader("JSON-LD")); + } finally { + IO_Jena.wireIntoJena(); + } } diff --git a/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaWriters.java b/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaWriters.java index d21b8ab5b6..de9c9f4a9b 100644 --- a/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaWriters.java +++ b/jena-arq/src/test/java/org/apache/jena/riot/system/TestIO_JenaWriters.java @@ -19,17 +19,22 @@ package org.apache.jena.riot.system; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.apache.jena.atlas.logging.LogCtl; +import org.apache.jena.rdf.model.Model; +import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.RDFWriterF; import org.apache.jena.rdf.model.impl.RDFWriterFImpl; import org.apache.jena.riot.IO_Jena; import org.apache.jena.riot.adapters.RDFWriterRIOT; import org.apache.jena.shared.NoWriterForLangException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class TestIO_JenaWriters { @@ -57,30 +62,34 @@ public class TestIO_JenaWriters { assertEquals(RDFWriterRIOT.class, writerF.getWriter("RDFJSON").getClass()); } + @SuppressWarnings("removal") @Test public void testResetJena() { IO_Jena.wireIntoJena(); IO_Jena.resetJena(); - RDFWriterF writerF = new RDFWriterFImpl(); + try { + RDFWriterF writerF = new RDFWriterFImpl(); + Model model = ModelFactory.createDefaultModel(); + Logger logger = LoggerFactory.getLogger("RDFWriter"); - assertNotEquals(RDFWriterRIOT.class, writerF.getWriter(null).getClass()); - assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("RDF/XML").getClass()); - assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("RDF/XML-ABBREV").getClass()); - assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N-TRIPLE").getClass()); - assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N-Triples").getClass()); - assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N-TRIPLE").getClass()); + LogCtl.withLevel(logger, "off", ()->{ + assertThrows(NoWriterForLangException.class, ()->writerF.getWriter("RDF/XML")); + assertThrows(NoWriterForLangException.class, ()->writerF.getWriter("RDF/XML-ABBREV")); - //N3 , Turtle in jena-core removed. -// assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N3").getClass()); -// assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("TURTLE").getClass()); -// assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("Turtle").getClass()); -// assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("TTL").getClass()); - - try { writerF.getWriter("NT"); fail("Exception expected"); } catch (NoWriterForLangException ex) {} - try { writerF.getWriter("RDF/JSON"); fail("Exception expected"); } catch (NoWriterForLangException ex) {} - try { writerF.getWriter("RDFJSON"); fail("Exception expected"); } catch (NoWriterForLangException ex) {} - IO_Jena.wireIntoJena(); - } + assertThrows(NoWriterForLangException.class, ()->model.write(System.err)); + assertThrows(NoWriterForLangException.class, ()->model.write(System.err, "RDF/XML")); + }); + assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N-TRIPLE").getClass()); + assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N-Triples").getClass()); + assertNotEquals(RDFWriterRIOT.class, writerF.getWriter("N-TRIPLE").getClass()); + // It's not called "NT" in jena-core on it's own. + assertThrows(NoWriterForLangException.class, ()->writerF.getWriter("NT")); + assertThrows(NoWriterForLangException.class, ()->writerF.getWriter("TURTLE")); + assertThrows(NoWriterForLangException.class, ()->writerF.getWriter("JSON-LD")); + } finally { + IO_Jena.wireIntoJena(); + } + } } diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java b/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java index aaa026f7d5..a8036423af 100644 --- a/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java +++ b/jena-core/src/main/java/org/apache/jena/rdf/model/Model.java @@ -552,7 +552,7 @@ public interface Model */ @Deprecated(forRemoval = true) public default Model write(Writer writer) { return write(writer, "RDF/XML"); } - + /** * <p>Write a serialized representation of a model in a specified language. * It is often better to use an OutputStream rather than a Writer, since this @@ -585,13 +585,11 @@ public interface Model */ public Model write( Writer writer, String lang, String base ); - /** * <p>Write a serialization of this model as an XML document.s</p> * @deprecated Use {@code write(OutputStream, "RDF/XML")} */ @Deprecated(forRemoval = true) - public default Model write(OutputStream out) { return write(out, "RDF/XML"); } /** diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFReaderFImpl.java b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFReaderFImpl.java index 6711c1b1bf..a44206c657 100644 --- a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFReaderFImpl.java +++ b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFReaderFImpl.java @@ -20,81 +20,81 @@ package org.apache.jena.rdf.model.impl; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; -import org.apache.jena.atlas.logging.Log ; +import org.apache.jena.atlas.lib.Creator; +import org.apache.jena.atlas.logging.Log; import org.apache.jena.rdf.model.RDFReaderF; import org.apache.jena.rdf.model.RDFReaderI; +import org.apache.jena.rdfxml.xmlinput1.RDFXMLReader; import org.apache.jena.shared.JenaException; import org.apache.jena.shared.NoReaderForLangException; public class RDFReaderFImpl extends Object implements RDFReaderF { - public static final String DEFAULTLANG = "RDF/XML" ; - private static Map<String, Class<? extends RDFReaderI>> custom = new LinkedHashMap<>(); - private static RDFReaderF rewiredAlternative = null ; + public static final String DEFAULTLANG = "RDF/XML"; + private static Map<String, Creator<RDFReaderI>> custom = new LinkedHashMap<>(); + private static RDFReaderF rewiredAlternative = null; + + // Jena6 + private static final boolean includeRDFXML = false; + /** Rewire to use an external RDFReaderF (typically, RIOT). * Set to null to use old jena-core setup. * @param other */ public static void alternative(RDFReaderF other) { - rewiredAlternative = other ; + rewiredAlternative = other; } + private static String msgNoRDFXML = "RDF/XML is no longer supported in jena-core. Add jena-arq to the classpath"; + private static Set<String> removedLangs = Set.of("RDF", "RDF/XML", "RDF/XML-ABBREV"); + /** Creates new RDFReaderFImpl */ public RDFReaderFImpl() {} + static { setup(); } + @Override public RDFReaderI getReader(String lang) { // Jena model.read rule for defaulting. if (lang==null || lang.equals("")) - lang = DEFAULTLANG ; + lang = DEFAULTLANG; // if RIOT -> if ( rewiredAlternative != null ) - return rewiredAlternative.getReader(lang) ; - Class<? extends RDFReaderI> c = custom.get(lang); + return rewiredAlternative.getReader(lang); + + if ( ! includeRDFXML ) { + // Jena6: reading RDF/XML removed from jena-core + if ( removedLangs.contains(lang) ) + Log.error("RDFReader", msgNoRDFXML); + } + + Creator<RDFReaderI> c = custom.get(lang); if ( c == null ) throw new NoReaderForLangException("Reader not found: " + lang); try { - return c.getConstructor().newInstance(); + return c.create(); } - catch (Exception e) { + catch (RuntimeException e) { throw new JenaException(e); } } - static { - // static initializer - set default readers - reset(); - } - - private static void reset() { - Class<? extends RDFReaderI> rdfxmlReader = org.apache.jena.rdfxml.xmlinput1.RDFXMLReader.class; - Class<? extends RDFReaderI> ntReader = org.apache.jena.rdf.model.impl.NTripleReader.class; + private static void setup() { // Turtle moved to test-only + // Jena6: ARP1 (RDF/XML) not installed except for tests - custom.put("RDF", rdfxmlReader); - custom.put("RDF/XML", rdfxmlReader); - custom.put("RDF/XML-ABBREV", rdfxmlReader); - - custom.put("N-TRIPLE", ntReader); + Creator<RDFReaderI> ntReader = NTripleReader::new; + custom.put("N-TRIPLE", ntReader); custom.put("N-TRIPLES", ntReader); custom.put("N-Triples", ntReader); - } - - private static String currentEntry(String lang) { - Class<? extends RDFReaderI> oldClass = custom.get(lang); - if ( oldClass != null ) - return oldClass.getName(); - else - return null; - } - - private static String remove(String lang) { - if ( rewiredAlternative != null ) - Log.error(RDFReaderFImpl.class, "Rewired RDFReaderFImpl - configuration changes have no effect on reading"); - String oldClassName = currentEntry(lang); - custom.remove(lang); - return oldClassName; + if ( includeRDFXML ) { + Creator<RDFReaderI> rdfxmlReader = RDFXMLReader::new; + custom.put("RDF", rdfxmlReader); + custom.put("RDF/XML", rdfxmlReader); + custom.put("RDF/XML-ABBREV", rdfxmlReader); + } } } diff --git a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFWriterFImpl.java b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFWriterFImpl.java index 391e86529e..3ea7cce774 100644 --- a/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFWriterFImpl.java +++ b/jena-core/src/main/java/org/apache/jena/rdf/model/impl/RDFWriterFImpl.java @@ -20,25 +20,31 @@ package org.apache.jena.rdf.model.impl; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; -import org.apache.jena.atlas.logging.Log ; -import org.apache.jena.rdf.model.RDFWriterI; +import org.apache.jena.atlas.lib.Creator; +import org.apache.jena.atlas.logging.Log; import org.apache.jena.rdf.model.RDFWriterF; +import org.apache.jena.rdf.model.RDFWriterI; +import org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Abbrev; +import org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Basic; import org.apache.jena.shared.JenaException; -import org.apache.jena.shared.NoWriterForLangException ; +import org.apache.jena.shared.NoWriterForLangException; -/** - */ public class RDFWriterFImpl extends Object implements RDFWriterF { public static final String DEFAULTLANG = "RDF/XML"; - private static Map<String, Class<? extends RDFWriterI>> custom = new LinkedHashMap<>(); - private static RDFWriterF rewiredAlternative = null ; + private static Map<String, Creator<RDFWriterI>> custom = new LinkedHashMap<>(); + private static RDFWriterF rewiredAlternative = null; + + // Jena6 + private static final boolean includeRDFXML = false; + /** Rewire to use an external RDFWriterF (typically, RIOT). * Set to null to use old jena-core setup. * @param other */ public static void alternative(RDFWriterF other) { - rewiredAlternative = other ; + rewiredAlternative = other; } /** Return the the current "rewiredAlternative" which may be null, meaning {@code RDFWriterFImpl} is in use. */ @@ -46,59 +52,51 @@ public class RDFWriterFImpl extends Object implements RDFWriterF { return rewiredAlternative; } + private static String msgNoRDFXML = "RDF/XML is no longer supported in jena-core. Add jena-arq to the classpath"; + private static Set<String> removedLangs = Set.of("RDF/XML", "RDF/XML-ABBREV"); + + static { setup(); } + /** Creates new RDFReaderFImpl */ public RDFWriterFImpl() {} @Override public RDFWriterI getWriter(String lang) { if (lang==null || lang.equals("")) - lang = DEFAULTLANG ; + lang = DEFAULTLANG; // If RIOT -> if ( rewiredAlternative != null ) - return rewiredAlternative.getWriter(lang) ; - if (lang==null || lang.equals("")) - lang = DEFAULTLANG ; - Class<? extends RDFWriterI> c = custom.get(lang); + return rewiredAlternative.getWriter(lang); + + if ( ! includeRDFXML ) { + // Jena6: writing RDF/XML removed from jena-core + if ( removedLangs.contains(lang) ) + Log.error("RDFWriter", msgNoRDFXML); + } + + Creator<RDFWriterI> c = custom.get(lang); if ( c == null ) throw new NoWriterForLangException("Writer not found: " + lang); try { - return c.getConstructor().newInstance(); + return c.create(); } - catch (Exception e) { + catch (RuntimeException e) { throw new JenaException(e); } } - static { - reset(); - } - - private static void reset() { - Class<? extends RDFWriterI> rdfxmlWriter = org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Basic.class; - Class<? extends RDFWriterI> rdfxmlAbbrevWriter = org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Abbrev.class; - Class<? extends RDFWriterI> ntWriter = org.apache.jena.rdf.model.impl.NTripleWriter.class; - - custom.put("RDF/XML", rdfxmlWriter); - custom.put("RDF/XML-ABBREV", rdfxmlAbbrevWriter); - + private static void setup() { + Creator<RDFWriterI> ntWriter = NTripleWriter::new; custom.put("N-TRIPLE", ntWriter); custom.put("N-TRIPLES", ntWriter); custom.put("N-Triples", ntWriter); - } - - private static String currentEntry(String lang) { - Class<? extends RDFWriterI> oldClass = custom.get(lang); - if ( oldClass != null ) - return oldClass.getName(); - else - return null; - } - private static String remove(String lang) { - if ( rewiredAlternative != null ) - Log.error(RDFWriterFImpl.class, "Rewired RDFWriterFImpl2 - configuration changes have no effect on writing"); - String oldClassName = currentEntry(lang); - custom.remove(lang); - return oldClassName; + if ( includeRDFXML ) { + // Jena6: RDF/XML writing not installed except for tests. + Creator<RDFWriterI> rdfxmlWriterBasic = RDFXML_Basic::new; + Creator<RDFWriterI> rdfxmlWriterAbbrev = RDFXML_Abbrev::new; + custom.put("RDF/XML", rdfxmlWriterBasic); + custom.put("RDF/XML-ABBREV", rdfxmlWriterAbbrev); + } } } diff --git a/jena-core/src/main/java/org/apache/jena/rdfxml/xmlinput1/RDFXMLReader.java b/jena-core/src/main/java/org/apache/jena/rdfxml/xmlinput1/RDFXMLReader.java index 882e857eb3..c0cad01e7b 100644 --- a/jena-core/src/main/java/org/apache/jena/rdfxml/xmlinput1/RDFXMLReader.java +++ b/jena-core/src/main/java/org/apache/jena/rdfxml/xmlinput1/RDFXMLReader.java @@ -33,6 +33,7 @@ import org.apache.jena.rdf.model.Model ; import org.apache.jena.rdf.model.RDFErrorHandler ; import org.apache.jena.rdf.model.RDFReaderI ; import org.apache.jena.rdf.model.impl.RDFDefaultErrorHandler ; +import org.apache.jena.rdf.model.impl.RDFReaderFImpl; import org.apache.jena.rdfxml.xmlinput1.impl.RDFXMLParser; import org.apache.jena.shared.DoesNotExistException ; import org.apache.jena.shared.JenaException ; @@ -68,11 +69,10 @@ public class RDFXMLReader implements RDFReaderI, ARPErrorNumbers { private final boolean resolveInitialXmlBase; /** - * Creates new JenaReader + * Creates new JenaReader (used by {@link RDFReaderFImpl}) */ public RDFXMLReader() { - arpf = RDFXMLParser.create(); - this.resolveInitialXmlBase = true; + this(true); } /** @@ -86,7 +86,6 @@ public class RDFXMLReader implements RDFReaderI, ARPErrorNumbers { this.resolveInitialXmlBase = resolveInitialXmlBase; } - final private RDFXMLParser arpf; private Model model; diff --git a/jena-core/src/test/java/org/apache/jena/test/JenaCoreTestAll.java b/jena-core/src/test/java/org/apache/jena/test/JenaCoreTestAll.java index 4f9d030e94..ea1f02e72a 100644 --- a/jena-core/src/test/java/org/apache/jena/test/JenaCoreTestAll.java +++ b/jena-core/src/test/java/org/apache/jena/test/JenaCoreTestAll.java @@ -18,8 +18,12 @@ package org.apache.jena.test; -import junit.framework.*; +import junit.framework.JUnit4TestAdapter; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; import org.apache.jena.rdf.model.impl.RDFReaderFImpl; +import org.apache.jena.rdf.model.impl.RDFWriterFImpl; import org.apache.jena.sys.JenaSystem; /** @@ -29,8 +33,10 @@ public class JenaCoreTestAll extends TestCase { static public TestSuite suite() { JenaSystem.init(); - // Include old Turtle parser - not up-to-date but enough to read test input files + // Include parsers and writers needed for the tests. + // These are not up-to-date but enough to work with the test suite. RDFReaderFImpl.alternative(new X_RDFReaderF()); + RDFWriterFImpl.alternative(new X_RDFWriterF()); TestSuite ts = new TestSuite(); ts.setName("Jena Core"); diff --git a/jena-core/src/test/java/org/apache/jena/test/X_RDFReaderF.java b/jena-core/src/test/java/org/apache/jena/test/X_RDFReaderF.java index 87b5cd69f0..1674517c9c 100644 --- a/jena-core/src/test/java/org/apache/jena/test/X_RDFReaderF.java +++ b/jena-core/src/test/java/org/apache/jena/test/X_RDFReaderF.java @@ -21,15 +21,19 @@ package org.apache.jena.test; import java.util.LinkedHashMap; import java.util.Map; -import org.apache.jena.rdf.model.RDFReaderI; +import org.apache.jena.atlas.lib.Creator; import org.apache.jena.rdf.model.RDFReaderF; +import org.apache.jena.rdf.model.RDFReaderI; +import org.apache.jena.rdf.model.impl.NTripleReader; +import org.apache.jena.rdfxml.xmlinput1.RDFXMLReader; import org.apache.jena.shared.JenaException; import org.apache.jena.shared.NoReaderForLangException; +import org.apache.jena.ttl_test.turtle.TurtleReader; /** * For jena-core tests only. * <p> - * The RDFReaderF provides the languages needed by the jena-core test suite. + * This RDFReaderF provides the languages needed by the jena-core test suite. * <ul> * <li>RDF/XML</li> * <li>test-only Turtle (not fully RDF 1.1 compliant)<li> @@ -37,41 +41,38 @@ import org.apache.jena.shared.NoReaderForLangException; * </ul> */ public class X_RDFReaderF extends Object implements RDFReaderF { - public static final String DEFAULTLANG = "RDF/XML"; - private static Map<String, Class<? extends RDFReaderI>> custom = new LinkedHashMap<>(); + public static final String DEFAULTLANG = "RDF/XML" ; + private static Map<String, Creator<RDFReaderI>> custom = new LinkedHashMap<>(); + private static RDFReaderF rewiredAlternative = null ; + static { reset(); } - /** Creates new RDFReaderFImpl */ public X_RDFReaderF() {} @Override public RDFReaderI getReader(String lang) { // Jena model.read rule for defaulting. if (lang==null || lang.equals("")) - lang = DEFAULTLANG; - Class<? extends RDFReaderI> c = custom.get(lang); + lang = DEFAULTLANG ; + // if RIOT -> + if ( rewiredAlternative != null ) + return rewiredAlternative.getReader(lang) ; + Creator<RDFReaderI> c = custom.get(lang); if ( c == null ) throw new NoReaderForLangException("Reader not found: " + lang); try { - return c.getConstructor().newInstance(); + return c.create(); } - catch (Exception e) { + catch (RuntimeException e) { throw new JenaException(e); } } - static { - // static initializer - set default readers - reset(); - } private static void reset() { - Class<? extends RDFReaderI> rdfxmlReader = org.apache.jena.rdfxml.xmlinput1.RDFXMLReader.class; - - // test use only - Class<? extends RDFReaderI> ntReader = org.apache.jena.rdf.model.impl.NTripleReader.class; - // test use only - Class<? extends RDFReaderI> turtleReader = org.apache.jena.ttl_test.turtle.TurtleReader.class; + Creator<RDFReaderI> rdfxmlReader = RDFXMLReader::new; + Creator<RDFReaderI> ntReader = NTripleReader::new; + Creator<RDFReaderI> turtleReader = TurtleReader::new; custom.put("RDF", rdfxmlReader); custom.put("RDF/XML", rdfxmlReader); @@ -86,12 +87,4 @@ public class X_RDFReaderF extends Object implements RDFReaderF { custom.put("Turtle", turtleReader); custom.put("TTL", turtleReader); } - - private static String currentEntry(String lang) { - Class<? extends RDFReaderI> oldClass = custom.get(lang); - if ( oldClass != null ) - return oldClass.getName(); - else - return null; - } } diff --git a/jena-core/src/test/java/org/apache/jena/test/X_RDFWriterF.java b/jena-core/src/test/java/org/apache/jena/test/X_RDFWriterF.java index bb8cf89dd2..767594d15d 100644 --- a/jena-core/src/test/java/org/apache/jena/test/X_RDFWriterF.java +++ b/jena-core/src/test/java/org/apache/jena/test/X_RDFWriterF.java @@ -21,16 +21,29 @@ package org.apache.jena.test; import java.util.LinkedHashMap; import java.util.Map; +import org.apache.jena.atlas.lib.Creator; import org.apache.jena.rdf.model.RDFWriterF; import org.apache.jena.rdf.model.RDFWriterI; +import org.apache.jena.rdf.model.impl.NTripleWriter; +import org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Abbrev; +import org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Basic; import org.apache.jena.shared.JenaException; import org.apache.jena.shared.NoWriterForLangException; + /** + * For jena-core tests only. + * <p> + * This RDFWriterF provides the languages needed by the jena-core test suite. + * <ul> + * <li>RDF/XML</li> + * <li>An N-triples reader<li> + * </ul> */ public class X_RDFWriterF extends Object implements RDFWriterF { public static final String DEFAULTLANG = "RDF/XML"; - private static Map<String, Class<? extends RDFWriterI>> custom = new LinkedHashMap<>(); + private static Map<String, Creator<RDFWriterI>> custom = new LinkedHashMap<>(); + static { reset(); } /** Creates new RDFReaderFImpl */ public X_RDFWriterF() {} @@ -39,25 +52,21 @@ public class X_RDFWriterF extends Object implements RDFWriterF { public RDFWriterI getWriter(String lang) { if (lang==null || lang.equals("")) lang = DEFAULTLANG; - Class<? extends RDFWriterI> c = custom.get(lang); + Creator<RDFWriterI> c = custom.get(lang); if ( c == null ) throw new NoWriterForLangException("Writer not found: " + lang); try { - return c.getConstructor().newInstance(); + return c.create(); } - catch (Exception e) { + catch (RuntimeException e) { throw new JenaException(e); } } - static { - reset(); - } - private static void reset() { - Class<? extends RDFWriterI> rdfxmlWriter = org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Basic.class; - Class<? extends RDFWriterI> rdfxmlAbbrevWriter = org.apache.jena.rdfxml.xmloutput.impl.RDFXML_Abbrev.class; - Class<? extends RDFWriterI> ntWriter = org.apache.jena.rdf.model.impl.NTripleWriter.class; + Creator<RDFWriterI> rdfxmlWriter = RDFXML_Basic::new; + Creator<RDFWriterI> rdfxmlAbbrevWriter = RDFXML_Abbrev::new; + Creator<RDFWriterI> ntWriter = NTripleWriter::new; custom.put("RDF/XML", rdfxmlWriter); custom.put("RDF/XML-ABBREV", rdfxmlAbbrevWriter); @@ -66,12 +75,4 @@ public class X_RDFWriterF extends Object implements RDFWriterF { custom.put("N-TRIPLES", ntWriter); custom.put("N-Triples", ntWriter); } - - private static String currentEntry(String lang) { - Class<? extends RDFWriterI> oldClass = custom.get(lang); - if ( oldClass != null ) - return oldClass.getName(); - else - return null; - } }
