1 9 10 package org.netbeans.modules.xml.retriever.catalog.impl; 11 12 import java.beans.PropertyChangeListener ; 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.net.URI ; 16 import java.net.URISyntaxException ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import org.netbeans.api.project.Project; 20 import org.netbeans.modules.xml.retriever.catalog.CatalogElement; 21 import org.netbeans.modules.xml.retriever.catalog.CatalogEntry; 22 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel; 23 import org.netbeans.modules.xml.retriever.catalog.Utilities; 24 import org.netbeans.modules.xml.retriever.catalog.model.CatalogModel; 25 import org.netbeans.modules.xml.retriever.catalog.model.CatalogModelFactory; 26 import org.netbeans.modules.xml.retriever.catalog.model.NextCatalog; 27 import org.netbeans.modules.xml.xam.Model; 28 import org.netbeans.modules.xml.xam.ModelSource; 29 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 30 import org.openide.cookies.SaveCookie; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.netbeans.modules.xml.retriever.catalog.model.System; 34 import org.openide.loaders.DataObject; 35 36 40 public class XAMCatalogWriteModelImpl extends CatalogModelImpl implements CatalogWriteModel { 41 42 protected XAMCatalogWriteModelImpl(Project prj) throws CatalogModelException, IOException { 43 super(prj); 44 modelSource = createModelSource(super.catalogFileObject); 45 } 46 47 51 protected XAMCatalogWriteModelImpl(FileObject catalogFileObject) throws CatalogModelException, IOException { 52 super(catalogFileObject); 53 modelSource = createModelSource(super.catalogFileObject); 54 } 55 56 57 boolean unitTestSaveStrategy = false; 59 public XAMCatalogWriteModelImpl(File myProjectRootFile) throws IOException , CatalogModelException{ 60 super(myProjectRootFile); 61 unitTestSaveStrategy = true; 62 modelSource = createModelSource(super.catalogFileObject); 63 } 64 65 66 private CatalogModel catalogModel; 67 private ModelSource modelSource; 68 69 77 protected ModelSource createModelSource(FileObject catFileObject) throws CatalogModelException { 78 return Utilities.createModelSource(super.catalogFileObject, true); 79 80 } 81 82 83 84 public URI searchURI(URI locationURI) { 85 if(locationURI == null) 86 return null; 87 URI strRes = null; 88 if(catalogFileObject != null){ 89 File publicCatalogFile = FileUtil.toFile(catalogFileObject); 91 if(publicCatalogFile.isFile()){ 92 try { 93 strRes = resolveUsingApacheCatalog(publicCatalogFile, locationURI.toString()); 94 } catch (IOException ex) { 95 return null; 96 } catch (CatalogModelException ex) { 97 return null; 98 } 99 } 100 } 101 return strRes; 102 } 103 104 public synchronized void addURI(URI locationURI, FileObject fileObj) throws IOException { 105 URI fileObjURI = FileUtil.toFile(fileObj).toURI(); 106 addURI(locationURI, fileObjURI); 107 } 108 109 public synchronized void addURI(URI locationURI, URI fileObjURI) throws IOException { 110 if(this.catalogFileObject == null) 111 return; 112 removeURI(locationURI); 114 115 URI master = FileUtil.toFile(this.catalogFileObject).toURI(); 116 117 String finalDestStr = Utilities.relativize(master, fileObjURI); 118 URI finalDestStrURI; 119 try { 120 finalDestStrURI = new URI (finalDestStr); 121 } catch (URISyntaxException ex) { 122 throw new IOException ("Invalid URI: "+finalDestStr); 123 } 124 125 System sys = getCatalogModel().getFactory().createSystem(); 126 127 getCatalogModel().startTransaction(); 128 try{ 129 getCatalogModel().getRootComponent().addSystem(sys); 130 sys.setSystemIDAttr(locationURI); 131 sys.setURIAttr(finalDestStrURI); 132 }finally{ 133 getCatalogModel().endTransaction(); 134 } 135 save(); 136 } 137 138 public void removeURI(URI locationURI) throws IOException { 139 System delete = null; 140 getCatalogModel().sync(); 141 for(System sys: getCatalogModel().getRootComponent().getSystems()){ 142 if(sys.getSystemIDAttr().equals(locationURI.toString())){ 143 delete = sys; 144 break; 145 } 146 } 147 if(delete != null){ 148 getCatalogModel().startTransaction(); 149 try{ 150 getCatalogModel().getRootComponent().removeSystem(delete); 151 }finally{ 152 getCatalogModel().endTransaction(); 153 } 154 save(); 155 } 156 } 157 158 public Collection <CatalogEntry> getCatalogEntries() { 159 ArrayList <CatalogEntry> result = new ArrayList <CatalogEntry>(); 160 for(System sys: getCatalogModel().getRootComponent().getSystems()){ 161 CatalogEntry catEnt = new CatalogEntryImpl(CatalogElement.system, sys.getSystemIDAttr(), 162 sys.getURIAttr()); 163 result.add(catEnt); 164 } 165 for(NextCatalog nc: getCatalogModel().getRootComponent().getNextCatalogs()){ 166 CatalogEntry catEnt = new CatalogEntryImpl(CatalogElement.nextCatalog, nc.getCatalogAttr(), 167 null); 168 result.add(catEnt); 169 } 170 return result; 171 } 172 173 public boolean isWellformed() { 174 return getCatalogModel().getState().equals(Model.State.VALID); 175 } 176 177 public org.netbeans.modules.xml.xam.dom.DocumentModel.State getState() { 178 return getCatalogModel().getState(); 179 } 180 181 public FileObject getCatalogFileObject() { 182 return super.catalogFileObject; 183 } 184 185 public void addPropertychangeListener(PropertyChangeListener pcl) { 186 getCatalogModel().addPropertyChangeListener(pcl); 187 } 188 189 public void removePropertyChangeListener(PropertyChangeListener pcl) { 190 getCatalogModel().removePropertyChangeListener(pcl); 191 } 192 193 public void addNextCatalog(URI nextCatalogFileURI, boolean relativize) throws IOException { 194 if(this.catalogFileObject == null) 195 return; 196 197 String nextCatalogFileURIStr = nextCatalogFileURI.toString(); 198 if(nextCatalogFileURI.isAbsolute() && relativize){ 199 nextCatalogFileURIStr = Utilities.relativize(FileUtil.toFile(this.catalogFileObject). 201 toURI(), nextCatalogFileURI); 202 } 203 try { 204 nextCatalogFileURI = new URI (nextCatalogFileURIStr); 205 } catch (URISyntaxException ex) { 206 throw new IOException ("Invalid URI: "+nextCatalogFileURIStr); 207 } 208 209 try { 210 removeNextCatalog(nextCatalogFileURI); 211 } catch (IOException ex) { 212 } 213 214 NextCatalog nc = getCatalogModel().getFactory().createNextCatalog(); 215 getCatalogModel().startTransaction(); 216 try{ 217 getCatalogModel().getRootComponent().addNextCatalog(nc); 218 nc.setCatalogAttr(nextCatalogFileURI); 219 }finally{ 220 getCatalogModel().endTransaction(); 221 } 222 save(); 223 } 224 225 public void removeNextCatalog(URI nextCatalogFileRelativeURI) throws IOException { 226 NextCatalog delete = null; 227 for(NextCatalog nc: getCatalogModel().getRootComponent().getNextCatalogs()){ 228 if(nc.getCatalogAttr().equals(nextCatalogFileRelativeURI.toString())){ 229 delete = nc; 230 break; 231 } 232 } 233 if(delete != null){ 234 getCatalogModel().startTransaction(); 235 try{ 236 getCatalogModel().getRootComponent().removeNextCatalog(delete); 237 }finally{ 238 getCatalogModel().endTransaction(); 239 } 240 save(); 241 } 242 } 243 244 protected void save() { 245 FileObject fo = (FileObject) getCatalogModel().getModelSource().getLookup().lookup(FileObject.class); 246 try { 247 DataObject dobj = DataObject.find(fo); 248 SaveCookie saveCookie = (SaveCookie) dobj.getCookie(SaveCookie.class); 249 if(saveCookie != null) 250 saveCookie.save(); 251 } catch (IOException ex) { 252 } 253 } 254 255 public CatalogModel getCatalogModel() { 256 return CatalogModelFactory.getInstance().getModel(getModelSource()); 257 } 258 259 public void setCatalogModel(CatalogModel catalogModel) { 260 this.catalogModel = catalogModel; 261 } 262 263 public ModelSource getModelSource() { 264 return modelSource; 265 } 266 } 267 | Popular Tags |