KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > dbschema > SchemaElementUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.dbschema;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.ObjectInput JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26
27 import org.openide.filesystems.FileObject;
28 import org.netbeans.api.java.classpath.ClassPath;
29
30 import org.netbeans.modules.dbschema.migration.archiver.XMLInputStream;
31
32 import org.netbeans.modules.dbschema.util.NameUtil;
33 import org.openide.loaders.DataObjectNotFoundException;
34
35 public class SchemaElementUtil {
36
37     private static FileObject schemaFO = null;
38
39     /** Returns the SchemaElement object associated with the schema with
40      * the given string name and object. The second argument is meant to
41      * help define the context for loading of the schema and can be a
42      * FileObject[] or FileObject. Note that if if FileObject[] is used,
43      * the first match is returned if it's not already in the cache.
44      * It might be extended later to accept a Project as well.
45      * Any other non-null value for the second argument will result in an
46      * UnsupportedOperationException.
47      * @param name the schema name
48      * @param obj the schema context
49      * @return the SchemaElement object for the given schema name
50      */

51     public static SchemaElement forName(String JavaDoc name, Object JavaDoc obj) {
52         SchemaElement se = SchemaElement.getLastSchema();
53         
54         if (se != null && se.getName().getFullName().equals(name) && schemaFO == null)
55             return se;
56         else
57             synchronized (SchemaElement.schemaCache) {
58                 String JavaDoc tempURL = ""; //NOI18N
59
if (schemaFO != null)
60                     try {
61                         tempURL = schemaFO.getURL().toString();
62                     } catch (Exception JavaDoc exc) {
63                         org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, exc);
64                     }
65                 
66                 if (schemaFO == null)
67                     se = (SchemaElement) SchemaElement.schemaCache.get(name);
68                 else
69                     se = (SchemaElement) SchemaElement.schemaCache.get(name + "#" + tempURL); //NOI18N
70
if (se != null)
71                     return se;
72
73                 FileObject fo = null;
74                 if (schemaFO == null) {
75                     if (obj instanceof FileObject) {
76                         fo = findResource((FileObject)obj, name);
77                     }
78                     else if (obj instanceof FileObject[]) {
79                         FileObject[] sourceRoots = (FileObject[])obj;
80
81                         for (int i = 0; ((fo == null) && (i < sourceRoots.length)); i++) {
82                             fo = findResource(sourceRoots[i], name);
83                         }
84                     } else if (obj != null) {
85                         throw new UnsupportedOperationException JavaDoc(
86                             "Cannot lookup schema " + name +
87                             " in context of type " + obj.getClass() +
88                             " expected FileObject, FileObject[], or null.");
89                     }
90                 } else
91                     fo = schemaFO;
92                 if (fo != null) {
93                     try {
94                         org.openide.loaders.DataObject dataObject = org.openide.loaders.DataObject.find(fo);
95
96                         if (dataObject != null)
97                             se = (SchemaElement)dataObject.getCookie(SchemaElement.class);
98                     }
99                     catch (ClassCastException JavaDoc e) {
100                         // really ugly, caused by faulty code in DBSchemaDataObject.getCookie(...)
101
// just find it by unarchiving (below)
102
}
103                     catch (DataObjectNotFoundException e) {
104                         org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
105                         // just find it by unarchiving (below)
106
}
107                     if (se == null) {
108                         try {
109                             org.openide.awt.StatusDisplayer.getDefault().setStatusText(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("RetrievingSchema")); //NOI18N
110

111                             InputStream JavaDoc s = fo.getInputStream();
112                             ObjectInput JavaDoc i = new XMLInputStream(s);
113                             se = (SchemaElement) i.readObject();
114                             if (!se.isCompatibleVersion()) {
115                                 String JavaDoc message = MessageFormat.format(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("PreviousVersion"), new String JavaDoc[] {name}); //NOI18N
116
org.openide.DialogDisplayer.getDefault().notify(new org.openide.NotifyDescriptor.Message(message, org.openide.NotifyDescriptor.ERROR_MESSAGE));
117                             }
118                             i.close();
119
120                             se.setName(DBIdentifier.create(name));
121
122                             if (schemaFO == null)
123                                 SchemaElement.addToCache(se);
124                             else {
125                                 SchemaElement.schemaCache.put(name + "#" + tempURL, se); //NOI18N
126
SchemaElement.setLastSchema(se);
127                             }
128
129                             // MBO: now set the declaring schema in TableElement(transient field)
130
TableElement tables[] = se.getTables();
131                             int size = (tables != null) ? tables.length : 0;
132                             for (int j = 0; j < size; j++)
133                                 tables[j].setDeclaringSchema(se);
134
135                         } catch (Exception JavaDoc e) {
136                             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
137                             org.openide.awt.StatusDisplayer.getDefault().setStatusText(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("CannotRetrieve")); //NOI18N
138
}
139                     }
140                 } else
141                     org.openide.ErrorManager.getDefault().log(org.openide.ErrorManager.INFORMATIONAL, ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("SchemaNotFound")); //NOI18N
142

143                 return se;
144             }
145     }
146
147     /** Returns the SchemaElement object associated with the schema with the given file object.
148      * @param fo the file object
149      * @return the SchemaElement object for the given file object
150      */

151     public static SchemaElement forName(FileObject fo) {
152         schemaFO = fo;
153         SchemaElement se = forName(fo.getName(), null);
154         schemaFO = null;
155         
156         return se;
157     }
158
159     private static FileObject findResource(FileObject sourceRoot, String JavaDoc name) {
160         ClassPath cp = ClassPath.getClassPath(sourceRoot, ClassPath.SOURCE);
161         return cp.findResource(NameUtil.getSchemaResourceName(name));
162     }
163 }
164
Popular Tags