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: * RelationshipInfoReader.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.util.URLHelper;
066: import org.jaffa.util.DefaultEntityResolver;
067: import org.jaffa.util.DefaultErrorHandler;
068: import org.jaffa.util.XmlHelper;
069:
070: import javax.xml.parsers.ParserConfigurationException;
071: import org.xml.sax.SAXException;
072:
073: /**
074: * This class will parse the RelationshipInfo file returning a List of Relationship objects.
075: * @author GautamJ
076: */
077: public class RelationshipInfoReader {
078:
079: private static final String RELATIONSHIP = "Relationship";
080: private static final String FROM_SCHEMA = "FromSchema";
081: private static final String TO_SCHEMA = "ToSchema";
082: private static final String FROM_TABLE_NAME = "FromTableName";
083: private static final String TO_TABLE_NAME = "ToTableName";
084: private static final String FROM_CARDINALITY = "FromCardinality";
085: private static final String TO_CARDINALITY = "ToCardinality";
086: private static final String TYPE = "Type";
087:
088: /**
089: * This will parse the RelationshipInfo file returning a List of Relationship objects.
090: *
091: * XML format for the RelationshipInfo file is:
092: * <?xml version="1.0"?>
093: * <Root>
094: * <Relationship(1..*)>
095: * <FromSchema(1)></FromSchema>
096: * <ToSchema(1)></ToSchema>
097: * <FromTableName(1)></FromTableName>
098: * <ToTableName(1)></ToTableName>
099: * <FromCardinality(1)></FromCardinality>
100: * <ToCardinality(1)></ToCardinality>
101: * <Type(1)></Type>
102: * </Relationship>
103: * </Root>
104: *
105: * @param input the RelationshipInfo file.
106: * @throws ParserConfigurationException if the xml parsing fails.
107: * @throws SAXException if the xml parsing fails.
108: * @throws IOException if the xml parsing fails.
109: * @return a sorted List of Relationship objects.
110: */
111: public static List parse(String input)
112: throws ParserConfigurationException, SAXException,
113: IOException {
114: InputStream stream = null;
115:
116: try {
117: List list = new ArrayList();
118:
119: // Create a factory object for creating DOM parsers
120: DocumentBuilderFactory factory = DocumentBuilderFactory
121: .newInstance();
122:
123: // Specifies that the parser produced by this factory will validate documents as they are parsed.
124: factory.setValidating(true);
125:
126: // Now use the factory to create a DOM parser
127: DocumentBuilder parser = factory.newDocumentBuilder();
128:
129: // Specifies the EntityResolver to resolve DTD used in XML documents
130: parser.setEntityResolver(new DefaultEntityResolver());
131:
132: // Specifies the ErrorHandler to handle warning/error/fatalError conditions
133: parser.setErrorHandler(new DefaultErrorHandler());
134:
135: // Parse the file and build a Document tree to represent its content
136: stream = URLHelper.getInputStream(input);
137: if (stream == null)
138: throw new IOException("File not found: " + input);
139: Document document = parser.parse(stream);
140:
141: // get a list of 'Relationship' elements
142: NodeList relationships = document
143: .getElementsByTagName(RELATIONSHIP);
144: for (int i = 0; i < relationships.getLength(); i++) {
145: Node relationshipNode = relationships.item(i);
146: Relationship relationship = new Relationship();
147: NodeList nodes = relationshipNode.getChildNodes();
148: for (int j = 0; j < nodes.getLength(); j++) {
149: Node node = nodes.item(j);
150: if (FROM_SCHEMA.equals(node.getNodeName()))
151: relationship.setFromSchema(XmlHelper
152: .getTextTrim(node));
153: else if (TO_SCHEMA.equals(node.getNodeName()))
154: relationship.setToSchema(XmlHelper
155: .getTextTrim(node));
156: else if (FROM_TABLE_NAME.equals(node.getNodeName()))
157: relationship.setFromTableName(XmlHelper
158: .getTextTrim(node));
159: else if (TO_TABLE_NAME.equals(node.getNodeName()))
160: relationship.setToTableName(XmlHelper
161: .getTextTrim(node));
162: else if (FROM_CARDINALITY
163: .equals(node.getNodeName()))
164: relationship.setFromCardinality(XmlHelper
165: .getTextTrim(node));
166: else if (TO_CARDINALITY.equals(node.getNodeName()))
167: relationship.setToCardinality(XmlHelper
168: .getTextTrim(node));
169: else if (TYPE.equals(node.getNodeName()))
170: relationship.setType(XmlHelper
171: .getTextTrim(node));
172: }
173: list.add(relationship);
174: }
175: document = null;
176: Collections.sort(list);
177: return list;
178: } finally {
179: if (stream != null)
180: stream.close();
181: }
182: }
183:
184: /**
185: * An instance of this class represents a relationship defined in the RelationshipInfo.xml file.
186: */
187: public static class Relationship implements Comparable {
188:
189: /** Holds value of property fromSchema. */
190: private String fromSchema;
191:
192: /** Holds value of property toSchema. */
193: private String toSchema;
194:
195: /** Holds value of property fromTableName. */
196: private String fromTableName;
197:
198: /** Holds value of property toTableName. */
199: private String toTableName;
200:
201: /** Holds value of property fromCardinality. */
202: private String fromCardinality;
203:
204: /** Holds value of property toCardinality. */
205: private String toCardinality;
206:
207: /** Holds value of property type. */
208: private String type;
209:
210: /** Default constructor.
211: */
212: public Relationship() {
213: }
214:
215: /** Creates an instance of this class with the supplied initial parameters.
216: * @param fromSchema the from schema.
217: * @param toSchema the to schema.
218: * @param fromTableName the from table name.
219: * @param toTableName the to table name.
220: */
221: public Relationship(String fromSchema, String toSchema,
222: String fromTableName, String toTableName) {
223: setFromSchema(fromSchema);
224: setToSchema(toSchema);
225: setFromTableName(fromTableName);
226: setToTableName(toTableName);
227: }
228:
229: /** Getter for property fromSchema.
230: * @return Value of property fromSchema.
231: */
232: public String getFromSchema() {
233: return fromSchema;
234: }
235:
236: /** Setter for property fromSchema.
237: * @param fromSchema New value of property fromSchema.
238: */
239: private void setFromSchema(String fromSchema) {
240: if (fromSchema == null)
241: this .fromSchema = null;
242: else
243: this .fromSchema = fromSchema.toUpperCase();
244: }
245:
246: /** Getter for property toSchema.
247: * @return Value of property toSchema.
248: */
249: public String getToSchema() {
250: return toSchema;
251: }
252:
253: /** Setter for property toSchema.
254: * @param toSchema New value of property toSchema.
255: */
256: private void setToSchema(String toSchema) {
257: if (toSchema == null)
258: this .toSchema = null;
259: else
260: this .toSchema = toSchema.toUpperCase();
261: }
262:
263: /** Getter for property fromTableName.
264: * @return Value of property fromTableName.
265: */
266: public String getFromTableName() {
267: return fromTableName;
268: }
269:
270: /** Setter for property fromTableName.
271: * @param fromTableName New value of property fromTableName.
272: */
273: private void setFromTableName(String fromTableName) {
274: if (fromTableName == null)
275: this .fromTableName = null;
276: else
277: this .fromTableName = fromTableName.toUpperCase();
278: }
279:
280: /** Getter for property toTableName.
281: * @return Value of property toTableName.
282: */
283: public String getToTableName() {
284: return toTableName;
285: }
286:
287: /** Setter for property toTableName.
288: * @param toTableName New value of property toTableName.
289: */
290: private void setToTableName(String toTableName) {
291: if (toTableName == null)
292: this .toTableName = null;
293: else
294: this .toTableName = toTableName.toUpperCase();
295: }
296:
297: /** Getter for property fromCardinality.
298: * @return Value of property fromCardinality.
299: */
300: public String getFromCardinality() {
301: return fromCardinality;
302: }
303:
304: /** Setter for property fromCardinality.
305: * @param fromCardinality New value of property fromCardinality.
306: */
307: private void setFromCardinality(String fromCardinality) {
308: this .fromCardinality = fromCardinality;
309: }
310:
311: /** Getter for property toCardinality.
312: * @return Value of property toCardinality.
313: */
314: public String getToCardinality() {
315: return toCardinality;
316: }
317:
318: /** Setter for property toCardinality.
319: * @param toCardinality New value of property toCardinality.
320: */
321: private void setToCardinality(String toCardinality) {
322: this .toCardinality = toCardinality;
323: }
324:
325: /** Getter for property type.
326: * @return Value of property type.
327: */
328: public String getType() {
329: return type;
330: }
331:
332: /** Setter for property type.
333: * @param type New value of property type.
334: */
335: private void setType(String type) {
336: this .type = type;
337: }
338:
339: /**
340: * Compares this object with the specified object for order.
341: * Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
342: * Note: this class has a natural ordering that is inconsistent with equals.
343: * @param obj the Object to be compared.
344: * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
345: */
346: public int compareTo(Object obj) {
347: Relationship target = (Relationship) obj;
348: if (target == null) {
349: return 1;
350: } else {
351: int i;
352: i = fromSchema == null ? (target.fromSchema == null ? 0
353: : -1) : (target.fromSchema == null ? 1
354: : fromSchema.compareTo(target.fromSchema));
355: if (i != 0)
356: return i;
357:
358: i = toSchema == null ? (target.toSchema == null ? 0
359: : -1) : (target.toSchema == null ? 1 : toSchema
360: .compareTo(target.toSchema));
361: if (i != 0)
362: return i;
363:
364: i = fromTableName == null ? (target.fromTableName == null ? 0
365: : -1)
366: : (target.fromTableName == null ? 1
367: : fromTableName
368: .compareTo(target.fromTableName));
369: if (i != 0)
370: return i;
371:
372: i = toTableName == null ? (target.toTableName == null ? 0
373: : -1)
374: : (target.toTableName == null ? 1 : toTableName
375: .compareTo(target.toTableName));
376: if (i != 0)
377: return i;
378: }
379: return 0;
380: }
381:
382: /**
383: * Returns diagnostic information.
384: * @return a String containing diagnostic information.
385: */
386: public String toString() {
387: StringBuffer buf = new StringBuffer();
388: buf.append("<Relationship>");
389: buf.append("<fromSchema>");
390: if (fromSchema != null)
391: buf.append(fromSchema);
392: buf.append("</fromSchema>");
393: buf.append("<toSchema>");
394: if (toSchema != null)
395: buf.append(toSchema);
396: buf.append("</toSchema>");
397: buf.append("<fromTableName>");
398: if (fromTableName != null)
399: buf.append(fromTableName);
400: buf.append("</fromTableName>");
401: buf.append("<toTableName>");
402: if (toTableName != null)
403: buf.append(toTableName);
404: buf.append("</toTableName>");
405: buf.append("<fromCardinality>");
406: if (fromCardinality != null)
407: buf.append(fromCardinality);
408: buf.append("</fromCardinality>");
409: buf.append("<toCardinality>");
410: if (toCardinality != null)
411: buf.append(toCardinality);
412: buf.append("</toCardinality>");
413: buf.append("<type>");
414: if (type != null)
415: buf.append(type);
416: buf.append("</type>");
417: buf.append("</Relationship>");
418: return buf.toString();
419: }
420: }
421: }
|