Initialise this resource, and return it.
()
| 243 | |
| 244 | /** Initialise this resource, and return it. */ |
| 245 | @Override |
| 246 | public Resource init() throws ResourceInstantiationException { |
| 247 | // set up the source URL and create the content |
| 248 | if(sourceUrl == null) { |
| 249 | if(stringContent == null) { throw new ResourceInstantiationException( |
| 250 | "The sourceURL and document's content were null."); } |
| 251 | content = new DocumentContentImpl(stringContent); |
| 252 | getFeatures().put("gate.SourceURL", "created from String"); |
| 253 | } else { |
| 254 | try { |
| 255 | URL resolved = gate.Utils.resolveURL(sourceUrl); |
| 256 | getFeatures().put("gate.OriginalURL", sourceUrl.toExternalForm()); |
| 257 | sourceUrl = resolved; |
| 258 | } |
| 259 | catch (IOException e) { |
| 260 | System.err.println("Unable to resolve URL"); |
| 261 | e.printStackTrace(); |
| 262 | } |
| 263 | |
| 264 | try { |
| 265 | if(!DocumentFormat.willReadFromUrl(mimeType, sourceUrl)) { |
| 266 | content = new DocumentContentImpl(sourceUrl, getEncoding(), |
| 267 | sourceUrlStartOffset, sourceUrlEndOffset); |
| 268 | } |
| 269 | getFeatures().put("gate.SourceURL", sourceUrl.toExternalForm()); |
| 270 | } catch(IOException e) { |
| 271 | throw new ResourceInstantiationException("DocumentImpl.init: " + e); |
| 272 | } |
| 273 | } |
| 274 | if(preserveOriginalContent && content != null) { |
| 275 | String originalContent = ((DocumentContentImpl)content) |
| 276 | .getOriginalContent(); |
| 277 | getFeatures().put(GateConstants.ORIGINAL_DOCUMENT_CONTENT_FEATURE_NAME, |
| 278 | originalContent); |
| 279 | } // if |
| 280 | // set up a DocumentFormat if markup unpacking required |
| 281 | if(getMarkupAware()) { |
| 282 | DocumentFormat docFormat = null; |
| 283 | // if a specific MIME type has been given, use it |
| 284 | if(this.mimeType != null && this.mimeType.length() > 0) { |
| 285 | MimeType theType = DocumentFormat.getMimeTypeForString(mimeType); |
| 286 | if(theType == null) { |
| 287 | throw new ResourceInstantiationException("MIME type \"" |
| 288 | + this.mimeType + " has no registered DocumentFormat"); |
| 289 | } |
| 290 | |
| 291 | docFormat = DocumentFormat.getDocumentFormat(this, theType); |
| 292 | } |
| 293 | else { |
| 294 | docFormat = DocumentFormat.getDocumentFormat(this, sourceUrl); |
| 295 | } |
| 296 | try { |
| 297 | if(docFormat != null) { |
| 298 | StatusListener sListener = (StatusListener)gate.Gate |
| 299 | .getListeners().get("gate.event.StatusListener"); |
| 300 | if(sListener != null) docFormat.addStatusListener(sListener); |
| 301 | // set the flag if true and if the document format support collecting |
| 302 | docFormat.setShouldCollectRepositioning(collectRepositioningInfo); |
nothing calls this directly
no test coverage detected