xsdfacm.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 984 行 · 第 1/3 页

JAVA
984
字号
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, *    if any, must include the following acknowledgment: *       "This product includes software developed by the *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowledgment may appear in the software itself, *    if and wherever such third-party acknowledgments normally appear. * * 4. The names "Xerces" and "Apache Software Foundation" must *    not be used to endorse or promote products derived from this *    software without prior written permission. For written *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", *    nor may "Apache" appear in their name, without prior written *    permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation and was * originally based on software copyright (c) 1999, International * Business Machines, Inc., http://www.apache.org.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */package com.sun.org.apache.xerces.internal.impl.xs.models;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.impl.dtd.models.CMNode;import com.sun.org.apache.xerces.internal.impl.dtd.models.CMStateSet;import com.sun.org.apache.xerces.internal.impl.xs.SubstitutionGroupHandler;import com.sun.org.apache.xerces.internal.impl.xs.XSElementDecl;import com.sun.org.apache.xerces.internal.impl.xs.XSParticleDecl;import com.sun.org.apache.xerces.internal.impl.xs.XSModelGroupImpl;import com.sun.org.apache.xerces.internal.impl.xs.XSWildcardDecl;import com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaException;import com.sun.org.apache.xerces.internal.impl.xs.XSConstraints;import java.util.Vector;/** * DFAContentModel is the implementation of XSCMValidator that does * all of the non-trivial element content validation. This class does * the conversion from the regular expression to the DFA that * it then uses in its validation algorithm. * * @author Neil Graham, IBM * @version $Id: XSDFACM.java,v 1.10 2003/04/30 20:24:49 sandygao Exp $ */public class XSDFACM    implements XSCMValidator {    //    // Constants    //    private static final boolean DEBUG = false;    // special strings    // debugging    /** Set to true to debug content model validation. */    private static final boolean DEBUG_VALIDATE_CONTENT = false;    //    // Data    //    /**     * This is the map of unique input symbol elements to indices into     * each state's per-input symbol transition table entry. This is part     * of the built DFA information that must be kept around to do the     * actual validation.  Note tat since either XSElementDecl or XSParticleDecl object     * can live here, we've got to use an Object.     */    private Object fElemMap[] = null;    /**     * This is a map of whether the element map contains information     * related to ANY models.     */    private int fElemMapType[] = null;    /**     * id of the unique input symbol     */    private int fElemMapId[] = null;        /** The element map size. */    private int fElemMapSize = 0;    /**     * This is an array of booleans, one per state (there are     * fTransTableSize states in the DFA) that indicates whether that     * state is a final state.     */    private boolean fFinalStateFlags[] = null;    /**     * The list of follow positions for each NFA position (i.e. for each     * non-epsilon leaf node.) This is only used during the building of     * the DFA, and is let go afterwards.     */    private CMStateSet fFollowList[] = null;    /**     * This is the head node of our intermediate representation. It is     * only non-null during the building of the DFA (just so that it     * does not have to be passed all around.) Once the DFA is built,     * this is no longer required so its nulled out.     */    private CMNode fHeadNode = null;    /**     * The count of leaf nodes. This is an important number that set some     * limits on the sizes of data structures in the DFA process.     */    private int fLeafCount = 0;    /**     * An array of non-epsilon leaf nodes, which is used during the DFA     * build operation, then dropped.     */    private XSCMLeaf fLeafList[] = null;    /** Array mapping ANY types to the leaf list. */    private int fLeafListType[] = null;    /**     * This is the transition table that is the main by product of all     * of the effort here. It is an array of arrays of ints. The first     * dimension is the number of states we end up with in the DFA. The     * second dimensions is the number of unique elements in the content     * model (fElemMapSize). Each entry in the second dimension indicates     * the new state given that input for the first dimension's start     * state.     * <p>     * The fElemMap array handles mapping from element indexes to     * positions in the second dimension of the transition table.     */    private int fTransTable[][] = null;    /**     * The number of valid entries in the transition table, and in the other     * related tables such as fFinalStateFlags.     */    private int fTransTableSize = 0;    // temp variables    //    // Constructors    //    /**     * Constructs a DFA content model.     *     * @param symbolTable    The symbol table.     * @param syntaxTree    The syntax tree of the content model.     * @param leafCount     The number of leaves.     *     * @exception RuntimeException Thrown if DFA can't be built.     */   public XSDFACM(CMNode syntaxTree, int leafCount) {           // Store away our index and pools in members        fLeafCount = leafCount;        //        //  Create some string pool indexes that represent the names of some        //  magical nodes in the syntax tree.        //  (already done in static initialization...        //        //        //  Ok, so lets grind through the building of the DFA. This method        //  handles the high level logic of the algorithm, but it uses a        //  number of helper classes to do its thing.        //        //  In order to avoid having hundreds of references to the error and        //  string handlers around, this guy and all of his helper classes        //  just throw a simple exception and we then pass it along.        //        if(DEBUG_VALIDATE_CONTENT) {            XSDFACM.time -= System.currentTimeMillis();        }        buildDFA(syntaxTree);        if(DEBUG_VALIDATE_CONTENT) {            XSDFACM.time += System.currentTimeMillis();            System.out.println("DFA build: " + XSDFACM.time + "ms");        }    }    private static long time = 0;    //    // XSCMValidator methods    //    /**     * check whether the given state is one of the final states     *     * @param state       the state to check     *     * @return whether it's a final state     */    public boolean isFinalState (int state) {        return (state < 0)? false :            fFinalStateFlags[state];    }    /**     * one transition only     *     * @param curElem     The current element's QName     * @param stateStack  stack to store the previous state     * @param curPos      the current position of the stack     *     * @return  null if transition is invalid; otherwise the Object corresponding to the     *      XSElementDecl or XSWildcardDecl identified.  Also, the     *      state array will be modified to include the new state; this so that the validator can     *      store it away.     *     * @exception RuntimeException thrown on error     */    public Object oneTransition(QName curElem, int[] state, SubstitutionGroupHandler subGroupHandler) {        int curState = state[0];        if(curState == XSCMValidator.FIRST_ERROR || curState == XSCMValidator.SUBSEQUENT_ERROR) {            // there was an error last time; so just go find correct Object in fElemmMap.            // ... after resetting state[0].            if(curState == XSCMValidator.FIRST_ERROR)                state[0] = XSCMValidator.SUBSEQUENT_ERROR;            return findMatchingDecl(curElem, subGroupHandler);        }        int nextState = 0;        int elemIndex = 0;        Object matchingDecl = null;        for (; elemIndex < fElemMapSize; elemIndex++) {            nextState = fTransTable[curState][elemIndex];            if (nextState == -1)                continue;            int type = fElemMapType[elemIndex] ;            if (type == XSParticleDecl.PARTICLE_ELEMENT) {                matchingDecl = subGroupHandler.getMatchingElemDecl(curElem, (XSElementDecl)fElemMap[elemIndex]);                if (matchingDecl != null) {                    break;                }            }            else if (type == XSParticleDecl.PARTICLE_WILDCARD) {                if(((XSWildcardDecl)fElemMap[elemIndex]).allowNamespace(curElem.uri)) {                    matchingDecl = fElemMap[elemIndex];                    break;                }            }        }        // if we still can't find a match, set the state to first_error        // and return null        if (elemIndex == fElemMapSize) {            state[1] = state[0];            state[0] = XSCMValidator.FIRST_ERROR;            return findMatchingDecl(curElem, subGroupHandler);        }        state[0] = nextState;        return matchingDecl;    } // oneTransition(QName, int[], SubstitutionGroupHandler):  Object    Object findMatchingDecl(QName curElem, SubstitutionGroupHandler subGroupHandler) {        Object matchingDecl = null;        for (int elemIndex = 0; elemIndex < fElemMapSize; elemIndex++) {            int type = fElemMapType[elemIndex] ;            if (type == XSParticleDecl.PARTICLE_ELEMENT) {                matchingDecl = subGroupHandler.getMatchingElemDecl(curElem, (XSElementDecl)fElemMap[elemIndex]);                if (matchingDecl != null) {                    return matchingDecl;                }            }            else if (type == XSParticleDecl.PARTICLE_WILDCARD) {                if(((XSWildcardDecl)fElemMap[elemIndex]).allowNamespace(curElem.uri))                    return fElemMap[elemIndex];            }        }        return null;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?