001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.tools.build;
028:
029: import java.io.File;
030: import java.io.PrintWriter;
031:
032: public class WriterBase {
033:
034: protected String targetDirName = null;
035: private File targetDir = null;
036: private File sourceDir = null;
037: private String genFileName = "generated.gen";
038:
039: protected File getTargetDir() {
040: return targetDir;
041: }
042:
043: protected File getSourceDir() {
044: return sourceDir;
045: }
046:
047: protected String getGenFileName() {
048: return genFileName;
049: }
050:
051: // Given a file handle, construct a package name
052: // that matches. Allows each module to have a Version that could
053: // be different.
054: protected String getPackageFromDir(File directory) {
055: if (directory == null)
056: return ".";
057:
058: String dirName = directory.getAbsolutePath();
059:
060: // Now: which piece of this is the package?
061: // Assume Version is under a "src" dir
062: if (dirName.lastIndexOf("src") != -1) {
063: dirName = dirName.substring(dirName.lastIndexOf("src") + 4);
064: } else {
065: }
066:
067: StringBuffer dirBuf = new StringBuffer(dirName);
068: // put in the "."
069: int ix = 0;
070: while ((ix = dirBuf.indexOf(File.separator)) != -1) {
071: dirBuf.replace(ix, ix + 1, ".");
072: }
073:
074: return dirBuf.substring(0);
075: }
076:
077: protected void setDirectories(String defFileName) {
078: if (defFileName.equals("-")) {
079: sourceDir = new File(System.getProperty("user.dir"));
080: } else {
081: File defFile = new File(defFileName);
082: sourceDir = defFile.getParentFile();
083: String s = defFile.getName();
084: int dotPos = s.lastIndexOf('.');
085: if (dotPos < 0) {
086: genFileName = s + ".gen";
087: } else {
088: genFileName = s.substring(0, dotPos) + ".gen";
089: }
090: }
091: if (targetDirName != null) {
092: targetDir = new File(targetDirName);
093: } else {
094: targetDir = sourceDir;
095: }
096: }
097:
098: protected final static void println(PrintWriter out, String s) {
099: print(out, s);
100: println(out);
101: }
102:
103: protected final static void println(PrintWriter out) {
104: out.println();
105: }
106:
107: protected final static void print(PrintWriter out, String s) {
108: int p = 0; // cursor position
109: int l = s.length(); // length of s
110: while (p < l) {
111: int i = s.indexOf('\n', p);
112: if (i == -1) {
113: out.print(s.substring(p));
114: p = l;
115: } else {
116: out.print(s.substring(p, i));
117: out.println();
118: p = i + 1;
119: }
120: }
121: }
122:
123: protected final static void writeCR(PrintWriter out) {
124: writeCR(out, null);
125: }
126:
127: protected final static void writeCR(PrintWriter out, String source) {
128: println(
129: out,
130: "/*\n"
131: + " * <copyright>\n"
132: + " * \n"
133: + " * Copyright 1997-2004 BBNT Solutions, LLC\n"
134: + " * under sponsorship of the Defense Advanced Research Projects\n"
135: + " * Agency (DARPA).\n"
136: + " * \n"
137: + " * You can redistribute this software and/or modify it under the\n"
138: + " * terms of the Cougaar Open Source License as published on the\n"
139: + " * Cougaar Open Source Website (www.cougaar.org).\n"
140: + " * \n"
141: + " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
142: + " * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
143: + " * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
144: + " * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
145: + " * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
146: + " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
147: + " * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
148: + " * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
149: + " * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
150: + " * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
151: + " * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
152: + " * \n" + " * </copyright>\n" + " */");
153: println(out);
154: if (source != null)
155: source = source.replace('\\', '/'); // stupid windows!
156: println(out, "/* @" + "generated " + new java.util.Date()
157: + ((source == null) ? "" : (" from " + source))
158: + " - DO NOT HAND EDIT */");
159: }
160: }
|