Package com.storedobject.office.od
Class Office
java.lang.Object
com.storedobject.office.od.Office
Utility class that provides methods for interacting with a LibreOffice or OpenOffice process
for document creation, manipulation, and conversion. This class uses the UNO API to
interface with the office process for operations such as opening documents, converting
formats, and exporting to PDF or HTML.
This class is implemented as a singleton to manage a single office service instance.
It ensures proper initialization, reuse of resources, and cleanup of the office service.
The singleton instance is lazily initialized through
get().
Thread-safety is guaranteed by synchronized blocks to ensure proper coordination between
threads when accessing or initializing shared resources.
The class provides capabilities to:
- Open and create new ODT (text document) and ODS (spreadsheet) files. - Convert documents to different formats such as PDF and HTML. - Manage lifecycle operations, including initialization and cleanup of office resources. Dependencies: - The LibreOffice or OpenOffice process must be installed and accessible. - The UNO runtime and related interfaces to communicate with office components. Error Handling: - Proper error handling is implemented to ensure exceptions such as initialization failures or IO errors are logged or thrown as needed.
- Author:
- Syam
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidclose(com.sun.star.lang.XComponent store) Closes the givenXComponentinstance, releasing any associated resources.static voidconvertToHTML(InputStream in, OutputStream out) Converts an input document from anInputStreaminto an HTML document and writes the resulting content to the specifiedOutputStream.static voidconvertToPDF(InputStream in, OutputStream out, PDFProperties pdfProperties) Converts an input stream of document data into a PDF and writes the resulting content to the provided output stream.static com.sun.star.lang.XComponentCreates a new OpenDocument Spreadsheet (ODS) document.static com.sun.star.lang.XComponentCreates a new OpenDocument Text (ODT) document.static Officeget()Retrieves the singleton instance of the Office class, initializing it if necessary.static com.sun.star.lang.XComponentopen(InputStream in) Opens anXComponentfrom the providedInputStream.static voidreset()Resets the state of the Office class by performing cleanup and re-initialization.static voidshutdown()Shuts down the template-based reporting service.static voidwrite(com.sun.star.lang.XComponent store, OutputStream out, PDFProperties pdfProperties) Exports the content of anXComponentto a specifiedOutputStreamin PDF format, optionally using providedPDFPropertiesfor configuring export settings.
-
Method Details
-
get
Retrieves the singleton instance of the Office class, initializing it if necessary. This method ensures that the Office instance is properly set up, re-initializing components like the server or the component loader if they are null or inactive.- Returns:
- The singleton instance of the Office class.
-
reset
public static void reset()Resets the state of the Office class by performing cleanup and re-initialization.-
- Invokes the
close()method to release any resources and shut down active components. - Calls theinit()method to reinitialize the Office class, ensuring all necessary parts and services are properly set up. This method is often used to recover from errors or stale state by resetting and starting afresh. -
createODS
Creates a new OpenDocument Spreadsheet (ODS) document. This method initializes and loads a default ODS document using the specified factory settings, ensuring it is hidden during the creation process.- Returns:
- An
XComponentrepresenting the newly created ODS document. - Throws:
Exception- If the creation or loading of the ODS document fails.
-
createODT
Creates a new OpenDocument Text (ODT) document. This method initializes and loads a default ODT document using the specified factory settings, ensuring it is hidden during the creation process.- Returns:
- An
XComponentrepresenting the newly created ODT document. - Throws:
Exception- If the creation or loading of the ODT document fails.
-
open
Opens anXComponentfrom the providedInputStream. This method converts theInputStreaminto a format compatible with the underlying Office framework and loads the component while ensuring it is initialized in a hidden state.- Parameters:
in- TheInputStreamcontaining the data to be opened as anXComponent.- Returns:
- An
XComponentrepresenting the opened document or file. - Throws:
Exception- If an error occurs during the opening or conversion process.
-
close
public static void close(com.sun.star.lang.XComponent store) Closes the givenXComponentinstance, releasing any associated resources. This method ensures proper cleanup of the provided component, either by invoking its close operation or by disposing of it directly, depending on its capabilities.- Parameters:
store- TheXComponentinstance to be closed. It can represent documents or other components managed by the Office framework.
-
write
public static void write(com.sun.star.lang.XComponent store, OutputStream out, PDFProperties pdfProperties) throws Exception Exports the content of anXComponentto a specifiedOutputStreamin PDF format, optionally using providedPDFPropertiesfor configuring export settings. This method requires the Office framework's initialization and throws an exception if it cannot initialize.- Parameters:
store- TheXComponentrepresenting the document or content to be exported.out- TheOutputStreamwhere the content will be written in PDF format.pdfProperties- An instance ofPDFPropertiescontaining optional settings such as filter type or export configurations. Can benullif no additional properties are required.- Throws:
Exception- If an error occurs during the export process, such as initialization failure, unsupported filter, or output stream issues.
-
convertToPDF
public static void convertToPDF(InputStream in, OutputStream out, PDFProperties pdfProperties) throws Exception Converts an input stream of document data into a PDF and writes the resulting content to the provided output stream. The conversion process utilizes specified PDF properties for configuration and formatting.- Parameters:
in- TheInputStreamcontaining the source document data to be converted. This stream is expected to be in a format that the underlying Office framework can process (e.g., ODT, DOC, XLS).out- TheOutputStreamwhere the generated PDF content will be written. This stream must be writable and is where the output will be stored.pdfProperties- An instance ofPDFPropertiescontaining optional settings for the PDF export process, such as page range or encryption. Can benullif no additional properties are required.- Throws:
Exception- If an error occurs during the conversion process, such as issues while opening the document, exporting to PDF, or writing to the output stream.
-
convertToHTML
Converts an input document from anInputStreaminto an HTML document and writes the resulting content to the specifiedOutputStream. The conversion process ensures that images are embedded within the HTML output.- Parameters:
in- TheInputStreamcontaining the source document to be converted. This input should be in a format supported by the underlying Office framework such as ODT or DOC.out- TheOutputStreamwhere the generated HTML content will be written. This stream must be writable and will store the resulting HTML document.- Throws:
Exception- If an error occurs during initialization of the Office framework, the document conversion process, or the output stream operation.
-
shutdown
public static void shutdown()Shuts down the template-based reporting service. This method performs the following actions:- Logs a message indicating the shutdown process has started. - Invokes the
This method is typically used to ensure proper cleanup of the reporting service during application termination or when the service is no longer required.close()method to release resources and terminate active components. - Logs a message confirming that the service has been successfully stopped.
-