1 /** 2 * Redistribution and use of this software and associated documentation ("Software"), with or 3 * without modification, are permitted provided that the following conditions are met: 4 * 5 * 1. Redistributions of source code must retain copyright statements and notices. Redistributions 6 * must also contain a copy of this document. 7 * 8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 9 * conditions and the following disclaimer in the documentation and/or other materials provided with 10 * the distribution. 11 * 12 * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software 13 * without prior written permission of Intalio, Inc. For written permission, please contact 14 * info@exolab.org. 15 * 16 * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in 17 * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of 18 * Intalio, Inc. 19 * 20 * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/). 21 * 22 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 29 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved. 32 * 33 * $Id$ 34 */ 35 36 package org.exolab.castor.xml.schema.reader; 37 38 // -- imported classes and packages 39 import org.exolab.castor.xml.AttributeSet; 40 import org.exolab.castor.xml.Namespaces; 41 import org.exolab.castor.xml.XMLException; 42 import org.exolab.castor.xml.schema.Annotation; 43 import org.exolab.castor.xml.schema.Facet; 44 import org.exolab.castor.xml.schema.FacetFactory; 45 import org.exolab.castor.xml.schema.SchemaContext; 46 import org.exolab.castor.xml.schema.SchemaException; 47 import org.exolab.castor.xml.schema.SchemaNames; 48 49 /** 50 * A class for Unmarshalling facets 51 * 52 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 53 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $ 54 **/ 55 public class FacetUnmarshaller extends ComponentReader { 56 57 58 // --------------------/ 59 // - Member Variables -/ 60 // --------------------/ 61 62 /** 63 * The current ComponentReader. 64 **/ 65 private ComponentReader unmarshaller = null; 66 67 /** 68 * The current branch depth 69 **/ 70 private int depth = 0; 71 72 /** 73 * The Facet we are constructing 74 **/ 75 private Facet _facet = null; 76 77 /** 78 * The element name of the Facet currently being unmarshalled 79 **/ 80 private String _elementName = null; 81 82 // ----------------/ 83 // - Constructors -/ 84 // ----------------/ 85 86 /** 87 * Creates a new FacetUnmarshaller. 88 * 89 * @param schemaContext the {@link SchemaContext} to get some configuration settings from 90 * @param name the name of the Facet 91 * @param atts the AttributeList 92 **/ 93 public FacetUnmarshaller(final SchemaContext schemaContext, final String name, 94 final AttributeSet atts) throws XMLException { 95 super(schemaContext); 96 97 _elementName = name; 98 99 if (!isFacet(name)) { 100 String err = "'" + name + "' is not a valid or supported facet."; 101 throw new IllegalArgumentException(err); 102 } 103 104 _facet = FacetFactory.getInstance().createFacet(name, atts.getValue(SchemaNames.VALUE_ATTR)); 105 106 } // -- FacetUnmarshaller 107 108 // -----------/ 109 // - Methods -/ 110 // -----------/ 111 112 /** 113 * Returns the name of the element that this ComponentReader handles 114 * 115 * @return the name of the element that this ComponentReader handles 116 **/ 117 public String elementName() { 118 return _elementName; 119 } // -- elementName 120 121 /** 122 * 123 **/ 124 public Facet getFacet() { 125 return _facet; 126 } // -- getArchetype 127 128 /** 129 * Returns the Object created by this ComponentReader 130 * 131 * @return the Object created by this ComponentReader 132 **/ 133 public Object getObject() { 134 return getFacet(); 135 } // -- getObject 136 137 /** 138 * Signals the start of an element with the given name. 139 * 140 * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a 141 * prefix). 142 * @param namespace the namespace of the element. This may be null. Note: A null namespace is not 143 * the same as the default namespace unless the default namespace is also null. 144 * @param atts the AttributeSet containing the attributes associated with the element. 145 * @param nsDecls the namespace declarations being declared for this element. This may be null. 146 **/ 147 public void startElement(String name, String namespace, AttributeSet atts, Namespaces nsDecls) 148 throws XMLException { 149 // -- Do delagation if necessary 150 if (unmarshaller != null) { 151 unmarshaller.startElement(name, namespace, atts, nsDecls); 152 ++depth; 153 return; 154 } 155 156 if (SchemaNames.ANNOTATION.equals(name)) { 157 unmarshaller = new AnnotationUnmarshaller(getSchemaContext(), atts); 158 } else 159 illegalElement(name); 160 161 } // -- startElement 162 163 /** 164 * Signals to end of the element with the given name. 165 * 166 * @param name the NCName of the element. It is an error if the name is a QName (ie. contains a 167 * prefix). 168 * @param namespace the namespace of the element. 169 **/ 170 public void endElement(String name, String namespace) throws XMLException { 171 // -- Do delagation if necessary 172 if ((unmarshaller != null) && (depth > 0)) { 173 unmarshaller.endElement(name, namespace); 174 --depth; 175 return; 176 } 177 178 if (unmarshaller == null) 179 throw new SchemaException("missing start element: " + name); 180 else if (SchemaNames.ANNOTATION.equals(name)) { 181 Annotation annotation = (Annotation) unmarshaller.getObject(); 182 _facet.addAnnotation(annotation); 183 } 184 185 } // -- endElement 186 187 public void characters(char[] ch, int start, int length) throws XMLException { 188 // -- Do delagation if necessary 189 if (unmarshaller != null) { 190 unmarshaller.characters(ch, start, length); 191 } 192 } // -- characters 193 194 protected static boolean isFacet(String name) { 195 196 if (Facet.ENUMERATION.equals(name)) 197 return true; 198 if (Facet.LENGTH.equals(name)) 199 return true; 200 if (Facet.PATTERN.equals(name)) 201 return true; 202 if (Facet.MAX_EXCLUSIVE.equals(name)) 203 return true; 204 if (Facet.MIN_EXCLUSIVE.equals(name)) 205 return true; 206 if (Facet.MAX_INCLUSIVE.equals(name)) 207 return true; 208 if (Facet.MIN_INCLUSIVE.equals(name)) 209 return true; 210 if (Facet.MAX_LENGTH.equals(name)) 211 return true; 212 if (Facet.MIN_LENGTH.equals(name)) 213 return true; 214 if (Facet.WHITESPACE.equals(name)) 215 return true; 216 if (Facet.TOTALDIGITS.equals(name)) 217 return true; 218 if (Facet.FRACTIONDIGITS.equals(name)) 219 return true; 220 return false; 221 } // -- isFacet 222 223 } // -- FacetUnmarshaller