KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > resolver > CatalogEntry


1 // CatalogEntry.java - Represents Catalog entries
2

3 /*
4  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
5  * as applicable.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.sun.org.apache.xml.internal.resolver;
21
22 import java.util.Hashtable JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 /**
26  * Represents a Catalog entry.
27  *
28  * <p>Instances of this class represent individual entries
29  * in a Catalog.</p>
30  *
31  * <p>Each catalog entry has a unique name and is associated with
32  * an arbitrary number of arguments (all strings). For example, the
33  * TR9401 catalog entry "PUBLIC" has two arguments, a public identifier
34  * and a system identifier. Each entry has a unique numeric type,
35  * assigned automatically when the entry type is created.</p>
36  *
37  * <p>The number and type of catalog entries is maintained
38  * <em>statically</em>. Catalog classes, or their subclasses, can add
39  * new entry types, but all Catalog objects share the same global pool
40  * of types.</p>
41  *
42  * <p>Initially there are no valid entries.</p>
43  *
44  * @see Catalog
45  *
46  * @author Norman Walsh
47  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
48  *
49  * @version 1.0
50  */

51 public class CatalogEntry {
52   /** The nextEntry is the ordinal number of the next entry type. */
53   protected static int nextEntry = 0;
54
55   /**
56    * The entryTypes vector maps catalog entry names
57    * (e.g., 'BASE' or 'SYSTEM') to their type (1, 2, etc.).
58    * Names are case sensitive.
59    */

60   protected static Hashtable JavaDoc entryTypes = new Hashtable JavaDoc();
61
62   /** The entryTypes vector maps catalog entry types to the
63       number of arguments they're required to have. */

64   protected static Vector JavaDoc entryArgs = new Vector JavaDoc();
65
66   /**
67    * Adds a new catalog entry type.
68    *
69    * @param name The name of the catalog entry type. This must be
70    * unique among all types and is case-sensitive. (Adding a duplicate
71    * name effectively replaces the old type with the new type.)
72    * @param numArgs The number of arguments that this entry type
73    * is required to have. There is no provision for variable numbers
74    * of arguments.
75    * @return The type for the new entry.
76    */

77   public static int addEntryType(String JavaDoc name, int numArgs) {
78     entryTypes.put(name, new Integer JavaDoc(nextEntry));
79     entryArgs.add(nextEntry, new Integer JavaDoc(numArgs));
80     nextEntry++;
81
82     return nextEntry-1;
83   }
84
85   /**
86    * Lookup an entry type
87    *
88    * @param name The name of the catalog entry type.
89    * @return The type of the catalog entry with the specified name.
90    * @throws InvalidCatalogEntryTypeException if no entry has the
91    * specified name.
92    */

93   public static int getEntryType(String JavaDoc name)
94     throws CatalogException {
95     if (!entryTypes.containsKey(name)) {
96       throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
97     }
98
99     Integer JavaDoc iType = (Integer JavaDoc) entryTypes.get(name);
100
101     if (iType == null) {
102       throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
103     }
104
105     return iType.intValue();
106   }
107
108   /**
109    * Find out how many arguments an entry is required to have.
110    *
111    * @param name The name of the catalog entry type.
112    * @return The number of arguments that entry type is required to have.
113    * @throws InvalidCatalogEntryTypeException if no entry has the
114    * specified name.
115    */

116   public static int getEntryArgCount(String JavaDoc name)
117     throws CatalogException {
118     return getEntryArgCount(getEntryType(name));
119   }
120
121   /**
122    * Find out how many arguments an entry is required to have.
123    *
124    * @param type A valid catalog entry type.
125    * @return The number of arguments that entry type is required to have.
126    * @throws InvalidCatalogEntryTypeException if the type is invalid.
127    */

128   public static int getEntryArgCount(int type)
129     throws CatalogException {
130     try {
131       Integer JavaDoc iArgs = (Integer JavaDoc) entryArgs.get(type);
132       return iArgs.intValue();
133     } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
134       throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
135     }
136   }
137
138   /** The entry type of this entry */
139   protected int entryType = 0;
140
141   /** The arguments associated with this entry */
142   protected Vector JavaDoc args = null;
143
144   /**
145    * Null constructor; something for subclasses to call.
146    */

147   public CatalogEntry() {}
148
149   /**
150    * Construct a catalog entry of the specified type.
151    *
152    * @param name The name of the entry type
153    * @param args A String Vector of arguments
154    * @throws InvalidCatalogEntryTypeException if no such entry type
155    * exists.
156    * @throws InvalidCatalogEntryException if the wrong number of arguments
157    * is passed.
158    */

159   public CatalogEntry(String JavaDoc name, Vector JavaDoc args)
160     throws CatalogException {
161     Integer JavaDoc iType = (Integer JavaDoc) entryTypes.get(name);
162
163     if (iType == null) {
164       throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
165     }
166
167     int type = iType.intValue();
168
169     try {
170       Integer JavaDoc iArgs = (Integer JavaDoc) entryArgs.get(type);
171       if (iArgs.intValue() != args.size()) {
172     throw new CatalogException(CatalogException.INVALID_ENTRY);
173       }
174     } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
175       throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
176     }
177
178     entryType = type;
179     this.args = args;
180   }
181
182   /**
183    * Construct a catalog entry of the specified type.
184    *
185    * @param type The entry type
186    * @param args A String Vector of arguments
187    * @throws InvalidCatalogEntryTypeException if no such entry type
188    * exists.
189    * @throws InvalidCatalogEntryException if the wrong number of arguments
190    * is passed.
191    */

192   public CatalogEntry(int type, Vector JavaDoc args)
193     throws CatalogException {
194     try {
195       Integer JavaDoc iArgs = (Integer JavaDoc) entryArgs.get(type);
196       if (iArgs.intValue() != args.size()) {
197     throw new CatalogException(CatalogException.INVALID_ENTRY);
198       }
199     } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
200       throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE);
201     }
202
203     entryType = type;
204     this.args = args;
205   }
206
207   /**
208    * Get the entry type.
209    *
210    * @return The entry type of the CatalogEntry
211    */

212   public int getEntryType() {
213     return entryType;
214   }
215
216   /**
217    * Get an entry argument.
218    *
219    * @param argNum The argument number (arguments are numbered from 0).
220    * @return The specified argument or null if an invalid argNum is
221    * provided.
222    */

223   public String JavaDoc getEntryArg(int argNum) {
224     try {
225       String JavaDoc arg = (String JavaDoc) args.get(argNum);
226       return arg;
227     } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
228       return null;
229     }
230   }
231
232   /**
233    * Set an entry argument.
234    *
235    * <p>Catalogs sometimes need to adjust the catlog entry parameters,
236    * for example to make a relative URI absolute with respect to the
237    * current base URI. But in general, this function should only be
238    * called shortly after object creation to do some sort of cleanup.
239    * Catalog entries should not mutate over time.</p>
240    *
241    * @param argNum The argument number (arguments are numbered from 0).
242    * @throws ArrayIndexOutOfBoundsException if an invalid argument
243    * number is provided.
244    */

245   public void setEntryArg(int argNum, String JavaDoc newspec)
246     throws ArrayIndexOutOfBoundsException JavaDoc {
247     args.set(argNum, newspec);
248   }
249 }
250
Popular Tags