📄 saxchunkconsumer.java
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: SAXChunkConsumer.java,v 1.4 2003/04/11 13:38:56 per_nyfelt Exp $
package org.ozoneDB.xml.util;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.AttributesImpl;
import org.ozoneDB.util.LogWriter;
import org.ozoneDB.util.SimpleLogWriter;
import java.io.IOException;
import java.io.Serializable;
/**
* Objects of this class can be used to convert a sequence of
* byte[] back into a DOM tree or SAX events.
* WARNING: Don't reuse instances of this class. Create a new instance for each
* chunk set (chunks containing information about the same XML tree).
* @version $Revision: 1.4 $ $Date: 2003/04/11 13:38:56 $
* @author <a href="http://www.smb-tec.com">SMB</a>
*/
public final class SAXChunkConsumer implements ContentHandler, LexicalHandler, Serializable {
private final Node appendTo;
protected Node startNode = null;
protected Node currentNode = null;
protected final Document domFactory;
protected final boolean domLevel2;
protected final ContentHandler contentHandler;
protected final LexicalHandler lexicalHandler;
protected final ChunkInputStream chunkInput;
protected final CompiledXMLInputStream cxmlInput;
protected int processLevel;
protected ModifiableNodeList resultNodeList;
private LogWriter logWriter;
private void init() {
// we are on the client side so we cant use the Env logger
logWriter = SimpleLogWriter.getInstance().setDebugLevel(LogWriter.DEBUG);
}
/**
* Use this chunk consumer to produce a DOM tree out of the consumed
* chunks.
* @param domFactory
* @param appendTo The node where the newly created DOM tree will be appended to
* @throws IllegalArgumentException if the given node was null
*/
public SAXChunkConsumer(Document domFactory, Node appendTo) throws IOException{
init();
if (domFactory == null) {
throw new IllegalArgumentException("provided DOM factory node was null!");
}
this.contentHandler = this;
this.lexicalHandler = this;
this.appendTo = appendTo;
this.processLevel = 0;
this.resultNodeList = new ModifiableNodeList();
this.domFactory = domFactory;
// this.domLevel2 = domFactory.getImplementation().hasFeature("XML", "2.0");
this.domLevel2 = false;
this.chunkInput = new ChunkInputStream(null);
this.cxmlInput = new CompiledXMLInputStream(this.chunkInput);
}
/**
* Use this chunk consumer to produce SAX events out of the consumed chunks.
* @param contentHandler the handler the SAX events will be send to
* @throws IllegalArgumentException if the given content handler was null
*/
public SAXChunkConsumer(ContentHandler contentHandler) throws IOException{
init();
if (contentHandler == null) {
throw new IllegalArgumentException("provided SAX content handler was null");
}
this.contentHandler = contentHandler;
this.lexicalHandler = (contentHandler instanceof LexicalHandler)
? (LexicalHandler)contentHandler
: null;
this.appendTo = null;
this.chunkInput = new ChunkInputStream(null);
this.cxmlInput = new CompiledXMLInputStream(this.chunkInput);
this.domLevel2 = false;
this.domFactory = null;
}
/**
* Takes a chunk and produces corresponding SAX events
* @param chunkData the chunk containing definitions of events to be thrown
* @throws IllegalArgumentException if the given chunk was null
*/
public final void processChunk(byte[] chunkData) throws SAXException, IOException {
if (chunkData == null) {
throw new IllegalArgumentException("provided event chunk was null");
}
this.chunkInput.setBuffer(chunkData);
while (this.chunkInput.available() > 0) {
switch (cxmlInput.readEvent()) {
case CXMLContentHandler.START_DOCUMENT:
if (logWriter.hasTarget( LogWriter.DEBUG3 )) {
logWriter.newEntry(this, "startDocument()", LogWriter.DEBUG3);
}
this.contentHandler.startDocument();
break;
case CXMLContentHandler.END_DOCUMENT:
this.contentHandler.endDocument();
return;
case CXMLContentHandler.START_PREFIX_MAPPING:
this.contentHandler.startPrefixMapping(cxmlInput.readString(), cxmlInput.readString());
break;
case CXMLContentHandler.END_PREFIX_MAPPING:
this.contentHandler.endPrefixMapping(cxmlInput.readString());
break;
case CXMLContentHandler.START_ELEMENT:
if (logWriter.hasTarget( LogWriter.DEBUG3 )) {
logWriter.newEntry(this, "startElement()", LogWriter.DEBUG3);
}
int attributes = cxmlInput.readAttributes();
AttributesImpl atts = new AttributesImpl();
for (int i = 0; i < attributes; i++) {
// atts.addAttribute(cxmlInput.readString(), cxmlInput.readString(), cxmlInput.readString(),
// cxmlInput.readString(), cxmlInput.readString());
atts.addAttribute(cxmlInput.readString(), cxmlInput.readString(), cxmlInput.readString(),
cxmlInput.readString(), new String( cxmlInput.readChars() ) );
}
String namespace = cxmlInput.readString();
String localname = cxmlInput.readString();
String qname = cxmlInput.readString();
logWriter.newEntry(this, "namespace=" + namespace + ", localname=" + localname + ", qname=" + qname, LogWriter.DEBUG1);
this.contentHandler.startElement( namespace, localname, qname , atts);
break;
case CXMLContentHandler.END_ELEMENT:
this.contentHandler.endElement(cxmlInput.readString(), cxmlInput.readString(), cxmlInput.readString());
break;
case CXMLContentHandler.CHARACTERS:
char[] chars = cxmlInput.readChars();
this.contentHandler.characters(chars, 0, chars.length);
break;
case CXMLContentHandler.IGNORABLE_WHITESPACE:
char[] spaces = cxmlInput.readChars();
this.contentHandler.characters(spaces, 0, spaces.length);
break;
case CXMLContentHandler.PROCESSING_INSTRUCTION:
this.contentHandler.processingInstruction(cxmlInput.readString(), cxmlInput.readString());
break;
case CXMLContentHandler.COMMENT:
if (this.lexicalHandler != null) {
char[] comment = cxmlInput.readChars();
this.lexicalHandler.comment(comment, 0, comment.length);
}
break;
case CXMLContentHandler.START_CDATA:
if (this.lexicalHandler != null) {
this.lexicalHandler.startCDATA();
}
break;
case CXMLContentHandler.END_CDATA:
if (this.lexicalHandler != null) {
this.lexicalHandler.endCDATA();
}
break;
default:
throw new IOException("parsing error: event not supported: ");
}
}
}
/**
* @return the last created result node, if this consumer was initialized
* for DOM creation.
*/
public final Node getResultNode() {
return startNode;
}
/**
* @return the result node list, if this consumer was initialized for
* DOM creation.
*/
public final NodeList getResultNodeList() {
return resultNodeList;
}
//
// SAX content handler implemenation
//
/**
* Received notification of the beginning of the document.
*/
public final void startDocument() {
if (this.startNode == null) {
this.startNode = (this.appendTo != null) ? this.appendTo : this.domFactory;
}
if (logWriter.hasTarget( LogWriter.DEBUG3 )) {
logWriter.newEntry(this, "startDocument()", LogWriter.DEBUG3);
}
if (this.startNode.getNodeType() != Node.DOCUMENT_NODE) {
throw new IllegalStateException("A document can't be appended to another node!");
}
if (this.startNode.hasChildNodes()) {
throw new RuntimeException("The given DOM document must not have children if a whole document shall be converted!");
}
if (this.processLevel != 0) {
throw new RuntimeException("startDocument event must not occur within other start-end-event pairs!");
}
this.resultNodeList.addNode(this.startNode);
this.currentNode = this.startNode;
this.processLevel++;
}
/**
* Received notification of the end of the document.
*/
public final void endDocument() {
if (logWriter.hasTarget( LogWriter.DEBUG3 )) {
logWriter.newEntry(this, "endDocument()", LogWriter.DEBUG3);
}
this.currentNode = this.currentNode.getParentNode();
this.processLevel--;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -