001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.sdlctools.services.codegeneration;
016:
017: import java.io.PrintWriter;
018:
019: import com.metaboss.enterprise.bs.BSException;
020: import com.metaboss.enterprise.bs.BSNamingAndDirectoryServiceInvocationException;
021: import com.metaboss.util.XMLUtils;
022: import com.metaboss.licensing.LicenseService;
023:
024: /** Utility class dealing with copyrigt notices */
025: public class CopyrightUtils {
026: private static boolean sClassInitialised = false;
027: private static Object sClassInitialisationSemaphore = new Object();
028: private static LicenseService sLicenseService = null;
029:
030: // Pseudo class initialiser. Main feature that we can throw exception from here (unable to do that from native java class initialiser)
031: protected static void initialiseClassIfNecessary()
032: throws BSException {
033: if (!sClassInitialised) {
034: synchronized (sClassInitialisationSemaphore) {
035: if (!sClassInitialised) {
036: try {
037: javax.naming.Context lCtx = new javax.naming.InitialContext();
038: sLicenseService = (LicenseService) lCtx
039: .lookup(LicenseService.COMPONENT_URL);
040: } catch (javax.naming.NamingException e) {
041: throw new BSNamingAndDirectoryServiceInvocationException(
042: "Unable to initialise CopyrightUtils",
043: e);
044: }
045: sClassInitialised = true;
046: }
047: }
048: }
049: }
050:
051: /** Helper. Writes out header to any file */
052: private static void writeCopyrightToFileHeader(PrintWriter pWriter,
053: String pFirstLine, String pLastLine, String pLinePrefix,
054: boolean pIsXMLHeader) throws BSException {
055: initialiseClassIfNecessary();
056: pWriter.println(pFirstLine);
057: String[] lGeneratedFileHeader = sLicenseService
058: .getGeneratedFileHeader();
059: for (int i = 0; i < lGeneratedFileHeader.length; i++) {
060: if (pLinePrefix != null && pLinePrefix.length() > 0)
061: pWriter.print(pLinePrefix);
062: if (pIsXMLHeader)
063: pWriter.println(XMLUtils
064: .stringToXmlComment(lGeneratedFileHeader[i]));
065: else
066: pWriter.println(lGeneratedFileHeader[i]);
067: }
068: pWriter.println(pLastLine);
069: }
070:
071: /** Writes out copyright notice to the java file */
072: public static void writeCopyrightToGeneratedJavaSource(
073: PrintWriter pWriter) throws BSException {
074: initialiseClassIfNecessary();
075: // Call reuseable helper with parameters appropriate for the java header
076: writeCopyrightToFileHeader(pWriter, "//", "//", "// ", false);
077: }
078:
079: /** Writes out copyright notice to the java file, includes generator class name */
080: public static void writeCopyrightToGeneratedJavaSource(
081: PrintWriter pWriter, Class pGeneratorClass)
082: throws BSException {
083: initialiseClassIfNecessary();
084: // Call reuseable helper with parameters appropriate for the java header
085: writeCopyrightToFileHeader(pWriter, "//", "//", "// ", false);
086: // Generate additional comment dealing with the generator class
087: pWriter.println("// The generator class is "
088: + pGeneratorClass.getName());
089: }
090:
091: /** Writes out copyright notice to the 'C' or 'C++' file */
092: public static void writeCopyrightToGeneratedCSource(
093: PrintWriter pWriter) throws BSException {
094: initialiseClassIfNecessary();
095: // Call reuseable helper with parameters appropriate for the java header
096: writeCopyrightToFileHeader(pWriter, "/* ", " */", " * ", false);
097: }
098:
099: /** Writes out copyright notice to the C file, includes generator class name */
100: public static void writeCopyrightToGeneratedCSource(
101: PrintWriter pWriter, Class pGeneratorClass)
102: throws BSException {
103: initialiseClassIfNecessary();
104: // Call reuseable helper with parameters appropriate for the java header
105: writeCopyrightToFileHeader(pWriter, "/* ", " */", " * ", false);
106: // Generate additional comment dealing with the generator class
107: pWriter.println("/* The generator class is "
108: + pGeneratorClass.getName() + " */");
109: }
110:
111: /** Writes out copyright notice to the properties file */
112: public static void writeCopyrightToGeneratedPropertiesFile(
113: PrintWriter pWriter) throws BSException {
114: initialiseClassIfNecessary();
115: // Call reuseable helper with parameters appropriate for the properties file header
116: writeCopyrightToFileHeader(pWriter, "##", "##", "## ", false);
117: }
118:
119: /** Writes out copyright notice to the properties file, includes generator class name */
120: public static void writeCopyrightToGeneratedPropertiesFile(
121: PrintWriter pWriter, Class pGeneratorClass)
122: throws BSException {
123: initialiseClassIfNecessary();
124: // Call reuseable helper with parameters appropriate for the properties file header
125: writeCopyrightToFileHeader(pWriter, "##", "##", "## ", false);
126: // Generate additional comment dealing with the generator class
127: pWriter.println("## The generator class is "
128: + pGeneratorClass.getName());
129: }
130:
131: /** Writes out copyright notice to the xml file */
132: public static void writeCopyrightToGeneratedXMLFile(
133: PrintWriter pWriter) throws BSException {
134: initialiseClassIfNecessary();
135: // Call reuseable helper with parameters appropriate for the XML file header
136: writeCopyrightToFileHeader(pWriter, "<!--", "-->", null, true);
137: }
138:
139: /** Writes out copyright notice to the html file */
140: public static void writeCopyrightToGeneratedHTMLFile(
141: PrintWriter pWriter) throws BSException {
142: initialiseClassIfNecessary();
143: // Call reuseable helper with parameters appropriate for the HTML file header
144: writeCopyrightToFileHeader(pWriter, "<!--", "-->", null, true);
145: }
146:
147: /** Writes out copyright notice to the html file */
148: public static void writeCopyrightToGeneratedSQLFile(
149: PrintWriter pWriter) throws BSException {
150: initialiseClassIfNecessary();
151: // Call reuseable helper with parameters appropriate for the SQL file header
152: writeCopyrightToFileHeader(pWriter, "--", "--", "-- ", false);
153: }
154:
155: /** Writes out copyright notice to the xml file, includes generator class name */
156: public static void writeCopyrightToGeneratedXMLFile(
157: PrintWriter pWriter, Class pGeneratorClass)
158: throws BSException {
159: initialiseClassIfNecessary();
160: // Call reuseable helper with parameters appropriate for the XML file header
161: writeCopyrightToFileHeader(pWriter, "<!--", "", null, true);
162: // Generate additional comment dealing with the generator class
163: pWriter.println("The generator class is "
164: + pGeneratorClass.getName());
165: pWriter.println("-->");
166: }
167: }
|