saxmodifier.java
来自「解决如何把XML应用到JAVA里问题」· Java 代码 · 共 460 行 · 第 1/2 页
JAVA
460 行
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(Reader 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.net.URL}and writes it to the
* specified {@link XMLWriter}using SAX. Registered {@linkElementModifier}
* objects are invoked on the fly.
*
* @param source
* is the <code>java.net.URL</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(URL 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 URL or filename and writes it to the
* specified {@link XMLWriter}using SAX. Registered {@linkElementModifier}
* objects are invoked on the fly.
*
* @param source
* is the URL or filename to read from.
*
* @return the newly created Document instance
*
* @throws DocumentException
* DocumentException org.dom4j.DocumentException} if an error
* occurs during parsing.
*/
public Document modify(String source) throws DocumentException {
try {
return installModifyReader().read(source);
} catch (SAXModifyException ex) {
Throwable cause = ex.getCause();
throw new DocumentException(cause.getMessage(), cause);
}
}
/**
* Adds the {@link ElementModifier}to be called when the specified element
* path is encounted while parsing the source.
*
* @param path
* The element path to be handled
* @param modifier
* The {@link ElementModifier}to be called by the event based
* processor.
*/
public void addModifier(String path, ElementModifier modifier) {
this.modifiers.put(path, modifier);
}
/**
* Removes all registered {@link ElementModifier}instances from the event
* based processor.
*/
public void resetModifiers() {
this.modifiers.clear();
getSAXModifyReader().resetHandlers();
}
/**
* Removes the {@link ElementModifier}from the event based processor, for
* the specified element path.
*
* @param path
* The path to remove the {@link ElementModifier}for.
*/
public void removeModifier(String path) {
this.modifiers.remove(path);
getSAXModifyReader().removeHandler(path);
}
/**
* Get the {@link org.dom4j.DocumentFactory}used to create the DOM4J
* document structure
*
* @return <code>DocumentFactory</code> that will be used
*/
public DocumentFactory getDocumentFactory() {
return getSAXModifyReader().getDocumentFactory();
}
/**
* Sets the {@link org.dom4j.DocumentFactory}used to create the DOM4J
* document tree.
*
* @param factory
* <code>DocumentFactory</code> to be used
*/
public void setDocumentFactory(DocumentFactory factory) {
getSAXModifyReader().setDocumentFactory(factory);
}
/**
* Returns the current {@link XMLWriter}.
*
* @return XMLWriter
*/
public XMLWriter getXMLWriter() {
return this.xmlWriter;
}
/**
* Sets the {@link XMLWriter}used to write the modified document.
*
* @param writer
* The writer to use.
*/
public void setXMLWriter(XMLWriter writer) {
this.xmlWriter = writer;
}
/**
* Returns true when xml elements are not kept in memory while parsing. The
* {@link org.dom4j.Document}returned by the modify methods will be null.
*
* @return Returns the pruneElements.
*/
public boolean isPruneElements() {
return pruneElements;
}
private SAXReader installModifyReader() throws DocumentException {
try {
SAXModifyReader reader = getSAXModifyReader();
if (isPruneElements()) {
modifyReader.setDispatchHandler(new PruningDispatchHandler());
}
reader.resetHandlers();
Iterator modifierIt = this.modifiers.entrySet().iterator();
while (modifierIt.hasNext()) {
Map.Entry entry = (Map.Entry) modifierIt.next();
SAXModifyElementHandler handler = new SAXModifyElementHandler(
(ElementModifier) entry.getValue());
reader.addHandler((String) entry.getKey(), handler);
}
reader.setXMLWriter(getXMLWriter());
reader.setXMLReader(getXMLReader());
return reader;
} catch (SAXException ex) {
throw new DocumentException(ex.getMessage(), ex);
}
}
private XMLReader getXMLReader() throws SAXException {
if (this.xmlReader == null) {
xmlReader = SAXHelper.createXMLReader(false);
}
return this.xmlReader;
}
private SAXModifyReader getSAXModifyReader() {
if (modifyReader == null) {
modifyReader = new SAXModifyReader();
}
return modifyReader;
}
}
/*
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain copyright statements and
* notices. Redistributions must also contain a copy of this document.
*
* 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 name "DOM4J" must not be used to endorse or promote products derived
* from this Software without prior written permission of MetaStuff, Ltd. For
* written permission, please contact dom4j-info@metastuff.com.
*
* 4. Products derived from this Software may not be called "DOM4J" nor may
* "DOM4J" appear in their names without prior written permission of MetaStuff,
* Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
*
* 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
*
* THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``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 METASTUFF, LTD. 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.
*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?