xmlstreamfilterimpl.java
来自「JAVA 所有包」· Java 代码 · 共 973 行 · 第 1/2 页
JAVA
973 行
/* * The contents of this file are subject to the terms * of the Common Development and Distribution License * (the "License"). You may not use this file except * in compliance with the License. * * You can obtain a copy of the license at * https://jaxp.dev.java.net/CDDLv1.0.html. * See the License for the specific language governing * permissions and limitations under the License. * * When distributing Covered Code, include this CDDL * HEADER in each file and include the License file at * https://jaxp.dev.java.net/CDDLv1.0.html * If applicable add the following below this CDDL HEADER * with the fields enclosed by brackets "[]" replaced with * your own identifying information: Portions Copyright * [year] [name of copyright owner] *//* * $Id: XMLStreamFilterImpl.java,v 1.4 2005/12/02 09:02:19 neerajbj Exp $ * @(#)XMLStreamFilterImpl.java 1.8 06/03/23 * * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. */package com.sun.org.apache.xerces.internal.impl;import com.sun.xml.internal.stream.events.AttributeImpl;import com.sun.xml.internal.stream.events.NamespaceImpl;import java.util.ArrayList;import javax.xml.XMLConstants;import javax.xml.stream.Location;import javax.xml.stream.XMLStreamReader;import javax.xml.stream.StreamFilter;import javax.xml.stream.XMLStreamException;import javax.xml.namespace.QName;import javax.xml.stream.events.XMLEvent;import com.sun.org.apache.xerces.internal.util.NamespaceContextWrapper;import com.sun.org.apache.xerces.internal.util.NamespaceSupport;import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl;import com.sun.org.apache.xerces.internal.util.XMLChar;/** * * @author K.Venugopal@sun.com */public class XMLStreamFilterImpl implements javax.xml.stream.XMLStreamReader { private StreamFilter fStreamFilter = null; private XMLStreamReader fStreamReader = null; private int fCurrentEventType = -1; private QName fElementName = null; private String fLocalName = null; private boolean fHasName = false; private boolean fReadNext = true; private boolean fHasMoreEvents = true; private boolean fReadFromCache = true; private ArrayList fCachedAttributes = null; private ArrayList fCachedNamespaceAttr = null; private NamespaceContextWrapper fCachedNamespaceContext = null; private String fCachedElementText = null; private int fCachedEventType = -1; private String fCachedVersion = null; private String fCachedEncoding = null; private boolean fCachedStandalone = false; private Location fCachedLocation = null; private String fCachedTextValue = null; private String fCachedPITarget = null; private String fCachedPIData = null; private String fCachedCharEncoding = null; private static boolean DEBUG = false; /** Creates a new instance of XMLStreamFilterImpl */ public XMLStreamFilterImpl(XMLStreamReader reader,StreamFilter filter){ this.fStreamReader = reader; this.fStreamFilter = filter; fCachedAttributes = new ArrayList(); fCachedNamespaceAttr = new ArrayList(); try{ if(!fStreamFilter.accept(fStreamReader)){ next(); cache(); } }catch(XMLStreamException xs){ System.err.println("Error while creating a stream Filter"+xs); } //fCachedEventType = fStreamReader.getEventType(); fCurrentEventType = fStreamReader.getEventType(); if(DEBUG) System.out.println("Cached Event"+fCachedEventType); } /** * * @param sf */ protected void setStreamFilter(StreamFilter sf){ this.fStreamFilter = sf; } /** * * @throws XMLStreamException * @return */ public boolean hasNext() throws XMLStreamException { if(fReadNext){ fReadNext = false; cache(); if(DEBUG) System.out.println("Cached Event in hasNext"+fCachedEventType); return readNext(); } return fHasMoreEvents; } /** * * @throws XMLStreamException */ public void close() throws XMLStreamException { this.fStreamReader.close(); } /** * * @return */ public int getAttributeCount() { if(!fReadFromCache){ return this.fStreamReader.getAttributeCount(); }else{ return fCachedAttributes.size(); } } /** * * @param index * @return */ public QName getAttributeName(int index) { if(!fReadFromCache){ return this.fStreamReader.getAttributeName(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null) return attr.getName(); } return null; } /** * * @param index * @return */ public String getAttributeNamespace(int index) { if(!fReadFromCache){ return fStreamReader.getAttributeNamespace(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null) return attr.getName().getNamespaceURI(); } return null; } /** * * @param index * @return */ public String getAttributePrefix(int index) { if(!fReadFromCache){ return fStreamReader.getAttributePrefix(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null) return attr.getName().getPrefix(); } return null; } /** * * @param index * @return */ public String getAttributeType(int index) { if(!fReadFromCache){ return fStreamReader.getAttributeType(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null) return attr.getDTDType(); } return null; } /** * * @return * @param index */ public String getAttributeValue(int index) { if(!fReadFromCache){ return fStreamReader.getAttributeValue(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null) return attr.getValue(); } return null; } /** * * @param namespaceURI * @param localName * @return */ public String getAttributeValue(String namespaceURI, String localName) { if(!fReadFromCache){ return fStreamReader.getAttributeValue(namespaceURI,localName); }else{ if( fCachedEventType != XMLEvent.START_ELEMENT || fCachedEventType != XMLEvent.ATTRIBUTE) throw new IllegalStateException("Current event state is " + fCachedEventType ); for(int i=0; i< fCachedAttributes.size();i++){ AttributeImpl attr = (AttributeImpl)fCachedAttributes.get(i); if(attr != null && (attr.getName().getLocalPart().equals(localName)) && (namespaceURI.equals(attr.getName().getNamespaceURI()))) return attr.getValue(); } } return null; } /** * * @return */ public String getCharacterEncodingScheme() { if(!fReadFromCache){ return fStreamReader.getCharacterEncodingScheme(); }else{ return fCachedCharEncoding; } } /** * * @throws XMLStreamException * @return */ public String getElementText() throws XMLStreamException { if(!fReadFromCache){ return fStreamReader.getElementText(); }else{ if(fCachedEventType != XMLEvent.START_ELEMENT) { throw new XMLStreamException( "parser must be on START_ELEMENT to read next text", getLocation()); } return fCachedElementText; } } /** * * @return */ public String getEncoding() { if(!fReadFromCache){ return this.fStreamReader.getEncoding(); }else{ return fCachedEncoding; } } /** * * @return */ public int getEventType() { if(!fReadFromCache){ return fStreamReader.getEventType(); }else { return fCachedEventType; } } /** * * @return */ public String getLocalName() { if(!fReadFromCache){ return fStreamReader.getLocalName(); }else{ return fLocalName; } } /** * * @return */ public javax.xml.stream.Location getLocation() { if(!fReadFromCache){ return fStreamReader.getLocation(); }else{ return fCachedLocation; } } /** * * @return */ public javax.xml.namespace.QName getName() { if(!fReadFromCache){ return fStreamReader.getName(); }else{ if(fCachedEventType == XMLEvent.START_ELEMENT || fCachedEventType == XMLEvent.END_ELEMENT){ return fElementName; } else{ throw new java.lang.IllegalArgumentException("Illegal to call getName() "+ "when event type is "+fCachedEventType); } } } /** * * @return */ public javax.xml.namespace.NamespaceContext getNamespaceContext() { if(!fReadFromCache){ return fStreamReader.getNamespaceContext(); }else{ return fCachedNamespaceContext; } } /** * * @return */ public int getNamespaceCount() { if(!fReadFromCache){ return fStreamReader.getNamespaceCount(); }else{ if(fCachedEventType == XMLEvent.START_ELEMENT || fCachedEventType == XMLEvent.END_ELEMENT || fCachedEventType == XMLEvent.NAMESPACE){ return fCachedNamespaceAttr.size(); }else{ throw new IllegalStateException("Current event state is " + fCachedEventType ); } } } /** * * @param index * @return */ public String getNamespacePrefix(int index) { if(!fReadFromCache){ return fStreamReader.getNamespacePrefix(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null){ return attr.getName().getPrefix(); } } return null; } /** * * @return */ public String getNamespaceURI() { if(!fReadFromCache){ return fStreamReader.getNamespaceURI(); }else{ if((fCachedEventType == XMLEvent.START_ELEMENT || fCachedEventType == XMLEvent.END_ELEMENT) && (fElementName != null)) { return fElementName.getNamespaceURI(); } } return null; } /** * * @param index * @return */ public String getNamespaceURI(int index) { if(!fReadFromCache){ return this.fStreamReader.getNamespaceURI(index); }else{ AttributeImpl attr = getCachedAttribute(index); if(attr != null){ return attr.getName().getNamespaceURI(); } } return null; } /** * * @param prefix * @return */ public String getNamespaceURI(String prefix) { if(!fReadFromCache){ return this.fStreamReader.getNamespaceURI(); }else{ return fCachedNamespaceContext.getNamespaceURI(prefix); } } /** * * @return */ public String getPIData() { if(!fReadFromCache){ return this.fStreamReader.getPIData(); }else{ return fCachedPIData; } } /** * * @return */ public String getPITarget() { if(!fReadFromCache){ return this.fStreamReader.getPITarget(); }else{ return fCachedPITarget; } } /** * * @return */ public String getPrefix() { if(!fReadFromCache){ return this.fStreamReader.getPrefix(); }else{ if(fCachedEventType == XMLEvent.START_ELEMENT || fCachedEventType == XMLEvent.END_ELEMENT){ return fElementName.getPrefix(); } } return null; } /** * * @param name * @throws IllegalArgumentException * @return */ public Object getProperty(java.lang.String name) throws java.lang.IllegalArgumentException { return this.fStreamReader.getProperty(name); } /** * * @return */ public String getText() {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?