001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.resource;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.wings.Resource;
018: import org.wings.externalizer.ExternalizeManager;
019: import org.wings.session.SessionManager;
020:
021: import java.io.IOException;
022: import java.io.InputStream;
023:
024: /**
025: * A ClassPathStylesheetResource is a static resource whose content is
026: * read from a classloader. It is special for its handling of occurences
027: * of "url([classPath])" strings. These are read from classPath and
028: * externalized.
029: *
030: * @author ole
031: * @version $$
032: */
033: public class ClassPathStylesheetResource extends ClassPathResource {
034: private final transient static Log log = LogFactory
035: .getLog(ClassPathStylesheetResource.class);
036: /**
037: * The default max size for the buffer. since we do some replacement every time we
038: * read the input stream, we want this to be high, so we can cache the
039: * interpreted style sheet.
040: */
041: private static final int MAX_BUFFER_SIZE = 24 * 1024; // 24kb
042:
043: private transient ExternalizeManager extManager;
044:
045: /**
046: * A static css resource that is obtained from the default classpath.
047: */
048: public ClassPathStylesheetResource(String resourceFileName) {
049: this (Thread.currentThread().getContextClassLoader(),
050: resourceFileName, "unkonwn");
051: }
052:
053: /**
054: * A static css resource that is obtained from the default classpath.
055: */
056: public ClassPathStylesheetResource(String resourceFileName,
057: String mimeType) {
058: this (Thread.currentThread().getContextClassLoader(),
059: resourceFileName, mimeType);
060: }
061:
062: /**
063: * A static css resource that is obtained from the specified class loader
064: *
065: * @param classLoader the classLoader from which the resource is obtained
066: * @param resourceFileName the css resource relative to the baseClass
067: */
068: public ClassPathStylesheetResource(ClassLoader classLoader,
069: String resourceFileName) {
070: this (classLoader, resourceFileName, "unknown");
071: }
072:
073: /**
074: * A static css resource that is obtained from the specified class loader
075: *
076: * @param classLoader the classLoader from which the resource is obtained
077: * @param resourceFileName the css resource relative to the baseClass
078: * @param mimeType the mimetype of the resource
079: */
080: public ClassPathStylesheetResource(ClassLoader classLoader,
081: String resourceFileName, String mimeType) {
082: this (classLoader, resourceFileName, mimeType, MAX_BUFFER_SIZE);
083: }
084:
085: /**
086: * A static css resource that is obtained from the specified class loader
087: *
088: * @param classLoader the classLoader from which the resource is obtained
089: * @param resourceFileName the css resource relative to the baseClass
090: * @param mimeType the mimetype of the resource
091: * @param maxBufferSize the maximum buffer size for the style sheet. If
092: * big enough, stylesheet is cached, else parsed again.
093: */
094: public ClassPathStylesheetResource(ClassLoader classLoader,
095: String resourceFileName, String mimeType, int maxBufferSize) {
096: super (classLoader, resourceFileName, mimeType);
097: // need to set it here, because at this moment there is a session in the sessionManager
098: extManager = SessionManager.getSession()
099: .getExternalizeManager();
100: // we need a bigger buffer, so that the parsing only happens once
101: setMaxBufferSize(maxBufferSize);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.wings.StaticResource#getResourceStream()
106: */
107: @Override
108: protected final InputStream getResourceStream()
109: throws ResourceNotFoundException {
110: InputStream in = getClassLoader().getResourceAsStream(
111: resourceFileName);
112: if (in != null) {
113: return new CssUrlFilterInputStream(in, getExtManager());
114: } else {
115: throw new ResourceNotFoundException(
116: "Unable to find classpath resource "
117: + resourceFileName);
118: }
119: }
120:
121: /*
122: * the equal() and hashCode() method make sure, that the same resources
123: * get the same name in the SystemExternalizer.
124: */
125:
126: /**
127: * Two ClassPathStylesheetResource are equal if both of them are instances
128: * of ClassPathStylesheetResource and the equals method of ClassPathResource
129: * is true.
130: *
131: * @return true if the two instances are equal.
132: */
133: public boolean equals(Object o) {
134: if (o instanceof ClassPathStylesheetResource) {
135: if (super .equals(o)) {
136: return true;
137: }
138: }
139: return false;
140: }
141:
142: /**
143: * Simple hascode implementation
144: */
145: public int hashCode() {
146: return super .hashCode();
147: }
148:
149: /* (non-Javadoc)
150: * @see org.wings.StaticResource#bufferResource()
151: */
152: @Override
153: protected LimitedBuffer bufferResource() throws IOException,
154: ResourceNotFoundException {
155: try {
156: return super .bufferResource();
157: } catch (IOException e) {
158: log.error("Unable to retrieve css file from classpath: '"
159: + resourceFileName);
160: throw e;
161: }
162: }
163:
164: private ExternalizeManager getExtManager() {
165: if (extManager == null)
166: // need to set it here, because at this moment there is a session in the sessionManager
167: extManager = SessionManager.getSession()
168: .getExternalizeManager();
169: return extManager;
170: }
171: }
|