1 /* 2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.tools.internal.xjc.reader.xmlschema.parser; 27 28 import java.util.HashSet; 29 import java.util.Set; 30 import java.util.Stack; 31 32 import javax.xml.namespace.QName; 33 34 import com.sun.tools.internal.xjc.reader.Const; 35 import com.sun.xml.internal.bind.v2.WellKnownNamespace; 36 37 import org.xml.sax.Attributes; 38 import org.xml.sax.ErrorHandler; 39 import org.xml.sax.Locator; 40 import org.xml.sax.SAXException; 41 import org.xml.sax.SAXParseException; 42 import org.xml.sax.helpers.XMLFilterImpl; 43 44 /** 45 * Checks if binding declarations are placed where they are allowed. 46 * 47 * <p> 48 * For example, if a {@code <jaxb:property>} customization is given under 49 * the {@code <xs:simpleContent>} element, this class raises an error. 50 * 51 * <p> 52 * our main checkpoint of misplaced customizations are in BGMBuilder. 53 * There, we mark a customization whenever we use it. At the end of the 54 * day, we look for unmarked customizations and raise errors for them. 55 * 56 * <p> 57 * Between this approach and the JAXB spec 1.0 is a problem that 58 * the spec allows/prohibits customizations at schema element level, 59 * while BGMBuilder and XSOM works on schema component levels. 60 * 61 * <p> 62 * For example, a property customization is allowed on a complex type 63 * schema component, but it's only allowed on the {@code <complexType>} 64 * element. The spec team informed us that they would consider resolving 65 * this discrepancy in favor of RI, but meanwhile we need to detect 66 * errors correctly. 67 * 68 * <p> 69 * This filter is implemented for this purpose. 70 * 71 * 72 * <h2>Customization and allowed locations</h2> 73 * 74 * - globalBinding/schemaBinding 75 * schema 76 * 77 * - class 78 * complexType(*), modelGroupDecl, modelGroup, element 79 * 80 * - property 81 * attribute, element, any, modelGroup, modelGroupRef, complexType(*) 82 * 83 * - javaType 84 * simpleType(*) 85 * 86 * - typesafeEnumClass 87 * simpleType(*) 88 * 89 * - typesafeEnumMember 90 * simpleType(*), enumeration 91 * 92 * Components marked with '*' needs a check by this component 93 * since more than one schema element corresponds to one schema component 94 * of that type. 95 * 96 * <p> 97 * For simple types, customizations are allowed only under the {@code <xs:simpleType>} 98 * element, and for complex types they are allowed only under the 99 * {@code <xs:cimplexType>} element. 100 * 101 * <p> 102 * So the bottom line is that it would be suffice if we just make sure 103 * that no customization will be attached under other elements of 104 * simple types and complex types. Those are: 105 * 106 * - simpleType/restriction 107 * - list 108 * - union 109 * - complexType/(simple or complex)Content 110 * - complexType/(simple or complex)Content/(restriction of extension) 111 * 112 * @author 113 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 114 */ 115 public class CustomizationContextChecker extends XMLFilterImpl { 116 117 /** Keep names of all the ancestor elements. */ 118 private final Stack<QName> elementNames = new Stack<QName>(); 119 120 private final ErrorHandler errorHandler; 121 122 private Locator locator; 123 124 /** Set of element names that cannot have JAXB customizations. */ 125 private static final Set<String> prohibitedSchemaElementNames = new HashSet<String>(); 126 127 /** 128 * @param _errorHandler 129 * Detected errors will be sent to this object. 130 */ 131 public CustomizationContextChecker( ErrorHandler _errorHandler ) { 132 this.errorHandler = _errorHandler; 133 } 134 135 static { 136 prohibitedSchemaElementNames.add("restriction"); 137 prohibitedSchemaElementNames.add("extension"); 138 prohibitedSchemaElementNames.add("simpleContent"); 139 prohibitedSchemaElementNames.add("complexContent"); 140 prohibitedSchemaElementNames.add("list"); 141 prohibitedSchemaElementNames.add("union"); 142 } 143 144 145 146 147 /** Gets the stack top. */ 148 private QName top() { 149 return elementNames.peek(); 150 } 151 152 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 153 QName newElement = new QName(namespaceURI,localName); 154 155 if( newElement.getNamespaceURI().equals(Const.JAXB_NSURI) 156 && top().getNamespaceURI().equals(WellKnownNamespace.XML_SCHEMA) ) { 157 // we hit a JAXB customization. the stack top should be 158 // <xs:appinfo> 159 if( elementNames.size()>=3 ) { 160 // the above statement checks if the following statement doesn't 161 // cause an exception. 162 QName schemaElement = elementNames.get( elementNames.size()-3 ); 163 if( prohibitedSchemaElementNames.contains(schemaElement.getLocalPart()) ) { 164 // the owner schema element is in the wanted list. 165 errorHandler.error( new SAXParseException( 166 Messages.format( 167 Messages.ERR_UNACKNOWLEDGED_CUSTOMIZATION, 168 localName ), 169 locator ) ); 170 } 171 } 172 173 174 } 175 176 elementNames.push(newElement); 177 178 super.startElement(namespaceURI, localName, qName, atts ); 179 } 180 181 public void endElement(String namespaceURI, String localName, String qName) 182 throws SAXException { 183 184 super.endElement(namespaceURI, localName, qName); 185 186 elementNames.pop(); 187 } 188 189 public void setDocumentLocator(Locator locator) { 190 super.setDocumentLocator(locator); 191 this.locator = locator; 192 } 193 194 }