domnormalizer.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,354 行 · 第 1/5 页
JAVA
1,354 行
/* * The Apache Software License, Version 1.1 * * * Copyright (c) 1999-2002 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) 2002, 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.dom;import java.util.Vector;//--import com.sun.org.apache.xerces.internal.parsers.XMLGrammarPreparser;import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;import com.sun.org.apache.xerces.internal.xni.grammars.Grammar;import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;import com.sun.org.apache.xerces.internal.xni.parser.XMLParserConfiguration;import org.xml.sax.InputSource;//--import org.w3c.dom.DOMError;import org.w3c.dom.DOMErrorHandler;import com.sun.org.apache.xerces.internal.impl.Constants;import com.sun.org.apache.xerces.internal.impl.RevalidationHandler;import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;import com.sun.org.apache.xerces.internal.util.AugmentationsImpl;import com.sun.org.apache.xerces.internal.util.NamespaceSupport;import com.sun.org.apache.xerces.internal.util.SymbolTable;import com.sun.org.apache.xerces.internal.util.XMLSymbols;import com.sun.org.apache.xerces.internal.xni.Augmentations;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.XMLDocumentHandler;import com.sun.org.apache.xerces.internal.xni.XMLLocator;import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;import com.sun.org.apache.xerces.internal.xni.XMLString;import com.sun.org.apache.xerces.internal.xni.XNIException;import com.sun.org.apache.xerces.internal.xni.grammars.XMLGrammarDescription;import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;import com.sun.org.apache.xerces.internal.xs.AttributePSVI;import com.sun.org.apache.xerces.internal.xs.ElementPSVI;import org.w3c.dom.Attr;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.Text;import org.w3c.dom.ProcessingInstruction;import com.sun.org.apache.xerces.internal.util.XML11Char;import com.sun.org.apache.xerces.internal.util.XMLChar;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.Entity;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Comment;import com.sun.org.apache.xerces.internal.impl.dtd.*;import java.io.*;/** * This class adds implementation for normalizeDocument method. * It acts as if the document was going through a save and load cycle, putting * the document in a "normal" form. The actual result depends on the features being set * and governing what operations actually take place. See setNormalizationFeature for details. * Noticeably this method normalizes Text nodes, makes the document "namespace wellformed", * according to the algorithm described below in pseudo code, by adding missing namespace * declaration attributes and adding or changing namespace prefixes, updates the replacement * tree of EntityReference nodes, normalizes attribute values, etc. * Mutation events, when supported, are generated to reflect the changes occuring on the * document. * See Namespace normalization for details on how namespace declaration attributes and prefixes * are normalized. * * NOTE: There is an initial support for DOM revalidation with XML Schema as a grammar. * The tree might not be validated correctly if entityReferences, CDATA sections are * present in the tree. The PSVI information is not exposed, normalized data (including element * default content is not available). * * NOTE: the implementation is experimental and methods, functionality * can be modified or removed in the future. * * @author Elena Litani, IBM * @author Neeraj Bajaj, Sun Microsystems, inc. * @version $Id: DOMNormalizer.java,v 1.54 2004/04/22 20:39:03 mrglavas Exp $ */public class DOMNormalizer implements XMLDocumentHandler { //-- Added // Constants // // property IDs: /** Property identifier: symbol table. */ public static final String SYMBOL_TABLE = Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; /** Property identifier: grammar pool. */ public static final String GRAMMAR_POOL = Constants.XERCES_PROPERTY_PREFIX + Constants.XMLGRAMMAR_POOL_PROPERTY; // feature ids /** Namespaces feature id (http://xml.org/sax/features/namespaces). */ protected static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; /** Validation feature id (http://xml.org/sax/features/validation). */ protected static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; /** Schema validation feature id (http://apache.org/xml/features/validation/schema). */ protected static final String SCHEMA_VALIDATION_FEATURE_ID = "http://apache.org/xml/features/validation/schema"; /** Schema full checking feature id (http://apache.org/xml/features/validation/schema-full-checking). */ protected static final String SCHEMA_FULL_CHECKING_FEATURE_ID = "http://apache.org/xml/features/validation/schema-full-checking"; //-- // // constants // /** Debug normalize document*/ protected final static boolean DEBUG_ND = false; /** Debug namespace fix up algorithm*/ protected final static boolean DEBUG = false; /** Debug document handler events */ protected final static boolean DEBUG_EVENTS = false; /** prefix added by namespace fixup algorithm should follow a pattern "NS" + index*/ protected final static String PREFIX = "NS"; // // Data // protected DOMConfigurationImpl fConfiguration = null; protected CoreDocumentImpl fDocument = null; protected final XMLAttributesProxy fAttrProxy = new XMLAttributesProxy(); protected final QName fQName = new QName(); /** Validation handler represents validator instance. */ protected RevalidationHandler fValidationHandler; protected XMLDTDValidator fDTDValidator; /** symbol table */ protected SymbolTable fSymbolTable; /** error handler. may be null. */ protected DOMErrorHandler fErrorHandler; /** * Cached {@link DOMError} impl. * The same object is re-used to report multiple errors. */ private final DOMErrorImpl fError = new DOMErrorImpl(); // Validation against namespace aware grammar protected boolean fNamespaceValidation = false; // Update PSVI information in the tree protected boolean fPSVI = false; /** The namespace context of this document: stores namespaces in scope */ protected final NamespaceContext fNamespaceContext = new NamespaceSupport(); /** Stores all namespace bindings on the current element */ protected final NamespaceContext fLocalNSBinder = new NamespaceSupport(); /** list of attributes */ protected final Vector fAttributeList = new Vector(5,10); /** DOM Locator - for namespace fixup algorithm */ protected final DOMLocatorImpl fLocator = new DOMLocatorImpl(); /** for setting the PSVI */ protected Node fCurrentNode = null; private QName fAttrQName = new QName(); // attribute value normalization final XMLString fNormalizedValue = new XMLString(new char[16], 0, 0); /** * If the user stops the process, this exception will be thrown. */ public static final RuntimeException abort = new RuntimeException(); // Constructor // public boolean isWhitespace = false; public boolean docTypeFound = false; public DOMNormalizer(){} /** * Normalizes document. * Note: reset() must be called before this method. */ protected void normalizeDocument(CoreDocumentImpl document, DOMConfigurationImpl config) { fDocument = document; fConfiguration = config; // intialize and reset DOMNormalizer component // fSymbolTable = (SymbolTable) fConfiguration.getProperty(DOMConfigurationImpl.SYMBOL_TABLE); // reset namespace context fNamespaceContext.reset(); fNamespaceContext.declarePrefix(XMLSymbols.EMPTY_STRING, XMLSymbols.EMPTY_STRING); docTypeFound = false; if ((fConfiguration.features & DOMConfigurationImpl.VALIDATE) != 0) { String type =(String) fConfiguration.getProperty(fConfiguration.JAXP_SCHEMA_LANGUAGE); if(type != null && type.equals(Constants.NS_XMLSCHEMA)) fValidationHandler = CoreDOMImplementationImpl.singleton.getValidator(XMLGrammarDescription.XML_SCHEMA); fConfiguration.setFeature(DOMConfigurationImpl.XERCES_VALIDATION, true); fConfiguration.setFeature(DOMConfigurationImpl.SCHEMA, true); // report fatal error on DOM Level 1 nodes fNamespaceValidation = true; // check if we need to fill in PSVI
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?