KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > extensions > pdfbox > OooToPdf


1 package org.lucane.extensions.pdfbox;
2
3 import java.io.IOException JavaDoc;
4 import java.net.URL JavaDoc;
5
6 import com.sun.star.beans.PropertyValue;
7 import com.sun.star.bridge.XUnoUrlResolver;
8 import com.sun.star.comp.helper.Bootstrap;
9 import com.sun.star.frame.XComponentLoader;
10 import com.sun.star.frame.XStorable;
11 import com.sun.star.lang.XComponent;
12 import com.sun.star.lang.XMultiComponentFactory;
13 import com.sun.star.uno.UnoRuntime;
14 import com.sun.star.uno.XComponentContext;
15
16 /**
17  * Convert documents to pdf using openoffice
18  */

19 public class OooToPdf
20 {
21     //-- PDF export filters
22
private static final String JavaDoc[] PDF_FILTERS = {
23         "writer_pdf_Export",
24         "writer_web_pdf_Export",
25         "writer_globaldocument_pdf_Export",
26         "calc_pdf_Export",
27         "impress_pdf_Export",
28         "draw_pdf_Export",
29         "math_pdf_Export"};
30     
31     //-- instance management
32
//singleton to avoid fetching OOo's context at each time
33
private static OooToPdf instance = null;
34     private static OooProcess process = null;
35     public static void initInstance(String JavaDoc oooCommand)
36     throws Exception JavaDoc
37     {
38         if(OooToPdf.instance == null)
39         {
40             //start OpenOffice.org
41
if(OooToPdf.process == null)
42             {
43                 OooToPdf.process = new OooProcess(oooCommand);
44                 new Thread JavaDoc(OooToPdf.process).start();
45             }
46         }
47     }
48     
49     public synchronized static OooToPdf getInstance()
50     throws Exception JavaDoc
51     {
52         //check connection : is it still up ?
53
if(OooToPdf.instance != null)
54         {
55             try {
56                 XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(
57                         XComponentLoader.class,
58                         OooToPdf.instance.desktop);
59             } catch(Exception JavaDoc e) {
60                 OooToPdf.instance = null;
61             }
62         }
63         
64         
65         if(OooToPdf.instance == null)
66         {
67             //create a usable instance
68
OooToPdf.instance = new OooToPdf();
69             OooToPdf.instance.initComponentContext();
70             OooToPdf.instance.initDesktop();
71         }
72         
73         return OooToPdf.instance;
74     }
75     
76     public static void shutdown()
77     {
78             OooToPdf.process.shutdown();
79     }
80     
81     //-- attributes
82
private XComponentContext context;
83     private Object JavaDoc desktop;
84     
85     //-- init methods
86

87     /**
88      * Fetch context
89      */

90     private void initComponentContext()
91     throws Exception JavaDoc
92     {
93         XComponentContext localContext = Bootstrap.createInitialComponentContext(null);
94         XMultiComponentFactory localServiceManager = localContext.getServiceManager();
95         Object JavaDoc urlResolver = localServiceManager.createInstanceWithContext(
96                 "com.sun.star.bridge.UnoUrlResolver",
97                 localContext);
98         XUnoUrlResolver unoUrlResolver = (XUnoUrlResolver)UnoRuntime.queryInterface(
99                 XUnoUrlResolver.class,
100                 urlResolver);
101         Object JavaDoc initialObject = unoUrlResolver.resolve(
102                 "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext");
103     
104         this.context = (XComponentContext) UnoRuntime.queryInterface(
105                 XComponentContext.class,
106                 initialObject);
107     }
108     
109     /**
110      * Fetch desktop object
111      */

112     private void initDesktop()
113     throws Exception JavaDoc
114     {
115         XMultiComponentFactory serviceManager = context.getServiceManager();
116         this.desktop = serviceManager.createInstanceWithContext(
117                 "com.sun.star.frame.Desktop",
118                 context);
119     }
120     
121     //-- conversion methods
122

123     public void convertFile(URL JavaDoc from, URL JavaDoc to)
124     throws Exception JavaDoc
125     {
126         XComponent document = loadDocument(from);
127         this.storeDocument(document, to);
128         document.dispose();
129     }
130     
131     private XComponent loadDocument(URL JavaDoc from)
132     throws Exception JavaDoc
133     {
134         //load document
135
XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(
136                 XComponentLoader.class,
137                 desktop);
138         XComponent document = (XComponent)componentLoader.loadComponentFromURL(
139                 from.toExternalForm(),
140                 "_blank",
141                 0,
142                 new PropertyValue[] {}
143         );
144         
145         if(document == null)
146             throw new IOException JavaDoc("Unable to load : " + from);
147         
148         return document;
149     }
150     
151     private void storeDocument(XComponent document, URL JavaDoc to)
152     {
153         //prepare for save
154
XStorable storable = (XStorable)UnoRuntime.queryInterface(
155             XStorable.class,
156             document);
157         
158         //apply filters
159
for(int i=0;i<PDF_FILTERS.length;i++)
160             storeDocumentWithFilter(storable, PDF_FILTERS[i], to);
161     }
162     
163     private void storeDocumentWithFilter(XStorable storable, String JavaDoc filter, URL JavaDoc to)
164     {
165         PropertyValue p = new PropertyValue();
166         p.Name = "FilterName";
167         p.Value = filter;
168         
169         try {
170             storable.storeToURL(to.toExternalForm(), new PropertyValue[]{p});
171         } catch(com.sun.star.io.IOException ioe) {
172             //burk, but OOo throws an exception each time, even if it works !
173
}
174     }
175 }
Popular Tags