001 // This file is part of AceWiki. 002 // Copyright 2008-2012, AceWiki developers. 003 // 004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU 005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of 006 // the License, or (at your option) any later version. 007 // 008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 010 // Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If 013 // not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.base; 016 017 import java.util.*; 018 019 import ch.uzh.ifi.attempto.ape.ACEParser; 020 import ch.uzh.ifi.attempto.ape.APESocket; 021 import ch.uzh.ifi.attempto.ape.APELocal; 022 import ch.uzh.ifi.attempto.ape.APEWebservice; 023 024 /** 025 * This class is used to get an instance of ACEParser by parameters. 026 * 027 * Following parameters are recognized: 028 * - apetype 029 * Specify the type of APE, should be one of "local", "socket" and "webservice". 030 * - apecommand 031 * Location of ape.exe, used by APELocal when apetype is "local". 032 * - apehost 033 * Host name of APE socket server, used by APESocket when apetype is "socket". 034 * - apeport 035 * Port number of APE socket server, used by APESocket whe apetype is "socket". 036 * - apewebservice 037 * APE webservice URL, use by APEWebservice when apetype is "webservice". 038 * 039 * @author Yu Changyuan 040 */ 041 042 public class APE { 043 044 private static Map<String, String> parameters = new HashMap<String, String>(); 045 046 /** 047 * Get a ACEParser instance by parameters. 048 * 049 * @param parameters The parameters set in servlet web.xml. 050 * @return The ACEParser instance. 051 */ 052 public static ACEParser getParser(Map<String, String> parameters) { 053 String apeType = parameters.get("context:apetype"); 054 if (apeType == null) apeType = "local"; 055 056 if (apeType.equals("socket")) { 057 String host = parameters.get("context:apehost"); 058 if (host == null) host = "127.0.0.1"; 059 int port; 060 try { 061 port = Integer.parseInt(parameters.get("context:apeport")); 062 } 063 catch (NumberFormatException e) { 064 port = 0xace; 065 } 066 067 return getAPESocket(host, port); 068 } 069 070 if (apeType.equals("webservice")) { 071 String url = parameters.get("context:apewebservice"); 072 if (url == null) url = "http://127.0.0.1:8000/"; 073 074 return getAPEWebservice(url); 075 } 076 077 String apeCommand = parameters.get("context:apecommand"); 078 if (apeCommand == null) apeCommand = "ape.exe"; 079 080 return getAPELocal(apeCommand); 081 } 082 083 /** 084 * Get a ACEParser instance with default parameters. 085 * 086 * @return The ACEParser instance. 087 */ 088 public static ACEParser getParser() { 089 return getParser(parameters); 090 } 091 092 /** 093 * Set default parameters. 094 * 095 * @param parameters The parameters to set. 096 */ 097 public static void setParameters(Map<String, String> parameters) { 098 APE.parameters = parameters; 099 } 100 101 /** 102 * Get a APELocal instance with specified ape.exe location 103 * 104 * @param apeCommand The location of ape.exe. 105 * @return The APELocal instance. 106 */ 107 public static ACEParser getAPELocal(String apeCommand) { 108 if (!APELocal.isInitialized()) { 109 APELocal.init(apeCommand); 110 } 111 return APELocal.getInstance(); 112 } 113 114 /** 115 * Get a APESocket instance with specified host and port 116 * 117 * @param host The host of APE socket server. 118 * @param port The port of APE socket server. 119 * @return The APESocket instance. 120 */ 121 public static ACEParser getAPESocket(String host, int port) { 122 return new APESocket(host, port); 123 } 124 125 /** 126 * Get a APEWebservice instance with specified host and port 127 * 128 * @param url The URL of APE webservice. 129 * @return The APEWebservice instance. 130 */ 131 public static ACEParser getAPEWebservice(String url) { 132 return new APEWebservice(url); 133 } 134 135 }