domnormalizer.java
来自「JAVA 所有包」· Java 代码 · 共 1,336 行 · 第 1/5 页
JAVA
1,336 行
/* * Copyright 1999-2002,2004, 2005 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.dom;import java.io.IOException;import java.io.StringReader;import java.util.Vector;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.dtd.DTDGrammar;import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDDescription;import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator;import com.sun.org.apache.xerces.internal.impl.dv.XSSimpleType;import com.sun.org.apache.xerces.internal.impl.xs.util.SimpleLocator;import com.sun.org.apache.xerces.internal.parsers.XMLGrammarPreparser;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.XML11Char;import com.sun.org.apache.xerces.internal.util.XMLChar;import com.sun.org.apache.xerces.internal.util.XMLGrammarPoolImpl;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.grammars.XMLGrammarPool;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.xni.parser.XMLInputSource;import com.sun.org.apache.xerces.internal.xs.AttributePSVI;import com.sun.org.apache.xerces.internal.xs.ElementPSVI;import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;import org.w3c.dom.Attr;import org.w3c.dom.Comment;import org.w3c.dom.DOMError;import org.w3c.dom.DOMErrorHandler;import org.w3c.dom.Document;import org.w3c.dom.DocumentType;import org.w3c.dom.Element;import org.w3c.dom.Entity;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.ProcessingInstruction;import org.w3c.dom.Text;/** * 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). * * @xerces.experimental * * @author Elena Litani, IBM * @author Neeraj Bajaj, Sun Microsystems, inc. * @version $Id: DOMNormalizer.java,v 1.2.6.3 2005/09/12 05:16:40 sunithareddy Exp $ */public class DOMNormalizer implements XMLDocumentHandler { // // 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; /** 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(); //DTD validator private XMLDTDValidator fDTDValidator; //Check if element content is all "ignorable whitespace" private boolean allWhitespace = false; // Constructor // 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); if ((fConfiguration.features & DOMConfigurationImpl.VALIDATE) != 0) { String schemaLang = (String)fConfiguration.getProperty(DOMConfigurationImpl.JAXP_SCHEMA_LANGUAGE); if(schemaLang != null && schemaLang.equals(Constants.NS_XMLSCHEMA)) { fValidationHandler = CoreDOMImplementationImpl.singleton.getValidator(XMLGrammarDescription.XML_SCHEMA); fConfiguration.setFeature(DOMConfigurationImpl.SCHEMA, true); fConfiguration.setFeature(DOMConfigurationImpl.SCHEMA_FULL_CHECKING, true); // report fatal error on DOM Level 1 nodes fNamespaceValidation = true; // check if we need to fill in PSVI fPSVI = ((fConfiguration.features & DOMConfigurationImpl.PSVI) !=0)?true:false; } fConfiguration.setFeature(DOMConfigurationImpl.XERCES_VALIDATION, true); // reset ID table fDocument.clearIdentifiers(); if(fValidationHandler != null) // reset schema validator ((XMLComponent) fValidationHandler).reset(fConfiguration); } fErrorHandler = (DOMErrorHandler) fConfiguration.getParameter(Constants.DOM_ERROR_HANDLER); if (fValidationHandler != null) { fValidationHandler.setDocumentHandler(this); fValidationHandler.startDocument( new SimpleLocator(fDocument.fDocumentURI, fDocument.fDocumentURI, -1, -1 ), fDocument.encoding, fNamespaceContext, null); } try { Node kid, next; for (kid = fDocument.getFirstChild(); kid != null; kid = next) { next = kid.getNextSibling(); kid = normalizeNode(kid); if (kid != null) { // don't advance next = kid; } } // release resources if (fValidationHandler != null) { fValidationHandler.endDocument(null); CoreDOMImplementationImpl.singleton.releaseValidator( XMLGrammarDescription.XML_SCHEMA, fValidationHandler); fValidationHandler = null; } } catch (RuntimeException e) { if( e==abort ) return; // processing aborted by the user throw e; // otherwise re-throw. } } /** * * This method 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. * * @param node Modified node or null. If node is returned, we need * to normalize again starting on the node returned. * @return the normalized Node */ protected Node normalizeNode (Node node){
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?