📄 defaultconfiguration.java
字号:
buffer.append(escapeForXML(varText));
buffer.append("\"/>\n");
}
if (vars) {
buffer.append(" </variables>\n");
}
}
protected String escapeForXML(String text) {
StringBuffer result = new StringBuffer();
for ( int i = 0; i < text.length();i++) {
char character = text.charAt(i);
if (character == '<') {
result.append("<");
}
else if (character == '>') {
result.append(">");
}
else if (character == '\"') {
result.append(""");
}
else if (character == '\'') {
result.append("'");
}
else if (character == '\\') {
result.append("\");
}
else if (character == '&') {
result.append("&");
}
else {
result.append(character);
}
}
return result.toString();
}
/**
* Adds the PropertyListener to the main category.
*
* If the main category changes after the listener has
* already been added, the listener is still registered,
* but not to the main category any longer. One way to
* handle this behavior is to remove the listener from
* the old main category and add it to the new main
* category.
*
* @param listener
*/
public void addPropertyListener(PropertyListener listener) {
addPropertyListener(listener, mainCategory);
}
/**
* Adds the PropertyListener to the given category.
*/
public void addPropertyListener(PropertyListener listener, String categoryName) {
getCategory(categoryName).addPropertyListener(listener);
}
public void addCategoryListener(CategoryListener listener) {
addCategoryListener(listener,mainCategory);
}
public void addCategoryListener(CategoryListener listener,String categoryName) {
getCategory(categoryName).addCategoryListener(listener);
}
/**
* Return the name of the current configuration.
* @return
*/
public String getConfigName() {
return configName;
}
public void setConfigName(String configName) {
this.configName = configName;
}
/**
* Adds the specified configuration listener to receive configuration
* changed events from this configuration.
*
* @param listener The ConfigurationListener
*/
public void addConfigurationListener(ConfigurationListener listener) {
configurationListenerList.add(ConfigurationListener.class, listener);
}
/**
* Removes the specified configuration listener from the configuration
* change events from this configuration.
*
* @param listener The ConfigurationListener
*/
public void removeConfigurationListener(ConfigurationListener listener) {
configurationListenerList.remove(ConfigurationListener.class, listener);
}
/**
* Deliver configuration changed event to all listeners that are registered
* with our listener list.
*
* @param event The ConfigurationChangedEvent
*/
public void fireConfigurationChangedEvent(ConfigurationChangedEvent event) {
// Guaranteed to return a non-null array
Object[] listeners = configurationListenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ConfigurationListener.class) {
// Lazily create the event:
((ConfigurationListener) listeners[i + 1]).configurationChanged(event);
}
}
}
/**
* Returns the main category for this configuration
*
* @return The main category
*/
public Category getCategory() {
return getCategory(mainCategory);
}
/**
* Returns a category based on the name provided.
*
* This implementation has an interesting twist now inside. We need to check
* if the parent's configuration has a category of the same name. The normal
* case is that we override a value in a parent configuration, but we still want
* the values from the parent configuration if they aren't overridden.
*
* Example. parentConfiguration has category A and has property key AKey
* childConfiguration extends parentConfiguration has category and has property key BKey
*
* childConfiguration.getCategory("A").getProperty("AKey"); // didn't work, works with this implementation
* childConfiguration.getCategory("A").getProperty("BKey"); // always worked
*
* The reason for this change is that the category objects don't know anything about
* brother, sister, parent or any other relations. This method handles the logic to
* gather the information about brother, sister, parent, cousin or whatever.
*
* @param categoryName The name of the category (if null, main category will be used)
* @return The category object (new instance if necessary)
*/
public Category getCategory(String categoryName) {
// System.err.println("retrieving category "+categoryName);
if(categoryName == null) {
categoryName = mainCategory;
}
Category category = (Category)categories.get(categoryName);
// the value might be in the base config, so we need to check that too.
if( category == null ) { // category not found, but could be in baseConfig defined
if ( baseConfigName != null ) {
// System.err.println("!!looking in base config " +baseConfigName + " for category");
Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
return cfg.getCategory(categoryName);
}
// System.err.println("getCategory(): creating DefaultCategory " + categoryName + " in " + configName + " configuration");
category = new DefaultCategory(categoryName);
categories.put(categoryName, category);
} else { // category found, but we still need to see if the parent config has a matching category
if ( baseConfigName != null && !"base".equals(baseConfigName) ) { // "base" needs to checked, otherwise endless loop!
// System.err.println("have a baseConfigName:"+baseConfigName);
Configuration parentCfg = ConfigurationManager.getConfiguration(baseConfigName);
Category parentCategory = parentCfg.getCategory(categoryName);
Properties props = parentCategory.getProperties();
// TODO: needs to be investigated, whether or not we should clone the categories
// when we rewrite them. This might cost a little bit, but we should have an
// easier time if we need to save the values back to the property/xml/database/etc.
// I think the save configuration might produce false results.
for (Iterator iter = props.keySet().iterator(); iter.hasNext();) {
String element = (String) iter.next();
if ( category.getProperty(element) == null) { // we don't want to overwrite the child value, if it exists
// in the parent config's category too!
// System.err.println("## ##");
category.setProperty(element, parentCategory.getProperty(element));
}
}
}
}
return category;
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
*/
public void setLongProperty(String key, long value) {
getCategory().setLongProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
*/
public void setIntProperty(String key, int value) {
getCategory().setIntProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
*/
public void setCharProperty(String key, char value) {
getCategory().setCharProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
* @param category
*/
public void setCharProperty(String key, char value, String category) {
getCategory(category).setCharProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
*/
public void setDoubleProperty(String key, double value) {
getCategory().setDoubleProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
* @param category
*/
public void setLongProperty(String key, long value, String category) {
getCategory(category).setLongProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
* @param category
*/
public void setIntProperty(String key, int value, String category) {
getCategory(category).setIntProperty(key, value);
}
/**
* Wrapper method to Category method
*
* @param key
* @param value
* @param category
*/
public void setDoubleProperty(String key, double value, String category) {
getCategory(category).setDoubleProperty(key, value);
}
public boolean hasChanged() {
return dirtyFlag;
}
public String[] getArray(String key) {
return getArray(key,null);
}
public String[] getArray(String key, String[] defaultValue) {
return getArray(key,defaultValue,mainCategory);
}
public String[] getArray(String key, String[] defaultValue, String category) {
return getArray(key,defaultValue,category,",");
}
public String[] getArray(String key, String[] defaultValue, String category,String separator) {
String value = getProperty(key,null,category);
if ( value == null ) {
return defaultValue;
}
Vector all = new Vector();
StringTokenizer sto = new StringTokenizer(value,separator);
while ( sto.hasMoreElements() ) {
String val = (String)sto.nextElement();
val = vm.replaceVariables(val,configName);
all.add(val);
}
return (String[])all.toArray(new String[0]);
}
public boolean isNew() {
return created;
}
public void resetCreated() {
created = false;
}
public String getEncoding() {
return encoding;
}
protected void markDirty() {
dirtyFlag = true;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public boolean containsCategory(String categoryName) {
if(categoryName == null) {
return false;
}
Category category = (Category)categories.get(categoryName);
if( category != null ) {
return true;
}
return false;
}
public void addInclude(int type, String name) {
IncludeEntry ie = new IncludeEntry(name,type);
includes.add(ie);
}
public Vector getIncludes() {
return includes;
}
public void setBaseConfiguration(String name) {
baseConfigName = name;
VariableManager.getInstance().setInheritance(configName,name);
}
public String getBaseConfiguration() {
return baseConfigName;
}
protected class MyCategoryListener implements CategoryListener {
/* (non-Javadoc)
* @see org.jconfig.event.CategoryListener#categoryChanged(org.jconfig.event.CategoryChangedEvent)
*/
public void categoryChanged(CategoryChangedEvent event) {
fireConfigurationChangedEvent((ConfigurationChangedEvent)event);
}
/* (non-Javadoc)
* @see org.jconfig.event.PropertyListener#propertyChanged(org.jconfig.event.PropertyChangedEvent)
*/
public void propertyChanged(PropertyChangedEvent e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -