1 19 20 package org.netbeans.modules.xml.retriever.catalog.impl; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.net.URI ; 28 import java.net.URISyntaxException ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.List ; 32 import java.util.logging.Logger ; 33 import org.netbeans.api.project.Project; 34 import org.netbeans.modules.xml.retriever.catalog.Utilities; 35 import org.netbeans.modules.xml.xam.dom.DocumentModel; 36 import org.netbeans.modules.xml.retriever.catalog.CatalogElement; 37 import org.netbeans.modules.xml.retriever.catalog.CatalogEntry; 38 import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel; 39 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 40 import org.openide.filesystems.FileObject; 41 import org.openide.filesystems.FileUtil; 42 43 47 public class CatalogWriteModelImpl extends CatalogModelImpl implements CatalogWriteModel{ 48 49 public static final String PROPERTY_CHANGE_PROPERTY_KEY= "CatalogModelImpl.PropertyChange.Property"; 50 51 PropertyChangeSupport pcs = new PropertyChangeSupport (this); 52 53 private static final Logger logger = Utilities.getLogger(); 54 55 private DocumentModel.State currentStateOfCatalog; 56 57 private CatalogFileWrapper catalogWrapper = null; 58 59 62 protected CatalogWriteModelImpl(Project prj) throws IOException { 63 super(prj); 64 } 65 66 70 protected CatalogWriteModelImpl(FileObject catalogFileObject) throws IOException { 71 super(catalogFileObject); 72 } 73 74 75 boolean unitTestSaveStrategy = false; 77 public CatalogWriteModelImpl(File myProjectRootFile) throws IOException { 78 super(myProjectRootFile); 79 unitTestSaveStrategy = true; 80 } 81 public CatalogWriteModelImpl(){ 83 84 } 85 86 public URI searchURI(URI locationURI){ 87 if(locationURI == null) 88 return null; 89 bootStrapCatalog(); 90 if(catalogFileObject != null){ 91 File publicCatalogFile = FileUtil.toFile(catalogFileObject); 93 if(publicCatalogFile.isFile()){ 94 try { 95 URI strRes = resolveUsingApacheCatalog(publicCatalogFile, locationURI.toString()); 96 } catch (IOException ex) { 97 return null; 98 } catch (CatalogModelException ex) { 99 return null; 100 } 101 } 102 } 103 return null; 104 } 105 106 107 public DocumentModel.State getState(){ 108 return currentStateOfCatalog; 109 } 110 111 public synchronized void addURI(URI locationURI, FileObject fileObj) throws IOException { 112 URI fileObjURI = FileUtil.toFile(fileObj).toURI(); 113 addURI(locationURI, fileObjURI); 114 } 115 116 public synchronized void addURI(URI locationURI, URI fileObjURI) throws IOException { 117 if(this.catalogFileObject == null) 118 return; 119 removeURI(locationURI); 121 122 bootStrapCatalog(); 123 124 URI master = FileUtil.toFile(this.catalogFileObject).toURI(); 125 126 String finalDestStr = Utilities.relativize(master, fileObjURI); 127 CatalogEntry catEnt = new CatalogEntryImpl(CatalogElement.system, locationURI.toString(), finalDestStr); 128 catalogWrapper.addSystem(catEnt); 129 } 130 131 public String toString(){ 132 return "This Public Catalog FO:"+this.catalogFileObject; 133 } 134 135 public synchronized void removeURI(URI locationURI) throws IOException { 136 logger.finer("ENTRING:"+locationURI); 137 if(this.catalogFileObject == null) 138 return; 139 bootStrapCatalog(); 140 List <CatalogEntry> catEntList = catalogWrapper.getSystems(); 141 if(catEntList == null) 142 return; 143 CatalogEntry remVal = null; 144 for(CatalogEntry catEnt : catEntList){ 145 if(catEnt.getSource().equals(locationURI.toString())) 146 remVal = catEnt; 147 } 148 logger.finer("Removing Value: "+remVal); 149 if(remVal == null) 150 return; 151 int index = catEntList.indexOf(remVal); 152 catalogWrapper.deleteSystem(index); 153 if(catEntList.size() == 1){ 154 logger.finer("There are no more entries so removing catalog file"); 156 } 159 logger.finer("RETURN: "+catEntList.size()); 160 } 161 162 public Collection <CatalogEntry> getCatalogEntries() { 163 if(this.catalogFileObject == null) 164 return Collections.emptyList(); 165 bootStrapCatalog(); 166 List <CatalogEntry> catEntList = catalogWrapper.getSystems(); 167 if(catEntList == null) 168 return Collections.emptyList(); 169 for(CatalogEntry catEnt: catEntList) 170 ((CatalogEntryImpl)catEnt).setCatalogModel(this); 171 return catEntList; 172 } 173 174 public boolean isWellformed() { 175 bootStrapCatalog(); 176 currentStateOfCatalog = catalogWrapper.getCatalogState(); 177 if(currentStateOfCatalog == DocumentModel.State.NOT_WELL_FORMED) 178 return false; 179 else 180 return true; 181 } 182 183 public FileObject getCatalogFileObject() { 184 return this.catalogFileObject; 185 } 186 187 public void addPropertychangeListener(PropertyChangeListener pcl) { 188 this.pcs.addPropertyChangeListener(PROPERTY_CHANGE_PROPERTY_KEY, pcl); 189 } 190 191 public void removePropertyChangeListener(PropertyChangeListener pcl) { 192 this.pcs.removePropertyChangeListener(PROPERTY_CHANGE_PROPERTY_KEY, pcl); 193 } 194 195 private synchronized void bootStrapCatalog(){ 196 if(catalogWrapper == null){ 197 try { 198 catalogWrapper = CatalogFileWrapperDOMImpl.getInstance(this.catalogFileObject, unitTestSaveStrategy); 199 200 if(catalogWrapper == null) 201 throw new IllegalStateException ("Could not get CatalogFileWrapper"); 202 currentStateOfCatalog = catalogWrapper.getCatalogState(); 203 catalogWrapper.addPropertyChangeListener(new PropertyChangeListener () { 204 public void propertyChange(PropertyChangeEvent evt) { 205 pcs.firePropertyChange(evt); 206 currentStateOfCatalog = catalogWrapper.getCatalogState(); 207 } 208 }); 209 } catch (IOException ex) { 210 throw new IllegalStateException (ex); 211 } 212 } 213 if(catalogWrapper != null){ 214 if(catalogWrapper.getCatalogState() == DocumentModel.State.NOT_WELL_FORMED) 215 throw new IllegalStateException ("Catalog file not wellformed"); 216 } 217 218 219 } 220 221 public void addNextCatalog(URI nextCatalogFileURI, boolean relativize) throws IOException { 222 if(this.catalogFileObject == null) 223 return; 224 225 String nextCatalogFileURIStr = nextCatalogFileURI.toString(); 226 if(nextCatalogFileURI.isAbsolute() && relativize){ 227 nextCatalogFileURIStr = Utilities.relativize(FileUtil.toFile(this.catalogFileObject). 229 toURI(), nextCatalogFileURI); 230 } 231 232 try { 233 removeNextCatalog(new URI (nextCatalogFileURIStr)); 234 } catch (URISyntaxException ex) { 235 } catch (IOException ex) { 236 } 237 238 bootStrapCatalog(); 239 240 CatalogEntry catEnt = new CatalogEntryImpl(CatalogElement.nextCatalog, 241 nextCatalogFileURIStr, null); 242 catalogWrapper.addNextCatalog(catEnt); 243 } 244 245 public void removeNextCatalog(URI nextCatalogFileRelativeURI) throws IOException { 246 logger.finer("ENTRING:"+nextCatalogFileRelativeURI); 247 if(this.catalogFileObject == null) 248 return; 249 250 bootStrapCatalog(); 251 252 List <CatalogEntry> catEntList = catalogWrapper.getNextCatalogs(); 253 if(catEntList == null) 254 return; 255 CatalogEntry remVal = null; 256 for(CatalogEntry catEnt : catEntList){ 257 if(catEnt.getSource().equals(nextCatalogFileRelativeURI.toString())) 258 remVal = catEnt; 259 } 260 logger.finer("Removing Value: "+remVal); 261 if(remVal == null) 262 return; 263 int index = catEntList.indexOf(remVal); 264 catalogWrapper.deleteNextCatalog(index); 265 if(catEntList.size() == 1){ 266 logger.finer("There are no more entries so removing catalog file"); 268 } 271 logger.finer("RETURN: "+catEntList.size()); 272 273 } 274 275 276 } 277 | Popular Tags |