[Solved] Check if a Calc document has macros in Java

The Application Programming Interface and the OASIS Open Document Format

[Solved] Check if a Calc document has macros in Java

Postby bcris » Wed Aug 24, 2011 12:21 pm

Is there a way to programmatically check (with java) if a calc document has macros (starting point XSpreadsheetDocument) ? And if there are protected macros among them ?

Thanks in advance !

(I use Open Office 3.3 on Windows XP SP3 and Java 1.6.0_26.)
Last edited by Hagar Delest on Thu Sep 08, 2011 5:28 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.3 on Windows XP SP3
bcris
 
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm

Re: Check if a calc document has macros in Java

Postby bcris » Thu Sep 08, 2011 4:12 pm

Possible solutions:

Code: Select all   Expand viewCollapse view
   public boolean hasMacro(XSpreadsheetDocument xSpreadsheetDocument) {
      boolean ret = false;

      XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSpreadsheetDocument);
      XLibraryContainer xLibraryContainer;

      try {
         xLibraryContainer = (XLibraryContainer)
            UnoRuntime.queryInterface(XLibraryContainer.class, xPropertySet.getPropertyValue("BasicLibraries"));

         if (xLibraryContainer.getElementNames().length > 1) {
            ret = true;
         } else if (xLibraryContainer.getElementNames().length == 1) { // Standard library
            Object standardLibrary;
            try {
               standardLibrary = xLibraryContainer.getByName(xLibraryContainer.getElementNames()[0]);
               XNameAccess xNameAccess = (XNameAccess)
                  UnoRuntime.queryInterface(XNameAccess.class, standardLibrary);
               ret = xNameAccess.hasElements();               

            } catch (NoSuchElementException e) {
               ...
            } catch (WrappedTargetException e) {
               ...
            }
         }
      } catch (UnknownPropertyException e1) {
         ...
      } catch (WrappedTargetException e1) {
         ...
      }
      
      return ret;
   }



Code: Select all   Expand viewCollapse view
   public boolean hasProtectedMacro(XSpreadsheetDocument xSpreadsheetDocument) {
      boolean ret = false;
      
      XPropertySet xPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSpreadsheetDocument);
      XLibraryContainer xLibraryContainer = null;

      try {
         xLibraryContainer = (XLibraryContainer)
            UnoRuntime.queryInterface(XLibraryContainer.class, xPropertySet.getPropertyValue("BasicLibraries"));
      } catch (UnknownPropertyException e) {
         ...
         return false;
      } catch (WrappedTargetException e) {
         ...
         return false;
      }

      XLibraryContainerPassword xLibraryContainerPassword  =
         (XLibraryContainerPassword) UnoRuntime.queryInterface(XLibraryContainerPassword.class, xLibraryContainer);
      
      if (xLibraryContainer != null) {
         int noOfLibraries = xLibraryContainer.getElementNames().length;
         int i = 0;
         while (!ret && i < noOfLibraries) {
            String libraryName = xLibraryContainer.getElementNames()[i];
            
            try {
               ret = xLibraryContainerPassword.isLibraryPasswordProtected(libraryName);
            } catch (NoSuchElementException e) {
               ...
            }
            
            i++;
         }
      }

      return ret;
   }
OpenOffice 3.3 on Windows XP SP3
bcris
 
Posts: 21
Joined: Wed Aug 17, 2011 3:51 pm


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 2 guests