KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > layers > NbinstURLMapper


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.core.startup.layers;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.net.URL JavaDoc;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.URLMapper;
29 import org.openide.modules.InstalledFileLocator;
30 import org.openide.util.Exceptions;
31
32 /**
33  * URLMapper for the nbinst URL protocol.
34  * The mapper handles only the translation from URL into FileObjects.
35  * The opposite conversion is not needed, it is handled by the default URLMapper.
36  * The format of the nbinst URL is nbinst://host/path.
37  * The host part is optional, if presents it specifies the name of the supplying module.
38  * The path is mandatory and specifies the relative path from the ${netbeans.home}, ${netbeans.user}
39  * or ${netbeans.dirs}.
40  * @author Tomas Zezula
41  */

42 public class NbinstURLMapper extends URLMapper {
43     
44     public static final String JavaDoc PROTOCOL = "nbinst"; //NOI18N
45

46     /** Creates a new instance of NbInstURLMapper */
47     public NbinstURLMapper() {
48     }
49
50     /**
51      * Returns FileObjects for given URL
52      * @param url the URL for which the FileObjects should be find.
53      * @return FileObject[], returns null in case of unknown protocol.
54      */

55     public FileObject[] getFileObjects(URL JavaDoc url) {
56         return (PROTOCOL.equals(url.getProtocol())) ? decodeURL (url) : null;
57     }
58
59     /**
60      * Returns null, the translation into URL is doen by default URLMapper
61      */

62     public URL JavaDoc getURL(FileObject fo, int type) {
63         return null;
64     }
65
66     /**
67      * Resolves the nbinst URL into the array of the FileObjects.
68      * @param url to be resolved
69      * @return FileObject[], returns null if unknown url protocol.
70      */

71     static FileObject[] decodeURL (URL JavaDoc url) {
72         assert url != null;
73         try {
74             URI JavaDoc uri = new URI JavaDoc (url.toExternalForm());
75             String JavaDoc protocol = uri.getScheme();
76             if (PROTOCOL.equals(protocol)) {
77                 String JavaDoc module = uri.getHost();
78                 String JavaDoc path = uri.getPath();
79                 if (path.length()>0) {
80                     try {
81                         File JavaDoc file = InstalledFileLocator.getDefault().locate(path.substring(1),module,false);
82                         if (file != null) {
83                             return new FileObject[] {URLMapper.findFileObject(file.toURI().toURL())};
84                         }
85                     }
86                     catch (MalformedURLException JavaDoc mue) {
87                         Exceptions.printStackTrace(mue);
88                     }
89                 }
90             }
91         } catch (URISyntaxException JavaDoc use) {
92             Exceptions.printStackTrace(use);
93         }
94         return null;
95     }
96
97 }
98
Popular Tags