1 16 19 package servlet; 20 21 import java.net.*; 22 import javax.servlet.*; 23 import javax.servlet.http.*; 24 import java.util.Enumeration ; 25 import java.util.Properties ; 26 27 38 39 public class DefaultApplyXSLTProperties extends ApplyXSLTProperties { 40 41 45 private final String DEFAULT_catalog; 46 47 52 protected transient String localHost = null; 53 54 57 protected static int port =0; 58 59 62 public DefaultApplyXSLTProperties() 63 { 64 super(); 65 DEFAULT_catalog = null; 66 setLocalHost(); 67 } 69 70 75 public DefaultApplyXSLTProperties(ServletConfig config) 76 { 77 super(config); 78 String cat = config.getInitParameter("catalog"); 79 if (cat != null) DEFAULT_catalog = cat; 80 else DEFAULT_catalog = null; 81 setLocalHost(); 82 setSystemProperties(); 83 } 84 85 90 protected void setLocalHost() 91 { 92 try 93 { 94 localHost = InetAddress.getLocalHost().getHostName(); 95 } 96 catch (Exception uhe) 97 { 98 localHost = null; 99 } 100 } 101 102 107 public String getLocalHost() 108 { 109 return localHost; 110 } 111 112 120 public URL toSafeURL(String xURL, HttpServletRequest request) 121 throws MalformedURLException 122 { 123 if (port == 0) 125 port = request.getServerPort(); 126 127 if (xURL == null) 128 return null; 129 130 if (xURL.startsWith("/")) 131 { 132 try 133 { 134 return new URL("http", localHost, port, xURL); 135 } 136 catch (MalformedURLException mue) 137 { 138 throw new MalformedURLException("toSafeURL(): " + xURL + 139 " did not map to local"); 140 } 141 } 142 URL tempURL = null; 143 try 144 { 145 tempURL = new URL(xURL); 146 } 147 catch (MalformedURLException mue) 148 { 149 throw new MalformedURLException("toSafeURL(): " + xURL + 150 " not a valid URL"); 151 } 152 try 153 { 154 return new URL(tempURL.getProtocol(), localHost, 155 port, tempURL.getFile()); 156 } 157 catch (MalformedURLException mue) 158 { 159 throw new MalformedURLException("toSafeURL(): " + xURL + 160 " could not be converted to local host"); 161 } 162 } 163 164 172 public String getXMLurl(HttpServletRequest request) 173 throws MalformedURLException 174 { 175 URL url = toSafeURL(getRequestParmString(request, "URL"),request); 176 if (url == null) 177 return super.getXMLurl(null); 178 return url.toExternalForm(); 179 } 180 181 189 public String getXSLRequestURL(HttpServletRequest request) 190 throws MalformedURLException 191 { 192 URL url = toSafeURL(getRequestParmString(request, "xslURL"),request); 193 if (url == null) 194 return null; 195 return url.toExternalForm(); 196 } 197 198 206 public String getXSLurl(HttpServletRequest request) 207 throws MalformedURLException 208 { 209 String reqURL = getXSLRequestURL(request); 210 if (reqURL != null) 211 return reqURL; 212 URL url = toSafeURL(super.getXSLurl(null), request); 213 return url.toExternalForm(); 214 } 215 216 225 public String [] getCatalog(HttpServletRequest request) 226 { 227 String temp[] = request.getParameterValues("catalog"); 228 if (DEFAULT_catalog == null) 229 return temp; 230 if (temp == null) 231 { 232 String defaultArray[] = new String [1]; 233 defaultArray[0] = DEFAULT_catalog; 234 return defaultArray; 235 } 236 int i, len = temp.length + 1; 237 String newCatalogs[] = new String [len]; 238 newCatalogs[0] = DEFAULT_catalog; 239 for (i=1; i < len; i++) 240 { 241 newCatalogs[i] = temp[i-1]; 242 } 243 return newCatalogs; 244 } 245 246 249 protected void setSystemProperties() 250 { 251 Properties props = new Properties (); 252 props.put("javax.xml.transform.TransformerFactory", 253 "org.apache.xalan.processor.TransformerFactoryImpl"); 254 props.put("javax.xml.parsers.DocumentBuilderFactory", 255 "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl"); 256 props.put("javax.xml.parsers.SAXParserFactory", 257 "org.apache.xerces.jaxp.SAXParserFactoryImpl"); 258 259 Properties systemProps = System.getProperties(); 260 Enumeration propEnum = props.propertyNames(); 261 while(propEnum.hasMoreElements()) 262 { 263 String prop = (String )propEnum.nextElement(); 264 if(!systemProps.containsKey(prop)) 265 systemProps.put(prop, props.getProperty(prop)); 266 } 267 System.setProperties(systemProps); 268 } 269 270 } 271 | Popular Tags |