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: /*
043: * XAMCatalogWriteModelImpl.java
044: *
045: * Created on December 14, 2006, 12:06 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.modules.xml.retriever.catalog.impl;
052:
053: import java.beans.PropertyChangeListener;
054: import java.io.File;
055: import java.io.IOException;
056: import java.net.URI;
057: import java.net.URISyntaxException;
058: import java.util.ArrayList;
059: import java.util.Collection;
060: import org.netbeans.api.project.Project;
061: import org.netbeans.modules.xml.retriever.catalog.CatalogElement;
062: import org.netbeans.modules.xml.retriever.catalog.CatalogEntry;
063: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
064: import org.netbeans.modules.xml.retriever.catalog.Utilities;
065: import org.netbeans.modules.xml.retriever.catalog.model.CatalogModel;
066: import org.netbeans.modules.xml.retriever.catalog.model.CatalogModelFactory;
067: import org.netbeans.modules.xml.retriever.catalog.model.NextCatalog;
068: import org.netbeans.modules.xml.xam.Model;
069: import org.netbeans.modules.xml.xam.ModelSource;
070: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
071: import org.openide.cookies.SaveCookie;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.FileUtil;
074: import org.netbeans.modules.xml.retriever.catalog.model.System;
075: import org.openide.loaders.DataObject;
076:
077: /**
078: *
079: * @author girix
080: */
081: public class XAMCatalogWriteModelImpl extends CatalogModelImpl
082: implements CatalogWriteModel {
083:
084: protected XAMCatalogWriteModelImpl(Project prj)
085: throws CatalogModelException, IOException {
086: super (prj);
087: modelSource = createModelSource(super .catalogFileObject);
088: }
089:
090: /**
091: * Constructor for creating a CatalogWriteModel for the given file object.
092: * The catalogFileObject should not be null
093: */
094: protected XAMCatalogWriteModelImpl(FileObject catalogFileObject)
095: throws CatalogModelException, IOException {
096: super (catalogFileObject);
097: modelSource = createModelSource(super .catalogFileObject);
098: }
099:
100: //for unit tests
101: boolean unitTestSaveStrategy = false;
102:
103: public XAMCatalogWriteModelImpl(File myProjectRootFile)
104: throws IOException, CatalogModelException {
105: super (myProjectRootFile);
106: unitTestSaveStrategy = true;
107: modelSource = createModelSource(super .catalogFileObject);
108: }
109:
110: private CatalogModel catalogModel;
111: private ModelSource modelSource;
112:
113: /**
114: * This method will be called by the constructor for instantiating the protected
115: * object "modelSource" (that is returned by getModelSource()).
116: * Since, this impl is based on XAM, it has a dependency on ModelSource. ModelSource's lookup
117: * must contain at the very least FileObject and javax.swing.Document objects for XAM to work.
118: * NOTE: Unit test env needs to overwrite this method and return proper test env model source.
119: * and also the ModelSource must contain proper Swing document and FileObject in the lookup.
120: **/
121: protected ModelSource createModelSource(FileObject catFileObject)
122: throws CatalogModelException {
123: return Utilities.createModelSource(super .catalogFileObject,
124: true);
125:
126: }
127:
128: public URI searchURI(URI locationURI) {
129: if (locationURI == null)
130: return null;
131: URI strRes = null;
132: if (catalogFileObject != null) {
133: //look up in the global catalog
134: File publicCatalogFile = FileUtil.toFile(catalogFileObject);
135: if (publicCatalogFile.isFile()) {
136: try {
137: strRes = resolveUsingApacheCatalog(
138: publicCatalogFile, locationURI.toString());
139: } catch (IOException ex) {
140: return null;
141: } catch (CatalogModelException ex) {
142: return null;
143: }
144: }
145: }
146: return strRes;
147: }
148:
149: public synchronized void addURI(URI locationURI, FileObject fileObj)
150: throws IOException {
151: URI fileObjURI = FileUtil.toFile(fileObj).toURI();
152: addURI(locationURI, fileObjURI);
153: }
154:
155: public synchronized void addURI(URI locationURI, URI fileObjURI)
156: throws IOException {
157: if (this .catalogFileObject == null)
158: return;
159: //remove the old entry if exists
160: removeURI(locationURI);
161:
162: URI master = FileUtil.toFile(this .catalogFileObject).toURI();
163:
164: String finalDestStr = Utilities.relativize(master, fileObjURI);
165: URI finalDestStrURI;
166: try {
167: finalDestStrURI = new URI(finalDestStr);
168: } catch (URISyntaxException ex) {
169: throw new IOException("Invalid URI: " + finalDestStr);
170: }
171:
172: System sys = getCatalogModel().getFactory().createSystem();
173:
174: getCatalogModel().startTransaction();
175: try {
176: getCatalogModel().getRootComponent().addSystem(sys);
177: sys.setSystemIDAttr(locationURI);
178: sys.setURIAttr(finalDestStrURI);
179: } finally {
180: getCatalogModel().endTransaction();
181: }
182: save();
183: }
184:
185: public void removeURI(URI locationURI) throws IOException {
186: System delete = null;
187: getCatalogModel().sync();
188: for (System sys : getCatalogModel().getRootComponent()
189: .getSystems()) {
190: if (sys.getSystemIDAttr().equals(locationURI.toString())) {
191: delete = sys;
192: break;
193: }
194: }
195: if (delete != null) {
196: getCatalogModel().startTransaction();
197: try {
198: getCatalogModel().getRootComponent().removeSystem(
199: delete);
200: } finally {
201: getCatalogModel().endTransaction();
202: }
203: save();
204: }
205: }
206:
207: public Collection<CatalogEntry> getCatalogEntries() {
208: ArrayList<CatalogEntry> result = new ArrayList<CatalogEntry>();
209: if (getCatalogModel() == null
210: || getCatalogModel().getRootComponent() == null)
211: return result;
212: for (System sys : getCatalogModel().getRootComponent()
213: .getSystems()) {
214: CatalogEntry catEnt = new CatalogEntryImpl(
215: CatalogElement.system, sys.getSystemIDAttr(), sys
216: .getURIAttr());
217: result.add(catEnt);
218: }
219: for (NextCatalog nc : getCatalogModel().getRootComponent()
220: .getNextCatalogs()) {
221: CatalogEntry catEnt = new CatalogEntryImpl(
222: CatalogElement.nextCatalog, nc.getCatalogAttr(),
223: null);
224: result.add(catEnt);
225: }
226: return result;
227: }
228:
229: public boolean isWellformed() {
230: return getCatalogModel().getState().equals(Model.State.VALID);
231: }
232:
233: public org.netbeans.modules.xml.xam.dom.DocumentModel.State getState() {
234: return getCatalogModel().getState();
235: }
236:
237: public FileObject getCatalogFileObject() {
238: return super .catalogFileObject;
239: }
240:
241: public void addPropertychangeListener(PropertyChangeListener pcl) {
242: getCatalogModel().addPropertyChangeListener(pcl);
243: }
244:
245: public void removePropertyChangeListener(PropertyChangeListener pcl) {
246: getCatalogModel().removePropertyChangeListener(pcl);
247: }
248:
249: public void addNextCatalog(URI nextCatalogFileURI,
250: boolean relativize) throws IOException {
251: if (this .catalogFileObject == null)
252: return;
253:
254: String nextCatalogFileURIStr = nextCatalogFileURI.toString();
255: if (nextCatalogFileURI.isAbsolute() && relativize) {
256: //then resolve URI relative to this catalog file
257: nextCatalogFileURIStr = Utilities.relativize(FileUtil
258: .toFile(this .catalogFileObject).toURI(),
259: nextCatalogFileURI);
260: }
261: try {
262: nextCatalogFileURI = new URI(nextCatalogFileURIStr);
263: } catch (URISyntaxException ex) {
264: throw new IOException("Invalid URI: "
265: + nextCatalogFileURIStr);
266: }
267:
268: try {
269: removeNextCatalog(nextCatalogFileURI);
270: } catch (IOException ex) {
271: }
272:
273: NextCatalog nc = getCatalogModel().getFactory()
274: .createNextCatalog();
275: getCatalogModel().startTransaction();
276: try {
277: getCatalogModel().getRootComponent().addNextCatalog(nc);
278: nc.setCatalogAttr(nextCatalogFileURI);
279: } finally {
280: getCatalogModel().endTransaction();
281: }
282: save();
283: }
284:
285: public void removeNextCatalog(URI nextCatalogFileRelativeURI)
286: throws IOException {
287: NextCatalog delete = null;
288: for (NextCatalog nc : getCatalogModel().getRootComponent()
289: .getNextCatalogs()) {
290: if (nc.getCatalogAttr().equals(
291: nextCatalogFileRelativeURI.toString())) {
292: delete = nc;
293: break;
294: }
295: }
296: if (delete != null) {
297: getCatalogModel().startTransaction();
298: try {
299: getCatalogModel().getRootComponent().removeNextCatalog(
300: delete);
301: } finally {
302: getCatalogModel().endTransaction();
303: }
304: save();
305: }
306: }
307:
308: protected void save() {
309: FileObject fo = (FileObject) getCatalogModel().getModelSource()
310: .getLookup().lookup(FileObject.class);
311: try {
312: DataObject dobj = DataObject.find(fo);
313: SaveCookie saveCookie = (SaveCookie) dobj
314: .getCookie(SaveCookie.class);
315: if (saveCookie != null)
316: saveCookie.save();
317: } catch (IOException ex) {
318: }
319: }
320:
321: public CatalogModel getCatalogModel() {
322: return CatalogModelFactory.getInstance().getModel(
323: getModelSource());
324: }
325:
326: public void setCatalogModel(CatalogModel catalogModel) {
327: this .catalogModel = catalogModel;
328: }
329:
330: public ModelSource getModelSource() {
331: return modelSource;
332: }
333: }
|