| 1 |
/** |
| 2 |
* ======================================== |
| 3 |
* JFreeReport : a free Java report library |
| 4 |
* ======================================== |
| 5 |
* |
| 6 |
* Project Info: http://reporting.pentaho.org/ |
| 7 |
* |
| 8 |
* (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors. |
| 9 |
* |
| 10 |
* This library is free software; you can redistribute it and/or modify it under the terms |
| 11 |
* of the GNU Lesser General Public License as published by the Free Software Foundation; |
| 12 |
* either version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* See the GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License along with this |
| 19 |
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 20 |
* Boston, MA 02111-1307, USA. |
| 21 |
* |
| 22 |
* [Java is a trademark or registered trademark of Sun Microsystems, Inc. |
| 23 |
* in the United States and other countries.] |
| 24 |
* |
| 25 |
* ------------ |
| 26 |
* $Id: ConfigFactory.java 10756 2009-12-02 15:58:24Z tmorgner $ |
| 27 |
* ------------ |
| 28 |
* (C) Copyright 2000-2005, by Object Refinery Limited. |
| 29 |
* (C) Copyright 2005-2007, by Pentaho Corporation. |
| 30 |
*/ |
| 31 |
|
| 32 |
package org.jfree.report.modules.preferences.base; |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* The config factory is used to access the currently active config storage |
| 37 |
* implementation. The implementation itself allows to read or store a set of properties |
| 38 |
* stored under a certain path. |
| 39 |
* |
| 40 |
* @author Thomas Morgner |
| 41 |
*/ |
| 42 |
public final class ConfigFactory |
| 43 |
{ |
| 44 |
/** |
| 45 |
* The selector configuration key that defines the active config storage |
| 46 |
* implementation. |
| 47 |
*/ |
| 48 |
public static final String CONFIG_TARGET_KEY = "org.jfree.report.ConfigStore"; |
| 49 |
|
| 50 |
/** |
| 51 |
* The singleton instance of the config factory. |
| 52 |
*/ |
| 53 |
private static ConfigFactory factory; |
| 54 |
/** |
| 55 |
* The user storage is used to store user dependend settings. |
| 56 |
*/ |
| 57 |
private ConfigStorage userStorage; |
| 58 |
/** |
| 59 |
* The system storage is used to store system wide settings. |
| 60 |
*/ |
| 61 |
private ConfigStorage systemStorage; |
| 62 |
|
| 63 |
/** |
| 64 |
* Returns the singleton instance of the config factory. |
| 65 |
* |
| 66 |
* @return the config factory |
| 67 |
*/ |
| 68 |
public synchronized static ConfigFactory getInstance () |
| 69 |
{ |
| 70 |
if (factory == null) |
| 71 |
{ |
| 72 |
factory = new ConfigFactory(); |
| 73 |
factory.defineSystemStorage(new NullConfigStorage()); |
| 74 |
factory.defineUserStorage(new NullConfigStorage()); |
| 75 |
} |
| 76 |
return factory; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* DefaultConstructor. |
| 81 |
*/ |
| 82 |
private ConfigFactory () |
| 83 |
{ |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Defines the user storage implementation that should be used. This method should only |
| 88 |
* be called by the module initialization methods. |
| 89 |
* |
| 90 |
* @param storage the user settings storage implementation. |
| 91 |
*/ |
| 92 |
public void defineUserStorage (final ConfigStorage storage) |
| 93 |
{ |
| 94 |
if (storage == null) |
| 95 |
{ |
| 96 |
throw new NullPointerException(); |
| 97 |
} |
| 98 |
this.userStorage = storage; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Defines the system storage implementation that should be used. This method should |
| 103 |
* only be called by the module initialization methods. |
| 104 |
* |
| 105 |
* @param storage the system settings storage implementation. |
| 106 |
*/ |
| 107 |
public void defineSystemStorage (final ConfigStorage storage) |
| 108 |
{ |
| 109 |
if (storage == null) |
| 110 |
{ |
| 111 |
throw new NullPointerException(); |
| 112 |
} |
| 113 |
this.systemStorage = storage; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Returns the user settings storage implementation used in the config subsystem. |
| 118 |
* |
| 119 |
* @return the user settingsstorage provider. |
| 120 |
*/ |
| 121 |
public ConfigStorage getUserStorage () |
| 122 |
{ |
| 123 |
return userStorage; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the system settings storage implementation used in the config subsystem. |
| 128 |
* |
| 129 |
* @return the system settings storage provider. |
| 130 |
*/ |
| 131 |
public ConfigStorage getSystemStorage () |
| 132 |
{ |
| 133 |
return systemStorage; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Checks, whether the given string denotes a valid config storage path. Such an path |
| 138 |
* must not contain whitespaces or non-alphanumeric characters. |
| 139 |
* |
| 140 |
* @param path the path that should be tested. |
| 141 |
* @return true, if the path is valid, false otherwise. |
| 142 |
*/ |
| 143 |
public static boolean isValidPath (final String path) |
| 144 |
{ |
| 145 |
final char[] data = path.toCharArray(); |
| 146 |
for (int i = 0; i < data.length; i++) |
| 147 |
{ |
| 148 |
if (Character.isJavaIdentifierPart(data[i]) == false) |
| 149 |
{ |
| 150 |
return false; |
| 151 |
} |
| 152 |
} |
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Encodes the given configuration path. All non-ascii characters get |
| 158 |
* replaced by an escape sequence. |
| 159 |
* |
| 160 |
* @param path the path. |
| 161 |
* @return the translated path. |
| 162 |
*/ |
| 163 |
public static String encodePath (final String path) |
| 164 |
{ |
| 165 |
final char[] data = path.toCharArray(); |
| 166 |
final StringBuffer encoded = new StringBuffer(); |
| 167 |
for (int i = 0; i < data.length; i++) |
| 168 |
{ |
| 169 |
if (data[i] == '$') |
| 170 |
{ |
| 171 |
// double quote |
| 172 |
encoded.append('$'); |
| 173 |
encoded.append('$'); |
| 174 |
} |
| 175 |
else if (Character.isJavaIdentifierPart(data[i]) == false) |
| 176 |
{ |
| 177 |
// padded hex string |
| 178 |
encoded.append('$'); |
| 179 |
final String hex = Integer.toHexString(data[i]); |
| 180 |
for (int x = hex.length(); x < 4; x++) |
| 181 |
{ |
| 182 |
encoded.append('0'); |
| 183 |
} |
| 184 |
encoded.append(hex); |
| 185 |
} |
| 186 |
else |
| 187 |
{ |
| 188 |
encoded.append(data[i]); |
| 189 |
} |
| 190 |
} |
| 191 |
return encoded.toString(); |
| 192 |
|
| 193 |
} |
| 194 |
} |