001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: /*
051: * TableInfoReader.java
052: *
053: * Created on April 24, 2002, 2:25 PM
054: */
055:
056: package org.jaffa.tools.domainmeta.uniface;
057:
058: import java.util.*;
059: import java.io.*;
060: import javax.xml.parsers.DocumentBuilderFactory;
061: import javax.xml.parsers.DocumentBuilder;
062: import org.w3c.dom.Document;
063: import org.w3c.dom.NodeList;
064: import org.w3c.dom.Node;
065: import org.jaffa.datatypes.Parser;
066: import org.jaffa.util.URLHelper;
067: import org.jaffa.util.DefaultEntityResolver;
068: import org.jaffa.util.DefaultErrorHandler;
069: import org.jaffa.util.XmlHelper;
070:
071: import javax.xml.parsers.ParserConfigurationException;
072: import org.xml.sax.SAXException;
073:
074: /**
075: * This class will parse the TableInfo file returning an instance of TableInfo object.
076: * @author GautamJ
077: */
078: public class TableInfoReader {
079:
080: private static final String APPLICATION_NAME = "ApplicationName";
081: private static final String PACKAGE_PREFIX = "PackagePrefix";
082: private static final String FULL_PACKAGE_NAMES = "FullPackageNames";
083: private static final String TABLE = "Table";
084: private static final String DATABASE_TABLE = "DatabaseTable";
085: private static final String SCHEMA = "Schema";
086: private static final String MODULE_NAME = "ModuleName";
087: private static final String DOMAIN_OBJECT = "DomainObject";
088: private static final String DOMAIN_PACKAGE = "DomainPackage";
089: private static final String REGENERATE = "Regenerate";
090:
091: /**
092: * This will parse the TableInfo file returning a TableInfo object.
093: *
094: * XML format for the TableInfo file is:
095: * <?xml version="1.0"?>
096: * <Root>
097: * <ApplicationName(1)></ApplicationName>
098: * <PackagePrefix(1)></PackagePrefix>
099: * <FullPackageNames(1, T/F)></FullPackageNames>
100: * <Table(1..*)>
101: * <DatabaseTable(1)></DatabaseTable>
102: * <Schema(1)></Schema>
103: * <ModuleName(1)></ModuleName>
104: * <DomainObject(1)></DomainObject>
105: * <DomainPackage(0..1)></DomainPackage>
106: * <Regenerate(1, T/F/ASK)></Regenerate>
107: * </Table>
108: * </Root>
109: *
110: * @param input the TableInfo file.
111: * @throws ParserConfigurationException if the xml parsing fails.
112: * @throws SAXException if ?the xml parsing fails.
113: * @throws IOException if the xml parsing fails.
114: * @return a TableInfo object.
115: */
116: public static TableInfo parse(String input)
117: throws ParserConfigurationException, SAXException,
118: IOException {
119: InputStream stream = null;
120:
121: try {
122: TableInfo tableInfo = new TableInfo();
123: List list = new ArrayList();
124:
125: // Create a factory object for creating DOM parsers
126: DocumentBuilderFactory factory = DocumentBuilderFactory
127: .newInstance();
128:
129: // Specifies that the parser produced by this factory will validate documents as they are parsed.
130: factory.setValidating(true);
131:
132: // Now use the factory to create a DOM parser
133: DocumentBuilder parser = factory.newDocumentBuilder();
134:
135: // Specifies the EntityResolver to resolve DTD used in XML documents
136: parser.setEntityResolver(new DefaultEntityResolver());
137:
138: // Specifies the ErrorHandler to handle warning/error/fatalError conditions
139: parser.setErrorHandler(new DefaultErrorHandler());
140:
141: // Parse the file and build a Document tree to represent its content
142: stream = URLHelper.getInputStream(input);
143: if (stream == null)
144: throw new IOException("File not found: " + input);
145: Document document = parser.parse(stream);
146:
147: tableInfo.setApplicationName(XmlHelper.getTextTrim(document
148: .getElementsByTagName(APPLICATION_NAME).item(0)));
149: tableInfo.setPackagePrefix(XmlHelper.getTextTrim(document
150: .getElementsByTagName(PACKAGE_PREFIX).item(0)));
151: tableInfo.setFullPackageNames(Parser.parseBoolean(
152: XmlHelper.getTextTrim(document
153: .getElementsByTagName(FULL_PACKAGE_NAMES)
154: .item(0))).booleanValue());
155:
156: // get a list of 'Table' elements
157: NodeList tables = document.getElementsByTagName(TABLE);
158: for (int i = 0; i < tables.getLength(); i++) {
159: Node tableNode = tables.item(i);
160: Table table = new Table();
161: NodeList nodes = tableNode.getChildNodes();
162: for (int j = 0; j < nodes.getLength(); j++) {
163: Node node = nodes.item(j);
164: if (DATABASE_TABLE.equals(node.getNodeName()))
165: table.setDatabaseTable(XmlHelper
166: .getTextTrim(node));
167: else if (SCHEMA.equals(node.getNodeName()))
168: table.setSchema(XmlHelper.getTextTrim(node));
169: else if (MODULE_NAME.equals(node.getNodeName()))
170: table
171: .setModuleName(XmlHelper
172: .getTextTrim(node));
173: else if (DOMAIN_OBJECT.equals(node.getNodeName()))
174: table.setDomainObject(XmlHelper
175: .getTextTrim(node));
176: else if (DOMAIN_PACKAGE.equals(node.getNodeName()))
177: table.setDomainPackage(XmlHelper
178: .getTextTrim(node));
179: else if (REGENERATE.equals(node.getNodeName()))
180: table
181: .setRegenerate(XmlHelper
182: .getTextTrim(node));
183: }
184: list.add(table);
185: }
186: document = null;
187: tableInfo.setTables(list);
188: return tableInfo;
189: } finally {
190: if (stream != null)
191: stream.close();
192: }
193: }
194:
195: /**
196: * An instance of this class represents a TableInfo.xml file.
197: */
198: public static class TableInfo {
199:
200: /** Holds value of property applicationName. */
201: private String applicationName;
202:
203: /** Holds value of property packagePrefix. */
204: private String packagePrefix;
205:
206: /** Holds value of property fullPackageNames. */
207: private boolean fullPackageNames;
208:
209: /** Holds value of property tables. */
210: private List tables;
211:
212: /** Getter for property applicationName.
213: * @return Value of property applicationName.
214: *
215: */
216: public String getApplicationName() {
217: return this .applicationName;
218: }
219:
220: /** Setter for property applicationName.
221: * @param applicationName New value of property applicationName.
222: *
223: */
224: private void setApplicationName(String applicationName) {
225: this .applicationName = applicationName;
226: }
227:
228: /** Getter for property packagePrefix.
229: * @return Value of property packagePrefix.
230: *
231: */
232: public String getPackagePrefix() {
233: return this .packagePrefix;
234: }
235:
236: /** Setter for property packagePrefix.
237: * @param packagePrefix New value of property packagePrefix.
238: *
239: */
240: private void setPackagePrefix(String packagePrefix) {
241: this .packagePrefix = packagePrefix;
242: }
243:
244: /** Getter for property fullPackageNames.
245: * @return Value of property fullPackageNames.
246: *
247: */
248: public boolean isFullPackageNames() {
249: return this .fullPackageNames;
250: }
251:
252: /** Setter for property fullPackageNames.
253: * @param fullPackageNames New value of property fullPackageNames.
254: *
255: */
256: private void setFullPackageNames(boolean fullPackageNames) {
257: this .fullPackageNames = fullPackageNames;
258: }
259:
260: /** Getter for property tables.
261: * @return Value of property tables.
262: *
263: */
264: public List getTables() {
265: return this .tables;
266: }
267:
268: /** Setter for property tables.
269: * @param tables New value of property tables.
270: *
271: */
272: private void setTables(List tables) {
273: this .tables = tables;
274: }
275:
276: /**
277: * Returns diagnostic information.
278: * @return a String containing diagnostic information.
279: */
280: public String toString() {
281: StringBuffer buf = new StringBuffer();
282: buf.append("<TableInfo>");
283: buf.append("<applicationName>");
284: if (applicationName != null)
285: buf.append(applicationName);
286: buf.append("</applicationName>");
287: buf.append("<packagePrefix>");
288: if (packagePrefix != null)
289: buf.append(packagePrefix);
290: buf.append("</packagePrefix>");
291: buf.append("<fullPackageNames>");
292: buf.append(fullPackageNames);
293: buf.append("</fullPackageNames>");
294: if (tables != null && tables.size() > 0) {
295: buf.append("<tables>");
296: for (Iterator itr = tables.iterator(); itr.hasNext();)
297: buf.append(itr.next());
298: buf.append("</tables>");
299: }
300: buf.append("</TableInfo>");
301: return buf.toString();
302: }
303:
304: }
305:
306: /**
307: * An instance of this class represents a table defined in the TableInfo.xml file.
308: */
309: public static class Table {
310:
311: /** Holds value of property databaseTable. */
312: private String databaseTable;
313:
314: /** Holds value of property schema. */
315: private String schema;
316:
317: /** Holds value of property moduleName. */
318: private String moduleName;
319:
320: /** Holds value of property domainObject. */
321: private String domainObject;
322:
323: /** Holds value of property domainPackage. */
324: private String domainPackage;
325:
326: /** Holds value of property regenerate. */
327: private String regenerate;
328:
329: /** Getter for property databaseTable.
330: * @return Value of property databaseTable.
331: */
332: public String getDatabaseTable() {
333: return databaseTable;
334: }
335:
336: /** Setter for property databaseTable.
337: * @param databaseTable New value of property databaseTable.
338: */
339: private void setDatabaseTable(String databaseTable) {
340: if (databaseTable == null)
341: this .databaseTable = null;
342: else
343: this .databaseTable = databaseTable.toUpperCase();
344: }
345:
346: /** Getter for property schema.
347: * @return Value of property schema.
348: */
349: public String getSchema() {
350: return schema;
351: }
352:
353: /** Setter for property schema.
354: * @param schema New value of property schema.
355: */
356: private void setSchema(String schema) {
357: if (schema == null)
358: this .schema = null;
359: else
360: this .schema = schema.toUpperCase();
361: }
362:
363: /** Getter for property moduleName.
364: * @return Value of property moduleName.
365: *
366: */
367: public String getModuleName() {
368: return this .moduleName;
369: }
370:
371: /** Setter for property moduleName.
372: * @param moduleName New value of property moduleName.
373: *
374: */
375: private void setModuleName(String moduleName) {
376: this .moduleName = moduleName;
377: }
378:
379: /** Getter for property domainObject.
380: * @return Value of property domainObject.
381: */
382: public String getDomainObject() {
383: return domainObject;
384: }
385:
386: /** Setter for property domainObject.
387: * @param domainObject New value of property domainObject.
388: */
389: private void setDomainObject(String domainObject) {
390: this .domainObject = domainObject;
391: }
392:
393: /** Getter for property domainPackage.
394: * @return Value of property domainPackage.
395: */
396: public String getDomainPackage() {
397: return domainPackage;
398: }
399:
400: /** Setter for property domainPackage.
401: * @param domainPackage New value of property domainPackage.
402: */
403: private void setDomainPackage(String domainPackage) {
404: this .domainPackage = domainPackage;
405: }
406:
407: /** Getter for property regenerate.
408: * @return Value of property regenerate.
409: */
410: public String getRegenerate() {
411: return regenerate;
412: }
413:
414: /** Setter for property regenerate.
415: * @param regenerate New value of property regenerate.
416: */
417: private void setRegenerate(String regenerate) {
418: this .regenerate = regenerate;
419: }
420:
421: /**
422: * Returns diagnostic information.
423: * @return a String containing diagnostic information.
424: */
425: public String toString() {
426: StringBuffer buf = new StringBuffer();
427: buf.append("<Table>");
428: buf.append("<databaseTable>");
429: if (databaseTable != null)
430: buf.append(databaseTable);
431: buf.append("</databaseTable>");
432: buf.append("<schema>");
433: if (schema != null)
434: buf.append(schema);
435: buf.append("</schema>");
436: buf.append("<moduleName>");
437: if (moduleName != null)
438: buf.append(moduleName);
439: buf.append("</moduleName>");
440: buf.append("<domainObject>");
441: if (domainObject != null)
442: buf.append(domainObject);
443: buf.append("</domainObject>");
444: buf.append("<domainPackage>");
445: if (domainPackage != null)
446: buf.append(domainPackage);
447: buf.append("</domainPackage>");
448: buf.append("<regenerate>");
449: if (regenerate != null)
450: buf.append(regenerate);
451: buf.append("</regenerate>");
452: buf.append("</Table>");
453: return buf.toString();
454: }
455:
456: }
457: }
|