Extract Lotus Notes database icon
Extract Lotus Notes database icon
- Create DatabaseIcon class as following
- Call DatabaseIcon.extract() method as following
Call DatabaseIcon.extract() method
DatabaseIcon class
1 | String idfile = ""; |
2 | String password = ""; |
3 | String dbfile = ""; |
4 | NotesThread.sinitThread(); |
5 | Session s = NotesFactory.createSessionWithFullAccess(); |
6 | s.createRegistration().switchToID(idfile, password); |
7 | Database d = s.getDatabase("", dbfile); |
8 | |
9 | String filename = DatabaseIcon.extract(d); |
10 | |
11 | logger.info(filename); |
String idfile = ""; String password = ""; String dbfile = ""; NotesThread.sinitThread(); Session s = NotesFactory.createSessionWithFullAccess(); s.createRegistration().switchToID(idfile, password); Database d = s.getDatabase("", dbfile); String filename = DatabaseIcon.extract(d); logger.info(filename);
1 | public static class DatabaseIcon { |
2 | |
3 | public static String extract(Database d) { |
4 | String tag = null; |
5 | try { |
6 | NoteCollection nc = d.createNoteCollection(false); |
7 | nc.setSelectIcon(true); |
8 | nc.buildCollection(); |
9 | String noteId = nc.getFirstNoteID(); |
10 | int counter = 0; |
11 | while (noteId != null) { |
12 | counter++; |
13 | try { |
14 | Document doc = d.getDocumentByID(noteId); |
15 | DxlExporter dxl = d.getParent().createDxlExporter(); |
16 | String xml = dxl.exportDxl(doc); |
17 | xml = xml.substring(xml.indexOf("<note ")); |
18 | org.jsoup.nodes.Document jdoc = Jsoup.parse(xml); |
19 | Element ele = jdoc.select("rawitemdata").first(); |
20 | String raw = ele.text().trim(); |
21 | String temp = System.getProperty("java.io.tmpdir") + UUID.randomUUID().toString() + "\\"; |
22 | File file = new File(temp); |
23 | file.mkdir(); |
24 | String filename = temp + UUID.randomUUID().toString().replaceAll("-", "") + ".png"; |
25 | byte[] buffer = decode(raw.getBytes()); |
26 | |
27 | Image image = byte2image(buffer); |
28 | ImageLoader loader = new ImageLoader(); |
29 | loader.data = new ImageData[] {image.getImageData()}; |
30 | loader.save(filename, SWT.IMAGE_PNG); |
31 | |
32 | tag = filename; |
33 | } catch (Exception e) { |
34 | logger.error("", e); |
35 | } |
36 | |
37 | if (counter >= nc.getCount()) { |
38 | noteId = null; |
39 | } else { |
40 | noteId = nc.getNextNoteID(noteId); |
41 | } |
42 | } |
43 | } catch (Exception e) { |
44 | logger.error("", e); |
45 | } |
46 | return tag; |
47 | } |
48 | |
49 | private static Image byte2image(byte[] src) { |
50 | Image tag = new Image(Display.getDefault(), 32, 32); |
51 | GC gc = new GC(tag); |
52 | org.eclipse.swt.graphics.Color[] colors = new org.eclipse.swt.graphics.Color[16]; |
53 | colors[0] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 0, 0); |
54 | colors[1] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 255, 255); |
55 | colors[2] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 0, 0); |
56 | colors[3] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 255, 0); |
57 | colors[4] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 0, 255); |
58 | colors[5] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 0, 255); |
59 | colors[6] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 255, 0); |
60 | colors[7] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 255, 255); |
61 | colors[8] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 0, 0); |
62 | colors[9] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 128, 0); |
63 | colors[10] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 0, 128); |
64 | colors[11] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 0, 128); |
65 | colors[12] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 128, 0); |
66 | colors[13] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 128, 128); |
67 | colors[14] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 128, 128); |
68 | colors[15] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 192, 192, 192); |
69 | |
70 | gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); |
71 | gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); |
72 | int start = 6 + 128; |
73 | int row = 0; |
74 | int col = 0; |
75 | for (int i = src.length - 16; i >= start; i -= 16) { |
76 | col = 0; |
77 | for (int j = i; j < i + 16; j++) { |
78 | byte b = src[j]; |
79 | String s = String.format("%02X", b); |
80 | byte b1 = Byte.parseByte("0" + s.charAt(0), 16); |
81 | byte b2 = Byte.parseByte("0" + s.charAt(1), 16); |
82 | gc.setForeground(colors[b1]); |
83 | gc.drawPoint(col, row); |
84 | gc.setForeground(colors[b2]); |
85 | gc.drawPoint(col + 1, row); |
86 | col += 2; |
87 | } |
88 | row++; |
89 | } |
90 | return tag; |
91 | } |
92 | |
93 | private static byte[] decode(byte[] b) throws Exception { |
94 | ByteArrayInputStream bais = new ByteArrayInputStream(b); |
95 | InputStream b64is = MimeUtility.decode(bais, "base64"); |
96 | byte[] tmp = new byte[b.length]; |
97 | int n = b64is.read(tmp); |
98 | byte[] res = new byte[n]; |
99 | System.arraycopy(tmp, 0, res, 0, n); |
100 | return res; |
101 | } |
102 | |
103 | } |
public static class DatabaseIcon { public static String extract(Database d) { String tag = null; try { NoteCollection nc = d.createNoteCollection(false); nc.setSelectIcon(true); nc.buildCollection(); String noteId = nc.getFirstNoteID(); int counter = 0; while (noteId != null) { counter++; try { Document doc = d.getDocumentByID(noteId); DxlExporter dxl = d.getParent().createDxlExporter(); String xml = dxl.exportDxl(doc); xml = xml.substring(xml.indexOf("<note ")); org.jsoup.nodes.Document jdoc = Jsoup.parse(xml); Element ele = jdoc.select("rawitemdata").first(); String raw = ele.text().trim(); String temp = System.getProperty("java.io.tmpdir") + UUID.randomUUID().toString() + "\\"; File file = new File(temp); file.mkdir(); String filename = temp + UUID.randomUUID().toString().replaceAll("-", "") + ".png"; byte[] buffer = decode(raw.getBytes()); Image image = byte2image(buffer); ImageLoader loader = new ImageLoader(); loader.data = new ImageData[] {image.getImageData()}; loader.save(filename, SWT.IMAGE_PNG); tag = filename; } catch (Exception e) { logger.error("", e); } if (counter >= nc.getCount()) { noteId = null; } else { noteId = nc.getNextNoteID(noteId); } } } catch (Exception e) { logger.error("", e); } return tag; } private static Image byte2image(byte[] src) { Image tag = new Image(Display.getDefault(), 32, 32); GC gc = new GC(tag); org.eclipse.swt.graphics.Color[] colors = new org.eclipse.swt.graphics.Color[16]; colors[0] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 0, 0); colors[1] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 255, 255); colors[2] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 0, 0); colors[3] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 255, 0); colors[4] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 0, 255); colors[5] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 0, 255); colors[6] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 255, 255, 0); colors[7] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 255, 255); colors[8] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 0, 0); colors[9] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 128, 0); colors[10] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 0, 128); colors[11] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 0, 128); colors[12] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 128, 0); colors[13] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 0, 128, 128); colors[14] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 128, 128, 128); colors[15] = new org.eclipse.swt.graphics.Color(Display.getDefault(), 192, 192, 192); gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK)); int start = 6 + 128; int row = 0; int col = 0; for (int i = src.length - 16; i >= start; i -= 16) { col = 0; for (int j = i; j < i + 16; j++) { byte b = src[j]; String s = String.format("%02X", b); byte b1 = Byte.parseByte("0" + s.charAt(0), 16); byte b2 = Byte.parseByte("0" + s.charAt(1), 16); gc.setForeground(colors[b1]); gc.drawPoint(col, row); gc.setForeground(colors[b2]); gc.drawPoint(col + 1, row); col += 2; } row++; } return tag; } private static byte[] decode(byte[] b) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(b); InputStream b64is = MimeUtility.decode(bais, "base64"); byte[] tmp = new byte[b.length]; int n = b64is.read(tmp); byte[] res = new byte[n]; System.arraycopy(tmp, 0, res, 0, n); return res; } }
No comments:
Post a Comment