Project

General

Profile

« Previous | Next » 

Revision 2816580a

Added by Niels Hoffmann over 15 years ago

refactored and fixed some issues concerning the import of Media for BerlinModel NameFacts

View differences:

cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/CdmUtils.java
13 13
import java.io.File;
14 14
import java.io.IOException;
15 15
import java.io.InputStream;
16
import java.net.HttpURLConnection;
16 17
import java.net.MalformedURLException;
17 18
import java.net.URL;
18 19

  
......
153 154
	
154 155
	static public boolean urlExists(String strUrl, boolean withWarning){
155 156
		try {
156
			URL url = new URL(strUrl);
157
			url.getContent();
158
			return true;
157
		     HttpURLConnection.setFollowRedirects(false);
158
		      // note : you may also need
159
		      //        HttpURLConnection.setInstanceFollowRedirects(false)
160
		      HttpURLConnection con =
161
		         (HttpURLConnection) new URL(strUrl).openConnection();
162
		      con.setRequestMethod("HEAD");
163
		      return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
159 164
		} catch (MalformedURLException e) {
160 165
			if (withWarning) {
161 166
				logger.warn(e);
cdmlib-commons/src/main/java/eu/etaxonomy/cdm/common/MediaMetaData.java
5 5
import java.io.File;
6 6
import java.io.FileInputStream;
7 7
import java.io.IOException;
8
import java.util.Iterator;
8 9

  
9 10
import javax.imageio.ImageIO;
10 11

  
......
80 81
            if (!ii.check()) {
81 82
                // ImageInfo can not handle TIFF, ...
82 83
                // use Java AWT/ImageIO installation of the
84
//            	
85
//            	FIXME
86
//            	the above comment got truncated so i am posting 
87
//            	what I found out. Tiff-Images can only be read if an 
88
//            	extension to ImageIO is installed. The Extension comes as 
89
//            	JAI-IMAGEIO, which is part the JavaAdvancedImage-API and also 
90
//            	platform dependant. Maybe include the in our own maven repo
91
//            	https://jai-imageio.dev.java.net/binary-builds.html
92
//            	linux version comes with jar files which are platform independant 
93
//            	but you loose native acceleration
94
//            	
95
            	
83 96
                ii = null;
84
            	BufferedImage bim = ImageIO.read(in);
85
            	if(bim == null)
86
            		throw new IOException("Cannot read "+imageFile);
87
            	imageMetaData.width = bim.getWidth();
88
            	imageMetaData.height = bim.getHeight();
89
            	imageMetaData.bitPerPixel = bim.getColorModel().getPixelSize();
97
    			BufferedImage bImage = ImageIO.read(imageFile);
98
            	if(bImage == null){
99
            		in.close();
100
            		throw new IOException(" No ImageReader for image type. Is jai-imageio installed?");
101
            	}
102
            	imageMetaData.width = bImage.getWidth();
103
            	imageMetaData.height = bImage.getHeight();
104
            	imageMetaData.bitPerPixel = bImage.getColorModel().getPixelSize();
90 105
            	imageMetaData.mimeType = "?";
91
            	imageMetaData.formatName = String.valueOf(bim.getType());
106
            	imageMetaData.formatName = String.valueOf(bImage.getType());
92 107
            	
93 108
            } else {
94 109
                // ImageInfo can analyze:
......
96 111
                // PPM
97 112
                // and PSD
98 113
            	imageMetaData.formatName = ii.getFormatName();
114
            	
115
            	
99 116
            	imageMetaData.mimeType = ii.getMimeType();
100 117
            	imageMetaData.width = ii.getWidth();
101 118
            	imageMetaData.height = ii.getHeight();
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/berlinModel/BerlinModelImportConfigurator.java
1 1
 package eu.etaxonomy.cdm.io.berlinModel;
2 2

  
3
import java.io.File;
4
import java.net.MalformedURLException;
5
import java.net.URL;
3 6
import java.util.HashSet;
4 7
import java.util.Set;
5 8

  
6 9
import org.apache.log4j.Logger;
7 10

  
8 11
import eu.etaxonomy.cdm.database.ICdmDataSource;
9
import eu.etaxonomy.cdm.io.common.ICdmIO;
10 12
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
11 13
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
12 14
import eu.etaxonomy.cdm.io.common.Source;
13
import eu.etaxonomy.cdm.io.tcs.TcsImportConfigurator;
14
import eu.etaxonomy.cdm.io.tcs.TcsReferenceIO;
15
import eu.etaxonomy.cdm.io.tcs.TcsTaxonIO;
16
import eu.etaxonomy.cdm.io.tcs.TcsTaxonNameIO;
17
import eu.etaxonomy.cdm.io.tcs.TcsTaxonNameRelationsIO;
18
import eu.etaxonomy.cdm.io.tcs.TcsTaxonRelationsIO;
19 15
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
20 16
import eu.etaxonomy.cdm.model.reference.Database;
21 17
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
......
31 27
	private Set<Synonym> proParteSynonyms = new HashSet<Synonym>();
32 28
	private Set<Synonym> partialSynonyms = new HashSet<Synonym>();
33 29
	
30
	private URL mediaUrl;
31
	private File mediaPath;
32
	
34 33
	protected void makeIoClassList(){
35 34
		ioClassList = new Class[]{
36 35
				BerlinModelAuthorIO.class
......
108 107
	protected boolean isPartialSynonym(Synonym synonym){
109 108
		return this.partialSynonyms.contains(synonym);
110 109
	}
110

  
111
	/**
112
	 * @return the mediaUrl
113
	 */
114
	public URL getMediaUrl() {
115
		return mediaUrl;
116
	}
117

  
118
	/**
119
	 * @param mediaUrl the mediaUrl to set
120
	 */
121
	public void setMediaUrl(URL mediaUrl) {
122
		this.mediaUrl = mediaUrl;
123
	}
124

  
125
	/**
126
	 * @return the mediaPath
127
	 */
128
	public File getMediaPath() {
129
		return mediaPath;
130
	}
131

  
132
	/**
133
	 * @param mediaPath the mediaPath to set
134
	 */
135
	public void setMediaPath(File mediaPath) {
136
		this.mediaPath = mediaPath;
137
	}
138
	
139
	public void setMediaPath(String mediaPathString){
140
		this.mediaPath = new File(mediaPathString);
141
	}
142

  
143
	public void setMediaUrl(String mediaUrlString) {
144
		try {
145
			this.mediaUrl = new URL(mediaUrlString);
146
		} catch (MalformedURLException e) {
147
			logger.error("Could not set mediaUrl because it was malformed: " + mediaUrlString);
148
		}
149
	}
111 150
	
112 151
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/berlinModel/BerlinModelNameFactsIO.java
7 7
import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.NAME_FACT_PROTOLOGUE;
8 8

  
9 9
import java.io.File;
10
import java.net.MalformedURLException;
11
import java.net.URL;
10 12
import java.sql.ResultSet;
11 13
import java.sql.SQLException;
12 14
import java.util.HashSet;
......
111 113
					if (category.equalsIgnoreCase(NAME_FACT_PROTOLOGUE)){
112 114
						//ReferenceBase ref = (ReferenceBase)taxonNameBase.getNomenclaturalReference();
113 115
						//ref = Book.NewInstance();
114
						Media media = getMedia(nameFact);
116
						Media media = getMedia(nameFact, bmiConfig.getMediaUrl(), bmiConfig.getMediaPath());
115 117
						if (media.getRepresentations().size() > 0){
116 118
							TaxonNameDescription description = TaxonNameDescription.NewInstance();
117 119
							TextData protolog = TextData.NewInstance(Feature.PROTOLOG());
......
170 172
	}
171 173
	
172 174
	
173
	private Media getMedia(String nameFact){
175
	private Media getMedia(String nameFact, URL mediaUrl, File mediaPath){
174 176
		String mimeTypeTif = "image/tiff";
175 177
		String mimeTypeJpeg = "image/jpeg";
176 178
		String mimeTypePng = "image/png";
......
180 182
		String sep = File.separator;
181 183
		Integer size = null;
182 184
		
185
		logger.warn("Getting media for NameFact: " + nameFact);
183 186
		
184 187
		Media media = Media.NewInstance();
185
		ImageMetaData imageMetaData = new ImageMetaData();
186
		String urlPath = "http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/";
187
		String strFolderPath = sep + sep + "Bgbm11" + sep  + "Edit-WP6" + sep + "protolog" + sep;
188
		
189
		String mediaUrlString = mediaUrl.toString();
190

  
188 191
		//tiff
189
		String urlTif = urlPath + "tif/" + nameFact + "." + suffixTif;
190
		String fileTif = strFolderPath + "tif" + sep + nameFact + "." + suffixTif;;
191
		//if (CdmUtils.urlExists(urlTif, true)){
192
		File file = new File(fileTif);
193
		if (file.exists()){ 
194
			imageMetaData.readFrom(file);
195
			ImageFile tifImage = ImageFile.NewInstance(urlTif, size, imageMetaData);
196
			MediaRepresentation tifRepresentation = MediaRepresentation.NewInstance(mimeTypeTif, suffixTif);
197
			tifRepresentation.addRepresentationPart(tifImage);
198
			media.addRepresentation(tifRepresentation);
192
		String urlStringTif = mediaUrlString + "tif/" + nameFact + "." + suffixTif;
193
		File file = new File(mediaPath, "tif" + sep + nameFact + "." + suffixTif);
194
		if (file.exists()){
195
			media.addRepresentation(makeImageRepresentation(urlStringTif, size, file, mimeTypeTif, suffixTif));
199 196
		}
200
		//jpeg
197
		// jpg
201 198
		boolean fileExists = true;
202 199
		int jpgCount = 0;
203
		while (fileExists){
200
		while(fileExists){
204 201
			jpgCount++;
205
			String urlJpeg = urlPath + "jpeg/" + nameFact + "_p" + jpgCount + "." + suffixJpg;
206
			String fileJpeg = strFolderPath + "jpeg" + sep + nameFact + "_p" + jpgCount + "." + suffixJpg;
207
			file = new File(fileJpeg);
202
			String urlStringJpeg = mediaUrlString + "jpeg/" + nameFact + "_p" + jpgCount + "." + suffixJpg;
203
			file = new File(mediaPath, "jpeg" + sep + nameFact + "_p" + jpgCount + "." + suffixJpg);
208 204
			if (file.exists()){ 
209
				imageMetaData.readFrom(file);
210
				//if (CdmUtils.urlExists(urlJpeg, true)){
211
				ImageFile jpgImage = ImageFile.NewInstance(urlJpeg, size, imageMetaData);
212
				MediaRepresentation jpgRepresentation = MediaRepresentation.NewInstance(mimeTypeJpeg, suffixJpg);
213
				jpgRepresentation.addRepresentationPart(jpgImage);
214
				media.addRepresentation(jpgRepresentation);
205
				media.addRepresentation(makeImageRepresentation(urlStringJpeg, size, file, mimeTypeJpeg, suffixJpg));
215 206
			}else{
216 207
				fileExists = false;
217 208
			}
218 209
		}
219 210
		//png
220
		String urlPng = urlPath + "png/" + nameFact + "." + suffixPng;
221
		String filePng = strFolderPath + "png" + sep + nameFact + "." + suffixPng;
222
		file = new File(filePng);
211
		String urlStringPng = mediaUrlString + "png/" + nameFact + "." + suffixPng;
212
		file = new File(mediaPath, "png" + sep + nameFact + "." + suffixPng);
223 213
		if (file.exists()){ 
224
		//if (CdmUtils.urlExists(urlPng, true)){
225
			imageMetaData.readFrom(file);
226
			ImageFile pngImage = ImageFile.NewInstance(urlPng, size, imageMetaData);
227
			MediaRepresentation pngRepresentation = MediaRepresentation.NewInstance(mimeTypePng, suffixPng);
228
			pngRepresentation.addRepresentationPart(pngImage);
229
			media.addRepresentation(pngRepresentation);
214
			media.addRepresentation(makeImageRepresentation(urlStringPng, size, file, mimeTypePng, suffixPng));
230 215
		}else{
231 216
			fileExists = true;
232 217
			int pngCount = 0;
233 218
			while (fileExists){
234 219
				pngCount++;
235
				urlPng = urlPath + "png/" + nameFact + "00" + pngCount + "." + suffixPng;
236
				filePng = strFolderPath + "png" + sep + nameFact + "00" + pngCount + "." + suffixPng;
237
				file = new File(filePng);
220
				urlStringPng = mediaUrlString + "png/" + nameFact + "00" + pngCount + "." + suffixPng;
221
				file = new File(mediaPath, "png" + sep + nameFact + "00" + pngCount + "." + suffixPng);
238 222
				if (file.exists()){ 
239
				//if (CdmUtils.urlExists(urlPng, true)){
240
					handleFile(media, urlPng, file, imageMetaData, size, mimeTypePng, suffixPng);
241
//					imageMetaData.readFrom(file);
242
//					ImageFile pngImage = ImageFile.NewInstance(urlPng, size, imageMetaData);
243
//					MediaRepresentation pngRepresentation = MediaRepresentation.NewInstance(mimeTypePng, suffixPng);
244
//					pngRepresentation.addRepresentationPart(pngImage);
245
//					media.addRepresentation(pngRepresentation);
223
					media.addRepresentation(makeImageRepresentation(urlStringPng, size, file, mimeTypePng, suffixPng));
246 224
				}else{
247 225
					fileExists = false;
248 226
				}
249 227
			}
250 228
		} //end png
229
		
251 230
		return media;
252 231
	}
253 232
	
254
	private boolean handleFile(Media media, String url, File file,ImageMetaData imageMetaData, Integer size, String mimeType, String suffix){
255
		//if (CdmUtils.urlExists(urlPng, true)){
233
	
234
	private MediaRepresentation makeImageRepresentation(String imageUri, Integer size, File file, String mimeType, String suffix){
235
		ImageMetaData imageMetaData = new ImageMetaData();
256 236
		imageMetaData.readFrom(file);
257
		ImageFile pngImage = ImageFile.NewInstance(url, size, imageMetaData);
258
		MediaRepresentation pngRepresentation = MediaRepresentation.NewInstance(mimeType, suffix);
259
		pngRepresentation.addRepresentationPart(pngImage);
260
		media.addRepresentation(pngRepresentation);
261
		return true;
237
		ImageFile image = ImageFile.NewInstance(imageUri, size, imageMetaData);
238
		MediaRepresentation representation = MediaRepresentation.NewInstance(mimeType, suffix);
239
		representation.addRepresentationPart(image);
240
		return representation;
262 241
	}
263 242
	
264

  
265 243
	//for testing only
266 244
	public static void main(String[] args) {
267
		String mimeTypeTif = "image/tiff";
268
		String mimeTypeJpeg = "image/jpeg";
269
		String mimeTypePng = "image/png";
270
		String suffixTif = "tif";
271
		String suffixJpg = "jpg";
272
		String suffixPng = "png";
273
		String sep = File.separator;
274
		Media media = Media.NewInstance();
275
		ImageMetaData imageMetaData = new ImageMetaData();
276
		Integer size = null;
277
		String nameFact = "Lactuca_adenophora";
278
	
279
		String urlPath = "http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/";
280
		String strFolderPath = sep + sep + "Bgbm11" + sep  + "Edit-WP6" + sep + "protolog" + sep;
281
		//tiff
282
		String urlTif = urlPath + "tif/" + nameFact + "." + suffixTif;
283
		String fileTif = strFolderPath + "tif" + sep + nameFact + "." + suffixTif;;
284
		//if (CdmUtils.urlExists(urlTif, true)){
285
		if (imageMetaData.readFrom(new File(fileTif))){
286
			
287
		}
288 245
		
289
		//jpeg
290
		boolean fileExists = true;
291
		int jpgCount = 0;
246
		BerlinModelNameFactsIO nf = new BerlinModelNameFactsIO();
292 247
		
293
		while (fileExists){
294
			jpgCount++;
295
			String urlJpeg = urlPath + "jpeg/" + nameFact + "_p" + jpgCount + "." + suffixJpg;
296
			String fileJpeg = strFolderPath + "jpeg" + sep + nameFact + "_p" + jpgCount + "." + suffixJpg;
297
			
298
			if (imageMetaData.readFrom(new File(fileJpeg))){
299
			//if (CdmUtils.urlExists(urlJpeg, true)){
300
				ImageFile jpgImage = ImageFile.NewInstance(urlJpeg, size, imageMetaData);
301
				MediaRepresentation jpgRepresentation = MediaRepresentation.NewInstance(mimeTypeJpeg, suffixJpg);
302
				jpgRepresentation.addRepresentationPart(jpgImage);
303
				media.addRepresentation(jpgRepresentation);
304
			}else{
305
				fileExists = false;
248
		URL url;
249
		try {
250
			url = new URL("http://wp5.e-taxonomy.eu/dataportal/cichorieae/media/protolog/");
251
			File path = new File("/Volumes/protolog/protolog/");
252
			if(path.exists()){
253
				String fact = "Sonchus_eryngiifolius";
254
				// gotta make getMedia public for this to work
255
				nf.getMedia(fact, url, path);
306 256
			}
307
		}
257
		} catch (MalformedURLException e) {
258
			e.printStackTrace();
259
		}		
308 260
	}
309 261

  
310 262
}

Also available in: Unified diff