| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.any23.rdf; |
| 19 | |
|
| 20 | |
import org.apache.any23.extractor.ErrorReporter; |
| 21 | |
import org.openrdf.model.BNode; |
| 22 | |
import org.openrdf.model.Literal; |
| 23 | |
import org.openrdf.model.Resource; |
| 24 | |
import org.openrdf.model.Statement; |
| 25 | |
import org.openrdf.model.URI; |
| 26 | |
import org.openrdf.model.Value; |
| 27 | |
import org.openrdf.model.ValueFactory; |
| 28 | |
import org.slf4j.Logger; |
| 29 | |
import org.slf4j.LoggerFactory; |
| 30 | |
|
| 31 | |
import javax.xml.datatype.XMLGregorianCalendar; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class Any23ValueFactoryWrapper implements ValueFactory { |
| 39 | |
|
| 40 | 0 | private static final Logger logger = LoggerFactory.getLogger(Any23ValueFactoryWrapper.class); |
| 41 | |
|
| 42 | |
private final ValueFactory wrappedFactory; |
| 43 | |
|
| 44 | |
private ErrorReporter errorReporter; |
| 45 | |
|
| 46 | |
private String defaultLiteralLanguage; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public Any23ValueFactoryWrapper( |
| 56 | |
final ValueFactory factory, |
| 57 | |
ErrorReporter er, |
| 58 | |
String defaultLitLanguage |
| 59 | 0 | ) { |
| 60 | 0 | if(factory == null) { |
| 61 | 0 | throw new NullPointerException("factory cannot be null."); |
| 62 | |
} |
| 63 | 0 | wrappedFactory = factory; |
| 64 | 0 | errorReporter = er; |
| 65 | 0 | defaultLiteralLanguage = defaultLitLanguage; |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public Any23ValueFactoryWrapper(final ValueFactory vFactory, ErrorReporter er) { |
| 69 | 0 | this(vFactory, er, null); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
public Any23ValueFactoryWrapper(final ValueFactory vFactory) { |
| 73 | 0 | this(vFactory, null, null); |
| 74 | 0 | } |
| 75 | |
|
| 76 | |
public ErrorReporter getErrorReporter() { |
| 77 | 0 | return errorReporter; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public void setErrorReporter(ErrorReporter er) { |
| 81 | 0 | errorReporter = er; |
| 82 | 0 | } |
| 83 | |
|
| 84 | |
public String getDefaultLiteralLanguage() { |
| 85 | 0 | return defaultLiteralLanguage; |
| 86 | |
} |
| 87 | |
|
| 88 | |
public BNode createBNode() { |
| 89 | 0 | return wrappedFactory.createBNode(); |
| 90 | |
} |
| 91 | |
|
| 92 | |
public BNode createBNode(String id) { |
| 93 | 0 | if (id == null) return null; |
| 94 | 0 | return wrappedFactory.createBNode(id); |
| 95 | |
} |
| 96 | |
|
| 97 | |
public Literal createLiteral(String content) { |
| 98 | 0 | if (content == null) return null; |
| 99 | 0 | return wrappedFactory.createLiteral(content, defaultLiteralLanguage); |
| 100 | |
} |
| 101 | |
|
| 102 | |
public Literal createLiteral(boolean b) { |
| 103 | 0 | return wrappedFactory.createLiteral(b); |
| 104 | |
} |
| 105 | |
|
| 106 | |
public Literal createLiteral(byte b) { |
| 107 | 0 | return wrappedFactory.createLiteral(b); |
| 108 | |
} |
| 109 | |
|
| 110 | |
public Literal createLiteral(short i) { |
| 111 | 0 | return wrappedFactory.createLiteral(i); |
| 112 | |
} |
| 113 | |
|
| 114 | |
public Literal createLiteral(int i) { |
| 115 | 0 | return wrappedFactory.createLiteral(i); |
| 116 | |
} |
| 117 | |
|
| 118 | |
public Literal createLiteral(long l) { |
| 119 | 0 | return wrappedFactory.createLiteral(l); |
| 120 | |
} |
| 121 | |
|
| 122 | |
public Literal createLiteral(float v) { |
| 123 | 0 | return wrappedFactory.createLiteral(v); |
| 124 | |
} |
| 125 | |
|
| 126 | |
public Literal createLiteral(double v) { |
| 127 | 0 | return wrappedFactory.createLiteral(v); |
| 128 | |
} |
| 129 | |
|
| 130 | |
public Literal createLiteral(XMLGregorianCalendar calendar) { |
| 131 | 0 | return wrappedFactory.createLiteral(calendar); |
| 132 | |
} |
| 133 | |
|
| 134 | |
public Literal createLiteral(String label, String language) { |
| 135 | 0 | if (label == null) return null; |
| 136 | 0 | return wrappedFactory.createLiteral(label, language); |
| 137 | |
} |
| 138 | |
|
| 139 | |
public Literal createLiteral(String pref, URI value) { |
| 140 | 0 | if (pref == null) return null; |
| 141 | 0 | return wrappedFactory.createLiteral(pref, value); |
| 142 | |
} |
| 143 | |
|
| 144 | |
public Statement createStatement(Resource sub, URI pre, Value obj) { |
| 145 | 0 | if (sub == null || pre == null || obj == null) { |
| 146 | 0 | return null; |
| 147 | |
} |
| 148 | 0 | return wrappedFactory.createStatement(sub, pre, obj); |
| 149 | |
} |
| 150 | |
|
| 151 | |
public Statement createStatement(Resource sub, URI pre, Value obj, Resource context) { |
| 152 | 0 | if (sub == null || pre == null || obj == null) return null; |
| 153 | 0 | return wrappedFactory.createStatement(sub, pre, obj, context); |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
public URI createURI(String uriStr) { |
| 161 | 0 | if (uriStr == null) return null; |
| 162 | |
try { |
| 163 | 0 | return wrappedFactory.createURI(RDFUtils.fixURIWithException(uriStr)); |
| 164 | 0 | } catch (Exception e) { |
| 165 | 0 | reportError(e); |
| 166 | 0 | return null; |
| 167 | |
} |
| 168 | |
} |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
public URI createURI(String namespace, String localName) { |
| 174 | 0 | if (namespace == null || localName == null) return null; |
| 175 | 0 | return wrappedFactory.createURI(RDFUtils.fixURIWithException(namespace), localName); |
| 176 | |
} |
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
public URI resolveURI(String uri, java.net.URI baseURI) { |
| 186 | |
try { |
| 187 | 0 | return wrappedFactory.createURI(baseURI.resolve(RDFUtils.fixURIWithException(uri)).toString()); |
| 188 | 0 | } catch (IllegalArgumentException iae) { |
| 189 | 0 | reportError(iae); |
| 190 | 0 | return null; |
| 191 | |
} |
| 192 | |
} |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
public URI fixURI(String uri) { |
| 199 | |
try { |
| 200 | 0 | return wrappedFactory.createURI(RDFUtils.fixURIWithException(uri)); |
| 201 | 0 | } catch (Exception e) { |
| 202 | 0 | reportError(e); |
| 203 | 0 | return null; |
| 204 | |
} |
| 205 | |
} |
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public URI fixLink(String link, String defaultSchema) { |
| 211 | 0 | if (link == null) return null; |
| 212 | 0 | link = fixWhiteSpace(link); |
| 213 | 0 | if ("".equals(link)) return null; |
| 214 | 0 | if (defaultSchema != null && !link.startsWith(defaultSchema + ":")) { |
| 215 | 0 | link = defaultSchema + ":" + link; |
| 216 | |
} |
| 217 | 0 | return fixURI(link); |
| 218 | |
} |
| 219 | |
|
| 220 | |
public String fixWhiteSpace(String name) { |
| 221 | 0 | return name.replaceAll("\\s+", " ").trim(); |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
private void reportError(Exception e) { |
| 230 | 0 | if(errorReporter == null) { |
| 231 | 0 | logger.warn(e.getMessage()); |
| 232 | |
} else { |
| 233 | 0 | errorReporter.notifyError(ErrorReporter.ErrorLevel.WARN, e.getMessage(), -1 , -1); |
| 234 | |
} |
| 235 | 0 | } |
| 236 | |
|
| 237 | |
} |