1 19 20 package org.netbeans.modules.dbschema; 21 22 import java.io.InputStream ; 23 import java.io.ObjectInput ; 24 import java.text.MessageFormat ; 25 import java.util.ResourceBundle ; 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 51 public static SchemaElement forName(String name, Object 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 tempURL = ""; if (schemaFO != null) 60 try { 61 tempURL = schemaFO.getURL().toString(); 62 } catch (Exception 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); 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 ( 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 e) { 100 } 103 catch (DataObjectNotFoundException e) { 104 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 105 } 107 if (se == null) { 108 try { 109 org.openide.awt.StatusDisplayer.getDefault().setStatusText(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("RetrievingSchema")); 111 InputStream s = fo.getInputStream(); 112 ObjectInput i = new XMLInputStream(s); 113 se = (SchemaElement) i.readObject(); 114 if (!se.isCompatibleVersion()) { 115 String message = MessageFormat.format(ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("PreviousVersion"), new String [] {name}); 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); SchemaElement.setLastSchema(se); 127 } 128 129 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 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")); } 139 } 140 } else 141 org.openide.ErrorManager.getDefault().log(org.openide.ErrorManager.INFORMATIONAL, ResourceBundle.getBundle("org.netbeans.modules.dbschema.resources.Bundle").getString("SchemaNotFound")); 143 return se; 144 } 145 } 146 147 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 name) { 160 ClassPath cp = ClassPath.getClassPath(sourceRoot, ClassPath.SOURCE); 161 return cp.findResource(NameUtil.getSchemaResourceName(name)); 162 } 163 } 164 | Popular Tags |