KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > impl > CatalogWriteModelImpl


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.xml.retriever.catalog.impl;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URI JavaDoc;
28 import java.net.URISyntaxException JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.logging.Logger JavaDoc;
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 /**
44  *
45  * @author girix
46  */

47 public class CatalogWriteModelImpl extends CatalogModelImpl implements CatalogWriteModel{
48     
49     public static final String JavaDoc PROPERTY_CHANGE_PROPERTY_KEY= "CatalogModelImpl.PropertyChange.Property";
50     
51     PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
52     
53     private static final Logger JavaDoc logger = Utilities.getLogger();
54     
55     private DocumentModel.State currentStateOfCatalog;
56     
57     private CatalogFileWrapper catalogWrapper = null;
58     
59     /**
60      * Constructor for public catalog files
61      */

62     protected CatalogWriteModelImpl(Project prj) throws IOException JavaDoc{
63         super(prj);
64     }
65     
66     /**
67      * Constructor for creating a CatalogWriteModel for the given file object.
68      * The catalogFileObject should not be null
69      */

70     protected CatalogWriteModelImpl(FileObject catalogFileObject) throws IOException JavaDoc{
71         super(catalogFileObject);
72     }
73     
74     
75     //for unit tests
76
boolean unitTestSaveStrategy = false;
77     public CatalogWriteModelImpl(File JavaDoc myProjectRootFile) throws IOException JavaDoc{
78         super(myProjectRootFile);
79         unitTestSaveStrategy = true;
80     }
81     //For unit tests
82
public CatalogWriteModelImpl(){
83         
84     }
85     
86     public URI JavaDoc searchURI(URI JavaDoc locationURI){
87         if(locationURI == null)
88             return null;
89         bootStrapCatalog();
90         if(catalogFileObject != null){
91             //look up in the global catalog
92
File JavaDoc publicCatalogFile = FileUtil.toFile(catalogFileObject);
93             if(publicCatalogFile.isFile()){
94                 try {
95                     URI JavaDoc strRes = resolveUsingApacheCatalog(publicCatalogFile, locationURI.toString());
96                 } catch (IOException JavaDoc 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 JavaDoc locationURI, FileObject fileObj) throws IOException JavaDoc {
112         URI JavaDoc fileObjURI = FileUtil.toFile(fileObj).toURI();
113         addURI(locationURI, fileObjURI);
114     }
115     
116     public synchronized void addURI(URI JavaDoc locationURI, URI JavaDoc fileObjURI) throws IOException JavaDoc {
117         if(this.catalogFileObject == null)
118             return;
119         //remove the old entry if exists
120
removeURI(locationURI);
121         
122         bootStrapCatalog();
123         
124         URI JavaDoc master = FileUtil.toFile(this.catalogFileObject).toURI();
125         
126         String JavaDoc finalDestStr = Utilities.relativize(master, fileObjURI);
127         CatalogEntry catEnt = new CatalogEntryImpl(CatalogElement.system, locationURI.toString(), finalDestStr);
128         catalogWrapper.addSystem(catEnt);
129     }
130     
131     public String JavaDoc toString(){
132         return "This Public Catalog FO:"+this.catalogFileObject;
133     }
134     
135     public synchronized void removeURI(URI JavaDoc locationURI) throws IOException JavaDoc {
136         logger.finer("ENTRING:"+locationURI);
137         if(this.catalogFileObject == null)
138             return;
139         bootStrapCatalog();
140         List JavaDoc<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             //file has no entry, so just delete it
155
logger.finer("There are no more entries so removing catalog file");
156             //publicCatWrap.cleanInstance();
157
//publicCatWrap = null;
158
}
159         logger.finer("RETURN: "+catEntList.size());
160     }
161     
162     public Collection JavaDoc<CatalogEntry> getCatalogEntries() {
163         if(this.catalogFileObject == null)
164             return Collections.emptyList();
165         bootStrapCatalog();
166         List JavaDoc<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 JavaDoc pcl) {
188         this.pcs.addPropertyChangeListener(PROPERTY_CHANGE_PROPERTY_KEY, pcl);
189     }
190     
191     public void removePropertyChangeListener(PropertyChangeListener JavaDoc 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 JavaDoc("Could not get CatalogFileWrapper");
202                 currentStateOfCatalog = catalogWrapper.getCatalogState();
203                 catalogWrapper.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
204                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
205                         pcs.firePropertyChange(evt);
206                         currentStateOfCatalog = catalogWrapper.getCatalogState();
207                     }
208                 });
209             } catch (IOException JavaDoc ex) {
210                 throw new IllegalStateException JavaDoc(ex);
211             }
212         }
213         if(catalogWrapper != null){
214             if(catalogWrapper.getCatalogState() == DocumentModel.State.NOT_WELL_FORMED)
215                 throw new IllegalStateException JavaDoc("Catalog file not wellformed");
216         }
217         
218         
219     }
220     
221     public void addNextCatalog(URI JavaDoc nextCatalogFileURI, boolean relativize) throws IOException JavaDoc {
222         if(this.catalogFileObject == null)
223             return;
224         
225         String JavaDoc nextCatalogFileURIStr = nextCatalogFileURI.toString();
226         if(nextCatalogFileURI.isAbsolute() && relativize){
227             //then resolve URI relative to this catalog file
228
nextCatalogFileURIStr = Utilities.relativize(FileUtil.toFile(this.catalogFileObject).
229                     toURI(), nextCatalogFileURI);
230         }
231         
232         try {
233             removeNextCatalog(new URI JavaDoc(nextCatalogFileURIStr));
234         } catch (URISyntaxException JavaDoc ex) {
235         } catch (IOException JavaDoc 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 JavaDoc nextCatalogFileRelativeURI) throws IOException JavaDoc {
246         logger.finer("ENTRING:"+nextCatalogFileRelativeURI);
247         if(this.catalogFileObject == null)
248             return;
249         
250         bootStrapCatalog();
251         
252         List JavaDoc<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             //file has no entry, so just delete it
267
logger.finer("There are no more entries so removing catalog file");
268             //publicCatWrap.cleanInstance();
269
//publicCatWrap = null;
270
}
271         logger.finer("RETURN: "+catEntList.size());
272         
273     }
274     
275     
276 }
277
Popular Tags