1 /** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Intalio, Inc. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Intalio, Inc. Exolab is a registered 23 * trademark of Intalio, Inc. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2000 (C) Intalio Inc. All Rights Reserved. 42 * 43 * $Id$ 44 */ 45 46 package org.exolab.castor.xml.dtd; 47 48 /** 49 * Implementation of DTD General Entity declaration specification. 50 * @author <a href="mailto:totok@intalio.com">Alexander Totok</a> 51 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $ 52 */ 53 public class GeneralEntity { 54 55 private static final short INTERNAL = 0; 56 private static final short EXTERNAL_PUBLIC = 1; 57 private static final short EXTERNAL_SYSTEM = 2; 58 59 /** 60 * Name of the general entity. 61 */ 62 private String name = null; 63 64 /** 65 * DTD document owning this General Entity declaration. 66 */ 67 private DTDdocument document = null; 68 69 /** 70 * Value of the general entity - <b>replacement text</b>. 71 */ 72 private String value = null; 73 74 /** 75 * Type of the general entity. Value may be {@link #INTERNAL INTERNAL} - 76 * the entity is internal and the {@link #value value} is specified, 77 * {@link #EXTERNAL_PUBLIC EXTERNAL_PUBLIC} - the 78 * {@link #pubIdentifier pubIdentifier} and {@link #sysIdentifier sysIdentifier} 79 * are specified, {@link #EXTERNAL_SYSTEM EXTERNAL_SYSTEM} - the 80 * {@link #sysIdentifier sysIdentifier} only is specified, or -1 81 * if unspecified. 82 */ 83 private short type = - 1; 84 85 /** 86 * <b>Public identifier</b> of the general entity. 87 */ 88 private String pubIdentifier = null; 89 90 /** 91 * <b>System identifier</b> of the general entity. 92 */ 93 private String sysIdentifier = null; 94 95 /** 96 * Name of associated <b>NOTATION</b>. 97 */ 98 private String ndata = null; 99 100 /** 101 * Default constructor. 102 */ 103 public GeneralEntity() { 104 } 105 106 /** 107 * Constructor, setting name and owning DTD document of the general entity. 108 * @param document must not be <tt>null</tt>. 109 * @param name must not be <tt>null</tt> or equal to empty String. 110 */ 111 public GeneralEntity(DTDdocument document, String name) { 112 113 if (document == null) { 114 String err = "GeneralEntity constructor: document must not be null."; 115 throw new IllegalArgumentException(err); 116 } 117 118 if (name == null || name.equals("")) { 119 String err = "GeneralEntity constructor: name must not be empty."; 120 throw new IllegalArgumentException(err); 121 } 122 123 this.name = name; 124 this.document = document; 125 } //-- GeneralEntity 126 127 /** 128 * Returns the name of the general entity. 129 */ 130 public String getName() { 131 return name; 132 } //-- getName 133 134 135 /** 136 * Return DTD document owning this General Entity declaration. 137 */ 138 public DTDdocument getDocument() { 139 return document; 140 } //-- getDocument 141 142 /** 143 * Sets the value (replacement text) of the general entity, making it 144 * internal parsed entity. 145 * @param value must not be <tt>null</tt>. 146 */ 147 public void setValue(String value) { 148 if (value == null) { 149 String err = "GeneralEntity: can not set null value."; 150 throw new IllegalArgumentException(err); 151 } 152 type = INTERNAL; 153 this.value = value; 154 } //-- setValue 155 156 /** 157 * <b>True</b> if internal entity, <b>false</b> otherwise. 158 */ 159 public boolean isInternal() { 160 return type == INTERNAL; 161 } //-- isInternal 162 163 /** 164 * Returns the value of the entity (replacement text) if internal entity, 165 * <tt>null</tt> otherwise. 166 */ 167 public String getValue() { 168 if (isInternal()) return value; 169 return null; 170 } //-- getValue 171 172 /** 173 * Sets the general entity to <tt>EXTERNAL_PUBLIC</tt>. 174 * @param pubId public identifier - must not be <tt>null</tt>. 175 * @param sysId system identifier - must not be <tt>null</tt>. 176 */ 177 public void setExternalPublic(String pubId, String sysId) { 178 if (pubId == null) { 179 String err = "GeneralEntity: can not set null public ID."; 180 throw new IllegalArgumentException(err); 181 } 182 183 if (sysId == null) { 184 String err = "GeneralEntity: can not set null system ID."; 185 throw new IllegalArgumentException(err); 186 } 187 188 type = EXTERNAL_PUBLIC; 189 pubIdentifier = pubId; 190 sysIdentifier = sysId; 191 } //-- setExternalPublic 192 193 /** 194 * <b>True</b> if <tt>EXTERNAL_PUBLIC</tt> entity, <b>false</b> otherwise. 195 */ 196 public boolean isExternalPublic() { 197 return type == EXTERNAL_PUBLIC; 198 } //-- isExternalPublic 199 200 /** 201 * Sets the general entity to <tt>EXTERNAL_SYSTEM</tt>. 202 * @param sysId system identifier - must not be <tt>null</tt>. 203 */ 204 public void setExternalSystem(String sysId) { 205 if (sysId == null) { 206 String err = "GeneralEntity: can not set null system ID."; 207 throw new IllegalArgumentException(err); 208 } 209 210 type = EXTERNAL_SYSTEM; 211 sysIdentifier = sysId; 212 } //-- setExternalSystem 213 214 /** 215 * <b>True</b> if <tt>EXTERNAL_SYSTEM</tt> entity, <b>false</b> otherwise. 216 */ 217 public boolean isExternalSystem() { 218 return type == EXTERNAL_SYSTEM; 219 } //-- isExternalSystem 220 221 /** 222 * Returns system identifier, if <tt>EXTERNAL_PUBLIC</tt> or 223 * <tt>EXTERNAL_SYSTEM</tt> entity, <tt>null</tt> otherwise. 224 */ 225 public String getSysIdentifier() { 226 if (isExternalSystem() || isExternalPublic()) return sysIdentifier; 227 return null; 228 } //-- getSysIdentifier 229 230 /** 231 * Returns public identifier, if <tt>EXTERNAL_PUBLIC</tt> entity, 232 * <tt>null</tt> otherwise. 233 */ 234 public String getPubIdentifier() { 235 if (isExternalPublic()) return pubIdentifier; 236 return null; 237 } //-- getPubIdentifier 238 239 /** 240 * Sets the associated notation. 241 * @param notationName - must not be <tt>null</tt> or equal to empty String. 242 */ 243 public void setNDATA(String notationName) { 244 if (notationName == null || notationName.equals("")) { 245 String err = "General Entity: can not set empty associated notation name."; 246 throw new IllegalArgumentException(err); 247 } 248 ndata = notationName; 249 } //-- setNDATA 250 251 /** 252 * <b>True</b> if external unparsed entity, that is if external and 253 * associated notation name is specified, <b>false</b> otherwise. 254 */ 255 public boolean isExternalUnparsed() { 256 if ((isExternalPublic() || isExternalSystem()) && ndata != null) return true; 257 return false; 258 } //-- isUnparsed 259 260 /** 261 * Returns name of associated notation, if external entity, 262 * <tt>null</tt> otherwise. 263 */ 264 public String getNotation() { 265 if (isExternalPublic() || isExternalSystem()) return ndata; 266 return null; 267 } //-- getNotation 268 269 } //-- GeneralEntity