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 1999-2003 (C) Intalio, Inc. All Rights Reserved. 42 * 43 * $Id$ 44 */ 45 46 package org.exolab.castor.xml.schema; 47 48 /** 49 * An extension of the ContentType to support simple content 50 * extension and restriction for complexTypes. 51 * 52 * 53 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 54 * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $ 55 */ 56 public class SimpleContent extends ContentType implements java.io.Serializable { 57 /** SerialVersionUID */ 58 private static final long serialVersionUID = 6406889524623062413L; 59 60 /** 61 * The associated parent Schema 62 */ 63 private Schema _schema = null; 64 65 /** 66 * The simpleType definition that defines this content type (if any) 67 */ 68 private SimpleType _simpleType; 69 70 /** 71 * The name of the simpleType, used when the simpleType 72 * has not yet been read or added to the parent Schema 73 */ 74 private String _typeName = null; 75 76 /** 77 * Creates a new SimpleContent 78 */ 79 public SimpleContent() { 80 super(ContentType.SIMPLE); 81 } //-- ContentType 82 83 /** 84 * Creates a new SimpleContent using the given SimpleContent. 85 * This constructor will copy from the given SimpleContent. 86 * 87 * 88 * @param content the SimpleContent to copy from 89 */ 90 public SimpleContent(SimpleContent content) { 91 super(ContentType.SIMPLE); 92 if (content != null) { 93 _simpleType = content._simpleType; 94 _typeName = content._typeName; 95 _schema = content._schema; 96 } 97 } //-- ContentType 98 99 /** 100 * Creates a new SimpleContent 101 * 102 * @param simpleType the simpleType of this SimpleContent 103 */ 104 public SimpleContent(SimpleType simpleType) { 105 super(ContentType.SIMPLE); 106 _simpleType = simpleType; 107 } //-- SimpleContent 108 109 /** 110 * Creates a new SimpleContent 111 * 112 * @param schema the associated parent Schema 113 * @param typeName the name of the base simpleType, or 114 * complexType (must also be a SimpleContent). 115 * 116 */ 117 public SimpleContent(Schema schema, String typeName) { 118 super(ContentType.SIMPLE); 119 if (schema == null) { 120 String err = "The argument 'schema' must not be null."; 121 throw new IllegalArgumentException(err); 122 } 123 if (typeName == null) { 124 String err = "The argument 'typeName' must not be null."; 125 throw new IllegalArgumentException(err); 126 } 127 _schema = schema; 128 _typeName = typeName; 129 } //-- SimpleContent 130 131 /** 132 * Creates a copy of this SimpleContent 133 * 134 * @return the new SimpleContent which is a copy of this SimpleContent 135 */ 136 public SimpleContent copy() { 137 return new SimpleContent(this); 138 } //-- copy 139 140 /** 141 * Returns the simpleType that represents that contentType. This may 142 * be null, if no simpleType has been set. 143 * 144 * @return the simpleType that represents that contentType. 145 */ 146 public SimpleType getSimpleType() { 147 if ((_simpleType == null) && (_typeName != null)) { 148 XMLType base = _schema.getType(_typeName); 149 if (base != null) { 150 if (base.isSimpleType()) { 151 _simpleType = (SimpleType)base; 152 } 153 else { 154 ComplexType complexType = (ComplexType)base; 155 if (complexType.isSimpleContent()) { 156 SimpleContent sc = (SimpleContent)complexType.getContentType(); 157 _simpleType = sc.getSimpleType(); 158 } 159 else { 160 //-- Report error 161 String error = "The base ComplexType '" + _typeName + "' "; 162 error += "must be a simpleContent."; 163 throw new IllegalStateException(error); 164 } 165 } 166 } 167 } 168 return _simpleType; 169 } //-- getSimpleType 170 171 /** 172 * Returns the name of the associated type for this SimpleContent 173 * 174 * @return the associated type name for this SimpleContent. 175 */ 176 public String getTypeName() { 177 if (_simpleType != null) { 178 return _simpleType.getName(); 179 } 180 return _typeName; 181 } //-- getTypeName 182 183 184 /** 185 * Sets the simpleType that represents that contentType. 186 * 187 * @param simpleType the simpleType to set 188 */ 189 public void setSimpleType(SimpleType simpleType) { 190 _simpleType = simpleType; 191 } //-- setSimpleType 192 193 194 } //-- SimpleContent