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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 0
373ffda63c9a
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

     1 /*
     2  * Copyright (c) 1997, 2011, 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  */
    26 package com.sun.tools.internal.xjc.reader.xmlschema.parser;
    28 import com.sun.tools.internal.xjc.reader.Const;
    29 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    30 import org.xml.sax.Attributes;
    31 import org.xml.sax.ErrorHandler;
    32 import org.xml.sax.Locator;
    33 import org.xml.sax.SAXException;
    34 import org.xml.sax.SAXParseException;
    35 import org.xml.sax.helpers.XMLFilterImpl;
    37 /**
    38  * This filter detects the use of incorrect JAXB namespace URI.
    39  *
    40  * When the binding compiler looks at a schema file, it always look
    41  * for the namespace URI of the elements (which is correct, BTW.)
    42  *
    43  * <p>
    44  * However, one unfortunate downside of this philosophically correct
    45  * behavior is that there is no provision or safety check when an user
    46  * misspelled JAXB binding customization namespace.
    47  *
    48  * <p>
    49  * This checker inspects the input document and look for the use of the
    50  * prefix "jaxb". If the document doesn't associate any prefix to the
    51  * JAXB customization URI and if it does associate the jaxb prefix,
    52  * this checker will issue a warning.
    53  *
    54  * <p>
    55  * This warning can happen to completely correct schema (because
    56  * nothing prevents you from using the prefix "jaxb" for other purpose
    57  * while using a JAXB compiler on the same schema) but in practice
    58  * this would be quite unlikely.
    59  *
    60  * <p>
    61  * This justifies the use of this filter.
    62  *
    63  * @author
    64  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
    65  */
    66 public class IncorrectNamespaceURIChecker extends XMLFilterImpl {
    68     public IncorrectNamespaceURIChecker( ErrorHandler handler ) {
    69         this.errorHandler = handler;
    70     }
    72     private ErrorHandler errorHandler;
    74     private Locator locator = null;
    76     /** Sets to true once we see the jaxb prefix in use. */
    77     private boolean isJAXBPrefixUsed = false;
    78     /** Sets to true once we see the JAXB customization namespace URI. */
    79     private boolean isCustomizationUsed = false;
    81     @Override
    82     public void endDocument() throws SAXException {
    83         if( isJAXBPrefixUsed && !isCustomizationUsed ) {
    84             SAXParseException e = new SAXParseException(
    85                 Messages.format(Messages.WARN_INCORRECT_URI, Const.JAXB_NSURI),
    86                 locator );
    87             errorHandler.warning(e);
    88         }
    90         super.endDocument();
    91     }
    93     @Override
    94     public void startPrefixMapping(String prefix, String uri) throws SAXException {
    95         if (WellKnownNamespace.XML_NAMESPACE_URI.equals(uri)) return; //xml prefix shall not be declared based on jdk api javadoc
    96         if( prefix.equals("jaxb") )
    97             isJAXBPrefixUsed = true;
    98         if( uri.equals(Const.JAXB_NSURI) )
    99             isCustomizationUsed = true;
   101         super.startPrefixMapping(prefix, uri);
   102     }
   104     @Override
   105     public void endPrefixMapping(String prefix) throws SAXException {
   106         if ("xml".equals(prefix)) return; //xml prefix shall not be declared based on jdk api javadoc
   107         super.endPrefixMapping(prefix);
   108     }
   110     @Override
   111     public void startElement(String namespaceURI, String localName, String qName, Attributes atts)
   112         throws SAXException {
   113         super.startElement(namespaceURI, localName, qName, atts);
   115         // I'm not sure if this is necessary (SAX might report the change of the default prefix
   116         // through the startPrefixMapping method, and I think it does indeed.)
   117         //
   118         // but better safe than sorry.
   120         if( namespaceURI.equals(Const.JAXB_NSURI) )
   121             isCustomizationUsed = true;
   122     }
   124     @Override
   125     public void setDocumentLocator( Locator locator ) {
   126         super.setDocumentLocator( locator );
   127         this.locator = locator;
   128     }
   129 }

mercurial