1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 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 Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.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: HttpSwordInstallerFactory.java 2099 2011-03-07 17:13:00Z dmsmith $
21   */
22  package org.crosswire.jsword.book.install.sword;
23  
24  import java.util.regex.Pattern;
25  
26  import org.crosswire.jsword.JSOtherMsg;
27  import org.crosswire.jsword.book.install.Installer;
28  import org.crosswire.jsword.book.install.InstallerFactory;
29  
30  /**
31   * A Factory for instances of HttpSwordInstaller.
32   * 
33   * @see gnu.lgpl.License for license details.<br>
34   *      The copyright to this program is held by it's authors.
35   * @author Mark Goodwin [goodwinster at gmail dot com]
36   * @author Joe Walker [joe at eireneh dot com]
37   * @author DM Smith [dmsmith555 at yahoo dot com]
38   */
39  public class HttpSwordInstallerFactory implements InstallerFactory {
40      /*
41       * (non-Javadoc)
42       * 
43       * @see org.crosswire.jsword.book.install.InstallerFactory#createInstaller()
44       */
45      public Installer createInstaller() {
46          return new HttpSwordInstaller();
47      }
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see
53       * org.crosswire.jsword.book.install.InstallerFactory#createInstaller(java
54       * .lang.String)
55       */
56      public Installer createInstaller(String installerDefinition) {
57          String[] parts = commaPattern.split(installerDefinition, 6);
58          switch (parts.length) {
59          case 4:
60              return createOldInstaller(parts);
61          case 6:
62              return createInstaller(parts);
63          default:
64              throw new IllegalArgumentException(JSOtherMsg.lookupText("Not enough / symbols in url: {0}", installerDefinition));
65          }
66  
67      }
68  
69      private Installer createInstaller(String[] parts) {
70          AbstractSwordInstaller reply = new HttpSwordInstaller();
71  
72          reply.setHost(parts[0]);
73          reply.setPackageDirectory(parts[1]);
74          reply.setCatalogDirectory(parts[2]);
75          if (parts[3].length() > 0) {
76              reply.setProxyHost(parts[3]);
77              if (parts[4].length() > 0) {
78                  reply.setProxyPort(Integer.valueOf(parts[4]));
79              }
80          }
81  
82          return reply;
83      }
84  
85      private Installer createOldInstaller(String[] parts) {
86          AbstractSwordInstaller reply = new HttpSwordInstaller();
87  
88          reply.setHost(parts[0]);
89          reply.setPackageDirectory(parts[1] + '/' + PACKAGE_DIR);
90          reply.setCatalogDirectory(parts[1] + '/' + LIST_DIR);
91          if (parts[2].length() > 0) {
92              reply.setProxyHost(parts[2]);
93              if (parts[3].length() > 0) {
94                  reply.setProxyPort(Integer.valueOf(parts[3]));
95              }
96          }
97  
98          return reply;
99      }
100 
101     /**
102      * The relative path of the dir holding the zip files
103      */
104     protected static final String PACKAGE_DIR = "packages/rawzip";
105 
106     /**
107      * The relative path of the dir holding the index file
108      */
109     private static final String LIST_DIR = "raw";
110 
111     private Pattern commaPattern = Pattern.compile(",");
112 }
113