Adding JMCR-Stable version
[Benchmarks_CSolver.git] / JMCR-Stable / real-world application / jigsaw / src / org / w3c / jigsaw / config / PropertySet.java
diff --git a/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/config/PropertySet.java b/JMCR-Stable/real-world application/jigsaw/src/org/w3c/jigsaw/config/PropertySet.java
new file mode 100644 (file)
index 0000000..b256574
--- /dev/null
@@ -0,0 +1,179 @@
+// PropertySet.java\r
+// $Id: PropertySet.java,v 1.2 2010/06/15 17:53:13 smhuang Exp $\r
+// (c) COPYRIGHT MIT and INRIA, 1996.\r
+// Please first read the full copyright statement in file COPYRIGHT.html\r
+\r
+package org.w3c.jigsaw.config ;\r
+\r
+import java.util.Hashtable;\r
+\r
+import java.io.File;\r
+\r
+import org.w3c.tools.resources.Attribute;\r
+import org.w3c.tools.resources.AttributeHolder;\r
+import org.w3c.tools.resources.AttributeRegistry;\r
+import org.w3c.tools.resources.BooleanAttribute;\r
+import org.w3c.tools.resources.ClassAttribute;\r
+import org.w3c.tools.resources.DoubleAttribute;\r
+import org.w3c.tools.resources.FileAttribute;\r
+import org.w3c.tools.resources.FrameArrayAttribute;\r
+import org.w3c.tools.resources.IllegalAttributeAccess;\r
+import org.w3c.tools.resources.IntegerAttribute;\r
+import org.w3c.tools.resources.LongAttribute;\r
+import org.w3c.tools.resources.Resource;\r
+import org.w3c.tools.resources.StringArrayAttribute;\r
+import org.w3c.tools.resources.StringAttribute;\r
+\r
+import org.w3c.util.ObservableProperties;\r
+\r
+import org.w3c.jigsaw.http.httpd;\r
+\r
+public class PropertySet extends Resource {\r
+    protected httpd   server         = null;\r
+\r
+    static {\r
+       Class     c = null;\r
+       Attribute a = null;\r
+\r
+       try {\r
+           c = Class.forName("org.w3c.jigsaw.config.PropertySet");\r
+           //Added by Jeff Huang\r
+           //TODO: FIXIT\r
+       } catch (Exception ex) {\r
+           ex.printStackTrace();\r
+           System.exit(1);\r
+       }\r
+    }\r
+\r
+    /**\r
+     * Get this property set title.\r
+     * @return A String encoding the title of the property set.\r
+     */\r
+\r
+    public String getTitle() {\r
+       return getIdentifier()+" property set.";\r
+    }\r
+\r
+    /**\r
+     * Get this resource's help url.\r
+     * @return An URL, encoded as a String, or <strong>null</strong> if not\r
+     * available.\r
+     */\r
+\r
+    public String getHelpURL() {\r
+       String docurl = server.getDocumentationURL();\r
+       if ( docurl == null )\r
+           return null;\r
+       return docurl + "/" + getClass().getName() + ".html";\r
+    }\r
+\r
+    /**\r
+     * Get the help URL for that resource's attribute.\r
+     * @param topic The topic (can be an attribute name, or a property, etc).\r
+     * @return A String encoded URL, or <strong>null</strong>.\r
+     */\r
+\r
+    public String getHelpURL(String topic) {\r
+       String docurl = server.getDocumentationURL();\r
+       if ( docurl == null )\r
+           return null;\r
+       Class defines = AttributeRegistry.getAttributeClass(getClass(), topic);\r
+       if ( defines != null ) \r
+           return docurl + "/" + defines.getName() + ".html";\r
+       return null;\r
+    }\r
+\r
+    /**\r
+     * Set value forwards the effectation to the properties.\r
+     * @param idx The attribute (property in that case) being set.\r
+     * @param value The new value for that property.\r
+     */\r
+\r
+    public synchronized void setValue(int idx, Object value) {\r
+       // Check access (we don't care about side effects)\r
+       super.setValue(idx, value);\r
+       if ( idx > ATTR_LAST_MODIFIED ) {\r
+           Attribute a = attributes[idx];\r
+           if ( value == null ) {\r
+               server.getProperties().remove(a.getName());\r
+           } else {\r
+               if ( ! server.getProperties().putValue(a.getName()\r
+                                                      , a.stringify(value)) )\r
+                   throw new IllegalAttributeAccess(this\r
+                                                    , getAttributes()[idx]\r
+                                                    , value);\r
+           }\r
+       }\r
+    }\r
+\r
+    protected Object convertingGet(httpd s, Attribute a, Object def) {\r
+       ObservableProperties p = s.getProperties();\r
+       def = (def == null) ? a.getDefault() : def;\r
+       if ( a instanceof FileAttribute ) {\r
+           return p.getFile(a.getName(), (File) def);\r
+       } else if ( a instanceof StringAttribute ) {\r
+           return p.getString(a.getName(), (String) def);\r
+       } else if ( a instanceof IntegerAttribute ) {\r
+           int d = (def == null) ? -1 : ((Integer) def).intValue();\r
+           int i = p.getInteger(a.getName(), d);\r
+           return new Integer(i); \r
+       } else if ( a instanceof LongAttribute ) {\r
+           long d = (def == null) ? -1 : ((Long) def).longValue();\r
+           long l = p.getLong(a.getName(), d);\r
+           return new Long(l); \r
+       } else if ( a instanceof BooleanAttribute ) {\r
+           boolean b = p.getBoolean(a.getName(), (def == Boolean.TRUE));\r
+           return b ? Boolean.TRUE : Boolean.FALSE;\r
+       } else if ( a instanceof ClassAttribute ) {\r
+           try {\r
+               String cn = p.getString(a.getName(), null);\r
+               if ( cn == null )\r
+                   return def;\r
+               return Class.forName(cn);\r
+           //Added by Jeff Huang\r
+           //TODO: FIXIT\r
+           } catch (Exception ex) {\r
+               throw new RuntimeException("Invalid class name.");\r
+           }\r
+       } else if ( a instanceof StringArrayAttribute ) {\r
+           return p.getStringArray(a.getName(), null);\r
+       } else if ( a instanceof DoubleAttribute ) {\r
+           double d = ((def == null) \r
+                       ? Double.NaN \r
+                       : ((Double) def).doubleValue());\r
+           d = p.getDouble(a.getName(), d);\r
+           return (d == Double.NaN) ? def : new Double(d);\r
+       } else if ( a instanceof FrameArrayAttribute ) {\r
+           return null; // ugly hack FIXME\r
+       } else {\r
+           throw new RuntimeException("// FIXME !!!");\r
+       }\r
+    }\r
+\r
+    public Object getValue(int idx, Object def) {\r
+       // Check access (again we don't care about side effectes)\r
+       if ( idx <= ATTR_LAST_MODIFIED )\r
+           return super.getValue(idx, def);\r
+       return convertingGet(server, attributes[idx], def);\r
+    }\r
+\r
+    public Object unsafeGetValue(int idx, Object def) {\r
+       // Check access (again we don't care about side effectes)\r
+       if ( idx <= ATTR_LAST_MODIFIED )\r
+           return super.unsafeGetValue(idx, def);\r
+       return convertingGet(server, attributes[idx], def);\r
+    }   \r
+\r
+    public PropertySet(String name, httpd server) {\r
+       super();\r
+       this.server = server;\r
+       setValue(ATTR_IDENTIFIER, name);\r
+    }\r
+\r
+    public void initialize(Object values[]) {\r
+       super.initialize(values);\r
+       attributes[ATTR_IDENTIFIER] = new StringAttribute("identifier"\r
+                                                         , null\r
+                                                         , 0);\r
+    }\r
+}\r