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: package org.netbeans.modules.xml.retriever.impl;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.net.URI;
046: import java.net.URISyntaxException;
047: import java.net.UnknownHostException;
048: import org.netbeans.api.project.FileOwnerQuery;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.modules.xml.retriever.Retriever;
051: import org.netbeans.modules.xml.retriever.XMLCatalogProvider;
052: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModel;
053: import org.netbeans.modules.xml.retriever.catalog.CatalogWriteModelFactory;
054: import org.netbeans.modules.xml.retriever.catalog.Utilities;
055: import org.netbeans.modules.xml.xam.locator.CatalogModelException;
056: import org.netbeans.spi.project.CacheDirectoryProvider;
057: import org.openide.filesystems.FileObject;
058: import org.openide.filesystems.FileUtil;
059:
060: /**
061: *
062: * @author Samaresh
063: */
064: public class Util {
065:
066: private Util() {
067: }
068:
069: public static boolean retrieveAndCache(URI locationURI,
070: FileObject sourceFileObject, boolean newThread) {
071: URI privateCatalogURI = null;
072: URI privateCacheURI = null;
073:
074: Project prj = FileOwnerQuery.getOwner(sourceFileObject);
075: if (prj == null)
076: return false;
077:
078: FileObject prjrtfo = prj.getProjectDirectory();
079: File prjrt = FileUtil.toFile(prjrtfo);
080: if (prjrt == null)
081: return false;
082:
083: //determine the cache dir
084: CacheDirectoryProvider cdp = (CacheDirectoryProvider) prj
085: .getLookup().lookup(CacheDirectoryProvider.class);
086: String catalogstr = Utilities.DEFAULT_PRIVATE_CATALOG_URI_STR;
087: String cachestr = Utilities.DEFAULT_PRIVATE_CAHCE_URI_STR;
088: try {
089: if ((cdp != null) && (cdp.getCacheDirectory() != null)) {
090: URI prjrturi = prjrt.toURI();
091: URI cpduri = FileUtil.toFile(cdp.getCacheDirectory())
092: .toURI();
093: String cachedirstr = Utilities.relativize(prjrturi,
094: cpduri);
095: catalogstr = cachedirstr + "/"
096: + Utilities.PRIVATE_CATALOG_URI_STR;
097: cachestr = cachedirstr + "/"
098: + Utilities.PRIVATE_CAHCE_URI_STR;
099: }
100: privateCatalogURI = new URI(catalogstr);
101: privateCacheURI = new URI(cachestr);
102: } catch (Exception e) {
103: return false;
104: }
105:
106: //retrieve
107: URI cacheURI = prjrt.toURI().resolve(privateCacheURI);
108: File cacheFile = new File(cacheURI);
109: if (!cacheFile.isDirectory())
110: cacheFile.mkdirs();
111: FileObject cacheFO = FileUtil.toFileObject(FileUtil
112: .normalizeFile(cacheFile));
113: if (cacheFO == null)
114: return false;
115: Retriever ret = Retriever.getDefault();
116: FileObject result;
117: try {
118: ((RetrieverImpl) ret).setNewThread(newThread);
119: result = ret.retrieveResource(cacheFO, privateCatalogURI,
120: locationURI);
121: } catch (UnknownHostException ex) {
122: result = null;
123: } catch (IOException ex) {
124: result = null;
125: } catch (URISyntaxException ex) {
126: result = null;
127: }
128:
129: /*if(result == null)
130: return false;*/
131:
132: //add private catalog as next catalog file to the public and peer catalog
133: XMLCatalogProvider catProv = (XMLCatalogProvider) prj
134: .getLookup().lookup(XMLCatalogProvider.class);
135: FileObject publicCatFO = null;
136: FileObject peerCatFO = null;
137: if (catProv != null) {
138:
139: //get public catalog
140: URI publicCatURI = catProv.getProjectWideCatalog();
141: if (publicCatURI != null) {
142:
143: URI pubcatURI = prjrt.toURI().resolve(publicCatURI);
144: if (pubcatURI != null) {
145:
146: File pubcatFile = new File(pubcatURI);
147: if (!pubcatFile.isFile())
148: try {
149: pubcatFile.createNewFile();
150: } catch (IOException ex) {
151: }
152: publicCatFO = FileUtil.toFileObject(FileUtil
153: .normalizeFile(pubcatFile));
154: }
155: }
156:
157: //get peer catalog
158: URI peerCatURI = catProv.getCatalog(sourceFileObject);
159: if (peerCatURI != null) {
160: URI peercatURI = prjrt.toURI().resolve(peerCatURI);
161: if (peercatURI != null) {
162: File peercatFile = new File(peercatURI);
163: if (!peercatFile.isFile())
164: try {
165: peercatFile.createNewFile();
166: } catch (IOException ex) {
167: }
168: peerCatFO = FileUtil.toFileObject(FileUtil
169: .normalizeFile(peercatFile));
170: }
171: }
172: }
173: //get the catalog write model
174: //add next cat entry to public catalog
175: URI cacheCatFullURI = FileUtil.toFile(prjrtfo).toURI().resolve(
176: privateCatalogURI);
177: CatalogWriteModel catWriter = null;
178: try {
179: if (publicCatFO == null) {
180: //get the public catalog legacy way
181: catWriter = CatalogWriteModelFactory.getInstance()
182: .getCatalogWriteModelForProject(
183: sourceFileObject);
184: } else {
185: catWriter = CatalogWriteModelFactory
186: .getInstance()
187: .getCatalogWriteModelForCatalogFile(publicCatFO);
188: }
189: } catch (CatalogModelException ex) {
190: }
191: if (catWriter == null) {
192: //return true. May be public cat had the priv cat entry already
193: return true;
194: }
195: try {
196: catWriter.addNextCatalog(cacheCatFullURI, true);
197: } catch (IOException ex) {
198: }
199:
200: //add the next cat entry to peer catalog
201: if (publicCatFO != peerCatFO) {
202: //get the catalog write model
203: catWriter = null;
204: try {
205: if (peerCatFO == null) {
206: //get the public catalog legacy way
207: catWriter = CatalogWriteModelFactory.getInstance()
208: .getCatalogWriteModelForProject(
209: sourceFileObject);
210: } else {
211: catWriter = CatalogWriteModelFactory.getInstance()
212: .getCatalogWriteModelForCatalogFile(
213: peerCatFO);
214: }
215: } catch (CatalogModelException ex) {
216: }
217: if (catWriter == null) {
218: //return true. May be public cat had the priv cat entry already
219: return true;
220: }
221: try {
222: catWriter.addNextCatalog(cacheCatFullURI, true);
223: } catch (IOException ex) {
224: }
225: }
226: return true;
227: }
228:
229: public static FileObject getProjectCatalogFileObject(Project prj,
230: boolean create) throws IOException {
231: if (prj == null)
232: return null;
233:
234: FileObject result = null;
235: FileObject myProjectRootFileObject = prj.getProjectDirectory();
236:
237: //see if this prj has XMLCatalogProvider. If yes use it.
238: XMLCatalogProvider catProv = prj
239: .getLookup()
240: .lookup(
241: org.netbeans.modules.xml.retriever.XMLCatalogProvider.class);
242: if (catProv != null) {
243: URI caturi = catProv.getProjectWideCatalog();
244: if (caturi != null) {
245: caturi = FileUtil.toFile(myProjectRootFileObject)
246: .toURI().resolve(caturi);
247: File catFile = new File(caturi);
248: if (!catFile.isFile()) {
249: catFile.createNewFile();
250: }
251: result = FileUtil.toFileObject(FileUtil
252: .normalizeFile(catFile));
253: }
254: }
255:
256: if (result == null) {
257: String fileName = CatalogWriteModel.PUBLIC_CATALOG_FILE_NAME
258: + CatalogWriteModel.CATALOG_FILE_EXTENSION;
259: result = myProjectRootFileObject.getFileObject(fileName);
260: if (result == null && create) {
261: result = myProjectRootFileObject.createData(fileName);
262: }
263: }
264: return result;
265: }
266:
267: }
|