##### Coverage Report - org.apache.any23.rdf.Any23ValueFactoryWrapper

Classes in this File | Line Coverage | Branch Coverage | Complexity
Any23ValueFactoryWrapper |
0% | 0/64
0% | 0/38
| 2.2857142857142856;2.286

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
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 |  |
      * Any23 specialization of the {@link org.openrdf.model.ValueFactory}.
35 |  |
      * It provides a wrapper to instantiate RDF objects.
36 |  |
      */
37 |  |
     // TODO: Merge with RDFUtils.java
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 |  |
          * Constructor with error reporter.
50 |  |
          *
51 |  |
          * @param factory the wrapped value factory, cannot be <code>null</code>.
52 |  |
          * @param er the error reporter.
53 |  |
          * @param defaultLitLanguage the default literal language.
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 |  |
          * @param uriStr
158 |  |
          * @return a valid sesame URI or null if any exception occurred
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 |  |
          * @return a valid sesame URI or null if any exception occurred
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 |  |
          * Fixes typical errors in URIs, and resolves relative URIs against a base URI.
180 |  |
          *
181 |  |
          * @param uri     A URI, relative or absolute, can have typical syntax errors
182 |  |
          * @param baseURI A base URI to use for resolving relative URIs
183 |  |
          * @return An absolute URI, sytnactically valid, or null if not fixable
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 |  |
          * @param uri
196 |  |
          * @return a valid sesame URI or null if any exception occurred
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 |  |
          * Helper method to conditionally add a schema to a URI unless it's there, or null if link is empty.
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 |  |
          * Reports an error in the most appropriate way.
226 |  |
          *
227 |  |
          * @param e error to be reported.
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 |  |
     }