1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
package org.libreoffice.example.java_scripts;
//import com.sun.star.frame.XComponentLoader;
import java.io.*;
import com.sun.star.lang.XComponent;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XStorable;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XModel;
import com.sun.star.script.framework.runtime.XScriptContext;
// for debug only
import javax.swing.JOptionPane;
public class OfficeAttachment
{
private StatusWindow status = null;
private XStorable storedDoc = null;
private File htmlFile = null;
private File officeFile = null;
private boolean isHtmlDoc = false;
private boolean isOfficeDoc = false;
private String templocationURL = "";
private String templocationSystem = "";
private String attachmentName = "";
private String statusLine = "";
public OfficeAttachment( XScriptContext xsc, StatusWindow sw, boolean html, boolean office )
{
status = sw;
isHtmlDoc = html;
isOfficeDoc = office;
templocationSystem = templocationURL = System.getProperty( "user.home" );
if( System.getProperty( "os.name" ).indexOf( "Windows" ) != -1 )
{
while( templocationURL.indexOf( "\\" ) != -1 )
{
int sepPos = templocationURL.indexOf( "\\" );
String firstPart = templocationURL.substring( 0, sepPos );
String lastPart = templocationURL.substring( sepPos + 1, templocationURL.length() );
templocationURL = firstPart + "/" + lastPart;
//JOptionPane.showMessageDialog( null, "Temp Location URL is: " + templocationURL + "\nfirstPart is: " + firstPart + "\nlastPart is: " + lastPart );
}
}
try
{
statusLine = "Querying Office for current document";
status.setStatus( 1, statusLine );
XScriptContext scriptcontext = xsc;
XModel xmodel = scriptcontext.getDocument();
storedDoc = (XStorable) UnoRuntime.queryInterface(XStorable.class, xmodel);
// find document name from storedDoc
attachmentName = storedDoc.getLocation();
}
catch( Exception e )
{
//UNO error
status.setStatus( 1, "Error: " + statusLine );
}
if( attachmentName.equalsIgnoreCase( "" ) )
{
attachmentName = "Attachment";
}
else
{
//int lastSep = attachmentName.lastIndexOf( System.getProperty( "file.separator" ) );
int lastSep = attachmentName.lastIndexOf( "/" );
attachmentName = attachmentName.substring( lastSep + 1, attachmentName.length() );
int dot = attachmentName.indexOf( "." );
attachmentName = attachmentName.substring( 0, dot );
}
}
public boolean createTempDocs()
{
String filenameURL = "file:///" + templocationURL + "/" + attachmentName;
//String filenameSystem = templocationSystem + System.getProperty( "file.separator" ) + attachmentName;
//JOptionPane.showMessageDialog( null, "Filename URL " + filenameURL );
try
{
if( isHtmlDoc )
{
//JOptionPane.showMessageDialog( null, "Saving doc in HTML format" );
statusLine = "Saving doc in HTML format";
status.setStatus( 4, statusLine );
//System.out.print( "Saving attachment as " + filename + ".html..." );
PropertyValue[] propertyvalue_html = new PropertyValue[2];
propertyvalue_html[0] = new PropertyValue();
propertyvalue_html[0].Name = new String("Overwrite");
propertyvalue_html[0].Value = new Boolean(true);
propertyvalue_html[1] = new PropertyValue();
propertyvalue_html[1].Name = ("FilterName");
// propertyvalue_html[1].Value = new String("scalc: HTML (StarCalc)");
propertyvalue_html[1].Value = new String("swriter: HTML (StarWriter)");
storedDoc.storeAsURL( filenameURL + ".html", propertyvalue_html);
File homedir = new File( templocationSystem );
//JOptionPane.showMessageDialog( null, "homedir (Java File): " + homedir.getPath() );
File homefiles[] = homedir.listFiles();
String file = "";
for(int i=0; i < homefiles.length; i++ )
{
if( homefiles[i].getName().equals( attachmentName + ".html" ) )
{
//htmlFile = new File( homefiles[i].getAbsolutePath() );
//JOptionPane.showMessageDialog( null, "Found HTML" );
file = homefiles[i].getAbsolutePath();
}
}
htmlFile = new File( file );
//htmlFile = new File( filename + ".html" );
//htmlFile = new File( storedDoc.getLocation() );
}
if( isOfficeDoc )
{
//JOptionPane.showMessageDialog( null, "Saving doc in .sxw format" );
statusLine = "Saving doc in .sxw format";
status.setStatus( 4, statusLine );
//System.out.print( "Saving attachment as " + filename + ".sxw..." );
PropertyValue[] propertyvalue_sxw = new PropertyValue[2];
propertyvalue_sxw[0] = new PropertyValue();
propertyvalue_sxw[0].Name = new String("Overwrite");
propertyvalue_sxw[0].Value = new Boolean(true);
propertyvalue_sxw[1] = new PropertyValue();
propertyvalue_sxw[1].Name = new String("Overwrite");
propertyvalue_sxw[1].Value = new Boolean(true);
storedDoc.storeAsURL( filenameURL + ".sxw", propertyvalue_sxw);
File homedir = new File( templocationSystem );
//JOptionPane.showMessageDialog( null, "homedir (Java File): " + homedir.getPath() );
File homefiles[] = homedir.listFiles();
String file = "";
for(int i=0; i < homefiles.length; i++ )
{
if( homefiles[i].getName().equals( attachmentName + ".sxw" ) )
{
//officeFile = new File( homefiles[i].getAbsolutePath() );
//JOptionPane.showMessageDialog( null, "Found .sxw" );
file = homefiles[i].getAbsolutePath();
}
}
officeFile = new File( file );
//officeFile = new File( filename + ".sxw" );
//officeFile = new File (storedDoc.getLocation() );
}
//status.setStatus( 10, "Attachments successfully created" );
}
catch( SecurityException se )
{
status.setStatus( 4, "Error: " + statusLine );
System.out.println( "Security error while saving temporary Document(s). Check file permissions in home directory." );
se.printStackTrace();
htmlFile = null;
officeFile = null;
return false;
}
catch( Exception e )
{
status.setStatus( 4, "Error: " + statusLine );
System.out.println( "Error saving temporary Document(s)" );
e.printStackTrace();
htmlFile = null;
officeFile = null;
return false;
}
return true;
}
public boolean removeTempDocs()
{
/*
if( !htmlFile.exists() && !officeFile.exists() )
{
System.out.println("Error: Document(s) have not been saved." );
}
*/
statusLine = "Removing temp docs";
status.setStatus( 13, statusLine );
try
{
if( isOfficeDoc && isHtmlDoc )
{
//System.out.println( "Removing: " + htmlFile.getPath() + " " + officeFile.getPath() );
//System.out.println( "htmlfile " + htmlFile.exists() + " officeFile " + officeFile.exists() );
//JOptionPane.showMessageDialog( null, "Removing: " + htmlFile.getPath() + " " + officeFile.getPath() );
//JOptionPane.showMessageDialog( null, "htmlfile " + htmlFile.exists() + " officeFile " + officeFile.exists() );
htmlFile.delete();
officeFile.delete();
//JOptionPane.showMessageDialog( null, "htmlfile " + htmlFile.exists() + " officeFile " + officeFile.exists() );
}
else
{
if( isOfficeDoc )
{
//System.out.println( "Removing: " + officeFile.getPath() );
officeFile.delete();
}
else
{
//System.out.println( "Removing: " + htmlFile.getPath() );
htmlFile.delete();
}
}
}
catch( SecurityException se )
{
status.setStatus( 13, "Error: " + statusLine );
System.out.println( "Security Error while deleting temporary Document(s). Check file permissions in home directory." );
se.printStackTrace();
return false;
}
return true;
}
public void cleanUpOnError()
{
try
{
if( isOfficeDoc && isHtmlDoc )
{
htmlFile.delete();
officeFile.delete();
}
else
{
if( isOfficeDoc )
{
officeFile.delete();
}
else
{
htmlFile.delete();
}
}
}
catch( SecurityException se )
{
System.out.println( "Security Error while deleting temporary Document(s). Check file permissions in home directory." );
se.printStackTrace();
}
}
public File[] getAttachments()
{
/*
if( htmlFile == null && officeFile == null )
{
System.out.println( "Error: Document(s) have not been saved." );
return null;
}
*/
//(officeDoc) ? (number = 2) : (number = 1);
statusLine = "Retrieving temp docs";
status.setStatus( 8, statusLine );
File attachments[] = null;
if( isOfficeDoc && isHtmlDoc )
{
attachments = new File[2];
attachments[0] = htmlFile;
attachments[1] = officeFile;
}
else
{
if( isOfficeDoc )
{
attachments = new File[1];
attachments[0] = officeFile;
}
else
{
attachments = new File[1];
attachments[0] = htmlFile;
}
}
return attachments;
}
public boolean isHtmlAttachment()
{
return isHtmlDoc;
}
public boolean isOfficeAttachment()
{
return isOfficeDoc;
}
}
|