📄 saxmodifier.java
字号:
/*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*
* This software is open source.
* See the bottom of this file for the licence.
*/
package org.dom4j.io;
import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* The SAXModifier reads, modifies and writes XML documents using SAX.
*
* <p>
* Registered {@link ElementModifier}objects can provide modifications to (part
* of) the xml tree, while the document is still being processed. This makes it
* possible to change large xml documents without having them in memory.
* </p>
*
* <p>
* The modified document is written when the {@link XMLWriter}is specified.
* </p>
*
* @author Wonne Keysers (Realsoftware.be)
*
* @see org.dom4j.io.SAXReader
* @see org.dom4j.io.XMLWriter
*/
public class SAXModifier {
private XMLWriter xmlWriter;
private XMLReader xmlReader;
private boolean pruneElements;
private SAXModifyReader modifyReader;
private HashMap modifiers = new HashMap();
/**
* Creates a new modifier. <br>
* The XMLReader to parse the source will be created via the
* org.xml.sax.driver system property or JAXP if the system property is not
* set.
*/
public SAXModifier() {
}
/**
* Creates a new modifier. <br>
* The XMLReader to parse the source will be created via the
* org.xml.sax.driver system property or JAXP if the system property is not
* set.
*
* @param pruneElements
* Set to true when the modified document must NOT be kept in
* memory.
*/
public SAXModifier(boolean pruneElements) {
this.pruneElements = pruneElements;
}
/**
* Creates a new modifier that will the specified {@link
* org.xml.sax.XMLReader} to parse the source.
*
* @param xmlReader
* The XMLReader to use
*/
public SAXModifier(XMLReader xmlReader) {
this.xmlReader = xmlReader;
}
/**
* Creates a new modifier that will the specified {@link
* org.xml.sax.XMLReader} to parse the source.
*
* @param xmlReader
* The XMLReader to use
* @param pruneElements
* Set to true when the modified document must NOT be kept in
* memory.
*/
public SAXModifier(XMLReader xmlReader, boolean pruneElements) {
this.xmlReader = xmlReader;
}
/**
* Reads a Document from the given {@link java.io.File}and writes it to the
* specified {@link XMLWriter}using SAX. Registered {@linkElementModifier}
* objects are invoked on the fly.
*
* @param source
* is the <code>File</code> to read from.
*
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(File source) throws DocumentException {
try {
return installModifyReader().read(source);
} catch (SAXModifyException ex) {
Throwable cause = ex.getCause();
throw new DocumentException(cause.getMessage(), cause);
}
}
/**
* Reads a Document from the given {@link org.xml.sax.InputSource}and
* writes it to the specified {@link XMLWriter}using SAX. Registered
* {@link ElementModifier}objects are invoked on the fly.
*
* @param source
* is the <code>org.xml.sax.InputSource</code> to read from.
*
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(InputSource source) throws DocumentException {
try {
return installModifyReader().read(source);
} catch (SAXModifyException ex) {
Throwable cause = ex.getCause();
throw new DocumentException(cause.getMessage(), cause);
}
}
/**
* Reads a Document from the given {@link java.io.InputStream}and writes it
* to the specified {@link XMLWriter}using SAX. Registered {@link
* ElementModifier} objects are invoked on the fly.
*
* @param source
* is the <code>java.io.InputStream</code> to read from.
*
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(InputStream source) throws DocumentException {
try {
return installModifyReader().read(source);
} catch (SAXModifyException ex) {
Throwable cause = ex.getCause();
throw new DocumentException(cause.getMessage(), cause);
}
}
/**
* Reads a Document from the given {@link java.io.InputStream}and writes it
* to the specified {@link XMLWriter}using SAX. Registered {@link
* ElementModifier} objects are invoked on the fly.
*
* @param source
* is the <code>java.io.InputStream</code> to read from.
* @param systemId
* DOCUMENT ME!
*
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(InputStream source, String systemId)
throws DocumentException {
try {
return installModifyReader().read(source);
} catch (SAXModifyException ex) {
Throwable cause = ex.getCause();
throw new DocumentException(cause.getMessage(), cause);
}
}
/**
* Reads a Document from the given {@link java.io.Reader}and writes it to
* the specified {@link XMLWriter}using SAX. Registered {@link
* ElementModifier} objects are invoked on the fly.
*
* @param source
* is the <code>java.io.Reader</code> to read from.
*
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(Reader source) throws DocumentException {
try {
return installModifyReader().read(source);
} catch (SAXModifyException ex) {
Throwable cause = ex.getCause();
throw new DocumentException(cause.getMessage(), cause);
}
}
/**
* Reads a Document from the given {@link java.io.Reader}and writes it to
* the specified {@link XMLWriter}using SAX. Registered {@link
* ElementModifier} objects are invoked on the fly.
*
* @param source
* is the <code>java.io.Reader</code> to read from.
* @param systemId
* DOCUMENT ME!
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -