1 |
| package photospace.meta.rdf; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import java.util.*; |
5 |
| import org.apache.commons.lang.*; |
6 |
| import org.apache.commons.logging.*; |
7 |
| import org.w3c.tools.jpeg.*; |
8 |
| import com.hp.hpl.jena.rdf.model.*; |
9 |
| import com.hp.hpl.jena.vocabulary.*; |
10 |
| import junit.framework.*; |
11 |
| |
12 |
| public class JpegRdfTest |
13 |
| extends TestCase |
14 |
| { |
15 |
| private static final Log log = LogFactory.getLog(JpegRdfTest.class); |
16 |
| |
17 |
1
| public void testPhotospaceModel() throws Exception
|
18 |
| { |
19 |
1
| Model model = getPhotoModel();
|
20 |
1
| model.write(System.out);
|
21 |
| } |
22 |
| |
23 |
2
| private Model getPhotoModel()
|
24 |
| { |
25 |
2
| Model model = ModelFactory.createDefaultModel();
|
26 |
2
| Resource asset = model.createResource();
|
27 |
2
| asset.addProperty(DC.title, "test title");
|
28 |
2
| asset.addProperty(DC.description, "test description");
|
29 |
2
| asset.addProperty(DC.type, DCTypes.Image);
|
30 |
2
| asset.addProperty(DC.creator, model.createResource("http://photospace.org/people/alon")
|
31 |
| .addProperty(VCARD.FN, "Alon Salant") |
32 |
| .addProperty(VCARD.EMAIL, "alon@salant.org") |
33 |
| ); |
34 |
2
| asset.addProperty(DC.date, new Date());
|
35 |
2
| return model;
|
36 |
| } |
37 |
| |
38 |
1
| public void testReadWriteRdf() throws Exception
|
39 |
| { |
40 |
1
| File jpeg = new File(System.getProperty("project.root"), "build/test/exif-rdf.jpg");
|
41 |
| |
42 |
1
| Model model = getPhotoModel();
|
43 |
1
| ByteArrayOutputStream rdf = new ByteArrayOutputStream();
|
44 |
1
| model.write(rdf);
|
45 |
| |
46 |
1
| log.debug("RDF to write to jpeg:\n" + rdf);
|
47 |
| |
48 |
1
| ByteArrayOutputStream jpegOS = new ByteArrayOutputStream();
|
49 |
1
| JpegCommentWriter jcw = new JpegCommentWriter(jpegOS, new FileInputStream(jpeg));
|
50 |
1
| jcw.write(rdf.toString());
|
51 |
1
| jcw.close();
|
52 |
| |
53 |
1
| FileOutputStream fos = new FileOutputStream(jpeg);
|
54 |
1
| fos.write(jpegOS.toByteArray());
|
55 |
1
| fos.close();
|
56 |
| |
57 |
1
| FileInputStream jpegIn = new FileInputStream(jpeg);
|
58 |
1
| JpegHeaders jh = new JpegHeaders(jpegIn, new org.w3c.tools.jpeg.Exif());
|
59 |
1
| String comments = StringUtils.join(jh.getComments(), "");
|
60 |
1
| jpegIn.close();
|
61 |
| |
62 |
1
| assertNotNull(comments);
|
63 |
1
| log.info("EXIF comments from jpeg:\n" + comments);
|
64 |
| |
65 |
1
| Model fromJpeg = ModelFactory.createDefaultModel();
|
66 |
1
| fromJpeg.read(new StringReader(comments), "");
|
67 |
1
| fromJpeg.write(System.out);
|
68 |
| |
69 |
1
| log.info("RDF from EXIF comments:\n" + comments);
|
70 |
| } |
71 |
| |
72 |
0
| public void dump() throws Exception
|
73 |
| { |
74 |
0
| File jpeg = new File(System.getProperty("project.root"), "build/test/exif-rdf.jpg");
|
75 |
0
| photospace.meta.Reader reader = new photospace.meta.Reader(jpeg);
|
76 |
0
| log.info(reader.getDump());
|
77 |
| |
78 |
| } |
79 |
| |
80 |
| } |