KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > SavantURLBuilder


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant;
8
9
10 import java.io.File JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13
14
15 /**
16  * <p>
17  * This class is the implementation of the URLBuilder interface
18  * for Savant URLs.
19  * </p>
20  *
21  * <p>
22  * Savant's URL scheme is
23  * <b>domain</b>/<b>group</b>/<b>project</b>/<b>name</b>-<b>version</b>.<b>type</b>
24  * </p>
25  *
26  * @author Brian Pontarelli
27  */

28 public class SavantURLBuilder extends AbstractURLBuilder {
29
30     /**
31      * Builds the URL to the artifact using Savant style URLs.
32      *
33      * @param defaultDomain The default domain to use while building the URL
34      * @param mapping (optional) Mapping file that might override the default
35      * domain URL
36      * @param artifact The artifact to build the URL for
37      * @return The URL or null if it couldn't be built (default domain was null
38      * or something)
39      * @throws SavantException If the URL could not be built
40      */

41     public URL JavaDoc buildURL(String JavaDoc defaultDomain, File JavaDoc mapping, Artifact artifact)
42     throws SavantException {
43         String JavaDoc baseURL = makeBaseURLSpec(defaultDomain, mapping, artifact);
44         URL JavaDoc url = null;
45         if (baseURL != null) {
46             String JavaDoc urlSpec = baseURL + "/" + makeURLSpec(artifact);
47             try {
48                 url = new URL JavaDoc(urlSpec);
49             } catch (MalformedURLException JavaDoc mue) {
50                 throw new SavantException(mue);
51             }
52         }
53
54         return url;
55     }
56
57     /**
58      * Builds the URL to the MD5 for the artifact using Savant style URLs.
59      *
60      * @param defaultDomain The default domain to use while building the URL
61      * @param mapping (optional) Mapping file that might override the default
62      * domain URL
63      * @param artifact The artifact to build the URL for
64      * @return The URL or null if it couldn't be built (default domain was null
65      * or something)
66      * @throws SavantException If the URL could not be built
67      */

68     public URL JavaDoc buildMD5URL(String JavaDoc defaultDomain, File JavaDoc mapping, Artifact artifact)
69     throws SavantException {
70         String JavaDoc baseURL = makeBaseURLSpec(defaultDomain, mapping, artifact);
71         URL JavaDoc url = null;
72         if (baseURL != null) {
73             String JavaDoc urlSpec = baseURL + "/" + makeURLSpec(artifact) + ".md5";
74             try {
75                 url = new URL JavaDoc(urlSpec);
76             } catch (MalformedURLException JavaDoc mue) {
77                 throw new SavantException(mue);
78             }
79         }
80
81         return url;
82     }
83
84     /**
85      * Attempts to builds a URL to the XML dependency meta-data for the given
86      * artifact.
87      *
88      * @param artifact The artifact to build the URL for
89      * @param defaultDomain The default domain URL specification (base URL)
90      * @param mapping A Mapping file that can be used to map groups to base URLs
91      * to override the default
92      * @return The XML dependency URL or null if one could not be built
93      */

94     public URL JavaDoc buildDepsURL(String JavaDoc defaultDomain, File JavaDoc mapping,
95             Artifact artifact)
96     throws SavantException {
97         String JavaDoc baseURL = makeBaseURLSpec(defaultDomain, mapping, artifact);
98         URL JavaDoc url = null;
99         if (baseURL != null) {
100             String JavaDoc urlSpec = baseURL + "/" + makeURLSpec(artifact) + ".deps";
101             try {
102                 url = new URL JavaDoc(urlSpec);
103             } catch (MalformedURLException JavaDoc mue) {
104                 throw new SavantException(mue);
105             }
106         }
107
108         return url;
109     }
110
111     /**
112      * Makes the URL additions specifically for Savant URLs.
113      *
114      * @param artifact The artifact to build the URL for
115      * @return The url and never null
116      */

117     private String JavaDoc makeURLSpec(Artifact artifact) {
118         String JavaDoc url = artifact.getGroup() + "/" + artifact.getProjectname() + "/" +
119             artifact.getName();
120         if (artifact.getVersion() != null) {
121             url = url + "-" + artifact.getVersion();
122         }
123
124         url = url + "." + artifact.getType();
125
126         return url;
127     }
128 }
129
Popular Tags