1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 package complex.framework.autosave; 23 24 import com.sun.star.uno.*; 25 import com.sun.star.lang.*; 26 import com.sun.star.container.*; 27 import com.sun.star.beans.*; 28 import com.sun.star.util.*; 29 30 class ConfigHelper 31 { 32 private XMultiServiceFactory m_xSMGR = null; 33 private XHierarchicalNameAccess m_xConfig = null; 34 35 //----------------------------------------------- ConfigHelper(XMultiServiceFactory xSMGR , String sConfigPath , boolean bReadOnly )36 public ConfigHelper(XMultiServiceFactory xSMGR , 37 String sConfigPath , 38 boolean bReadOnly ) 39 throws com.sun.star.uno.Exception 40 { 41 m_xSMGR = xSMGR; 42 43 XMultiServiceFactory xConfigRoot = UnoRuntime.queryInterface(XMultiServiceFactory.class, m_xSMGR.createInstance("com.sun.star.configuration.ConfigurationProvider")); 44 45 PropertyValue[] lParams = new PropertyValue[1]; 46 lParams[0] = new PropertyValue(); 47 lParams[0].Name = "nodepath"; 48 lParams[0].Value = sConfigPath; 49 50 Object aConfig; 51 if (bReadOnly) 52 { 53 aConfig = xConfigRoot.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", lParams); 54 } 55 else 56 { 57 aConfig = xConfigRoot.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", lParams); 58 } 59 60 m_xConfig = UnoRuntime.queryInterface(XHierarchicalNameAccess.class, aConfig); 61 62 if (m_xConfig == null) 63 { 64 throw new com.sun.star.uno.Exception("Could not open configuration \"" + sConfigPath + "\""); 65 } 66 } 67 68 //----------------------------------------------- readRelativeKey(String sRelPath, String sKey )69 public Object readRelativeKey(String sRelPath, 70 String sKey ) 71 throws com.sun.star.container.NoSuchElementException 72 { 73 try 74 { 75 XPropertySet xPath = UnoRuntime.queryInterface(XPropertySet.class, m_xConfig.getByHierarchicalName(sRelPath)); 76 return xPath.getPropertyValue(sKey); 77 } 78 catch(com.sun.star.uno.Exception ex) 79 { 80 throw new com.sun.star.container.NoSuchElementException(ex.getMessage()); 81 } 82 } 83 84 //----------------------------------------------- writeRelativeKey(String sRelPath, String sKey , Object aValue )85 public void writeRelativeKey(String sRelPath, 86 String sKey , 87 Object aValue ) 88 throws com.sun.star.container.NoSuchElementException 89 { 90 try 91 { 92 XPropertySet xPath = UnoRuntime.queryInterface(XPropertySet.class, m_xConfig.getByHierarchicalName(sRelPath)); 93 xPath.setPropertyValue(sKey, aValue); 94 } 95 catch(com.sun.star.uno.Exception ex) 96 { 97 throw new com.sun.star.container.NoSuchElementException(ex.getMessage()); 98 } 99 } 100 101 //----------------------------------------------- flush()102 public void flush() 103 { 104 try 105 { 106 XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, m_xConfig); 107 xBatch.commitChanges(); 108 } 109 catch(com.sun.star.uno.Exception ex) 110 {} 111 } 112 113 //----------------------------------------------- readDirectKey(XMultiServiceFactory xSMGR , String sConfigFile, String sRelPath , String sKey )114 public static Object readDirectKey(XMultiServiceFactory xSMGR , 115 String sConfigFile, 116 String sRelPath , 117 String sKey ) 118 throws com.sun.star.uno.Exception 119 { 120 ConfigHelper aConfig = new ConfigHelper(xSMGR, sConfigFile, true); 121 return aConfig.readRelativeKey(sRelPath, sKey); 122 } 123 124 //----------------------------------------------- writeDirectKey(XMultiServiceFactory xSMGR , String sConfigFile, String sRelPath , String sKey , Object aValue )125 public static void writeDirectKey(XMultiServiceFactory xSMGR , 126 String sConfigFile, 127 String sRelPath , 128 String sKey , 129 Object aValue ) 130 throws com.sun.star.uno.Exception 131 { 132 ConfigHelper aConfig = new ConfigHelper(xSMGR, sConfigFile, false); 133 aConfig.writeRelativeKey(sRelPath, sKey, aValue); 134 aConfig.flush(); 135 } 136 } 137