Project

General

Profile

1
// CatalogEntry.java - Represent OASIS Catalog entries
2
// Written by Norman Walsh, nwalsh@arbortext.com
3
// NO WARRANTY! This class is in the public domain.
4

    
5
package com.arbortext.catalog;
6

    
7
import com.arbortext.catalog.InvalidCatalogEntryTypeException;
8
import com.arbortext.catalog.InvalidCatalogEntryException;
9

    
10
/**
11
 * <p>Represents an OASIS Open Catalog entry.</p>
12
 *
13
 * <blockquote>
14
 * <em>This module, both source code and documentation, is in the
15
 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
16
 * </blockquote>
17
 *
18
 * <p>Instances of this class represent individual entries
19
 * from an <a href="http://www.oasis-open.org/html/a401.htm">OASIS
20
 * Open Catalog</a> file.</p>
21
 * <p>While this could have been implemented as a base class with a
22
 * separate subclass for each type of catalog entry, it didn't seem
23
 * to be worth the extra overhead.</p>
24
 *
25
 * @see Catalog
26
 *
27
 * @author Arbortext, Inc.
28
 * @author Norman Walsh
29
 *         <a href="mailto:nwalsh@arbortext.com">nwalsh@arbortext.com</a>
30
 * @version 1.0
31
 */
32
public class CatalogEntry {
33
    // These have one argument in the catalog file
34

    
35
    /** The entry type for a BASE entry */
36
    public static final int BASE = 1;       // BASE fsispec
37
    /** The entry type for a CATALOG entry */
38
    public static final int CATALOG = 2;    // CATALOG fsispec
39
    /** The entry type for a DOCUMENT entry */
40
    public static final int DOCUMENT = 3;   // DOCUMENT fsispec
41
    /** The entry type for a OVERRIDE entry */
42
    public static final int OVERRIDE = 4;   // OVERRIDE (yes|no)
43
    /** The entry type for a SGMLDECL entry */
44
    public static final int SGMLDECL = 5;   // SGMLDECL fsispec
45

    
46
    // These have two arguments in the catalog file
47
    /** The entry type for a DELEGATE entry */
48
    public static final int DELEGATE = 6;   // DELEGATE partialpublic fsispec
49
    /** The entry type for a DOCTYPE entry */
50
    public static final int DOCTYPE = 7;    // DOCTYPE entityname fsispec
51
    /** The entry type for a DTDDECL entry */
52
    public static final int DTDDECL = 8;    // DTDDECL publicid fsispec
53
    /** The entry type for a ENTITY entry */
54
    public static final int ENTITY = 9;     // ENTITY entityname fsispec
55
    /** The entry type for a LINKTYPE entry */
56
    public static final int LINKTYPE = 10;  // LINKTYPE entityname fsispec
57
    /** The entry type for a NOTATION entry */
58
    public static final int NOTATION = 11;  // NOTATION entityname fsispec
59
    /** The entry type for a PUBLIC entry */
60
    public static final int PUBLIC = 12;    // PUBLIC publicid fsispec
61
    /** The entry type for a SYSTEM entry */
62
    public static final int SYSTEM = 13;    // SYSTEM systemid fsispec
63

    
64
    /** The entry type (one of BASE..SYSTEM) */
65
    private int entryType = 0;
66

    
67
    /** The first argument in a catalog entry */
68
    private String spec1 = "";
69

    
70
    /** The second argument in a catalog entry (usually an fsispec) */
71
    private String spec2 = "";
72

    
73
    /**
74
     * <p>Construct a catalog entry of the specified type. The two-argument
75
     * form of the constructor can be used for BASE, CATALOG, DOCUMENT,
76
     * OVERRIDE, and SGMLDECL entries.</p>
77
     *
78
     * @param type The entry type.
79
     * @param spec The argument to the entry, a formal system
80
     * identifier in all cases except OVERRIDE when it must be either
81
     * "yes" or "no".
82
     */
83
    public CatalogEntry(int type, String spec)
84
	throws InvalidCatalogEntryTypeException,
85
	       InvalidCatalogEntryException {
86

    
87
	if (type < BASE || type > SYSTEM) {
88
	    throw new InvalidCatalogEntryTypeException();
89
	}
90

    
91
	if (type > SGMLDECL) {
92
	    throw new InvalidCatalogEntryException();
93
	}
94

    
95
	if (type == OVERRIDE
96
	    && !(spec.equalsIgnoreCase("YES")
97
		 || spec.equalsIgnoreCase("NO"))) {
98
	    throw new InvalidCatalogEntryException();
99
	}
100

    
101
	entryType = type;
102
	spec1 = spec;
103
    }
104

    
105
    /**
106
     * <p>Construct a catalog entry of the specified type. The three-argument
107
     * form of the constructor can be used for DELEGATE, DOCTYPE, DTDDECL,
108
     * ENTITY, LINKTYPE, NOTATION, PUBLIC, and SYSTEM entries.</p>
109
     *
110
     * @param type The entry type.
111
     * @param spec1 The first argument to the entry, usually an
112
     * entity name or (partial) public identifier.
113
     * @param spec2 The second argument to the entry, often a
114
     * formal system identifier.
115
     */
116
    public CatalogEntry(int type, String spec, String fsispec)
117
	throws InvalidCatalogEntryTypeException,
118
	       InvalidCatalogEntryException {
119

    
120
	if (type < BASE || type > SYSTEM) {
121
	    throw new InvalidCatalogEntryTypeException();
122
	}
123

    
124
	if (type < DELEGATE) {
125
	    throw new InvalidCatalogEntryException();
126
	}
127

    
128
	entryType = type;
129
	spec1 = spec;
130
	spec2 = fsispec;
131
    }
132

    
133
    /**
134
     * <p>The entry type</p>
135
     *
136
     * @return The entry type
137
     */
138
    public int entryType() {
139
	return entryType;
140
    }
141

    
142
    /**
143
     * <p>The formal system identifier of the entry, if appropriate</p>
144
     *
145
     * @return The FSI for the entry, or null if it has no FSI.
146
     */
147
    public String formalSystemIdentifier() {
148
	if (entryType > SGMLDECL) {
149
	    return spec2;
150
	} else {
151
	    if (entryType != OVERRIDE) {
152
		return spec1;
153
	    } else {
154
		return null;
155
	    }
156
	}
157
    }
158

    
159
    /**
160
     * <p>The argument, YES or NO, of an OVERRIDE entry.</p>
161
     *
162
     * @return The YES or NO setting of an OVERRIDE entry,
163
     * null otherwise.
164
     */
165
    public String yes_or_no() {
166
	if (entryType != OVERRIDE) {
167
	    return null;
168
	} else {
169
	    return spec1;
170
	}
171
    }
172

    
173
    /**
174
     * <p>The partial public identifier of a DELEGATE entry.</p>
175
     *
176
     * @return The partial public identifier of a DELEGATE entry,
177
     * null otherwise.
178
     */
179
    public String partialPublicId() {
180
	if (entryType != DELEGATE) {
181
	    return null;
182
	} else {
183
	    return spec1;
184
	}
185
    }
186

    
187
    /**
188
     * <p>The entity name</p>
189
     *
190
     * @return The entity name of a DOCTYPE, ENTITY, LINKTYPE, or
191
     * NOTATION entry,
192
     * null otherwise.
193
     */
194
    public String entityName() {
195
	if (entryType == DOCTYPE
196
	    || entryType == ENTITY
197
	    || entryType == LINKTYPE
198
	    || entryType == NOTATION) {
199
	    return spec1;
200
	} else {
201
	    return null;
202
	}
203
    }
204

    
205
    /**
206
     * <p>The public identifier</p>
207
     *
208
     * @return The public identifier of a DTDDECL or PUBLIC entry,
209
     * null otherwise.
210
     */
211
    public String publicId() {
212
	if (entryType == DTDDECL
213
	    || entryType == PUBLIC) {
214
	    return spec1;
215
	} else {
216
	    return null;
217
	}
218
    }
219

    
220
    /**
221
     * <p>The system identifier</p>
222
     *
223
     * @return The system identifier of a SYSTEM entry,
224
     * null otherwise.
225
     */
226
    public String systemId() {
227
	if (entryType != SYSTEM) {
228
	    return null;
229
	} else {
230
	    return spec1;
231
	}
232
    }
233

    
234
    /**
235
     * <p>Update the formal system identifier</p>
236
     *
237
     * <p>The FSI initial specified in an entry may be relative (to
238
     * the location of the catalog file or as modified by a BASE entry).
239
     * A system processing catalog files
240
     * (e.g. {@link com.arbortext.catalog.Catalog}),
241
     * must be able to update the FSI in order to change it from a relative
242
     * location to an absolute one.</p>
243
     *
244
     * @param newspec The new FSI
245
     */
246
    public void updateFormalSystemIdentifier(String newspec) {
247
	if (entryType > SGMLDECL) {
248
	    spec2 = newspec;
249
	} else {
250
	    spec1 = newspec;
251
	}
252
    }
253
}
(3-3/11)