View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.tools.imagecollection.collection.webpage;
31  
32  import java.io.IOException;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  import java.text.ParseException;
36  import java.util.ArrayList;
37  import java.util.HashSet;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Set;
42  
43  import org.jsoup.Jsoup;
44  import org.jsoup.nodes.Document;
45  import org.openimaj.image.MBFImage;
46  import org.openimaj.tools.imagecollection.collection.ImageCollection;
47  import org.openimaj.tools.imagecollection.collection.ImageCollectionEntry;
48  import org.openimaj.tools.imagecollection.collection.ImageCollectionEntrySelection;
49  import org.openimaj.tools.imagecollection.collection.ImageCollectionSetupException;
50  import org.openimaj.tools.imagecollection.collection.config.ImageCollectionConfig;
51  import org.openimaj.util.pair.IndependentPair;
52  
53  public abstract class AbstractWebpageImageCollection implements ImageCollection<MBFImage>{
54  
55  	private ImageCollectionEntrySelection<MBFImage> selection = null;
56  	private Set<IndependentPair<URL, Map<String, String>>> imageList;
57  
58  	@Override
59  	public Iterator<ImageCollectionEntry<MBFImage>> iterator() {
60  		return new URLImageIterator(imageList,selection);
61  	}
62  	
63  	@Override
64  	public void setup(ImageCollectionConfig config) throws ImageCollectionSetupException {
65  		
66  		String url = null;
67  		
68  		try {
69  			url = config.read("webpage.url");
70  		} catch (ParseException e) {
71  			throw new ImageCollectionSetupException("Could not deal with image source url, configuration error");
72  		}
73  		try {
74  			this.imageList = prepareURLs(new URL(url));
75  		} catch (MalformedURLException e) {
76  			throw new ImageCollectionSetupException("Could not deal with image source url, invalid URL");
77  		}
78  	}
79  
80  	public abstract Set<IndependentPair<URL, Map<String, String>>> prepareURLs(URL url) throws ImageCollectionSetupException;
81  	
82  
83  	@Override
84  	public int useable(ImageCollectionConfig config) {
85  		String url;
86  		try {
87  			url = config.read("webpage.url");
88  		} catch (ParseException e) {
89  			return -1;
90  		}
91  		if(url!=null) return 0;
92  		return -1;
93  	}
94  
95  	@Override
96  	public List<ImageCollectionEntry<MBFImage>> getAll() {
97  		List<ImageCollectionEntry<MBFImage>> entries = new ArrayList<ImageCollectionEntry<MBFImage>>();
98  		for (ImageCollectionEntry<MBFImage> imageCollectionEntry : this) {
99  			entries.add(imageCollectionEntry);
100 		}
101 		return entries;
102 	}
103 
104 	@Override
105 	public int countImages() {
106 		return this.imageList.size();
107 	}
108 
109 	@Override
110 	public void setEntrySelection(ImageCollectionEntrySelection<MBFImage> selection) {
111 		this.selection  = selection;
112 		
113 	}
114 	
115 	public static class Generic extends AbstractWebpageImageCollection{
116 		@Override
117 		public Set<IndependentPair<URL, Map<String, String>>> prepareURLs(URL url) throws ImageCollectionSetupException {
118 			Document doc = null;
119 			try {
120 				doc = Jsoup.parse(url, 1000);
121 			} catch (IOException e) {
122 				throw new ImageCollectionSetupException("Could not deal with image source url, problem parsing HTML");
123 			}
124 			Set<IndependentPair<URL, Map<String, String>>> imageList = 
125 				new HashSet<IndependentPair<URL, Map<String, String>>>();
126 			imageList.addAll(WebpageUtils.allURLs(doc,"img","src"));
127 			imageList.addAll(WebpageUtils.allURLs(doc,"a[href$=.png]","href"));
128 			return imageList;
129 		}
130 
131 		@Override
132 		public int useable(String rawInput) {
133 			// TODO Auto-generated method stub
134 			return 0;
135 		}
136 
137 		@Override
138 		public ImageCollectionConfig defaultConfig(String rawInput) {
139 			// TODO Auto-generated method stub
140 			return null;
141 		}
142 	}
143 }