KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * XAMCatalogWriteModelImpl.java
3  *
4  * Created on December 14, 2006, 12:06 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.retriever.catalog.impl;
11
12 import java.beans.PropertyChangeListener JavaDoc;
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.net.URI JavaDoc;
16 import java.net.URISyntaxException JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collection JavaDoc;
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 /**
37  *
38  * @author girix
39  */

40 public class XAMCatalogWriteModelImpl extends CatalogModelImpl implements CatalogWriteModel {
41     
42     protected XAMCatalogWriteModelImpl(Project prj) throws CatalogModelException, IOException JavaDoc{
43         super(prj);
44         modelSource = createModelSource(super.catalogFileObject);
45     }
46     
47     /**
48      * Constructor for creating a CatalogWriteModel for the given file object.
49      * The catalogFileObject should not be null
50      */

51     protected XAMCatalogWriteModelImpl(FileObject catalogFileObject) throws CatalogModelException, IOException JavaDoc{
52         super(catalogFileObject);
53         modelSource = createModelSource(super.catalogFileObject);
54     }
55     
56     
57     //for unit tests
58
boolean unitTestSaveStrategy = false;
59     public XAMCatalogWriteModelImpl(File JavaDoc myProjectRootFile) throws IOException JavaDoc, 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     /**
70      * This method will be called by the constructor for instantiating the protected
71      * object "modelSource" (that is returned by getModelSource()).
72      * Since, this impl is based on XAM, it has a dependency on ModelSource. ModelSource's lookup
73      * must contain at the very least FileObject and javax.swing.Document objects for XAM to work.
74      * NOTE: Unit test env needs to overwrite this method and return proper test env model source.
75      * and also the ModelSource must contain proper Swing document and FileObject in the lookup.
76      **/

77     protected ModelSource createModelSource(FileObject catFileObject) throws CatalogModelException {
78         return Utilities.createModelSource(super.catalogFileObject, true);
79         
80     }
81     
82     
83     
84     public URI JavaDoc searchURI(URI JavaDoc locationURI) {
85         if(locationURI == null)
86             return null;
87         URI JavaDoc strRes = null;
88         if(catalogFileObject != null){
89             //look up in the global catalog
90
File JavaDoc publicCatalogFile = FileUtil.toFile(catalogFileObject);
91             if(publicCatalogFile.isFile()){
92                 try {
93                     strRes = resolveUsingApacheCatalog(publicCatalogFile, locationURI.toString());
94                 } catch (IOException JavaDoc 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 JavaDoc locationURI, FileObject fileObj) throws IOException JavaDoc {
105         URI JavaDoc fileObjURI = FileUtil.toFile(fileObj).toURI();
106         addURI(locationURI, fileObjURI);
107     }
108     
109     public synchronized void addURI(URI JavaDoc locationURI, URI JavaDoc fileObjURI) throws IOException JavaDoc {
110         if(this.catalogFileObject == null)
111             return;
112         //remove the old entry if exists
113
removeURI(locationURI);
114         
115         URI JavaDoc master = FileUtil.toFile(this.catalogFileObject).toURI();
116         
117         String JavaDoc finalDestStr = Utilities.relativize(master, fileObjURI);
118         URI JavaDoc finalDestStrURI;
119         try {
120             finalDestStrURI = new URI JavaDoc(finalDestStr);
121         } catch (URISyntaxException JavaDoc ex) {
122             throw new IOException JavaDoc("Invalid URI: "+finalDestStr);
123         }
124         
125         System JavaDoc 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 JavaDoc locationURI) throws IOException JavaDoc {
139         System JavaDoc delete = null;
140         getCatalogModel().sync();
141         for(System JavaDoc 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 JavaDoc<CatalogEntry> getCatalogEntries() {
159         ArrayList JavaDoc<CatalogEntry> result = new ArrayList JavaDoc<CatalogEntry>();
160         for(System JavaDoc 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 JavaDoc pcl) {
186         getCatalogModel().addPropertyChangeListener(pcl);
187     }
188     
189     public void removePropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
190         getCatalogModel().removePropertyChangeListener(pcl);
191     }
192     
193     public void addNextCatalog(URI JavaDoc nextCatalogFileURI, boolean relativize) throws IOException JavaDoc {
194         if(this.catalogFileObject == null)
195             return;
196         
197         String JavaDoc nextCatalogFileURIStr = nextCatalogFileURI.toString();
198         if(nextCatalogFileURI.isAbsolute() && relativize){
199             //then resolve URI relative to this catalog file
200
nextCatalogFileURIStr = Utilities.relativize(FileUtil.toFile(this.catalogFileObject).
201                     toURI(), nextCatalogFileURI);
202         }
203         try {
204             nextCatalogFileURI = new URI JavaDoc(nextCatalogFileURIStr);
205         } catch (URISyntaxException JavaDoc ex) {
206             throw new IOException JavaDoc("Invalid URI: "+nextCatalogFileURIStr);
207         }
208         
209         try {
210             removeNextCatalog(nextCatalogFileURI);
211         } catch (IOException JavaDoc 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 JavaDoc nextCatalogFileRelativeURI) throws IOException JavaDoc {
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 JavaDoc 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