Search in sources :

Example 6 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class JournalBuilder method parseImage.

private static String parseImage(String text) throws IOException {
    String prefix = "<!-- start_image:";
    String postfix = ":end_image-->";
    while (text.indexOf(prefix) >= 0) {
        String file = text.substring(text.indexOf(prefix) + prefix.length(), text.indexOf(postfix));
        File f = new File(file);
        if (f.exists()) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Base64Encoder b64 = new Base64Encoder(bos);
            FileInputStream fin = new FileInputStream(f);
            Stream.copy(new BufferedInputStream(fin), b64, true);
            text = StringUtils.replaceString(text, prefix + file + postfix, "data:image/" + getImageExtension(f.getName()) + ";base64," + bos.toString());
        } else {
            text = StringUtils.replaceString(text, prefix + file + postfix, "");
        }
    }
    return parseHtmlImage(text);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder) FileInputStream(java.io.FileInputStream)

Example 7 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class JournalBuilder method parseHtmlImage.

private static String parseHtmlImage(String text) throws IOException {
    String prefix = "<!-- start_html_image:";
    String postfix = ":end_html_image-->";
    while (text.indexOf(prefix) >= 0) {
        String token = text.substring(text.indexOf(prefix) + prefix.length(), text.indexOf(postfix));
        String file = token;
        String[] elements = token.split(";");
        if (elements.length > 3) {
            continue;
        }
        if (elements.length == 2) {
            text = StringUtils.replaceString(text, prefix + token + postfix, "");
            continue;
        }
        if (elements.length == 3) {
            file = elements[2];
        }
        File f = new File(file);
        if (f.exists()) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Base64Encoder b64 = new Base64Encoder(bos);
            FileInputStream fin = new FileInputStream(f);
            Stream.copy(new BufferedInputStream(fin), b64, true);
            text = StringUtils.replaceString(text, prefix + token + postfix, "<img src=\"data:image/" + getImageExtension(f.getName()) + ";base64," + bos.toString() + "\" " + (elements.length == 3 ? "width=\"" + elements[0] + "\" height=\"" + elements[1] + "\" " : "") + "/>");
        } else if (file.length() > 128) {
            // assume this is bas64 encoded content
            text = StringUtils.replaceString(text, prefix + token + postfix, "<img src=\"data:image/jpeg;base64," + file + "\" " + (elements.length == 3 ? "width=\"" + elements[0] + "\" height=\"" + elements[1] + "\" " : "") + "/>");
        } else {
            text = StringUtils.replaceString(text, prefix + token + postfix, "");
        }
    }
    return text;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder) FileInputStream(java.io.FileInputStream)

Example 8 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class XMLSerializer method includeImage.

private static String includeImage(ICaller caller) throws IOException {
    if (ImageHandler.getInstance().hasImage(caller) && !caller.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
        InputStream in = ImageHandler.getInstance().getImageStream(caller);
        if (in != null) {
            StringBuffer c = new StringBuffer();
            c.append("<cimagecontent value=\"");
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Base64Encoder b64 = new Base64Encoder(bos);
            Stream.copy(new BufferedInputStream(in), b64, true);
            c.append(bos.toString());
            c.append("\" />");
            return c.toString();
        }
    } else if (caller.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
        String file = caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
        File f = new File(PathResolver.getInstance().resolve(file));
        if (!f.exists())
            return "";
        StringBuffer c = new StringBuffer();
        c.append("<cimagecontent value=\"");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Base64Encoder b64 = new Base64Encoder(bos);
        FileInputStream fin = new FileInputStream(f);
        Stream.copy(new BufferedInputStream(fin), b64, true);
        c.append(bos.toString());
        c.append("\" />");
        return c.toString();
    }
    return "";
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder) FileInputStream(java.io.FileInputStream)

Example 9 with Base64Encoder

use of de.janrufmonitor.util.io.Base64Encoder in project janrufmonitor by tbrandt77.

the class VcfFileCallerExporter method doExport.

