• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Nanopublication / nanopub-java / 17038267114

18 Aug 2025 10:41AM UTC coverage: 47.35% (-1.7%) from 49.072%
17038267114

push

github

Ziroli Plutschow
Now all Unit Tests run successful without network connection.

- Moved the GetIndex test to an Integration Test.

881 of 2880 branches covered (30.59%)

Branch coverage included in aggregate %.

4729 of 8968 relevant lines covered (52.73%)

2.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.47
src/main/java/org/nanopub/CustomTrigWriter.java
1
package org.nanopub;
2

3
import org.apache.commons.io.output.NullOutputStream;
4
import org.eclipse.rdf4j.model.IRI;
5
import org.eclipse.rdf4j.model.Literal;
6
import org.eclipse.rdf4j.model.util.Literals;
7
import org.eclipse.rdf4j.model.vocabulary.XSD;
8
import org.eclipse.rdf4j.rio.trig.TriGWriter;
9
import org.eclipse.rdf4j.rio.turtle.TurtleUtil;
10

11
import java.io.IOException;
12
import java.io.OutputStream;
13
import java.io.Writer;
14
import java.util.Set;
15

16
/**
17
 * Custom TriG writer that allows for the use of custom prefixes.
18
 *
19
 * @author Tobias Kuhn
20
 */
21
public class CustomTrigWriter extends TriGWriter {
22

23
    private Set<String> usedPrefixes;
24

25
    /**
26
     * Creates a new TriG writer that writes to the specified output stream.
27
     *
28
     * @param out The output stream to write to.
29
     */
30
    public CustomTrigWriter(OutputStream out) {
31
        super(out);
×
32
    }
×
33

34
    /**
35
     * Creates a new TriG writer that writes to the specified writer.
36
     *
37
     * @param writer The writer to write to.
38
     */
39
    public CustomTrigWriter(Writer writer) {
40
        super(writer);
3✔
41
    }
1✔
42

43
    /**
44
     * Creates a new TriG writer that uses the specified set of prefixes.
45
     *
46
     * @param out          The output stream to write to.
47
     * @param usedPrefixes The set of prefixes that are used in the document.
48
     */
49
    public CustomTrigWriter(OutputStream out, Set<String> usedPrefixes) {
50
        super(out);
×
51
        this.usedPrefixes = usedPrefixes;
×
52
    }
×
53

54
    /**
55
     * Creates a new TriG writer that uses the specified set of prefixes.
56
     *
57
     * @param writer       The writer to write to.
58
     * @param usedPrefixes The set of prefixes that are used in the document.
59
     */
60
    public CustomTrigWriter(Writer writer, Set<String> usedPrefixes) {
61
        super(writer);
×
62
        this.usedPrefixes = usedPrefixes;
×
63
    }
×
64

65
    /**
66
     * Creates a new TriG writer that uses the specified set of prefixes.
67
     *
68
     * @param usedPrefixes The set of prefixes that are used in the document.
69
     */
70
    public CustomTrigWriter(Set<String> usedPrefixes) {
71
        super(NullOutputStream.INSTANCE);
3✔
72
        this.usedPrefixes = usedPrefixes;
3✔
73
    }
1✔
74

75
    /**
76
     * {@inheritDoc}
77
     */
78
    @Override
79
    protected void writeURI(IRI uri) throws IOException {
80
        String uriString = uri.toString();
3✔
81

82
        String prefix = namespaceTable.get(uriString);
6✔
83
        if (prefix != null) {
2✔
84
            // Exact match: no suffix required
85
            writer.write(prefix);
4✔
86
            writer.write(":");
4✔
87
            if (usedPrefixes != null) {
3✔
88
                usedPrefixes.add(prefix);
5✔
89
            }
90
            return;
1✔
91
        }
92

93
        prefix = null;
2✔
94

95
        int splitIdx = TurtleUtil.findURISplitIndex(uriString);
3✔
96

97
        // Sesame bug for URIs that end with a period.
98
        // Port fix from https://bitbucket.org/openrdf/sesame/pull-request/301/ses-2086-fix-turtlewriter-writing/diff
99
        if (!TurtleUtil.isNameEndChar(uriString.charAt(uriString.length() - 1))) {
8✔
100
            splitIdx = -1;
2✔
101
        }
102

103
        if (splitIdx > 0) {
2✔
104
            String namespace = uriString.substring(0, splitIdx);
5✔
105
            prefix = namespaceTable.get(namespace);
6✔
106
        }
107

108
        // Do also split at dots:
109
        int splitIdxDot = uriString.lastIndexOf(".") + 1;
6✔
110
        if (uriString.length() == splitIdxDot) splitIdxDot = -1;
6✔
111
        if (splitIdx > 0 && splitIdxDot > splitIdx) {
5✔
112
            String namespace = uriString.substring(0, splitIdxDot);
5✔
113
            String p = namespaceTable.get(namespace);
6✔
114
            if (p != null) {
2!
115
                splitIdx = splitIdxDot;
×
116
                prefix = p;
×
117
            }
118
        }
119

120
        // ... and colons:
121
        int splitIdxColon = uriString.lastIndexOf(":") + 1;
6✔
122
        if (uriString.length() == splitIdxColon) splitIdxColon = -1;
4!
123
        if (splitIdx > 0 && splitIdxColon > splitIdx) {
5!
124
            String namespace = uriString.substring(0, splitIdxColon);
×
125
            String p = namespaceTable.get(namespace);
×
126
            if (p != null) {
×
127
                splitIdx = splitIdxColon;
×
128
                prefix = p;
×
129
            }
130
        }
131

132
        // ... and underscores:
133
        int splitIdxUnderscore = uriString.lastIndexOf("_") + 1;
6✔
134
        if (uriString.length() == splitIdxUnderscore) splitIdxUnderscore = -1;
4!
135
        if (splitIdx > 0 && splitIdxUnderscore > splitIdx) {
5✔
136
            String namespace = uriString.substring(0, splitIdxUnderscore);
5✔
137
            String p = namespaceTable.get(namespace);
6✔
138
            if (p != null) {
2!
139
                splitIdx = splitIdxUnderscore;
×
140
                prefix = p;
×
141
            }
142
        }
143

144
        // ... and *before* hash signs:
145
        int splitIdxHashsign = uriString.lastIndexOf("#");
4✔
146
        if (splitIdx > 0 && splitIdxHashsign > splitIdx - 2) {
7✔
147
            String namespace = uriString.substring(0, splitIdxHashsign);
5✔
148
            String p = namespaceTable.get(namespace);
6✔
149
            String postHashPrefix = namespaceTable.get(namespace + "#");
7✔
150
            if (p != null && postHashPrefix == null) {
4!
151
                splitIdx = splitIdxHashsign;
×
152
                prefix = p;
×
153
            }
154
        }
155

156
        if (uriString.endsWith(".")) {
4✔
157
            prefix = null;
2✔
158
        }
159

160
        if (prefix != null) {
2✔
161
            // Namespace is mapped to a prefix; write abbreviated URI
162
            writer.write(prefix);
4✔
163
            writer.write(":");
4✔
164
            writer.write(uriString.substring(splitIdx).replaceFirst("^#", "\\\\#"));
9✔
165
            if (usedPrefixes != null) {
3✔
166
                usedPrefixes.add(prefix);
6✔
167
            }
168
        } else {
169
            // Write full URI
170
            writer.write("<");
4✔
171
            writer.write(TurtleUtil.encodeURIString(uriString));
5✔
172
            writer.write(">");
4✔
173
        }
174
    }
1✔
175

176
    // Overriding this method to *not* normalize/pretty-print literals.
177

178
    /**
179
     * {@inheritDoc}
180
     */
181
    @Override
182
    protected void writeLiteral(Literal lit) throws IOException {
183
        String label = lit.getLabel();
3✔
184
        IRI datatype = lit.getDatatype();
3✔
185

186
        if (label.indexOf('\n') != -1 || label.indexOf('\r') != -1 || label.indexOf('\t') != -1) {
15!
187
            // Write label as long string
188
            writer.write("\"\"\"");
4✔
189
            writer.write(TurtleUtil.encodeLongString(label));
5✔
190
            writer.write("\"\"\"");
5✔
191
        } else {
192
            // Write label as normal string
193
            writer.write("\"");
4✔
194
            writer.write(TurtleUtil.encodeString(label));
5✔
195
            writer.write("\"");
4✔
196
        }
197

198
        if (Literals.isLanguageLiteral(lit)) {
3!
199
            // Append the literal's language
200
            writer.write("@");
×
201
            writer.write(lit.getLanguage().get());
×
202
        } else if (!XSD.STRING.equals(datatype)) {
4✔
203
            // Append the literal's datatype (possibly written as an abbreviated
204
            // URI)
205
            writer.write("^^");
4✔
206
            writeURI(datatype);
3✔
207
        }
208
    }
1✔
209
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc