protected void storeFileReferences()

in taverna-prov/src/main/java/org/apache/taverna/prov/W3ProvenanceExport.java [473:541]


    protected void storeFileReferences() {

        for (Entry<Path, T2Reference> entry : getFileToT2Reference().entrySet()) {
            Path file = entry.getKey();

            try {
                T2Reference t2Ref = entry.getValue();
                URI dataURI = URI.create(uriGenerator.makeT2ReferenceURI(t2Ref
                        .toUri().toASCIIString()));

                Individual entity = provModel.createArtifact(dataURI);

                String mediaType = saver.getMediaTypes().get(t2Ref);

                if (!Files.exists(file)) {
                    continue;
                }
                URI contentUri;
                if (DataBundles.isReference(file)) {
                    // TODO: Do we really need to read this back again from the
                    // file?
                    contentUri = DataBundles.getReference(file);
                } else {
                    contentUri = toURI(file);
                }

                Individual content = provModel.setContent(entity, contentUri);
                if (mediaType != null) {
                    mediaTypes.put(contentUri, mediaType);
                }
                if (!DataBundles.isValue(file)) {
                    // Don't capture the checksum and content of references and
                    // lists
                    continue;
                }

                // Add checksums
                String sha1 = saver.getSha1sums().get(file.toRealPath());
                if (sha1 != null) {
                    content.addLiteral(provModel.sha1, sha1);
                }
                String sha512 = saver.getSha512sums().get(file.toRealPath());
                if (sha512 != null) {
                    content.addLiteral(provModel.sha512, sha512);
                }
                long byteCount = Files.size(file);
                content.addLiteral(provModel.byteCount, byteCount);

                if (byteCount < EMBEDDED_MAX_FILESIZE) {
                    // Add content if it's "tiny"
                    byte[] bytes = Files.readAllBytes(file);
                    if (mediaType != null && mediaType.startsWith(TEXT)) {
                        // as string - assuming UTF8 (and declaring so)
                        String str = new String(bytes, UTF8);
                        content.addLiteral(provModel.chars, str);
                        content.addLiteral(provModel.characterEncoding,
                                UTF8.name());
                        content.addRDFType(provModel.ContentAsText);
                    } else {
                        // Or base64-encoded bytes
                        content.addRDFType(provModel.ContentAsBase64);
                        content.addLiteral(provModel.bytes, bytes);
                    }
                }
            } catch (IOException e) {
                logger.warn("Could not read " + file + " as " + UTF8, e);
            }
        }
    }