src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/parser/CustomizationContextChecker.java

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

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

mercurial