schemadom.java

来自「JAVA 所有包」· Java 代码 · 共 450 行 · 第 1/2 页

JAVA
450
字号
/* * Copyright 2001-2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.sun.org.apache.xerces.internal.impl.xs.opti;import com.sun.org.apache.xerces.internal.xni.NamespaceContext;import com.sun.org.apache.xerces.internal.xni.QName;import com.sun.org.apache.xerces.internal.xni.XMLAttributes;import com.sun.org.apache.xerces.internal.xni.XMLString;import com.sun.org.apache.xerces.internal.util.XMLSymbols;import org.w3c.dom.Attr;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import java.util.Vector;import java.util.Enumeration;/** * @xerces.internal   *  * @author Rahul Srivastava, Sun Microsystems Inc. * @author Sandy Gao, IBM * * @version $Id: SchemaDOM.java,v 1.2.6.1 2005/09/08 10:33:57 sunithareddy Exp $ */public class SchemaDOM extends DefaultDocument {        static final int relationsRowResizeFactor = 15;    static final int relationsColResizeFactor = 10;        NodeImpl[][] relations;    // parent must be an element in this scheme    ElementImpl parent;    int currLoc;    int nextFreeLoc;    boolean hidden;    boolean inCDATA;        // for annotation support:    StringBuffer fAnnotationBuffer = null;        public SchemaDOM() {        reset();    }            public void startElement(QName element, XMLAttributes attributes,            int line, int column, int offset) {        ElementImpl node = new ElementImpl(line, column, offset);        processElement(element, attributes, node);        // now the current node added, becomes the parent        parent = node;    }            public void emptyElement(QName element, XMLAttributes attributes,            int line, int column, int offset) {        ElementImpl node = new ElementImpl(line, column, offset);        processElement(element, attributes, node);    }        public void startElement(QName element, XMLAttributes attributes,            int line, int column) {        startElement(element, attributes, line, column, -1);    }            public void emptyElement(QName element, XMLAttributes attributes,            int line, int column) {        emptyElement(element, attributes, line, column, -1);    }            private void processElement(QName element, XMLAttributes attributes, ElementImpl node) {                // populate node        node.prefix = element.prefix;        node.localpart = element.localpart;        node.rawname = element.rawname;        node.uri = element.uri;        node.schemaDOM = this;                // set the attributes        Attr[] attrs = new Attr[attributes.getLength()];        for (int i=0; i<attributes.getLength(); i++) {            attrs[i] = new AttrImpl(null,                     attributes.getPrefix(i),                     attributes.getLocalName(i),                     attributes.getQName(i),                     attributes.getURI(i),                     attributes.getValue(i));        }        node.attrs = attrs;                // check if array needs to be resized        if (nextFreeLoc == relations.length) {            resizeRelations();        }                // store the current parent        //if (relations[currLoc][0] == null || relations[currLoc][0] != parent) {        if (relations[currLoc][0] != parent) {            relations[nextFreeLoc][0] = parent;            currLoc = nextFreeLoc++;        }                // add the current node as child of parent        boolean foundPlace = false;        int i = 1;        for (i = 1; i<relations[currLoc].length; i++) {            if (relations[currLoc][i] == null) {                foundPlace = true;                break;            }        }                if (!foundPlace) {            resizeRelations(currLoc);        }        relations[currLoc][i] = node;                parent.parentRow = currLoc;        node.row = currLoc;        node.col = i;    }            public void endElement()  {        // the parent of current parent node becomes the parent        // for the next node.        currLoc = parent.row;        parent = (ElementImpl)relations[currLoc][0];    }        // note that this will only be called within appinfo/documentation    void comment(XMLString text) {        fAnnotationBuffer.append("<!--").append(text.toString()).append("-->");    }        // note that this will only be called within appinfo/documentation    void processingInstruction(String target, String data) {        fAnnotationBuffer.append("<?").append(target).append(" ").append(data).append("?>");    }        // note that this will only be called within appinfo/documentation    void characters(XMLString text ) {                // escape characters if necessary        if (!inCDATA) {               for (int i = text.offset; i < text.offset+text.length; ++i ) {                char ch = text.ch[i];                if (ch == '&') {                    fAnnotationBuffer.append("&amp;");                }                 else if (ch == '<') {                    fAnnotationBuffer.append("&lt;");                }                // character sequence "]]>" cannot appear in content,                 // therefore we should escape '>'.                else if (ch == '>') {                    fAnnotationBuffer.append("&gt;");                }                // If CR is part of the document's content, it                // must not be printed as a literal otherwise                // it would be normalized to LF when the document                // is reparsed.                else if (ch == '\r') {                    fAnnotationBuffer.append("&#xD;");                }                else {                    fAnnotationBuffer.append(ch);                }            }        }        else {            fAnnotationBuffer.append(text.ch, text.offset, text.length);        }    }        void endAnnotationElement(QName elemName, boolean complete) {        if(complete) {            fAnnotationBuffer.append("\n</").append(elemName.rawname).append(">");            // note that this is always called after endElement on <annotation>'s            // child and before endElement on annotation.            // hence, we must make this the child of the current            // parent's only child.            ElementImpl child = (ElementImpl)relations[currLoc][1];                        // check if array needs to be resized            if (nextFreeLoc == relations.length) {                resizeRelations();            }            int newRow = child.parentRow = nextFreeLoc++;                         // now find the place to insert this node            boolean foundPlace = false;            int i = 1;            for (; i<relations[newRow].length; i++) {                if (relations[newRow][i] == null) {                    foundPlace = true;                    break;                }            }                        if (!foundPlace) {                resizeRelations(newRow);            }            relations[newRow][i] = new TextImpl(fAnnotationBuffer, this, newRow, i);            // apparently, there is no sensible way of resetting            // these things            fAnnotationBuffer = null;

⌨️ 快捷键说明

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