⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jaxbmodifier.java

📁 解决如何把XML应用到JAVA里问题
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.jaxb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.ElementModifier;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXModifier;
import org.dom4j.io.XMLWriter;

import org.xml.sax.InputSource;

/**
 * Reads an XML document using SAX and writes its content to the provided
 * {@link org.dom4j.io.XMLWriter}. Modifications must be provided by {@link
 * org.dom4j.jaxb.JAXBObjectModifier} objects, which are called prior to writing
 * the XML fragment they are registered for.
 * 
 * @author Wonne Keysers (Realsoftware.be)
 * 
 * @see org.dom4j.io.SAXModifier
 */
public class JAXBModifier extends JAXBSupport {
    private SAXModifier modifier;

    private XMLWriter xmlWriter;

    private boolean pruneElements;

    private OutputFormat outputFormat;

    private HashMap modifiers = new HashMap();

    /**
     * Creates a new JAXBModifier for the given JAXB context path. This is the
     * Java package where JAXB can find the generated XML classes. This package
     * MUST contain jaxb.properties!
     * 
     * @param contextPath
     *            JAXB context path to be used
     * 
     * @see javax.xml.bind.JAXBContext
     */
    public JAXBModifier(String contextPath) {
        super(contextPath);
        this.outputFormat = new OutputFormat();
    }

    /**
     * Creates a new JAXBModifier for the given JAXB context path, using the
     * given {@link java.lang.ClassLoader}. This is the Java package where JAXB
     * can find the generated XML classes. This package MUST contain
     * jaxb.properties!
     * 
     * @param contextPath
     *            JAXB context path to be used
     * @param classloader
     *            the classloader to use
     * 
     * @see javax.xml.bind.JAXBContext
     */
    public JAXBModifier(String contextPath, ClassLoader classloader) {
        super(contextPath, classloader);
        this.outputFormat = new OutputFormat();
    }

    /**
     * Creates a new JAXBModifier for the given JAXB context path. The specified
     * {@link org.dom4j.io.OutputFormat}will be used while writing the XML
     * stream.
     * 
     * @param contextPath
     *            JAXB context path to be used
     * @param outputFormat
     *            the DOM4J {@link org.dom4j.io.OutputFormat}to be used
     * 
     * @see javax.xml.bind.JAXBContext
     */
    public JAXBModifier(String contextPath, OutputFormat outputFormat) {
        super(contextPath);
        this.outputFormat = outputFormat;
    }

    /**
     * Creates a new JAXBModifier for the given JAXB context path, using the
     * specified {@link java.lang.Classloader}. The specified {@link
     * org.dom4j.io.OutputFormat} will be used while writing the XML stream.
     * 
     * @param contextPath
     *            JAXB context path to be used
     * @param classloader
     *            the class loader to be used to load JAXB
     * @param outputFormat
     *            the DOM4J {@link org.dom4j.io.OutputFormat}to be used
     * 
     * @see javax.xml.bind.JAXBContext
     */
    public JAXBModifier(String contextPath, ClassLoader classloader,
            OutputFormat outputFormat) {
        super(contextPath, classloader);
        this.outputFormat = outputFormat;
    }

    /**
     * Parses the specified {@link java.io.File}with SAX
     * 
     * @param source
     *            the file to parse
     * 
     * @return the resulting DOM4J document
     * 
     * @throws DocumentException
     *             when an error occurs while parsing
     * @throws IOException
     *             when an error occurs while writing to the {@link
     *             org.dom4j.io.XMLWriter}
     */
    public Document modify(File source) throws DocumentException, IOException {
        return installModifier().modify(source);
    }

    /**
     * Parses the specified {@link java.io.File}with SAX, using the given
     * {@link java.nio.charset.Charset}.
     * 
     * @param source
     *            the file to parse
     * @param charset
     *            the character set to use
     * 
     * @return the resulting DOM4J document
     * 
     * @throws DocumentException
     *             when an error occurs while parsing
     * @throws IOException
     *             when an error occurs while writing to the {@link
     *             org.dom4j.io.XMLWriter}
     */
    public Document modify(File source, Charset charset)
            throws DocumentException, IOException {
        try {
            Reader reader = new InputStreamReader(new FileInputStream(source),
                    charset);

            return installModifier().modify(reader);
        } catch (JAXBRuntimeException ex) {
            Throwable cause = ex.getCause();
            throw new DocumentException(cause.getMessage(), cause);
        } catch (FileNotFoundException ex) {
            throw new DocumentException(ex.getMessage(), ex);
        }
    }

    /**
     * Parses the specified {@link org.xml.sax.InputSource}with SAX.
     * 
     * @param source
     *            the input source to parse
     * 
     * @return the resulting DOM4J document
     * 
     * @throws DocumentException
     *             when an error occurs while parsing
     * @throws IOException
     *             when an error occurs while writing to the {@link
     *             org.dom4j.io.XMLWriter}
     */
    public Document modify(InputSource source) throws DocumentException,
            IOException {
        try {
            return installModifier().modify(source);
        } catch (JAXBRuntimeException ex) {
            Throwable cause = ex.getCause();
            throw new DocumentException(cause.getMessage(), cause);
        }
    }

    /**
     * Parses the specified {@link java.io.InputStream}with SAX.
     * 
     * @param source
     *            the inputstream to parse
     * 
     * @return the resulting DOM4J document
     * 
     * @throws DocumentException
     *             when an error occurs while parsing
     * @throws IOException
     *             when an error occurs while writing to the {@link
     *             org.dom4j.io.XMLWriter}
     */
    public Document modify(InputStream source) throws DocumentException,
            IOException {
        try {
            return installModifier().modify(source);
        } catch (JAXBRuntimeException ex) {
            Throwable cause = ex.getCause();
            throw new DocumentException(cause.getMessage(), cause);
        }
    }

    /**
     * Parses the specified {@link java.io.InputStream}with SAX.
     * 
     * @param source
     *            the inputstream to parse
     * @param systemId
     *            the URI of the given inputstream
     * 
     * @return the resulting DOM4J document
     * 
     * @throws DocumentException
     *             when an error occurs while parsing
     * @throws IOException
     *             when an error occurs while writing to the {@link
     *             org.dom4j.io.XMLWriter}
     */
    public Document modify(InputStream source, String systemId)
            throws DocumentException, IOException {
        try {
            return installModifier().modify(source);
        } catch (JAXBRuntimeException ex) {
            Throwable cause = ex.getCause();
            throw new DocumentException(cause.getMessage(), cause);
        }
    }

    /**
     * Parses the specified {@link java.io.Reader}with SAX.
     * 
     * @param r
     *            the reader to use for parsing
     * 
     * @return the resulting DOM4J document
     * 
     * @throws DocumentException
     *             when an error occurs while parsing
     * @throws IOException
     *             when an error occurs while writing to the {@link
     *             org.dom4j.io.XMLWriter}
     */
    public Document modify(Reader r) throws DocumentException, IOException {
        try {
            return installModifier().modify(r);
        } catch (JAXBRuntimeException ex) {
            Throwable cause = ex.getCause();
            throw new DocumentException(cause.getMessage(), cause);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -