001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (C) 2012  Paul Ferraro
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Lesser General Public License as published by
007 * the Free Software Foundation, either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Lesser General Public License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018package net.sf.hajdbc.xml;
019
020import java.io.File;
021import java.net.URISyntaxException;
022import java.net.URL;
023
024import javax.xml.transform.Result;
025import javax.xml.transform.Source;
026import javax.xml.transform.stream.StreamResult;
027import javax.xml.transform.stream.StreamSource;
028
029/**
030 * Alternative to {@link URLXMLStreamFactory} and the file:// protocol, since default JDK file url stream handler
031 * does not support output.
032 * 
033 * @author Paul Ferraro
034 */
035public class FileXMLStreamFactory implements XMLStreamFactory
036{
037        private static final long serialVersionUID = -8857228563490452629L;
038        
039        private final File file;
040        
041        public FileXMLStreamFactory(File file)
042        {
043                this.file = file;
044        }
045        
046        public FileXMLStreamFactory(URL url)
047        {
048                try
049                {
050                        this.file = new File(url.toURI());
051                }
052                catch (URISyntaxException e)
053                {
054                        throw new IllegalArgumentException(e);
055                }
056        }
057        
058        /**
059         * {@inheritDoc}
060         * @see net.sf.hajdbc.xml.XMLStreamFactory#createSource()
061         */
062        @Override
063        public Source createSource()
064        {
065                return new StreamSource(this.file);
066        }
067
068        /**
069         * {@inheritDoc}
070         * @see net.sf.hajdbc.xml.XMLStreamFactory#createResult()
071         */
072        @Override
073        public Result createResult()
074        {
075                return new StreamResult(this.file);
076        }
077
078        /**
079         * {@inheritDoc}
080         * @see java.lang.Object#toString()
081         */
082        @Override
083        public String toString()
084        {
085                return this.file.getPath();
086        }
087}