Statistics
| Revision:

gvsig-projects-pool / org.gvsig.pdf / trunk / org.gvsig.pdf / org.gvsig.pdf.lib / org.gvsig.pdf.lib.impl / src / main / java / org / gvsig / pdf / lib / impl / DefaultPDFDocument.java @ 1231

History | View | Annotate | Download (4.66 KB)

1
package org.gvsig.pdf.lib.impl;
2

    
3
import java.io.ByteArrayInputStream;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.IOException;
8
import java.io.InputStream;
9
import java.net.URL;
10
import org.gvsig.pdf.lib.api.PDFDocument;
11
import org.slf4j.LoggerFactory;
12
import org.apache.commons.codec.binary.Hex;
13
import org.apache.commons.codec.DecoderException;
14
import org.apache.commons.codec.binary.Base64;
15

    
16
/**
17
 *
18
 * @author osc
19
 */
20
public class DefaultPDFDocument implements PDFDocument {
21

    
22
//    private byte[] byteDocument;
23
//    private String strDocument;
24
//    private File fileDocument;
25
    private InputStream pdf;
26
    private static final org.slf4j.Logger LOGGER = LoggerFactory
27
            .getLogger(DefaultPDFDocument.class);
28

    
29
    public DefaultPDFDocument() {
30

    
31
    }
32
    
33
    @Override
34
    public void setDocument(Object document) {
35
        this.pdf = this.load(document);
36
    }
37
    
38
    @Override
39
    public InputStream getInputStream(){
40
        return this.pdf;
41
    }
42
    
43
    
44
    public InputStream load(Object source) {
45
        InputStream pdfInputSteam = null;
46
        if (source == null) {
47
            return null;
48
        } else if (source instanceof File) {
49
            File fileSource = (File) source;
50
            try {
51
                pdfInputSteam = new FileInputStream(fileSource);
52
            } catch (FileNotFoundException ex) {
53
                LOGGER.warn("Not been able to convert file to input stream", ex);
54
                return null;
55
            }
56
        } else if (source instanceof URL) {
57
            URL urlSource = (URL) source;
58
            try {
59
                pdfInputSteam = urlSource.openStream();
60
            } catch (IOException ex) {
61
                LOGGER.warn("Not been able to open URL stream", ex);
62
                return null;
63
            }
64
        } else if (source instanceof String) {
65
                String strSource = (String) source;
66
                File f = new File(strSource);
67
                if (f.exists()) {
68
                    try {
69
                        pdfInputSteam = new FileInputStream(f);
70
                    } catch (FileNotFoundException ex) {
71
                        LOGGER.warn("Not been able to load existing file", ex);
72
                        return null;
73
                    }
74
                } else {
75
                    try {
76
                        URL url = new URL(strSource);
77
                        pdfInputSteam = url.openStream();
78
                        pdfInputSteam.available(); // Force exception if is null
79
                    } catch (Exception ex) {
80
                        try {
81
                            byte[] data = Hex.decodeHex(strSource.toCharArray());
82
                            pdfInputSteam = new ByteArrayInputStream(data);
83
                        } catch (DecoderException ex2) {
84
                            try {
85
                                byte[] data = Base64.decodeBase64(strSource);
86
                                pdfInputSteam = new ByteArrayInputStream(data);
87
                            } catch (Exception ex3) {
88
                                return null;
89
                            }
90
                        }
91
                    }
92
                }
93
        } else if (source instanceof byte[]) {
94
            byte[] byteSource = (byte[]) source;
95
            pdfInputSteam = new ByteArrayInputStream(byteSource);
96
        }
97

    
98
        return pdfInputSteam;
99
    }
100
    @Override
101
    public byte[] toByteArray() {
102
    if (this.pdf!=null) {
103
        try {
104
            byte[] bytePdf = new byte[pdf.available()];
105
            pdf.read(bytePdf);
106
            return bytePdf;
107
        } catch (IOException ex) {
108
            LOGGER.warn("Not been able to convert pdf to byte array", ex);
109
        }
110
    }
111
    return null;
112
    
113
    
114
    }
115

    
116
//    @Override
117
//    public File toFile() {
118
//        if (this.fileDocument != null) {
119
//            return this.fileDocument;
120
//        } else if (this.strDocument != null) {
121
//            return new File(this.strDocument);
122
//        }
123
//
124
//        return null;
125
//    }
126
//
127
//    @Override
128
//    public String toFileString() {
129
//        if (this.strDocument != null) {
130
//            return this.strDocument;
131
//        } else if (this.fileDocument != null) {
132
//            return fileDocument.getAbsolutePath();
133
//        }
134
//        return null;
135
//    }
136
    
137
        private String bytearray_hex(byte[] data) {
138
        StringBuilder builder = new StringBuilder();
139
        for (byte abyte : data) {
140
            int v = abyte & 0xff;
141
            builder.append(String.format("%02x", v));
142
        }
143
        return builder.toString();
144
    }
145
        
146
    @Override
147
    public String toString() {
148
        byte[] data = this.toByteArray();
149
        return bytearray_hex(data);
150
    }
151
}