out.println("  private final class _Locked extends java.beans.SimpleBeanInfo\n"+
println(out,"  private final class _Locked extends java.beans.SimpleBeanInfo\n"+
    void writeImpl(PrintWriter out, String context, String className) {      out.println("/** Implementation of "+className+".");      out.println(" *  @see "+className);      out.println(" *  @see New"+className);      out.println(" **/");      out.println();      doPackaging(out, context);      out.println("import java.io.ObjectOutputStream;");      out.println("import java.io.ObjectInputStream;");      out.println("import java.io.IOException;");      out.println("import java.beans.PropertyDescriptor;");      out.println("import java.beans.IndexedPropertyDescriptor;");      String importstr=(String)p.get(context,"import");      if (importstr!=null) {        Vector v = explode(importstr, ',');        Enumeration ves = v.elements();        while (ves.hasMoreElements()) {          String ve=(String)ves.nextElement();          out.println("import "+ve+";");        }      }      out.println();      // figure out what we're supposed to extend      String exts = p.get(context, "extends");      String extendstring = "";      if (exts != null) {        extendstring = extendstring+", "+exts;      }            String adapter = p.get(context, "adapter");      if (adapter == null)        adapter = "java.beans.SimpleBeanInfo";      String implclassName = className+"Impl";      String newclassName = "New"+className;      // dataquality requires a subclass      out.println("public"+(hasDQ(context)?"":" final")+" class "+implclassName+" extends "+adapter+"\n"+                  "  implements "+newclassName+", Cloneable\n{");      // constructor      out.println("  public "+implclassName+"() {");      Vector v = getAllDelegateSpecs(context);      if (v != null) {        for (int i =0; i<v.size(); i++) {          Argument dv = (Argument) v.elementAt(i);          // define delegate getter and setter if non-automatic          String autop = (String)p.get(context, dv.name+".auto");          // if it is automatic, pre-initialize it          if (autop != null && Boolean.valueOf(autop).booleanValue()) {            out.println("    "+dv.name+" = new "+dv.type+"(this);");          }        }      }      out.println("  };");      out.println();            boolean timephased = (p.get(context, PGParser.TIMEPHASED) != null);      if (timephased) {        //handle time phased support separately because getters != setters        out.println(NewTimeSpanText);      }              Vector slotspecs = getAllSlotSpecs(context);      // declare the slots (including "inherited" ones)      out.println("  // Slots\n");      Enumeration se = slotspecs.elements();      while (se.hasMoreElements()) {        String slotspec = ((String)se.nextElement()).trim();        int s = slotspec.indexOf(" ");        String type = slotspec.substring(0,s);        String name = slotspec.substring(s+1);        ActiveSlot as = parseActiveSlot(slotspec);        if (as != null) {          name = as.name;        }        String etype = null;        CollectionType ct = parseCollectionType(type);        if (ct != null) {          type = ct.ctype;          etype = ct.etype;        }        String var = (String)p.get(context,name+".var");        if (var == null) { var = "the"+toClassName(name); } // set the default        if (var.equals("")) { var = null; } // unset if specified as empty                  String getter =  (String)p.get(context,name+".getter");        String setter =  (String)p.get(context,name+".setter");        if (as != null) {          // active slot - write dispatchers          String htype = as.handlerName();          var = "the"+htype;          // storage for the handler          out.println("  private transient "+className+"."+htype+" "+var+" = null;");                    // handler installation          out.println("  public void set"+htype+"("+className+"."+htype+" handler) {\n"+                      "    "+var+" = handler;\n"+                      "  }");          out.println("  public "+className+"."+htype+" get"+htype+"() {\n"+                      "    return "+var+";\n"+                      "  }");          // get dispatcher          if (getter != null){            out.println(getter);          } else {            out.println("  public "+type+" "+as.getter(var));          }          // set dispatcher          if (setter != null){            out.println(setter);          } else {            out.println("  public void "+as.setter(var));          }                  } else {          // storage          if (var != null) {            String init = (String)p.get(context,name+".init");            out.print("  private "+type+" "+var);            if (init != null) {              out.print(" = new "+init+"()");            }            out.println(";");            // Normal getter            if (getter != null) {              out.println(getter);            } else {              out.println( "  public "+type+" get"+toClassName(name)+"(){ "+                           "return "+var+"; }");            }            // test for Collection types            if (etype != null) {              String clname = toClassName(name);              out.println("  public boolean in"+clname+"("+etype+" _element) {\n"+                          "    return "+var+".contains(_element);\n"+                          "  }");              out.println("  public "+etype+"[] get"+clname+"AsArray() {");              out.println("    if ("+var+" == null) return new "+etype+"[0];");              out.println("    int l = "+var+".size();");              out.println("    "+etype+"[] v = new "+etype+"[l];");              out.println("    int i=0;");              out.println("    for (Iterator n="+var+".iterator(); n.hasNext(); ) {");              out.println("      v[i]=("+etype+") n.next();");              out.println("      i++;");              out.println("    }");              out.println("    return v;");              out.println("  }");                        out.println("  public "+etype+" getIndexed"+clname+"(int _index) {");              out.println("    if ("+var+" == null) return null;");              out.println("    for (Iterator _i = "+var+".iterator(); _i.hasNext();) {");              out.println("      "+etype+" _e = ("+etype+") _i.next();");              out.println("      if (_index == 0) return _e;");              out.println("      _index--;");              out.println("    }");              out.println("    return null;");              out.println("  }");            }            // setter            if (setter != null) {              out.println(setter);            } else {              out.println("  public void set"+toClassName(name)+"("+type+" "+name+") {");              // special handling for string setters              String isUnique = p.get(context, name+".unique");              if (type.equals("String") &&                  (isUnique == null || isUnique.equals("false"))){                out.println("    if ("+name+"!=null) "+name+"="+name+".intern();");              }              out.println("    "+var+"="+name+";");              out.println("  }");            }            // mutators for collection types            if (etype != null) {              out.println("  public void clear"+toClassName(name)+"() {\n"+                          "    "+var+".clear();\n"+                          "  }");              out.println("  public boolean removeFrom"+toClassName(name)+"("+etype+" _element) {\n"+                          "    return "+var+".remove(_element);\n"+                          "  }");              out.println("  public boolean addTo"+toClassName(name)+"("+etype+" _element) {\n"+                          "    return "+var+".add(_element);\n"+                          "  }");            }          } else {                // var is specified as empty            if (getter != null) out.println(getter);            if (setter != null) out.println(setter);          }        }      }      // delgates      {        Vector vs = getAllDelegateSpecs(context);        if (vs != null) {          out.println();          for (int i =0; i<vs.size(); i++) {            Argument dv = (Argument) vs.elementAt(i);            out.println("  private "+dv.type+" "+dv.name+" = null;");            // define delegate getter and setter if non-automatic            String autop = (String)p.get(context, dv.name+".auto");            // if it isn't automatic, define the setter and getter            if (!(autop != null && Boolean.valueOf(autop).booleanValue())) {              out.println("  public "+dv.type+" get"+toClassName(dv.name)+"() {\n"+                          "    return "+dv.name+";\n"+                          "  }");              out.println("  public void set"+toClassName(dv.name)+"("+dv.type+" _"+dv.name+") {\n"+                          "    if ("+dv.name+" != null) throw new IllegalArgumentException(\""+                          dv.name+" already set\");\n"+                          "    "+dv.name+" = _"+dv.name+";\n"+                          "  }");            }            Vector dsv = parseDelegateSpecs(p.get(context, dv.name+".delegate"));            for (int j=0; j<dsv.size(); j++) {              DelegateSpec ds = (DelegateSpec) dsv.elementAt(j);              out.println("  public "+ds.type+" "+ds.name+                          "("+unparseArguments(ds.args,true)+") {"+                          " "+ ("void".equals(ds.type)?"":"return ")+dv.name+"."+ds.name+                          "("+unparseArguments(ds.args,false)+");"+                          "  }");            }          }        }      }      // copy constructor      out.println();      out.println("  public "+implclassName+"("+className+" original) {");      se = slotspecs.elements();      while (se.hasMoreElements()) {        String slotspec = ((String)se.nextElement()).trim();        int s = slotspec.indexOf(" ");        String type = slotspec.substring(0,s);        String name = slotspec.substring(s+1);        ActiveSlot as = parseActiveSlot(slotspec);        if (as != null) {          name = as.name;        }        String etype = null;        CollectionType ct = parseCollectionType(type);        if (ct != null) {          type = ct.ctype;          etype = ct.etype;        }        if (as != null) {          // do nothing - clones don't get the handler automatically.        } else {          String var = (String)p.get(context,name+".var");          if (var == null) { var = "the"+toClassName(name); } // set the default          if (var.equals("")) { var = null; } // unset if specified as empty          if (var != null) {            out.println("    "+var+" = original.get"+toClassName(name)+"();");          }        }      }      out.println("  }");      out.println();      if (hasDQ(context)) {        out.println("  public boolean hasDataQuality() { return false; }");        out.println("  public org.cougaar.domain.planning.ldm.dq.DataQuality getDataQuality() { return null; }");        /*          // classes without runtime support do not implement NewHasDataQuality!        out.println("  public void setDataQuality(org.cougaar.domain.planning.ldm.dq.DataQuality dq) {\n"+                    "    throw new IllegalArgumentException(\"This instance does not support setting of DataQuality.\");\n"+                    "  }");        */        out.println();        out.println("  // static inner extension class for real DataQuality Support");        out.println("  public final static class DQ extends "+className+"Impl implements org.cougaar.domain.planning.ldm.dq.NewHasDataQuality {");        out.println("   public DQ() {\n"+ // never copy data quality                    "    super();\n"+                    "   }");        out.println("   public DQ("+className+" original) {\n"+ // never copy data quality                    "    super(original);\n"+                    "   }");        out.println("   public Object clone() { return new DQ(this); }");        out.println("   private transient org.cougaar.domain.planning.ldm.dq.DataQuality _dq = null;");        out.println("   public boolean hasDataQuality() { return (_dq!=null); }");        out.println("   public org.cougaar.domain.planning.ldm.dq.DataQuality getDataQuality() { return _dq; }");        out.println("   public void setDataQuality(org.cougaar.domain.planning.ldm.dq.DataQuality dq) { _dq=dq; }");        out.println("   private void writeObject(ObjectOutputStream out) throws IOException {\n"+                    "    out.defaultWriteObject();\n"+                    "    if (out instanceof org.cougaar.core.cluster.persist.PersistenceOutputStream) out.writeObject(_dq);\n"+                    "   }");        out.println("   private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {\n"+                    "    in.defaultReadObject();\n"+                    "    if (in instanceof org.cougaar.core.cluster.persist.PersistenceInputStream) _dq=(org.cougaar.domain.planning.ldm.dq.DataQuality)in.readObject();\n"+                    "   }");        out.println("    ");        out.println("    private final static PropertyDescriptor properties[]=new PropertyDescriptor[1];\n"+                    "    static {\n"+                    "      try {\n"+                    "        properties[0]= new PropertyDescriptor(\"dataQuality\", DQ.class, \"getDataQuality\", null);\n"+                    "      } catch (Exception e) { e.printStackTrace(); }\n"+                    "    }\n"+                    "    public PropertyDescriptor[] getPropertyDescriptors() {\n"+                    "      PropertyDescriptor[] pds = super.properties;\n"+                    "      PropertyDescriptor[] ps = new PropertyDescriptor[pds.length+properties.length];\n"+                    "      System.arraycopy(pds, 0, ps, 0, pds.length);\n"+                    "      System.arraycopy(properties, 0, ps, pds.length, properties.length);\n"+                    "      return ps;\n"+                    "    }");        out.println("  }");        out.println();      } else {        out.println("  public final boolean hasDataQuality() { return false; }");      }      out.println();      // lock and unlock methods      out.println("  private transient "+className+" _locked = null;");      out.println("  public PropertyGroup lock(Object key) {");      out.println("    if (_locked == null)");      out.println("      _locked = new _Locked(key);");      out.println("    return _locked; }");      out.println("  public PropertyGroup lock() "+                  "{ return lock(null); }");      out.println("  public NewPropertyGroup unlock(Object key) "+                  "{ return this; }");      out.println();      // clone method      out.println("  public Object clone() throws CloneNotSupportedException {\n"+                  //"    "+implclassName+" o = ("+implclassName+") super.clone();\n"+                  //"    return new "+implclassName+"(this);\n"+                  "    "+implclassName+" _tmp = new "+implclassName+"(this);");      {        Vector vs = getAllDelegateSpecs(context);        if (vs != null) {          for (int i =0; i<vs.size(); i++) {            Argument dv = (Argument) vs.elementAt(i);            out.println("    _tmp."+dv.name+" = ("+dv.type+") "+dv.name+".copy(_tmp);");          }        }      }      out.println("    return _tmp;\n"+                  "  }");      out.println();      out.println("  public PropertyGroup copy() {\n"+                  "    try {\n"+                  "      return (PropertyGroup) clone();\n"+                  "    } catch (CloneNotSupportedException cnse) { return null;}\n"+                  "  }");      out.println();      // introspection methods      out.println("  public Class getPrimaryClass() {\n"+                  "    return primaryClass;\n"+                  "  }");      out.println("  public String getAssetGetMethod() {\n"+                  "    return assetGetter;\n"+                  "  }");      out.println("  public String getAssetSetMethod() {\n"+                  "    return assetSetter;\n"+                  "  }");      out.println();            // Serialization      boolean needSerialization = false;      se = slotspecs.elements();      while (se.hasMoreElements()) {        String slotspec = ((String)se.nextElement()).trim();        int s = slotspec.indexOf(" ");        String type = slotspec.substring(0,s);        String name = slotspec.substring(s+1);        ActiveSlot as = parseActiveSlot(slotspec);        if (as != null) {          name = as.name;        }        String etype = null;        CollectionType ct = parseCollectionType(type);        if (ct != null) {          type = ct.ctype;          etype = ct.etype;        }        String isUnique = p.get(context, name+".unique");        if (type.equals("String") &&            (isUnique == null || isUnique.equals("false"))){          needSerialization = true;          break;        }      }      if (needSerialization) {        out.println("  private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {");        out.println("    in.defaultReadObject();");        se = slotspecs.elements();        while (se.hasMoreElements()) {          String slotspec = ((String)se.nextElement()).trim();          int s = slotspec.indexOf(" ");          String type = slotspec.substring(0,s);          String name = slotspec.substring(s+1);          ActiveSlot as = parseActiveSlot(slotspec);          if (as != null) {            name = as.name;          }          String etype = null;          CollectionType ct = parseCollectionType(type);          if (ct != null) {            type = ct.ctype;            etype = ct.etype;          }          if (as != null) {            // serialized copies can't get the active slot remotely.            continue;                     }          String var = (String)p.get(context,name+".var");          if (var == null) { var = "the"+toClassName(name); } // set the default          if (var.equals("")) { var = null; } // unset if specified as empty          if (var != null) {            String isUnique = p.get(context, name+".unique");            if (type.equals("String") &&                (isUnique == null || isUnique.equals("false"))){              out.println("    if ("+var+"!= null) "+var+"="+var+".intern();");            }          }        }        out.println("  }");        out.println();      }      writeBeanInfoBody(out, context, className);      // inner (locked) class      out.println("  private final class _Locked extends java.beans.SimpleBeanInfo\n"+                  "    implements "+className+", Cloneable, LockedPG\n"+                  "  {");      out.println("    private transient Object theKey = null;");      out.println("    _Locked(Object key) { ");      out.println("      if (this.theKey == null){  ");      out.println("        this.theKey = key; ");      out.println("      } ");      out.println("    }  ");      out.println();      out.println("    /** public constructor for beaninfo - probably wont work**/");      out.println("    public _Locked() {}");      out.println();      out.println("    public PropertyGroup lock() { return this; }");      out.println("    public PropertyGroup lock(Object o) { return this; }");      out.println();      out.println("    public NewPropertyGroup unlock(Object key) "+                  "throws IllegalAccessException {");      out.println("       if( theKey.equals(key) )");      out.println("         return "+implclassName+".this;");      out.println("       else ");      out.println("         throw new IllegalAccessException(\"unlock: mismatched internal and provided keys!\");");      out.println("    }");      out.println();      out.println("    public PropertyGroup copy() {\n"+                  "      return new "+implclassName+"("+implclassName+".this);\n"+                  "    }");      out.println();      out.println("    public Object clone() throws CloneNotSupportedException {\n"+                  "      return new "+implclassName+"("+implclassName+".this);\n"+                  "    }");      out.println();      if (timephased) {        out.println("    public long getStartTime() { return " +                     implclassName + ".this.getStartTime(); }");        out.println("    public long getEndTime() { return " +                    implclassName + ".this.getEndTime(); }");      }      // dispatch getters      se = slotspecs.elements();      while (se.hasMoreElements()) {        String slotspec = ((String)se.nextElement()).trim();        int s = slotspec.indexOf(" ");        String type = slotspec.substring(0,s);        String name = slotspec.substring(s+1);        ActiveSlot as = parseActiveSlot(slotspec);        if (as != null) {          name = as.name;        }        String etype = null;        CollectionType ct = parseCollectionType(type);        if (ct != null) {          type = ct.ctype;          etype = ct.etype;        }        String getter = "get"+toClassName(name);        if (as != null) {          out.println("    public "+as.rtype+" "+as.getterSpec()+" {\n"+                      "      return "+implclassName+".this."+as.getterCall()+";\n"+                      "    }");        } else {          out.println("    public "+type+" "+getter+"() { "+                      "return "+implclassName+".this."+getter+"(); }");        }        // test for Collection types        if (etype != null) {          String clname = toClassName(name);          String ref = implclassName+".this.";          out.println("  public boolean in"+clname+"("+etype+" _element) {\n"+                      "    return "+ref+"in"+clname+"(_element);\n"+                      "  }");          out.println("  public "+etype+"[] get"+clname+"AsArray() {");          out.println("    return "+ref+"get"+clname+"AsArray();");          out.println("  }");                    out.println("  public "+etype+" getIndexed"+clname+"(int _index) {");          out.println("    return "+ref+"getIndexed"+clname+"(_index);");          out.println("  }");        }      }      {        Vector vs = getAllDelegateSpecs(context);        if (vs != null) {          for (int i =0; i<vs.size(); i++) {            Argument dv = (Argument) vs.elementAt(i);            Vector dsv = parseDelegateSpecs(p.get(context, dv.name+".delegate"));            for (int j=0; j<dsv.size(); j++) {              DelegateSpec ds = (DelegateSpec) dsv.elementAt(j);              out.println("  public "+ds.type+" "+ds.name+                          "("+unparseArguments(ds.args,true)+") {\n"+                          "    "+("void".equals(ds.type)?"":"return ")+implclassName+".this."+ds.name+                          "("+unparseArguments(ds.args,false)+");\n"+                          "  }");            }          }        }      }      if (hasDQ(context)) {        out.println("  public final boolean hasDataQuality() { return "+                    implclassName+".this.hasDataQuality(); }");        out.println("  public final org.cougaar.domain.planning.ldm.dq.DataQuality getDataQuality() { return "+                    implclassName+".this.getDataQuality(); }");      } else {        out.println("  public final boolean hasDataQuality() { return false; }");      }      // introspection method of locked      out.println("    public Class getPrimaryClass() {\n"+                  "      return primaryClass;\n"+                  "    }");      out.println("    public String getAssetGetMethod() {\n"+                  "      return assetGetter;\n"+                  "    }");      out.println("    public String getAssetSetMethod() {\n"+                  "      return assetSetter;\n"+                  "    }");      out.println();      // BeanInfo dispatchers      out.println("    public PropertyDescriptor[] getPropertyDescriptors() {");      out.println("      return properties;");      out.println("    }");      out.println();      out.println("    public Class getIntrospectionClass() {");      out.println("      return "+className+"Impl.class;");      out.println("    }");      out.println();            out.println("  }");      out.println();      out.println("}");    }