1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 // CatalogEntry.java - Represents Catalog entries 6 7 /* 8 * Copyright 2001-2004 The Apache Software Foundation or its licensors, 9 * as applicable. 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); 12 * you may not use this file except in compliance with the License. 13 * You may obtain a copy of the License at 14 * 15 * http://www.apache.org/licenses/LICENSE-2.0 16 * 17 * Unless required by applicable law or agreed to in writing, software 18 * distributed under the License is distributed on an "AS IS" BASIS, 19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 20 * See the License for the specific language governing permissions and 21 * limitations under the License. 22 */ 23 24 package com.sun.org.apache.xml.internal.resolver; 25 26 import java.util.Hashtable; 27 import java.util.Vector; 28 29 /** 30 * Represents a Catalog entry. 31 * 32 * <p>Instances of this class represent individual entries 33 * in a Catalog.</p> 34 * 35 * <p>Each catalog entry has a unique name and is associated with 36 * an arbitrary number of arguments (all strings). For example, the 37 * TR9401 catalog entry "PUBLIC" has two arguments, a public identifier 38 * and a system identifier. Each entry has a unique numeric type, 39 * assigned automatically when the entry type is created.</p> 40 * 41 * <p>The number and type of catalog entries is maintained 42 * <em>statically</em>. Catalog classes, or their subclasses, can add 43 * new entry types, but all Catalog objects share the same global pool 44 * of types.</p> 45 * 46 * <p>Initially there are no valid entries.</p> 47 * 48 * @see Catalog 49 * 50 * @author Norman Walsh 51 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a> 52 * 53 */ 54 public class CatalogEntry { 55 /** The nextEntry is the ordinal number of the next entry type. */ 56 protected static int nextEntry = 0; 57 58 /** 59 * The entryTypes vector maps catalog entry names 60 * (e.g., 'BASE' or 'SYSTEM') to their type (1, 2, etc.). 61 * Names are case sensitive. 62 */ 63 protected static Hashtable entryTypes = new Hashtable(); 64 65 /** The entryTypes vector maps catalog entry types to the 66 number of arguments they're required to have. */ 67 protected static Vector entryArgs = new Vector(); 68 69 /** 70 * Adds a new catalog entry type. 71 * 72 * @param name The name of the catalog entry type. This must be 73 * unique among all types and is case-sensitive. (Adding a duplicate 74 * name effectively replaces the old type with the new type.) 75 * @param numArgs The number of arguments that this entry type 76 * is required to have. There is no provision for variable numbers 77 * of arguments. 78 * @return The type for the new entry. 79 */ 80 public static int addEntryType(String name, int numArgs) { 81 entryTypes.put(name, new Integer(nextEntry)); 82 entryArgs.add(nextEntry, new Integer(numArgs)); 83 nextEntry++; 84 85 return nextEntry-1; 86 } 87 88 /** 89 * Lookup an entry type 90 * 91 * @param name The name of the catalog entry type. 92 * @return The type of the catalog entry with the specified name. 93 * @throws InvalidCatalogEntryTypeException if no entry has the 94 * specified name. 95 */ 96 public static int getEntryType(String name) 97 throws CatalogException { 98 if (!entryTypes.containsKey(name)) { 99 throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE); 100 } 101 102 Integer iType = (Integer) entryTypes.get(name); 103 104 if (iType == null) { 105 throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE); 106 } 107 108 return iType.intValue(); 109 } 110 111 /** 112 * Find out how many arguments an entry is required to have. 113 * 114 * @param name The name of the catalog entry type. 115 * @return The number of arguments that entry type is required to have. 116 * @throws InvalidCatalogEntryTypeException if no entry has the 117 * specified name. 118 */ 119 public static int getEntryArgCount(String name) 120 throws CatalogException { 121 return getEntryArgCount(getEntryType(name)); 122 } 123 124 /** 125 * Find out how many arguments an entry is required to have. 126 * 127 * @param type A valid catalog entry type. 128 * @return The number of arguments that entry type is required to have. 129 * @throws InvalidCatalogEntryTypeException if the type is invalid. 130 */ 131 public static int getEntryArgCount(int type) 132 throws CatalogException { 133 try { 134 Integer iArgs = (Integer) entryArgs.get(type); 135 return iArgs.intValue(); 136 } catch (ArrayIndexOutOfBoundsException e) { 137 throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE); 138 } 139 } 140 141 /** The entry type of this entry */ 142 protected int entryType = 0; 143 144 /** The arguments associated with this entry */ 145 protected Vector args = null; 146 147 /** 148 * Null constructor; something for subclasses to call. 149 */ 150 public CatalogEntry() {} 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(CatalogException.INVALID_ENTRY_TYPE); 168 } 169 170 int type = iType.intValue(); 171 172 try { 173 Integer iArgs = (Integer) entryArgs.get(type); 174 if (iArgs.intValue() != args.size()) { 175 throw new CatalogException(CatalogException.INVALID_ENTRY); 176 } 177 } catch (ArrayIndexOutOfBoundsException e) { 178 throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE); 179 } 180 181 entryType = type; 182 this.args = args; 183 } 184 185 /** 186 * Construct a catalog entry of the specified type. 187 * 188 * @param type The entry type 189 * @param args A String Vector of arguments 190 * @throws InvalidCatalogEntryTypeException if no such entry type 191 * exists. 192 * @throws InvalidCatalogEntryException if the wrong number of arguments 193 * is passed. 194 */ 195 public CatalogEntry(int type, Vector args) 196 throws CatalogException { 197 try { 198 Integer iArgs = (Integer) entryArgs.get(type); 199 if (iArgs.intValue() != args.size()) { 200 throw new CatalogException(CatalogException.INVALID_ENTRY); 201 } 202 } catch (ArrayIndexOutOfBoundsException e) { 203 throw new CatalogException(CatalogException.INVALID_ENTRY_TYPE); 204 } 205 206 entryType = type; 207 this.args = args; 208 } 209 210 /** 211 * Get the entry type. 212 * 213 * @return The entry type of the CatalogEntry 214 */ 215 public int getEntryType() { 216 return entryType; 217 } 218 219 /** 220 * Get an entry argument. 221 * 222 * @param argNum The argument number (arguments are numbered from 0). 223 * @return The specified argument or null if an invalid argNum is 224 * provided. 225 */ 226 public String getEntryArg(int argNum) { 227 try { 228 String arg = (String) args.get(argNum); 229 return arg; 230 } catch (ArrayIndexOutOfBoundsException e) { 231 return null; 232 } 233 } 234 235 /** 236 * Set an entry argument. 237 * 238 * <p>Catalogs sometimes need to adjust the catlog entry parameters, 239 * for example to make a relative URI absolute with respect to the 240 * current base URI. But in general, this function should only be 241 * called shortly after object creation to do some sort of cleanup. 242 * Catalog entries should not mutate over time.</p> 243 * 244 * @param argNum The argument number (arguments are numbered from 0). 245 * @throws ArrayIndexOutOfBoundsException if an invalid argument 246 * number is provided. 247 */ 248 public void setEntryArg(int argNum, String newspec) 249 throws ArrayIndexOutOfBoundsException { 250 args.set(argNum, newspec); 251 } 252 }