001: // PropertySet.java
002: // $Id: PropertySet.java,v 1.16 2000/08/16 21:37:34 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1996.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigsaw.config;
007:
008: import java.util.Hashtable;
009:
010: import java.io.File;
011:
012: import org.w3c.tools.resources.Attribute;
013: import org.w3c.tools.resources.AttributeHolder;
014: import org.w3c.tools.resources.AttributeRegistry;
015: import org.w3c.tools.resources.BooleanAttribute;
016: import org.w3c.tools.resources.ClassAttribute;
017: import org.w3c.tools.resources.DoubleAttribute;
018: import org.w3c.tools.resources.FileAttribute;
019: import org.w3c.tools.resources.FrameArrayAttribute;
020: import org.w3c.tools.resources.IllegalAttributeAccess;
021: import org.w3c.tools.resources.IntegerAttribute;
022: import org.w3c.tools.resources.LongAttribute;
023: import org.w3c.tools.resources.Resource;
024: import org.w3c.tools.resources.StringArrayAttribute;
025: import org.w3c.tools.resources.StringAttribute;
026:
027: import org.w3c.util.ObservableProperties;
028:
029: import org.w3c.jigsaw.http.httpd;
030:
031: public class PropertySet extends Resource {
032: protected httpd server = null;
033:
034: static {
035: Class c = null;
036: Attribute a = null;
037:
038: try {
039: c = Class.forName("org.w3c.jigsaw.config.PropertySet");
040: } catch (Exception ex) {
041: ex.printStackTrace();
042: System.exit(1);
043: }
044: }
045:
046: /**
047: * Get this property set title.
048: * @return A String encoding the title of the property set.
049: */
050:
051: public String getTitle() {
052: return getIdentifier() + " property set.";
053: }
054:
055: /**
056: * Get this resource's help url.
057: * @return An URL, encoded as a String, or <strong>null</strong> if not
058: * available.
059: */
060:
061: public String getHelpURL() {
062: String docurl = server.getDocumentationURL();
063: if (docurl == null)
064: return null;
065: return docurl + "/" + getClass().getName() + ".html";
066: }
067:
068: /**
069: * Get the help URL for that resource's attribute.
070: * @param topic The topic (can be an attribute name, or a property, etc).
071: * @return A String encoded URL, or <strong>null</strong>.
072: */
073:
074: public String getHelpURL(String topic) {
075: String docurl = server.getDocumentationURL();
076: if (docurl == null)
077: return null;
078: Class defines = AttributeRegistry.getAttributeClass(getClass(),
079: topic);
080: if (defines != null)
081: return docurl + "/" + defines.getName() + ".html";
082: return null;
083: }
084:
085: /**
086: * Set value forwards the effectation to the properties.
087: * @param idx The attribute (property in that case) being set.
088: * @param value The new value for that property.
089: */
090:
091: public synchronized void setValue(int idx, Object value) {
092: // Check access (we don't care about side effects)
093: super .setValue(idx, value);
094: if (idx > ATTR_LAST_MODIFIED) {
095: Attribute a = attributes[idx];
096: if (value == null) {
097: server.getProperties().remove(a.getName());
098: } else {
099: if (!server.getProperties().putValue(a.getName(),
100: a.stringify(value)))
101: throw new IllegalAttributeAccess(this ,
102: getAttributes()[idx], value);
103: }
104: }
105: }
106:
107: protected Object convertingGet(httpd s, Attribute a, Object def) {
108: ObservableProperties p = s.getProperties();
109: def = (def == null) ? a.getDefault() : def;
110: if (a instanceof FileAttribute) {
111: return p.getFile(a.getName(), (File) def);
112: } else if (a instanceof StringAttribute) {
113: return p.getString(a.getName(), (String) def);
114: } else if (a instanceof IntegerAttribute) {
115: int d = (def == null) ? -1 : ((Integer) def).intValue();
116: int i = p.getInteger(a.getName(), d);
117: return new Integer(i);
118: } else if (a instanceof LongAttribute) {
119: long d = (def == null) ? -1 : ((Long) def).longValue();
120: long l = p.getLong(a.getName(), d);
121: return new Long(l);
122: } else if (a instanceof BooleanAttribute) {
123: boolean b = p
124: .getBoolean(a.getName(), (def == Boolean.TRUE));
125: return b ? Boolean.TRUE : Boolean.FALSE;
126: } else if (a instanceof ClassAttribute) {
127: try {
128: String cn = p.getString(a.getName(), null);
129: if (cn == null)
130: return def;
131: return Class.forName(cn);
132: } catch (Exception ex) {
133: throw new RuntimeException("Invalid class name.");
134: }
135: } else if (a instanceof StringArrayAttribute) {
136: return p.getStringArray(a.getName(), null);
137: } else if (a instanceof DoubleAttribute) {
138: double d = ((def == null) ? Double.NaN : ((Double) def)
139: .doubleValue());
140: d = p.getDouble(a.getName(), d);
141: return (d == Double.NaN) ? def : new Double(d);
142: } else if (a instanceof FrameArrayAttribute) {
143: return null; // ugly hack FIXME
144: } else {
145: throw new RuntimeException("// FIXME !!!");
146: }
147: }
148:
149: public Object getValue(int idx, Object def) {
150: // Check access (again we don't care about side effectes)
151: if (idx <= ATTR_LAST_MODIFIED)
152: return super .getValue(idx, def);
153: return convertingGet(server, attributes[idx], def);
154: }
155:
156: public Object unsafeGetValue(int idx, Object def) {
157: // Check access (again we don't care about side effectes)
158: if (idx <= ATTR_LAST_MODIFIED)
159: return super .unsafeGetValue(idx, def);
160: return convertingGet(server, attributes[idx], def);
161: }
162:
163: public PropertySet(String name, httpd server) {
164: super ();
165: this .server = server;
166: setValue(ATTR_IDENTIFIER, name);
167: }
168:
169: public void initialize(Object values[]) {
170: super .initialize(values);
171: attributes[ATTR_IDENTIFIER] = new StringAttribute("identifier",
172: null, 0);
173: }
174: }
|