001    /*
002     * Copyright (c) 2005 Marko Karppinen & Co. LLC
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package fi.karppinen.xml;
024    
025    import java.io.IOException;
026    import java.io.InputStream;
027    import java.io.OutputStream;
028    import java.io.OutputStreamWriter;
029    import java.io.Reader;
030    import java.io.Writer;
031    
032    import org.mortbay.util.IO;
033    import org.xml.sax.InputSource;
034    
035    /**
036     * Makes it possible to feed Markdown source to markdown.pl and then to TagSoup.
037     * The system property <code>fi.karppinen.xml.markdownexecutable</code> should 
038     * contain the absolute path to markdown.pl.
039     * 
040     * @version $Id: MarkdownTagSoupInputSource.java,v 1.1 2006/10/30 19:57:09 hsivonen Exp $
041     * @author hsivonen
042     */
043    public class MarkdownTagSoupInputSource extends InputSource {
044    
045        private static final String[] ARGS = { "--html4tags" };
046        
047        private Process markdown;
048        
049        private void startProcess() throws IOException {
050            markdown = Runtime.getRuntime().exec(System.getProperty("fi.karppinen.xml.markdownexecutable", "Markdown"));
051        }
052        
053        /**
054         * The string constructor.
055         * 
056         * @param content a string containing Markdown source
057         * @throws IOException if something goes wrong
058         */
059        public MarkdownTagSoupInputSource(String content) throws IOException {
060            super();
061            initWithString(content);
062        }
063    
064        /**
065         * @param content
066         * @throws IOException
067         */
068        private void initWithString(String content) throws IOException {
069            startProcess();
070            Writer out = new OutputStreamWriter(streamToMarkdown(), "UTF-8");
071            out.write(content);
072            out.close();
073        }
074    
075        /**
076         * The reader constructor.
077         * 
078         * @param content a reader reading Markdown source
079         * @throws IOException if something goes wrong
080         */
081        public MarkdownTagSoupInputSource(Reader content) throws IOException {
082            super();
083            initWithReader(content);
084        }
085        
086        /**
087         * @param content
088         * @throws IOException
089         */
090        private void initWithReader(Reader content) throws IOException {
091            startProcess();
092            Writer out = new OutputStreamWriter(streamToMarkdown(), "UTF-8");
093            IO.copy(content, out);
094            out.close();
095        }
096    
097        private OutputStream streamToMarkdown() {
098            return markdown.getOutputStream();
099        }
100        /**
101         * @see org.xml.sax.InputSource#getByteStream()
102         */
103        public InputStream getByteStream() {
104            return markdown.getInputStream();
105        }
106        /**
107         * @see org.xml.sax.InputSource#getCharacterStream()
108         */
109        public Reader getCharacterStream() {
110            return null;
111        }
112        /**
113         * @see org.xml.sax.InputSource#getEncoding()
114         */
115        public String getEncoding() {
116            return "UTF-8";
117        }
118        /**
119         * @see org.xml.sax.InputSource#setByteStream(java.io.InputStream)
120         */
121        public void setByteStream(InputStream arg0) {
122            throw new RuntimeException("Not supported.");
123        }
124        /**
125         * @see org.xml.sax.InputSource#setCharacterStream(java.io.Reader)
126         */
127        public void setCharacterStream(Reader arg0) {
128            throw new RuntimeException("Not supported.");
129        }
130        /**
131         * @see org.xml.sax.InputSource#setEncoding(java.lang.String)
132         */
133        public void setEncoding(String arg0) {
134            throw new RuntimeException("Not supported.");
135        }
136    }