Marcin created JENA-765:
---------------------------

             Summary: org.apache.jena.riot.RDFDataMgr converter to JSONLD 
problem
                 Key: JENA-765
                 URL: https://issues.apache.org/jira/browse/JENA-765
             Project: Apache Jena
          Issue Type: Bug
          Components: RIOT
    Affects Versions: Jena 2.12.0
         Environment: Linux, eclipse, junit test
            Reporter: Marcin


Convertion from N-TRIPLE to JSONLD return a random JSONLD format. For example 
for constatn Ntriples string 'testNtriples' RDFDataMgr.write(osData, 
modelJena.getGraph(), Lang.JSONLD); return random JSONLD string by execution:
a) execution 1 return string:
{
    "@id" : "_:b3",
    "@type" : "http://rdf.data-vocabulary.org/#Breadcrumb";,
    "http://rdf.data-vocabulary.org/#title"; : {
      "@language" : "pl",
      "@value" : "Foto"
    },
    "http://rdf.data-vocabulary.org/#url"; : {
      "@id" : "http://www.fake.com/foto.html";
    }
  }, 
...

b) execution 2 return string:
{
  "@graph" : [ {
    "@id" : "_:b0",
    "@type" : "http://rdf.data-vocabulary.org/#Breadcrumb";,
    "title" : {
      "@language" : "pl",
      "@value" : "Aparaty Cyfrowe Nikon "
    },
    "url" : "http://www.fake.com/site/cat/2/filtr/_283_0_0_0_0_0_0_0_0_0";
  }, {
    "@id" : "_:b1",
    "@type" : "http://rdf.data-vocabulary.org/#Breadcrumb";,
    "title" : {
      "@language" : "pl",
      "@value" : "Foto"
    },
    "url" : "http://www.fake.com/foto.html";
  }, 
...

Simple jUnit test:

public class RdfaParserTest1 extends AbstractMarkupTest {

    String testNtriples = "<http://www.fake.com> 
<http://opengraph.org/schema/title> \"Nikon D5100 + 18-105 VR\"@pl 
.\n<http://www.fake.com> <http://opengraph.org/schema/type> \"product\"@pl 
.\n<http://www.fake.com> <http://opengraph.org/schema/url> 
\"http://www.skapiec.pl/site/cat/2/comp/2231447\"@pl .\n<http://www.fake.com> 
<http://opengraph.org/schema/image> 
\"http://static3.skapiec.pl/2231447-1-1-6.jpg\"@pl .\n<http://www.fake.com> 
<http://opengraph.org/schema/site_name> \"Skapiec.pl\"@pl 
.\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
<http://www.fake.com/gallery.css?1407830722872> .\n<http://www.fake.com> 
<http://www.w3.org/1999/xhtml/vocab#stylesheet> 
<http://www.fake.com/style.nps.css> .\n<http://www.fake.com> 
<http://www.w3.org/1999/xhtml/vocab#stylesheet> 
<http://www.fake.com/style.header.css?1407830722872> .\n<http://www.fake.com> 
<http://www.w3.org/1999/xhtml/vocab#search> 
<http://www.fake.com/searchengine/skapiec.xml> .\n<http://www.fake.com> 
<http://www.w3.org/1999/xhtml/vocab#icon> <http://www.fake.com/favicon.ico> 
.\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
<http://static2.skapiec.pl/sfx/prettyPhoto/prettyPhoto.css> 
.\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#stylesheet> 
<http://www.fake.com/sfx/colorbox.css> .\n<http://www.fake.com> 
<http://www.w3.org/1999/xhtml/vocab#start> <http://www.skapiec.pl> .\n_:n0 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n0 
<http://rdf.data-vocabulary.org/#url> <http://www.fake.com/> .\n_:n0 
<http://rdf.data-vocabulary.org/#title> \"Sk\\uFFFD\\uFFFDpiec.pl\"@pl .\n_:n1 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n1 
<http://rdf.data-vocabulary.org/#url> <http://www.fake.com/foto.html> .\n_:n1 
<http://rdf.data-vocabulary.org/#title> \"Foto\"@pl .\n_:n2 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n2 
<http://rdf.data-vocabulary.org/#url> 
<http://www.fake.com/cat/2-aparaty-cyfrowe.html> .\n_:n2 
<http://rdf.data-vocabulary.org/#title> \"Aparaty Cyfrowe\"@pl .\n_:n3 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://rdf.data-vocabulary.org/#Breadcrumb> .\n_:n3 
<http://rdf.data-vocabulary.org/#url> 
<http://www.fake.com/site/cat/2/filtr/_283_0_0_0_0_0_0_0_0_0> .\n_:n3 
<http://rdf.data-vocabulary.org/#title> \"Aparaty Cyfrowe Nikon \"@pl .\n_:n4 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://rdf.data-vocabulary.org/#Review-aggregate> .\n_:n4 
<http://rdf.data-vocabulary.org/#itemreviewed> \"Nikon D5100 + 18-105 VR\"@pl 
.\n_:n4 <http://rdf.data-vocabulary.org/#average> \"4.58\"@pl .\n_:n4 
<http://rdf.data-vocabulary.org/#worst> \"0\"@pl .\n_:n4 
<http://rdf.data-vocabulary.org/#votes> \"179\"@pl .\n_:n5 
<http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 
<http://rdf.data-vocabulary.org/#Offer-aggregate> .\n_:n5 
<http://rdf.data-vocabulary.org/#offerCount> \"50\"@pl .\n_:n5 
<http://rdf.data-vocabulary.org/#condition> \"new\"@pl .\n_:n5 
<http://rdf.data-vocabulary.org/#lowPrice> \"2199\"@pl .\n_:n5 
<http://rdf.data-vocabulary.org/#highPrice> \"2711.2\"@pl 
.\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#copyright> 
<http://www.fake.com/site/doc/7> .\n<http://www.fake.com> 
<http://www.w3.org/1999/xhtml/vocab#index> <http://www.fake.com/site/doc/10> 
.\n<http://www.fake.com> <http://www.w3.org/1999/xhtml/vocab#index> 
<http://www.fake.com/site/doc/150/2/> .";

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test1() throws IOException, SAXException, ParseException, 
JSONException {

        com.hp.hpl.jena.rdf.model.Model modelJena = 
ModelFactory.createDefaultModel();

        modelJena.read(IOUtils.toInputStream(testNtriples), 
"http://www.fake.com";, "N-TRIPLE");

        ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();

        RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);

        @SuppressWarnings("rawtypes")
        Filter fData = 
Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));

        List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
"$..['@graph'][?]", fData);

        try {
            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..title[?(@.['@value'] == Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 1 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 1 end-->");

        } catch (Exception e) {
            System.out.println("Catch yes");

            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 1 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 1 end-->");

        }

    }

    @Test
    public void test2() throws IOException, SAXException, ParseException, 
JSONException {

        com.hp.hpl.jena.rdf.model.Model modelJena = 
ModelFactory.createDefaultModel();

        modelJena.read(IOUtils.toInputStream(testNtriples), 
"http://www.fake.com";, "N-TRIPLE");

        ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();

        RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);

        @SuppressWarnings("rawtypes")
        Filter fData = 
Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));

        List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
"$..['@graph'][?]", fData);

        try {
            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..title[?(@.['@value'] == Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 2 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 2 end-->");

        } catch (Exception e) {
            System.out.println("Catch yes");

            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 2 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 2 end-->");

        }

    }

    @Test
    public void test3() throws IOException, SAXException, ParseException, 
JSONException {

        com.hp.hpl.jena.rdf.model.Model modelJena = 
ModelFactory.createDefaultModel();

        modelJena.read(IOUtils.toInputStream(testNtriples), 
"http://www.fake.com";, "N-TRIPLE");

        ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();

        RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);

        @SuppressWarnings("rawtypes")
        Filter fData = 
Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));

        List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
"$..['@graph'][?]", fData);

        try {
            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..title[?(@.['@value'] == Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 3 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 3 end-->");

        } catch (Exception e) {
            System.out.println("Catch yes");

            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 3 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 3 end-->");

        }

    }

    @Test
    public void test4() throws IOException, SAXException, ParseException, 
JSONException {

        com.hp.hpl.jena.rdf.model.Model modelJena = 
ModelFactory.createDefaultModel();

        modelJena.read(IOUtils.toInputStream(testNtriples), 
"http://www.fake.com";, "N-TRIPLE");

        ByteArrayOutputStream osData = new java.io.ByteArrayOutputStream();

        RDFDataMgr.write(osData, modelJena.getGraph(), Lang.JSONLD);

        @SuppressWarnings("rawtypes")
        Filter fData = 
Filter.filter(Criteria.where("@type").exists(true).and("@type").in("http://rdf.data-vocabulary.org/#Breadcrumb";));

        List<Object> breadcrumbs = JsonPath.read(osData.toString(), 
"$..['@graph'][?]", fData);

        try {
            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..title[?(@.['@value'] == Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 4 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 4 end-->");

        } catch (Exception e) {
            System.out.println("Catch yes");

            Object breadcumpPhoto = null;

            for (Object object : breadcrumbs) {
                JSONArray breadcump = (((JSONArray) JsonPath.read(object, 
"$..['http://rdf.data-vocabulary.org/#title'][?(@.['@value'] == 
Foto)].['@value']")));

                if (breadcump.size() != 0) {
                    breadcumpPhoto = object;
                }
            }

            System.out.println("<--Test 4 start-->");
            System.out.println(JsonPath.read(breadcumpPhoto, "$..['@value']"));
            System.out.println(JsonPath.read(breadcumpPhoto, 
"$..['http://rdf.data-vocabulary.org/#url']"));
            System.out.println(JsonPath.read(breadcumpPhoto, "$..url"));
            System.out.println("<--Test 4 end-->");

        }

    }
}




--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to