/** OOoRegistry.java - a Java class to handle OpenOffice.org's Registry
 *
 * Copyright (C) 2008  Dietmar Hiller, dhiller

    This program is free software; you can redistribute it and/or modify it under the Terms of the GNU Lesser General Public License 
    as published by the Free Software Foundation; version 3 of the License.

    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License along with this program; if not, 
    see <http://www.gnu.org/licenses/>.
 *
 * Created on 5. Mai 2008, 21:34
 *
 */

package name.dhiller.ooo.OOoRegistry;

import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.container.XNameAccess;
import com.sun.star.util.XMacroExpander;
import com.sun.star.beans.PropertyValue;

/** Handling of the Openoffice.org registry
 * @author Dietmar Hiller (dhiller)
 * @version 080508
 * @since OOo 2.0
 */
public class OOoRegistry {
    private final XComponentContext m_xContext;
    
    /** Creates a new instance of OOoRegistry
     */
    public OOoRegistry(XComponentContext xContext) {
        m_xContext = xContext;
    }    
    
    /** get String from registry
     * @param sPackageName name of package (e.g. org.openoffice.my_extension_name)
     * @param sPath the path a registryNode
     * @param sRegKey the name of the registry key
     * @return String
     * @exception Exception returns empty string
     */
    public String getString(String sPackageName, String sPath, String sRegKey) {
        String sString = "";
        try {
             // retrive the configuration node of the extension #####
             XNameAccess xNameAccess = getRegistryKeyContent(sPackageName+"."+sPath);
             if (xNameAccess != null){
                  if (xNameAccess.hasByName(sRegKey)){
                      sString = (String) xNameAccess.getByName(sRegKey);
                  }
             }
        } catch (Exception ex) {
               ex.printStackTrace(System.out);
        }
        return sString;
    }
    
    /** get the value of a path, expand %origin% if available
     * @param sPackageName name of package (e.g. org.openoffice.my_extension_name)
     * @param sPath the path a registryNode
     * @param sRegKey the name of the registry key
     * @return local path to <sPath> 
     * @exception Exception e.g. substring was not successfull, because string was not expanded; nevertheless, sUrl contains the correct data and can be returned
     */
    public String getValue(String sPackageName, String sPath, String sRegKey){
    String sUrl = "";
    try {
          // get the Url and process the Url by the macroexpander...
          sUrl = getString(sPackageName, sPath, sRegKey);
          Object oMacroExpander =
            this.m_xContext.getValueByName("/singletons/com.sun.star.util.theMacroExpander");
          XMacroExpander xMacroExpander = (XMacroExpander)
            UnoRuntime.queryInterface(XMacroExpander.class, oMacroExpander);
          sUrl = xMacroExpander.expandMacros(sUrl);
          sUrl = sUrl.substring(new String("vnd.sun.star.expand:").length(), sUrl.length());
          sUrl = sUrl.trim();
    } catch (Exception ex) {
        return sUrl;    
        // e.g. substring was not successfull, because string was not expanded
        // nevertheless, sUrl contains the correct data and can be returned
    }
         return sUrl;
    }

/* get access to the registry
 * @author dhiller
 * @version 071208
 * @param _sKeyName - key as in <Registry>.xcu
 * @return XNameAccess
 * @exception Exception 
 */
private XNameAccess getRegistryKeyContent(String _sKeyName){
try {
     Object oConfigProvider;
     PropertyValue[] aNodePath = new PropertyValue[1];
         
     XMultiComponentFactory m_xMCF = m_xContext.getServiceManager();
     
     oConfigProvider =
        m_xMCF.createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", this.m_xContext);
     aNodePath[0] = new PropertyValue();
     aNodePath[0].Name = "nodepath";
     aNodePath[0].Value = _sKeyName;
     XMultiServiceFactory xMSFConfig = (XMultiServiceFactory)
        UnoRuntime.queryInterface(XMultiServiceFactory.class, oConfigProvider);
     Object oNode =
        xMSFConfig.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aNodePath);
     XNameAccess xNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oNode);
     return xNameAccess;
}
catch (Exception exception) {
    exception.printStackTrace(System.out);
    return null;
}
}
}
