286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * The Apache Software License, Version 1.1
286N/A *
286N/A *
286N/A * Copyright (c) 1999-2003 The Apache Software Foundation.
286N/A * All rights reserved.
286N/A *
286N/A * Redistribution and use in source and binary forms, with or without
286N/A * modification, are permitted provided that the following conditions
286N/A * are met:
286N/A *
286N/A * 1. Redistributions of source code must retain the above copyright
286N/A * notice, this list of conditions and the following disclaimer.
286N/A *
286N/A * 2. Redistributions in binary form must reproduce the above copyright
286N/A * notice, this list of conditions and the following disclaimer in
286N/A * the documentation and/or other materials provided with the
286N/A * distribution.
286N/A *
286N/A * 3. The end-user documentation included with the redistribution,
286N/A * if any, must include the following acknowledgment:
286N/A * "This product includes software developed by the
286N/A * Apache Software Foundation (http://www.apache.org/)."
286N/A * Alternately, this acknowledgment may appear in the software itself,
286N/A * if and wherever such third-party acknowledgments normally appear.
286N/A *
286N/A * 4. The names "Xerces" and "Apache Software Foundation" must
286N/A * not be used to endorse or promote products derived from this
286N/A * software without prior written permission. For written
286N/A * permission, please contact apache@apache.org.
286N/A *
286N/A * 5. Products derived from this software may not be called "Apache",
286N/A * nor may "Apache" appear in their name, without prior written
286N/A * permission of the Apache Software Foundation.
286N/A *
286N/A * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
286N/A * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
286N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
286N/A * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
286N/A * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
286N/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
286N/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
286N/A * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
286N/A * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
286N/A * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
286N/A * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
286N/A * SUCH DAMAGE.
286N/A * ====================================================================
286N/A *
286N/A * This software consists of voluntary contributions made by many
286N/A * individuals on behalf of the Apache Software Foundation and was
286N/A * originally based on software copyright (c) 2002, International
286N/A * Business Machines, Inc., http://www.apache.org. For more
286N/A * information on the Apache Software Foundation, please see
286N/A * <http://www.apache.org/>.
286N/A */
286N/A
286N/Apackage com.sun.org.apache.xerces.internal.impl.dtd;
286N/A
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
286N/Aimport com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
286N/Aimport com.sun.org.apache.xerces.internal.util.XMLSymbols;
286N/Aimport com.sun.org.apache.xerces.internal.xni.Augmentations;
286N/Aimport com.sun.org.apache.xerces.internal.xni.NamespaceContext;
286N/Aimport com.sun.org.apache.xerces.internal.xni.QName;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLAttributes;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XNIException;
286N/A
286N/A/**
286N/A * The DTD validator. The validator implements a document
286N/A * filter: receiving document events from the scanner; validating
286N/A * the content and structure; augmenting the InfoSet, if applicable;
286N/A * and notifying the parser of the information resulting from the
286N/A * validation process.
286N/A * <p> Formerly, this component also handled DTD events and grammar construction.
286N/A * To facilitate the development of a meaningful DTD grammar caching/preparsing
286N/A * framework, this functionality has been moved into the XMLDTDLoader
286N/A * class. Therefore, this class no longer implements the DTDFilter
286N/A * or DTDContentModelFilter interfaces.
286N/A * <p>
286N/A * This component requires the following features and properties from the
286N/A * component manager that uses it:
286N/A * <ul>
286N/A * <li>http://xml.org/sax/features/namespaces</li>
286N/A * <li>http://xml.org/sax/features/validation</li>
286N/A * <li>http://apache.org/xml/features/validation/dynamic</li>
286N/A * <li>http://apache.org/xml/properties/internal/symbol-table</li>
286N/A * <li>http://apache.org/xml/properties/internal/error-reporter</li>
286N/A * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
286N/A * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
286N/A * </ul>
286N/A *
286N/A * @xerces.internal
286N/A *
286N/A * @author Elena Litani, IBM
286N/A * @author Michael Glavassevich, IBM
286N/A *
286N/A
286N/A */
286N/Apublic class XML11NSDTDValidator extends XML11DTDValidator {
286N/A
286N/A /** Attribute QName. */
286N/A private QName fAttributeQName = new QName();
286N/A
286N/A /** Bind namespaces */
286N/A protected final void startNamespaceScope(QName element, XMLAttributes attributes, Augmentations augs)
286N/A throws XNIException {
286N/A
286N/A // add new namespace context
286N/A fNamespaceContext.pushContext();
286N/A
286N/A if (element.prefix == XMLSymbols.PREFIX_XMLNS) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "ElementXMLNSPrefix",
286N/A new Object[] { element.rawname },
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A
286N/A // search for new namespace bindings
286N/A int length = attributes.getLength();
286N/A for (int i = 0; i < length; i++) {
286N/A String localpart = attributes.getLocalName(i);
286N/A String prefix = attributes.getPrefix(i);
286N/A // when it's of form xmlns="..." or xmlns:prefix="...",
286N/A // it's a namespace declaration. but prefix:xmlns="..." isn't.
286N/A if (prefix == XMLSymbols.PREFIX_XMLNS || prefix == XMLSymbols.EMPTY_STRING
286N/A && localpart == XMLSymbols.PREFIX_XMLNS) {
286N/A
286N/A // get the internalized value of this attribute
286N/A String uri = fSymbolTable.addSymbol(attributes.getValue(i));
286N/A
286N/A // 1. "xmlns" can't be bound to any namespace
286N/A if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "CantBindXMLNS",
286N/A new Object[] { attributes.getQName(i)},
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A
286N/A // 2. the namespace for "xmlns" can't be bound to any prefix
286N/A if (uri == NamespaceContext.XMLNS_URI) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "CantBindXMLNS",
286N/A new Object[] { attributes.getQName(i)},
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A
286N/A // 3. "xml" can't be bound to any other namespace than it's own
286N/A if (localpart == XMLSymbols.PREFIX_XML) {
286N/A if (uri != NamespaceContext.XML_URI) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "CantBindXML",
286N/A new Object[] { attributes.getQName(i)},
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A }
286N/A // 4. the namespace for "xml" can't be bound to any other prefix
286N/A else {
286N/A if (uri == NamespaceContext.XML_URI) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "CantBindXML",
286N/A new Object[] { attributes.getQName(i)},
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A }
286N/A
286N/A prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING;
286N/A
286N/A // Declare prefix in context. Removing the association between a prefix and a
286N/A // namespace name is permitted in XML 1.1, so if the uri value is the empty string,
286N/A // the prefix is being unbound. -- mrglavas
286N/A fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null);
286N/A }
286N/A }
286N/A
286N/A // bind the element
286N/A String prefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING;
286N/A element.uri = fNamespaceContext.getURI(prefix);
286N/A if (element.prefix == null && element.uri != null) {
286N/A element.prefix = XMLSymbols.EMPTY_STRING;
286N/A }
286N/A if (element.prefix != null && element.uri == null) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "ElementPrefixUnbound",
286N/A new Object[] { element.prefix, element.rawname },
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A
286N/A // bind the attributes
286N/A for (int i = 0; i < length; i++) {
286N/A attributes.getName(i, fAttributeQName);
286N/A String aprefix = fAttributeQName.prefix != null ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING;
286N/A String arawname = fAttributeQName.rawname;
286N/A if (arawname == XMLSymbols.PREFIX_XMLNS) {
286N/A fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS);
286N/A attributes.setName(i, fAttributeQName);
286N/A } else if (aprefix != XMLSymbols.EMPTY_STRING) {
286N/A fAttributeQName.uri = fNamespaceContext.getURI(aprefix);
286N/A if (fAttributeQName.uri == null) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "AttributePrefixUnbound",
286N/A new Object[] { element.rawname, arawname, aprefix },
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A attributes.setName(i, fAttributeQName);
286N/A }
286N/A }
286N/A
286N/A // verify that duplicate attributes don't exist
286N/A // Example: <foo xmlns:a='NS' xmlns:b='NS' a:attr='v1' b:attr='v2'/>
286N/A int attrCount = attributes.getLength();
286N/A for (int i = 0; i < attrCount - 1; i++) {
286N/A String auri = attributes.getURI(i);
286N/A if (auri == null || auri == NamespaceContext.XMLNS_URI) {
286N/A continue;
286N/A }
286N/A String alocalpart = attributes.getLocalName(i);
286N/A for (int j = i + 1; j < attrCount; j++) {
286N/A String blocalpart = attributes.getLocalName(j);
286N/A String buri = attributes.getURI(j);
286N/A if (alocalpart == blocalpart && auri == buri) {
286N/A fErrorReporter.reportError(
286N/A XMLMessageFormatter.XMLNS_DOMAIN,
286N/A "AttributeNSNotUnique",
286N/A new Object[] { element.rawname, alocalpart, auri },
286N/A XMLErrorReporter.SEVERITY_FATAL_ERROR);
286N/A }
286N/A }
286N/A }
286N/A
286N/A } // startNamespaceScope(QName,XMLAttributes)
286N/A
286N/A /** Handles end element. */
286N/A protected void endNamespaceScope(QName element, Augmentations augs, boolean isEmpty)
286N/A throws XNIException {
286N/A
286N/A // bind element
286N/A String eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING;
286N/A element.uri = fNamespaceContext.getURI(eprefix);
286N/A if (element.uri != null) {
286N/A element.prefix = eprefix;
286N/A }
286N/A
286N/A // call handlers
286N/A if (fDocumentHandler != null) {
286N/A if (!isEmpty) {
286N/A fDocumentHandler.endElement(element, augs);
286N/A }
286N/A }
286N/A
286N/A // pop context
286N/A fNamespaceContext.popContext();
286N/A
286N/A } // endNamespaceScope(QName,boolean)
286N/A}