Source code: dods/doml/DOMLUpgradeFactory.java
1 /*-----------------------------------------------------------------------------
2 * Enhydra Java Application Server
3 * Copyright 1997-2000 Lutris Technologies, Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes Enhydra software developed by Lutris
17 * Technologies, Inc. and its contributors.
18 * 4. Neither the name of Lutris Technologies nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY LUTRIS TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL LUTRIS TECHNOLOGIES OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *-----------------------------------------------------------------------------
34 * $Id: DOMLUpgradeFactory.java,v 1.4.12.1 2000/10/12 00:18:15 markd Exp $
35 *-----------------------------------------------------------------------------
36 */
37
38 package dods.doml;
39
40 import dods.data.*;
41
42 import java.util.Vector;
43 import java.util.Enumeration;
44
45 import org.w3c.dom.*;
46
47 import org.apache.xerces.dom.DOMImplementationImpl;
48 import org.apache.xerces.parsers.DOMParser;
49
50 /**
51 * This class is used to create DOML files out of DODS Project trees.
52 *
53 * @author Jason Abbott (jason@lutris.com)
54 */
55 public class DOMLUpgradeFactory {
56 private void appendTextElement(Element target,
57 String elementName,
58 String text) {
59 Document doc = target.getOwnerDocument();
60 Element textElement = doc.createElement(elementName);
61 Text textNode = doc.createTextNode(text);
62
63 target.appendChild(textElement);
64 textElement.appendChild(textNode);
65 }
66
67 private Element generateAttributeElement(Document doc,
68 Attribute attr) {
69 Element retVal = doc.createElement("column");
70
71 retVal.setAttribute("id", attr.getName());
72
73 if (attr.isIndex()) {
74 retVal.setAttribute("isIndex", "true");
75 }
76 if (attr.isUsedForQuery()) {
77 retVal.setAttribute("usedForQuery", "true");
78 }
79 if (attr.isConstant()) {
80 retVal.setAttribute("isConstant", "true");
81 }
82 if (attr.hasError()) {
83 appendTextElement(retVal, "error", attr.getErrorText());
84 }
85 String javadoc = attr.getJavadoc();
86 if (javadoc != null) {
87 String tmpJavaDoc = javadoc.replace('*', ' ').replace('/', ' ');
88 if (tmpJavaDoc.trim().length() > 0) {
89 appendTextElement(retVal, "javadoc", javadoc);
90 }
91 }
92 DataObject objectRef = (DataObject) attr.getObjectRefDO();
93 if (objectRef != null) {
94 Element objectRefElement = doc.createElement("referenceObject");
95 retVal.appendChild(objectRefElement);
96 if (attr.isRefConstraint()) {
97 objectRefElement.setAttribute("constraint", "true");
98 }
99 objectRefElement.setAttribute("reference",
100 objectRef.getPackageName() +
101 "." + objectRef.getName());
102 }
103
104 Element typeElement = null;
105 String javatype = attr.getJavaType();
106 if (javatype != null && javatype.trim().length() > 0) {
107 if (typeElement == null) {
108 typeElement = doc.createElement("type");
109 }
110 typeElement.setAttribute("javaType", javatype);
111 }
112 String dbtype = attr.getDBType();
113 if (dbtype != null && dbtype.trim().length() > 0) {
114 if (typeElement == null) {
115 typeElement = doc.createElement("type");
116 }
117 typeElement.setAttribute("dbType", dbtype);
118 }
119 String size = attr.getSize();
120 if (size != null && size.trim().length() > 0) {
121 if (typeElement == null) {
122 typeElement = doc.createElement("type");
123 }
124 typeElement.setAttribute("size", size);
125 }
126
127 if (attr.canBeNull()) {
128 if (typeElement == null) {
129 typeElement = doc.createElement("type");
130 }
131 typeElement.setAttribute("canBeNull", "true");
132 }
133
134 if (typeElement != null) {
135 retVal.appendChild(typeElement);
136 }
137
138
139 String initialValue = attr.getInitialValue();
140 if (initialValue != null && initialValue.length() > 0) {
141 appendTextElement(retVal, "initialValue", initialValue);
142 }
143 return retVal;
144 }
145
146 private void placeDataObjectInDocument(DataObject dataObject,
147 Element parentElement) {
148 Document doc = parentElement.getOwnerDocument();
149 String nodeType = "table";
150 boolean printTable = true;
151 String dataObjectName = dataObject.getName();
152
153 if (dataObject instanceof DOPackage) {
154 printTable = false;
155 nodeType = "package";
156 }
157
158 Element element = doc.createElement(nodeType);
159
160 parentElement.appendChild(element);
161 if (dataObject.getPackageName() != null &&
162 dataObject.getPackageName().trim().length() > 0) {
163 element.setAttribute("id",
164 dataObject.getPackageName().trim() + "." +
165 dataObjectName);
166 } else {
167 element.setAttribute("id", dataObjectName);
168 }
169
170 if (printTable) {
171 if (dataObjectName.equals(dataObject.getDBTableName()) == false) {
172 element.setAttribute("dbTableName",
173 dataObject.getDBTableName());
174 }
175 }
176
177 if (dataObject.isFinal()) {
178 element.setAttribute("isFinal", "true");
179 }
180 if (dataObject.isLazyLoading()) {
181 element.setAttribute("isLazyLoading", "true");
182 }
183 if (dataObject.isFullCaching())
184 element.setAttribute("caching", "full");
185 else if (dataObject.isCaching())
186 element.setAttribute("caching", "partial");
187 if (dataObject.isIndex()) {
188 element.setAttribute("isIndex", "true");
189 }
190 if (dataObject.isAbstract()) {
191 element.setAttribute("isAbstract", "true");
192 }
193 if (false) { /* Will be implemented in the future */
194 element.setAttribute("isEJB", "true");
195 }
196 if (false) { /* Will be implemented in the future */
197 element.setAttribute("isView", "true");
198 }
199 DataObject extension = dataObject.getExtensionOf();
200 if (extension != null) {
201 element.setAttribute("extensionOf",
202 extension.getPackageName() + "." +
203 extension.getName());
204 }
205
206 Vector attributes = dataObject.getAttributes();
207 if (attributes != null) {
208 Enumeration e = attributes.elements();
209 while (e.hasMoreElements()) {
210 Attribute attribute = (Attribute) e.nextElement();
211 element.appendChild(generateAttributeElement(doc, attribute));
212 }
213 }
214
215 if (dataObject instanceof DOPackage) {
216 DOPackage dopackage = (DOPackage) dataObject;
217 Vector vector = dopackage.getDataObjects();
218 if (vector != null) {
219 Enumeration e = vector.elements();
220 while (e.hasMoreElements()) {
221 DataObject childDataObject = (DataObject) e.nextElement();
222 placeDataObjectInDocument(childDataObject, element);
223 }
224 }
225 }
226 }
227
228 /**
229 * Constructor. Each factory object should be able to create many documents
230 * out of many given projects without having to call for new factory
231 * objects (although there is no real saving in doing so).
232 */
233 public DOMLUpgradeFactory() {
234 }
235
236 /**
237 * Create a DOM Document out of an existing project data structure.
238 *
239 * @param project
240 * The DODSProject tree structure that will be read.
241 * @return
242 * A Document which is a DOML style DOM object.
243 */
244 public Document createDocument(DODSProject project)
245 throws UpException {
246
247 DOMImplementation domImpl = DOMImplementationImpl.getDOMImplementation();
248 DocumentType docType = domImpl.createDocumentType("doml", "doml",
249 "doml.dtd");
250 Document doc = domImpl.createDocument("project.doml",
251 "doml",
252 docType);
253
254
255 Element dbElement = doc.createElement("database");
256 String database = project.getDatabase();
257
258 doc.getDocumentElement().appendChild(dbElement);
259 placeDataObjectInDocument(project, dbElement);
260
261 if (database != null && database.trim().length() > 0) {
262 dbElement.setAttribute("database", database);
263 }
264
265 return doc;
266 }
267 }