286N/A/*
286N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
286N/A */
286N/A
286N/A/*
286N/A * Copyright 2005 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A
286N/Apackage com.sun.xml.internal.stream.dtd;
286N/Aimport com.sun.xml.internal.stream.dtd.nonvalidating.DTDGrammar;
286N/Aimport com.sun.xml.internal.stream.dtd.nonvalidating.XMLAttributeDecl;
286N/Aimport com.sun.xml.internal.stream.dtd.nonvalidating.XMLElementDecl;
286N/Aimport com.sun.xml.internal.stream.dtd.nonvalidating.XMLSimpleType;
286N/Aimport com.sun.org.apache.xerces.internal.impl.Constants;
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
286N/Aimport com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
286N/Aimport com.sun.org.apache.xerces.internal.util.SymbolTable;
286N/Aimport com.sun.org.apache.xerces.internal.util.XMLChar;
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.QName;
286N/Aimport com.sun.org.apache.xerces.internal.xni.NamespaceContext;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLAttributes;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLLocator;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XMLString;
286N/Aimport com.sun.org.apache.xerces.internal.xni.XNIException;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
286N/Aimport com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
286N/Aimport javax.xml.XMLConstants;
286N/A
286N/A /*
286N/A * @author Eric Ye, IBM
286N/A * @author Andy Clark, IBM
286N/A * @author Jeffrey Rodriguez IBM
286N/A * @author Neil Graham, IBM
286N/A * @author Sunitha Reddy, Sun Microsystems
286N/A */
286N/A
286N/Apublic class DTDGrammarUtil {
286N/A
286N/A
286N/A /** Property identifier: symbol table. */
286N/A protected static final String SYMBOL_TABLE =
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
286N/A
286N/A protected static final String NAMESPACES =
286N/A Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
286N/A
286N/A
286N/A /** Compile to true to debug attributes. */
286N/A private static final boolean DEBUG_ATTRIBUTES = false;
286N/A
286N/A /** Compile to true to debug element children. */
286N/A private static final boolean DEBUG_ELEMENT_CHILDREN = false;
286N/A
286N/A protected DTDGrammar fDTDGrammar = null;
286N/A /** Namespaces. */
286N/A protected boolean fNamespaces;
286N/A
286N/A /** Symbol table. */
286N/A protected SymbolTable fSymbolTable = null;
286N/A
286N/A /** Current element index. */
286N/A private int fCurrentElementIndex = -1;
286N/A
286N/A /** Current content spec type. */
286N/A private int fCurrentContentSpecType = -1;
286N/A
286N/A /** Content spec type stack. */
286N/A private boolean[] fElementContentState = new boolean[8];
286N/A
286N/A /** Element depth. */
286N/A private int fElementDepth = -1;
286N/A
286N/A /** True if inside of element content. */
286N/A private boolean fInElementContent = false;
286N/A
286N/A /** Temporary atribute declaration. */
286N/A private XMLAttributeDecl fTempAttDecl = new XMLAttributeDecl();
286N/A
286N/A /** Temporary qualified name. */
286N/A private QName fTempQName = new QName();
286N/A
286N/A /** Temporary string buffers. */
286N/A private StringBuffer fBuffer = new StringBuffer();
286N/A
286N/A private NamespaceContext fNamespaceContext = null;
286N/A
286N/A /** Default constructor. */
286N/A public DTDGrammarUtil(SymbolTable symbolTable) {
286N/A fSymbolTable = symbolTable;
286N/A }
286N/A
286N/A public DTDGrammarUtil(DTDGrammar grammar, SymbolTable symbolTable) {
286N/A fDTDGrammar = grammar;
286N/A fSymbolTable = symbolTable;
286N/A }
286N/A
286N/A public DTDGrammarUtil(DTDGrammar grammar, SymbolTable symbolTable,
286N/A NamespaceContext namespaceContext) {
286N/A fDTDGrammar = grammar;
286N/A fSymbolTable = symbolTable;
286N/A fNamespaceContext = namespaceContext;
286N/A }
286N/A
286N/A /*
286N/A * Resets the component. The component can query the component manager
286N/A * about any features and properties that affect the operation of the
286N/A * component.
286N/A *
286N/A * @param componentManager The component manager.
286N/A *
286N/A * @throws SAXException Thrown by component on finitialization error.
286N/A * For example, if a feature or property is
286N/A * required for the operation of the component, the
286N/A * component manager may throw a
286N/A * SAXNotRecognizedException or a
286N/A * SAXNotSupportedException.
286N/A */
286N/A public void reset(XMLComponentManager componentManager)
286N/A throws XMLConfigurationException {
286N/A
286N/A fDTDGrammar = null;
286N/A fInElementContent = false;
286N/A fCurrentElementIndex = -1;
286N/A fCurrentContentSpecType = -1;
286N/A fNamespaces = componentManager.getFeature(NAMESPACES, true);
286N/A fSymbolTable = (SymbolTable) componentManager.getProperty(
286N/A Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY);
286N/A fElementDepth = -1;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * The start of an element.
286N/A *
286N/A * @param element The name of the element.
286N/A * @param attributes The element attributes.
286N/A * @param augs Additional information that may include infoset augmentations
286N/A *
286N/A * @throws XNIException Thrown by handler to signal an error.
286N/A */
286N/A public void startElement(QName element, XMLAttributes attributes) throws XNIException {
286N/A handleStartElement(element, attributes);
286N/A }
286N/A
286N/A /**
286N/A * The end of an element.
286N/A *
286N/A * @param element The name of the element.
286N/A * @param augs Additional information that may include infoset augmentations
286N/A *
286N/A * @throws XNIException Thrown by handler to signal an error.
286N/A */
286N/A public void endElement(QName element) throws XNIException {
286N/A handleEndElement(element);
286N/A }
286N/A
286N/A /**
286N/A * The start of a CDATA section.
286N/A * @param augs Additional information that may include infoset augmentations
286N/A *
286N/A * @throws XNIException Thrown by handler to signal an error.
286N/A */
286N/A public void startCDATA(Augmentations augs) throws XNIException {
286N/A }
286N/A
286N/A /**
286N/A * The end of a CDATA section.
286N/A * @param augs Additional information that may include infoset augmentations
286N/A *
286N/A * @throws XNIException Thrown by handler to signal an error.
286N/A */
286N/A public void endCDATA(Augmentations augs) throws XNIException {
286N/A }
286N/A
286N/A
286N/A
286N/A /** Add default attributes and validate. */
286N/A public void addDTDDefaultAttrs(QName elementName, XMLAttributes attributes)
286N/A throws XNIException {
286N/A
286N/A int elementIndex;
286N/A elementIndex = fDTDGrammar.getElementDeclIndex(elementName);
286N/A // is there anything to do?
286N/A if (elementIndex == -1 || fDTDGrammar == null) {
286N/A return;
286N/A }
286N/A
286N/A //
286N/A // Check after all specified attrs are scanned
286N/A // (1) report error for REQUIRED attrs that are missing (V_TAGc)
286N/A // (2) add default attrs (FIXED and NOT_FIXED)
286N/A //
286N/A int attlistIndex = fDTDGrammar.getFirstAttributeDeclIndex(elementIndex);
286N/A
286N/A while (attlistIndex != -1) {
286N/A
286N/A fDTDGrammar.getAttributeDecl(attlistIndex, fTempAttDecl);
286N/A
286N/A if (DEBUG_ATTRIBUTES) {
286N/A if (fTempAttDecl != null) {
286N/A XMLElementDecl elementDecl = new XMLElementDecl();
286N/A fDTDGrammar.getElementDecl(elementIndex, elementDecl);
286N/A System.out.println("element: " + (elementDecl.name.localpart));
286N/A System.out.println("attlistIndex " + attlistIndex + "\n" +
286N/A "attName : '" + (fTempAttDecl.name.localpart) + "'\n"
286N/A + "attType : " + fTempAttDecl.simpleType.type + "\n"
286N/A + "attDefaultType : " + fTempAttDecl.simpleType.defaultType + "\n"
286N/A + "attDefaultValue : '" + fTempAttDecl.simpleType.defaultValue + "'\n"
286N/A + attributes.getLength() + "\n"
286N/A );
286N/A }
286N/A }
286N/A String attPrefix = fTempAttDecl.name.prefix;
286N/A String attLocalpart = fTempAttDecl.name.localpart;
286N/A String attRawName = fTempAttDecl.name.rawname;
286N/A String attType = getAttributeTypeName(fTempAttDecl);
286N/A int attDefaultType = fTempAttDecl.simpleType.defaultType;
286N/A String attValue = null;
286N/A
286N/A if (fTempAttDecl.simpleType.defaultValue != null) {
286N/A attValue = fTempAttDecl.simpleType.defaultValue;
286N/A }
286N/A boolean specified = false;
286N/A boolean required = attDefaultType == XMLSimpleType.DEFAULT_TYPE_REQUIRED;
286N/A boolean cdata = attType == XMLSymbols.fCDATASymbol;
286N/A
286N/A if (!cdata || required || attValue != null) {
286N/A
286N/A //check whether attribute is a namespace declaration
286N/A if (fNamespaceContext != null && attRawName.startsWith(XMLConstants.XMLNS_ATTRIBUTE)) {
286N/A String prefix = "";
286N/A int pos = attRawName.indexOf(':');
286N/A if (pos != -1) {
286N/A prefix = attRawName.substring(0, pos);
286N/A } else {
286N/A prefix = attRawName;
286N/A }
286N/A prefix = fSymbolTable.addSymbol(prefix);
286N/A if (!((com.sun.org.apache.xerces.internal.util.
286N/A NamespaceSupport) fNamespaceContext).
286N/A containsPrefixInCurrentContext(prefix)) {
286N/A fNamespaceContext.declarePrefix(prefix, attValue);
286N/A }
286N/A specified = true;
286N/A } else {
286N/A
286N/A int attrCount = attributes.getLength();
286N/A for (int i = 0; i < attrCount; i++) {
286N/A if (attributes.getQName(i) == attRawName) {
286N/A specified = true;
286N/A break;
286N/A }
286N/A }
286N/A
286N/A }
286N/A
286N/A }
286N/A
286N/A if (!specified) {
286N/A if (attValue != null) {
286N/A if (fNamespaces) {
286N/A int index = attRawName.indexOf(':');
286N/A if (index != -1) {
286N/A attPrefix = attRawName.substring(0, index);
286N/A attPrefix = fSymbolTable.addSymbol(attPrefix);
286N/A attLocalpart = attRawName.substring(index + 1);
286N/A attLocalpart = fSymbolTable.addSymbol(attLocalpart);
286N/A }
286N/A }
286N/A fTempQName.setValues(attPrefix, attLocalpart, attRawName,
286N/A fTempAttDecl.name.uri);
286N/A int newAttr = attributes.addAttribute(fTempQName, attType,
286N/A attValue);
286N/A }
286N/A }
286N/A attlistIndex = fDTDGrammar.getNextAttributeDeclIndex(attlistIndex);
286N/A }
286N/A
286N/A // now iterate through the expanded attributes for
286N/A // 1. if every attribute seen is declared in the DTD
286N/A // 2. check if the VC: default_fixed holds
286N/A // 3. validate every attribute.
286N/A int attrCount = attributes.getLength();
286N/A for (int i = 0; i < attrCount; i++) {
286N/A String attrRawName = attributes.getQName(i);
286N/A boolean declared = false;
286N/A int position =
286N/A fDTDGrammar.getFirstAttributeDeclIndex(elementIndex);
286N/A while (position != -1) {
286N/A fDTDGrammar.getAttributeDecl(position, fTempAttDecl);
286N/A if (fTempAttDecl.name.rawname == attrRawName) {
286N/A // found the match att decl,
286N/A declared = true;
286N/A break;
286N/A }
286N/A position = fDTDGrammar.getNextAttributeDeclIndex(position);
286N/A }
286N/A if (!declared) {
286N/A continue;
286N/A }
286N/A
286N/A String type = getAttributeTypeName(fTempAttDecl);
286N/A attributes.setType(i, type);
286N/A
286N/A boolean changedByNormalization = false;
286N/A if (attributes.isSpecified(i) && type != XMLSymbols.fCDATASymbol) {
286N/A changedByNormalization = normalizeAttrValue(attributes, i);
286N/A }
286N/A } // for all attributes
286N/A
286N/A } // addDTDDefaultAttrsAndValidate(int,XMLAttrList)
286N/A
286N/A
286N/A /**
286N/A * Normalize the attribute value of a non CDATA attributes collapsing
286N/A * sequences of space characters (x20)
286N/A *
286N/A * @param attributes The list of attributes
286N/A * @param index The index of the attribute to normalize
286N/A */
286N/A private boolean normalizeAttrValue(XMLAttributes attributes, int index) {
286N/A // vars
286N/A boolean leadingSpace = true;
286N/A boolean spaceStart = false;
286N/A boolean readingNonSpace = false;
286N/A int count = 0;
286N/A int eaten = 0;
286N/A String attrValue = attributes.getValue(index);
286N/A char[] attValue = new char[attrValue.length()];
286N/A
286N/A fBuffer.setLength(0);
286N/A attrValue.getChars(0, attrValue.length(), attValue, 0);
286N/A for (int i = 0; i < attValue.length; i++) {
286N/A
286N/A if (attValue[i] == ' ') {
286N/A
286N/A // now the tricky part
286N/A if (readingNonSpace) {
286N/A spaceStart = true;
286N/A readingNonSpace = false;
286N/A }
286N/A
286N/A if (spaceStart && !leadingSpace) {
286N/A spaceStart = false;
286N/A fBuffer.append(attValue[i]);
286N/A count++;
286N/A } else {
286N/A if (leadingSpace || !spaceStart) {
286N/A eaten++;
286N/A }
286N/A }
286N/A
286N/A } else {
286N/A readingNonSpace = true;
286N/A spaceStart = false;
286N/A leadingSpace = false;
286N/A fBuffer.append(attValue[i]);
286N/A count++;
286N/A }
286N/A }
286N/A
286N/A // check if the last appended character is a space.
286N/A if (count > 0 && fBuffer.charAt(count - 1) == ' ') {
286N/A fBuffer.setLength(count - 1);
286N/A
286N/A }
286N/A String newValue = fBuffer.toString();
286N/A attributes.setValue(index, newValue);
286N/A return !attrValue.equals(newValue);
286N/A }
286N/A
286N/A
286N/A
286N/A /** convert attribute type from ints to strings */
286N/A private String getAttributeTypeName(XMLAttributeDecl attrDecl) {
286N/A
286N/A switch (attrDecl.simpleType.type) {
286N/A case XMLSimpleType.TYPE_ENTITY: {
286N/A return attrDecl.simpleType.list ? XMLSymbols.fENTITIESSymbol :
286N/A XMLSymbols.fENTITYSymbol;
286N/A }
286N/A case XMLSimpleType.TYPE_ENUMERATION: {
286N/A StringBuffer buffer = new StringBuffer();
286N/A buffer.append('(');
286N/A for (int i = 0; i < attrDecl.simpleType.enumeration.length; i++) {
286N/A if (i > 0) {
286N/A buffer.append("|");
286N/A }
286N/A buffer.append(attrDecl.simpleType.enumeration[i]);
286N/A }
286N/A buffer.append(')');
286N/A return fSymbolTable.addSymbol(buffer.toString());
286N/A }
286N/A case XMLSimpleType.TYPE_ID: {
286N/A return XMLSymbols.fIDSymbol;
286N/A }
286N/A case XMLSimpleType.TYPE_IDREF: {
286N/A return attrDecl.simpleType.list ? XMLSymbols.fIDREFSSymbol :
286N/A XMLSymbols.fIDREFSymbol;
286N/A }
286N/A case XMLSimpleType.TYPE_NMTOKEN: {
286N/A return attrDecl.simpleType.list ? XMLSymbols.fNMTOKENSSymbol :
286N/A XMLSymbols.fNMTOKENSymbol;
286N/A }
286N/A case XMLSimpleType.TYPE_NOTATION: {
286N/A return XMLSymbols.fNOTATIONSymbol;
286N/A }
286N/A }
286N/A return XMLSymbols.fCDATASymbol;
286N/A
286N/A }
286N/A
286N/A
286N/A /** ensure element stack capacity */
286N/A private void ensureStackCapacity(int newElementDepth) {
286N/A if (newElementDepth == fElementContentState.length) {
286N/A boolean[] newStack = new boolean[newElementDepth * 2];
286N/A System.arraycopy(this.fElementContentState, 0, newStack, 0,
286N/A newElementDepth);
286N/A fElementContentState = newStack;
286N/A }
286N/A }
286N/A
286N/A
286N/A
286N/A /** Handle element
286N/A * @return true if validator is removed from the pipeline
286N/A */
286N/A protected void handleStartElement(QName element, XMLAttributes attributes) throws XNIException {
286N/A
286N/A if (fDTDGrammar == null) {
286N/A fCurrentElementIndex = -1;
286N/A fCurrentContentSpecType = -1;
286N/A fInElementContent = false;
286N/A return;
286N/A } else {
286N/A fCurrentElementIndex = fDTDGrammar.getElementDeclIndex(element);
286N/A fCurrentContentSpecType = fDTDGrammar.getContentSpecType(
286N/A fCurrentElementIndex);
286N/A //handleDTDDefaultAttrs(element,attributes);
286N/A addDTDDefaultAttrs(element, attributes);
286N/A }
286N/A
286N/A fInElementContent = fCurrentContentSpecType == XMLElementDecl.TYPE_CHILDREN;
286N/A fElementDepth++;
286N/A ensureStackCapacity(fElementDepth);
286N/A fElementContentState[fElementDepth] = fInElementContent;
286N/A }
286N/A
286N/A
286N/A /** Handle end element. */
286N/A protected void handleEndElement(QName element) throws XNIException {
286N/A if (fDTDGrammar == null) return;
286N/A fElementDepth--;
286N/A if (fElementDepth < -1) {
286N/A throw new RuntimeException("FWK008 Element stack underflow");
286N/A }
286N/A if (fElementDepth < 0) {
286N/A fCurrentElementIndex = -1;
286N/A fCurrentContentSpecType = -1;
286N/A fInElementContent = false;
286N/A return;
286N/A }
286N/A fInElementContent = fElementContentState[fElementDepth];
286N/A }
286N/A
286N/A public boolean isInElementContent() {
286N/A return fInElementContent;
286N/A }
286N/A
286N/A public boolean isIgnorableWhiteSpace(XMLString text) {
286N/A if (isInElementContent()) {
286N/A for (int i = text.offset; i < text.offset + text.length; i++) {
286N/A if (!XMLChar.isSpace(text.ch[i])) {
286N/A return false;
286N/A }
286N/A }
286N/A return true;
286N/A }
286N/A return false;
286N/A }
286N/A}