0001: /*
0002: * <copyright>
0003: *
0004: * Copyright 1997-2004 BBNT Solutions, LLC
0005: * under sponsorship of the Defense Advanced Research Projects
0006: * Agency (DARPA).
0007: *
0008: * You can redistribute this software and/or modify it under the
0009: * terms of the Cougaar Open Source License as published on the
0010: * Cougaar Open Source Website (www.cougaar.org).
0011: *
0012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0023: *
0024: * </copyright>
0025: */
0026:
0027: package org.cougaar.tools.build;
0028:
0029: import java.io.File;
0030: import java.io.FileInputStream;
0031: import java.io.FileOutputStream;
0032: import java.io.IOException;
0033: import java.io.InputStream;
0034: import java.io.OutputStreamWriter;
0035: import java.io.PrintWriter;
0036: import java.util.ArrayList;
0037: import java.util.Collections;
0038: import java.util.HashMap;
0039: import java.util.List;
0040: import java.util.Map;
0041:
0042: public class PGWriter extends WriterBase {
0043: private final static String NewTimeSpanText = "//NewTimeSpan implementation\n"
0044: + " private long theStartTime = TimeSpan.MIN_VALUE;\n"
0045: + " public long getStartTime() {\n"
0046: + " return theStartTime;\n"
0047: + " }\n"
0048: + "\n"
0049: + " private long theEndTime = TimeSpan.MAX_VALUE;\n"
0050: + " public long getEndTime() {\n"
0051: + " return theEndTime;\n"
0052: + " }\n"
0053: + "\n"
0054: + " public void setTimeSpan(long startTime, long endTime) {\n"
0055: +
0056:
0057: " if ((startTime >= MIN_VALUE) && \n"
0058: + " (endTime <= MAX_VALUE) &&\n"
0059: + " (endTime >= startTime + EPSILON)) {\n"
0060: + " theStartTime = startTime;\n"
0061: + " theEndTime = endTime;\n"
0062: + " } else {\n"
0063: + " throw new IllegalArgumentException();\n"
0064: + " }\n"
0065: + " }\n"
0066: + "\n"
0067: + " public void setTimeSpan(TimeSpan timeSpan) {\n"
0068: + " setTimeSpan(timeSpan.getStartTime(), timeSpan.getEndTime());\n"
0069: + " }\n";
0070:
0071: class Writer {
0072: PrintWriter filelist = null;
0073: PGParser p;
0074:
0075: public Writer(PGParser p) {
0076: this .p = p;
0077: try {
0078: filelist = new PrintWriter(new OutputStreamWriter(
0079: new FileOutputStream(new File(getTargetDir(),
0080: getGenFileName()))));
0081: noteFile(getGenFileName());
0082: } catch (IOException ioe) {
0083: throw new RuntimeException();
0084: }
0085: }
0086:
0087: public void noteFile(String s) {
0088: println(filelist, s);
0089: }
0090:
0091: public void done() {
0092: filelist.close();
0093: }
0094:
0095: void grokGlobal() {
0096: }
0097:
0098: /** convert a string like foo_bar_baz to FooBarBaz **/
0099: String toClassName(String s) {
0100: StringBuffer b = new StringBuffer();
0101: boolean isFirst = true;
0102:
0103: for (int i = 0; i < s.length(); i++) {
0104: char c = s.charAt(i);
0105: if (c == '_') {
0106: isFirst = true;
0107: } else {
0108: if (isFirst && Character.isLowerCase(c))
0109: c = Character.toUpperCase(c);
0110: isFirst = false;
0111: b.append(c);
0112: }
0113: }
0114:
0115: return b.toString();
0116: }
0117:
0118: /** convert a string like foo_bar_baz to fooBarBaz **/
0119: String toVariableName(String s) {
0120: StringBuffer b = new StringBuffer();
0121: boolean isFirst = true;
0122: boolean isStart = true;
0123:
0124: for (int i = 0; i < s.length(); i++) {
0125: char c = s.charAt(i);
0126: if (c == '_') {
0127: isFirst = true;
0128: } else {
0129: if (isStart && isFirst && Character.isLowerCase(c))
0130: c = Character.toUpperCase(c);
0131: isFirst = false;
0132: isStart = false;
0133: b.append(c);
0134: }
0135: }
0136:
0137: return b.toString();
0138: }
0139:
0140: /** convert a string like foo_bar_baz to FOO_BAR_BAZ **/
0141: String toConstantName(String s) {
0142: StringBuffer b = new StringBuffer();
0143:
0144: for (int i = 0; i < s.length(); i++) {
0145: char c = s.charAt(i);
0146: if (Character.isLowerCase(c))
0147: c = Character.toUpperCase(c);
0148: b.append(c);
0149: }
0150:
0151: return b.toString();
0152: }
0153:
0154: List<String> explode(String s) {
0155: return explode(s, ' ');
0156: }
0157:
0158: List<String> explode(String s, char x) {
0159: List<String> r = new ArrayList<String>();
0160: int last = 0;
0161: for (int i = 0; i < s.length(); i++) {
0162: char c = s.charAt(i);
0163: if (c == x) {
0164: if (i > last)
0165: r.add(s.substring(last, i));
0166: last = i + 1;
0167: }
0168: }
0169: if (s.length() > last)
0170: r.add(s.substring(last));
0171: return r;
0172: }
0173:
0174: String findPackage(String context) {
0175: String pkg = p.get(context, "package");
0176: if (pkg == null)
0177: pkg = p.get("global", "package");
0178: if (pkg == null)
0179: pkg = "org.cougaar.planning.ldm.asset";
0180: return pkg;
0181: }
0182:
0183: void doPackaging(PrintWriter out, String context) {
0184: println(out, "package " + findPackage(context) + ";");
0185: println(out);
0186: println(out, "import org.cougaar.planning.ldm.measure.*;");
0187: println(out, "import org.cougaar.planning.ldm.asset.*;");
0188: println(out, "import org.cougaar.planning.ldm.plan.*;");
0189: if (isTimePhased(context)) {
0190: println(out, "import org.cougaar.util.TimeSpan;");
0191: }
0192: println(out, "import java.util.*;");
0193: println(out);
0194: doImports(out, "global");
0195: if (!context.equals("global"))
0196: doImports(out, context);
0197:
0198: if (hasRelationships(context)) {
0199: println(out,
0200: "import org.cougaar.planning.ldm.plan.HasRelationships;");
0201: println(out,
0202: "import org.cougaar.planning.ldm.plan.RelationshipSchedule;");
0203: }
0204: }
0205:
0206: void doImports(PrintWriter out, String context) {
0207: String importstr = (String) p.get(context, "import");
0208: if (importstr != null) {
0209: for (String ve : explode(importstr, ',')) {
0210: println(out, "import " + ve + ";");
0211: }
0212: }
0213: println(out);
0214: }
0215:
0216: /** return DQ specification of context, first looking at local context,
0217: * then global, then default (true).
0218: **/
0219: boolean hasDQ(String context) {
0220: return p
0221: .getValueFlag(context, "hasDataQuality", true, true);
0222: }
0223:
0224: /** return HasRelationships specification of context, looking at local
0225: * context, then global, then default (false).
0226: **/
0227: boolean hasRelationships(String context) {
0228: return p.getValueFlag(context, PGParser.HAS_RELATIONSHIPS,
0229: true, false);
0230: }
0231:
0232: /** return TimePhased specification of context, looking at local
0233: * context, then global, then default (false).
0234: **/
0235: boolean isTimePhased(String context) {
0236: return p.getValueFlag(context, PGParser.TIMEPHASED, true,
0237: false);
0238: }
0239:
0240: /** return Local specification of context, looking at local
0241: * context, then global, then default (false).
0242: **/
0243: boolean isLocal(String context) {
0244: return p.getValueFlag(context, PGParser.LOCAL, true, false);
0245: }
0246:
0247: String getExtraInterface(String context) {
0248: return (String) p.getValue(context, "interface", false,
0249: null);
0250: }
0251:
0252: void writeGetterIfc(PrintWriter out, String context,
0253: String className) {
0254: println(out, "/** Primary client interface for "
0255: + className + ".");
0256: String doc = (String) p.get(context, "doc");
0257: if (doc != null)
0258: println(out, " * " + doc);
0259: println(out, " * @see New" + className);
0260: println(out, " * @see " + className + "Impl");
0261: println(out, " **/");
0262: println(out);
0263: doPackaging(out, context);
0264:
0265: // figure out what we're supposed to extend
0266:
0267: {
0268: String dq = (hasDQ(context) ? ", org.cougaar.planning.ldm.dq.HasDataQuality"
0269: : "");
0270: String local = (isLocal(context) ? ", LocalPG" : "");
0271: String timePhased = (isTimePhased(context) ? "TimePhased"
0272: : "");
0273: String xifc = getExtraInterface(context);
0274: String xifcs = (xifc == null) ? "" : (", " + xifc);
0275: println(out, "public interface " + className
0276: + " extends " + timePhased + "PropertyGroup"
0277: + dq + local + xifcs + " {");
0278: }
0279:
0280: // declare the slots
0281: for (String slotspec : getAllSlotSpecs(context)) {
0282: int s = slotspec.indexOf(" ");
0283: String type = slotspec.substring(0, s);
0284: String name = slotspec.substring(s + 1);
0285: ActiveSlot as = parseActiveSlot(slotspec);
0286: if (as != null) {
0287: name = as.name;
0288: }
0289: String etype = null;
0290: CollectionType ct = parseCollectionType(type);
0291: if (ct != null) {
0292: type = ct.ctype;
0293: etype = ct.etype;
0294: }
0295: String clname = toClassName(name);
0296:
0297: String slotdoc = (String) p.get(context, name + ".doc");
0298: String depp = (String) p.get(context, name
0299: + ".deprecated");
0300: if (depp != null) {
0301: if (slotdoc != null)
0302: slotdoc = slotdoc + "\n * @deprecated "
0303: + depp;
0304: else
0305: slotdoc = "@deprecated " + depp;
0306: }
0307: if (slotdoc != null)
0308: println(out, " /** " + slotdoc + " **/");
0309: if (as != null) {
0310: println(out, " " + type + " " + as.getterSpec()
0311: + ";");
0312: } else {
0313: println(out, " " + type + " get" + clname + "();");
0314: }
0315:
0316: // extra collection type api
0317: if (etype != null) {
0318: println(out,
0319: " /** test to see if an element is a member of the "
0320: + name + " Collection **/");
0321: println(out, " boolean in" + clname + "(" + etype
0322: + " element);");
0323: println(out);
0324:
0325: println(out, " /** array getter for beans **/");
0326: println(out, " " + etype + "[] get" + clname
0327: + "AsArray();");
0328: println(out);
0329:
0330: println(out, " /** indexed getter for beans **/");
0331: println(out, " " + etype + " getIndexed" + clname
0332: + "(int index);");
0333: println(out);
0334: }
0335:
0336: if (as != null) {
0337: println(out, as.handlerClassDef());
0338: }
0339: }
0340: println(out);
0341:
0342: // delegation handling
0343: for (Argument dv : getAllDelegateSpecs(context)) {
0344: for (DelegateSpec ds : parseDelegateSpecs(p.get(
0345: context, dv.name + ".delegate"))) {
0346: String slotdoc = (String) p.get(context, ds.name
0347: + ".doc");
0348: if (slotdoc != null)
0349: println(out, " /** " + slotdoc + " **/");
0350:
0351: println(out, " " + ds.type + " " + ds.name + "("
0352: + unparseArguments(ds.args, true) + ");");
0353: }
0354: }
0355:
0356: println(out, " // introspection and construction");
0357: println(out,
0358: " /** the method of factoryClass that creates this type **/");
0359: println(out, " String factoryMethod = \"new" + className
0360: + "\";");
0361: println(out,
0362: " /** the (mutable) class type returned by factoryMethod **/");
0363: println(out, " String mutableClass = \""
0364: + findPackage(context) + ".New" + className + "\";");
0365: println(out, " /** the factory class **/");
0366: println(out, " Class factoryClass = "
0367: + findPackage("global")
0368: + ".PropertyGroupFactory.class;");
0369:
0370: println(out,
0371: " /** the (immutable) class type returned by domain factory **/");
0372: println(out, " Class primaryClass = "
0373: + findPackage(context) + "." + className
0374: + ".class;");
0375:
0376: println(out, " String assetSetter = \"set" + className
0377: + "\";");
0378: println(out, " String assetGetter = \"get" + className
0379: + "\";");
0380: println(
0381: out,
0382: " /** The Null instance for indicating that the PG definitely has no value **/");
0383: println(out, " " + className + " nullPG = new Null_"
0384: + className + "();");
0385: writeNullClass(out, context, className);
0386: writeFutureClass(out, context, className);
0387: println(out, "}");
0388: }
0389:
0390: void writeNullClass(PrintWriter out, String context,
0391: String className) {
0392: println(out);
0393: // null class implementation
0394: println(out, "/** Null_PG implementation for " + className
0395: + " **/");
0396: println(out, "final class Null_" + className + "\n"
0397: + " implements " + className + ", Null_PG\n" + "{");
0398:
0399: // declare the slots
0400: for (String slotspec : getAllSlotSpecs(context)) {
0401: int s = slotspec.indexOf(" ");
0402: String type = slotspec.substring(0, s);
0403: String name = slotspec.substring(s + 1);
0404: ActiveSlot as = parseActiveSlot(slotspec);
0405: if (as != null) {
0406: name = as.name;
0407: }
0408: String etype = null;
0409: CollectionType ct = parseCollectionType(type);
0410: if (ct != null) {
0411: type = ct.ctype;
0412: etype = ct.etype;
0413: }
0414: String clname = toClassName(name);
0415:
0416: String slotdoc = null;
0417: String depp = (String) p.get(context, name
0418: + ".deprecated");
0419: if (depp != null) {
0420: slotdoc = "@deprecated " + depp;
0421: }
0422: if (slotdoc != null)
0423: println(out, " /** " + slotdoc + " **/");
0424: if (as != null) {
0425: println(
0426: out,
0427: " public "
0428: + type
0429: + " "
0430: + as.getterSpec()
0431: + "{ throw new UndefinedValueException(); }");
0432: } else {
0433: println(
0434: out,
0435: " public "
0436: + type
0437: + " get"
0438: + clname
0439: + "() { throw new UndefinedValueException(); }");
0440: }
0441:
0442: // extra collection type api
0443: if (etype != null) {
0444: println(out, " public boolean in" + clname + "("
0445: + etype + " element) { return false; }");
0446: println(out, " public " + etype + "[] get"
0447: + clname + "AsArray() { return null; }");
0448: println(
0449: out,
0450: " public "
0451: + etype
0452: + " getIndexed"
0453: + clname
0454: + "(int index) { throw new UndefinedValueException(); }");
0455: }
0456: }
0457:
0458: for (Argument dv : getAllDelegateSpecs(context)) {
0459: // define delegate getter and setter if non-automatic
0460: String autop = (String) p.get(context, dv.name
0461: + ".auto");
0462: // if it isn't automatic, define the setter and getter
0463: if (!(autop != null && Boolean.valueOf(autop)
0464: .booleanValue())) {
0465: println(
0466: out,
0467: " public "
0468: + dv.type
0469: + " get"
0470: + toClassName(dv.name)
0471: + "() {\n"
0472: + " throw new UndefinedValueException();\n"
0473: + " }");
0474: println(
0475: out,
0476: " public void set"
0477: + toClassName(dv.name)
0478: + "("
0479: + dv.type
0480: + " _"
0481: + dv.name
0482: + ") {\n"
0483: + " throw new UndefinedValueException();\n"
0484: + " }");
0485: }
0486:
0487: for (DelegateSpec ds : parseDelegateSpecs(p.get(
0488: context, dv.name + ".delegate"))) {
0489: println(out, " public " + ds.type + " " + ds.name
0490: + "(" + unparseArguments(ds.args, true)
0491: + ") {"
0492: + " throw new UndefinedValueException(); "
0493: + "}");
0494: }
0495: }
0496:
0497: // TimePhased getters
0498: if (isTimePhased(context)) {
0499: println(out,
0500: " public long getStartTime() { throw new UndefinedValueException(); }");
0501: println(out,
0502: " public long getEndTime() { throw new UndefinedValueException(); }");
0503: }
0504:
0505: println(
0506: out,
0507: " public boolean equals(Object object) { throw new UndefinedValueException(); }");
0508:
0509: println(
0510: out,
0511: " public Object clone() throws CloneNotSupportedException {\n"
0512: + " throw new CloneNotSupportedException();\n"
0513: + " }");
0514: println(out,
0515: " public NewPropertyGroup unlock(Object key) { return null; }");
0516: println(out,
0517: " public PropertyGroup lock(Object key) { return null; }");
0518: println(out,
0519: " public PropertyGroup lock() { return null; }");
0520: println(out,
0521: " public PropertyGroup copy() { return null; }");
0522: println(out,
0523: " public Class getPrimaryClass(){return primaryClass;}");
0524: println(out,
0525: " public String getAssetGetMethod() {return assetGetter;}");
0526: println(out,
0527: " public String getAssetSetMethod() {return assetSetter;}");
0528: println(out, " public Class getIntrospectionClass() {\n"
0529: + " return " + className + "Impl.class;\n"
0530: + " }");
0531:
0532: // implement PG basic api - null never has DQ
0533: println(out);
0534: println(out,
0535: " public boolean hasDataQuality() { return false; }");
0536:
0537: if (hasDQ(context)) { // if the class has it, we need to implement the getter
0538: println(
0539: out,
0540: " public org.cougaar.planning.ldm.dq.DataQuality getDataQuality() { return null; }");
0541: }
0542: println(out, "}");
0543:
0544: }
0545:
0546: void writeFutureClass(PrintWriter out, String context,
0547: String className) {
0548: println(out);
0549: // null class implementation
0550: println(out, "/** Future PG implementation for "
0551: + className + " **/");
0552: println(out, "final class Future\n" + " implements "
0553: + className + ", Future_PG\n" + "{");
0554: // declare the slots
0555: for (String slotspec : getAllSlotSpecs(context)) {
0556: int s = slotspec.indexOf(" ");
0557: String type = slotspec.substring(0, s);
0558: String name = slotspec.substring(s + 1);
0559: ActiveSlot as = parseActiveSlot(slotspec);
0560: if (as != null) {
0561: name = as.name;
0562: }
0563: String etype = null;
0564: CollectionType ct = parseCollectionType(type);
0565: if (ct != null) {
0566: type = ct.ctype;
0567: etype = ct.etype;
0568: }
0569: String clname = toClassName(name);
0570:
0571: String slotdoc = null;
0572: String depp = (String) p.get(context, name
0573: + ".deprecated");
0574: if (depp != null) {
0575: slotdoc = "@deprecated " + depp;
0576: }
0577: if (slotdoc != null)
0578: println(out, " /** " + slotdoc + " **/");
0579: if (as != null) {
0580: println(out, " public " + type + " "
0581: + as.getterSpec() + "{\n"
0582: + " waitForFinalize();\n"
0583: + " return _real." + as.getterCall()
0584: + ";\n" + " }");
0585: } else {
0586: println(out, " public " + type + " get" + clname
0587: + "() {\n" + " waitForFinalize();\n"
0588: + " return _real.get" + clname + "();\n"
0589: + " }");
0590: }
0591:
0592: // extra collection type api
0593: if (etype != null) {
0594: println(out, " public boolean in" + clname + "("
0595: + etype + " element) {\n"
0596: + " waitForFinalize();\n"
0597: + " return _real.in" + clname
0598: + "(element);\n" + " }");
0599: println(out, " public " + etype + "[] get"
0600: + clname + "AsArray() {\n"
0601: + " waitForFinalize();\n"
0602: + " return _real.get" + clname
0603: + "AsArray();\n" + " }");
0604: println(out, " public " + etype + " getIndexed"
0605: + clname + "(int index) {\n"
0606: + " waitForFinalize();\n"
0607: + " return _real.getIndexed" + clname
0608: + "(index);\n" + " }");
0609: }
0610: }
0611:
0612: println(out, " public boolean equals(Object object) {\n"
0613: + " waitForFinalize();\n"
0614: + " return _real.equals(object);\n" + " }");
0615:
0616: for (Argument dv : getAllDelegateSpecs(context)) {
0617: for (DelegateSpec ds : parseDelegateSpecs(p.get(
0618: context, dv.name + ".delegate"))) {
0619: println(out, " public " + ds.type + " " + ds.name
0620: + "(" + unparseArguments(ds.args, true)
0621: + ") {\n" + " waitForFinalize();\n"
0622: + " "
0623: + ("void".equals(ds.type) ? "" : "return ")
0624: + "_real." + ds.name + "("
0625: + unparseArguments(ds.args, false) + ");\n"
0626: + " }");
0627: }
0628: }
0629:
0630: // TimePhased getters
0631: if (isTimePhased(context)) {
0632: println(out, " public long getStartTime() {\n"
0633: + " waitForFinalize();\n"
0634: + " return _real.getStartTime();\n" + " }");
0635: println(out, " public long getEndTime() {\n"
0636: + " waitForFinalize();\n"
0637: + " return _real.getEndTime();\n" + " }");
0638: }
0639:
0640: println(
0641: out,
0642: " public Object clone() throws CloneNotSupportedException {\n"
0643: + " throw new CloneNotSupportedException();\n"
0644: + " }");
0645: println(out,
0646: " public NewPropertyGroup unlock(Object key) { return null; }");
0647: println(out,
0648: " public PropertyGroup lock(Object key) { return null; }");
0649: println(out,
0650: " public PropertyGroup lock() { return null; }");
0651: println(out,
0652: " public PropertyGroup copy() { return null; }");
0653: println(out,
0654: " public Class getPrimaryClass(){return primaryClass;}");
0655: println(out,
0656: " public String getAssetGetMethod() {return assetGetter;}");
0657: println(out,
0658: " public String getAssetSetMethod() {return assetSetter;}");
0659: println(out, " public Class getIntrospectionClass() {\n"
0660: + " return " + className + "Impl.class;\n"
0661: + " }");
0662:
0663: // Futures do not have data quality, though the replacement instance
0664: // may.
0665: println(
0666: out,
0667: " public synchronized boolean hasDataQuality() {\n"
0668: + " return (_real!=null) && _real.hasDataQuality();\n"
0669: + " }");
0670: if (hasDQ(context)) { // if the class has it, we need to implement the getter
0671: println(
0672: out,
0673: " public synchronized org.cougaar.planning.ldm.dq.DataQuality getDataQuality() {\n"
0674: + " return (_real==null)?null:(_real.getDataQuality());\n"
0675: + " }");
0676: }
0677:
0678: println(out);
0679: println(out, " // Finalization support");
0680: println(out, " private " + className + " _real = null;");
0681: println(
0682: out,
0683: " public synchronized void finalize(PropertyGroup real) {\n"
0684: + " if (real instanceof "
0685: + className
0686: + ") {\n"
0687: + " _real=("
0688: + className
0689: + ") real;\n"
0690: + " notifyAll();\n"
0691: + " } else {\n"
0692: + " throw new IllegalArgumentException(\"Finalization with wrong class: \"+real);\n"
0693: + " }\n" + " }");
0694: println(
0695: out,
0696: " private synchronized void waitForFinalize() {\n"
0697: + " while (_real == null) {\n"
0698: + " try {\n"
0699: + " wait();\n"
0700: + " } catch (InterruptedException _ie) {\n"
0701: + " // We should really let waitForFinalize throw InterruptedException\n"
0702: + " Thread.interrupted();\n"
0703: + " }\n" + " }\n" + " }");
0704: println(out, "}");
0705: }
0706:
0707: void writeSetterIfc(PrintWriter out, String context,
0708: String className) {
0709: println(out, "/** Additional methods for " + className);
0710: println(out,
0711: " * offering mutators (set methods) for the object's owner");
0712: println(out, " **/");
0713: println(out);
0714: doPackaging(out, context);
0715:
0716: String importstr = (String) p.get(context, "import");
0717: if (importstr != null) {
0718: for (String ve : explode(importstr, ',')) {
0719: println(out, "import " + ve + ";");
0720: }
0721: }
0722: println(out);
0723:
0724: String newclassName = "New" + className;
0725:
0726: // figure out what we're supposed to extend
0727: String timePhased = (isTimePhased(context) ? "TimePhased"
0728: : "");
0729: String dq = (hasDQ(context) ? ", org.cougaar.planning.ldm.dq.HasDataQuality"
0730: : "");
0731: String hasRelationships = (hasRelationships(context) ? ", HasRelationships"
0732: : "");
0733: String extendstring = "extends " + className + ", New"
0734: + timePhased + "PropertyGroup" + dq
0735: + hasRelationships;
0736:
0737: println(out, "public interface " + newclassName + " "
0738: + extendstring + " {");
0739:
0740: // declare the slots
0741: for (String slotspec : getAllSlotSpecs(context)) {
0742: int s = slotspec.indexOf(" ");
0743: String type = slotspec.substring(0, s);
0744: String name = slotspec.substring(s + 1);
0745: ActiveSlot as = parseActiveSlot(slotspec);
0746: if (as != null) {
0747: name = as.name;
0748: }
0749: String etype = null;
0750: CollectionType ct = parseCollectionType(type);
0751: if (ct != null) {
0752: type = ct.ctype;
0753: etype = ct.etype;
0754: }
0755:
0756: String depp = (String) p.get(context, name
0757: + ".deprecated");
0758: if (depp != null)
0759: println(out, " /** @deprecated " + depp + " **/");
0760: if (as != null) {
0761: println(out, " void " + as.setterSpec() + ";");
0762: } else {
0763: println(out, " void set" + toClassName(name) + "("
0764: + type + " " + name + ");");
0765: }
0766: // mutators for collection types
0767: if (etype != null) {
0768: println(out, " void clear" + toClassName(name)
0769: + "();");
0770: println(out, " boolean removeFrom"
0771: + toClassName(name) + "(" + etype
0772: + " _element);");
0773: println(out, " boolean addTo" + toClassName(name)
0774: + "(" + etype + " _element);");
0775: }
0776:
0777: if (as != null) {
0778: // handler installation method
0779: String htype = as.handlerName();
0780: println(out, " void set" + htype + "(" + className
0781: + "." + htype + " handler);");
0782: println(out, " " + className + "." + htype
0783: + " get" + htype + "();");
0784: }
0785:
0786: }
0787:
0788: // delegation
0789: {
0790: for (Argument dv : getAllDelegateSpecs(context)) {
0791: // define delegate getter and setter if non-automatic
0792: String autop = (String) p.get(context, dv.name
0793: + ".auto");
0794: // if it isn't automatic, define the setter and getter
0795: if (!(autop != null && Boolean.valueOf(autop)
0796: .booleanValue())) {
0797: println(out, " " + dv.type + " get"
0798: + toClassName(dv.name) + "();");
0799: println(out, " void set"
0800: + toClassName(dv.name) + "(" + dv.type
0801: + " _" + dv.name + ");");
0802: }
0803:
0804: }
0805: }
0806:
0807: println(out, "}");
0808: }
0809:
0810: List<String> getAllSlotSpecs(String context) {
0811: List<String> slotspecs = getLocalSlotSpecs(context);
0812: String exts = p.get(context, "extends");
0813:
0814: if (exts != null && exts.length() > 0) {
0815: for (String ss : getAllSlotSpecs(exts)) {
0816: slotspecs.add(ss);
0817: }
0818: }
0819:
0820: return slotspecs;
0821: }
0822:
0823: List<String> getLocalSlotSpecs(String context) {
0824: return parseSlots(p.get(context, "slots"));
0825: }
0826:
0827: List<Argument> getAllDelegateSpecs(String context) {
0828: List<Argument> slotspecs = getLocalDelegateSpecs(context);
0829: String exts = p.get(context, "extends");
0830: if (exts != null && exts.length() > 0) {
0831: for (Argument arg : getAllDelegateSpecs(exts)) {
0832: slotspecs.add(arg);
0833: }
0834: }
0835: return slotspecs;
0836: }
0837:
0838: List<Argument> getLocalDelegateSpecs(String context) {
0839: return parseArguments(p.get(context, "delegates"));
0840: }
0841:
0842: List<String> parseSlots(String slotstr) {
0843: if (slotstr == null)
0844: slotstr = "";
0845: int s = 0;
0846: char[] chars = slotstr.toCharArray();
0847: int l = chars.length;
0848: int parens = 0;
0849: List<String> slots = new ArrayList<String>();
0850:
0851: int p;
0852: for (p = 0; p < l; p++) {
0853: char c = chars[p];
0854: // need to parse out parens
0855: if (c == '(') {
0856: parens++;
0857: } else if (c == ')') {
0858: parens--;
0859: } else if (c == ',' && parens == 0) {
0860: slots.add(slotstr.substring(s, p).trim());
0861: s = p + 1;
0862: } else {
0863: // just advance
0864: }
0865: }
0866: if (p > s) {
0867: slots.add(slotstr.substring(s, p).trim());
0868: }
0869: return slots;
0870: }
0871:
0872: void writeImpl(PrintWriter out, String context, String className) {
0873: println(out, "/** Implementation of " + className + ".");
0874: println(out, " * @see " + className);
0875: println(out, " * @see New" + className);
0876: println(out, " **/");
0877: println(out);
0878: doPackaging(out, context);
0879: println(out, "import java.io.ObjectOutputStream;");
0880: println(out, "import java.io.ObjectInputStream;");
0881: println(out, "import java.io.IOException;");
0882: println(out, "import java.beans.PropertyDescriptor;");
0883: println(out, "import java.beans.IndexedPropertyDescriptor;");
0884:
0885: String importstr = (String) p.get(context, "import");
0886: if (importstr != null) {
0887: for (String ve : explode(importstr, ',')) {
0888: println(out, "import " + ve + ";");
0889: }
0890: }
0891: println(out);
0892: // figure out what we're supposed to extend
0893: String exts = p.get(context, "extends");
0894: String extendstring = "";
0895:
0896: if (exts != null) {
0897: extendstring = extendstring + ", " + exts;
0898: }
0899:
0900: String adapter = p.get(context, "adapter");
0901: if (adapter == null)
0902: adapter = "java.beans.SimpleBeanInfo";
0903:
0904: String implclassName = className + "Impl";
0905: String newclassName = "New" + className;
0906: // dataquality requires a subclass
0907: println(out, "public" + (hasDQ(context) ? "" : " final")
0908: + " class " + implclassName + " extends " + adapter
0909: + "\n" + " implements " + newclassName
0910: + ", Cloneable\n{");
0911:
0912: // constructor
0913: println(out, " public " + implclassName + "() {");
0914:
0915: for (Argument dv : getAllDelegateSpecs(context)) {
0916: // define delegate getter and setter if non-automatic
0917: String autop = (String) p.get(context, dv.name
0918: + ".auto");
0919: // if it is automatic, pre-initialize it
0920: if (autop != null
0921: && Boolean.valueOf(autop).booleanValue()) {
0922: println(out, " " + dv.name + " = new " + dv.type
0923: + "(this);");
0924: }
0925: }
0926: println(out, " }");
0927: println(out);
0928:
0929: boolean timephased = isTimePhased(context);
0930: if (timephased) {
0931: //handle time phased support separately because getters != setters
0932: println(out, NewTimeSpanText);
0933: }
0934:
0935: // declare the slots (including "inherited" ones)
0936: println(out, " // Slots\n");
0937: List<String> slotspecs = getAllSlotSpecs(context);
0938: for (String slotspec : slotspecs) {
0939: int s = slotspec.indexOf(" ");
0940: String type = slotspec.substring(0, s);
0941: String name = slotspec.substring(s + 1);
0942: ActiveSlot as = parseActiveSlot(slotspec);
0943: if (as != null) {
0944: name = as.name;
0945: }
0946: String etype = null;
0947: CollectionType ct = parseCollectionType(type);
0948: if (ct != null) {
0949: type = ct.ctype;
0950: etype = ct.etype;
0951: }
0952:
0953: String var = (String) p.get(context, name + ".var");
0954: if (var == null) {
0955: var = "the" + toClassName(name);
0956: } // set the default
0957: if (var.equals("")) {
0958: var = null;
0959: } // unset if specified as empty
0960:
0961: String getter = (String) p.get(context, name
0962: + ".getter");
0963: String setter = (String) p.get(context, name
0964: + ".setter");
0965:
0966: if (as != null) {
0967: // active slot - write dispatchers
0968: String htype = as.handlerName();
0969: var = "the" + htype;
0970:
0971: // storage for the handler
0972: println(out, " private transient " + className
0973: + "." + htype + " " + var + " = null;");
0974:
0975: // handler installation
0976: println(out, " public void set" + htype + "("
0977: + className + "." + htype + " handler) {\n"
0978: + " " + var + " = handler;\n" + " }");
0979: println(out, " public " + className + "." + htype
0980: + " get" + htype + "() {\n" + " return "
0981: + var + ";\n" + " }");
0982:
0983: // get dispatcher
0984: if (getter != null) {
0985: println(out, getter);
0986: } else {
0987: println(out, " public " + type + " "
0988: + as.getter(var));
0989: }
0990:
0991: // set dispatcher
0992: if (setter != null) {
0993: println(out, setter);
0994: } else {
0995: println(out, " public void " + as.setter(var));
0996: }
0997:
0998: } else {
0999:
1000: // storage
1001: if (var != null) {
1002: String init = (String) p.get(context, name
1003: + ".init");
1004: print(out, " private " + type + " " + var);
1005: if (init != null) {
1006: print(out, " = new " + init + "()");
1007: }
1008: println(out, ";");
1009:
1010: // Normal getter
1011: if (getter != null) {
1012: println(out, getter);
1013: } else {
1014: println(out, " public " + type + " get"
1015: + toClassName(name) + "(){ "
1016: + "return " + var + "; }");
1017: }
1018:
1019: // test for Collection types
1020: if (etype != null) {
1021: String clname = toClassName(name);
1022: println(out, " public boolean in" + clname
1023: + "(" + etype + " _element) {\n"
1024: + " return (" + var
1025: + "==null)?false:(" + var
1026: + ".contains(_element));\n" + " }");
1027:
1028: println(out, " public " + etype + "[] get"
1029: + clname + "AsArray() {");
1030: println(out, " if (" + var
1031: + " == null) return new " + etype
1032: + "[0];");
1033: println(out, " int l = " + var
1034: + ".size();");
1035: println(out, " " + etype + "[] v = new "
1036: + etype + "[l];");
1037: println(out, " int i=0;");
1038: println(out, " for (Iterator n=" + var
1039: + ".iterator(); n.hasNext(); ) {");
1040: println(out, " v[i]=(" + etype
1041: + ") n.next();");
1042: println(out, " i++;");
1043: println(out, " }");
1044: println(out, " return v;");
1045: println(out, " }");
1046:
1047: println(out, " public " + etype
1048: + " getIndexed" + clname
1049: + "(int _index) {");
1050: println(out, " if (" + var
1051: + " == null) return null;");
1052: println(out, " for (Iterator _i = "
1053: + var
1054: + ".iterator(); _i.hasNext();) {");
1055: println(out, " " + etype + " _e = ("
1056: + etype + ") _i.next();");
1057: println(out,
1058: " if (_index == 0) return _e;");
1059: println(out, " _index--;");
1060: println(out, " }");
1061: println(out, " return null;");
1062: println(out, " }");
1063: }
1064:
1065: // setter
1066: if (setter != null) {
1067: println(out, setter);
1068: } else {
1069: println(out, " public void set"
1070: + toClassName(name) + "(" + type
1071: + " " + name + ") {");
1072: // special handling for string setters
1073: if (type.equals("String")) {
1074: String isInternable = p.get(context,
1075: name + ".intern");
1076: if (isInternable != null
1077: && isInternable.equals("true")) {
1078: println(out, " if (" + name
1079: + "!=null) " + name + "="
1080: + name + ".intern();");
1081: }
1082: }
1083: println(out, " " + var + "=" + name
1084: + ";");
1085: println(out, " }");
1086: }
1087:
1088: // mutators for collection types
1089: if (etype != null) {
1090: println(out, " public void clear"
1091: + toClassName(name) + "() {\n"
1092: + " " + var + ".clear();\n"
1093: + " }");
1094: println(out, " public boolean removeFrom"
1095: + toClassName(name) + "(" + etype
1096: + " _element) {\n" + " return "
1097: + var + ".remove(_element);\n"
1098: + " }");
1099: println(out, " public boolean addTo"
1100: + toClassName(name) + "(" + etype
1101: + " _element) {\n" + " return "
1102: + var + ".add(_element);\n" + " }");
1103: }
1104:
1105: } else { // var is specified as empty
1106: if (getter != null)
1107: println(out, getter);
1108: if (setter != null)
1109: println(out, setter);
1110: }
1111: }
1112: }
1113:
1114: // delgates
1115: {
1116: println(out);
1117: for (Argument dv : getAllDelegateSpecs(context)) {
1118: println(out, " private " + dv.type + " " + dv.name
1119: + " = null;");
1120:
1121: // define delegate getter and setter if non-automatic
1122: String autop = (String) p.get(context, dv.name
1123: + ".auto");
1124: // if it isn't automatic, define the setter and getter
1125: if (!(autop != null && Boolean.valueOf(autop)
1126: .booleanValue())) {
1127: println(out, " public " + dv.type + " get"
1128: + toClassName(dv.name) + "() {\n"
1129: + " return " + dv.name + ";\n"
1130: + " }");
1131: println(
1132: out,
1133: " public void set"
1134: + toClassName(dv.name)
1135: + "("
1136: + dv.type
1137: + " _"
1138: + dv.name
1139: + ") {\n"
1140: + " if ("
1141: + dv.name
1142: + " != null) throw new IllegalArgumentException(\""
1143: + dv.name
1144: + " already set\");\n" + " "
1145: + dv.name + " = _" + dv.name
1146: + ";\n" + " }");
1147: }
1148:
1149: for (DelegateSpec ds : parseDelegateSpecs(p.get(
1150: context, dv.name + ".delegate"))) {
1151: println(out, " public "
1152: + ds.type
1153: + " "
1154: + ds.name
1155: + "("
1156: + unparseArguments(ds.args, true)
1157: + ") {"
1158: + " "
1159: + ("void".equals(ds.type) ? ""
1160: : "return ") + dv.name + "."
1161: + ds.name + "("
1162: + unparseArguments(ds.args, false)
1163: + ");" + " }");
1164: }
1165: }
1166: }
1167:
1168: // copy constructor
1169: println(out);
1170: println(out, " public " + implclassName + "(" + className
1171: + " original) {");
1172:
1173: if (timephased) {
1174: // copy TimeSpan info
1175: println(out,
1176: " setTimeSpan(original.getStartTime(), original.getEndTime());");
1177: }
1178:
1179: // Copy slot info
1180: for (String slotspec : slotspecs) {
1181: int s = slotspec.indexOf(" ");
1182: String type = slotspec.substring(0, s);
1183: String name = slotspec.substring(s + 1);
1184: ActiveSlot as = parseActiveSlot(slotspec);
1185: if (as != null) {
1186: name = as.name;
1187: }
1188: CollectionType ct = parseCollectionType(type);
1189: if (ct != null) {
1190: type = ct.ctype;
1191: }
1192:
1193: if (as != null) {
1194: // do nothing - clones don't get the handler automatically.
1195: } else {
1196: String var = (String) p.get(context, name + ".var");
1197: if (var == null) {
1198: var = "the" + toClassName(name);
1199: } // set the default
1200: if (var.equals("")) {
1201: var = null;
1202: } // unset if specified as empty
1203: if (var != null) {
1204: println(out, " " + var + " = original.get"
1205: + toClassName(name) + "();");
1206: }
1207: }
1208: }
1209: println(out, " }");
1210: println(out);
1211:
1212: // .equals
1213: writeEquals(out, context, className);
1214:
1215: if (hasDQ(context)) {
1216: println(out,
1217: " public boolean hasDataQuality() { return false; }");
1218: println(
1219: out,
1220: " public org.cougaar.planning.ldm.dq.DataQuality getDataQuality() { return null; }");
1221: /*
1222: // classes without runtime support do not implement NewHasDataQuality!
1223: println(out," public void setDataQuality(org.cougaar.planning.ldm.dq.DataQuality dq) {\n"+
1224: " throw new IllegalArgumentException(\"This instance does not support setting of DataQuality.\");\n"+
1225: " }");
1226: */
1227: println(out);
1228: println(out,
1229: " // static inner extension class for real DataQuality Support");
1230: println(
1231: out,
1232: " public final static class DQ extends "
1233: + className
1234: + "Impl implements org.cougaar.planning.ldm.dq.NewHasDataQuality {");
1235: println(out, " public DQ() {\n" + // never copy data quality
1236: " super();\n" + " }");
1237:
1238: println(out, " public DQ(" + className
1239: + " original) {\n" + // never copy data quality
1240: " super(original);\n" + " }");
1241: println(out,
1242: " public Object clone() { return new DQ(this); }");
1243: println(out,
1244: " private transient org.cougaar.planning.ldm.dq.DataQuality _dq = null;");
1245: println(out,
1246: " public boolean hasDataQuality() { return (_dq!=null); }");
1247: println(
1248: out,
1249: " public org.cougaar.planning.ldm.dq.DataQuality getDataQuality() { return _dq; }");
1250: println(
1251: out,
1252: " public void setDataQuality(org.cougaar.planning.ldm.dq.DataQuality dq) { _dq=dq; }");
1253: println(
1254: out,
1255: " private void writeObject(ObjectOutputStream out) throws IOException {\n"
1256: + " out.defaultWriteObject();\n"
1257: + " if (out instanceof org.cougaar.core.persist.PersistenceOutputStream) out.writeObject(_dq);\n"
1258: + " }");
1259: println(
1260: out,
1261: " private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {\n"
1262: + " in.defaultReadObject();\n"
1263: + " if (in instanceof org.cougaar.core.persist.PersistenceInputStream) _dq=(org.cougaar.planning.ldm.dq.DataQuality)in.readObject();\n"
1264: + " }");
1265: println(out, " ");
1266: println(
1267: out,
1268: " private final static PropertyDescriptor properties[]=new PropertyDescriptor[1];\n"
1269: + " static {\n"
1270: + " try {\n"
1271: + " properties[0]= new PropertyDescriptor(\"dataQuality\", DQ.class, \"getDataQuality\", null);\n"
1272: + " } catch (Exception e) { e.printStackTrace(); }\n"
1273: + " }\n"
1274: + " public PropertyDescriptor[] getPropertyDescriptors() {\n"
1275: + " PropertyDescriptor[] pds = super.properties;\n"
1276: + " PropertyDescriptor[] ps = new PropertyDescriptor[pds.length+properties.length];\n"
1277: + " System.arraycopy(pds, 0, ps, 0, pds.length);\n"
1278: + " System.arraycopy(properties, 0, ps, pds.length, properties.length);\n"
1279: + " return ps;\n" + " }");
1280: println(out, " }");
1281: println(out);
1282:
1283: } else {
1284: println(out,
1285: " public final boolean hasDataQuality() { return false; }");
1286: }
1287: println(out);
1288:
1289: // lock and unlock methods
1290: println(out, " private transient " + className
1291: + " _locked = null;");
1292: println(out, " public PropertyGroup lock(Object key) {");
1293: println(out,
1294: " if (_locked == null)_locked = new _Locked(key);");
1295: println(out, " return _locked; }");
1296: println(out, " public PropertyGroup lock() "
1297: + "{ return lock(null); }");
1298: println(out,
1299: " public NewPropertyGroup unlock(Object key) "
1300: + "{ return this; }");
1301: println(out);
1302:
1303: // clone method
1304: writeCloneMethod(out, context, implclassName, " ");
1305: println(out);
1306:
1307: println(
1308: out,
1309: " public PropertyGroup copy() {\n"
1310: + " try {\n"
1311: + " return (PropertyGroup) clone();\n"
1312: + " } catch (CloneNotSupportedException cnse) { return null;}\n"
1313: + " }");
1314: println(out);
1315:
1316: // introspection methods
1317: println(out, " public Class getPrimaryClass() {\n"
1318: + " return primaryClass;\n" + " }");
1319: println(out, " public String getAssetGetMethod() {\n"
1320: + " return assetGetter;\n" + " }");
1321: println(out, " public String getAssetSetMethod() {\n"
1322: + " return assetSetter;\n" + " }");
1323: println(out);
1324:
1325: // Serialization
1326: boolean needSerialization = false;
1327: for (String slotspec : slotspecs) {
1328: int s = slotspec.indexOf(" ");
1329: String type = slotspec.substring(0, s);
1330: String name = slotspec.substring(s + 1);
1331: ActiveSlot as = parseActiveSlot(slotspec);
1332: if (as != null) {
1333: name = as.name;
1334: }
1335: CollectionType ct = parseCollectionType(type);
1336: if (ct != null) {
1337: type = ct.ctype;
1338: }
1339:
1340: if (type.equals("String")) {
1341: String isInternable = p.get(context, name
1342: + ".intern");
1343: if (isInternable != null
1344: && isInternable.equals("true")) {
1345: needSerialization = true;
1346: break;
1347: }
1348: }
1349: }
1350:
1351: if (needSerialization) {
1352: println(
1353: out,
1354: " private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {");
1355: println(out, " in.defaultReadObject();");
1356: for (String slotspec : slotspecs) {
1357: int s = slotspec.indexOf(" ");
1358: String type = slotspec.substring(0, s);
1359: String name = slotspec.substring(s + 1);
1360: ActiveSlot as = parseActiveSlot(slotspec);
1361: if (as != null) {
1362: name = as.name;
1363: }
1364: CollectionType ct = parseCollectionType(type);
1365: if (ct != null) {
1366: type = ct.ctype;
1367: }
1368:
1369: if (as != null) {
1370: // serialized copies can't get the active slot remotely.
1371: continue;
1372: }
1373: String var = (String) p.get(context, name + ".var");
1374: if (var == null) {
1375: var = "the" + toClassName(name);
1376: } // set the default
1377: if (var.equals("")) {
1378: var = null;
1379: } // unset if specified as empty
1380: if (var != null) {
1381: if (type.equals("String")) {
1382: String isInternable = p.get(context, name
1383: + ".intern");
1384: if (isInternable != null
1385: && isInternable.equals("true")) {
1386: println(out, " if (" + var
1387: + "!= null) " + var + "=" + var
1388: + ".intern();");
1389: }
1390: }
1391: }
1392: }
1393:
1394: println(out, " }");
1395: println(out);
1396: }
1397:
1398: writeBeanInfoBody(out, context, className);
1399:
1400: // inner (locked) class
1401: println(out,
1402: " private final class _Locked extends java.beans.SimpleBeanInfo\n"
1403: + " implements " + className
1404: + ", Cloneable, LockedPG\n" + " {");
1405: println(out, " private transient Object theKey = null;");
1406: println(out, " _Locked(Object key) { ");
1407: println(out,
1408: " if (this.theKey == null) this.theKey = key;");
1409: println(out, " } ");
1410: println(out);
1411: println(out, " public _Locked() {}");
1412: println(out);
1413: println(out,
1414: " public PropertyGroup lock() { return this; }");
1415: println(out,
1416: " public PropertyGroup lock(Object o) { return this; }");
1417: println(out);
1418: println(out,
1419: " public NewPropertyGroup unlock(Object key) "
1420: + "throws IllegalAccessException {");
1421: println(out, " if( theKey.equals(key) ) {");
1422: println(out, " return " + implclassName + ".this;");
1423: println(out, " } else {");
1424: println(
1425: out,
1426: " throw new IllegalAccessException(\"unlock: mismatched internal and provided keys!\");");
1427: println(out, " }");
1428: println(out, " }");
1429: println(out);
1430: /*
1431: println(out," public PropertyGroup copy() {\n"+
1432: " return new "+implclassName+"("+implclassName+".this);\n"+
1433: " }");
1434: */
1435: println(
1436: out,
1437: " public PropertyGroup copy() {\n"
1438: + " try {\n"
1439: + " return (PropertyGroup) clone();\n"
1440: + " } catch (CloneNotSupportedException cnse) { return null;}\n"
1441: + " }");
1442: println(out);
1443:
1444: println(out);
1445:
1446: writeCloneMethod(out, context, implclassName, " ");
1447: println(out);
1448:
1449: if (timephased) {
1450: println(out, " public long getStartTime() { return "
1451: + implclassName + ".this.getStartTime(); }");
1452: println(out, " public long getEndTime() { return "
1453: + implclassName + ".this.getEndTime(); }");
1454: }
1455:
1456: println(out,
1457: " public boolean equals(Object object) { return "
1458: + implclassName + ".this.equals(object); }");
1459:
1460: // dispatch getters
1461: for (String slotspec : slotspecs) {
1462: int s = slotspec.indexOf(" ");
1463: String type = slotspec.substring(0, s);
1464: String name = slotspec.substring(s + 1);
1465: ActiveSlot as = parseActiveSlot(slotspec);
1466: if (as != null) {
1467: name = as.name;
1468: }
1469: String etype = null;
1470: CollectionType ct = parseCollectionType(type);
1471: if (ct != null) {
1472: type = ct.ctype;
1473: etype = ct.etype;
1474: }
1475:
1476: String getter = "get" + toClassName(name);
1477: if (as != null) {
1478: println(out, " public " + as.rtype + " "
1479: + as.getterSpec() + " {\n"
1480: + " return " + implclassName
1481: + ".this." + as.getterCall() + ";\n"
1482: + " }");
1483: } else {
1484: println(out, " public " + type + " " + getter
1485: + "() { " + "return " + implclassName
1486: + ".this." + getter + "(); }");
1487: }
1488:
1489: // test for Collection types
1490: if (etype != null) {
1491: String clname = toClassName(name);
1492: String ref = implclassName + ".this.";
1493: println(out, " public boolean in" + clname + "("
1494: + etype + " _element) {\n" + " return "
1495: + ref + "in" + clname + "(_element);\n"
1496: + " }");
1497: println(out, " public " + etype + "[] get"
1498: + clname + "AsArray() {");
1499: println(out, " return " + ref + "get" + clname
1500: + "AsArray();");
1501: println(out, " }");
1502:
1503: println(out, " public " + etype + " getIndexed"
1504: + clname + "(int _index) {");
1505: println(out, " return " + ref + "getIndexed"
1506: + clname + "(_index);");
1507: println(out, " }");
1508: }
1509:
1510: }
1511:
1512: {
1513: for (Argument dv : getAllDelegateSpecs(context)) {
1514: for (DelegateSpec ds : parseDelegateSpecs(p.get(
1515: context, dv.name + ".delegate"))) {
1516: println(out, " public "
1517: + ds.type
1518: + " "
1519: + ds.name
1520: + "("
1521: + unparseArguments(ds.args, true)
1522: + ") {\n"
1523: + " "
1524: + ("void".equals(ds.type) ? ""
1525: : "return ") + implclassName
1526: + ".this." + ds.name + "("
1527: + unparseArguments(ds.args, false)
1528: + ");\n" + " }");
1529: }
1530: }
1531: }
1532:
1533: if (hasDQ(context)) {
1534: println(out,
1535: " public final boolean hasDataQuality() { return "
1536: + implclassName
1537: + ".this.hasDataQuality(); }");
1538: println(
1539: out,
1540: " public final org.cougaar.planning.ldm.dq.DataQuality getDataQuality() { return "
1541: + implclassName
1542: + ".this.getDataQuality(); }");
1543: } else {
1544: println(out,
1545: " public final boolean hasDataQuality() { return false; }");
1546: }
1547:
1548: // introspection method of locked
1549: println(out, " public Class getPrimaryClass() {\n"
1550: + " return primaryClass;\n" + " }");
1551: println(out, " public String getAssetGetMethod() {\n"
1552: + " return assetGetter;\n" + " }");
1553: println(out, " public String getAssetSetMethod() {\n"
1554: + " return assetSetter;\n" + " }");
1555: println(out);
1556:
1557: // BeanInfo dispatchers
1558: println(out,
1559: " public PropertyDescriptor[] getPropertyDescriptors() {");
1560: println(out, " return properties;");
1561: println(out, " }");
1562: println(out);
1563: println(out, " public Class getIntrospectionClass() {");
1564: println(out, " return " + className + "Impl.class;");
1565: println(out, " }");
1566: println(out);
1567:
1568: println(out, " }");
1569: println(out);
1570:
1571: println(out, "}");
1572: }
1573:
1574: protected void writeCloneMethod(PrintWriter out,
1575: String context, String implclassName,
1576: String leadingSpaces) {
1577: println(
1578: out,
1579: leadingSpaces
1580: + "public Object clone() throws CloneNotSupportedException {");
1581: List<Argument> delegateSpecs = getAllDelegateSpecs(context);
1582: if (delegateSpecs.size() == 0) {
1583: println(out, leadingSpaces + " return new "
1584: + implclassName + "(" + implclassName
1585: + ".this);");
1586: } else {
1587: println(out, leadingSpaces + " " + implclassName
1588: + " _tmp = new " + implclassName + "(this);");
1589: for (Argument delegate : delegateSpecs) {
1590: println(out, leadingSpaces + " if ("
1591: + delegate.name + " != null) {");
1592: println(out, leadingSpaces + " _tmp."
1593: + delegate.name + " = (" + delegate.type
1594: + ") " + delegate.name + ".copy(_tmp);");
1595: println(out, leadingSpaces + " }");
1596: }
1597: println(out, leadingSpaces + " return _tmp;");
1598: }
1599: println(out, leadingSpaces + "}");
1600: }
1601:
1602: void writeBeanInfoBody(PrintWriter out, String context,
1603: String className) {
1604:
1605: List<String> slotspecs = getAllSlotSpecs(context);
1606:
1607: // Add in time phased slots
1608: if (isTimePhased(context)) {
1609: slotspecs.add("long start_time");
1610: slotspecs.add("long end_time");
1611: }
1612:
1613: // figure out how many property slots we need
1614: int l = 0; // props
1615: int asc = 0; // methods
1616: int cc = 0; // collections (not used)
1617: for (String slotspec : slotspecs) {
1618: int s = slotspec.indexOf(" ");
1619: String type = slotspec.substring(0, s);
1620: ActiveSlot as = parseActiveSlot(slotspec);
1621: // count the different slot types
1622: if (as == null) {
1623: CollectionType ct = parseCollectionType(type);
1624: if (ct != null) {
1625: cc++;
1626: } else {
1627: l++;
1628: }
1629: } else {
1630: asc++;
1631: }
1632: }
1633:
1634: println(
1635: out,
1636: " private final static PropertyDescriptor properties[] = new PropertyDescriptor["
1637: + (l + cc) + "];");
1638: if ((l + cc) > 0) {
1639: println(out, " static {");
1640: println(out, " try {");
1641: int i = 0;
1642: String clname = toClassName(context);
1643:
1644: for (String slotspec : slotspecs) {
1645: int s = slotspec.indexOf(" ");
1646: String type = slotspec.substring(0, s);
1647: String name = slotspec.substring(s + 1);
1648: ActiveSlot as = parseActiveSlot(slotspec);
1649: if (as != null) {
1650: name = as.name;
1651: }
1652: CollectionType ct = parseCollectionType(type);
1653: if (ct != null) {
1654: type = ct.ctype;
1655: }
1656:
1657: if (as == null) {
1658: // non-active slots
1659: if (ct == null) {
1660: // plain slots
1661: println(out, " properties[" + i
1662: + "]= new PropertyDescriptor(\""
1663: + name + "\", " + clname
1664: + ".class, " + "\"get"
1665: + toClassName(name) + "\", null);");
1666: } else {
1667: // collection slots
1668: println(
1669: out,
1670: " properties["
1671: + i
1672: + "]= new IndexedPropertyDescriptor(\""
1673: + name + "\", " + clname
1674: + ".class, " + "\"get"
1675: + toClassName(name)
1676: + "AsArray\", null, "
1677: + "\"getIndexed"
1678: + toClassName(name)
1679: + "\", null);");
1680: }
1681: i++;
1682: }
1683: }
1684: println(
1685: out,
1686: " } catch (Exception e) { \n"
1687: + " org.cougaar.util.log.Logging.getLogger("
1688: + clname
1689: + ".class).error(\"Caught exception\",e);\n"
1690: + " }");
1691: println(out, " }");
1692: }
1693: println(out);
1694: println(out,
1695: " public PropertyDescriptor[] getPropertyDescriptors() {");
1696: println(out, " return properties;");
1697: println(out, " }");
1698: }
1699:
1700: protected void writeEquals(PrintWriter out, String context,
1701: String className) {
1702:
1703: if (delegatesEquals(context)) {
1704: return;
1705: }
1706:
1707: String otherVar = "other" + className;
1708:
1709: println(out, " public boolean equals(Object other) {");
1710: println(out);
1711: println(out, " if (!(other instanceof " + className
1712: + ")) {");
1713: println(out, " return false;");
1714: println(out, " }");
1715: println(out);
1716: println(out, " " + className + " " + otherVar + " = ("
1717: + className + ") other;");
1718: println(out);
1719:
1720: String implClassName = className + "Impl";
1721:
1722: for (String slotspec : getAllSlotSpecs(context)) {
1723: int s = slotspec.indexOf(" ");
1724: String type = slotspec.substring(0, s);
1725: String name = slotspec.substring(s + 1);
1726: ActiveSlot as = parseActiveSlot(slotspec);
1727:
1728: String getter;
1729:
1730: if (as != null) {
1731: // Limit comparison to handler's .equals()
1732: getter = "get" + as.handlerName() + "()";
1733: type = as.handlerName();
1734: } else {
1735: String var = (String) p.get(context, name + ".var");
1736: if (var == null) {
1737: var = "the" + toClassName(name);
1738: } // set the default
1739: if (var.equals("")) {
1740: var = null;
1741: } // unset if specified as empty
1742:
1743: getter = (String) p.get(context, name + ".getter");
1744:
1745: if (getter == null) {
1746: getter = "get" + toClassName(name) + "()";
1747: }
1748: }
1749:
1750: // Does method return an object or a primitive data type?
1751: if (primitiveDataType(type)) {
1752: println(out, " if (!(" + getter + " == "
1753: + otherVar + "." + getter + ")) {");
1754: println(out, " return false;");
1755: println(out, " }");
1756: println(out);
1757: } else if (as != null) {
1758: println(out, " if (other instanceof "
1759: + implClassName + ") {");
1760: println(out, " if (" + getter + " == null) {");
1761: println(out, " if (((" + implClassName
1762: + ") " + otherVar + ")." + getter
1763: + " != null) {");
1764: println(out, " return false;");
1765: println(out, " }");
1766: println(out, " } else if (!(" + getter
1767: + ".equals(((" + implClassName + ") "
1768: + otherVar + ")." + getter + "))) {");
1769: println(out, " return false;");
1770: println(out, " }");
1771: println(out, " }");
1772: println(out);
1773: } else {
1774: println(out, " if (" + getter + " == null) {");
1775: println(out, " if (" + otherVar + "." + getter
1776: + " != null) {");
1777: println(out, " return false;");
1778: println(out, " }");
1779: println(out, " } else if (!(" + getter
1780: + ".equals(" + otherVar + "." + getter
1781: + "))) {");
1782: println(out, " return false;");
1783: println(out, " }");
1784: println(out);
1785: }
1786: }
1787:
1788: // Call .equals for all delegates
1789: List<Argument> vs = getAllDelegateSpecs(context);
1790: //BG only accessible from impl class so can't compare _Locked and Impl
1791: if (vs.size() > 0) {
1792: println(out, " if (other instanceof "
1793: + implClassName + ") {");
1794: for (Argument dv : vs) {
1795: String getter = "get" + toClassName(dv.name) + "()";
1796: println(out, " if (" + getter + " == null) {");
1797: println(out, " if (((" + implClassName
1798: + ") " + otherVar + ")." + getter
1799: + " != null) {");
1800: println(out, " return false;");
1801: println(out, " }");
1802: println(out, " } else if (!(" + getter
1803: + ".equals(((" + implClassName + ") "
1804: + otherVar + ")." + getter + "))) {");
1805: println(out, " return false;");
1806: println(out, " }");
1807: println(out);
1808: }
1809: println(out, " }");
1810: }
1811:
1812: println(out, " return true;");
1813: println(out, " }");
1814: println(out);
1815: }
1816:
1817: void writePropertyGroup(String context) throws Exception {
1818: String className = toClassName(context);
1819:
1820: FileOutputStream fos;
1821: OutputStreamWriter osw;
1822: PrintWriter out;
1823:
1824: if (cleanp) {
1825: (new File(className.toString() + ".java")).delete();
1826: (new File("New" + className.toString() + ".java"))
1827: .delete();
1828: (new File(className.toString() + "Impl.java")).delete();
1829: (new File(className.toString() + "BeanInfo.java"))
1830: .delete();
1831: return;
1832: }
1833:
1834: String outname = className.toString() + ".java";
1835: if (writeInterfaces) {
1836: debug("Writing GetterIfc \"" + context + "\" to \""
1837: + outname + "\"");
1838: noteFile(outname);
1839: fos = new FileOutputStream(new File(getTargetDir(),
1840: outname));
1841: osw = new OutputStreamWriter(fos);
1842: out = new PrintWriter(osw);
1843: writeCR(out, deffilename);
1844: writeGetterIfc(out, context, className);
1845: out.close();
1846:
1847: outname = "New" + className.toString() + ".java";
1848: debug("Writing SetterIfc \"" + context + "\" to \""
1849: + outname + "\"");
1850: noteFile(outname);
1851: fos = new FileOutputStream(new File(getTargetDir(),
1852: outname));
1853: osw = new OutputStreamWriter(fos);
1854: out = new PrintWriter(osw);
1855: writeCR(out, deffilename);
1856: writeSetterIfc(out, context, className);
1857: out.close();
1858: }
1859:
1860: outname = className.toString() + "Impl.java";
1861: debug("Writing Impl \"" + context + "\" to \"" + outname
1862: + "\"");
1863: noteFile(outname);
1864: fos = new FileOutputStream(
1865: new File(getTargetDir(), outname));
1866: osw = new OutputStreamWriter(fos);
1867: out = new PrintWriter(osw);
1868: writeCR(out, deffilename);
1869: writeImpl(out, context, className);
1870: out.close();
1871:
1872: }
1873:
1874: public void writeFactory() throws IOException {
1875: String outname = "PropertyGroupFactory.java";
1876:
1877: if (cleanp) {
1878: (new File(outname)).delete();
1879: return;
1880: }
1881:
1882: debug("Writing FactoryImplementation to \"" + outname
1883: + "\"");
1884: noteFile(outname);
1885: FileOutputStream fos = new FileOutputStream(new File(
1886: getTargetDir(), outname));
1887: OutputStreamWriter osw = new OutputStreamWriter(fos);
1888: PrintWriter out = new PrintWriter(osw);
1889: writeCR(out, deffilename);
1890:
1891: println(out,
1892: "/** AbstractFactory implementation for Properties.");
1893: println(out,
1894: " * Prevents clients from needing to know the implementation");
1895: println(out, " * class(es) of any of the properties.");
1896: println(out, " **/");
1897: println(out);
1898: doPackaging(out, "global");
1899:
1900: println(out);
1901: String xclause = "";
1902: String exts = p.get("global", "factoryExtends");
1903: if (exts != null) {
1904: xclause = "extends " + exts + " ";
1905: }
1906: println(out, "public class PropertyGroupFactory " + xclause
1907: + "{");
1908:
1909: for (String context : p.getContexts()) {
1910: if (!context.equals("global") && isPrimary(context)
1911: && (p.get(context, "abstract") == null)) {
1912: String newclassName = "New" + context;
1913: String icn = context + "Impl";
1914: println(out, " // brand-new instance factory");
1915: println(out, " public static " + newclassName
1916: + " new" + context + "() {");
1917: println(out, " return new " + icn + "();");
1918: println(out, " }");
1919:
1920: boolean timephased = isTimePhased(context);
1921: if (timephased) {
1922: println(out, " // brand-new instance factory");
1923: println(out,
1924: " public static PropertyGroupSchedule new"
1925: + context + "Schedule() {");
1926: println(out,
1927: " return new PropertyGroupSchedule(new"
1928: + context + "());");
1929: println(out, " }");
1930: }
1931:
1932: println(out, " // instance from prototype factory");
1933: println(out, " public static " + newclassName
1934: + " new" + context + "(" + context
1935: + " prototype) {");
1936: println(out, " return new " + icn
1937: + "(prototype);");
1938: println(out, " }");
1939: println(out);
1940:
1941: if (timephased) {
1942: println(out,
1943: " // instance from prototype factory");
1944: println(out,
1945: " public static PropertyGroupSchedule new"
1946: + context + "Schedule("
1947: + context + " prototype) {");
1948: println(out,
1949: " return new PropertyGroupSchedule(new"
1950: + context + "(prototype));");
1951: println(out, " }");
1952: println(out);
1953:
1954: println(out,
1955: " // instance from prototype schedule");
1956: println(
1957: out,
1958: " public static PropertyGroupSchedule new"
1959: + context
1960: + "Schedule(PropertyGroupSchedule prototypeSchedule) {");
1961: println(out,
1962: " if (!prototypeSchedule.getPGClass().equals("
1963: + context + ".class)) {");
1964: println(
1965: out,
1966: " throw new IllegalArgumentException(\"new"
1967: + context
1968: + "Schedule requires that getPGClass() on the PropertyGroupSchedule argument return "
1969: + context + ".class\");");
1970: println(out, " }");
1971: println(out,
1972: " return new PropertyGroupSchedule(prototypeSchedule);");
1973: println(out, " }");
1974: println(out);
1975: }
1976: }
1977: }
1978: println(out, " /** Abstract introspection information.");
1979: println(out,
1980: " * Tuples are {<classname>, <factorymethodname>}");
1981: println(out,
1982: " * return value of <factorymethodname> is <classname>.");
1983: println(out,
1984: " * <factorymethodname> takes zero or one (prototype) argument.");
1985: println(out, " **/");
1986: println(out, " public static String properties[][]={");
1987: for (String context : p.getContexts()) {
1988: if (!context.equals("global") && isPrimary(context)
1989: && (p.get(context, "abstract") == null)) {
1990: String pkg = findPackage(context);
1991: print(out, " {\"" + pkg + "." + context
1992: + "\", \"new" + context + "\"}");
1993: if (isTimePhased(context)) {
1994: print(out, ",");
1995: println(out);
1996: print(out,
1997: " {\"org.cougaar.planning.ldm.asset.PropertyGroupSchedule\", \"new"
1998: + context + "Schedule\"}");
1999: }
2000: println(out, ",");
2001: }
2002: }
2003: println(out, " };");
2004:
2005: println(out, "}");
2006: out.close();
2007: }
2008:
2009: protected boolean delegatesEquals(String context) {
2010: for (Argument dv : getAllDelegateSpecs(context)) {
2011: for (DelegateSpec ds : parseDelegateSpecs(p.get(
2012: context, dv.name + ".delegate"))) {
2013: if ((ds.type.equals("boolean"))
2014: && (ds.name.equals("equals"))
2015: && (ds.args.size() == 1)) {
2016: Argument arg = (Argument) ds.args.get(0);
2017: if (arg.type.equals("Object")) {
2018: return true;
2019: } else {
2020: return false;
2021: }
2022: }
2023: }
2024: }
2025: return false;
2026: }
2027:
2028: protected boolean primitiveDataType(String type) {
2029: return ((type != null) && ((type.equals("boolean"))
2030: || (type.equals("char")) || (type.equals("byte"))
2031: || (type.equals("short")) || (type.equals("int"))
2032: || (type.equals("long")) || (type.equals("float")) || (type
2033: .equals("double"))));
2034: }
2035:
2036: protected class CollectionType {
2037: public String ctype;
2038: public String etype;
2039:
2040: public CollectionType(String ct, String et) {
2041: ctype = ct;
2042: etype = et;
2043: }
2044: }
2045:
2046: protected CollectionType parseCollectionType(String typestring) {
2047: int es = typestring.indexOf("<");
2048: int ee = typestring.indexOf(">");
2049: if (ee > es && es >= 0) {
2050: String ctype = typestring.substring(0, es).trim();
2051: String etype = typestring.substring(es + 1, ee);
2052: return new CollectionType(ctype, etype);
2053: } else {
2054: return null;
2055: }
2056: }
2057:
2058: protected class ActiveSlot {
2059: public String rtype;
2060: public String name;
2061: public String[] arguments;
2062: public String[] types;
2063:
2064: public ActiveSlot(String rtype, String name,
2065: Object[] arguments, Object[] types) {
2066: this .rtype = rtype;
2067: this .name = name;
2068: int l = arguments.length;
2069: this .arguments = new String[l];
2070: this .types = new String[l];
2071: for (int i = 0; i < l; i++) {
2072: this .arguments[i] = (String) arguments[i];
2073: this .types[i] = (String) types[i];
2074: }
2075: }
2076:
2077: public String getterSpec() {
2078: String s = "get" + toClassName(name) + "(";
2079: s = s + typedarglist() + ")";
2080: return s;
2081: }
2082:
2083: public String getterCall() {
2084: return "get" + toClassName(name) + "(" + arglist()
2085: + ")";
2086: }
2087:
2088: public String getter(String var) {
2089: String s = getterSpec();
2090: s = s
2091: + " {\n"
2092: + " if ("
2093: + var
2094: + "==null) throw new UndefinedValueException();\n"
2095: + " return " + var + ".get"
2096: + toClassName(name) + "(";
2097: s = s + arglist() + ");\n }";
2098: return s;
2099: }
2100:
2101: public String setterSpec() {
2102: String s = "set" + toClassName(name) + "(" + rtype
2103: + " _value";
2104: if (arguments.length > 0)
2105: s = s + ", ";
2106: s = s + typedarglist() + ")";
2107: return s;
2108: }
2109:
2110: public String setter(String var) {
2111: String s = setterSpec();
2112: s = s
2113: + " {\n"
2114: + " if ("
2115: + var
2116: + "==null) throw new UndefinedValueException();\n"
2117: + " " + var + ".set" + toClassName(name)
2118: + "(_value";
2119: if (arguments.length > 0)
2120: s = s + ", ";
2121: s = s + arglist() + ");\n }";
2122: return s;
2123: }
2124:
2125: public String arglist() {
2126: String s = "";
2127: for (int i = 0; i < arguments.length; i++) {
2128: if (i != 0)
2129: s = s + ", ";
2130: s = s + arguments[i];
2131: }
2132: return s;
2133: }
2134:
2135: public String typedarglist() {
2136: String s = "";
2137: for (int i = 0; i < arguments.length; i++) {
2138: if (i != 0)
2139: s = s + ", ";
2140: s = s + types[i] + " " + arguments[i];
2141: }
2142: return s;
2143: }
2144:
2145: public String handlerName() {
2146: return toClassName(name) + "Handler";
2147: }
2148:
2149: public String handlerClassDef() {
2150: // BIZARRE - We apparently need the "public static"
2151: return " public static interface " + handlerName()
2152: + " {\n" + " " + rtype + " " + getterSpec()
2153: + ";\n" + " void " + setterSpec() + ";\n"
2154: + " }";
2155: }
2156: }
2157:
2158: protected ActiveSlot parseActiveSlot(String slotd) {
2159: int sp = slotd.indexOf(" ");
2160: String rtype = slotd.substring(0, sp).trim();
2161: String slotname = slotd.substring(sp + 1).trim();
2162: // search for foo(int x, int y)
2163: int es = slotname.indexOf("(");
2164: int ee = slotname.indexOf(")");
2165: if (!(ee > es && es >= 0))
2166: return null; // not an active slot?
2167:
2168: String name = slotname.substring(0, es);
2169:
2170: List<String> args = new ArrayList<String>();
2171: List<String> types = new ArrayList<String>();
2172:
2173: for (String arg : explode(slotname.substring(es + 1, ee),
2174: ',')) {
2175: arg = arg.trim();
2176: sp = arg.indexOf(" ");
2177: if (sp < 0)
2178: throw new RuntimeException(
2179: "Broken active slot specification: "
2180: + slotname);
2181: String at = arg.substring(0, sp);
2182: types.add(at);
2183: String av = arg.substring(sp + 1).trim();
2184: args.add(av);
2185: }
2186: return new ActiveSlot(rtype, name, args.toArray(), types
2187: .toArray());
2188: }
2189:
2190: class Argument {
2191: String type;
2192: String name;
2193:
2194: public Argument(String t, String n) {
2195: type = t;
2196: name = n;
2197: }
2198:
2199: public String toString() {
2200: return type + " " + name;
2201: }
2202: }
2203:
2204: class DelegateSpec {
2205: String type;
2206: String name;
2207: List<Argument> args;
2208:
2209: public DelegateSpec(String t, String n, List<Argument> a) {
2210: type = t;
2211: name = n;
2212: args = a;
2213: }
2214:
2215: public String toString() {
2216: return type + " " + name + "(" + args + ")";
2217: }
2218: }
2219:
2220: public Argument parseArgument(String s) {
2221: s = s.trim();
2222: int p = s.indexOf(" ");
2223: return new Argument(s.substring(0, p).trim(), s.substring(
2224: p + 1).trim());
2225: }
2226:
2227: public String unparseArguments(List<Argument> args,
2228: boolean typesToo) {
2229: String s = "";
2230: for (Argument arg : args) {
2231: if (!s.equals(""))
2232: s = s + ", ";
2233: if (typesToo)
2234: s = s + arg.type + " ";
2235: s = s + arg.name;
2236: }
2237: return s;
2238: }
2239:
2240: public List<Argument> parseArguments(String s) {
2241: if (s == null)
2242: return new ArrayList<Argument>(0);
2243: List<String> argstrs = explode(s, ',');
2244: List<Argument> args = new ArrayList<Argument>(argstrs
2245: .size());
2246: for (String arg : argstrs) {
2247: args.add(parseArgument(arg));
2248: }
2249: return args;
2250: }
2251:
2252: public DelegateSpec parseDelegateSpec(String s) {
2253: s = s.trim();
2254: int p1 = s.indexOf(" ");
2255: int p2 = s.indexOf("(");
2256: int p3 = s.indexOf(")");
2257: String type = s.substring(0, p1).trim();
2258: String name = s.substring(p1 + 1, p2).trim();
2259: List<Argument> args = parseArguments(s
2260: .substring(p2 + 1, p3));
2261: return new DelegateSpec(type, name, args);
2262: }
2263:
2264: public List<DelegateSpec> parseDelegateSpecs(String s) {
2265: List<String> v = explode(s, ';');
2266: List<DelegateSpec> ds = new ArrayList<DelegateSpec>(v
2267: .size());
2268: for (String x : v) {
2269: x = x.trim();
2270: if (x.length() > 0) {
2271: ds.add(parseDelegateSpec(x));
2272: }
2273: }
2274: return ds;
2275: }
2276:
2277: public void writeAsset() throws IOException {
2278: String outname = "AssetSkeleton.java";
2279: if (cleanp) {
2280: (new File(outname)).delete();
2281: return;
2282: }
2283:
2284: debug("Writing AssetSkeleton to \"" + outname + "\"");
2285: noteFile(outname);
2286: FileOutputStream fos = new FileOutputStream(new File(
2287: getTargetDir(), outname));
2288: OutputStreamWriter osw = new OutputStreamWriter(fos);
2289: PrintWriter out = new PrintWriter(osw);
2290: writeCR(out, deffilename);
2291:
2292: println(out, "/** Abstract Asset Skeleton implementation");
2293: println(out,
2294: " * Implements default property getters, and additional property");
2295: println(out, " * lists.");
2296: println(out,
2297: " * Intended to be extended by org.cougaar.planning.ldm.asset.Asset");
2298: println(out, " **/");
2299: println(out);
2300: doPackaging(out, "global");
2301: println(out, "import java.io.Serializable;");
2302: println(out, "import java.beans.PropertyDescriptor;");
2303: println(out, "import java.beans.IndexedPropertyDescriptor;");
2304: println(out);
2305: String baseclass = p.get("global", "skeletonBase");
2306: if (baseclass == null)
2307: baseclass = "org.cougaar.planning.ldm.asset.Asset";
2308: println(out, "public abstract class AssetSkeleton extends "
2309: + baseclass + " {");
2310: println(out);
2311: // default constructor
2312: println(out, " protected AssetSkeleton() {}");
2313: println(out);
2314: // copy constructor
2315: println(out,
2316: " protected AssetSkeleton(AssetSkeleton prototype) {\n"
2317: + " super(prototype);\n" + " }");
2318: println(out);
2319:
2320: println(out,
2321: " /** Default PG accessors **/");
2322: println(out);
2323:
2324: for (String context : p.getContexts()) {
2325: if (!context.equals("global") && isPrimary(context)
2326: && (p.get(context, "abstract") == null)) {
2327: println(out,
2328: " /** Search additional properties for a "
2329: + context + " instance.");
2330: println(out, " * @return instance of " + context
2331: + " or null.");
2332: println(out, " **/");
2333:
2334: boolean timephased = isTimePhased(context);
2335: String timeVar = "";
2336:
2337: if (timephased) {
2338: timeVar = "long time";
2339: }
2340: println(out, " public " + context + " get"
2341: + context + "(" + timeVar + ")");
2342: println(out, " {");
2343:
2344: if (timephased) {
2345: timeVar = ", time";
2346: }
2347: println(out, " " + context + " _tmp = ("
2348: + context + ") resolvePG(" + context
2349: + ".class" + timeVar + ");");
2350: println(out, " return (_tmp==" + context
2351: + ".nullPG)?null:_tmp;");
2352: println(out, " }");
2353: println(out);
2354:
2355: // For timephased - get default
2356: if (timephased) {
2357: println(out, " public " + context + " get"
2358: + context + "()");
2359: println(out, " {");
2360: println(out,
2361: " PropertyGroupSchedule pgSchedule = get"
2362: + context + "Schedule();");
2363: println(out, " if (pgSchedule != null) {");
2364: println(out, " return (" + context
2365: + ") pgSchedule.getDefault();");
2366: println(out, " } else {");
2367: println(out, " return null;");
2368: println(out, " }");
2369: println(out, " }");
2370: println(out);
2371: }
2372:
2373: if (timephased) {
2374: println(out,
2375: " /** Test for existence of a default "
2376: + context + "\n" + " **/");
2377: } else {
2378: println(out, " /** Test for existence of a "
2379: + context + "\n" + " **/");
2380: }
2381: println(out, " public boolean has" + context
2382: + "() {");
2383: println(out, " return (get" + context
2384: + "() != null);\n" + " }");
2385: println(out);
2386:
2387: if (timephased) {
2388: println(out, " /** Test for existence of a "
2389: + context + " at a specific time\n"
2390: + " **/");
2391: println(out, " public boolean has" + context
2392: + "(long time) {");
2393: println(out, " return (get" + context
2394: + "(time) != null);\n" + " }");
2395: println(out);
2396: }
2397:
2398: String vr = "a" + context;
2399: println(
2400: out,
2401: " /** Set the "
2402: + context
2403: + " property.\n"
2404: + " * The default implementation will create a new "
2405: + context
2406: + "\n"
2407: + " * property and add it to the otherPropertyGroup list.\n"
2408: + " * Many subclasses override with local slots.\n"
2409: + " **/\n" + " public void set"
2410: + context + "(PropertyGroup " + vr
2411: + ") {\n" + " if (" + vr
2412: + " == null) {\n"
2413: + " removeOtherPropertyGroup("
2414: + context + ".class);\n"
2415: + " } else {\n"
2416: + " addOtherPropertyGroup(a"
2417: + context + ");\n" + " }\n"
2418: + " }");
2419: println(out);
2420:
2421: if (timephased) {
2422: println(out,
2423: " public PropertyGroupSchedule get"
2424: + context + "Schedule()");
2425: println(out, " {");
2426: println(out,
2427: " return searchForPropertyGroupSchedule("
2428: + context + ".class);");
2429: println(out, " }");
2430: println(out);
2431:
2432: println(
2433: out,
2434: " public void set"
2435: + context
2436: + "Schedule(PropertyGroupSchedule schedule) {\n"
2437: + " removeOtherPropertyGroup("
2438: + context
2439: + ".class);\n"
2440: + " if (schedule != null) {\n"
2441: + " addOtherPropertyGroupSchedule(schedule);\n"
2442: + " }\n" + " }");
2443: println(out);
2444: }
2445: }
2446: }
2447:
2448: println(out, "}");
2449: out.close();
2450: }
2451:
2452: private void writeIndex() throws Exception {
2453: String outname = "Properties.index";
2454: if (cleanp) {
2455: (new File(outname)).delete();
2456: return;
2457: }
2458: noteFile(outname);
2459:
2460: debug("Writing Properties Index to \"" + outname + "\"");
2461: FileOutputStream fos = new FileOutputStream(new File(
2462: getTargetDir(), outname));
2463: OutputStreamWriter osw = new OutputStreamWriter(fos);
2464: PrintWriter out = new PrintWriter(osw);
2465:
2466: println(out,
2467: "- - - - - List of machine generated PropertyGroups - - - - -");
2468:
2469: Map<String, List<String>> permuted = new HashMap<String, List<String>>();
2470:
2471: List<String> l = new ArrayList<String>(p.table.keySet());
2472: Collections.sort(l);
2473: for (String context : l) {
2474: println(out, "* " + context);
2475: String doc = p.get(context, "doc");
2476: if (doc != null) {
2477: println(out, doc);
2478: }
2479:
2480: for (String slotspec : getAllSlotSpecs(context)) {
2481: int s = slotspec.indexOf(" ");
2482: String type = slotspec.substring(0, s);
2483: String name = slotspec.substring(s + 1);
2484: ActiveSlot as = parseActiveSlot(slotspec);
2485: if (as != null) {
2486: name = as.name;
2487: }
2488: print(out, " " + type + " " + toClassName(name)
2489: + ";");
2490: String slotdoc = (String) p.get(context, name
2491: + ".doc");
2492: if (slotdoc != null)
2493: print(out, "\t//" + slotdoc);
2494: println(out);
2495:
2496: List<String> pent = permuted.get(name);
2497: if (pent == null) {
2498: pent = new ArrayList<String>();
2499: permuted.put(name, pent);
2500: }
2501: pent.add(context);
2502: }
2503: }
2504:
2505: println(out,
2506: "- - - - - Permuted index of PropertyGroupSlots - - - - -");
2507: List<String> k = new ArrayList<String>(permuted.keySet());
2508: Collections.sort(k);
2509: for (String name : k) {
2510: println(out, "* " + toClassName(name));
2511: for (String context : permuted.get(name)) {
2512: println(out, " " + context);
2513: }
2514: }
2515:
2516: out.close();
2517: }
2518:
2519: public void write() throws Exception {
2520: grokGlobal();
2521: for (String context : p.getContexts()) {
2522: if (!context.equals("global")
2523: && (p.get(context, "abstract") == null)
2524: && (isPrimary(context))) {
2525: writePropertyGroup(context);
2526: }
2527: }
2528: if (writeAbstractFactory)
2529: writeFactory();
2530: writeAsset();
2531:
2532: writeIndex();
2533: }
2534:
2535: protected boolean isPrimary(String context) {
2536: String source = p.get(context, PGParser.PG_SOURCE);
2537: return (source != null)
2538: && (source.equals(PGParser.PRIMARY));
2539: }
2540: }
2541:
2542: /** arguments to the writer **/
2543: private String arguments[];
2544:
2545: public String deffilename = null;
2546:
2547: boolean isVerbose = false;
2548: boolean cleanp = false;
2549: boolean writeAbstractFactory = true;
2550: boolean writeInterfaces = true;
2551:
2552: public void debug(String s) {
2553: if (isVerbose)
2554: System.err.println(s);
2555: }
2556:
2557: void processFile(String filename) {
2558: InputStream stream = null;
2559: try {
2560: setDirectories(filename);
2561: if (filename.equals("-")) {
2562: debug("Reading from standard input.");
2563: stream = new java.io.DataInputStream(System.in);
2564: } else if (new File(filename).exists()) {
2565: debug("Reading \"" + filename + "\".");
2566: deffilename = filename;
2567: stream = new FileInputStream(filename);
2568: } else {
2569: deffilename = filename;
2570: debug("Using ClassLoader to read \"" + filename + "\".");
2571: stream = getClass().getClassLoader()
2572: .getResourceAsStream(filename);
2573: }
2574:
2575: PGParser p = new PGParser(isVerbose);
2576: p.parse(stream);
2577: p.setModifiable(false);
2578: stream.close();
2579:
2580: Writer w = new Writer(p);
2581: w.write();
2582: w.done();
2583: } catch (Exception e) {
2584: System.err.println("Caught: " + e);
2585: e.printStackTrace();
2586: }
2587: }
2588:
2589: void usage(String s) {
2590: System.err.println(s);
2591: System.err
2592: .println("Usage: PGWriter [-v] [-f] [-i] [--] file [file ...]\n"
2593: + "-v toggle verbose mode (default off)\n"
2594: + "-f toggle AbstractFactory generation (on)\n"
2595: + "-i toggle Interface generation (on)\n");
2596: System.exit(1);
2597: }
2598:
2599: public void start() {
2600: boolean ignoreDash = false;
2601: String propertiesFile = null;
2602:
2603: for (int i = 0; i < arguments.length; i++) {
2604: String arg = arguments[i];
2605: if (!ignoreDash && arg.startsWith("-")) { // parse flags
2606: if (arg.equals("--")) {
2607: ignoreDash = true;
2608: } else if (arg.equals("-v")) {
2609: isVerbose = (!isVerbose);
2610: } else if (arg.equals("-f")) {
2611: writeAbstractFactory = (!writeAbstractFactory);
2612: } else if (arg.equals("-clean")) {
2613: cleanp = true;
2614: } else if (arg.equals("-properties")) {
2615: propertiesFile = arguments[++i];
2616: } else if (arg.equals("-d")) {
2617: targetDirName = arguments[++i];
2618: } else {
2619: usage("Unknown option \"" + arg + "\"");
2620: }
2621: } else { // deal with files
2622: propertiesFile = arg;
2623: }
2624: }
2625:
2626: if (propertiesFile == null) {
2627: propertiesFile = PGParser.DEFAULT_FILENAME;
2628: }
2629:
2630: processFile(propertiesFile);
2631: }
2632:
2633: public PGWriter(String args[]) {
2634: arguments = args;
2635: }
2636:
2637: public static void main(String args[]) {
2638: PGWriter mw = new PGWriter(args);
2639: mw.start();
2640: }
2641: }
|