001: // CatalogEntry.java - Represents Catalog entries
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation or its licensors,
005: * as applicable.
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package com.sun.org.apache.xml.internal.resolver;
021:
022: import java.util.Hashtable;
023: import java.util.Vector;
024:
025: /**
026: * Represents a Catalog entry.
027: *
028: * <p>Instances of this class represent individual entries
029: * in a Catalog.</p>
030: *
031: * <p>Each catalog entry has a unique name and is associated with
032: * an arbitrary number of arguments (all strings). For example, the
033: * TR9401 catalog entry "PUBLIC" has two arguments, a public identifier
034: * and a system identifier. Each entry has a unique numeric type,
035: * assigned automatically when the entry type is created.</p>
036: *
037: * <p>The number and type of catalog entries is maintained
038: * <em>statically</em>. Catalog classes, or their subclasses, can add
039: * new entry types, but all Catalog objects share the same global pool
040: * of types.</p>
041: *
042: * <p>Initially there are no valid entries.</p>
043: *
044: * @see Catalog
045: *
046: * @author Norman Walsh
047: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
048: *
049: * @version 1.0
050: */
051: public class CatalogEntry {
052: /** The nextEntry is the ordinal number of the next entry type. */
053: protected static int nextEntry = 0;
054:
055: /**
056: * The entryTypes vector maps catalog entry names
057: * (e.g., 'BASE' or 'SYSTEM') to their type (1, 2, etc.).
058: * Names are case sensitive.
059: */
060: protected static Hashtable entryTypes = new Hashtable();
061:
062: /** The entryTypes vector maps catalog entry types to the
063: number of arguments they're required to have. */
064: protected static Vector entryArgs = new Vector();
065:
066: /**
067: * Adds a new catalog entry type.
068: *
069: * @param name The name of the catalog entry type. This must be
070: * unique among all types and is case-sensitive. (Adding a duplicate
071: * name effectively replaces the old type with the new type.)
072: * @param numArgs The number of arguments that this entry type
073: * is required to have. There is no provision for variable numbers
074: * of arguments.
075: * @return The type for the new entry.
076: */
077: public static int addEntryType(String name, int numArgs) {
078: entryTypes.put(name, new Integer(nextEntry));
079: entryArgs.add(nextEntry, new Integer(numArgs));
080: nextEntry++;
081:
082: return nextEntry - 1;
083: }
084:
085: /**
086: * Lookup an entry type
087: *
088: * @param name The name of the catalog entry type.
089: * @return The type of the catalog entry with the specified name.
090: * @throws InvalidCatalogEntryTypeException if no entry has the
091: * specified name.
092: */
093: public static int getEntryType(String name) throws CatalogException {
094: if (!entryTypes.containsKey(name)) {
095: throw new CatalogException(
096: CatalogException.INVALID_ENTRY_TYPE);
097: }
098:
099: Integer iType = (Integer) entryTypes.get(name);
100:
101: if (iType == null) {
102: throw new CatalogException(
103: CatalogException.INVALID_ENTRY_TYPE);
104: }
105:
106: return iType.intValue();
107: }
108:
109: /**
110: * Find out how many arguments an entry is required to have.
111: *
112: * @param name The name of the catalog entry type.
113: * @return The number of arguments that entry type is required to have.
114: * @throws InvalidCatalogEntryTypeException if no entry has the
115: * specified name.
116: */
117: public static int getEntryArgCount(String name)
118: throws CatalogException {
119: return getEntryArgCount(getEntryType(name));
120: }
121:
122: /**
123: * Find out how many arguments an entry is required to have.
124: *
125: * @param type A valid catalog entry type.
126: * @return The number of arguments that entry type is required to have.
127: * @throws InvalidCatalogEntryTypeException if the type is invalid.
128: */
129: public static int getEntryArgCount(int type)
130: throws CatalogException {
131: try {
132: Integer iArgs = (Integer) entryArgs.get(type);
133: return iArgs.intValue();
134: } catch (ArrayIndexOutOfBoundsException e) {
135: throw new CatalogException(
136: CatalogException.INVALID_ENTRY_TYPE);
137: }
138: }
139:
140: /** The entry type of this entry */
141: protected int entryType = 0;
142:
143: /** The arguments associated with this entry */
144: protected Vector args = null;
145:
146: /**
147: * Null constructor; something for subclasses to call.
148: */
149: public CatalogEntry() {
150: }
151:
152: /**
153: * Construct a catalog entry of the specified type.
154: *
155: * @param name The name of the entry type
156: * @param args A String Vector of arguments
157: * @throws InvalidCatalogEntryTypeException if no such entry type
158: * exists.
159: * @throws InvalidCatalogEntryException if the wrong number of arguments
160: * is passed.
161: */
162: public CatalogEntry(String name, Vector args)
163: throws CatalogException {
164: Integer iType = (Integer) entryTypes.get(name);
165:
166: if (iType == null) {
167: throw new CatalogException(
168: CatalogException.INVALID_ENTRY_TYPE);
169: }
170:
171: int type = iType.intValue();
172:
173: try {
174: Integer iArgs = (Integer) entryArgs.get(type);
175: if (iArgs.intValue() != args.size()) {
176: throw new CatalogException(
177: CatalogException.INVALID_ENTRY);
178: }
179: } catch (ArrayIndexOutOfBoundsException e) {
180: throw new CatalogException(
181: CatalogException.INVALID_ENTRY_TYPE);
182: }
183:
184: entryType = type;
185: this .args = args;
186: }
187:
188: /**
189: * Construct a catalog entry of the specified type.
190: *
191: * @param type The entry type
192: * @param args A String Vector of arguments
193: * @throws InvalidCatalogEntryTypeException if no such entry type
194: * exists.
195: * @throws InvalidCatalogEntryException if the wrong number of arguments
196: * is passed.
197: */
198: public CatalogEntry(int type, Vector args) throws CatalogException {
199: try {
200: Integer iArgs = (Integer) entryArgs.get(type);
201: if (iArgs.intValue() != args.size()) {
202: throw new CatalogException(
203: CatalogException.INVALID_ENTRY);
204: }
205: } catch (ArrayIndexOutOfBoundsException e) {
206: throw new CatalogException(
207: CatalogException.INVALID_ENTRY_TYPE);
208: }
209:
210: entryType = type;
211: this .args = args;
212: }
213:
214: /**
215: * Get the entry type.
216: *
217: * @return The entry type of the CatalogEntry
218: */
219: public int getEntryType() {
220: return entryType;
221: }
222:
223: /**
224: * Get an entry argument.
225: *
226: * @param argNum The argument number (arguments are numbered from 0).
227: * @return The specified argument or null if an invalid argNum is
228: * provided.
229: */
230: public String getEntryArg(int argNum) {
231: try {
232: String arg = (String) args.get(argNum);
233: return arg;
234: } catch (ArrayIndexOutOfBoundsException e) {
235: return null;
236: }
237: }
238:
239: /**
240: * Set an entry argument.
241: *
242: * <p>Catalogs sometimes need to adjust the catlog entry parameters,
243: * for example to make a relative URI absolute with respect to the
244: * current base URI. But in general, this function should only be
245: * called shortly after object creation to do some sort of cleanup.
246: * Catalog entries should not mutate over time.</p>
247: *
248: * @param argNum The argument number (arguments are numbered from 0).
249: * @throws ArrayIndexOutOfBoundsException if an invalid argument
250: * number is provided.
251: */
252: public void setEntryArg(int argNum, String newspec)
253: throws ArrayIndexOutOfBoundsException {
254: args.set(argNum, newspec);
255: }
256: }
|