Source code: com/virtuosotechnologies/asaph/maingui/DatabaseIdentifier.java
1 /*
2 ================================================================================
3
4 FILE: DatabaseIdentifier.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 An identifier for a SongDatabase.
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.maingui;
43
44
45 import java.net.URI;
46 import java.net.URISyntaxException;
47
48
49 /**
50 * An identifier for a SongDatabase. This is essentially a parsed URI that
51 * specifies a particular database by the connector used to open it, and a
52 * connector-specific locator for the database.
53 */
54 public final class DatabaseIdentifier
55 {
56 private static final String STR_err_URINotAsaphdb =
57 ResourceAccess.Strings.buildString("err_URINotAsaphdb");
58 private static final String STR_err_URIColonMissing =
59 ResourceAccess.Strings.buildString("err_URIColonMissing");
60 private static final String STR_Identifier_IllegalURI =
61 ResourceAccess.Strings.buildString("Identifier_IllegalURI");
62
63
64 /**
65 * URI scheme for asaph databases
66 */
67 public static final String ASAPHDB_URI_SCHEME = "asaphdb";
68
69
70 private String connectorName_;
71 private String databaseLocator_;
72
73
74 /**
75 * Constructor
76 */
77 public DatabaseIdentifier(
78 String connectorName,
79 String databaseLocator)
80 {
81 connectorName_ = connectorName;
82 databaseLocator_ = databaseLocator;
83 }
84
85
86 /**
87 * Constructor
88 */
89 public DatabaseIdentifier(
90 String uriString)
91 throws
92 URISyntaxException
93 {
94 this(new URI(uriString));
95 }
96
97
98 /**
99 * Constructor
100 */
101 public DatabaseIdentifier(
102 URI uri)
103 throws
104 URISyntaxException
105 {
106 if (!uri.getScheme().equals(ASAPHDB_URI_SCHEME))
107 {
108 throw new URISyntaxException(uri.toString(), STR_err_URINotAsaphdb);
109 }
110 String ssp = uri.getSchemeSpecificPart();
111 int index = ssp.indexOf(':');
112 if (index == -1)
113 {
114 throw new URISyntaxException(uri.toString(), STR_err_URIColonMissing);
115 }
116 connectorName_ = ssp.substring(0, index);
117 databaseLocator_ = ssp.substring(index+1);
118 }
119
120
121 /**
122 * Get the plugin name
123 */
124 public String getConnectorName()
125 {
126 return connectorName_;
127 }
128
129
130 /**
131 * Get the database locator string
132 */
133 public String getDatabaseLocator()
134 {
135 return databaseLocator_;
136 }
137
138
139 /**
140 * Get a URI for this reference.
141 */
142 public URI toURI()
143 throws
144 URISyntaxException
145 {
146 return new URI(ASAPHDB_URI_SCHEME, connectorName_+":"+databaseLocator_, null);
147 }
148
149
150 /**
151 * Returns the string representation of the URI
152 */
153 public String toString()
154 {
155 try
156 {
157 return toURI().toString();
158 }
159 catch (URISyntaxException ex)
160 {
161 return STR_Identifier_IllegalURI;
162 }
163 }
164
165
166 /**
167 * Equals
168 */
169 public boolean equals(
170 Object obj)
171 {
172 if (obj instanceof DatabaseIdentifier)
173 {
174 DatabaseIdentifier di = (DatabaseIdentifier)obj;
175 return connectorName_.equals(di.connectorName_) &&
176 databaseLocator_.equals(di.databaseLocator_);
177 }
178 return false;
179 }
180
181
182 /**
183 * HashCode
184 */
185 public int hashCode()
186 {
187 return connectorName_.hashCode() + databaseLocator_.hashCode();
188 }
189 }