Wednesday 11 April 2012

Extract Lotus Notes database icon

Extract Lotus Notes database icon
This task use java, jsoup and SWT to extract Lotus Notes database icon
Extract Lotus Notes database icon
  1. Create DatabaseIcon class as following
  2. Call DatabaseIcon.extract() method as following
Call DatabaseIcon.extract() method
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);
    
DatabaseIcon class
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;
    }  
        
}
    

  Protected by Copyscape Online Copyright Protection

No comments:

Post a Comment