1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 import org.apache.avalon.framework.component.Component; 20 import org.apache.avalon.framework.component.ComponentManager; 21 import org.apache.cocoon.components.source.SourceUtil; 22 import org.apache.cocoon.environment.Context; 23 import org.apache.cocoon.environment.ObjectModelHelper; 24 import org.apache.cocoon.environment.Session; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.cocoon.util.NetUtils; 27 import org.apache.cocoon.xml.IncludeXMLConsumer; 28 import org.apache.commons.lang.StringEscapeUtils; 29 import org.apache.commons.lang.StringUtils; 30 import org.apache.excalibur.source.Source; 31 import org.apache.excalibur.xml.sax.SAXParser; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.InputSource ; 34 35 import java.io.File ; 36 import java.io.FileInputStream ; 37 import java.io.FileReader ; 38 import java.io.IOException ; 39 import java.io.InputStream ; 40 import java.io.InputStreamReader ; 41 import java.io.Reader ; 42 import java.io.StringReader ; 43 import java.io.FileNotFoundException ; 44 import java.net.URLDecoder ; 45 import java.net.URLEncoder ; 46 import java.net.URL ; 47 import java.text.SimpleDateFormat ; 48 import java.util.Date ; 49 import java.util.Map ; 50 51 57 public class XSPUtil { 58 59 public static String pathComponent(String filename) { 60 int i = filename.lastIndexOf(File.separator); 61 return (i >= 0) ? filename.substring(0, i) : filename; 62 } 63 64 public static String fileComponent(String filename) { 65 int i = filename.lastIndexOf(File.separator); 66 return (i >= 0) ? filename.substring(i + 1) : filename; 67 } 68 69 public static String baseName(String filename) { 70 return baseName(filename, "."); 71 } 72 73 public static String baseName(String filename, String suffix) { 74 int lastDot = filename.lastIndexOf(suffix); 75 if (lastDot >= 0) { 76 filename = filename.substring(0, lastDot); 77 } 78 return filename; 79 } 80 81 public static String normalizedBaseName(String filename) { 82 filename = baseName(filename); 83 return normalizedName(filename); 84 } 85 86 public static String normalizedName(String filename) { 87 String [] path = split(filename, File.separator); 88 int start = (path[0].length() == 0) ? 1 : 0; 89 StringBuffer buffer = new StringBuffer (); 90 for (int i = start; i < path.length; i++) { 91 if (i > start) { 92 buffer.append(File.separator); 93 } 94 buffer.append('_'); 95 char[] chars = path[i].toCharArray(); 96 for (int j = 0; j < chars.length; j++) { 97 if (isAlphaNumeric(chars[j])) { 98 buffer.append(chars[j]); 99 } else { 100 buffer.append('_'); 101 } 102 } 103 } 104 return buffer.toString(); 105 } 106 107 public static String relativeFilename(String filename, Map objectModel) throws IOException { 108 File file = new File (filename); 109 if (file.isAbsolute() && file.exists()) { 110 return filename; 111 } 112 Context context = ObjectModelHelper.getContext(objectModel); 113 URL resource = context.getResource(filename); 114 if (resource == null) { 115 throw new FileNotFoundException ("The file " + filename + " does not exist!"); 116 } 117 return NetUtils.getPath(resource.toExternalForm()); 118 } 119 120 public static boolean isAlphaNumeric(char c) { 121 return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); 122 } 123 124 public static String [] split(String line) { 125 return StringUtils.split(line, " \t\r\f\n"); 126 } 127 128 public static String [] split(String line, String delimiter) { 129 return StringUtils.split(line, delimiter); 130 } 131 132 public static String encodeMarkup(String string) { 133 return StringEscapeUtils.escapeXml(string); 134 } 135 136 public static String formEncode(String text) throws Exception { 137 return URLEncoder.encode(text); 138 } 139 140 public static String formDecode(String s) throws Exception { 142 return URLDecoder.decode(s); 143 } 144 145 146 147 public static String formatDate(Date date, String pattern) { 149 if (StringUtils.isEmpty(pattern)) { 150 pattern = "yyyy/MM/dd hh:mm:ss aa"; 151 } 152 try { 153 return (new SimpleDateFormat (pattern)).format(date); 154 } catch (Exception e) { 155 return date.toString(); 156 } 157 } 158 159 private static volatile int count = 0; 161 162 public static int getCount() { 163 return ++count; 164 } 165 166 public static int getSessionCount(Session session) { 167 synchronized (session) { 168 Integer integer = (Integer )session.getAttribute("util.counter"); 169 if (integer == null) { 170 integer = new Integer (0); 171 } 172 int cnt = integer.intValue() + 1; 173 session.setAttribute("util.counter", new Integer (cnt)); 174 return cnt; 175 } 176 } 177 178 public static Object getContextAttribute(Map objectModel, String name) { 179 Context context = ObjectModelHelper.getContext(objectModel); 180 return context.getAttribute(name); 181 } 182 183 public static String getSourceContents(String url, SourceResolver resolver) throws IOException { 185 Source source = resolver.resolveURI(url); 186 try { 187 return getContents(source.getInputStream()); 188 } finally { 189 resolver.release(source); 190 } 191 } 192 193 public static String getSourceContents(String uri, String base, SourceResolver resolver) throws IOException { 194 if (StringUtils.isEmpty(base)) { 195 base = null; 196 } 197 Source source = resolver.resolveURI(uri, base, null); 198 try { 199 return getContents(source.getInputStream()); 200 } finally { 201 resolver.release(source); 202 } 203 } 204 205 public static String getFileContents(String filename) throws IOException { 206 return getContents(new FileReader (filename)); 207 } 208 209 public static String getFileContents(String filename, String encoding) throws IOException { 210 return getContents(new FileInputStream (filename), encoding); 211 } 212 213 public static String getContents(InputStream in, String encoding) throws IOException { 214 return getContents(new InputStreamReader (in, encoding)); 215 } 216 217 public static String getContents(InputStream in) throws IOException { 218 return getContents(new InputStreamReader (in)); 219 } 220 221 public static String getContents(Reader reader) throws IOException { 222 int len; 223 char[] chr = new char[4096]; 224 StringBuffer buffer = new StringBuffer (); 225 try { 226 while ((len = reader.read(chr)) > 0) { 227 buffer.append(chr, 0, len); 228 } 229 } finally { 230 reader.close(); 231 } 232 return buffer.toString(); 233 } 234 235 public static void includeSource(String uri, String base, SourceResolver resolver, ContentHandler contentHandler) 236 throws RuntimeException { 237 if (StringUtils.isEmpty(base)) { 238 base = null; 239 } 240 Source source = null; 241 try { 242 source = resolver.resolveURI(uri, base, null); 243 SourceUtil.toSAX(source, new IncludeXMLConsumer(contentHandler)); 244 } catch (Exception e) { 245 throw new CascadingRuntimeException("Error including source " + base + " " + uri, e); 246 } finally { 247 if (source != null) { 248 resolver.release(source); 249 } 250 } 251 } 252 253 public static void includeString(String string, ComponentManager manager, ContentHandler contentHandler) 254 throws RuntimeException { 255 XSPUtil.includeInputSource(new InputSource (new StringReader (String.valueOf(string))), manager, 256 contentHandler); 257 } 258 259 public static void includeFile(String name, ComponentManager manager, ContentHandler contentHandler, Map objectModel) 260 throws RuntimeException { 261 try { 262 XSPUtil.includeInputSource(new InputSource (new FileReader (XSPUtil.relativeFilename(name, objectModel))), 263 manager, contentHandler); 264 } catch (IOException e) { 265 throw new CascadingRuntimeException("Could not include file " + name, e); 266 } 267 } 268 269 public static void includeInputSource(InputSource source, ComponentManager manager, ContentHandler contentHandler) 270 throws RuntimeException { 271 SAXParser parser = null; 272 try { 273 parser = (SAXParser)manager.lookup(SAXParser.ROLE); 274 IncludeXMLConsumer consumer = new IncludeXMLConsumer(contentHandler); 275 parser.parse(source, consumer, consumer); 276 } catch (Exception e) { 277 throw new CascadingRuntimeException("Could not include page", e); 278 } finally { 279 if (parser != null) { 280 manager.release((Component)parser); 281 } 282 } 283 } 284 } 285 | Popular Tags |