KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > resource > ClassPathStylesheetResource


1 /*
2  * $Id: ClassPathStylesheetResource.java,v 1.5 2005/05/13 16:27:15 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.resource;
15
16 import org.apache.commons.logging.Log;
17 import org.apache.commons.logging.LogFactory;
18 import org.wings.Resource;
19 import org.wings.externalizer.ExternalizeManager;
20 import org.wings.session.SessionManager;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 /**
26  * A ClassPathStylesheetResource is a static resource whose content is
27  * read from a classloader. It is special for its handling of occurences
28  * of "url([classPath])" strings. These are read from classPath and
29  * externalized.
30  *
31  * @author ole
32  * @version $$
33  */

34 public class ClassPathStylesheetResource
35         extends ClasspathResource {
36     private final transient static Log log = LogFactory.getLog(ClassPathStylesheetResource.class);
37     /**
38      * The default max size for the buffer. since we do some replacement every time we
39      * read the input stream, we want this to be high, so we can cache the
40      * interpreted style sheet.
41      */

42     private static final int MAX_BUFFER_SIZE = 24 * 1024; // 24kb
43

44     private ExternalizeManager extManager;
45
46     /**
47      * A static css resource that is obtained from the default classpath.
48      */

49     public ClassPathStylesheetResource(String JavaDoc resourceFileName) {
50         this(Resource.class.getClassLoader(), resourceFileName, "unkonwn");
51     }
52
53     /**
54      * A static css resource that is obtained from the default classpath.
55      */

56     public ClassPathStylesheetResource(String JavaDoc resourceFileName, String JavaDoc mimeType) {
57         this(Resource.class.getClassLoader(), resourceFileName, mimeType);
58     }
59
60     /**
61      * A static css resource that is obtained from the specified class loader
62      *
63      * @param classLoader the classLoader from which the resource is obtained
64      * @param resourceFileName the css resource relative to the baseClass
65      */

66     public ClassPathStylesheetResource(ClassLoader JavaDoc classLoader, String JavaDoc resourceFileName) {
67         this(classLoader, resourceFileName, "unknown");
68     }
69
70     /**
71      * A static css resource that is obtained from the specified class loader
72      *
73      * @param classLoader the classLoader from which the resource is obtained
74      * @param resourceFileName the css resource relative to the baseClass
75      * @param mimeType the mimetype of the resource
76      */

77     public ClassPathStylesheetResource(ClassLoader JavaDoc classLoader, String JavaDoc resourceFileName, String JavaDoc mimeType) {
78         this(classLoader, resourceFileName, mimeType, MAX_BUFFER_SIZE);
79     }
80
81     /**
82      * A static css resource that is obtained from the specified class loader
83      *
84      * @param classLoader the classLoader from which the resource is obtained
85      * @param resourceFileName the css resource relative to the baseClass
86      * @param mimeType the mimetype of the resource
87      * @param maxBufferSize the maximum buffer size for the style sheet. If
88      * big enough, stylesheet is cached, else parsed again.
89      */

90     public ClassPathStylesheetResource(ClassLoader JavaDoc classLoader, String JavaDoc resourceFileName, String JavaDoc mimeType, int maxBufferSize) {
91         super(classLoader, resourceFileName, mimeType);
92         // need to set it here, because at this moment there is a session in the sessionManager
93
extManager = SessionManager.getSession().getExternalizeManager();
94         // we need a bigger buffer, so that the parsing only happens once
95
setMaxBufferSize(maxBufferSize);
96     }
97
98     /* (non-Javadoc)
99      * @see org.wings.StaticResource#getResourceStream()
100      */

101     protected final InputStream JavaDoc getResourceStream() {
102         InputStream JavaDoc in = classLoader.getResourceAsStream(resourceFileName);
103         CssUrlFilterInputStream stream = new CssUrlFilterInputStream(in, extManager);
104         return stream;
105     }
106
107     /*
108      * the equal() and hashCode() method make sure, that the same resources
109      * get the same name in the SystemExternalizer.
110      */

111
112     /**
113      * Two ClassPathStylesheetResource are equal if both of them are instances
114      * of ClassPathStylesheetResource and the equals method of ClasspathResource
115      * is true.
116      *
117      * @return true if the two instances are equal.
118      */

119     public boolean equals(Object JavaDoc o) {
120         if (o instanceof ClassPathStylesheetResource) {
121             if (super.equals(o)) {
122                 return true;
123             }
124         }
125         return false;
126     }
127
128     /* (non-Javadoc)
129      * @see org.wings.StaticResource#bufferResource()
130      */

131     protected LimitedBuffer bufferResource() throws IOException JavaDoc {
132         try {
133             return super.bufferResource();
134         } catch (IOException JavaDoc e) {
135             log.error("Unable to retrieve css file from classpath: '"+resourceFileName);
136             throw e;
137         }
138     }
139 }
140
141
142
Popular Tags