View Javadoc

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.IssueReport;
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      private static final Logger logger = LoggerFactory.getLogger(Any23ValueFactoryWrapper.class);
41  
42      private final ValueFactory wrappedFactory;
43  
44      private IssueReport issueReport;
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              IssueReport er,
58              String defaultLitLanguage
59      ) {
60          if(factory == null) {
61              throw new NullPointerException("factory cannot be null.");
62          }
63          wrappedFactory = factory;
64          issueReport = er;
65          defaultLiteralLanguage = defaultLitLanguage;
66      }
67  
68      public Any23ValueFactoryWrapper(final ValueFactory vFactory, IssueReport er) {
69          this(vFactory, er, null);
70      }
71  
72      public Any23ValueFactoryWrapper(final ValueFactory vFactory) {
73          this(vFactory, null, null);
74      }
75  
76      public IssueReport getIssueReport() {
77          return issueReport;
78      }
79  
80      public void setIssueReport(IssueReport er) {
81          issueReport = er;
82      }
83  
84      public String getDefaultLiteralLanguage() {
85          return defaultLiteralLanguage;
86      }
87  
88      public BNode createBNode() {
89          return wrappedFactory.createBNode();
90      }
91  
92      public BNode createBNode(String id) {
93          if (id == null) return null;
94          return wrappedFactory.createBNode(id);
95      }
96  
97      public Literal createLiteral(String content) {
98          if (content == null) return null;
99          return wrappedFactory.createLiteral(content, defaultLiteralLanguage);
100     }
101 
102     public Literal createLiteral(boolean b) {
103         return wrappedFactory.createLiteral(b);
104     }
105 
106     public Literal createLiteral(byte b) {
107         return wrappedFactory.createLiteral(b);
108     }
109 
110     public Literal createLiteral(short i) {
111         return wrappedFactory.createLiteral(i);
112     }
113 
114     public Literal createLiteral(int i) {
115         return wrappedFactory.createLiteral(i);
116     }
117 
118     public Literal createLiteral(long l) {
119         return wrappedFactory.createLiteral(l);
120     }
121 
122     public Literal createLiteral(float v) {
123         return wrappedFactory.createLiteral(v);
124     }
125 
126     public Literal createLiteral(double v) {
127         return wrappedFactory.createLiteral(v);
128     }
129 
130     public Literal createLiteral(XMLGregorianCalendar calendar) {
131         return wrappedFactory.createLiteral(calendar);
132     }
133 
134     public Literal createLiteral(String label, String language) {
135         if (label == null) return null;
136         return wrappedFactory.createLiteral(label, language);
137     }
138 
139     public Literal createLiteral(String pref, URI value) {
140         if (pref == null) return null;
141         return wrappedFactory.createLiteral(pref, value);
142     }
143 
144     public Statement createStatement(Resource sub, URI pre, Value obj) {
145         if (sub == null || pre == null || obj == null) {
146             return null;
147         }
148         return wrappedFactory.createStatement(sub, pre, obj);
149     }
150 
151     public Statement createStatement(Resource sub, URI pre, Value obj, Resource context) {
152         if (sub == null || pre == null || obj == null) return null;
153         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         if (uriStr == null) return null;
162         try {
163             return wrappedFactory.createURI(RDFUtils.fixURIWithException(uriStr));
164         } catch (Exception e) {
165             reportError(e);
166             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         if (namespace == null || localName == null) return null;
175         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             return wrappedFactory.createURI(baseURI.resolve(RDFUtils.fixURIWithException(uri)).toString());
188         } catch (IllegalArgumentException iae) {
189             reportError(iae);
190             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             return wrappedFactory.createURI(RDFUtils.fixURIWithException(uri));
201         } catch (Exception e) {
202             reportError(e);
203             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         if (link == null) return null;
212         link = fixWhiteSpace(link);
213         if ("".equals(link)) return null;
214         if (defaultSchema != null && !link.startsWith(defaultSchema + ":")) {
215             link = defaultSchema + ":" + link;
216         }
217         return fixURI(link);
218     }
219 
220     public String fixWhiteSpace(String name) {
221         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         if(issueReport == null) {
231             logger.warn(e.getMessage());
232         } else {
233             issueReport.notifyIssue(IssueReport.IssueLevel.Warning, e.getMessage(), -1, -1);
234         }
235     }
236 
237 }