From: Markus Koschany <apo@debian.org>
Date: Mon, 12 Apr 2021 22:38:15 +0200
Subject: add profile support

Revert the removal of profile support because we still need those classes to
build libodfdom-java.

Forwarded: not-needed
---
 .../src/main/java/net/rootdev/javardfa/Parser.java |  35 +++----
 .../java/net/rootdev/javardfa/ParserFactory.java   |  15 +--
 .../net/rootdev/javardfa/ProfileCollector.java     |  57 ++++++++++
 .../rootdev/javardfa/SimpleProfileCollector.java   | 116 +++++++++++++++++++++
 .../javardfa/SimpleProfileCollectorTest.java       |  72 +++++++++++++
 .../javardfa/conformance2/RDFaConformance.java     |  60 +++++------
 6 files changed, 293 insertions(+), 62 deletions(-)
 create mode 100644 rdfa-module/src/main/java/net/rootdev/javardfa/ProfileCollector.java
 create mode 100644 rdfa-module/src/main/java/net/rootdev/javardfa/SimpleProfileCollector.java
 create mode 100644 rdfa-module/src/test/java/net/rootdev/javardfa/SimpleProfileCollectorTest.java

diff --git a/rdfa-module/src/main/java/net/rootdev/javardfa/Parser.java b/rdfa-module/src/main/java/net/rootdev/javardfa/Parser.java
index 289f320..c6764de 100644
--- a/rdfa-module/src/main/java/net/rootdev/javardfa/Parser.java
+++ b/rdfa-module/src/main/java/net/rootdev/javardfa/Parser.java
@@ -24,38 +24,40 @@ import javax.xml.stream.events.StartElement;
 import javax.xml.stream.events.XMLEvent;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
-import org.xml.sax.ErrorHandler;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
 
 /**
  * @author Damian Steer <pldms@mac.com>
  */
-public class Parser implements ContentHandler, ErrorHandler {
+public class Parser implements ContentHandler {
 
     private final XMLEventFactory eventFactory;
     private final StatementSink sink;
     private final Set<Setting> settings;
     private final LiteralCollector literalCollector;
     private final URIExtractor extractor;
+    private final ProfileCollector profileCollector;
 
     public Parser(StatementSink sink) {
         this(   sink,
                 XMLOutputFactory.newInstance(),
                 XMLEventFactory.newInstance(),
-                new URIExtractor10(new IRIResolver()));
+                new URIExtractor10(new IRIResolver()),
+                ProfileCollector.EMPTY_COLLECTOR);
     }
 
     public Parser(StatementSink sink,
             XMLOutputFactory outputFactory,
             XMLEventFactory eventFactory,
-            URIExtractor extractor) {
+            URIExtractor extractor,
+            ProfileCollector profileCollector) {
         this.sink = sink;
         this.eventFactory = eventFactory;
         this.settings = EnumSet.noneOf(Setting.class);
         this.extractor = extractor;
         this.literalCollector = new LiteralCollector(this, eventFactory, outputFactory);
+        this.profileCollector = profileCollector;
 
         extractor.setSettings(settings);
 
@@ -130,6 +132,15 @@ public class Parser implements ContentHandler, ErrorHandler {
             if (element.getAttributeByName(Constants.prefix) != null) {
                 parsePrefixes(element.getAttributeByName(Constants.prefix).getValue(), context);
             }
+
+            if (element.getAttributeByName(Constants.profile) != null) {
+                String profileURI = extractor.resolveURI(
+                        element.getAttributeByName(Constants.profile).getValue(),
+                        context);
+                profileCollector.getProfile(
+                        profileURI,
+                        context);
+            }
         }
         
         String about = extractor.getURI(element, Constants.about, context);
@@ -602,20 +613,6 @@ public class Parser implements ContentHandler, ErrorHandler {
         }
     }
     
-    // SAX error handling
-    
-    public void warning(SAXParseException exception) throws SAXException {
-        System.err.printf("Warning: %s\n", exception.getLocalizedMessage());
-    }
-
-    public void error(SAXParseException exception) throws SAXException {
-        System.err.printf("Error: %s\n", exception.getLocalizedMessage());
-    }
-
-    public void fatalError(SAXParseException exception) throws SAXException {
-        System.err.printf("Fatal error: %s\n", exception.getLocalizedMessage());
-    }
-    
     // Coalesce utility functions. Useful in parsing.
     
     private static <T> T coalesce(T a, T b) {
diff --git a/rdfa-module/src/main/java/net/rootdev/javardfa/ParserFactory.java b/rdfa-module/src/main/java/net/rootdev/javardfa/ParserFactory.java
index 8c597c2..435c523 100644
--- a/rdfa-module/src/main/java/net/rootdev/javardfa/ParserFactory.java
+++ b/rdfa-module/src/main/java/net/rootdev/javardfa/ParserFactory.java
@@ -96,10 +96,11 @@ public class ParserFactory {
         for (Setting setting: settings) if (setting == Setting.OnePointOne) is11 = true;
         URIExtractor extractor = (is11) ?
             new URIExtractor11(resolver) : new URIExtractor10(resolver);
-        Parser parser = getParser(format, sink, extractor);
+        ProfileCollector profileCollector = (is11) ?
+            new SimpleProfileCollector() : ProfileCollector.EMPTY_COLLECTOR ;
+        Parser parser = getParser(format, sink, extractor, profileCollector);
         for (Setting setting: settings) parser.enable(setting);
         reader.setContentHandler(parser);
-        reader.setErrorHandler(parser);
         return reader;
     }
 
@@ -113,19 +114,19 @@ public class ParserFactory {
     }
 
     private static Parser getParser(Format format, StatementSink sink,
-            URIExtractor extractor) {
+            URIExtractor extractor, ProfileCollector profileCollector) {
         return getParser(format, sink, XMLOutputFactory.newInstance(), 
-                XMLEventFactory.newInstance(), extractor);
+                XMLEventFactory.newInstance(), extractor, profileCollector);
     }
 
     private static Parser getParser(Format format, StatementSink sink,
             XMLOutputFactory outputFactory, XMLEventFactory eventFactory,
-            URIExtractor extractor) {
+            URIExtractor extractor, ProfileCollector profileCollector) {
         switch (format) {
             case XHTML:
-                return new Parser(sink, outputFactory, eventFactory, extractor);
+                return new Parser(sink, outputFactory, eventFactory, extractor, profileCollector);
             default:
-                Parser p = new Parser(sink, outputFactory, eventFactory, extractor);
+                Parser p = new Parser(sink, outputFactory, eventFactory, extractor, profileCollector);
                 p.enable(Setting.ManualNamespaces);
                 return p;
         }
diff --git a/rdfa-module/src/main/java/net/rootdev/javardfa/ProfileCollector.java b/rdfa-module/src/main/java/net/rootdev/javardfa/ProfileCollector.java
new file mode 100644
index 0000000..53f42f5
--- /dev/null
+++ b/rdfa-module/src/main/java/net/rootdev/javardfa/ProfileCollector.java
@@ -0,0 +1,57 @@
+/*
+ * (c) Copyright 2010 University of Bristol
+ * All rights reserved.
+ * [See end of file]
+ */
+package net.rootdev.javardfa;
+
+/**
+ *
+ * @author pldms
+ */
+public interface ProfileCollector {
+
+    public final static String NS = "http://www.w3.org/ns/rdfa#";
+    public final static String uri = NS + "uri";
+    public final static String term = NS + "term";
+    public final static String prefix = NS + "prefix";
+    public final static ProfileCollector EMPTY_COLLECTOR = new NullProfileCollector();
+
+    void getProfile(String profileURI, EvalContext context);
+
+    static final class NullProfileCollector implements ProfileCollector {
+
+        public void getProfile(String profileURI, EvalContext context) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+    }
+
+}
+
+/*
+ * (c) Copyright 2010 University of Bristol
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
\ No newline at end of file
diff --git a/rdfa-module/src/main/java/net/rootdev/javardfa/SimpleProfileCollector.java b/rdfa-module/src/main/java/net/rootdev/javardfa/SimpleProfileCollector.java
new file mode 100644
index 0000000..b64adb3
--- /dev/null
+++ b/rdfa-module/src/main/java/net/rootdev/javardfa/SimpleProfileCollector.java
@@ -0,0 +1,116 @@
+/*
+ * (c) Copyright 2010 University of Bristol
+ * All rights reserved.
+ * [See end of file]
+ */
+package net.rootdev.javardfa;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+
+/**
+ *
+ * @author pldms
+ */
+public class SimpleProfileCollector implements ProfileCollector {
+
+    public void getProfile(String profileURI, EvalContext context) {
+        System.err.println("!! GET PROFILE?? " + profileURI);
+        if (true) return;
+        try {
+            XMLReader reader =
+                    ParserFactory.createReaderForFormat(
+                        new SimpleCollector(context),
+                        ParserFactory.Format.XHTML,
+                        Setting.OnePointOne);
+            reader.parse(profileURI);
+        } catch (SAXException ex) {
+            ex.printStackTrace();
+        } catch (IOException ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    static class SimpleCollector implements StatementSink {
+        private final EvalContext context;
+        private final Map<String, Value> subjToVals;
+
+        SimpleCollector(EvalContext context) {
+            this.context = context;
+            this.subjToVals = new HashMap<String, Value>();
+        }
+
+        public void start() {}
+
+        public void end() {}
+
+        // Doesn't use objects
+        public void addObject(String subject, String predicate, String object) {}
+
+        public void addLiteral(String subject, String predicate, String lex, String lang, String datatype) {
+            Value val = subjToVals.get(subject);
+            if (val == null && ( predicate.equals(ProfileCollector.prefix) ||
+                    predicate.equals(ProfileCollector.term) ||
+                    predicate.equals(ProfileCollector.uri))) {
+                val = new Value();
+                subjToVals.put(subject, val);
+            }
+
+            if (val == null) return;
+            
+            if (predicate.equals(ProfileCollector.prefix)) {
+                if (val.uri != null) context.setPrefix(lex, val.uri);
+                else val.prefix = lex;
+            } else if (predicate.equals(ProfileCollector.term)) {
+                if (val.uri != null) context.setTerm(lex, val.uri);
+                else val.term = lex;
+            } else if (predicate.equals(ProfileCollector.uri)) {
+                if (val.prefix != null) context.setPrefix(val.prefix, lex);
+                else if (val.term != null) context.setTerm(val.term, lex);
+                else val.uri = lex;
+            }
+        }
+
+        public void addPrefix(String prefix, String uri) {}
+
+        public void setBase(String base) {}
+
+    }
+
+    static final class Value {
+        String prefix;
+        String term;
+        String uri;
+    }
+}
+
+
+/*
+ * (c) Copyright 2010 University of Bristol
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
\ No newline at end of file
diff --git a/rdfa-module/src/test/java/net/rootdev/javardfa/SimpleProfileCollectorTest.java b/rdfa-module/src/test/java/net/rootdev/javardfa/SimpleProfileCollectorTest.java
new file mode 100644
index 0000000..0b5eaad
--- /dev/null
+++ b/rdfa-module/src/test/java/net/rootdev/javardfa/SimpleProfileCollectorTest.java
@@ -0,0 +1,72 @@
+/*
+ * (c) Copyright 2010 University of Bristol
+ * All rights reserved.
+ * [See end of file]
+ */
+package net.rootdev.javardfa;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author pldms
+ */
+public class SimpleProfileCollectorTest {
+
+    public SimpleProfileCollectorTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    /**
+     * Test of getProfile method, of class SimpleProfileCollector.
+     */
+    @Test
+    public void testGetProfile() {
+        String profileURI = SimpleProfileCollectorTest.class.getResource("/query-tests/profile.xhtml").toExternalForm();
+        // Fix java stupidity -- again
+        profileURI = profileURI.replaceFirst("^file:/(?!/)", "file:///");
+        EvalContext context = new EvalContext("http://example.com/base");
+        ProfileCollector instance = new SimpleProfileCollector();
+        instance.getProfile(profileURI, context);
+        assertEquals("http://xmlns.com/foaf/0.2/", context.getURIForPrefix("foaf2"));
+        assertEquals("http://example.com/Rabbit", context.getURIForTerm("Rabbit"));
+    }
+
+}
+
+/*
+ * (c) Copyright 2010 University of Bristol
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
\ No newline at end of file
diff --git a/rdfa-module/src/test/java/net/rootdev/javardfa/conformance2/RDFaConformance.java b/rdfa-module/src/test/java/net/rootdev/javardfa/conformance2/RDFaConformance.java
index 6076523..19db90e 100644
--- a/rdfa-module/src/test/java/net/rootdev/javardfa/conformance2/RDFaConformance.java
+++ b/rdfa-module/src/test/java/net/rootdev/javardfa/conformance2/RDFaConformance.java
@@ -9,7 +9,6 @@ import org.apache.jena.query.Query;
 import org.apache.jena.query.QueryExecution;
 import org.apache.jena.query.QueryExecutionFactory;
 import org.apache.jena.query.QueryFactory;
-import org.apache.jena.query.QueryParseException;
 import org.apache.jena.query.QuerySolution;
 import org.apache.jena.query.ResultSet;
 import org.apache.jena.rdf.model.Model;
@@ -89,9 +88,9 @@ public abstract class RDFaConformance {
             //boolean expected = (soln.contains("expect")) ?
             //    soln.getLiteral("expect").getBoolean() : true;
             params[5] = soln.contains("expect") ? soln.getLiteral("expect").getLexicalForm() : "true";
-            if (toExclude.contains(params[0])
-                    || toExclude.contains(params[3])
-                    || toExclude.contains(params[4])) {
+            if (toExclude.contains(params[0]) ||
+                    toExclude.contains(params[3]) ||
+                    toExclude.contains(params[4]) ) {
                 log.warn("Skipping test <" + params[0] + ">");
                 continue;
             }
@@ -120,43 +119,32 @@ public abstract class RDFaConformance {
     public abstract XMLReader getParser(Model model) throws SAXException;
 
     @Test
-    public void compare() throws Throwable {
+    public void compare() throws SAXException, IOException {
+        Model model = ModelFactory.createDefaultModel();
+        InputStream in = FileManager.get().open(input);
+        XMLReader reader = getParser(model);
         try {
-            Model model = ModelFactory.createDefaultModel();
-            InputStream in = FileManager.get().open(input);
-            XMLReader reader = getParser(model);
             InputSource ins = new InputSource(in);
             ins.setEncoding("utf-8");
             ins.setSystemId(input);
             reader.parse(ins);
-
-            Query theQuery = QueryFactory.read(query);
-            QueryExecution qe = QueryExecutionFactory.create(theQuery, model);
-            boolean result = qe.execAsk();
-            if (result != expected) {
-                System.err.println("------ " + test + " ------");
-                model.write(System.err, "TTL");
-                System.err.println("------ Query ------");
-                System.err.println(theQuery);
-                System.err.println("-----------------------");
-            }
-            if (expected) {
-                assertTrue(title + " <" + test + ">", result);
-            } else {
-                assertFalse(title + " <" + test + ">", result);
-            }
-        } catch (Throwable e) {
-            // Make error reporting more informative by noting test that failed
-            
-            // These are fine, of course
-            if (e instanceof AssertionError) throw e;
-            
-            // Note source of issue and throw
-            String message = String.format("<%s>: %s %s", test, e.getClass().getName(), e.getLocalizedMessage());
-            Exception newEx = new Exception(message, e);
-            newEx.setStackTrace(e.getStackTrace()); // very dodgy!
-            
-            throw newEx;
+        } catch (NullPointerException e) {
+            fail("NPE <" + test + ">");
+        }
+        Query theQuery = QueryFactory.read(query);
+        QueryExecution qe = QueryExecutionFactory.create(theQuery, model);
+        boolean result = qe.execAsk();
+        if (result != expected) {
+            System.err.println("------ " + test + " ------");
+            model.write(System.err, "TTL");
+            System.err.println("------ Query ------");
+            System.err.println(theQuery);
+            System.err.println("-----------------------");
+        }
+        if (expected) {
+            assertTrue(title + " <" + test + ">", result);
+        } else {
+            assertFalse(title + " <" + test + ">", result);
         }
     }
 }
