~slub.team/goobi-production/bug-1013622

« back to all changes in this revision

Viewing changes to src/de/unigoettingen/sub/commons/util/datasource/DirectoryListingUrlImageSource.java

  • Committer: Ralf Claussnitzer
  • Date: 2012-05-29 10:55:34 UTC
  • mfrom: (66.1.1 integrate-util)
  • Revision ID: ralf.claussnitzer@slub-dresden.de-20120529105534-t5u5vxj9x5v2vifb
fixes lp:1005844 integrate sub-commons util library

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of the SUB Commons project.
 
3
 * Visit the websites for more information. 
 
4
 *              - http://gdz.sub.uni-goettingen.de 
 
5
 * 
 
6
 * Copyright 2009, Center for Retrospective Digitization, Göttingen (GDZ),
 
7
 * intranda software.
 
8
 * 
 
9
 * Licensed under the Apache License, Version 2.0 (the “License”);
 
10
 * you may not use this file except in compliance with the License.
 
11
 * You may obtain a copy of the License at
 
12
 * 
 
13
 *  http://www.apache.org/licenses/LICENSE-2.0
 
14
 * 
 
15
 * Unless required by applicable law or agreed to in writing, software
 
16
 * distributed under the License is distributed on an “AS IS” BASIS,
 
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
18
 * See the License for the specific language governing permissions and
 
19
 * limitations under the License.
 
20
 */
 
21
package de.unigoettingen.sub.commons.util.datasource;
 
22
 
 
23
import java.io.File;
 
24
import java.io.IOException;
 
25
import java.net.MalformedURLException;
 
26
import java.net.URL;
 
27
import java.util.ArrayList;
 
28
import java.util.Arrays;
 
29
import java.util.List;
 
30
import java.util.Map;
 
31
 
 
32
// TODO: Auto-generated Javadoc
 
33
/**
 
34
 * The Class DirectoryListingUrlImageSource.
 
35
 */
 
36
public class DirectoryListingUrlImageSource implements ImageSource {
 
37
        
 
38
        /** The dir. */
 
39
        protected File dir = null;
 
40
        
 
41
        /** The files. */
 
42
        private List<File> files = null;
 
43
        
 
44
        /**
 
45
         * Instantiates a new directory listing url image source.
 
46
         * 
 
47
         * @param dir the dir
 
48
         */
 
49
        public DirectoryListingUrlImageSource (File dir) {
 
50
                this.dir = dir;
 
51
        }
 
52
 
 
53
        /* (non-Javadoc)
 
54
         * @see de.unigoettingen.sub.commons.util.datasource.ImageSource#getImage(java.lang.Integer)
 
55
         */
 
56
        public Image getImage(Integer pageNr) throws IOException {
 
57
                init();
 
58
                SimpleUrlImage img = new SimpleUrlImage();
 
59
                img.setPageNumber(pageNr);
 
60
                img.setURL(files.get(pageNr).toURI().toURL());
 
61
                return img;
 
62
        }
 
63
 
 
64
        /* (non-Javadoc)
 
65
         * @see de.unigoettingen.sub.commons.util.datasource.ImageSource#getImageList()
 
66
         */
 
67
        public List<? extends Image> getImageList() throws IOException {
 
68
                init();
 
69
                List<SimpleUrlImage> images = new ArrayList<SimpleUrlImage>();
 
70
                for (int i = 0; i < files.size(); i++) {
 
71
                        SimpleUrlImage img = new SimpleUrlImage();
 
72
                        img.setPageNumber(i + 1);
 
73
                        img.setURL(files.get(i).toURI().toURL());
 
74
                        images.add(img);
 
75
                }
 
76
                return images;
 
77
        }
 
78
 
 
79
        /* (non-Javadoc)
 
80
         * @see de.unigoettingen.sub.commons.util.datasource.ImageSource#getImageMap()
 
81
         */
 
82
        public Map<Integer, ? extends Image> getImageMap() throws IOException {
 
83
                // TODO Auto-generated method stub
 
84
                return null;
 
85
        }
 
86
 
 
87
        /* (non-Javadoc)
 
88
         * @see de.unigoettingen.sub.commons.util.datasource.ImageSource#getNumberOfPages()
 
89
         */
 
90
        public Integer getNumberOfPages() {
 
91
                try {
 
92
                        init();
 
93
                } catch (IOException e) {
 
94
                        return 0;
 
95
                }
 
96
                return files.size();
 
97
        }
 
98
 
 
99
        /* (non-Javadoc)
 
100
         * @see de.unigoettingen.sub.commons.util.datasource.DataSource#close()
 
101
         */
 
102
        public void close() throws IOException {
 
103
                //Do nothing
 
104
        }
 
105
 
 
106
        /* (non-Javadoc)
 
107
         * @see de.unigoettingen.sub.commons.util.datasource.DataSource#getUrl()
 
108
         */
 
109
        public URL getUrl() {
 
110
                try {
 
111
                        return dir.toURI().toURL();
 
112
                } catch (MalformedURLException e) {
 
113
                        return null;
 
114
                }
 
115
        }
 
116
 
 
117
        /**
 
118
         * Checks the given directory and add its contens as list
 
119
         * 
 
120
         * @throws IOException Signals that an I/O exception has occurred.
 
121
         */
 
122
        private void init () throws IOException {
 
123
                if (files == null) {
 
124
                        if (!dir.isDirectory()) {
 
125
                                throw new IOException("Given File is not a directory");
 
126
                        }
 
127
                        files = Arrays.asList(dir.listFiles());
 
128
                        //TODO: Filter for supported Filetypes
 
129
                }
 
130
        }
 
131
 
 
132
}