public boolean doExport() {
    File db = new File(m_filename);
    try {
        StringBuffer vcf = new StringBuffer();
        Formatter f = Formatter.getInstance(PIMRuntime.getInstance());
        String number_pattern = "+%intareacode% (%areacode%) %callnumber%";
        ICaller c = null;
        for (int i = 0, j = this.m_callerList.size(); i < j; i++) {
            c = this.m_callerList.get(i);
            vcf.append("BEGIN:VCARD");
            vcf.append(CRLF);
            vcf.append("VERSION:3.0");
            vcf.append(CRLF);
            vcf.append("N;CHARSET=ISO-8859-1:");
            vcf.append(c.getName().getLastname());
            vcf.append(";");
            vcf.append(c.getName().getFirstname());
            vcf.append(";;");
            vcf.append(CRLF);
            vcf.append("FN;CHARSET=ISO-8859-1:");
            vcf.append(c.getName().getFirstname());
            vcf.append(" ");
            vcf.append(c.getName().getLastname());
            vcf.append(CRLF);
            if (c.getName().getAdditional().trim().length() > 0)
                vcf.append("ORG;CHARSET=ISO-8859-1:");
            vcf.append(c.getName().getAdditional());
            vcf.append(CRLF);
            vcf.append("SORT-STRING:");
            vcf.append(c.getName().getLastname());
            vcf.append(CRLF);
            vcf.append("CLASS:PRIVATE");
            vcf.append(CRLF);
            if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_EMAIL)) {
                vcf.append("EMAIL;TYPE=home:");
                vcf.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_EMAIL).getValue());
                vcf.append(CRLF);
            }
            vcf.append("ADR;TYPE=home;CHARSET=ISO-8859-1:;;");
            vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_STREET));
            vcf.append(" ");
            vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_STREET_NO));
            vcf.append(";");
            vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_CITY));
            vcf.append(";;");
            vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_POSTAL_CODE));
            vcf.append(";");
            vcf.append(this.getAttribute(c, IJAMConst.ATTRIBUTE_NAME_COUNTRY));
            vcf.append(CRLF);
            if (c instanceof IMultiPhoneCaller) {
                List pns = ((IMultiPhoneCaller) c).getPhonenumbers();
                IPhonenumber p = null;
                for (int k = 0, l = pns.size(); k < l; k++) {
                    p = (IPhonenumber) pns.get(k);
                    if (p.isClired())
                        continue;
                    String numbertype = this.getPhoneType(p, c);
                    if (PhonenumberAnalyzer.getInstance(PIMRuntime.getInstance()).isInternal(p)) {
                        if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE)) {
                            vcf.append("TEL;TYPE=home:");
                            vcf.append(p.getCallNumber());
                            vcf.append(CRLF);
                        }
                        if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                            vcf.append("TEL;TYPE=fax,home:");
                            vcf.append(p.getCallNumber());
                            vcf.append(CRLF);
                        }
                        if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                            vcf.append("TEL;TYPE=cell:");
                            vcf.append(p.getCallNumber());
                            vcf.append(CRLF);
                        }
                    } else {
                        if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE)) {
                            vcf.append("TEL;TYPE=home:");
                            vcf.append(f.parse(number_pattern, p));
                            vcf.append(CRLF);
                        }
                        if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                            vcf.append("TEL;TYPE=fax,home:");
                            vcf.append(f.parse(number_pattern, p));
                            vcf.append(CRLF);
                        }
                        if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                            vcf.append("TEL;TYPE=cell:");
                            vcf.append(f.parse(number_pattern, p));
                            vcf.append(CRLF);
                        }
                    }
                }
            } else {
                String numbertype = this.getPhoneType(c.getPhoneNumber(), c);
                if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_LANDLINE_TYPE)) {
                    vcf.append("TEL;TYPE=home:");
                    vcf.append(f.parse(number_pattern, c.getPhoneNumber()));
                    vcf.append(CRLF);
                }
                if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                    vcf.append("TEL;TYPE=fax,home:");
                    vcf.append(f.parse(number_pattern, c.getPhoneNumber()));
                    vcf.append(CRLF);
                }
                if (numbertype.equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                    vcf.append("TEL;TYPE=cell:");
                    vcf.append(f.parse(number_pattern, c.getPhoneNumber()));
                    vcf.append(CRLF);
                }
            }
            if (ImageHandler.getInstance().hasImage(c)) {
                vcf.append("PHOTO;ENCODING=b;TYPE=JPEG:");
                InputStream fim = ImageHandler.getInstance().getImageStream(c);
                if (c != null) {
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    // finalize lines with '\n ' instead of '\n'
                    Base64Encoder b64 = new Base64Encoder(encodedOut, " ".getBytes());
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    String imagedata = new String(encodedOut.toByteArray());
                    int size = imagedata.length();
                    vcf.append(imagedata.substring(0, 43));
                    int z = 43;
                    while (z < imagedata.length()) {
                        vcf.append(imagedata.substring(z, Math.min((z + 69), size)));
                        z += 69;
                    }
                    vcf.append(CRLF);
                }
            } else if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
                // add embedded photo as base 64 encoded object
                String file = c.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue();
                File img = new File(PathResolver.getInstance().resolve(file));
                if (img.exists()) {
                    vcf.append("PHOTO;ENCODING=b;TYPE=JPEG:");
                    FileInputStream fim = new FileInputStream(img);
                    ByteArrayOutputStream encodedOut = new ByteArrayOutputStream();
                    // finalize lines with '\n ' instead of '\n'
                    Base64Encoder b64 = new Base64Encoder(encodedOut, " ".getBytes());
                    Stream.copy(fim, b64);
                    b64.flush();
                    b64.close();
                    String imagedata = new String(encodedOut.toByteArray());
                    int size = imagedata.length();
                    vcf.append(imagedata.substring(0, 43));
                    int z = 43;
                    while (z < imagedata.length()) {
                        vcf.append(imagedata.substring(z, Math.min((z + 69), size)));
                        z += 69;
                    }
                    vcf.append(CRLF);
                }
            }
            if (c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_GEO_LAT) && c.getAttributes().contains(IJAMConst.ATTRIBUTE_NAME_GEO_LNG)) {
                // add geo tagging
                vcf.append("GEO:");
                vcf.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LAT).getValue());
                vcf.append(";");
                vcf.append(c.getAttribute(IJAMConst.ATTRIBUTE_NAME_GEO_LNG).getValue());
                vcf.append(CRLF);
            }
            vcf.append("PRODID:-//jAnrufmonitor//www.janrufmonitor.de//Version 5.0");
            vcf.append(CRLF);
            vcf.append("UID:JAM-UID-");
            vcf.append(c.getUUID());
            vcf.append(CRLF);
            vcf.append("REV:");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
            vcf.append(sdf.format(new Date()));
            vcf.append(CRLF);
            vcf.append("END:VCARD");
            vcf.append(CRLF);
            vcf.append(CRLF);
        }
        FileOutputStream fos = new FileOutputStream(db);
        ByteArrayInputStream bin = new ByteArrayInputStream(vcf.toString().getBytes());
        Stream.copy(bin, fos, true);
    } catch (FileNotFoundException ex) {
        this.m_logger.severe("File not found: " + m_filename);
        return false;
    } catch (IOException ex) {
        this.m_logger.severe("IOException on file " + m_filename);
        return false;
    }
    return true;
}
Also used : Formatter(de.janrufmonitor.util.formatter.Formatter) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Base64Encoder(de.janrufmonitor.util.io.Base64Encoder) FileInputStream(java.io.FileInputStream) Date(java.util.Date) ICaller(de.janrufmonitor.framework.ICaller) ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) ICallerList(de.janrufmonitor.framework.ICallerList) List(java.util.List) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

Base64Encoder (de.janrufmonitor.util.io.Base64Encoder)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 FileInputStream (java.io.FileInputStream)6 BufferedInputStream (java.io.BufferedInputStream)5 File (java.io.File)5 InputStream (java.io.InputStream)4 ICaller (de.janrufmonitor.framework.ICaller)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 FileNotFoundException (java.io.FileNotFoundException)2 IAttribute (de.janrufmonitor.framework.IAttribute)1 ICall (de.janrufmonitor.framework.ICall)1 ICallerList (de.janrufmonitor.framework.ICallerList)1 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)1 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)1 Formatter (de.janrufmonitor.util.formatter.Formatter)1 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1