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.BufferedReader;
0030: import java.io.File;
0031: import java.io.FileInputStream;
0032: import java.io.FileOutputStream;
0033: import java.io.IOException;
0034: import java.io.InputStream;
0035: import java.io.InputStreamReader;
0036: import java.io.OutputStreamWriter;
0037: import java.io.PrintWriter;
0038: import java.util.ArrayList;
0039: import java.util.Collection;
0040: import java.util.List;
0041:
0042: public class AssetWriter extends WriterBase {
0043: public final static String DEFAULT_FILENAME = "assets.def";
0044:
0045: class ClassD {
0046: Collection<String> imports = new ArrayList<String>();
0047: String name;
0048: String doc;
0049: String base;
0050: boolean hasRelationships = false;
0051: List<SlotD> slotds = new ArrayList<SlotD>();
0052:
0053: public ClassD(String name) {
0054: this .name = name;
0055: doc = null;
0056: base = null;
0057: }
0058:
0059: void parseSlot(String t) {
0060: if (t == null && t.length() == 0)
0061: return;
0062:
0063: String v = t;
0064: // two arg case
0065: int j;
0066: if ((j = t.indexOf(' ')) != -1) {
0067: v = t.substring(j + 1).trim();
0068: t = t.substring(0, j);
0069: } else if (!pgParser.hasContext(v)) {
0070: // Assuming that two arg case never applies to property groups
0071: System.err.println("Definition of PropertyGroup " + v
0072: + " has not been parsed.");
0073: }
0074:
0075: SlotD sd = new SlotD(t, v);
0076:
0077: sd.setTimePhased(pgParser.getValueFlag(v,
0078: PGParser.TIMEPHASED, true, false));
0079:
0080: if (pgParser.getValueFlag(v, PGParser.HAS_RELATIONSHIPS,
0081: true, false)) {
0082: if (!hasRelationships) {
0083: hasRelationships = true;
0084: sd.setHasRelationships(true);
0085: } else {
0086: System.err
0087: .println("AssetWriter.parseSlot(): "
0088: + name
0089: + " has multiple PGs which implement HasRelationships");
0090: sd.setHasRelationships(false);
0091: }
0092: } else {
0093: sd.setHasRelationships(false);
0094: }
0095: addSlotD(sd);
0096:
0097: }
0098:
0099: public void setSlots(String s) {
0100: int p = 0;
0101:
0102: int i;
0103: while ((i = s.indexOf(',', p)) != -1) {
0104: parseSlot(s.substring(p, i).trim());
0105: p = i + 1;
0106: }
0107: parseSlot(s.substring(p).trim());
0108: }
0109:
0110: public SlotD getSlotD(String slotdname) {
0111: for (SlotD slotd : slotds) {
0112: if (slotdname.equals(slotd.getName()))
0113: return slotd;
0114: }
0115: return null;
0116: }
0117:
0118: public void addSlotD(SlotD slotd) {
0119: slotds.add(slotd);
0120: }
0121:
0122: public void setDoc(String doc) {
0123: this .doc = doc;
0124: }
0125:
0126: public String getDoc() {
0127: return doc;
0128: }
0129:
0130: public void setBase(String base) {
0131: this .base = base;
0132: }
0133:
0134: public String getBase() {
0135: return base;
0136: }
0137:
0138: public List<SlotD> getSlotDs() {
0139: return slotds;
0140: }
0141:
0142: public List<SlotD> getAllSlots() {
0143: if (base != null) {
0144: ClassD cd = session.findClassD(base);
0145: if (cd != null) {
0146: List<SlotD> v = cd.getAllSlots();
0147: v = new ArrayList<SlotD>(v);
0148: v.addAll(slotds);
0149: return v;
0150: }
0151: }
0152: return slotds;
0153: }
0154:
0155: public String getName() {
0156: return name;
0157: }
0158:
0159: public String toString() {
0160: return "ClassD " + name;
0161: }
0162:
0163: public Collection<String> getImports() {
0164: return imports;
0165: }
0166:
0167: public void addImport(String imp) {
0168: imports.add(imp);
0169: }
0170:
0171: public boolean getHasRelationships() {
0172: return hasRelationships;
0173: }
0174: }
0175:
0176: class SlotD {
0177: String type;
0178: String name;
0179: String doc;
0180: String init;
0181: boolean trans = false;
0182: boolean exact = false;
0183: boolean timephased = false;
0184: boolean hasRelationships = false;
0185:
0186: public SlotD(String type, String name) {
0187: this .type = type;
0188: this .name = name;
0189: init = null;
0190: doc = null;
0191: }
0192:
0193: public void setDoc(String doc) {
0194: this .doc = doc;
0195: }
0196:
0197: public void setInit(String init) {
0198: this .init = init;
0199: }
0200:
0201: public void setTrans(String t) {
0202: this .trans = Boolean.valueOf(t).booleanValue();
0203: }
0204:
0205: public void setExact(String t) {
0206: exact = Boolean.valueOf(t).booleanValue();
0207: }
0208:
0209: public void setTimePhased(boolean tp) {
0210: timephased = tp;
0211: }
0212:
0213: public void setHasRelationships(boolean hr) {
0214: hasRelationships = hr;
0215: }
0216:
0217: public String getType() {
0218: return type;
0219: }
0220:
0221: public String getName() {
0222: return name;
0223: }
0224:
0225: public boolean hasInit() {
0226: return (init != null);
0227: }
0228:
0229: public String getInit() {
0230: if (init != null) {
0231: return init;
0232: } else {
0233: if (timephased) {
0234: return "PropertyGroupFactory.new" + type
0235: + "Schedule()";
0236: } else {
0237: return "PropertyGroupFactory.new" + type + "()";
0238: }
0239: }
0240: }
0241:
0242: public String getDoc() {
0243: return doc;
0244: }
0245:
0246: public boolean getTrans() {
0247: return trans;
0248: }
0249:
0250: public boolean getExact() {
0251: return exact;
0252: }
0253:
0254: public boolean getTimePhased() {
0255: return timephased;
0256: }
0257:
0258: public boolean getHasRelationships() {
0259: return hasRelationships;
0260: }
0261:
0262: public String toString() {
0263: return "SlotD " + name;
0264: }
0265: }
0266:
0267: class Session {
0268: PrintWriter filelist = null;
0269:
0270: InputStream s;
0271:
0272: public Session(InputStream s) {
0273: this .s = s;
0274: try {
0275: filelist = new PrintWriter(new OutputStreamWriter(
0276: new FileOutputStream(new File(getTargetDir(),
0277: getGenFileName()))));
0278: noteFile(getGenFileName());
0279:
0280: } catch (IOException ioe) {
0281: throw new RuntimeException();
0282: }
0283: }
0284:
0285: public void noteFile(String s) {
0286: println(filelist, s);
0287: }
0288:
0289: public void done() {
0290: filelist.close();
0291: }
0292:
0293: public Collection<ClassD> classds = new ArrayList<ClassD>();
0294:
0295: public ClassD findClassD(String name) {
0296: for (ClassD cd : classds) {
0297: if (cd.getName().equals(name))
0298: return cd;
0299: }
0300: return null;
0301: }
0302:
0303: public void parse() {
0304: InputStreamReader isr = new InputStreamReader(s);
0305: BufferedReader br = new BufferedReader(isr);
0306:
0307: ClassD cd = null;
0308:
0309: String line;
0310: int ln = 0;
0311: try {
0312: for (line = br.readLine(); line != null; line = br
0313: .readLine()) {
0314: int i, j;
0315:
0316: ln++;
0317: // ignore comments
0318: if ((i = line.indexOf(';')) >= 0)
0319: line = line.substring(0, i);
0320:
0321: // zap extra whitespace
0322: line = line.trim();
0323:
0324: // ignore empty lines
0325: if (line.length() <= 0)
0326: continue;
0327:
0328: int l;
0329: while (line.charAt((l = line.length()) - 1) == '\\') {
0330: line = line.substring(0, l - 1)
0331: + br.readLine().trim();
0332: ln++;
0333: }
0334:
0335: if (line.charAt(0) == '[') {
0336: // classd line
0337:
0338: //Assume default property defs file if not yet initialized
0339: if (pgParser == null) {
0340: initProperties(PGParser.DEFAULT_FILENAME);
0341: }
0342:
0343: j = line.indexOf(']');
0344: String name = line.substring(1, j).trim();
0345: String ext = null;
0346:
0347: if ((j = name.indexOf(' ')) > -1) {
0348: ext = name.substring(j + 1).trim();
0349: name = name.substring(0, j).trim();
0350: }
0351: cd = new ClassD(name);
0352: if (ext != null) {
0353: cd.setBase(ext);
0354: }
0355: classds.add(cd);
0356: } else {
0357: // a param line
0358: j = line.indexOf('=');
0359: String before = line.substring(0, j).trim();
0360: String after = line.substring(j + 1).trim();
0361:
0362: if (before.equals("package") && cd == null) {
0363: asset_package = after;
0364: } else if (before.equals("propertydefs")) {
0365: System.out.println("propertydefs " + after);
0366: initProperties(after);
0367: } else if (before.equals("extends")) {
0368: cd.setBase(after);
0369: } else if (before.equals("slots")) {
0370: cd.setSlots(after);
0371: } else if (before.equals("doc")) {
0372: cd.setDoc(after);
0373: } else if (before.equals("import")) {
0374: cd.addImport(after);
0375: } else {
0376: j = before.indexOf('.');
0377: if (j >= 0) {
0378: // slot property
0379: String slot = before.substring(0, j);
0380: String param = before.substring(j + 1);
0381:
0382: SlotD slotd = cd.getSlotD(slot);
0383: if (slotd == null) {
0384: System.err
0385: .println("Unknown slot in "
0386: + slot + "."
0387: + param);
0388: } else if (param.equals("init")) {
0389: slotd.setInit(after);
0390: } else if (param.equals("transient")) {
0391: slotd.setTrans(after);
0392: } else if (param.equals("exact")) {
0393: slotd.setExact(after);
0394: } else {
0395: System.err
0396: .println("Unknown slot parameter in "
0397: + slot
0398: + "."
0399: + param);
0400: }
0401: } else {
0402: System.err.println("Bogus line '"
0403: + line + "'");
0404: }
0405:
0406: }
0407: }
0408:
0409: }
0410: } catch (Exception e) {
0411: System.err.println("Exception parsing " + s
0412: + " at line " + ln);
0413: // e.printStackTrace();
0414: }
0415: }
0416:
0417: public void writeFactory(String path) {
0418: debug("Writing AssetFactory to " + path);
0419: if (cleanp) {
0420: (new File(path)).delete();
0421: return;
0422: }
0423: try {
0424: noteFile(path);
0425: PrintWriter out = new PrintWriter(
0426: new OutputStreamWriter(new FileOutputStream(
0427: new File(getTargetDir(), path))));
0428: writeCR(out, deffilename);
0429:
0430: println(out, "package " + asset_package + ";");
0431: println(out,
0432: "import org.cougaar.planning.ldm.asset.EssentialAssetFactory;");
0433: println(out);
0434: println(out,
0435: "public class AssetFactory extends EssentialAssetFactory {");
0436: println(out, " public static String[] assets = {");
0437:
0438: for (ClassD cd : classds) {
0439: String name = cd.getName();
0440: print(out, " \"" + asset_package + "." + name
0441: + "\",");
0442: println(out);
0443: }
0444:
0445: println(out, " };");
0446: println(out, "}");
0447:
0448: out.close();
0449: } catch (Exception e) {
0450: System.err.println("Caught Exception while writing "
0451: + path);
0452: e.printStackTrace();
0453: }
0454: }
0455:
0456: public void write() {
0457: for (ClassD cd : classds) {
0458: // write the class
0459: String path = cd.getName() + ".java";
0460: debug("Writing " + cd + " to " + path);
0461:
0462: if (cleanp) {
0463: (new File(path)).delete();
0464: continue;
0465: }
0466:
0467: try {
0468: noteFile(path);
0469: PrintWriter out = new PrintWriter(
0470: new OutputStreamWriter(
0471: new FileOutputStream(new File(
0472: getTargetDir(), path))));
0473: writeCR(out, deffilename);
0474:
0475: println(out, "package " + asset_package + ";");
0476: println(out,
0477: "import org.cougaar.planning.ldm.asset.*;");
0478: println(out, "import java.io.ObjectOutputStream;");
0479: println(out, "import java.io.ObjectInputStream;");
0480: println(out, "import java.io.IOException;");
0481: println(out, "import java.util.Vector;");
0482: println(out,
0483: "import java.beans.PropertyDescriptor;");
0484: println(out,
0485: "import java.beans.IndexedPropertyDescriptor;");
0486: println(out,
0487: "import java.beans.IntrospectionException;");
0488:
0489: for (String imp : cd.getImports()) {
0490: println(out, "import " + imp + ";");
0491: }
0492:
0493: if (cd.getHasRelationships()) {
0494: println(out,
0495: "import org.cougaar.planning.ldm.plan.HasRelationships;");
0496: println(out,
0497: "import org.cougaar.planning.ldm.plan.RelationshipSchedule;");
0498: println(out,
0499: "import org.cougaar.planning.ldm.plan.RelationshipScheduleImpl;");
0500: }
0501:
0502: String doc = cd.getDoc();
0503: if (doc != null) {
0504: println(out, "/** " + doc + " **/");
0505: println(out);
0506: }
0507:
0508: String name = cd.getName();
0509: print(out, "public class " + name);
0510:
0511: String ext = cd.getBase();
0512: if (ext != null && !ext.equals("")) {
0513: print(out, " extends " + ext);
0514: }
0515: if (cd.getHasRelationships()) {
0516: print(out, " implements HasRelationships");
0517: }
0518: println(out, " {");
0519: println(out);
0520:
0521: // constructor
0522: // is public so that anyone can construct assets.
0523: // may be a problem if we reintroduce internal links to cluster
0524: // state.
0525: println(out, " public " + name + "() {");
0526: for (SlotD sd : cd.getSlotDs()) {
0527: String sname = sd.getName();
0528:
0529: // for time phased, want snameSchedule
0530: if (!sd.getTimePhased()) {
0531: println(out, " my" + sname + " = null;");
0532: } else {
0533: println(out, " my" + sname
0534: + "Schedule = null;");
0535: }
0536: }
0537:
0538: println(out, " }");
0539: println(out);
0540:
0541: // Prototype constructor
0542: println(out, " public " + name + "(" + name
0543: + " prototype) {");
0544: println(out, " super(prototype);");
0545:
0546: for (SlotD sd : cd.getSlotDs()) {
0547: String sname = sd.getName();
0548:
0549: if (sd.getTimePhased()) {
0550: sname = sname + "Schedule";
0551: }
0552:
0553: if (!sd.getTrans()) {
0554: // values default to prototype
0555: println(out, " my" + sname + "=null;");
0556: } else {
0557: println(out, " my" + sname + "="
0558: + sd.getInit()
0559: + "; //non-property");
0560: }
0561: }
0562: println(out, " }");
0563: println(out);
0564:
0565: // clone - used by copy
0566: println(
0567: out,
0568: " /** For infrastructure only - use org.cougaar.core.domain.Factory.copyInstance instead. **/");
0569: println(out,
0570: " public Object clone() throws CloneNotSupportedException {\n"
0571: + " " + name + " _thing = ("
0572: + name + ") super.clone();");
0573: for (SlotD sd : cd.getSlotDs()) {
0574: // Don't clone RelationshipSchedules
0575: // Hack until we figure out how to do this right
0576: if (!sd.getHasRelationships()) {
0577: String sname = sd.getName();
0578: boolean stimephased = sd.getTimePhased();
0579:
0580: String vname = "my" + sname;
0581: String vv = vname;
0582:
0583: if (!stimephased) {
0584: if (!sd.getExact()) { // if it's a property, we'll need to lock it
0585: vv = vv + ".lock()";
0586: }
0587: println(out, " if (" + vname
0588: + "!=null) _thing.set" + sname
0589: + "(" + vv + ");");
0590:
0591: } else {
0592: vname = vname + "Schedule";
0593: println(
0594: out,
0595: " if ("
0596: + vname
0597: + "!=null) _thing.set"
0598: + sname
0599: + "Schedule((PropertyGroupSchedule) "
0600: + vname + ".lock());");
0601: }
0602: }
0603: }
0604: println(out, " return _thing;\n" + " }\n");
0605:
0606: // create the base instance for copies.
0607: println(
0608: out,
0609: " /** create an instance of the right class for copy operations **/\n"
0610: + " public Asset instanceForCopy() {\n"
0611: + " return new " + name
0612: + "();\n" + " }\n");
0613:
0614: // create the base instance for copies.
0615: println(
0616: out,
0617: " /** create an instance of this prototype **/\n"
0618: + " public Asset createInstance() {\n"
0619: + " return new " + name
0620: + "(this);\n" + " }\n");
0621:
0622: // property filler
0623: println(out,
0624: " protected void fillAllPropertyGroups(Vector v) {");
0625: println(out, " super.fillAllPropertyGroups(v);");
0626: for (SlotD sd : cd.getSlotDs()) {
0627: String sname = sd.getName();
0628: if (sd.getTimePhased()) {
0629: sname = sname + "Schedule";
0630: }
0631:
0632: if (!sd.hasInit()) {
0633: println(out, " { Object _tmp = get"
0634: + sname
0635: + "();\n"
0636: +
0637: // LATE?
0638: " if (_tmp != null && !(_tmp instanceof Null_PG)) {\n"
0639: + " v.addElement(_tmp);\n"
0640: + " } }");
0641: }
0642: }
0643: println(out, " }");
0644: println(out);
0645:
0646: // slot hackery
0647: for (SlotD sd : cd.getSlotDs()) {
0648: String sname = sd.getName();
0649: String stype = sd.getType();
0650: boolean stimephased = sd.getTimePhased();
0651:
0652: String sdoc = sd.getDoc();
0653: boolean exact = sd.getExact();
0654:
0655: String var = "my" + sname;
0656: if (stimephased) {
0657: var = var + "Schedule";
0658: println(out,
0659: " private transient PropertyGroupSchedule "
0660: + var + ";");
0661: } else {
0662: println(out, " private transient " + stype
0663: + " " + var + ";");
0664: }
0665: println(out);
0666:
0667: if (sdoc != null) {
0668: println(out, " /** " + sdoc + " **/");
0669: }
0670:
0671: String argType = "";
0672: String argName = "";
0673: String argStr = "";
0674: if (stimephased) {
0675: argType = "long";
0676: argName = "time";
0677: argStr = argType + " " + argName;
0678: }
0679:
0680: if (sd.getHasRelationships()) {
0681: addHasRelationshipsImpl(out, sname);
0682: }
0683:
0684: println(out, " public " + stype + " get"
0685: + sname + "(" + argStr + ") {");
0686: if (exact) {
0687: println(out, " if (" + var
0688: + " != null) return " + var + ";");
0689: println(out,
0690: " if (myPrototype instanceof "
0691: + name + ")\n"
0692: + " return ((" + name
0693: + ")myPrototype).get"
0694: + sname + "(" + argName
0695: + ");");
0696: println(out, " return null;");
0697: } else {
0698: println(out, " " + sname + " _tmp = ("
0699: + var + " != null) ?");
0700: if (stimephased) {
0701: println(out, " (" + sname + ")"
0702: + var + ".intersects(time) :");
0703: println(out, " (" + sname
0704: + ")resolvePG(" + sname
0705: + ".class, time);");
0706: } else {
0707: println(out, " " + var + " : ("
0708: + sname + ")resolvePG(" + sname
0709: + ".class);");
0710: }
0711:
0712: println(out, " return (_tmp == " + sname
0713: + ".nullPG)?null:_tmp;");
0714: }
0715: println(out, " }");
0716:
0717: if (stimephased) {
0718: String fname = "get" + sname + "Schedule";
0719:
0720: println(out,
0721: " public PropertyGroupSchedule "
0722: + fname + "() {");
0723: if (exact) {
0724: println(out, " if (" + var
0725: + " != null) return " + var
0726: + ";");
0727:
0728: println(out,
0729: " // exact slots must delegate to same class proto");
0730: println(out,
0731: " if (myPrototype instanceof "
0732: + name + ")");
0733: println(out, " return ((" + name
0734: + ")myPrototype)." + fname
0735: + "();");
0736: println(out, " return null;");
0737: } else {
0738: println(out,
0739: " PropertyGroupSchedule _tmp = ("
0740: + var + " != null) ?");
0741: println(out, " " + var + " : "
0742: + "resolvePGSchedule(" + stype
0743: + ".class);");
0744: println(out, " return _tmp;");
0745: println(out, " }");
0746: println(out);
0747: }
0748: }
0749:
0750: String arg = "arg_" + sname;
0751:
0752: // ADD methods
0753: if (sd.getHasRelationships()) {
0754: }
0755:
0756: if (exact) {
0757: // exact slots aren't PropertyGroups
0758: println(out, " public void set" + sname
0759: + "(" + stype + " " + arg + ") {");
0760: println(out, " my" + sname + "= " + arg
0761: + ";");
0762: println(out, " }");
0763: } else {
0764: // non-exact slots have setters which just take PropertyGroup
0765: println(out, " public void set" + sname
0766: + "(PropertyGroup " + arg + ") {");
0767: println(
0768: out,
0769: " if (!("
0770: + arg
0771: + " instanceof "
0772: + stype
0773: + "))\n"
0774: + " throw new IllegalArgumentException(\"set"
0775: + sname + " requires a "
0776: + stype + " argument.\");");
0777: if (stimephased) {
0778: println(out, " if (" + var
0779: + " == null) {");
0780: println(out, " " + var + " = "
0781: + sd.getInit() + ";");
0782: println(out, " }");
0783: println(out);
0784: println(out, " " + var + ".add("
0785: + arg + ");");
0786: } else {
0787: println(out, " " + var + " = ("
0788: + stype + ") " + arg + ";");
0789: }
0790: println(out, " }");
0791: println(out);
0792: }
0793:
0794: if (stimephased) {
0795: arg = "arg_" + name + "Schedule";
0796: // non-exact slots have setters which just take PropertyGroup
0797: println(out, " public void set" + sname
0798: + "Schedule(PropertyGroupSchedule "
0799: + arg + ") {");
0800: println(
0801: out,
0802: " if (!("
0803: + stype
0804: + ".class.equals("
0805: + arg
0806: + ".getPGClass())))\n"
0807: + " throw new IllegalArgumentException(\"set"
0808: + sname
0809: + "Schedule requires a PropertyGroupSchedule of"
0810: + sname + "s.\");");
0811: println(out);
0812: println(out, " " + var + " = " + arg
0813: + ";");
0814: println(out, " }");
0815: println(out);
0816: }
0817: }
0818:
0819: if (!cd.getSlotDs().isEmpty()) { // don't write the methods unless we need to
0820: println(out, " // generic search methods");
0821:
0822: println(out,
0823: " public PropertyGroup getLocalPG(Class c, long t) {");
0824: for (SlotD sd : cd.getSlotDs()) {
0825: if (!sd.getExact()) {
0826: String sname = sd.getName();
0827: // for time phased, want snameSchedule
0828: String var = "my" + sname;
0829: println(out, " if (" + sname
0830: + ".class.equals(c)) {");
0831: if (!sd.getTimePhased()) {
0832: println(out, " return (" + var
0833: + "==" + sname
0834: + ".nullPG)?null:" + var
0835: + ";");
0836: } else {
0837: var = var + "Schedule";
0838: println(
0839: out,
0840: " if ("
0841: + var
0842: + "==null) {\n"
0843: + " return null;\n"
0844: + " } else {\n"
0845: + " if (t == UNSPECIFIED_TIME) {\n"
0846: + " return ("
0847: + sname
0848: + ")"
0849: + var
0850: + ".getDefault();\n"
0851: + " } else {\n"
0852: + " return ("
0853: + sname
0854: + ")"
0855: + var
0856: + ".intersects(t);\n"
0857: + " }\n"
0858: + " }");
0859: }
0860: println(out, " }");
0861: }
0862: }
0863: println(out,
0864: " return super.getLocalPG(c,t);");
0865: println(out, " }");
0866: println(out);
0867:
0868: println(out,
0869: " public PropertyGroupSchedule getLocalPGSchedule(Class c) {");
0870: for (SlotD sd : cd.getSlotDs()) {
0871: String sname = sd.getName();
0872: // for time phased, want snameSchedule
0873: if (!sd.getExact() && sd.getTimePhased()) {
0874: println(out, " if (" + sname
0875: + ".class.equals(c)) {");
0876: println(out, " return my" + sname
0877: + "Schedule;");
0878: println(out, " }");
0879: }
0880: }
0881: println(out,
0882: " return super.getLocalPGSchedule(c);");
0883: println(out, " }");
0884: println(out);
0885:
0886: println(out,
0887: " public void setLocalPG(Class c, PropertyGroup pg) {");
0888: for (SlotD sd : cd.getSlotDs()) {
0889: if (!sd.getExact()) {
0890: String sname = sd.getName();
0891: String var = "my" + sname;
0892: println(out, " if (" + sname
0893: + ".class.equals(c)) {");
0894: if (!sd.getTimePhased()) {
0895: println(out, " " + var + "=("
0896: + sname + ")pg;");
0897: } else {
0898: var = var + "Schedule";
0899: println(out, " if (" + var
0900: + "==null) {");
0901: println(out, " " + var + "="
0902: + sd.getInit() + ";");
0903: println(out, " } else {");
0904: println(
0905: out,
0906: " "
0907: + var
0908: + ".removeAll("
0909: + var
0910: + ".intersectingSet((TimePhasedPropertyGroup) pg));");
0911: println(out, " }");
0912: println(out, " " + var
0913: + ".add(pg);");
0914: }
0915: println(out, " } else");
0916: }
0917: }
0918: println(out, " super.setLocalPG(c,pg);");
0919: println(out, " }");
0920: println(out);
0921:
0922: println(out,
0923: " public void setLocalPGSchedule(PropertyGroupSchedule pgSchedule) {");
0924: for (SlotD sd : cd.getSlotDs()) {
0925: if ((!sd.getExact())
0926: && (sd.getTimePhased())) {
0927: String sname = sd.getName();
0928: // for time phased, want snameSchedule
0929: String var = "my" + sname + "Schedule";
0930: println(
0931: out,
0932: " if ("
0933: + sname
0934: + ".class.equals(pgSchedule.getPGClass())) {");
0935: println(out, " " + var
0936: + "=pgSchedule;");
0937: println(out, " } else");
0938: }
0939: }
0940: println(out,
0941: " super.setLocalPGSchedule(pgSchedule);");
0942: println(out, " }");
0943: println(out);
0944:
0945: println(out,
0946: " public PropertyGroup removeLocalPG(Class c) {");
0947: println(out,
0948: " PropertyGroup removed = null;");
0949: print(out, " ");
0950: for (SlotD sd : cd.getSlotDs()) {
0951: if (!sd.getExact()) {
0952: String sname = sd.getName();
0953: // for time phased, want snameSchedule
0954: String var = "my" + sname;
0955: println(out, "if (" + sname
0956: + ".class.equals(c)) {");
0957: if (!sd.getTimePhased()) {
0958: println(out, " removed=" + var
0959: + ";");
0960: println(out, " " + var
0961: + "=null;");
0962: } else {
0963: var = var + "Schedule";
0964: println(out, " if (" + var
0965: + "!=null) {");
0966: println(out, " if (" + var
0967: + ".getDefault()!=null) {");
0968: println(out, " removed="
0969: + var + ".getDefault();");
0970: println(out, " } else if ("
0971: + var + ".size() > 0) {");
0972: println(out,
0973: " removed=(PropertyGroup) "
0974: + var + ".get(0);");
0975: println(out, " }");
0976: println(out, " " + var
0977: + "=null;");
0978: println(out, " }");
0979: }
0980: print(out, " } else ");
0981: }
0982: }
0983: println(out, "{");
0984: println(out,
0985: " removed=super.removeLocalPG(c);");
0986: println(out, " }");
0987: println(out, " return removed;");
0988: println(out, " }");
0989: println(out);
0990:
0991: println(out,
0992: " public PropertyGroup removeLocalPG(PropertyGroup pg) {");
0993: println(out,
0994: " Class pgc = pg.getPrimaryClass();");
0995: print(out, " ");
0996: for (SlotD sd : cd.getSlotDs()) {
0997: if (!sd.getExact()) {
0998: String sname = sd.getName();
0999: // for time phased, want snameSchedule
1000: String var = "my" + sname;
1001: println(out, "if (" + sname
1002: + ".class.equals(pgc)) {");
1003: if (!sd.getTimePhased()) {
1004: println(out,
1005: " PropertyGroup removed="
1006: + var + ";");
1007: println(out, " " + var
1008: + "=null;");
1009: println(out,
1010: " return removed;");
1011: } else {
1012: var = var + "Schedule";
1013: println(out, "if ((" + var
1014: + "!=null) && ");
1015: println(out, " (" + var
1016: + ".remove(pg))) {");
1017: println(out, " return pg;");
1018: println(out, " }");
1019: }
1020: print(out, " } else ");
1021: }
1022: }
1023: println(out, "{}");
1024: println(out,
1025: " return super.removeLocalPG(pg);");
1026: println(out, " }");
1027: println(out);
1028:
1029: println(out,
1030: " public PropertyGroupSchedule removeLocalPGSchedule(Class c) {");
1031: for (SlotD sd : cd.getSlotDs()) {
1032: if ((!sd.getExact())
1033: && (sd.getTimePhased())) {
1034: String sname = sd.getName();
1035: // for time phased, want snameSchedule
1036: String var = "my" + sname + "Schedule";
1037: println(out, " if (" + sname
1038: + ".class.equals(c)) {");
1039: println(out,
1040: " PropertyGroupSchedule removed="
1041: + var + ";");
1042: println(out, " " + var + "=null;");
1043: println(out, " return removed;");
1044: println(out, " } else ");
1045: }
1046: }
1047: println(out, " {");
1048: println(out,
1049: " return super.removeLocalPGSchedule(c);");
1050: println(out, " }");
1051: println(out, " }");
1052: println(out);
1053:
1054: println(out,
1055: " public PropertyGroup generateDefaultPG(Class c) {");
1056: for (SlotD sd : cd.getSlotDs()) {
1057: if (!sd.getExact()) {
1058: String sname = sd.getName();
1059: // for time phased, want snameSchedule
1060: String var = "my" + sname;
1061: println(out, " if (" + sname
1062: + ".class.equals(c)) {");
1063: if (!sd.getTimePhased()) {
1064: println(out, " return (" + var
1065: + "= new " + sname
1066: + "Impl());");
1067: } else {
1068: println(out, " return null;");
1069: }
1070: println(out, " } else");
1071: }
1072: }
1073: println(out,
1074: " return super.generateDefaultPG(c);");
1075: println(out, " }");
1076: println(out);
1077:
1078: println(out, " // dumb serialization methods");
1079: println(out);
1080:
1081: // writer
1082: println(out,
1083: " private void writeObject(ObjectOutputStream out) throws IOException {");
1084: println(out, " out.defaultWriteObject();");
1085: for (SlotD sd : cd.getSlotDs()) {
1086: if (!sd.getTrans()) {
1087: String sname = sd.getName();
1088:
1089: String var = "my" + sname;
1090: if (sd.getTimePhased()) {
1091: var = var + "Schedule";
1092: }
1093: println(
1094: out,
1095: " if ("
1096: + var
1097: + " instanceof Null_PG || "
1098: + var
1099: + " instanceof Future_PG) {\n"
1100: + " out.writeObject(null);\n"
1101: + " } else {\n"
1102: + " out.writeObject("
1103: + var + ");\n"
1104: + " }");
1105: }
1106: }
1107: println(out, " }");
1108: println(out);
1109:
1110: // reader
1111: println(
1112: out,
1113: " private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {");
1114: println(out, " in.defaultReadObject();");
1115: for (SlotD sd : cd.getSlotDs()) {
1116: String sname = sd.getName();
1117: String stype = sd.getType();
1118:
1119: String var = "my" + sname;
1120: if (sd.getTimePhased()) {
1121: var = var + "Schedule";
1122: stype = "PropertyGroupSchedule";
1123: }
1124:
1125: if (!sd.getTrans()) {
1126: println(out, " " + var + "=("
1127: + stype + ")in.readObject();");
1128: } else {
1129: println(out, " " + var + "="
1130: + sd.getInit() + ";");
1131: }
1132: }
1133: println(out, " }");
1134:
1135: }
1136:
1137: println(out, " // beaninfo support");
1138: writeBeanInfoBody(out, cd);
1139:
1140: println(out, "}");
1141:
1142: out.close();
1143: } catch (Exception e) {
1144: System.err
1145: .println("Caught Exception while writing "
1146: + path);
1147: e.printStackTrace();
1148: }
1149: }
1150: }
1151:
1152: public void writeBeanInfoBody(PrintWriter out, ClassD cd) {
1153: String name = cd.getName();
1154:
1155: try {
1156: println(out,
1157: " private static PropertyDescriptor properties[];\n"
1158: + " static {");
1159: List<SlotD> v = cd.getSlotDs();
1160: int l = v.size();
1161: if (l == 0) {
1162: println(out,
1163: " properties = new PropertyDescriptor["
1164: + l + "];");
1165: } else {
1166: println(out, " try {");
1167: println(out,
1168: " properties = new PropertyDescriptor["
1169: + l + "];");
1170:
1171: int i = 0;
1172: for (SlotD sd : v) {
1173: String sname = sd.getName();
1174:
1175: if (sd.getTimePhased()) {
1176: println(out, " properties[" + i
1177: + "] = new PropertyDescriptor(\""
1178: + sname + "Schedule\", " + name
1179: + ".class, " + "\"get" + sname
1180: + "Schedule\", " + "null);");
1181: } else {
1182: println(out, " properties[" + i
1183: + "] = new PropertyDescriptor(\""
1184: + sname + "\", " + name
1185: + ".class, " + "\"get" + sname
1186: + "\", " + "null);");
1187:
1188: }
1189: i++;
1190: }
1191: println(out,
1192: " } catch (IntrospectionException ie) {}");
1193: }
1194: println(out, " }");
1195: println(out);
1196: println(
1197: out,
1198: " public PropertyDescriptor[] getPropertyDescriptors() {\n"
1199: + " PropertyDescriptor[] pds = super.getPropertyDescriptors();\n"
1200: + " PropertyDescriptor[] ps = new PropertyDescriptor[pds.length+"
1201: + l
1202: + "];\n"
1203: + " System.arraycopy(pds, 0, ps, 0, pds.length);\n"
1204: + " System.arraycopy(properties, 0, ps, pds.length, "
1205: + l + ");\n" + " return ps;\n"
1206: + " }");
1207: } catch (Exception e) {
1208: System.err.println("Caught " + e);
1209: }
1210: }
1211:
1212: public void writeBeanInfo() {
1213: for (ClassD cd : classds) {
1214: // write the class
1215: String name = cd.getName();
1216: String path = name + "BeanInfo.java";
1217: debug("Writing " + cd + " to " + path);
1218:
1219: if (cleanp) {
1220: (new File(path)).delete();
1221: continue;
1222: }
1223:
1224: String ext = cd.getBase();
1225: if (ext != null && !ext.equals("")) {
1226: ext = " extends " + ext + "BeanInfo";
1227: } else {
1228: ext = " extends SimpleBeanInfo";
1229: }
1230:
1231: try {
1232: noteFile(path);
1233: PrintWriter out = new PrintWriter(
1234: new OutputStreamWriter(
1235: new FileOutputStream(new File(
1236: getTargetDir(), path))));
1237: writeCR(out, deffilename);
1238:
1239: println(out);
1240: println(out, "package " + asset_package + ";");
1241: println(out, "import java.beans.*;");
1242: println(out);
1243: println(out, "/** BeanInfo for " + name + " **/");
1244: println(out);
1245:
1246: for (String imp : cd.getImports()) {
1247: println(out, "import " + imp + ";");
1248: }
1249:
1250: println(out, "public class " + name + "BeanInfo"
1251: + ext + " {");
1252: writeBeanInfoBody(out, cd);
1253: println(out, "}");
1254: out.close();
1255: } catch (Exception e) {
1256: System.err.println("Caught " + e);
1257: }
1258: }
1259: }
1260:
1261: public void addHasRelationshipsImpl(PrintWriter out,
1262: String slotName) {
1263: String arg = "_arg" + slotName;
1264:
1265: println(out,
1266: " public RelationshipSchedule getRelationshipSchedule() {");
1267: println(out, " return get" + slotName
1268: + "().getRelationshipSchedule();");
1269: println(out, " }");
1270:
1271: println(out,
1272: " public void setRelationshipSchedule(RelationshipSchedule schedule) {");
1273: println(out, " New" + slotName + " " + arg + " = (New"
1274: + slotName + ") get" + slotName + "().copy();");
1275: println(out, " " + arg
1276: + ".setRelationshipSchedule(schedule);");
1277: println(out, " set" + slotName + "(" + arg + ");");
1278: println(out, " }");
1279:
1280: println(out, "");
1281: println(out, " public boolean isLocal() {");
1282: println(out, " return get" + slotName + "().getLocal();");
1283: println(out, " }");
1284:
1285: println(out, " public void setLocal(boolean localFlag) {");
1286: println(out, " New" + slotName + " " + arg + " = (New"
1287: + slotName + ") get" + slotName + "().copy();");
1288: println(out, " " + arg + ".setLocal(localFlag);");
1289: println(out, " set" + slotName + "(" + arg + ");");
1290: println(out, " }");
1291:
1292: println(out, "");
1293: println(out, " public boolean isSelf() {");
1294: println(out, " return get" + slotName + "().getLocal();");
1295: println(out, " }");
1296:
1297: println(out, "");
1298: }
1299:
1300: }
1301:
1302: public void debug(String s) {
1303: if (isVerbose)
1304: System.err.println(s);
1305: }
1306:
1307: public Session session = null;
1308:
1309: protected PGParser pgParser = null;
1310:
1311: protected void initProperties(String propertyFilenames) {
1312: pgParser = new PGParser(isVerbose);
1313:
1314: int i;
1315: int parsePos = 0;
1316: String filename;
1317: while (parsePos < propertyFilenames.length()) {
1318: if ((i = propertyFilenames.indexOf(',', parsePos)) != -1) {
1319: filename = propertyFilenames.substring(parsePos, i)
1320: .trim();
1321: parsePos = ++i;
1322: } else {
1323: filename = propertyFilenames.substring(parsePos).trim();
1324: parsePos = propertyFilenames.length();
1325: }
1326:
1327: InputStream stream = null;
1328:
1329: try {
1330: if (filename.equals("-")) {
1331: debug("Reading properties from standard input.");
1332: stream = new java.io.DataInputStream(System.in);
1333: } else if (filename.indexOf('/') == -1) {
1334: debug("Reading \"" + filename + "\".");
1335: File pf;
1336: // must be a '/', NOT filesystem dependent!
1337: if (filename.lastIndexOf('/') != -1) {
1338: pf = new File(filename);
1339: } else {
1340: pf = new File(getSourceDir(), filename);
1341: }
1342: stream = new FileInputStream(pf);
1343: } else {
1344: System.out.println("using class loader: "
1345: + filename);
1346: debug("Using ClassLoader to read \"" + filename
1347: + "\".");
1348: stream = getClass().getClassLoader()
1349: .getResourceAsStream(filename);
1350: }
1351: if (stream != null) {
1352: pgParser.parse(stream);
1353: stream.close();
1354: } else {
1355: System.err.println("Could not find \""
1356: + filename
1357: + "\" with "
1358: + ((filename.indexOf('/') == -1) ? "file"
1359: : "Resource") + " IO");
1360: }
1361: } catch (Exception e) {
1362: System.err.println("targetdir=" + getSourceDir());
1363: System.err.println("Unable to parse " + filename);
1364: System.err.println("Caught: " + e);
1365: e.printStackTrace();
1366: }
1367: }
1368: }
1369:
1370: protected void processFile(String assetFilename) {
1371: InputStream stream = null;
1372:
1373: try {
1374: setDirectories(assetFilename);
1375: if (assetFilename.equals("-")) {
1376: debug("Reading from standard input.");
1377: stream = new java.io.DataInputStream(System.in);
1378: } else {
1379: debug("Reading \"" + assetFilename + "\".");
1380: deffilename = assetFilename;
1381: stream = new FileInputStream(assetFilename);
1382: }
1383:
1384: Session p = new Session(stream);
1385: session = p;
1386: p.parse();
1387: p.write();
1388: //p.writeBeanInfo();
1389: p.writeFactory("AssetFactory.java");
1390: p.done();
1391: stream.close();
1392:
1393: } catch (Exception e) {
1394: System.err.println("Caught: " + e);
1395: e.printStackTrace();
1396: }
1397: }
1398:
1399: private boolean isVerbose = false;
1400: private boolean cleanp = false;
1401:
1402: private String arguments[];
1403:
1404: private String asset_package = "org.cougaar.planning.ldm.asset";
1405:
1406: protected void usage(String s) {
1407: System.err.println(s);
1408: System.err
1409: .println("Usage: AssetWriter [-v] [--] file [file ...]\n"
1410: + "-v toggle verbose mode (default off)\n");
1411: System.exit(1);
1412: }
1413:
1414: public void start() {
1415: boolean ignoreDash = false;
1416: String assetsFile = null;
1417:
1418: for (int i = 0; i < arguments.length; i++) {
1419: String arg = arguments[i];
1420: if (!ignoreDash && arg.startsWith("-")) { // parse flags
1421: if (arg.equals("--")) {
1422: ignoreDash = true;
1423: } else if (arg.equals("-v")) {
1424: isVerbose = (!isVerbose);
1425: } else if (arg.charAt(1) == 'P') {
1426: // handle -Ppackage.name argument
1427: asset_package = arg.substring(2);
1428: if (isVerbose) {
1429: System.err.println("Setting asset_package to "
1430: + asset_package);
1431: }
1432: } else if (arg.equals("-clean")) {
1433: cleanp = true;
1434: } else if (arg.equals("-assets")) {
1435: assetsFile = arguments[++i];
1436: } else if (arg.equals("-d")) {
1437: targetDirName = arguments[++i];
1438: } else {
1439: usage("Unknown option \"" + arg + "\"");
1440: }
1441: } else {
1442: assetsFile = arg;
1443: }
1444: }
1445:
1446: if (assetsFile == null) {
1447: assetsFile = DEFAULT_FILENAME;
1448: }
1449:
1450: processFile(assetsFile);
1451: }
1452:
1453: public String deffilename = null;
1454:
1455: public AssetWriter(String args[]) {
1456: arguments = args;
1457: }
1458:
1459: public static void main(String argv[]) {
1460: AssetWriter aw = new AssetWriter(argv);
1461: aw.start();
1462: }
1463:
1464: }
|