1   /**
2    * Distribution License:
3    * BibleDesktop is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU General Public License, version 2 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/gpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: ConfigurableSwingConverter.java 1997 2010-10-23 20:53:47Z dmsmith $
21   */
22  package org.crosswire.bibledesktop.util;
23  
24  import java.awt.Font;
25  import java.io.IOException;
26  import java.net.URL;
27  import java.util.MissingResourceException;
28  
29  import javax.xml.transform.TransformerException;
30  
31  import org.crosswire.bibledesktop.desktop.XSLTProperty;
32  import org.crosswire.common.swing.GuiConvert;
33  import org.crosswire.common.util.FileUtil;
34  import org.crosswire.common.util.NetUtil;
35  import org.crosswire.common.util.ResourceUtil;
36  import org.crosswire.common.util.URIFilter;
37  import org.crosswire.common.xml.Converter;
38  import org.crosswire.common.xml.SAXEventProvider;
39  import org.crosswire.common.xml.TransformingSAXEventProvider;
40  
41  /**
42   * Turn XML from a Bible into HTML according to a Display style.
43   * 
44   * @see gnu.gpl.License for license details.<br>
45   *      The copyright to this program is held by it's authors.
46   * @author Joe Walker [joe at eireneh dot com]
47   */
48  public class ConfigurableSwingConverter implements Converter {
49      /**
50       * Get an array of the available style names for a given subject. Different
51       * subjects are available for different contexts. For example - for
52       * insertion into a web page we might want to use a set that had complex
53       * HTML, or IE/NS specific HTML, where as a JFC HTMLDocument needs simpler
54       * HTML - and special tags like the starting &lt;HTML> tags.
55       * <p>
56       * If the protocol of the URL of the current directory is not file then we
57       * can't use File.list to get the contents of the directory. This will
58       * happen if this is being run as an applet. When we start doing that then
59       * we will need to think up something smarter here. Until then we just
60       * return a zero length array.
61       * 
62       * @return An array of available style names
63       */
64      public String[] getStyles() {
65          try {
66              String search = "xsl/cswing/" + NetUtil.INDEX_FILE;
67              URL index = ResourceUtil.getResource(search);
68              return NetUtil.listByIndexFile(NetUtil.toURI(index), new XSLTFilter());
69          } catch (IOException ex) {
70              return new String[0];
71          }
72      }
73  
74      /* (non-Javadoc)
75       * @see org.crosswire.common.xml.Converter#convert(org.crosswire.common.xml.SAXEventProvider)
76       */
77      public SAXEventProvider convert(SAXEventProvider xmlsep) throws TransformerException {
78          try {
79              String path = "xsl/cswing/" + style;
80              URL xslurl = ResourceUtil.getResource(path);
81  
82              TransformingSAXEventProvider tsep = new TransformingSAXEventProvider(NetUtil.toURI(xslurl), xmlsep);
83              // We used to do:
84              // tsep.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
85              // however for various reasons, now we don't but nothing seems to be
86              // broken ...
87              return tsep;
88          } catch (MissingResourceException ex) {
89              throw new TransformerException(ex);
90          }
91      }
92  
93      /**
94       * Accessor for the stylesheet we are transforming using
95       */
96      public static String getFont() {
97          return font;
98      }
99  
100     /**
101      * Accessor for the stylesheet we are transforming using
102      */
103     public static Font toFont() {
104         return GuiConvert.string2Font(font);
105     }
106 
107     /**
108      * Accessor for the stylesheet we are transforming using
109      */
110     public static void setFont(String font) {
111         ConfigurableSwingConverter.font = font;
112         XSLTProperty.FONT.setState(font);
113     }
114 
115     /**
116      * Accessor for the stylesheet we are transforming using
117      */
118     public static String getResourceName() {
119         return style;
120     }
121 
122     /**
123      * Accessor for the stylesheet we are transforming using
124      */
125     public static void setResourceName(String style) {
126         ConfigurableSwingConverter.style = style;
127     }
128 
129     /**
130      *
131      */
132     static final class XSLTFilter implements URIFilter {
133         /* (non-Javadoc)
134          * @see org.crosswire.common.util.URLFilter#accept(java.lang.String)
135          */
136         public boolean accept(String name) {
137             return name.endsWith(FileUtil.EXTENSION_XSLT);
138         }
139     }
140 
141     /**
142      * The font to be used in OSIS->HTML generation
143      */
144     private static String font = "Serif-PLAIN-14";
145 
146     /**
147      * The stylesheet we are transforming using
148      */
149     private static String style = "simple.xsl";
150 }
151