001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.retriever.catalog.impl;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.beans.PropertyChangeSupport;
047: import java.io.File;
048: import java.io.IOException;
049: import java.net.URI;
050: import java.net.URISyntaxException;
051: import java.util.Collection;
052: import java.util.Collections;
053: import java.util.List;
054: import java.util.logging.Logger;
055: import org.netbeans.api.project.Project;
056: import org.netbeans.modules.xml.retriever.catalog.Utilities;
057: import org.netbeans.modules.xml.xam.dom.DocumentModel;
058: import org.netbeans.modules.xml.retriever.catalog.CatalogElement;
059: import org.netbeans.modules.xml.retriever.catalog.CatalogEntry;
060: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
061: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064:
065: /**
066: *
067: * @author girix
068: */
069: public class CatalogWriteModelImpl extends CatalogModelImpl implements
070: CatalogWriteModel {
071:
072: public static final String PROPERTY_CHANGE_PROPERTY_KEY = "CatalogModelImpl.PropertyChange.Property";
073:
074: PropertyChangeSupport pcs = new PropertyChangeSupport(this );
075:
076: private static Logger logger = Logger
077: .getLogger(CatalogWriteModelImpl.class.getName());
078:
079: private DocumentModel.State currentStateOfCatalog;
080:
081: private CatalogFileWrapper catalogWrapper = null;
082:
083: /**
084: * Constructor for public catalog files
085: */
086: protected CatalogWriteModelImpl(Project prj) throws IOException {
087: super (prj);
088: }
089:
090: /**
091: * Constructor for creating a CatalogWriteModel for the given file object.
092: * The catalogFileObject should not be null
093: */
094: protected CatalogWriteModelImpl(FileObject catalogFileObject)
095: throws IOException {
096: super (catalogFileObject);
097: }
098:
099: //for unit tests
100: boolean unitTestSaveStrategy = false;
101:
102: public CatalogWriteModelImpl(File myProjectRootFile)
103: throws IOException {
104: super (myProjectRootFile);
105: unitTestSaveStrategy = true;
106: }
107:
108: //For unit tests
109: public CatalogWriteModelImpl() {
110:
111: }
112:
113: public URI searchURI(URI locationURI) {
114: if (locationURI == null)
115: return null;
116: bootStrapCatalog();
117: URI strRes = null;
118: if (catalogFileObject != null) {
119: //look up in the global catalog
120: File publicCatalogFile = FileUtil.toFile(catalogFileObject);
121: if (publicCatalogFile.isFile()) {
122: try {
123: strRes = resolveUsingApacheCatalog(
124: publicCatalogFile, locationURI.toString());
125: } catch (IOException ex) {
126: return null;
127: } catch (CatalogModelException ex) {
128: return null;
129: }
130: }
131: }
132: return strRes;
133: }
134:
135: public DocumentModel.State getState() {
136: return currentStateOfCatalog;
137: }
138:
139: public synchronized void addURI(URI locationURI, FileObject fileObj)
140: throws IOException {
141: URI fileObjURI = FileUtil.toFile(fileObj).toURI();
142: addURI(locationURI, fileObjURI);
143: }
144:
145: public synchronized void addURI(URI locationURI, URI fileObjURI)
146: throws IOException {
147: if (this .catalogFileObject == null)
148: return;
149: //remove the old entry if exists
150: removeURI(locationURI);
151:
152: bootStrapCatalog();
153:
154: URI master = FileUtil.toFile(this .catalogFileObject).toURI();
155:
156: String finalDestStr = Utilities.relativize(master, fileObjURI);
157: CatalogEntry catEnt = new CatalogEntryImpl(
158: CatalogElement.system, locationURI.toString(),
159: finalDestStr);
160: catalogWrapper.addSystem(catEnt);
161: }
162:
163: /*public void addURI(URI locationURI, FileObject fileObj, FileObject referringFileObject) throws IOException {
164: HashMap<String, String> extraAttrs = calculateExtraAttributes(fileObj, referringFileObject);
165: if(extraAttrs == null){
166: addURI(locationURI, fileObj);
167: return;
168: }
169:
170: if(this.catalogFileObject == null)
171: return;
172: //remove the old entry if exists
173: //TO DO: Handle multiple files refering same URI from same dir
174: //removeURI(locationURI);
175:
176: bootStrapCatalog();
177:
178: URI master = FileUtil.toFile(this.catalogFileObject).toURI();
179: URI fileObjURI = FileUtil.toFile(fileObj).toURI();
180:
181: String finalDestStr = Utilities.relativize(master, fileObjURI);
182: CatalogEntry catEnt = new CatalogEntryImpl(CatalogElement.system,
183: locationURI.toString(), finalDestStr, extraAttrs);
184: catalogWrapper.addSystem(catEnt);
185:
186: }*/
187:
188: /*protected HashMap<String, String> calculateExtraAttributes(FileObject fileObj, FileObject referringFileObject){
189: /*Get the FO of this URI
190: *Find out if FO belongs to this project
191: * Yes : return empty map
192: * No: and belongs to some project which has common path, calculate extra attributes
193: * xprojectCatalogFileLocation and referencingFiles
194: */
195: /*if(fileObj == null)
196: return null;
197: Project foprj = FileOwnerQuery.getOwner(fileObj);
198: Project myprj = FileOwnerQuery.getOwner(this.catalogFileObject);
199:
200: if(myprj == null)
201: return null;
202: if(myprj.equals(foprj))
203: return null;
204: if(foprj == null)
205: return null;
206:
207: //get the path to the cross proj catalog
208: URI master = FileUtil.toFile(this.catalogFileObject).toURI();
209: FileObject catFO = null;
210: try {
211: catFO = Utilities.getCatalogFile(fileObj);
212: } catch (IOException ex) {
213: }
214: if(catFO == null)
215: return null;
216:
217: URI catURI = FileUtil.toFile(catFO).toURI();
218:
219: //relativize to this cat file
220: String catStr = Utilities.relativize(master, catURI).toString();
221:
222: HashMap <String, String> result = new HashMap<String, String>();
223:
224: result.put(CatalogAttribute.xprojectCatalogFileLocation.toString(), catStr);
225:
226:
227: URI refURI = FileUtil.toFile(referringFileObject).toURI();
228:
229: //relativize to this cat file
230: String refStr = Utilities.relativize(master, refURI).toString();
231:
232: result.put(CatalogAttribute.referencingFiles.toString(), refStr);
233:
234: return result;
235:
236: }*/
237:
238: public String toString() {
239: return "This Public Catalog FO:" + this .catalogFileObject;
240: }
241:
242: public synchronized void removeURI(URI locationURI)
243: throws IOException {
244: logger.finer("ENTRING:" + locationURI);
245: if (this .catalogFileObject == null)
246: return;
247: bootStrapCatalog();
248: List<CatalogEntry> catEntList = catalogWrapper.getSystems();
249: if (catEntList == null)
250: return;
251: CatalogEntry remVal = null;
252: for (CatalogEntry catEnt : catEntList) {
253: if (catEnt.getSource().equals(locationURI.toString()))
254: remVal = catEnt;
255: }
256: logger.finer("Removing Value: " + remVal);
257: if (remVal == null)
258: return;
259: int index = catEntList.indexOf(remVal);
260: catalogWrapper.deleteSystem(index);
261: if (catEntList.size() == 1) {
262: //file has no entry, so just delete it
263: logger
264: .finer("There are no more entries so removing catalog file");
265: //publicCatWrap.cleanInstance();
266: //publicCatWrap = null;
267: }
268: logger.finer("RETURN: " + catEntList.size());
269: }
270:
271: public Collection<CatalogEntry> getCatalogEntries() {
272: if (this .catalogFileObject == null)
273: return Collections.emptyList();
274: bootStrapCatalog();
275: List<CatalogEntry> catEntList = catalogWrapper.getSystems();
276: if (catEntList == null)
277: return Collections.emptyList();
278: for (CatalogEntry catEnt : catEntList)
279: ((CatalogEntryImpl) catEnt).setCatalogModel(this );
280: return catEntList;
281: }
282:
283: public boolean isWellformed() {
284: bootStrapCatalog();
285: currentStateOfCatalog = catalogWrapper.getCatalogState();
286: if (currentStateOfCatalog == DocumentModel.State.NOT_WELL_FORMED)
287: return false;
288: else
289: return true;
290: }
291:
292: public FileObject getCatalogFileObject() {
293: return this .catalogFileObject;
294: }
295:
296: public void addPropertychangeListener(PropertyChangeListener pcl) {
297: this .pcs.addPropertyChangeListener(
298: PROPERTY_CHANGE_PROPERTY_KEY, pcl);
299: }
300:
301: public void removePropertyChangeListener(PropertyChangeListener pcl) {
302: this .pcs.removePropertyChangeListener(
303: PROPERTY_CHANGE_PROPERTY_KEY, pcl);
304: }
305:
306: private synchronized void bootStrapCatalog() {
307: if (catalogWrapper == null) {
308: try {
309: catalogWrapper = CatalogFileWrapperDOMImpl.getInstance(
310: this .catalogFileObject, unitTestSaveStrategy);
311:
312: if (catalogWrapper == null)
313: throw new IllegalStateException(
314: "Could not get CatalogFileWrapper");
315: currentStateOfCatalog = catalogWrapper
316: .getCatalogState();
317: catalogWrapper
318: .addPropertyChangeListener(new PropertyChangeListener() {
319: public void propertyChange(
320: PropertyChangeEvent evt) {
321: pcs.firePropertyChange(evt);
322: currentStateOfCatalog = catalogWrapper
323: .getCatalogState();
324: }
325: });
326: } catch (IOException ex) {
327: throw new IllegalStateException(ex);
328: }
329: }
330: if (catalogWrapper != null) {
331: if (catalogWrapper.getCatalogState() == DocumentModel.State.NOT_WELL_FORMED)
332: throw new IllegalStateException(
333: "Catalog file not wellformed");
334: }
335:
336: }
337:
338: public void addNextCatalog(URI nextCatalogFileURI,
339: boolean relativize) throws IOException {
340: if (this .catalogFileObject == null)
341: return;
342:
343: String nextCatalogFileURIStr = nextCatalogFileURI.toString();
344: if (nextCatalogFileURI.isAbsolute() && relativize) {
345: //then resolve URI relative to this catalog file
346: nextCatalogFileURIStr = Utilities.relativize(FileUtil
347: .toFile(this .catalogFileObject).toURI(),
348: nextCatalogFileURI);
349: }
350:
351: try {
352: removeNextCatalog(new URI(nextCatalogFileURIStr));
353: } catch (URISyntaxException ex) {
354: } catch (IOException ex) {
355: }
356:
357: bootStrapCatalog();
358:
359: CatalogEntry catEnt = new CatalogEntryImpl(
360: CatalogElement.nextCatalog, nextCatalogFileURIStr, null);
361: catalogWrapper.addNextCatalog(catEnt);
362: }
363:
364: public void removeNextCatalog(URI nextCatalogFileRelativeURI)
365: throws IOException {
366: logger.finer("ENTRING:" + nextCatalogFileRelativeURI);
367: if (this .catalogFileObject == null)
368: return;
369:
370: bootStrapCatalog();
371:
372: List<CatalogEntry> catEntList = catalogWrapper
373: .getNextCatalogs();
374: if (catEntList == null)
375: return;
376: CatalogEntry remVal = null;
377: for (CatalogEntry catEnt : catEntList) {
378: if (catEnt.getSource().equals(
379: nextCatalogFileRelativeURI.toString()))
380: remVal = catEnt;
381: }
382: logger.finer("Removing Value: " + remVal);
383: if (remVal == null)
384: return;
385: int index = catEntList.indexOf(remVal);
386: catalogWrapper.deleteNextCatalog(index);
387: if (catEntList.size() == 1) {
388: //file has no entry, so just delete it
389: logger
390: .finer("There are no more entries so removing catalog file");
391: //publicCatWrap.cleanInstance();
392: //publicCatWrap = null;
393: }
394: logger.finer("RETURN: " + catEntList.size());
395:
396: }
397:
398: }
|